Пример #1
0
int attachsql_drv_connect(db_conn_t *sb_conn)
{
  attachsql_connect_t     *con= NULL;
  const char              *host;
  attachsql_error_t      *error= NULL;
  attachsql_return_t aret= ATTACHSQL_RETURN_NONE;

  if (args.socket)
  {
    DEBUG("attachsql_connect_create(\"%s\", \"%s\", \"%s\", \"%s\")",
      args.socket,
      args.user,
      args.password,
      args.db);
    con= attachsql_connect_create(args.socket,
                             0,
                             args.user,
                             args.password,
                             args.db,
                             &error);
  } else {

    pthread_mutex_lock(&hosts_mutex);
    hosts_pos = SB_LIST_ITEM_NEXT(hosts_pos);
    if (hosts_pos == args.hosts)
      hosts_pos = SB_LIST_ITEM_NEXT(hosts_pos);
    host = SB_LIST_ENTRY(hosts_pos, value_t, listitem)->data;
    pthread_mutex_unlock(&hosts_mutex);

    DEBUG("attachsql_connect_create(\"%s\", %u, \"%s\", \"%s\", \"%s\")",
          host,
          args.port,
          args.user,
          args.password,
          args.db);
    con= attachsql_connect_create(host,
                             args.port,
                             args.user,
                             args.password,
                             args.db,
                             &error);
  }
  if (con == NULL)
  {
    log_text(LOG_FATAL, "unable to Add libAttachSQL Connection, aborting...");
    log_text(LOG_FATAL, "error %d: %s", attachsql_error_code(error), attachsql_error_message(error));
    attachsql_error_free(error);
    return 1;
  }
  attachsql_connect_set_option(con, ATTACHSQL_OPTION_SEMI_BLOCKING, NULL);

  if (!attachsql_connect(con, &error))
  {
    log_text(LOG_FATAL, "unable to connect to libAttachSQL server");
    log_text(LOG_FATAL, "error %d: %s", attachsql_error_code(error), attachsql_error_message(error));
    attachsql_error_free(error);
    attachsql_connect_destroy(con);
    return 1;

  }

  while (aret != ATTACHSQL_RETURN_IDLE)
  {
    aret = attachsql_connect_poll(con, &error);

    if (error)
    {
      log_text(LOG_FATAL, "unable to connect to libAttachSQL server");
      log_text(LOG_FATAL, "error %d: %s", attachsql_error_code(error), attachsql_error_message(error));
      attachsql_error_free(error);
      attachsql_connect_destroy(con);
      return 1;
    }
  }

  sb_conn->ptr = con;

  return 0;
}
Пример #2
0
int mysql_drv_connect(db_conn_t *sb_conn)
{
  MYSQL          *con;
  char           *host;
  char           *ssl_key;
  char           *ssl_cert;
  char           *ssl_ca;
  
  con = (MYSQL *)malloc(sizeof(MYSQL));
  if (con == NULL)
    return 1;
  sb_conn->ptr = con;
  
  mysql_init(con);
  DEBUG("mysql_init(%p)", con);

  pthread_mutex_lock(&hosts_mutex);
  hosts_pos = SB_LIST_ITEM_NEXT(hosts_pos);
  if (hosts_pos == args.hosts)
    hosts_pos = SB_LIST_ITEM_NEXT(hosts_pos);
  host = SB_LIST_ENTRY(hosts_pos, value_t, listitem)->data;
  pthread_mutex_unlock(&hosts_mutex);
  
#if 0
  /*
    FIXME: the following leads to crash in the client lib.
    http://bugs.mysql.com/?id=40552
  */
  mysql_options(con, MYSQL_READ_DEFAULT_GROUP, "sysbench");
  DEBUG("mysql_options(%p, MYSQL_READ_DEFAULT_GROUP, \"sysbench\")", con);
#endif
  
  if (args.use_ssl)
  {
    ssl_key= "client-key.pem";
    ssl_cert= "client-cert.pem";
    ssl_ca= "cacert.pem";

    DEBUG("mysql_ssl_set(%p,\"%s\", \"%s\", \"%s\", NULL, NULL)", con, ssl_key,
          ssl_cert, ssl_ca);
    mysql_ssl_set(con, ssl_key, ssl_cert, ssl_ca, NULL, NULL);
  }
  
  DEBUG("mysql_real_connect(%p, \"%s\", \"%s\", \"%s\", \"%s\", %u, \"%s\", %s)",
        con,
        host,
        args.user,
        args.password,
        args.db,
        args.port,
        args.socket,
        (MYSQL_VERSION_ID >= 50000) ? "CLIENT_MULTI_STATEMENTS" : "0"
        );
  if (!mysql_real_connect(con,
                         host,
                         args.user,
                         args.password,
                         args.db,
                         args.port,
                         args.socket,
#if MYSQL_VERSION_ID >= 50000
                          CLIENT_MULTI_STATEMENTS)
#else
                          0)
#endif
      )
  {
    log_text(LOG_FATAL, "unable to connect to MySQL server, aborting...");
    log_text(LOG_FATAL, "error %d: %s", mysql_errno(con),
           mysql_error(con));
    free(con);
    return 1;
  }

  return 0;
}