Exemplo n.º 1
0
Eina_Bool
esql_reconnect_handler(Esql *e)
{
   Esql *ev;
   int ret, fd;

   ev = e->pool_member ? (Esql *)e->pool_struct : e; /* use pool struct for events */
   e->reconnect_timer = NULL;
   ret = e->backend.connect(e);
   EINA_SAFETY_ON_NULL_GOTO(e->backend.db, error);
   if (ret == ECORE_FD_ERROR)
     {
        ERR("Connection error: %s", e->backend.error_get(e));
        goto error;
     }
   fd = e->backend.fd_get(e);
   if (fd != -1)
     {
        e->fdh = ecore_main_fd_handler_add(fd, ECORE_FD_READ | ECORE_FD_WRITE | ECORE_FD_ERROR, (Ecore_Fd_Cb)esql_connect_handler, e, NULL, NULL);
        ecore_main_fd_handler_active_set(e->fdh, ret);
        e->current = ESQL_CONNECT_TYPE_INIT;
        if (ev->database) esql_database_set(e, ev->database);
     }
   return EINA_FALSE;
error:
   ecore_event_add(ESQL_EVENT_DISCONNECT, ev, (Ecore_End_Cb)esql_fake_free, NULL);
   e->event_count++;
   return EINA_FALSE;
}
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;
}