コード例 #1
0
ファイル: dbi_main.c プロジェクト: cention-nazri/libdbi
dbi_conn dbi_conn_new_r(const char *name, dbi_inst Inst) {
	dbi_driver driver;
	dbi_conn conn;

	driver = dbi_driver_open_r(name, Inst);
	conn = dbi_conn_open(driver);

	return conn;
}
コード例 #2
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;
}
コード例 #3
0
ファイル: dbi.c プロジェクト: 4thAce/collectd
static int cdbi_connect_database (cdbi_database_t *db) /* {{{ */
{
  dbi_driver driver;
  dbi_conn connection;
  size_t i;
  int status;

  if (db->connection != NULL)
  {
    status = dbi_conn_ping (db->connection);
    if (status != 0) /* connection is alive */
      return (0);

    dbi_conn_close (db->connection);
    db->connection = NULL;
  }

  driver = dbi_driver_open_r (db->driver, dbi_instance);
  if (driver == NULL)
  {
    ERROR ("dbi plugin: cdbi_connect_database: dbi_driver_open_r (%s) failed.",
        db->driver);
    INFO ("dbi plugin: Maybe the driver isn't installed? "
        "Known drivers are:");
    for (driver = dbi_driver_list_r (NULL, dbi_instance);
        driver != NULL;
        driver = dbi_driver_list_r (driver, dbi_instance))
    {
      INFO ("dbi plugin: * %s", dbi_driver_get_name (driver));
    }
    return (-1);
  }

  connection = dbi_conn_open (driver);
  if (connection == NULL)
  {
    ERROR ("dbi plugin: cdbi_connect_database: dbi_conn_open (%s) failed.",
        db->driver);
    return (-1);
  }

  /* Set all the driver options. Because this is a very very very generic
   * interface, the error handling is kind of long. If an invalid option is
   * encountered, it will get a list of options understood by the driver and
   * report that as `INFO'. This way, users hopefully don't have too much
   * trouble finding out how to configure the plugin correctly.. */
  for (i = 0; i < db->driver_options_num; i++)
  {
    if (db->driver_options[i].is_numeric)
    {
      status = dbi_conn_set_option_numeric (connection,
          db->driver_options[i].key, db->driver_options[i].value.numeric);
      if (status != 0)
      {
        char errbuf[1024];
        ERROR ("dbi plugin: cdbi_connect_database (%s): "
            "dbi_conn_set_option_numeric (\"%s\", %i) failed: %s.",
            db->name,
            db->driver_options[i].key, db->driver_options[i].value.numeric,
            cdbi_strerror (connection, errbuf, sizeof (errbuf)));
      }
    }
    else
    {
      status = dbi_conn_set_option (connection,
          db->driver_options[i].key, db->driver_options[i].value.string);
      if (status != 0)
      {
        char errbuf[1024];
        ERROR ("dbi plugin: cdbi_connect_database (%s): "
            "dbi_conn_set_option (\"%s\", \"%s\") failed: %s.",
            db->name,
            db->driver_options[i].key, db->driver_options[i].value.string,
            cdbi_strerror (connection, errbuf, sizeof (errbuf)));
      }
    }

    if (status != 0)
    {
      char const *opt;

      INFO ("dbi plugin: This is a list of all options understood "
          "by the `%s' driver:", db->driver);
      for (opt = dbi_conn_get_option_list (connection, NULL);
          opt != NULL;
          opt = dbi_conn_get_option_list (connection, opt))
      {
        INFO ("dbi plugin: * %s", opt);
      }

      dbi_conn_close (connection);
      return (-1);
    }
  } /* for (i = 0; i < db->driver_options_num; i++) */

  status = dbi_conn_connect (connection);
  if (status != 0)
  {
    char errbuf[1024];
    ERROR ("dbi plugin: cdbi_connect_database (%s): "
        "dbi_conn_connect failed: %s",
        db->name, cdbi_strerror (connection, errbuf, sizeof (errbuf)));
    dbi_conn_close (connection);
    return (-1);
  }

  if (db->select_db != NULL)
  {
    status = dbi_conn_select_db (connection, db->select_db);
    if (status != 0)
    {
      char errbuf[1024];
      WARNING ("dbi plugin: cdbi_connect_database (%s): "
          "dbi_conn_select_db (%s) failed: %s. Check the `SelectDB' option.",
          db->name, db->select_db,
          cdbi_strerror (connection, errbuf, sizeof (errbuf)));
      dbi_conn_close (connection);
      return (-1);
    }
  }

  db->connection = connection;
  return (0);
} /* }}} int cdbi_connect_database */