Esempio n. 1
0
int main(void)
{
  attachsql_connect_t *con= NULL;
  attachsql_error_t *error= NULL;
  const char *query= "SELECT * FROM t1 WHERE name = ? AND age > ?";
  attachsql_return_t ret= ATTACHSQL_RETURN_NONE;

  con= attachsql_connect_create("localhost", 3306, "test", "test", "testdb", NULL);
  attachsql_statement_prepare(con, strlen(query), query, &error);
  while((ret != ATTACHSQL_RETURN_EOF) && (error == NULL))
  {
    ret= attachsql_connect_poll(con, &error);
  }
  if (error != NULL)
  {
    printf("Error occurred: %s", attachsql_error_message(error));
    attachsql_error_free(error);
    attachsql_statement_close(con);
    attachsql_connect_destroy(con);
    return 1;
  }

  const char *name= "fred";
  uint32_t age= 30;
  attachsql_statement_set_string(con, 0, strlen(name), name, NULL);
  attachsql_statement_set_int(con, 1, age, NULL);
  attachsql_statement_execute(con, &error);
  ret= ATTACHSQL_RETURN_NONE;
  while ((ret != ATTACHSQL_RETURN_EOF) && (error == NULL))
  {
    ret= attachsql_connect_poll(con, &error);
    if (ret != ATTACHSQL_RETURN_ROW_READY)
    {
      continue;
    }
    attachsql_statement_row_get(con, &error);
    printf("ID: %d, ", attachsql_statement_get_int(con, 0, &error));
    size_t len;
    char *name_data= attachsql_statement_get_char(con, 1, &len, &error);
    printf("Name: %.*s, ", (int)len, name_data);
    printf("Age: %d\n", attachsql_statement_get_int(con, 2, &error));
    attachsql_statement_row_next(con);
  }
  if (error != NULL)
  {
    printf("Error occurred: %s", attachsql_error_message(error));
    attachsql_error_free(error);
  }
  attachsql_statement_close(con);
  attachsql_connect_destroy(con);
}
Esempio n. 2
0
int main(void)
{
  attachsql_connect_t *con= NULL;
  attachsql_error_t *error= NULL;
  const char *query= "SELECT * FROM t1 WHERE name = ? AND age > ?";
  attachsql_return_t ret= ATTACHSQL_RETURN_NONE;
  attachsql_query_row_st *row;
  uint16_t columns, current_column;
  attachsql_query_parameter_st param[2];

  con= attachsql_connect_create("localhost", 3306, "test", "test", "testdb", NULL);
  const char *name= "fred";
  uint32_t age= 30;
  param[0].type= ATTACHSQL_ESCAPE_TYPE_CHAR;
  param[0].data= (char*)name;
  param[0].length= strlen(name);
  param[1].type= ATTACHSQL_ESCAPE_TYPE_INT;
  param[1].data= &age;
  param[1].is_unsigned= true;
  attachsql_query(con, strlen(query), query, 2, param, &error);

  while ((ret != ATTACHSQL_RETURN_EOF) && (error == NULL))
  {
    ret= attachsql_connect_poll(con, &error);
    if (ret != ATTACHSQL_RETURN_ROW_READY)
    {
      continue;
    }
    row= attachsql_query_row_get(con, &error);
    columns= attachsql_query_column_count(con);
    for (current_column= 0; current_column < columns; current_column++)
    {
      printf("%.*s ", (int)row[current_column].length, row[current_column].data);
    }
    printf("\n");
    attachsql_query_row_next(con);
  }
  if (error != NULL)
  {
    printf("Error occurred: %s", attachsql_error_message(error));
    attachsql_error_free(error);
  }
  attachsql_query_close(con);
  attachsql_connect_destroy(con);
}
Esempio n. 3
0
int attachsql_drv_fetch_row(db_result_set_t *rs, db_row_t *row)
{
  attachsql_error_t *error= NULL;
  attachsql_return_t aret= ATTACHSQL_RETURN_NONE;

  /* NYI */

  attachsql_connect_t *con = rs->connection->ptr;

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

    if (error)
    {
      log_text(LOG_ALERT, "libAttachSQL Query Failed: %u:%s", attachsql_error_code(error), attachsql_error_message(error));
      attachsql_error_free(error);
      return 1;
    }
  }
  if (aret == ATTACHSQL_RETURN_EOF)
  {
    return 1;
  }
  row->ptr= attachsql_query_row_get(con, NULL);
  attachsql_query_row_next(con);

  return 0;
}
Esempio n. 4
0
int attachsql_drv_fetch(db_result_set_t *rs)
{
  /* NYI */
  attachsql_connect_t *con = rs->connection->ptr;
  size_t tmp_len;
  uint16_t columns, col;
  attachsql_return_t aret= ATTACHSQL_RETURN_NONE;
  attachsql_error_t *error= NULL;

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

    if (error)
    {
      log_text(LOG_ALERT, "libAttachSQL Query Failed: %u:%s", attachsql_error_code(error), attachsql_error_message(error));
      attachsql_error_free(error);
      return 1;
    }
  }
  if (aret == ATTACHSQL_RETURN_EOF)
  {
    return 1;
  }
  attachsql_statement_row_get(con, NULL);
  columns= attachsql_query_column_count(con);
  for (col= 0; col < columns; col++)
  {
    switch (attachsql_statement_get_column_type(con, col))
    {
      case ATTACHSQL_COLUMN_TYPE_TINY:
      case ATTACHSQL_COLUMN_TYPE_SHORT:
      case ATTACHSQL_COLUMN_TYPE_LONG:
      case ATTACHSQL_COLUMN_TYPE_YEAR:
      case ATTACHSQL_COLUMN_TYPE_INT24:
        attachsql_statement_get_int(con, col, &error);
        break;
      case ATTACHSQL_COLUMN_TYPE_LONGLONG:
        attachsql_statement_get_bigint(con, col, &error);
        break;
      case ATTACHSQL_COLUMN_TYPE_FLOAT:
        attachsql_statement_get_float(con, col, &error);
        break;
      case ATTACHSQL_COLUMN_TYPE_DOUBLE:
        attachsql_statement_get_double(con, col, &error);
        break;
      default:
        attachsql_statement_get_char(con, col, &tmp_len, &error);
        break;
    }
  }
  attachsql_query_row_next(con);

  return 0;
}
Esempio n. 5
0
int attachsql_drv_query(db_conn_t *sb_conn, const char *query,
                      db_result_set_t *rs)
{
  (void) rs;
  attachsql_connect_t *con = sb_conn->ptr;
  unsigned int rc;
  attachsql_error_t *error= NULL;
  attachsql_return_t aret= ATTACHSQL_RETURN_NONE;

  /* Close any previous query */
  attachsql_query_close(con);

  DEBUG("attachsql_query(%p, \"%s\", %u)",
        con,
        query,
        strlen(query));
  attachsql_query(con, strlen(query), query, 0, NULL, &error);

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

    if (error)
    {
      rc= attachsql_error_code(error);
      if (rc == 1213 || rc == 1205 || rc == 1020)
      {
        attachsql_error_free(error);
        return SB_DB_ERROR_DEADLOCK;
      }
      log_text(LOG_ALERT, "libAttachSQL Query Failed: %u:%s", attachsql_error_code(error), attachsql_error_message(error));
      attachsql_error_free(error);
      return SB_DB_ERROR_FAILED;
    }
  }
  //rs->connection->ptr= con;
  DEBUG("attachsql_query \"%s\" returned %d", query, aret);

  return SB_DB_ERROR_NONE;
}
Esempio n. 6
0
int attachsql_drv_execute(db_stmt_t *stmt, db_result_set_t *rs)
{
  (void) rs;
  attachsql_connect_t *con= (attachsql_connect_t *)stmt->connection->ptr;
  attachsql_return_t aret= ATTACHSQL_RETURN_NONE;
  attachsql_error_t *error= NULL;

  uint16_t i;
  int8_t tinyint;
  int16_t smallint;
  int32_t normalint;
  int64_t bigint;
  float float_type;
  double double_type;
  db_time_t *time_data;
  if (con == NULL)
    return 1;

  for (i= 0; i < stmt->bound_param_len; i++)
  {
    db_bind_t *param= &stmt->bound_param[i];
    switch(param->type)
    {
      case DB_TYPE_TINYINT:
        tinyint= *(int8_t*)param->buffer;
        attachsql_statement_set_int(con, i, tinyint, NULL);
        break;
      case DB_TYPE_SMALLINT:
        smallint= *(int16_t*)param->buffer;
        attachsql_statement_set_int(con, i, smallint, NULL);
        break;
      case DB_TYPE_INT:
        normalint= *(int32_t*)param->buffer;
        attachsql_statement_set_int(con, i, normalint, NULL);
        break;
      case DB_TYPE_BIGINT:
        bigint= *(int64_t*)param->buffer;
        attachsql_statement_set_bigint(con, i, bigint, NULL);
        break;
      case DB_TYPE_FLOAT:
        float_type= *(float*)param->buffer;
        attachsql_statement_set_float(con, i, float_type, NULL);
        break;
      case DB_TYPE_DOUBLE:
        double_type= *(double*)param->buffer;
        attachsql_statement_set_double(con, i, double_type, NULL);
        break;
      case DB_TYPE_TIME:
        time_data= (db_time_t*)param->buffer;
        attachsql_statement_set_time(con, i, time_data->hour, time_data->minute, time_data->second, 0, false, NULL);
        break;
      case DB_TYPE_DATE:
      case DB_TYPE_DATETIME:
      case DB_TYPE_TIMESTAMP:
        time_data= (db_time_t*)param->buffer;
        attachsql_statement_set_datetime(con, i, time_data->year, time_data->month, time_data->day, time_data->hour, time_data->minute, time_data->second, 0, NULL);
        break;
      case DB_TYPE_CHAR:
      case DB_TYPE_VARCHAR:
        attachsql_statement_set_string(con, i, param->max_len, param->buffer, NULL);
      case DB_TYPE_NONE:
      default:
        attachsql_statement_set_null(con, i, NULL);
        /* Not supported */
    }
  }

  attachsql_statement_execute(con, &error);

  while(aret != ATTACHSQL_RETURN_EOF)
  {
    aret= attachsql_connect_poll(con, &error);
    if (aret == ATTACHSQL_RETURN_ROW_READY)
    {
      return 0;
    }
    if (error)
    {
      log_text(LOG_ALERT, "libAttachSQL Execute Failed: %u:%s", attachsql_error_code(error), attachsql_error_message(error));
      attachsql_error_free(error);
      return SB_DB_ERROR_FAILED;
    }
  }

  return SB_DB_ERROR_NONE;
}
Esempio n. 7
0
int attachsql_drv_prepare(db_stmt_t *stmt, const char *query)
{
  attachsql_connect_t *con= (attachsql_connect_t *)stmt->connection->ptr;
  attachsql_error_t *error= NULL;
  attachsql_return_t aret= ATTACHSQL_RETURN_NONE;
  attachsql_statement_prepare(con, strlen(query), query, &error);
  while(aret != ATTACHSQL_RETURN_EOF)
  {
    aret= attachsql_connect_poll(con, &error);
    if (error)
    {
      log_text(LOG_ALERT, "libAttachSQL Prepare Failed: %u:%s", attachsql_error_code(error), attachsql_error_message(error));
      attachsql_error_free(error);
      return SB_DB_ERROR_FAILED;
    }
  }

  return 0;
}
Esempio n. 8
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;
}