/* 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; }
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; }
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; }
/* 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; }
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"); } }