Esempio n. 1
0
template<> StrVec
GncDbiProviderImpl<DbType::DBI_MYSQL>::get_table_list (dbi_conn conn,
                                                       const std::string& table)
{
    std::string dbname (dbi_conn_get_option (conn, "dbname"));
    dbname.insert((std::string::size_type)0, 1, '`');
    dbname += '`';
    return conn_get_table_list (conn, dbname, table);
}
Esempio n. 2
0
template<> StrVec
GncDbiProviderImpl<DbType::DBI_SQLITE>::get_table_list (dbi_conn conn,
                                                        const std::string& table)
{
    /* Return the list, but remove the tables that sqlite3 adds for
     * its own use. */
    std::string dbname (dbi_conn_get_option (conn, "dbname"));
    auto list = conn_get_table_list (conn, dbname, table);
    auto end = std::remove(list.begin(), list.end(), "sqlite_sequence");
    list.erase(end, list.end());
    return list;
}
Esempio n. 3
0
template<> StrVec
GncDbiProviderImpl<DbType::DBI_PGSQL>::get_table_list (dbi_conn conn,
                                                       const std::string& table)
{
    std::string dbname (dbi_conn_get_option (conn, "dbname"));
    auto list = conn_get_table_list (conn, dbname, table);
    auto end = std::remove_if (list.begin(), list.end(),
                               [](std::string& table_name){
                                   return table_name == "sql_features" ||
                                   table_name == "sql_implementation_info" ||
                                   table_name == "sql_languages" ||
                                   table_name == "sql_packages" ||
                                   table_name == "sql_parts" ||
                                   table_name == "sql_sizing" ||
                                   table_name == "sql_sizing_profiles";
                               });
    list.erase(end, list.end());
    return list;
}
Esempio n. 4
0
char *_firebird_populate_db_string( dbi_conn_t *conn, const char *dbname, char *db_fullpath )
{
	/* ToDo: make OS-independent */
	const char dirsep[] = "/";
	const char *dbdir = dbi_conn_get_option(conn, "firebird_dbdir");
	
	if (!dbdir || !*dbdir) {
		/* use default directory instead */
		dbdir = default_dbdir;
	}

	bzero(db_fullpath, PATH_MAX);

	if (dbdir && *dbdir) {
		strcpy(db_fullpath, dbdir);
	}
	
	if (db_fullpath[strlen(db_fullpath)-1] != *dirsep && db_fullpath[0] != '\0') 
		strcat(db_fullpath, dirsep);
	
	strcat(db_fullpath, dbname);
	return db_fullpath;
}
Esempio n. 5
0
/**
 * check if requested driver is provided by local DBI installation
 * 
 * @param driver_name string with the name of the driver
 * @return 1 if exists, 0 otherwise
 */
int tagsistant_driver_is_available(const char *driver_name)
{
	int counter = 0;
	int driver_found = 0;
	dbi_driver driver = NULL;

	dbg('b', LOG_INFO, "Available drivers:");
#if TAGSISTANT_REENTRANT_DBI
	while ((driver = dbi_driver_list_r(driver, tagsistant.dbi_instance)) != NULL) {
#else
	while ((driver = dbi_driver_list(driver)) != NULL) {
#endif
		counter++;
		dbg('b', LOG_INFO, "  Driver #%d: %s - %s", counter, dbi_driver_get_name(driver), dbi_driver_get_filename(driver));
		if (g_strcmp0(dbi_driver_get_name(driver), driver_name) == 0) {
			driver_found = 1;
		}
	}

	if (!counter) {
		dbg('b', LOG_ERR, "No SQL driver found! Exiting now.");
		return(0);
	}

	if (!driver_found) {
		dbg('b', LOG_ERR, "No %s driver found!", driver_name);
		return(0);
	}

	return(1);
}

/**
 * Contains DBI parsed options
 */
struct {
	int backend;
	gchar *backend_name;
	gchar *host;
	gchar *db;
	gchar *username;
	gchar *password;
} dboptions;

#if TAGSISTANT_ENABLE_TAG_ID_CACHE
/** a map tag_name -> tag_id */
GHashTable *tagsistant_tag_cache = NULL;
#endif

/** regular expressions used to escape query parameters */
GRegex *RX1, *RX2, *RX3;

/**
 * Initialize libDBI structures
 */
void tagsistant_db_init()
{
	// initialize DBI library
#if TAGSISTANT_REENTRANT_DBI
	dbi_initialize_r(NULL, &(tagsistant.dbi_instance));
#else
	dbi_initialize(NULL);
#endif

#if TAGSISTANT_USE_QUERY_MUTEX
	g_mutex_init(&tagsistant_query_mutex);
#endif

#if TAGSISTANT_ENABLE_TAG_ID_CACHE
	tagsistant_tag_cache = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
#endif

	// by default, DBI backend provides intersect
	tagsistant.sql_backend_have_intersect = 1;
	tagsistant.sql_database_driver = TAGSISTANT_NULL_BACKEND;
	dboptions.backend = TAGSISTANT_NULL_BACKEND;

	// if no database option has been passed, use default SQLite3
	if (strlen(tagsistant.dboptions) == 0) {
		tagsistant.dboptions = g_strdup("sqlite3::::");
		dboptions.backend_name = g_strdup("sqlite3");
		dboptions.backend = TAGSISTANT_DBI_SQLITE_BACKEND;
		dbg('b', LOG_INFO, "Using default driver: sqlite3");
	}

	dbg('b', LOG_INFO, "Database options: %s", tagsistant.dboptions);

	// split database option value up to 5 tokens
	gchar **_dboptions = g_strsplit(tagsistant.dboptions, ":", 5);

	// set failsafe DB options
	if (_dboptions[0]) {
		if (strcmp(_dboptions[0], "sqlite3") == 0) {

			tagsistant.sql_database_driver = TAGSISTANT_DBI_SQLITE_BACKEND;
			dboptions.backend = TAGSISTANT_DBI_SQLITE_BACKEND;
			dboptions.backend_name = g_strdup("sqlite3");

		} else if (strcmp(_dboptions[0], "mysql") == 0) {

			tagsistant.sql_database_driver = TAGSISTANT_DBI_MYSQL_BACKEND;
			dboptions.backend = TAGSISTANT_DBI_MYSQL_BACKEND;
			dboptions.backend_name = g_strdup("mysql");

		}
	}

	if (TAGSISTANT_DBI_MYSQL_BACKEND == dboptions.backend) {
		if (_dboptions[1] && strlen(_dboptions[1])) {
			dboptions.host = g_strdup(_dboptions[1]);

			if (_dboptions[2] && strlen(_dboptions[2])) {
				dboptions.db = g_strdup(_dboptions[2]);

				if (_dboptions[3] && strlen(_dboptions[3])) {
					dboptions.username = g_strdup(_dboptions[3]);

					if (_dboptions[4] && strlen(_dboptions[4])) {
						dboptions.password = g_strdup(_dboptions[4]);
					} else {
						dboptions.password = g_strdup("tagsistant");
					}

				} else {
					dboptions.password = g_strdup("tagsistant");
					dboptions.username = g_strdup("tagsistant");
				}

			} else {
				dboptions.password = g_strdup("tagsistant");
				dboptions.username = g_strdup("tagsistant");
				dboptions.db = g_strdup("tagsistant");
			}

		} else {
			dboptions.password = g_strdup("tagsistant");
			dboptions.username = g_strdup("tagsistant");
			dboptions.db = g_strdup("tagsistant");
			dboptions.host = g_strdup("localhost");
		}

	}

	g_strfreev(_dboptions);

#if 0
	dbg('b', LOG_INFO, "Database driver: %s", dboptions.backend_name);

	// list configured options
	const char *option = NULL;
	int counter = 0;
	dbg('b', LOG_INFO, "Connection settings: ");
	while ((option = dbi_conn_get_option_list(tagsistant_dbi_conn, option))	!= NULL ) {
		counter++;
		dbg('b', LOG_INFO, "  Option #%d: %s = %s", counter, option, dbi_conn_get_option(tagsistant_dbi_conn, option));
	}

	// tell if backend have INTERSECT
	if (tagsistant.sql_backend_have_intersect) {
		dbg('b', LOG_INFO, "Database supports INTERSECT operator");
	} else {
		dbg('b', LOG_INFO, "Database does not support INTERSECT operator");
	}
#endif

	/* initialize the regular expressions used to escape the SQL queries */
	RX1 = g_regex_new("[\"']", 0, 0, NULL);
	RX2 = g_regex_new("'", 0, 0, NULL);
	RX3 = g_regex_new("<><>", 0, 0, NULL);
}
Esempio n. 6
0
int _dbd_real_connect(dbi_conn_t *conn, char *enc) 
{
	char dpb_buffer[256], *dpb, *p, *fb_encoding; 
	char dbase[256];
	short dpb_length; 

	char db_fullpath[PATH_MAX];

        isc_db_handle db = 0L; /* database handle */
        isc_tr_handle trans = 0L; /* transaction handle */
	ibase_conn_t *iconn = (ibase_conn_t * ) malloc(sizeof(ibase_conn_t));
	ISC_STATUS status_vector[ISC_STATUS_LENGTH];

	const char *dbname =  dbi_conn_get_option(conn, "dbname");
	const char *host =  dbi_conn_get_option(conn, "host");
	const char *username = dbi_conn_get_option(conn, "username");
	const char *password = dbi_conn_get_option(conn, "password");
	const char *encoding = dbi_conn_get_option(conn, "encoding");
	
	if(encoding == NULL || *encoding == '\0') 
	  encoding = "NONE";

	dpb = dpb_buffer;
	*dpb++ = isc_dpb_version1;
	*dpb++ = isc_dpb_num_buffers;
	*dpb++ = 1;
	*dpb++ = 90;
	
	*dpb++ = isc_dpb_user_name;
	*dpb++ = strlen(username);
	for (p = (char*)username; *p; *dpb++ = *p++);

	*dpb++ = isc_dpb_password;
	*dpb++ = strlen(password);
	for (p = (char*)password; *p; *dpb++ = *p++);

	*dpb++ = isc_dpb_lc_ctype;
	fb_encoding = (char*)dbd_encoding_from_iana(encoding);
	*dpb++ = strlen(fb_encoding);
	for (p = fb_encoding; *p; *dpb++ = *p++);

	dpb_length = dpb - dpb_buffer;
	dpb = dpb_buffer;

	/* could be used here, but is tagged deprecated */
/* 	isc_expand_dpb(&dpb, &dpb_length, */
/* 		       isc_dpb_user_name, username, */
/* 		       isc_dpb_password, password, */
/* 		       isc_dpb_lc_ctype, dbd_encoding_from_iana(encoding), */
/* 		       NULL); */

	
	if (!dbname) {
		_dbd_internal_error_handler(conn, "no database specified", DBI_ERROR_DBD);
		return -1;
	}
	
	_firebird_populate_db_string( conn, dbname, db_fullpath );

	if (host == NULL || !*host) {
		snprintf(dbase, 256, "%s", db_fullpath);
	}
	else {
		snprintf(dbase, 256, "%s:%s", host, db_fullpath);
	}

/*  	printf("dbase went to: %s<<; host: %s, username: %s, passwd: %s, encoding: %s; dbp:%s<<\n", dbase, host, username, password, encoding, dpb); */
/*  	fflush(stdout); */
	isc_attach_database(status_vector, strlen(dbase), dbase, &db, dpb_length, dpb);
	if (status_vector[0] == 1 && status_vector[1]) {
		char msg[512];
		long* pvector = status_vector;
		dealocate_iconn( iconn );
		isc_interprete(msg, &pvector);
		_dbd_internal_error_handler(conn, msg, DBI_ERROR_DBD);
		return -1;
	}
	else {
		isc_start_transaction(status_vector, &trans, 1, &db, 0, NULL);
	}

	iconn->trans = trans;
	iconn->db = db;
	iconn->charset = strdup(encoding);
	conn->connection = (void *)iconn;

        if (dbase) conn->current_db = strdup(dbase);
	
	return 0;
}