Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
Arquivo: zrpcdb.c Projeto: zmike/ezrpc
/**
 * 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;
}