Ejemplo n.º 1
0
/* Open a connection to the database. */
int _connect_to_db(struct db_context_t *dbc)
{
    dbc->drizzle= drizzle_create(NULL);
    if (dbc->drizzle == NULL)
    {
        printf("drizzle_create failed\n");
        return ERROR;
    }

    dbc->drizzle_con= drizzle_con_create(dbc->drizzle, NULL);
    if (dbc->drizzle_con == NULL)
    {
        printf("drizzle_con_create failed\n");
        return ERROR;
    }

    drizzle_con_set_tcp(dbc->drizzle_con, drizzle_host, atoi(drizzle_port));
    drizzle_con_set_db(dbc->drizzle_con, drizzle_dbname);
    drizzle_con_set_options(dbc->drizzle_con, DRIZZLE_CON_MYSQL);

    /* Disable AUTOCOMMIT mode for connection */
    (void)drizzle_query_str(dbc->drizzle_con, dbc->drizzle_result_set, "SET AUTOCOMMIT=0", &drizzle_return);
    if (drizzle_return != DRIZZLE_RETURN_OK)
    {
        printf("drizzle_query AUTOCOMMIT=0: %s\n", drizzle_con_error(dbc->drizzle_con));
        return ERROR;
    }


    return OK;
}
Ejemplo n.º 2
0
int commit_transaction(struct db_context_t *dbc)
{

    (void)drizzle_query_str(dbc->drizzle_con, dbc->drizzle_result_set, "COMMIT", &drizzle_return);
    if (drizzle_return != DRIZZLE_RETURN_OK)
    {
        printf("drizzle_query COMMIT failed: %s\n", drizzle_con_error(dbc->drizzle_con));
        return ERROR;
    }
    return OK;
}
Ejemplo n.º 3
0
int rollback_transaction(struct db_context_t *dbc)
{
    LOG_ERROR_MESSAGE("ROLLBACK INITIATED\n");

    (void)drizzle_query_str(dbc->drizzle_con, dbc->drizzle_result_set, "ROLLBACK", &drizzle_return);
    if (drizzle_return != DRIZZLE_RETURN_OK)
    {
        printf("drizzle_query ROLLBACK failed: %s\n", drizzle_con_error(dbc->drizzle_con));
        return ERROR;
    }

    return STATUS_ROLLBACK;
}
Ejemplo n.º 4
0
/* NOTE that both db_context_t and sql_result_t have a
   drizzle_result_t, dbc->drizzle_result_set and sql_result->result_set
   When calling drizzle_query_str, second argment is a drizzle_result_t type.
   so in dbt2_sql_execute, we will use sql_result->result_set. Other routines
   will use dbc->drizzle_result_set but do not do anything with the results
*/
int dbt2_sql_execute(struct db_context_t *dbc, char * query, struct sql_result_t * sql_result, char * query_name)
{

    sql_result->result_set= NULL;
    sql_result->num_columns= 0;
    sql_result->num_rows= 0;
    sql_result->query=query;

    sql_result->result_set= drizzle_query_str(dbc->drizzle_con, NULL, query, &drizzle_return);
    if (drizzle_return != DRIZZLE_RETURN_OK)
    {
        LOG_ERROR_MESSAGE("drizzle_query %s failed: %s\n", query, drizzle_con_error(dbc->drizzle_con));
        return 0;
    }
    else
    {
        drizzle_return = drizzle_result_buffer(sql_result->result_set);
        if (drizzle_return != DRIZZLE_RETURN_OK)
        {
            printf("drizzle_result_buffer:%s\n", drizzle_con_error(dbc->drizzle_con));
            return 0;

            /* if we have rows return then we have a SELECT statement */
        }
        if (sql_result->result_set->row_count)
        {
            sql_result->num_columns= drizzle_result_column_count(sql_result->result_set);
            sql_result->num_rows= sql_result->result_set->row_count;
        }
        else   /* else we have INSERT, UPDATE, DELETE statements */
        {
            sql_result->num_rows = drizzle_result_affected_rows(sql_result->result_set);
        }
    }

    return 1;
}
Ejemplo n.º 5
0
static void _client(drizzle_con_st *con, client_state_st *state)
{
  drizzle_return_t ret;
  drizzle_column_st *column;
  drizzle_row_t row;
  size_t *field_sizes;

  switch(state->state)
  {
  case CLIENT_STATE_START:
    drizzle_test("drizzle_query_str");
    (void)drizzle_query_str(con, &state->result, "SELECT", &ret);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = CLIENT_STATE_START;
      return;
    }
    else if (ret == DRIZZLE_RETURN_COULD_NOT_CONNECT)
    {
      state->state= CLIENT_STATE_DONE;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
    {
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
    }

  case CLIENT_STATE_RESULT:
    drizzle_test("drizzle_result_buffer");
    ret = drizzle_result_buffer(&state->result);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = CLIENT_STATE_RESULT;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
    {
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
    }

    drizzle_test("drizzle_con_protocol_version");
    if (drizzle_con_protocol_version(con) != 10)
    {
      drizzle_test_error("no match");
    }

    drizzle_test("drizzle_con_server_version");
    if (strcmp(drizzle_con_server_version(con), "test_version"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_con_thread_id");
    if (drizzle_con_thread_id(con) != 1)
      drizzle_test_error("no match");

    drizzle_test("drizzle_con_scramble");
    if (memcmp(drizzle_con_scramble(con), "ABCDEFGHIJKLMNOPQRST",
               DRIZZLE_MAX_SCRAMBLE_SIZE))
    {
      drizzle_test_error("no match");
    }

    /* We default to MySQL protocol right now, which sets this flag. */
    drizzle_test("drizzle_con_capabilities");
    if (drizzle_con_capabilities(con) != DRIZZLE_CAPABILITIES_PROTOCOL_41)
      drizzle_test_error("no match");

    drizzle_test("drizzle_con_charset");
    if (drizzle_con_charset(con) != 8)
      drizzle_test_error("no match");

    drizzle_test("drizzle_con_status");
    if (drizzle_con_status(con) != DRIZZLE_CON_STATUS_NONE)
      drizzle_test_error("no match");

    drizzle_test("drizzle_con_packet_size");
    if (drizzle_con_max_packet_size(con) != DRIZZLE_MAX_PACKET_SIZE)
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_next");
    column= drizzle_column_next(&state->result);
    if (column == NULL)
      drizzle_test_error("column not found");

    drizzle_test("drizzle_column_drizzle_result");
    if (drizzle_column_drizzle_result(column) != &state->result)
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_catalog");
    if (strcmp(drizzle_column_catalog(column), "test_catalog"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_db");
    if (strcmp(drizzle_column_db(column), "test_db"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_table");
    if (strcmp(drizzle_column_table(column), "test_table"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_orig_table");
    if (strcmp(drizzle_column_orig_table(column), "test_orig_table"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_name");
    if (strcmp(drizzle_column_name(column), "test_column_1"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_orig_name");
    if (strcmp(drizzle_column_orig_name(column), "test_orig_column_1"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_charset");
    if (drizzle_column_charset(column) != 8)
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_size");
    if (drizzle_column_size(column) != 32)
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_max_size");
    if (drizzle_column_max_size(column) != 12)
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_type_drizzle");
    if (drizzle_column_type_drizzle(column) != DRIZZLE_COLUMN_TYPE_DRIZZLE_VARCHAR)
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_flags");
    if (drizzle_column_flags(column) != DRIZZLE_COLUMN_FLAGS_NONE)
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_next");
    column= drizzle_column_next(&state->result);
    if (column == NULL)
      drizzle_test_error("column not found");

    drizzle_test("drizzle_column_name");
    if (strcmp(drizzle_column_name(column), "test_column_2"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_column_next");
    column= drizzle_column_next(&state->result);
    if (column != NULL)
      drizzle_test_error("column found");

    drizzle_test("drizzle_column_prev");
    column= drizzle_column_prev(&state->result);
    if (column == NULL)
      drizzle_test_error("column not found");

    drizzle_test("drizzle_column_name");
    if (strcmp(drizzle_column_name(column), "test_column_2"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_row_next");
    row= drizzle_row_next(&state->result);
    if (row == NULL)
      drizzle_test_error("row not found");

    if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_row_field_sizes");
    field_sizes= drizzle_row_field_sizes(&state->result);
    if (field_sizes[0] != 12 || field_sizes[1] != 12)
      drizzle_test_error("no match");

    drizzle_test("drizzle_row_prev");
    row = drizzle_row_prev(&state->result);
    if (row == NULL)
      drizzle_test_error("row not found");

    if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_row_next");
    row = drizzle_row_next(&state->result);

    drizzle_test("drizzle_row_index");
    row = drizzle_row_index(&state->result, 0);
    if (row == NULL)
      drizzle_test_error("row not found");

    if (strcmp(row[0], "test_field_1") || strcmp(row[1], "test_field_2"))
      drizzle_test_error("no match");

    drizzle_test("drizzle_row_index");
    row = drizzle_row_index(&state->result, 1);
    if (row != NULL)
      drizzle_test_error("row found");

    drizzle_test("drizzle_result_free");
    drizzle_result_free(&state->result);

    state->state = CLIENT_STATE_DONE;

  case CLIENT_STATE_DONE:
    return;

  default:
    drizzle_test_error("invalid client state");
  }
}
Ejemplo n.º 6
0
static void _server(drizzle_con_st *con, server_state_st *state)
{
  drizzle_return_t ret;
  const drizzle_field_t fields[2]=
  {
    (drizzle_field_t)"test_field_1",
    (drizzle_field_t)"test_field_2"
  };
  const size_t field_sizes[2]= { 12, 12 };

  switch(state->state)
  {
  case SERVER_STATE_START:
    drizzle_con_set_protocol_version(con, 10);
    drizzle_con_set_server_version(con, "test_version");
    drizzle_con_set_thread_id(con, 1);
    drizzle_con_set_scramble(con, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
    drizzle_con_set_capabilities(con, DRIZZLE_CAPABILITIES_NONE);
    drizzle_con_set_charset(con, 8);
    drizzle_con_set_status(con, DRIZZLE_CON_STATUS_NONE);
    drizzle_con_set_max_packet_size(con, DRIZZLE_MAX_PACKET_SIZE);

  case SERVER_STATE_HANDSHAKE_WRITE:
    drizzle_test("drizzle_handshake_server_write");
    ret= drizzle_handshake_server_write(con);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_HANDSHAKE_WRITE;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
    {
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);
    }

  case SERVER_STATE_HANDSHAKE_READ:
    drizzle_test("drizzle_handshake_client_read");
    ret= drizzle_handshake_client_read(con);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_HANDSHAKE_READ;
      return;
    }
    else if (ret == DRIZZLE_RETURN_LOST_CONNECTION)
    {
      return;
    }
    else if (ret == DRIZZLE_RETURN_COULD_NOT_CONNECT)
    {
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
    {
      drizzle_test_error("returned %s (%s)", drizzle_con_error(con), drizzle_strerror(ret));
    }

    drizzle_test("drizzle_result_create");
    if (drizzle_result_create(con, &state->result) == NULL)
      drizzle_test_error("returned %s", drizzle_con_error(con));

  case SERVER_STATE_HANDSHAKE_RESULT:
    drizzle_test("drizzle_handshake_result_write");
    ret= drizzle_result_write(con, &state->result, true);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_HANDSHAKE_RESULT;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_result_free(&state->result);

  case SERVER_STATE_COMMAND_BUFFER:
    drizzle_test("drizzle_con_command_buffer");
    state->data= drizzle_con_command_buffer(con, &state->command, &state->total,
                                            &ret);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_COMMAND_BUFFER;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    if (state->command != DRIZZLE_COMMAND_QUERY)
      drizzle_test_error("command was not a query");

    if (state->total != 6 && !memcmp(state->data, "SELECT", 6))
      drizzle_test_error("query doesn't match");

    if (state->data != NULL)
    {
      free(state->data);
      state->data= NULL;
    }

    drizzle_test("drizzle_result_create");
    if (drizzle_result_create(con, &state->result) == NULL)
      drizzle_test_error("returned %s", drizzle_con_error(con));

    drizzle_result_set_column_count(&state->result, 2);

  case SERVER_STATE_RESULT_HEADER:
    drizzle_test("drizzle_handshake_result_write");
    ret= drizzle_result_write(con, &state->result, false);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_HEADER;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_test("drizzle_column_create");
    if (drizzle_column_create(&state->result, &state->column) == NULL)
      drizzle_test_error("returned %s", drizzle_con_error(con));

    drizzle_column_set_catalog(&state->column, "test_catalog");
    drizzle_column_set_db(&state->column, "test_db");
    drizzle_column_set_table(&state->column, "test_table");
    drizzle_column_set_orig_table(&state->column, "test_orig_table");
    drizzle_column_set_name(&state->column, "test_column_1");
    drizzle_column_set_orig_name(&state->column, "test_orig_column_1");
    drizzle_column_set_charset(&state->column, 8);
    drizzle_column_set_size(&state->column, 32);
    drizzle_column_set_type(&state->column, DRIZZLE_COLUMN_TYPE_VARCHAR);
    drizzle_column_set_flags(&state->column, DRIZZLE_COLUMN_FLAGS_NONE);

  case SERVER_STATE_RESULT_COLUMN_1:
    drizzle_test("drizzle_column_write");
    ret= drizzle_column_write(&state->result, &state->column);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_COLUMN_1;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_column_set_name(&state->column, "test_column_2");
    drizzle_column_set_orig_name(&state->column, "test_orig_column_2");

  case SERVER_STATE_RESULT_COLUMN_2:
    drizzle_test("drizzle_column_write");
    ret= drizzle_column_write(&state->result, &state->column);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_COLUMN_2;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_test("drizzle_column_free");
    drizzle_column_free(&state->column);

    drizzle_result_set_eof(&state->result, true);

  case SERVER_STATE_RESULT_COLUMN_EOF:
    drizzle_test("drizzle_handshake_result_write");
    ret= drizzle_result_write(con, &state->result, false);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_COLUMN_EOF;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_result_calc_row_size(&state->result, fields, field_sizes);

  case SERVER_STATE_RESULT_ROW_1:
    ret= drizzle_row_write(&state->result);

  case SERVER_STATE_RESULT_ROW_1_FIELD_1:
    ret= drizzle_field_write(&state->result, fields[0], field_sizes[0],
                             field_sizes[0]);

  case SERVER_STATE_RESULT_ROW_1_FIELD_2:
    ret= drizzle_field_write(&state->result, fields[1], field_sizes[1],
                             field_sizes[1]);

  case SERVER_STATE_RESULT_ROW_EOF:
    drizzle_test("drizzle_handshake_result_write");
    ret= drizzle_result_write(con, &state->result, true);
    if (ret == DRIZZLE_RETURN_IO_WAIT)
    {
      state->state = SERVER_STATE_RESULT_ROW_EOF;
      return;
    }
    else if (ret != DRIZZLE_RETURN_OK)
      drizzle_test_error("returned %s (%d)", drizzle_con_error(con), ret);

    drizzle_result_free(&state->result);

    state->state = SERVER_STATE_DONE;

  case SERVER_STATE_DONE:
    return;

  default:
    drizzle_test_error("invalid server state");
  }
}