struct dlr_storage *dlr_init_sdb(Cfg* cfg) { CfgGroup *grp; List *grplist; Octstr *sdb_url, *sdb_id; Octstr *p = NULL; long pool_size; DBConf *db_conf = NULL; /* * check for all mandatory directives that specify the field names * of the used table */ if (!(grp = cfg_get_single_group(cfg, octstr_imm("dlr-db")))) panic(0, "DLR: SDB: group 'dlr-db' is not specified!"); if (!(sdb_id = cfg_get(grp, octstr_imm("id")))) panic(0, "DLR: SDB: directive 'id' is not specified!"); fields = dlr_db_fields_create(grp); gw_assert(fields != NULL); /* * now grap the required information from the 'mysql-connection' group * with the sdb-id we just obtained * * we have to loop through all available SDB connection definitions * and search for the one we are looking for */ grplist = cfg_get_multi_group(cfg, octstr_imm("sdb-connection")); while (grplist && (grp = gwlist_extract_first(grplist)) != NULL) { p = cfg_get(grp, octstr_imm("id")); if (p != NULL && octstr_compare(p, sdb_id) == 0) { goto found; } if (p != NULL) octstr_destroy(p); } panic(0, "DLR: SDB: connection settings for id '%s' are not specified!", octstr_get_cstr(sdb_id)); found: octstr_destroy(p); gwlist_destroy(grplist, NULL); if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1 || pool_size == 0) pool_size = 1; if (!(sdb_url = cfg_get(grp, octstr_imm("url")))) panic(0, "DLR: SDB: directive 'url' is not specified!"); if (octstr_search(sdb_url, octstr_imm("oracle:"), 0) == 0) sdb_conn_type = SDB_ORACLE; else if (octstr_search(sdb_url, octstr_imm("mysql:"), 0) == 0) { warning(0, "DLR[sdb]: Please use native MySQL support, instead of libsdb."); sdb_conn_type = SDB_MYSQL; } else if (octstr_search(sdb_url, octstr_imm("postgres:"), 0) == 0) { sdb_conn_type = SDB_POSTGRES; } else sdb_conn_type = SDB_OTHER; /* * ok, ready to connect */ info(0,"Connecting to sdb resource <%s>.", octstr_get_cstr(sdb_url)); db_conf = gw_malloc(sizeof(DBConf)); gw_assert(db_conf != NULL); db_conf->sdb = gw_malloc(sizeof(SDBConf)); gw_assert(db_conf->sdb != NULL); db_conf->sdb->url = sdb_url; pool = dbpool_create(DBPOOL_SDB, db_conf, pool_size); gw_assert(pool != NULL); /* * XXX should a failing connect throw panic?! */ if (dbpool_conn_count(pool) == 0) panic(0,"DLR: SDB: database pool has no connections!"); return &handles; }
struct dlr_storage *dlr_init_redis(Cfg *cfg) { CfgGroup *grp; List *grplist; Octstr *redis_host, *redis_pass, *redis_id; long redis_port = 0, redis_database = -1, redis_idle_timeout = -1; Octstr *p = NULL; long pool_size; DBConf *db_conf = NULL; /* * Check for all mandatory directives that specify the field names * of the used Redis key */ if (!(grp = cfg_get_single_group(cfg, octstr_imm("dlr-db")))) panic(0, "DLR: Redis: group 'dlr-db' is not specified!"); if (!(redis_id = cfg_get(grp, octstr_imm("id")))) panic(0, "DLR: Redis: directive 'id' is not specified!"); fields = dlr_db_fields_create(grp); gw_assert(fields != NULL); /* * Escaping special quotes for field/table names */ octstr_replace(fields->table, octstr_imm("`"), octstr_imm("``")); octstr_replace(fields->field_smsc, octstr_imm("`"), octstr_imm("``")); octstr_replace(fields->field_ts, octstr_imm("`"), octstr_imm("``")); octstr_replace(fields->field_src, octstr_imm("`"), octstr_imm("``")); octstr_replace(fields->field_dst, octstr_imm("`"), octstr_imm("``")); octstr_replace(fields->field_serv, octstr_imm("`"), octstr_imm("``")); octstr_replace(fields->field_url, octstr_imm("`"), octstr_imm("``")); octstr_replace(fields->field_mask, octstr_imm("`"), octstr_imm("``")); octstr_replace(fields->field_status, octstr_imm("`"), octstr_imm("``")); octstr_replace(fields->field_boxc, octstr_imm("`"), octstr_imm("``")); /* * Now grab the required information from the 'redis-connection' group * with the redis-id we just obtained. * * We have to loop through all available Redis connection definitions * and search for the one we are looking for. */ grplist = cfg_get_multi_group(cfg, octstr_imm("redis-connection")); while (grplist && (grp = gwlist_extract_first(grplist)) != NULL) { p = cfg_get(grp, octstr_imm("id")); if (p != NULL && octstr_compare(p, redis_id) == 0) { goto found; } if (p != NULL) octstr_destroy(p); } panic(0, "DLR: Redis: connection settings for id '%s' are not specified!", octstr_get_cstr(redis_id)); found: octstr_destroy(p); gwlist_destroy(grplist, NULL); if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1 || pool_size == 0) pool_size = 1; if (!(redis_host = cfg_get(grp, octstr_imm("host")))) panic(0, "DLR: Redis: directive 'host' is not specified!"); if (cfg_get_integer(&redis_port, grp, octstr_imm("port")) == -1) panic(0, "DLR: Redis: directive 'port' is not specified!"); redis_pass = cfg_get(grp, octstr_imm("password")); cfg_get_integer(&redis_database, grp, octstr_imm("database")); cfg_get_integer(&redis_idle_timeout, grp, octstr_imm("idle-timeout")); /* * Ok, ready to connect to Redis */ db_conf = gw_malloc(sizeof(DBConf)); gw_assert(db_conf != NULL); db_conf->redis = gw_malloc(sizeof(RedisConf)); gw_assert(db_conf->redis != NULL); db_conf->redis->host = redis_host; db_conf->redis->port = redis_port; db_conf->redis->password = redis_pass; db_conf->redis->database = redis_database; db_conf->redis->idle_timeout = redis_idle_timeout; pool = dbpool_create(DBPOOL_REDIS, db_conf, pool_size); gw_assert(pool != NULL); /* * Panic on failure to connect. Should we just try to reconnect? */ if (dbpool_conn_count(pool) == 0) panic(0,"DLR: Redis: database pool has no connections!"); octstr_destroy(redis_id); return &handles; }
struct dlr_storage *dlr_init_pgsql(Cfg *cfg) { CfgGroup *grp; List *grplist; Octstr *pgsql_host, *pgsql_user, *pgsql_pass, *pgsql_db, *pgsql_id; long pgsql_port = 0; Octstr *p = NULL; long pool_size; DBConf *db_conf = NULL; /* * check for all mandatory directives that specify the field names * of the table used */ if (!(grp = cfg_get_single_group(cfg, octstr_imm("dlr-db")))) panic(0, "DLR: PgSQL: group 'dlr-db' is not specified!"); if (!(pgsql_id = cfg_get(grp, octstr_imm("id")))) panic(0, "DLR: PgSQL: directive 'id' is not specified!"); fields = dlr_db_fields_create(grp); gw_assert(fields != NULL); /* * now grap the required information from the 'pgsql-connection' group * with the pgsql-id we just obtained * * we have to loop through all available PostgreSQL connection definitions * and search for the one we are looking for */ grplist = cfg_get_multi_group(cfg, octstr_imm("pgsql-connection")); while (grplist && (grp = gwlist_extract_first(grplist)) != NULL) { p = cfg_get(grp, octstr_imm("id")); if (p != NULL && octstr_compare(p, pgsql_id) == 0) { goto found; } if (p != NULL) octstr_destroy(p); } panic(0, "DLR: PgSQL: connection settings for id '%s' are not specified!", octstr_get_cstr(pgsql_id)); found: octstr_destroy(p); gwlist_destroy(grplist, NULL); if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1 || pool_size == 0) pool_size = 1; if (!(pgsql_host = cfg_get(grp, octstr_imm("host")))) panic(0, "DLR: PgSQL: directive 'host' is not specified!"); if (!(pgsql_user = cfg_get(grp, octstr_imm("username")))) panic(0, "DLR: PgSQL: directive 'username' is not specified!"); if (!(pgsql_pass = cfg_get(grp, octstr_imm("password")))) panic(0, "DLR: PgSQL: directive 'password' is not specified!"); if (!(pgsql_db = cfg_get(grp, octstr_imm("database")))) panic(0, "DLR: PgSQL: directive 'database' is not specified!"); cfg_get_integer(&pgsql_port, grp, octstr_imm("port")); /* optional */ /* * ok, ready to connect to the database */ db_conf = gw_malloc(sizeof(DBConf)); gw_assert(db_conf != NULL); db_conf->pgsql = gw_malloc(sizeof(PgSQLConf)); gw_assert(db_conf->pgsql != NULL); db_conf->pgsql->host = pgsql_host; db_conf->pgsql->port = pgsql_port; db_conf->pgsql->username = pgsql_user; db_conf->pgsql->password = pgsql_pass; db_conf->pgsql->database = pgsql_db; pool = dbpool_create(DBPOOL_PGSQL, db_conf, pool_size); gw_assert(pool != NULL); /* * XXX should a failing connect throw panic?! */ if (dbpool_conn_count(pool) == 0) panic(0,"DLR: PgSQL: database pool has no connections!"); octstr_destroy(pgsql_id); return &handles; }
struct dlr_storage *dlr_init_sqlite3(Cfg *cfg) { CfgGroup *grp; List *grplist; long pool_size; DBConf *db_conf = NULL; Octstr *id, *file; int found; if ((grp = cfg_get_single_group(cfg, octstr_imm("dlr-db"))) == NULL) panic(0, "DLR: SQLite3: group 'dlr-db' is not specified!"); if (!(id = cfg_get(grp, octstr_imm("id")))) panic(0, "DLR: SQLite3: directive 'id' is not specified!"); /* initialize database fields */ fields = dlr_db_fields_create(grp); gw_assert(fields != NULL); grplist = cfg_get_multi_group(cfg, octstr_imm("sqlite3-connection")); found = 0; while (grplist && (grp = gwlist_extract_first(grplist)) != NULL) { Octstr *p = cfg_get(grp, octstr_imm("id")); if (p != NULL && octstr_compare(p, id) == 0) { found = 1; } if (p != NULL) octstr_destroy(p); if (found == 1) break; } gwlist_destroy(grplist, NULL); if (found == 0) panic(0, "DLR: SQLite3: connection settings for id '%s' are not specified!", octstr_get_cstr(id)); file = cfg_get(grp, octstr_imm("database")); if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1) pool_size = 1; if (file == NULL) panic(0, "DLR: SQLite3: connection settings missing for id '%s', please" " check you configuration.",octstr_get_cstr(id)); /* ok we are ready to create dbpool */ db_conf = gw_malloc(sizeof(*db_conf)); db_conf->sqlite3 = gw_malloc(sizeof(SQLite3Conf)); db_conf->sqlite3->file = file; pool = dbpool_create(DBPOOL_SQLITE3, db_conf, pool_size); gw_assert(pool != NULL); if (dbpool_conn_count(pool) == 0) panic(0, "DLR: SQLite3: Could not establish sqlite3 connection(s)."); octstr_destroy(id); return &handles; }
struct dlr_storage *dlr_init_oracle(Cfg *cfg) { CfgGroup *grp; List *grplist; long pool_size; DBConf *db_conf = NULL; Octstr *id, *username, *password, *tnsname; int found; if ((grp = cfg_get_single_group(cfg, octstr_imm("dlr-db"))) == NULL) panic(0, "DLR: ORACLE: group 'dlr-db' is not specified!"); if (!(id = cfg_get(grp, octstr_imm("id")))) panic(0, "DLR: ORACLE: directive 'id' is not specified!"); /* initialize database fields */ fields = dlr_db_fields_create(grp); gw_assert(fields != NULL); grplist = cfg_get_multi_group(cfg, octstr_imm("oracle-connection")); found = 0; while (grplist && (grp = gwlist_extract_first(grplist)) != NULL) { Octstr *p = cfg_get(grp, octstr_imm("id")); if (p != NULL && octstr_compare(p, id) == 0) { found = 1; } if (p != NULL) octstr_destroy(p); if (found == 1) break; } gwlist_destroy(grplist, NULL); if (found == 0) panic(0, "DLR: ORACLE: connection settings for id '%s' are not specified!", octstr_get_cstr(id)); username = cfg_get(grp, octstr_imm("username")); password = cfg_get(grp, octstr_imm("password")); tnsname = cfg_get(grp, octstr_imm("tnsname")); if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1) pool_size = 1; if (username == NULL || password == NULL || tnsname == NULL) panic(0, "DLR: ORACLE: connection settings missing for id '%s', please" " check you configuration.",octstr_get_cstr(id)); /* ok we are ready to create dbpool */ db_conf = gw_malloc(sizeof(*db_conf)); db_conf->oracle = gw_malloc(sizeof(OracleConf)); db_conf->oracle->username = username; db_conf->oracle->password = password; db_conf->oracle->tnsname = tnsname; pool = dbpool_create(DBPOOL_ORACLE, db_conf, pool_size); gw_assert(pool != NULL); if (dbpool_conn_count(pool) == 0) panic(0, "DLR: ORACLE: Couldnot establish oracle connection(s)."); octstr_destroy(id); return &handles; }
struct server_type *sqlbox_init_mysql(Cfg* cfg) { CfgGroup *grp; List *grplist; Octstr *mysql_host, *mysql_user, *mysql_pass, *mysql_db, *mysql_id; Octstr *p = NULL; long pool_size, mysql_port; int have_port; DBConf *db_conf = NULL; struct server_type *res = NULL; /* * check for all mandatory directives that specify the field names * of the used MySQL table */ if (!(grp = cfg_get_single_group(cfg, octstr_imm("sqlbox")))) panic(0, "SQLBOX: MySQL: group 'sqlbox' is not specified!"); if (!(mysql_id = cfg_get(grp, octstr_imm("id")))) panic(0, "SQLBOX: MySQL: directive 'id' is not specified!"); /* * now grap the required information from the 'mysql-connection' group * with the mysql-id we just obtained * * we have to loop through all available MySQL connection definitions * and search for the one we are looking for */ grplist = cfg_get_multi_group(cfg, octstr_imm("mysql-connection")); while (grplist && (grp = (CfgGroup *)gwlist_extract_first(grplist)) != NULL) { p = cfg_get(grp, octstr_imm("id")); if (p != NULL && octstr_compare(p, mysql_id) == 0) { goto found; } if (p != NULL) octstr_destroy(p); } panic(0, "SQLBOX: MySQL: connection settings for id '%s' are not specified!", octstr_get_cstr(mysql_id)); found: octstr_destroy(p); gwlist_destroy(grplist, NULL); if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1 || pool_size == 0) pool_size = 1; if (!(mysql_host = cfg_get(grp, octstr_imm("host")))) panic(0, "SQLBOX: MySQL: directive 'host' is not specified!"); if (!(mysql_user = cfg_get(grp, octstr_imm("username")))) panic(0, "SQLBOX: MySQL: directive 'username' is not specified!"); if (!(mysql_pass = cfg_get(grp, octstr_imm("password")))) panic(0, "SQLBOX: MySQL: directive 'password' is not specified!"); if (!(mysql_db = cfg_get(grp, octstr_imm("database")))) panic(0, "SQLBOX: MySQL: directive 'database' is not specified!"); have_port = (cfg_get_integer(&mysql_port, grp, octstr_imm("port")) != -1); /* * ok, ready to connect to MySQL */ db_conf = gw_malloc(sizeof(DBConf)); gw_assert(db_conf != NULL); db_conf->mysql = gw_malloc(sizeof(MySQLConf)); gw_assert(db_conf->mysql != NULL); db_conf->mysql->host = mysql_host; db_conf->mysql->username = mysql_user; db_conf->mysql->password = mysql_pass; db_conf->mysql->database = mysql_db; if (have_port) { db_conf->mysql->port = mysql_port; } pool = dbpool_create(DBPOOL_MYSQL, db_conf, pool_size); gw_assert(pool != NULL); /* * XXX should a failing connect throw panic?! */ if (dbpool_conn_count(pool) == 0) panic(0,"SQLBOX: MySQL: database pool has no connections!"); octstr_destroy(mysql_id); res = gw_malloc(sizeof(struct server_type)); gw_assert(res != NULL); res->type = octstr_create("MySQL"); res->sql_enter = sqlbox_configure_mysql; res->sql_leave = mysql_leave; res->sql_fetch_msg = mysql_fetch_msg; res->sql_save_msg = mysql_save_msg; return res; }
struct dlr_storage *dlr_init_mongodb(Cfg *cfg) { CfgGroup *grp; List *grplist; Octstr *mongodb_host, *mongodb_user, *mongodb_pass, *mongodb_db, *mongodb_id; long pool_size; long mongodb_port = 27017; DBConf *db_conf = NULL; int found; if ((grp = cfg_get_single_group(cfg, octstr_imm("dlr-db"))) == NULL) { panic(0, "DLR: MongoDB: group 'dlr-db' is not specified!"); } if (!(mongodb_id = cfg_get(grp, octstr_imm("id")))) { panic(0, "DLR: MongoDB: directive 'id' is not specified!"); } /* initialize database fields */ fields = dlr_db_fields_create(grp); gw_assert(fields != NULL); grplist = cfg_get_multi_group(cfg, octstr_imm("mongodb-connection")); found = 0; while (grplist && (grp = gwlist_extract_first(grplist)) != NULL) { Octstr *p = cfg_get(grp, octstr_imm("id")); if (p != NULL && octstr_compare(p, mongodb_id) == 0) { found = 1; } if (p != NULL) { octstr_destroy(p); } if (found == 1) { break; } } gwlist_destroy(grplist, NULL); if (found == 0) { panic(0, "DLR: MongoDB: connection settings for id '%s' are not specified!", octstr_get_cstr(mongodb_id)); } if (!(mongodb_host = cfg_get(grp, octstr_imm("host")))) { panic(0, "DLR: MongoDB: directive 'host' is not specified!"); } if (!(mongodb_db = cfg_get(grp, octstr_imm("database")))) { panic(0, "DLR: MongoDB: directive 'database' is not specified!"); } /* Keep a global reference to the database and table */ mongodb_database = octstr_get_cstr(mongodb_db); mongodb_table = octstr_get_cstr(fields->table); mongodb_namespace = (char *)gw_malloc(strlen(mongodb_database) + strlen(mongodb_table) + 2); /* . and \0 */ sprintf(mongodb_namespace, "%s.%s", mongodb_database, mongodb_table); mongodb_user = cfg_get(grp, octstr_imm("username")); mongodb_pass = cfg_get(grp, octstr_imm("password")); cfg_get_integer(&mongodb_port, grp, octstr_imm("port")); /* optional */ if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1) { pool_size = 1; } /* ok we are ready to create dbpool */ db_conf = gw_malloc(sizeof(*db_conf)); gw_assert(db_conf != NULL); db_conf->mongodb = gw_malloc(sizeof(MongoDBConf)); gw_assert(db_conf->mongodb != NULL); db_conf->mongodb->host = mongodb_host; db_conf->mongodb->port = mongodb_port; db_conf->mongodb->username = mongodb_user; db_conf->mongodb->password = mongodb_pass; db_conf->mongodb->database = mongodb_db; pool = dbpool_create(DBPOOL_MONGODB, db_conf, pool_size); gw_assert(pool != NULL); if (dbpool_conn_count(pool) == 0) { panic(0, "DLR: MongoDB: Could not establish connection(s)."); } octstr_destroy(mongodb_id); return &handles; }
int main(int argc, char **argv) { DBPool *pool; DBConf *conf = NULL; /* for compiler please */ unsigned int num_threads = 1; unsigned long i; int opt; time_t start = 0, end = 0; double run_time; Octstr *user, *pass, *db, *host, *db_type; int j, bail_out; user = pass = db = host = db_type = NULL; gwlib_init(); sql = octstr_imm("SHOW STATUS"); while ((opt = getopt(argc, argv, "v:h:u:p:d:s:q:t:S:T:")) != EOF) { switch (opt) { case 'v': log_set_output_level(atoi(optarg)); break; case 'h': host = octstr_create(optarg); break; case 'u': user = octstr_create(optarg); break; case 'p': pass = octstr_create(optarg); break; case 'd': db = octstr_create(optarg); break; case 'S': octstr_destroy(sql); sql = octstr_create(optarg); break; case 's': pool_size = atoi(optarg); break; case 'q': queries = atoi(optarg); break; case 't': num_threads = atoi(optarg); break; case 'T': db_type = octstr_create(optarg); break; case '?': default: error(0, "Invalid option %c", opt); help(); panic(0, "Stopping."); } } if (!optind) { help(); exit(0); } if (!db_type) { info(0, "No database type given assuming MySQL."); } else if (octstr_case_compare(db_type, octstr_imm("mysql")) == 0) { info(0, "Do tests for mysql database."); database_type = DBPOOL_MYSQL; } else if (octstr_case_compare(db_type, octstr_imm("oracle")) == 0) { info(0, "Do tests for oracle database."); database_type = DBPOOL_ORACLE; } else if (octstr_case_compare(db_type, octstr_imm("sqlite")) == 0) { info(0, "Do tests for sqlite database."); database_type = DBPOOL_SQLITE; } else if (octstr_case_compare(db_type, octstr_imm("sqlite3")) == 0) { info(0, "Do tests for sqlite3 database."); database_type = DBPOOL_SQLITE3; } else { panic(0, "Unknown database type '%s'", octstr_get_cstr(db_type)); } /* check if we have the database connection details */ switch (database_type) { case DBPOOL_ORACLE: bail_out = (!user || !pass || !db) ? 1 : 0; break; case DBPOOL_SQLITE: case DBPOOL_SQLITE3: bail_out = (!db) ? 1 : 0; break; default: bail_out = (!host || !user || !pass || !db) ? 1 : 0; break; } if (bail_out) { help(); panic(0, "Database connection details are not fully provided!"); } for (j = 0; j < 1; j++) { /* create DBConf */ switch (database_type) { #ifdef HAVE_MYSQL case DBPOOL_MYSQL: conf = mysql_create_conf(user,pass,db,host); client_thread = mysql_client_thread; break; #endif #ifdef HAVE_ORACLE case DBPOOL_ORACLE: conf = oracle_create_conf(user, pass, db); client_thread = oracle_client_thread; break; #endif #ifdef HAVE_SQLITE case DBPOOL_SQLITE: conf = sqlite_create_conf(db); client_thread = sqlite_client_thread; break; #endif #ifdef HAVE_SQLITE3 case DBPOOL_SQLITE3: conf = sqlite3_create_conf(db); client_thread = sqlite3_client_thread; break; #endif default: panic(0, "ooops ...."); }; /* create */ info(0,"Creating database pool to `%s' with %d connections type '%s'.", (host ? octstr_get_cstr(host) : octstr_get_cstr(db)), pool_size, octstr_get_cstr(db_type)); pool = dbpool_create(database_type, conf, pool_size); debug("",0,"Connections within pool: %ld", dbpool_conn_count(pool)); for (i = 0; i < num_threads; ++i) { if (gwthread_create(inc_dec_thread, pool) == -1) panic(0, "Could not create thread %ld", i); } gwthread_join_all(); info(0, "Connections within pool: %ld", dbpool_conn_count(pool)); info(0, "Checked pool, %d connections still active and ok", dbpool_check(pool)); /* queries */ info(0,"SQL query is `%s'", octstr_get_cstr(sql)); time(&start); for (i = 0; i < num_threads; ++i) { #if 0 if (gwthread_create(inc_dec_thread, pool) == -1) panic(0, "Couldnot create thread %ld", i); #endif if (gwthread_create(client_thread, pool) == -1) panic(0, "Couldnot create thread %ld", i); } gwthread_join_all(); time(&end); run_time = difftime(end, start); info(0, "%ld requests in %.2f seconds, %.2f requests/s.", (queries * num_threads), run_time, (float) (queries * num_threads) / (run_time==0?1:run_time)); /* check all active connections */ debug("",0,"Connections within pool: %ld", dbpool_conn_count(pool)); info(0,"Checked pool, %d connections still active and ok", dbpool_check(pool)); info(0,"Destroying pool"); dbpool_destroy(pool); } /* for loop */ octstr_destroy(sql); octstr_destroy(db_type); octstr_destroy(user); octstr_destroy(pass); octstr_destroy(db); octstr_destroy(host); gwlib_shutdown(); return 0; }
struct server_type *sqlbox_init_redis(Cfg* cfg) { CfgGroup *grp; List *grplist; Octstr *redis_host, *redis_password, *redis_id; Octstr *p = NULL; long pool_size, redis_port = 0, redis_database = -1, redis_idle_timeout_secs = -1; DBConf *db_conf = NULL; struct server_type *res = NULL; /* * check for all mandatory directives that specify the key names * for the redis storage */ if (!(grp = cfg_get_single_group(cfg, octstr_imm("sqlbox")))) panic(0, "SQLBOX: Redis: group 'sqlbox' is not specified!"); if (!(redis_id = cfg_get(grp, octstr_imm("id")))) panic(0, "SQLBOX: Redis: directive 'id' is not specified in the 'sqlbox' group!"); /* * now grap the required information from the 'redis-connection' group * with the redis-id we just obtained * * we have to loop through all available Redis connection definitions * and search for the one we are looking for */ grplist = cfg_get_multi_group(cfg, octstr_imm("redis-connection")); while (grplist && (grp = (CfgGroup *)gwlist_extract_first(grplist)) != NULL) { p = cfg_get(grp, octstr_imm("id")); if (p != NULL && octstr_compare(p, redis_id) == 0) { goto found; } if (p != NULL) octstr_destroy(p); } panic(0, "SQLBOX: Redis: connection settings for id '%s' are not specified!", octstr_get_cstr(redis_id)); found: octstr_destroy(p); gwlist_destroy(grplist, NULL); if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1 || pool_size == 0) pool_size = 1; if (!(redis_host = cfg_get(grp, octstr_imm("host")))) panic(0, "SQLBOX: Redis: directive 'host' is not specified!"); if (cfg_get_integer(&redis_port, grp, octstr_imm("port")) == -1) panic(0, "SQLBOX: Redis: directive 'port' is not specified!"); redis_password = cfg_get(grp, octstr_imm("password")); cfg_get_integer(&redis_database, grp, octstr_imm("database")); cfg_get_integer(&redis_idle_timeout_secs, grp, octstr_imm("idle-timeout-secs")); /* * ok, ready to connect to Redis */ db_conf = gw_malloc(sizeof(DBConf)); gw_assert(db_conf != NULL); db_conf->redis = gw_malloc(sizeof(RedisConf)); gw_assert(db_conf->redis != NULL); db_conf->redis->host = redis_host; db_conf->redis->port = redis_port; db_conf->redis->password = redis_password; db_conf->redis->database = redis_database; db_conf->redis->idle_timeout_secs = redis_idle_timeout_secs; pool = dbpool_create(DBPOOL_REDIS, db_conf, pool_size); gw_assert(pool != NULL); /* * XXX should a failing connect throw panic?! */ if (dbpool_conn_count(pool) == 0) panic(0,"SQLBOX: Redis: database pool has no connections!"); octstr_destroy(redis_id); res = gw_malloc(sizeof(struct server_type)); gw_assert(res != NULL); res->type = octstr_create("Redis"); res->sql_enter = sqlbox_configure_redis; res->sql_leave = redis_leave; res->sql_fetch_msg = redis_fetch_msg; res->sql_save_msg = redis_save_msg; return res; }
struct server_type *sqlbox_init_oracle(Cfg* cfg) { CfgGroup *grp; List *grplist; Octstr *oracle_user, *oracle_pass, *oracle_tnsname, *oracle_id; Octstr *p = NULL; long pool_size; DBConf *db_conf = NULL; struct server_type *res = NULL; /* * check for all mandatory directives that specify the field names * of the used Oracle table */ if (!(grp = cfg_get_single_group(cfg, octstr_imm("sqlbox")))) panic(0, "SQLBOX: Oracle: group 'sqlbox' is not specified!"); if (!(oracle_id = cfg_get(grp, octstr_imm("id")))) panic(0, "SQLBOX: Oracle: directive 'id' is not specified!"); /* * now grap the required information from the 'oracle-connection' group * with the oracle-id we just obtained * * we have to loop through all available Oracle connection definitions * and search for the one we are looking for */ grplist = cfg_get_multi_group(cfg, octstr_imm("oracle-connection")); while (grplist && (grp = (CfgGroup *)gwlist_extract_first(grplist)) != NULL) { p = cfg_get(grp, octstr_imm("id")); if (p != NULL && octstr_compare(p, oracle_id) == 0) { goto found; } if (p != NULL) octstr_destroy(p); } panic(0, "SQLBOX: Oracle: connection settings for id '%s' are not specified!", octstr_get_cstr(oracle_id)); found: octstr_destroy(p); gwlist_destroy(grplist, NULL); if (cfg_get_integer(&pool_size, grp, octstr_imm("max-connections")) == -1 || pool_size == 0) pool_size = 1; if (!(oracle_user = cfg_get(grp, octstr_imm("username")))) panic(0, "SQLBOX: Oracle: directive 'username' is not specified!"); if (!(oracle_pass = cfg_get(grp, octstr_imm("password")))) panic(0, "SQLBOX: Oracle: directive 'password' is not specified!"); if (!(oracle_tnsname = cfg_get(grp, octstr_imm("tnsname")))) panic(0, "SQLBOX: Oracle: directive 'tnsname' is not specified!"); /* * ok, ready to connect to Oracle */ db_conf = gw_malloc(sizeof(DBConf)); gw_assert(db_conf != NULL); db_conf->oracle = gw_malloc(sizeof(OracleConf)); gw_assert(db_conf->oracle != NULL); db_conf->oracle->username = oracle_user; db_conf->oracle->password = oracle_pass; db_conf->oracle->tnsname = oracle_tnsname; pool = dbpool_create(DBPOOL_ORACLE, db_conf, pool_size); gw_assert(pool != NULL); /* * XXX should a failing connect throw panic?! */ if (dbpool_conn_count(pool) == 0) panic(0,"SQLBOX: Oracle: database pool has no connections!"); octstr_destroy(oracle_id); res = gw_malloc(sizeof(struct server_type)); gw_assert(res != NULL); res->type = octstr_create("Oracle"); res->sql_enter = sqlbox_configure_oracle; res->sql_leave = oracle_leave; res->sql_fetch_msg = oracle_fetch_msg; res->sql_save_msg = oracle_save_msg; return res; }