Пример #1
0
database *open_database(const char *dbtype, const char *dbhost, int dbport, const char *dbname, const char *dbuser, const char *dbpasswd)
{
    database *db = calloc(1, sizeof(database));
    static initialized = FALSE;

    strncpy(db->driver, dbtype, sizeof(db->driver));
    strncpy(db->host, dbhost, sizeof(db->host));
    sprintf(db->port, "%d", dbport);
    strncpy(db->name, dbname, sizeof(db->name));
    strncpy(db->username, dbuser, sizeof(db->username));
    strncpy(db->password, dbpasswd, sizeof(db->password));

    if (!initialized) {
        int c = dbi_initialize(NULL);
        if (c < 0) {
            LOG(LOG_ERR, "Unable to initialize libdbi! Make sure you specified a valid driver directory.\n");
            dbi_shutdown();
            return NULL;
        } else if (c == 0) {
            LOG(LOG_ERR, "Initialized libdbi, but no drivers were found!\n");
            dbi_shutdown();
            return NULL;
        }
    }
    initialized = TRUE;
    return(db);
}
Пример #2
0
static apr_status_t log_sql_dbi_cleanup(void *data)
{
    dbi_shutdown();
#if defined(WITH_APACHE20)
    return APR_SUCCESS;
#endif
}
Пример #3
0
int
main (int argc, char *argv[])
{
  dbi_conn conn;
  dbi_result result;
  const char *err_msg;
  int err_code;

  if (argc < 2)
    {
      fprintf (stderr, "%s: Need a libdir as argument!\n", argv[0]);
      return 1;
    }

  dbi_initialize (argv[1]);

  conn = dbi_conn_new ("null");
  dbi_conn_connect (conn);

  /*
   * Queries
   */
  result = dbi_conn_query (conn, "COMMIT");
  assert (result != NULL);
  dbi_result_free (result);

  dbi_conn_set_option_numeric (conn, "null.error.commit", 1);
  result = dbi_conn_query (conn, "COMMIT");
  assert (result == NULL);

  dbi_conn_set_option_numeric (conn, "null.error.commit", 0);
  result = dbi_conn_query (conn, "COMMIT");
  assert (result != NULL);
  dbi_result_free (result);

  /*
   * Normal queries
   */
  result = dbi_conn_query (conn, "SELECT * FROM DUAL");
  assert (result != NULL);
  dbi_result_free (result);

  dbi_conn_set_option_numeric (conn, "null.error.query", 1);
  result = dbi_conn_query (conn, "SELECT * FROM DUAL");
  assert (result == NULL);

  dbi_conn_set_option_numeric (conn, "null.error.query", 0);
  result = dbi_conn_query (conn, "SELECT * FROM DUAL");
  assert (result != NULL);
  dbi_result_free (result);

  /*
   * Cleanup
   */
  dbi_conn_close (conn);

  dbi_shutdown ();

  return 0;
}
Пример #4
0
Файл: dbi.c Проект: Bradan/gammu
/* Disconnects from a database */
void SMSDDBI_Free(GSM_SMSDConfig * Config)
{
	if (Config->conn.dbi != NULL) {
		dbi_conn_close(Config->conn.dbi);
		dbi_shutdown();
		Config->conn.dbi = NULL;
	}
}
Пример #5
0
static int _sql_setparam(struct sql_table_helper* th,char* key, char* value) {
    char* dbi_errstr=NULL;
    dbi_driver driver;
    /* if not connected */
    if (! th->conn) {
        /* initialize some stuff */
        th->table_next=th->table_start;
        th->result=NULL;
        th->connected=0;
        /* initialize db */
        if (getenv("RRDDEBUGSQL")) {
            fprintf(stderr,"RRDDEBUGSQL: %li: initialize libDBI\n",time(NULL) );
        }
        dbi_initialize(NULL);
        /* load the driver */
        driver=dbi_driver_open(th->dbdriver);
        if (! driver) {
            rrd_set_error( "libdbi - no such driver: %s (possibly a dynamic link problem of the driver being linked without -ldbi)",th->dbdriver);
            return -1;
        }
        /* and connect to driver */
        th->conn=dbi_conn_open(driver);
        /* and handle errors */
        if (! th->conn) {
            rrd_set_error( "libdbi - could not open connection to driver %s",th->dbdriver);
            dbi_shutdown();
            return -1;
        }
    }
    if (th->connected) {
        rrd_set_error( "we are already connected - can not set parameter %s=%s",key,value);
        _sql_close(th);
        return -1;
    }
    if (getenv("RRDDEBUGSQL")) {
        fprintf(stderr,"RRDDEBUGSQL: %li: setting option %s to %s\n",time(NULL),key,value );
    }
    if (strcmp(key, "port") == 0) {
        if (dbi_conn_set_option_numeric(th->conn,key,atoi(value))) {
            dbi_conn_error(th->conn,(const char**)&dbi_errstr);
            rrd_set_error( "libdbi: problems setting %s to %d - %s",key,value,dbi_errstr);
            _sql_close(th);
            return -1;
        }
    } else {
        if (dbi_conn_set_option(th->conn,key,value)) {
            dbi_conn_error(th->conn,(const char**)&dbi_errstr);
            rrd_set_error( "libdbi: problems setting %s to %s - %s",key,value,dbi_errstr);
            _sql_close(th);
            return -1;
        }
    }
    return 0;
}
Пример #6
0
static void dbiw_atexit()
{
#if USE_DEPRECATED_DBI_API
	dbi_shutdown();
#else
	if (DBI_INSTANCE)
	{
		dbi_inst foo = DBI_INSTANCE;
		DBI_INSTANCE = NULL;
		dbi_shutdown_r(foo);
	}
#endif
}
Пример #7
0
static void _sql_close(struct sql_table_helper* th) {
  /* close only if connected */
  if (th->conn) {
    if (getenv("RRDDEBUGSQL")) { fprintf(stderr,"RRDDEBUGSQL: %li: close connection\n",time(NULL) ); }
    /* shutdown dbi */
    dbi_conn_close(th->conn);
    if (getenv("RRDDEBUGSQL")) { fprintf(stderr,"RRDDEBUGSQL: %li: shutting down libdbi\n",time(NULL) ); }
    dbi_shutdown();
    /* and assign empty */
    th->conn=NULL;
    th->connected=0;
  }
}
Пример #8
0
void
gnc_module_finalize_backend_dbi (void)
{
#if HAVE_LIBDBI_R
    if (dbi_instance)
    {
        dbi_shutdown_r (dbi_instance);
        dbi_instance = nullptr;
    }
#else
    dbi_shutdown ();
#endif
}
Пример #9
0
static apr_status_t kill_dbi(void *p)
{
    apr_status_t rv = APR_SUCCESS;
    apr_hash_index_t *idx;
    char *key;
    ftpd_dbi_config *val;
    apr_ssize_t len;

    for (idx = apr_hash_first((apr_pool_t *) p, ftpd_dbi_config_hash); idx;
            idx = apr_hash_next(idx)) {
        apr_hash_this(idx, (void *) &key, &len, (void *) &val);
        apr_reslist_destroy(val->pool);
    }
    dbi_shutdown();

    return rv;
}
Пример #10
0
Файл: IoDBI.c Проект: BMeph/io
void IoDBI_free(IoDBI *self)
{
	dbi_shutdown();
	free(IoObject_dataPointer(self));
}
Пример #11
0
int main( int argc, char* argv[] ) {

	// Parse command line

	const char* idl_file_name = NULL;
	const char* query_file_name = NULL;
	int verbose = 0;                        // boolean

	int opt;
	opterr = 0;
	const char optstring[] = ":f:i:v";

	while( ( opt = getopt( argc, argv, optstring ) ) != -1 ) {
		switch( opt )
		{
			case 'f' :  // get file name of query
				if( query_file_name ) {
					fprintf( stderr, "Multiple input files not allowed\n" );
					return EXIT_FAILURE;
				}
				else
					query_file_name = optarg;
				break;
			case 'i' :  // get name of IDL file
				if( idl_file_name ) {
					fprintf( stderr, "Multiple IDL file names not allowed\n" );
					return EXIT_FAILURE;
				}
				else
					idl_file_name = optarg;
				break;
			case 'v' :  // Verbose
				verbose = 1;
				break;
			case '?' :  // Invalid option
				fprintf( stderr, "Invalid option '-%c' on command line\n",
						 (char) optopt );
				return EXIT_FAILURE;
			default :  // Huh?
				fprintf( stderr, "Internal error: unexpected value '%c'"
						"for optopt", (char) optopt );
				return EXIT_FAILURE;

		}
	}

	// If the command line doesn't specify an IDL file, get it
	// from an environmental variable, or apply a default
	if( NULL == idl_file_name ) {
		idl_file_name = getenv( "OILS_IDL_FILENAME" );
		if( NULL == idl_file_name )
			idl_file_name = "/openils/conf/fm_IDL.xml";
	}

	if( verbose )
		printf( "IDL file: %s\n", idl_file_name );

	char* loaded_json = NULL;
	const char* json_query = NULL;

	// Get the JSON query into a string
	if( query_file_name ) {   // Got a file?  Load it
		if( optind < argc )
			fprintf( stderr, "Extra parameter(s) ignored\n" );
		loaded_json = load_query( query_file_name );
		if( !loaded_json )
			return EXIT_FAILURE;
		json_query = loaded_json;
	} else {                  // No file?  Use command line parameter
		if ( optind == argc ) {
			fprintf( stderr, "No JSON query specified\n" );
			return EXIT_FAILURE;
		} else
			json_query = argv[ optind ];
	}

	if( verbose )
		printf( "JSON query: %s\n", json_query );

	osrfLogSetLevel( OSRF_LOG_WARNING );    // Suppress informational messages
	(void) oilsIDLInit( idl_file_name );    // Load IDL into memory

	// Load a database driver, connect to it, and install the connection in
	// the cstore module.  We don't actually connect to a database, but we
	// need the driver to process quoted strings correctly.
	if( dbi_initialize( NULL ) < 0 ) {
		printf( "Unable to load database driver\n" );
		return EXIT_FAILURE;
	};

	dbi_conn conn = dbi_conn_new( "pgsql" );  // change string if ever necessary
	if( !conn ) {
		printf( "Unable to establish dbi connection\n" );
		dbi_shutdown();
		return EXIT_FAILURE;
	}

	oilsSetDBConnection( conn );

	// The foregoing is an inelegant kludge.  The true, proper, and uniquely
	// correct thing to do is to load the system settings and then call
	// osrfAppInitialize() and osrfAppChildInit().  Maybe we'll actually
	// do that some day, but this will do for now.

	// Translate the JSON into SQL
	int rc = test_json_query( json_query );

	dbi_conn_close( conn );
	dbi_shutdown();
	if( loaded_json )
		free( loaded_json );

	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
}
Пример #12
0
Файл: dbi.c Проект: Bradan/gammu
/* Connects to database */
static GSM_Error SMSDDBI_Connect(GSM_SMSDConfig * Config)
{
	int rc;
	struct GSM_SMSDdbobj *db = Config->db;

	rc = dbi_initialize(Config->driverspath);

	if (rc == 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI did not find any drivers, try using DriversPath option");
		dbi_shutdown();
		return ERR_DB_DRIVER;
	} else if (rc < 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to initialize!");
		return ERR_DB_DRIVER;
	}

	Config->conn.dbi = dbi_conn_new(Config->driver);
	if (Config->conn.dbi == NULL) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to init %s driver!", Config->driver);
		dbi_shutdown();
		return ERR_DB_DRIVER;
	} else {
		SMSD_Log(DEBUG_SQL, Config, "Using DBI driver '%s'", dbi_driver_get_name(dbi_conn_get_driver(Config->conn.dbi)));
	}

	dbi_conn_error_handler(Config->conn.dbi, SMSDDBI_Callback, Config);

	if (dbi_conn_set_option(Config->conn.dbi, "sqlite_dbdir", Config->dbdir) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set sqlite_dbdir!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "sqlite3_dbdir", Config->dbdir) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set sqlite3_dbdir!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "host", Config->host) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set host!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "username", Config->user) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set username!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "password", Config->password) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set password!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "dbname", Config->database) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set dbname!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}
	if (dbi_conn_set_option(Config->conn.dbi, "encoding", "UTF-8") != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to set encoding!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONFIG;
	}

	if (dbi_conn_connect(Config->conn.dbi) != 0) {
		SMSD_Log(DEBUG_ERROR, Config, "DBI failed to connect!");
		SMSDDBI_Free(Config);
		return ERR_DB_CONNECT;
	}
	Config->db = db;
	return ERR_NONE;
}