library/src/databases/sqlite/BP-DBSqlite.cc
// attempt to open a sqlite file
attempt to open a sqlite file
attempt to open a sqlite file
P_BP_DB_SQLITE BP_SQLiteOpen
(
	char * open_file,
	char * query_log_file
)
{
	if(!open_file)
		return NULL;
	if(!BP_StringIsReasonablePrintableString(open_file, bpstrlen(open_file), BP_FALSE, BP_FALSE))
		return NULL;
	if(query_log_file)
	if(!BP_StringIsReasonablePrintableString(query_log_file, bpstrlen(query_log_file), BP_FALSE, BP_FALSE))
		return NULL;
	if(!sqlite3_threadsafe())
		return NULL;
	P_BP_HASH_TABLE_REGISTRY sqli_hreg = BP_CreateHashTableRegistry();
	if(!sqli_hreg)
		return NULL;
	P_BP_DB_SQLITE sqli = (P_BP_DB_SQLITE) BP_HashRegAddMemoryEntry(sqli_hreg, BP_HASH_REG_MEM_ENTRY_TYPE_GENERIC_ALLOCATION, sizeof(BP_DB_SQLITE), BPLN);
	sqli->system_hreg = sqli_hreg;
	BP_HASH_TABLE_ENTER_CRITICAL_SECTION(sqli->system_hreg);
	int rc = sqlite3_open(open_file, &sqli->db);
	if(rc != SQLITE_OK)
	{
		printf("\n [!!] Cannot open sqlite db %s ", open_file);
		BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC(sqli->system_hreg, NULL);
	}
	sqli->db_file = ht_strdup(sqli->system_hreg, open_file);
	if(query_log_file)
	{
		int log_rc = sqlite3_open(query_log_file, &sqli->log_db);
		sqli->log_db_file = ht_strdup(sqli->system_hreg, query_log_file);
		sqli->only_log_errors      = BP_TRUE;
		sqli->log_max_n            = 100000;
		sqli->log_query_parameters = BP_TRUE;
		if(log_rc != SQLITE_OK)
		{
			printf("\n [!!] Cannot open query log sqlite db %s ", query_log_file);
			BP_HASH_TABLE_LEAVE_CRITICAL_SECTION(sqli->system_hreg);
			BP_SQLiteClose(sqli);
			return NULL;
		}
	}
	BP_SQLiteSchemaFetchTables(sqli);
	BP_SQLiteSchemaFetchColumnsForTables(sqli);
	BP_HASH_TABLE_LEAVE_CRITICAL_SECTION(sqli->system_hreg);
	return sqli;
}
P_BP_DB_SQLITE BP_SQLiteOpen ( char * open_file , char * query_log_file ) { if ( ! open_file ) return NULL ; if ( ! BP_StringIsReasonablePrintableString ( open_file , bpstrlen ( open_file ) , BP_FALSE , BP_FALSE ) ) return NULL ; if ( query_log_file ) if ( ! BP_StringIsReasonablePrintableString ( query_log_file , bpstrlen ( query_log_file ) , BP_FALSE , BP_FALSE ) ) return NULL ; if ( ! sqlite3_threadsafe ( ) ) return NULL ; P_BP_HASH_TABLE_REGISTRY sqli_hreg = BP_CreateHashTableRegistry ( ) ; if ( ! sqli_hreg ) return NULL ; P_BP_DB_SQLITE sqli = ( P_BP_DB_SQLITE ) BP_HashRegAddMemoryEntry ( sqli_hreg , BP_HASH_REG_MEM_ENTRY_TYPE_GENERIC_ALLOCATION , sizeof ( BP_DB_SQLITE ) , BPLN ) ; sqli -> system_hreg = sqli_hreg ; BP_HASH_TABLE_ENTER_CRITICAL_SECTION ( sqli -> system_hreg ) ; int rc = sqlite3_open ( open_file , & sqli -> db ) ; if ( rc != SQLITE_OK ) { printf ( " \n " , open_file ) ; BP_HASH_TABLE_LEAVE_CRITICAL_SECTION_AND_RETURN_FROM_FUNC ( sqli -> system_hreg , NULL ) ; } sqli -> db_file = ht_strdup ( sqli -> system_hreg , open_file ) ; if ( query_log_file ) { int log_rc = sqlite3_open ( query_log_file , & sqli -> log_db ) ; sqli -> log_db_file = ht_strdup ( sqli -> system_hreg , query_log_file ) ; sqli -> only_log_errors = BP_TRUE ; sqli -> log_max_n = 100000 ; sqli -> log_query_parameters = BP_TRUE ; if ( log_rc != SQLITE_OK ) { printf ( " \n " , query_log_file ) ; BP_HASH_TABLE_LEAVE_CRITICAL_SECTION ( sqli -> system_hreg ) ; BP_SQLiteClose ( sqli ) ; return NULL ; } } BP_SQLiteSchemaFetchTables ( sqli ) ; BP_SQLiteSchemaFetchColumnsForTables ( sqli ) ; BP_HASH_TABLE_LEAVE_CRITICAL_SECTION ( sqli -> system_hreg ) ; return sqli ; }
attempt to open a sqlite file
attempt to open a sqlite file
// ensure we can open the file // ensure string is reasonable/printable // query the log file // check to make sure sqlite is thread safe // create hash table registry // create new sqli structure // set the system hreg // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Enter Critical Section and Open %%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // enter critical section // open the sqlite // check return values // set the db file // NOTE: Do not create query result cache here.  The result cache //       will be created automatically if a query has results //       to generate. // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Open Query Log %%%%%%%%%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // open the query log file // open the logger  sqlite log db // log database file // set defaults to log 100000 errors to disk // check return values // verbosity // leave critical section // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Populate Initial Schema %%%%%%%%%%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // attempt to fetch tables // attempt to fetch columns for tables // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // %%% Leave Critical Section and Exit %%%%%%%%%% // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // leave critical section // return the new sqli handle