int bind_params(sqlite3_stmt* stmt, const mxArray *params, int column) { TYPECHECK(params, mxSTRUCT_CLASS); int i, n = mxGetNumberOfFields(params); for (i = 0; i < n; i++) { mxArray *array = mxGetFieldByNumber(params, column, i); mxClassID cls = mxGetClassID(array); int res; switch (cls) { case mxFUNCTION_CLASS: break; case mxCHAR_CLASS: res = bind_string(stmt, i + 1, array); break; case mxSINGLE_CLASS: case mxDOUBLE_CLASS: res = bind_double(stmt, i + 1, array); break; default: /* anything else is an integer */ res = bind_int64(stmt, i + 1, array); } if (res != SQLITE_OK) { return res; } } return SQLITE_OK; }
DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) : _sdi(sdi) { GVariant *gvar_opts, *gvar_list; gsize num_opts; if ((sr_config_list(sdi->driver, sdi, NULL, SR_CONF_DEVICE_CONFIGS, &gvar_opts) != SR_OK)) /* Driver supports no device instance options. */ return; const int *const options = (const int32_t *)g_variant_get_fixed_array( gvar_opts, &num_opts, sizeof(int32_t)); for (unsigned int i = 0; i < num_opts; i++) { const struct sr_config_info *const info = sr_config_info_get(options[i]); if (!info) continue; const int key = info->key; if(sr_config_list(_sdi->driver, _sdi, NULL, key, &gvar_list) != SR_OK) gvar_list = NULL; const QString name(info->label); switch(key) { case SR_CONF_SAMPLERATE: bind_samplerate(name, gvar_list); break; case SR_CONF_CAPTURE_RATIO: bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100)); break; case SR_CONF_PATTERN_MODE: case SR_CONF_BUFFERSIZE: case SR_CONF_TRIGGER_SOURCE: case SR_CONF_FILTER: case SR_CONF_COUPLING: case SR_CONF_EN_CH: case SR_CONF_OPERATION_MODE: case SR_CONF_THRESHOLD: case SR_CONF_ZERO: case SR_CONF_STREAM: case SR_CONF_TEST: case SR_CONF_STATUS: case SR_CONF_FACTOR: bind_enum(name, key, gvar_list); break; case SR_CONF_VTH: bind_double(name, key, "V", pair<double, double>(0.0, 5.0), 1, 0.1); break; case SR_CONF_RLE: bind_bool(name, key); break; case SR_CONF_CLOCK_TYPE: case SR_CONF_CLOCK_EDGE: case SR_CONF_INSTANT: case SR_CONF_DATALOCK: bind_bool(name, key); break; case SR_CONF_TIMEBASE: bind_enum(name, key, gvar_list, print_timebase); break; case SR_CONF_VDIV: bind_enum(name, key, gvar_list, print_vdiv); break; default: gvar_list = NULL; } if (gvar_list) g_variant_unref(gvar_list); } g_variant_unref(gvar_opts); }
static int add_log(const char *table, const opal_value_t *kvs, int nkvs) { int i, rc; char *sql, **cmd = NULL, *tmp; sqlite3_stmt *stmt; opal_output_verbose(2, opal_db_base_framework.framework_output, "Logging data for table %s", table); /* setup the insert statement */ for (i=0; i < nkvs; i++) { opal_argv_append_nosize(&cmd, "?"); } tmp = opal_argv_join(cmd, ','); asprintf(&sql, "INSERT INTO %s VALUES (%s)", table, tmp); free(tmp); opal_argv_free(cmd); /* use the next worker thread */ OPAL_SQLITE_CMD(prepare_v2(dbhandles[active], sql, strlen(sql)+1, &stmt, NULL), dbhandles[active], &rc); if (SQLITE_OK != rc) { return OPAL_ERROR; } /* cycle through the provided values and construct * an insert command for them - note that the values * MUST be in column-order for the database! */ for (i=0; i < nkvs; i++) { switch (kvs[i].type) { case OPAL_STRING: OPAL_SQLITE_CMD(bind_text(stmt, i, kvs[i].data.string, strlen(kvs[i].data.string), NULL), dbhandles[active], &rc); break; case OPAL_INT32: OPAL_SQLITE_CMD(bind_int(stmt, i, kvs[i].data.int32), dbhandles[active], &rc); break; case OPAL_INT16: OPAL_SQLITE_CMD(bind_int(stmt, i, kvs[i].data.int16), dbhandles[active], &rc); break; case OPAL_PID: OPAL_SQLITE_CMD(bind_int64(stmt, i, kvs[i].data.pid), dbhandles[active], &rc); break; case OPAL_INT64: OPAL_SQLITE_CMD(bind_int64(stmt, i, kvs[i].data.int64), dbhandles[active], &rc); break; case OPAL_FLOAT: OPAL_SQLITE_CMD(bind_double(stmt, i, kvs[i].data.fval), dbhandles[active], &rc); break; case OPAL_TIMEVAL: asprintf(&tmp, "%d.%06d", (int)kvs[i].data.tv.tv_sec, (int)kvs[i].data.tv.tv_usec); OPAL_SQLITE_CMD(bind_text(stmt, i, tmp, strlen(tmp), NULL), dbhandles[active], &rc); free(tmp); break; } if (SQLITE_OK != rc) { return OPAL_ERROR; } } OPAL_SQLITE_OP(step(stmt), DONE, dbhandles[active], &rc); if (SQLITE_OK != rc) { return OPAL_ERROR; } opal_output_verbose(2, opal_db_base_framework.framework_output, "INSERTED ROW %d", (int)sqlite3_last_insert_rowid(dbhandles[active])); /* cycle to the next worker thread */ active++; if (nthreads < active) { active = 0; } return OPAL_SUCCESS; }
static int store(struct orcm_db_base_module_t *imod, const char *primary_key, opal_list_t *kvs) { int i, rc; char *sql, **cmd = NULL, *tmp; sqlite3_stmt *stmt; opal_value_t *kv; mca_db_sqlite_module_t *mod = (mca_db_sqlite_module_t*)imod; /* setup the insert statement */ for (i=0; i < (int)opal_list_get_size(kvs); i++) { opal_argv_append_nosize(&cmd, "?"); } tmp = opal_argv_join(cmd, ','); asprintf(&sql, "INSERT INTO %s VALUES (%s)", mod->dbfile, tmp); free(tmp); opal_argv_free(cmd); /* use the next worker thread */ ORCM_SQLITE_CMD(prepare_v2(mod->dbhandles[mod->active], sql, strlen(sql)+1, &stmt, NULL), mod->dbhandles[mod->active], &rc); if (SQLITE_OK != rc) { return ORCM_ERROR; } /* cycle through the provided values and construct * an insert command for them - note that the values * MUST be in column-order for the database! */ OPAL_LIST_FOREACH(kv, kvs, opal_value_t) { switch (kv->type) { case OPAL_STRING: ORCM_SQLITE_CMD(bind_text(stmt, i, kv->data.string, strlen(kv->data.string), NULL), mod->dbhandles[mod->active], &rc); break; case OPAL_INT32: ORCM_SQLITE_CMD(bind_int(stmt, i, kv->data.int32), mod->dbhandles[mod->active], &rc); break; case OPAL_INT16: ORCM_SQLITE_CMD(bind_int(stmt, i, kv->data.int16), mod->dbhandles[mod->active], &rc); break; case OPAL_PID: ORCM_SQLITE_CMD(bind_int64(stmt, i, kv->data.pid), mod->dbhandles[mod->active], &rc); break; case OPAL_INT64: ORCM_SQLITE_CMD(bind_int64(stmt, i, kv->data.int64), mod->dbhandles[mod->active], &rc); break; case OPAL_FLOAT: ORCM_SQLITE_CMD(bind_double(stmt, i, kv->data.fval), mod->dbhandles[mod->active], &rc); break; case OPAL_TIMEVAL: asprintf(&tmp, "%d.%06d", (int)kv->data.tv.tv_sec, (int)kv->data.tv.tv_usec); ORCM_SQLITE_CMD(bind_text(stmt, i, tmp, strlen(tmp), NULL), mod->dbhandles[mod->active], &rc); free(tmp); break; } if (SQLITE_OK != rc) { return ORCM_ERROR; } } ORCM_SQLITE_OP(step(stmt), DONE, mod->dbhandles[mod->active], &rc); if (SQLITE_OK != rc) { return ORCM_ERROR; } opal_output_verbose(2, orcm_db_base_framework.framework_output, "INSERTED ROW %d", (int)sqlite3_last_insert_rowid(mod->dbhandles[mod->active])); /* cycle to the next worker thread */ mod->active++; if (mod->nthreads < mod->active) { mod->active = 0; } return ORCM_SUCCESS; }