Esempio n. 1
0
int db_wrap_dbi_init2(char const * driver, db_wrap_conn_params const * param, db_wrap ** tgt)
{
	INIT_DBI(-1);
	if (! driver || !*driver || !tgt)
	{
		return DB_WRAP_E_BAD_ARG;
	}
	dbi_conn conn =
#if USE_DEPRECATED_DBI_API
		dbi_conn_new(driver)
#else
		dbi_conn_new_r(driver,DBI_INSTANCE)
#endif
		;
	if (! conn)
	{
		return DB_WRAP_E_UNKNOWN_ERROR;
	}
	db_wrap * db = NULL;
	int rc = db_wrap_dbi_init(conn, param, &db);
	if (rc)
	{
		dbi_conn_close(conn);
		return rc;
	}
	assert(db);
	*tgt = db;
	return rc;
}
Esempio n. 2
0
/* The following function is responsible for initializing a connection
 */
static rsRetVal initConn(instanceData *pData, int bSilent)
{
	DEFiRet;
	int iDrvrsLoaded;

	ASSERT(pData != NULL);
	ASSERT(pData->conn == NULL);

	if(bDbiInitialized == 0) {
		/* we need to init libdbi first */
#		ifdef HAVE_DBI_R
		iDrvrsLoaded = dbi_initialize_r((char*) pData->dbiDrvrDir, &dbiInst);
#		else
		iDrvrsLoaded = dbi_initialize((char*) pData->dbiDrvrDir);
#		endif
		if(iDrvrsLoaded == 0) {
			errmsg.LogError(0, RS_RET_SUSPENDED, "libdbi error: libdbi or libdbi drivers not present on this system - suspending.");
			ABORT_FINALIZE(RS_RET_SUSPENDED);
		} else if(iDrvrsLoaded < 0) {
			errmsg.LogError(0, RS_RET_SUSPENDED, "libdbi error: libdbi could not be "
				"initialized (do you have any dbi drivers installed?) - suspending.");
			ABORT_FINALIZE(RS_RET_SUSPENDED);
		}
		bDbiInitialized = 1; /* we are done for the rest of our existence... */
	}

#	ifdef HAVE_DBI_R
	pData->conn = dbi_conn_new_r((char*)pData->drvrName, dbiInst);
#	else
	pData->conn = dbi_conn_new((char*)pData->drvrName);
#	endif
	if(pData->conn == NULL) {
		errmsg.LogError(0, RS_RET_SUSPENDED, "can not initialize libdbi connection");
		ABORT_FINALIZE(RS_RET_SUSPENDED);
	} else { /* we could get the handle, now on with work... */
		/* Connect to database */
		dbi_conn_set_option(pData->conn, "host",     (char*) pData->host);
		dbi_conn_set_option(pData->conn, "username", (char*) pData->usrName);
		dbi_conn_set_option(pData->conn, "dbname",   (char*) pData->dbName);
		if(pData->pwd != NULL)
			dbi_conn_set_option(pData->conn, "password", (char*) pData->pwd);
		if(dbi_conn_connect(pData->conn) < 0) {
			reportDBError(pData, bSilent);
			closeConn(pData); /* ignore any error we may get */
			ABORT_FINALIZE(RS_RET_SUSPENDED);		
		}
		pData->txSupport = dbi_conn_cap_get(pData->conn, "transaction_support");
	}

finalize_it:
	RETiRet;
}
Esempio n. 3
0
template <DbType Type> dbi_conn
conn_setup (QofBackend* qbe, PairVec& options,
            UriStrings& uri)
{
    const char* dbstr = (Type == DbType::DBI_SQLITE ? "sqlite3" :
                         Type == DbType::DBI_MYSQL ? "mysql" : "pgsql");
#if HAVE_LIBDBI_R
    dbi_conn conn = nullptr;
    if (dbi_instance)
        conn = dbi_conn_new_r (dbstr, dbi_instance);
    else
        PERR ("Attempt to connect with an uninitialized dbi_instance");
#else
    auto conn = dbi_conn_new (dbstr);
#endif

    if (conn == nullptr)
    {
        PERR ("Unable to create %s dbi connection", dbstr);
        qof_backend_set_error (qbe, ERR_BACKEND_BAD_URL);
	return nullptr;
    }

    dbi_conn_error_handler (conn, error_handler<Type>, qbe);
    if (!uri.m_dbname.empty() &&
        !set_standard_connection_options(qbe, conn, uri))
    {
        dbi_conn_close(conn);
        return nullptr;
    }
    if(!options.empty())
    {
        try {
            set_options(conn, options);
        }
        catch (std::runtime_error& err)
        {
            dbi_conn_close(conn);
            qof_backend_set_error (qbe, ERR_BACKEND_SERVER_ERR);
            return nullptr;
        }
    }

    return conn;
}
Esempio n. 4
0
void swd::database::connect(const std::string& driver, const std::string& host,
 const std::string& port, const std::string& username, const std::string& password,
 const std::string& name, const std::string& encoding) {
#if defined(HAVE_DBI_NEW)
    dbi_initialize_r(NULL, &instance_);
    conn_ = dbi_conn_new_r(driver.c_str(), instance_);
#else
    dbi_initialize(NULL);
    conn_ = dbi_conn_new(driver.c_str());
#endif

    dbi_conn_set_option(conn_, "host", host.c_str());
    dbi_conn_set_option(conn_, "port", port.c_str());
    dbi_conn_set_option(conn_, "username", username.c_str());
    dbi_conn_set_option(conn_, "password", password.c_str());
    dbi_conn_set_option(conn_, "dbname", name.c_str());
    dbi_conn_set_option(conn_, "encoding", encoding.c_str());

    /* If the initial connection can not be established the process is shut down. */
    if (dbi_conn_connect(conn_) < 0) {
        throw swd::exceptions::core_exception("Can't connect to database server");
    }
}
Esempio n. 5
0
dbi_conn dbi_conn_new(const char *name) {
  return (dbi_conn_new_r(name, dbi_inst_legacy));
}
Esempio n. 6
0
/**
 * Parse command line options, create connection object,
 * start the connection and finally create database schema
 *
 * @return DBI connection handle
 */
dbi_conn *tagsistant_db_connection(int start_transaction)
{
	/* DBI connection handler used by subsequent calls to dbi_* functions */
	dbi_conn dbi = NULL;

	if (start_transaction) {
		g_rw_lock_writer_lock(&(tagsistant_query_rwlock));
	} else {
		g_rw_lock_reader_lock(&(tagsistant_query_rwlock));
	}

	/* lock the pool */
	g_mutex_lock(&tagsistant_connection_pool_lock);

	GList *pool = tagsistant_connection_pool;
	while (pool) {
		dbi = (dbi_conn) pool->data;

		/* check if the connection is still alive */
		if (!dbi_conn_ping(dbi) && dbi_conn_connect(dbi) < 0) {
			dbi_conn_close(dbi);
			tagsistant_connection_pool = g_list_delete_link(tagsistant_connection_pool, pool);
			connections--;
		} else {
			tagsistant_connection_pool = g_list_remove_link(tagsistant_connection_pool, pool);
			g_list_free_1(pool);
			break;
		}

		pool = pool->next;
	}

	/*
	 * unlock the pool mutex only if the backend is not SQLite
	 */
	g_mutex_unlock(&tagsistant_connection_pool_lock);

	if (!dbi) {
		// initialize DBI drivers
		if (TAGSISTANT_DBI_MYSQL_BACKEND == dboptions.backend) {
			if (!tagsistant_driver_is_available("mysql")) {
				fprintf(stderr, "MySQL driver not installed\n");
				dbg('s', LOG_ERR, "MySQL driver not installed");
				exit (1);
			}

			// unlucky, MySQL does not provide INTERSECT operator
			tagsistant.sql_backend_have_intersect = 0;

			// create connection
#if TAGSISTANT_REENTRANT_DBI
			dbi = dbi_conn_new_r("mysql", tagsistant.dbi_instance);
#else
			dbi = dbi_conn_new("mysql");
#endif
			if (NULL == dbi) {
				dbg('s', LOG_ERR, "Error creating MySQL connection");
				exit (1);
			}

			// set connection options
			dbi_conn_set_option(dbi, "host",     dboptions.host);
			dbi_conn_set_option(dbi, "dbname",   dboptions.db);
			dbi_conn_set_option(dbi, "username", dboptions.username);
			dbi_conn_set_option(dbi, "password", dboptions.password);
			dbi_conn_set_option(dbi, "encoding", "UTF-8");

		} else if (TAGSISTANT_DBI_SQLITE_BACKEND == dboptions.backend) {
			if (!tagsistant_driver_is_available("sqlite3")) {
				fprintf(stderr, "SQLite3 driver not installed\n");
				dbg('s', LOG_ERR, "SQLite3 driver not installed");
				exit(1);
			}

			// create connection
#if TAGSISTANT_REENTRANT_DBI
			dbi = dbi_conn_new_r("sqlite3", tagsistant.dbi_instance);
#else
			dbi = dbi_conn_new("sqlite3");
#endif
			if (NULL == dbi) {
				dbg('s', LOG_ERR, "Error connecting to SQLite3");
				exit (1);
			}

			// set connection options
			dbi_conn_set_option(dbi, "dbname", "tags.sql");
			dbi_conn_set_option(dbi, "sqlite3_dbdir", tagsistant.repository);

		} else {

			dbg('s', LOG_ERR, "No or wrong database family specified!");
			exit (1);
		}

		// try to connect
		if (dbi_conn_connect(dbi) < 0) {
			int error = dbi_conn_error(dbi, NULL);
			dbg('s', LOG_ERR, "Could not connect to DB (error %d). Please check the --db settings", error);
			exit(1);
		}

		connections++;

		dbg('s', LOG_INFO, "SQL connection established");
	}

	/* start a transaction */
	if (start_transaction) {
#if TAGSISTANT_USE_INTERNAL_TRANSACTIONS
		switch (tagsistant.sql_database_driver) {
			case TAGSISTANT_DBI_SQLITE_BACKEND:
				tagsistant_query("begin transaction", dbi, NULL, NULL);
				break;

			case TAGSISTANT_DBI_MYSQL_BACKEND:
				tagsistant_query("start transaction", dbi, NULL, NULL);
				break;
		}
#else
		dbi_conn_transaction_begin(dbi);
#endif
	}

	return(dbi);
}
Esempio n. 7
0
static gboolean
afsql_dd_ensure_initialized_connection(AFSqlDestDriver *self)
{
  if (self->dbi_ctx)
    return TRUE;

  self->dbi_ctx = dbi_conn_new_r(self->type, dbi_instance);

  if (!self->dbi_ctx)
    {
      msg_error("No such DBI driver",
                evt_tag_str("type", self->type),
                NULL);
      return FALSE;
    }

  dbi_conn_set_option(self->dbi_ctx, "host", self->host);

  if (strcmp(self->type, "mysql"))
    dbi_conn_set_option(self->dbi_ctx, "port", self->port);
  else
    dbi_conn_set_option_numeric(self->dbi_ctx, "port", atoi(self->port));

  dbi_conn_set_option(self->dbi_ctx, "username", self->user);
  dbi_conn_set_option(self->dbi_ctx, "password", self->password);
  dbi_conn_set_option(self->dbi_ctx, "dbname", self->database);
  dbi_conn_set_option(self->dbi_ctx, "encoding", self->encoding);
  dbi_conn_set_option(self->dbi_ctx, "auto-commit", self->flags & AFSQL_DDF_EXPLICIT_COMMITS ? "false" : "true");

  /* database specific hacks */
  dbi_conn_set_option(self->dbi_ctx, "sqlite_dbdir", "");
  dbi_conn_set_option(self->dbi_ctx, "sqlite3_dbdir", "");

  /* Set user-specified options */
  g_hash_table_foreach(self->dbd_options, afsql_dd_set_dbd_opt, self->dbi_ctx);
  g_hash_table_foreach(self->dbd_options_numeric, afsql_dd_set_dbd_opt_numeric, self->dbi_ctx);

  if (dbi_conn_connect(self->dbi_ctx) < 0)
    {
      const gchar *dbi_error;

      dbi_conn_error(self->dbi_ctx, &dbi_error);

      msg_error("Error establishing SQL connection",
                evt_tag_str("type", self->type),
                evt_tag_str("host", self->host),
                evt_tag_str("port", self->port),
                evt_tag_str("username", self->user),
                evt_tag_str("database", self->database),
                evt_tag_str("error", dbi_error),
                NULL);

      return FALSE;
    }

  if (self->session_statements != NULL)
    {
      GList *l;

      for (l = self->session_statements; l; l = l->next)
        {
          if (!afsql_dd_run_query(self, (gchar *) l->data, FALSE, NULL))
            {
              msg_error("Error executing SQL connection statement",
                        evt_tag_str("statement", (gchar *) l->data),
                        NULL);

              return FALSE;
            }
        }
    }

  return TRUE;
}
Esempio n. 8
0
STATUS spacestate_init(void)
{
	configuration *conf = get_modifiable_conf();
	GError *error = NULL;
	struct sigaction sa;
	dbi_inst dbi_instance = 0;

	/* Establish SIGCHLD handler. */
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = 0;
	sa.sa_handler = child_handler;

	sigaction(SIGCHLD, &sa, NULL);


	conf->spacestate_host = g_key_file_get_string(conf->keyfile, "spacestate",
	                                              "host", &error);
	if (error) {
		fprintf(stderr, "No spacestate host supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_name = g_key_file_get_string(conf->keyfile, "spacestate",
	                                              "name", &error);
	if (error) {
		fprintf(stderr, "No spacestate name supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_driver = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                "driver", &error);
	if (error) {
		fprintf(stderr, "No spacestate driver supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_username = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                  "username", &error);
	if (error) {
		fprintf(stderr, "No spacestate username supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_password = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                  "password", &error);
	if (error) {
		fprintf(stderr, "No spacestate password supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_hook_open = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                  "open script", &error);
	if (error) {
		fprintf(stderr, "No spacestate password supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}
	conf->spacestate_hook_close = g_key_file_get_string(conf->keyfile, "spacestate",
	                                                  "close script", &error);
	if (error) {
		fprintf(stderr, "No spacestate password supplied in the configuration.\n");
		return ST_CONFIGURATION_ERROR;
	}



	conf->event_handlers = talloc_realloc(conf, conf->event_handlers, event_function, conf->event_handler_cnt+1);
	conf->event_handlers[conf->event_handler_cnt] = spacestate_update;
	conf->event_handler_cnt++;

	DEBUG(1, "Setting properties to %s space state database %s at %s as user %s", conf->spacestate_driver,
		conf->spacestate_name, conf->spacestate_host, conf->spacestate_username);

	dbi_initialize_r(NULL, &dbi_instance);
	conn = dbi_conn_new_r(conf->spacestate_driver, &dbi_instance);
	dbi_conn_set_option(conn, "host", conf->spacestate_host);
	dbi_conn_set_option(conn, "username", conf->spacestate_username);
	dbi_conn_set_option(conn, "password", conf->spacestate_password);
	dbi_conn_set_option(conn, "dbname", conf->spacestate_name);
	dbi_conn_set_option(conn, "encoding", "UTF-8");

	return ST_OK;
}