/** * Close anything opened by this module */ Eina_Bool uninit(DB *db) { EINA_SAFETY_ON_NULL_RETURN_VAL(db, EINA_FALSE); INF("Uninitializing source 'zrpcdb'"); if (db->config) db_config_free(db->config); if (db->e){ INF("Connection pool teardown for 'zrpcdb'"); esql_free(db->e); INF("Uninitialization of source 'zrpcdb' complete"); } if (--init_count == 0){ ecore_event_handler_del(zrpc_res); zrpc_res = NULL; ecore_event_handler_del(zrpc_err); zrpc_err = NULL; } if (type_cbs) eina_hash_free(type_cbs); type_cbs = NULL; if (db_cbs) eina_hash_free(db_cbs); db_cbs = NULL; if (db_cb_params) eina_hash_free(db_cb_params); db_cb_params = NULL; esql_shutdown(); eina_shutdown(); return EINA_TRUE; }
int main(void) { Esql *e; struct ctx ctx = {0, 0, 0}; ecore_init(); esql_init(); eina_log_domain_level_set("esskyuehl", EINA_LOG_LEVEL_DBG); e = esql_new(ESQL_TYPE_SQLITE); assert(e != NULL); ecore_event_handler_add(ESQL_EVENT_CONNECT, on_connect, &ctx); ecore_event_handler_add(ESQL_EVENT_ERROR, on_error, &ctx); assert(esql_connect(e, ":memory:", NULL, NULL)); ecore_main_loop_begin(); esql_disconnect(e); esql_shutdown(); ecore_shutdown(); assert(ctx.conns == 1); assert(ctx.errors == 0); assert(ctx.res == 2 + INSERTED_ROWS); return 0; }
/** * Prepare the library for use through initialization of any required connections and / or * variables * * @return boolean EINA_TRUE for success, EINA_FALSE on error. */ Eina_Bool init(DB *db, Zrpcdb_Connect_Cb cb, Ecore_Event_Handler_Cb error_cb, Ecore_Event_Handler_Cb result_cb, zentific_config *conf){ Esql *e; if ((!db) || (!cb) || (!conf)) return EINA_FALSE; eina_init(); zrpcdb_log_dom = eina_log_domain_register("zrpcdb", EINA_COLOR_ORANGE); if (zrpcdb_log_dom < 0) { fprintf(stderr, "Could not init zrpcdb log domain\n"); goto err; } INF("Initializing source 'zrpcdb'"); if (!db_config_parse(db, conf->DB_MODULES_PATH)){ ERR("Could not parse config file"); goto log; } /* make sure that we have all config options */ if ((!db->config->backend) || (!db->config->username) || (!db->config->password) || (!db->config->database)) { ERR("Missing options in db->config file. Requires HOSTNAME, USERNAME, PASSWORD, and DATABASE fields."); goto cfg; } /* FIXME: better value here */ esql_init(); if (db->config->conns < 5){ WRN("DB init: DB pool size must be at least 5. Resizing to 5."); db->config->conns = 5; } e = esql_pool_new(db->config->conns, ESQL_TYPE_MYSQL); EINA_SAFETY_ON_NULL_GOTO(e, esql); esql_data_set(e, db); db->pool_size = db->config->conns; esql_connect_callback_set(e, (Esql_Connect_Cb)init_cb, cb); esql_database_set(e, db->config->database); if (!esql_connect(e, db->config->backend, db->config->username, db->config->password)){ ERR("Could not begin database connection!"); goto conn; } db->e = e; if (!init_count++){ ZRPCDB_EVENT_ERROR = ecore_event_type_new(); zrpc_err = ecore_event_handler_add(ZRPCDB_EVENT_ERROR, error_cb, db); ZRPCDB_EVENT_RESULT = ecore_event_type_new(); zrpc_res = ecore_event_handler_add(ZRPCDB_EVENT_RESULT, result_cb, db); type_cbs = eina_hash_int64_new(NULL); db_cbs = eina_hash_int64_new(NULL); db_cb_params = eina_hash_int64_new(NULL); } // eina_log_domain_level_set("zrpcdb", db->config->loglevel); return EINA_TRUE; conn: esql_free(e); esql: esql_shutdown(); cfg: db_config_free(db->config); log: eina_log_domain_unregister(zrpcdb_log_dom); zrpcdb_log_dom = -1; err: eina_shutdown(); return EINA_FALSE; }