예제 #1
0
파일: cnuodb.cpp 프로젝트: tilinna/go-nuodb
int nuodb_statement_bind(struct nuodb *db, struct nuodb_statement *st,
                         struct nuodb_value parameters[]) {
    PreparedStatement *stmt = reinterpret_cast<PreparedStatement *>(st);
    try {
        int parameterCount = stmt->getParameterMetaData()->getParameterCount();
        for (int i=0; i < parameterCount; ++i) {
            int parameterIndex = i+1;
            switch (parameters[i].vt) {
                case NUODB_TYPE_NULL:
                    stmt->setNull(parameterIndex, NUOSQL_NULL);
                    break;
                case NUODB_TYPE_INT64:
                    stmt->setLong(parameterIndex, parameters[i].i64);
                    break;
                case NUODB_TYPE_FLOAT64: {
                    union {
                        int64_t i64;
                        double float64;
                    } value = { parameters[i].i64 };
                    stmt->setDouble(parameterIndex, value.float64);
                    break;
                }
                case NUODB_TYPE_BOOL:
                    stmt->setBoolean(parameterIndex, !!parameters[i].i64);
                    break;
                case NUODB_TYPE_STRING: {
                    size_t length = parameters[i].i32;
                    const char *s = reinterpret_cast<const char*>(parameters[i].i64);
                    // Extra conversion due to missing length param in the setString API
                    const std::string str(s, length);
                    stmt->setString(parameterIndex, str.c_str());
                    break;
                }
                case NUODB_TYPE_BYTES: {
                    int length = parameters[i].i32;
                    const unsigned char *bytes = reinterpret_cast<const unsigned char*>(parameters[i].i64);
                    stmt->setBytes(parameterIndex, length, bytes);
                    break;
                }
                case NUODB_TYPE_TIME: {
                    int64_t seconds = parameters[i].i64;
                    int32_t nanos = parameters[i].i32;
                    SqlTimestamp ts(seconds, nanos);
                    stmt->setTimestamp(parameterIndex, &ts);
                    break;
                }
            }
        }
        return 0;
    } catch  (SQLException &e) {
        return setError(db, e);
    }
}
예제 #2
0
파일: cnuodb.cpp 프로젝트: tilinna/go-nuodb
int nuodb_statement_prepare(struct nuodb *db, const char *sql,
                            struct nuodb_statement **st, int *parameter_count) {
    PreparedStatement *stmt = 0;
    try {
        stmt = db->conn->prepareStatement(sql, RETURN_GENERATED_KEYS);
        *parameter_count = stmt->getParameterMetaData()->getParameterCount();
        *st = reinterpret_cast<struct nuodb_statement *>(stmt);
        return 0;
    } catch (SQLException &e) {
        if (stmt) {
            stmt->close();
        }
        return setError(db, e);
    }
}