int main(void) { close(STDOUT_FILENO); drizzle_st *drizzle; drizzle_query_st *query; drizzle_query_st query_buffer; size_t size; printf("sizeof(drizzle_query_st) = %zu\n", sizeof(drizzle_query_st)); if ((drizzle= drizzle_create(NULL)) == NULL) drizzle_test_error("drizzle_create"); if ((query= drizzle_query_create(drizzle, &query_buffer)) == NULL) drizzle_test_error("drizzle_query_create"); drizzle_query_free(query); if ((query= drizzle_query_create(drizzle, NULL)) == NULL) drizzle_test_error("drizzle_query_create"); if (drizzle_query_options(query) != DRIZZLE_QUERY_ALLOCATED) drizzle_test_error("drizzle_query_options"); drizzle_query_set_string(query, "SELECT 1+1", 10); if (strncmp(drizzle_query_string(query, &size), "SELECT 1+1", 10) || size != 10) { drizzle_test_error("drizzle_query_string"); } drizzle_query_free(query); drizzle_free(drizzle); return 0; }
int main(int argc, char *argv[]) { int c; drizzle_st drizzle; drizzle_con_st con_listen; in_port_t port = 3307; char *host = NULL; struct ev_loop *loop = ev_default_loop(ev_recommended_backends() | EVBACKEND_KQUEUE); ev_io accept_w; int accept_fd; client_t client; dbgin(); while ((c = getopt(argc, argv, "p:h:")) != -1) { switch (c) { case 'p': port = (in_port_t) atoi(optarg); break; case 'h': host = optarg; break; default: usage(argv[0]); return 1; } } if (drizzle_create(&drizzle) == NULL) { printf("drizzle_create error: NULL\n"); return 1; } drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS); drizzle_add_options(&drizzle, DRIZZLE_NON_BLOCKING); drizzle_set_verbose(&drizzle, DRIZZLE_VERBOSE_NEVER); if (drizzle_con_create(&drizzle, &con_listen) == NULL) { printf("drizzle_con_create error: NULL\n"); return 1; } drizzle_con_add_options(&con_listen, DRIZZLE_CON_LISTEN); drizzle_con_set_tcp(&con_listen, host, port); /* Add mysql protocol support */ drizzle_con_add_options(&con_listen, DRIZZLE_CON_MYSQL); if (drizzle_con_listen(&con_listen) != DRIZZLE_RETURN_OK) { printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle)); return 1; } printf("Server started on host: %s, port: %d\n", host, port); client.drizzle = &drizzle; client.dc = &con_listen; accept_fd = drizzle_con_fd(&con_listen); ev_io_init(&accept_w, accept_cb, accept_fd, EV_READ); ev_io_start(EV_A_ &accept_w); client.loop = loop; accept_w.data = &client; ev_loop(EV_A_ 0); drizzle_con_free(&con_listen); drizzle_free(&drizzle); return 0; }
int main(void) { close(STDOUT_FILENO); drizzle_st drizzle; drizzle_con_st listen_con; drizzle_con_st client; drizzle_con_st server; drizzle_return_t ret; bool server_accepted = false; server_state_st server_state; client_state_st client_state; drizzle_test("drizzle_create"); if (drizzle_create(&drizzle) == NULL) { drizzle_test_error("returned NULL"); } drizzle_test("drizzle_con_add_tcp_listen"); if (drizzle_con_add_tcp_listen(&drizzle, &listen_con, DRIZZLE_TEST_HOST, DRIZZLE_TEST_PORT, 1, DRIZZLE_CON_NONE) == NULL) { drizzle_test_error("returned NULL"); } drizzle_test("drizzle_con_listen"); ret= drizzle_con_listen(&listen_con); if (ret != DRIZZLE_RETURN_OK) { drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret); } drizzle_test("drizzle_con_add_tcp"); if (drizzle_con_add_tcp(&drizzle, &client, DRIZZLE_TEST_HOST, DRIZZLE_TEST_PORT, NULL, NULL, NULL, DRIZZLE_CON_NONE) == NULL) { drizzle_test_error("returned NULL"); } drizzle_test("drizzle_add_options"); drizzle_add_options(&drizzle, DRIZZLE_NON_BLOCKING); server_state.state= SERVER_STATE_START; client_state.state= CLIENT_STATE_START; while (true) { if (server_accepted == false) { drizzle_test("drizzle_con_accept"); (void)drizzle_con_accept(&drizzle, &server, &ret); if (ret == DRIZZLE_RETURN_OK) { server_accepted = true; } else if (ret == DRIZZLE_RETURN_COULD_NOT_CONNECT || ret == DRIZZLE_RETURN_NO_ACTIVE_CONNECTIONS) { break; } else if (ret != DRIZZLE_RETURN_IO_WAIT) { drizzle_test_error("returned %s (%s)", drizzle_error(&drizzle), drizzle_strerror(ret)); } } if (server_accepted) { _server(&server, &server_state); } _client(&client, &client_state); if (server_state.state == SERVER_STATE_DONE && client_state.state == CLIENT_STATE_DONE) { break; } drizzle_test("drizzle_con_wait"); ret= drizzle_con_wait(&drizzle); if (ret == DRIZZLE_RETURN_COULD_NOT_CONNECT || ret == DRIZZLE_RETURN_NO_ACTIVE_CONNECTIONS) { break; } else if (ret != DRIZZLE_RETURN_OK) { drizzle_test_error("returned %s (%d)", drizzle_error(&drizzle), ret); } } if (server_accepted) { drizzle_test("drizzle_con_free"); drizzle_con_free(&server); } drizzle_test("drizzle_con_free"); drizzle_con_free(&client); drizzle_test("drizzle_con_free"); drizzle_con_free(&listen_con); drizzle_test("drizzle_free"); drizzle_free(&drizzle); return 0; }
int main(int argc, char *argv[]) { const char *query= "SELECT table_schema,table_name FROM tables"; drizzle_st drizzle; drizzle_con_st con[SIMPLE_MULTI_COUNT]; drizzle_result_st result[SIMPLE_MULTI_COUNT]; drizzle_query_st ql[SIMPLE_MULTI_COUNT]; drizzle_return_t ret; drizzle_row_t row; int x; if (drizzle_create(&drizzle) == NULL) { printf("drizzle_create:NULL\n"); return 1; } /* Create SIMPLE_MULTI_COUNT connections and initialize query list. */ for (x= 0; x < SIMPLE_MULTI_COUNT; x++) { if (x == 0) { if (drizzle_con_create(&drizzle, &(con[0])) == NULL) { printf("drizzle_con_create:%s\n", drizzle_error(&drizzle)); return 1; } if (argc == 2 && !strcmp(argv[1], "-m")) drizzle_con_add_options(&(con[0]), DRIZZLE_CON_MYSQL); else if (argc != 1) { printf("usage: %s [-m]\n", argv[0]); return 1; } drizzle_con_set_db(&(con[0]), "information_schema"); } else { if (drizzle_con_clone(&drizzle, &(con[x]), &(con[0])) == NULL) { printf("drizzle_con_clone:%s\n", drizzle_error(&drizzle)); return 1; } } if (drizzle_query_add(&drizzle, &(ql[x]), &(con[x]), &(result[x]), query, strlen(query), 0, NULL) == NULL) { printf("drizzle_query_add:%s\n", drizzle_error(&drizzle)); return 1; } } ret= drizzle_query_run_all(&drizzle); if (ret != DRIZZLE_RETURN_OK) { printf("drizzle_query_run_all:%s\n", drizzle_error(&drizzle)); return 1; } for (x= 0; x < SIMPLE_MULTI_COUNT; x++) { if (drizzle_result_error_code(&(result[x])) != 0) { printf("%d:%s\n", drizzle_result_error_code(&(result[x])), drizzle_result_error(&(result[x]))); continue; } while ((row= drizzle_row_next(&(result[x]))) != NULL) printf("%d %s:%s\n", x, row[0], row[1]); } drizzle_free(&drizzle); return 0; }
int main(void) { close(STDOUT_FILENO); if (getenv("TESTS_ENVIRONMENT") && strstr(getenv("TESTS_ENVIRONMENT"), "valgrind")) { return EXIT_SUCCESS; } drizzle_verbose_t verbose; drizzle_st *drizzle; drizzle_st drizzle_buffer; drizzle_st *clone; drizzle_st clone_buffer; drizzle_con_st *con; drizzle_con_st *listen_con; drizzle_return_t ret; printf("sizeof(drizzle_st) = %zu\n", sizeof(drizzle_st)); printf("sizeof(drizzle_return_t) = %zu\n", sizeof(drizzle_return_t)); printf("sizeof(drizzle_verbose_t) = %zu\n", sizeof(drizzle_verbose_t)); printf("sizeof(drizzle_options_t) = %zu\n", sizeof(drizzle_options_t)); drizzle_test("drizzle_version"); printf(" %s\n", drizzle_version()); drizzle_test("drizzle_bugreport"); printf(" %s\n", drizzle_bugreport()); drizzle_test("drizzle_verbose_name"); for (verbose= DRIZZLE_VERBOSE_NEVER; verbose <= DRIZZLE_VERBOSE_MAX; verbose++) { printf(" %s\n", drizzle_verbose_name(verbose)); } drizzle_test("drizzle_create buffer"); if ((drizzle= drizzle_create(&drizzle_buffer)) == NULL) { drizzle_test_error("returned NULL"); } drizzle_test("drizzle_free buffer"); drizzle_free(drizzle); drizzle_test("drizzle_create"); if ((drizzle= drizzle_create(NULL)) == NULL) { drizzle_test_error("returned NULL"); } drizzle_test("drizzle_clone"); if ((clone= drizzle_clone(NULL, drizzle)) == NULL) { drizzle_test_error("drizzle_clone"); } drizzle_test("drizzle_free"); drizzle_free(clone); drizzle_test("drizzle_clone buffer"); if ((clone= drizzle_clone(&clone_buffer, drizzle)) == NULL) { drizzle_test_error("returned NULL"); } drizzle_test("drizzle_free buffer"); drizzle_free(clone); drizzle_test("drizzle_error"); if (strcmp(drizzle_error(drizzle), "")) { drizzle_test_error("error not empty"); } drizzle_test("drizzle_errno"); if (drizzle_errno(drizzle) != 0) { drizzle_test_error("errno not 0"); } drizzle_test("drizzle_error_code"); if (drizzle_error_code(drizzle) != 0) { drizzle_test_error("error_code not 0"); } drizzle_test("drizzle_sqlstate"); if (strcmp(drizzle_sqlstate(drizzle), "")) { drizzle_test_error("sqlstate not empty"); } /* @todo remove this option with new API. */ drizzle_remove_options(drizzle, DRIZZLE_FREE_OBJECTS); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_add_options"); drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING)) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_remove_options"); drizzle_remove_options(drizzle, DRIZZLE_NON_BLOCKING); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_set_options"); drizzle_set_options(drizzle, DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != (DRIZZLE_ALLOCATED | DRIZZLE_NON_BLOCKING)) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_set_options"); drizzle_set_options(drizzle, DRIZZLE_ALLOCATED); drizzle_test("drizzle_options"); if (drizzle_options(drizzle) != DRIZZLE_ALLOCATED) { drizzle_test_error("expected options not set"); } drizzle_test("drizzle_set_timeout"); drizzle_set_timeout(drizzle, 1234); drizzle_test("drizzle_timeout"); if (drizzle_timeout(drizzle) != 1234) { drizzle_test_error("expected timeout not set"); } drizzle_test("drizzle_set_verbose"); drizzle_set_verbose(drizzle, DRIZZLE_VERBOSE_CRAZY); drizzle_test("drizzle_verbose"); if (drizzle_verbose(drizzle) != DRIZZLE_VERBOSE_CRAZY) { drizzle_test_error("expected verbose not set"); } drizzle_test("drizzle_set_log_fn"); drizzle_set_log_fn(drizzle, _log, NULL); drizzle_test("drizzle_set_event_watch_fn"); drizzle_set_event_watch_fn(drizzle, _event_watch, NULL); /* Create a listening connection to verify that event_watch_fn gets called. */ listen_con= drizzle_con_create(drizzle, NULL); assert(listen_con != NULL); drizzle_con_set_tcp(listen_con, "localhost", DRIZZLE_TEST_PORT); ret= drizzle_con_listen(listen_con); assert(ret == DRIZZLE_RETURN_OK); if (_event_watch_read_bits == 0) { drizzle_test_error("event_watch_fn not called to wait for connections"); } _event_watch_read_bits= 0; /* Attempt a non-blocking connection. */ drizzle_add_options(drizzle, DRIZZLE_NON_BLOCKING); con= drizzle_con_add_tcp(drizzle, NULL, "localhost", DRIZZLE_TEST_PORT, "user", "pw", "db", DRIZZLE_CON_NONE); assert(con != NULL); ret= drizzle_con_connect(con); assert(ret == DRIZZLE_RETURN_IO_WAIT || ret == DRIZZLE_RETURN_COULD_NOT_CONNECT); if (ret == DRIZZLE_RETURN_IO_WAIT && _event_watch_read_bits == 0 && _event_watch_write_bits == 0) { drizzle_test_error("event_watch_fn not called to wait for I/O"); } drizzle_con_free(con); _event_watch_read_bits= 0; _event_watch_write_bits= 0; drizzle_con_free(listen_con); drizzle_test("drizzle_free"); drizzle_free(drizzle); return 0; }
/* Disconnect from the database and free the connection handle. */ int _disconnect_from_db(struct db_context_t *dbc) { drizzle_con_free(dbc->drizzle_con); drizzle_free(dbc->drizzle); return OK; }
int main(int argc, char *argv[]) { int c; uint32_t count= 0; const char *host= NULL; bool mysql= false; in_port_t port= 0; drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER; drizzle_return_t ret; drizzle_st drizzle; drizzle_con_st con_listen; drizzle_con_st con; drizzle_result_st result; drizzle_column_st column; while((c = getopt(argc, argv, "c:h:mp:v")) != -1) { switch(c) { case 'c': count= (uint32_t)atoi(optarg); break; case 'h': host= optarg; break; case 'm': mysql= true; break; case 'p': port= (in_port_t)atoi(optarg); break; case 'v': verbose++; break; default: printf("\nusage: %s [-c <count>] [-h <host>] [-m] [-p <port>] [-v]\n", argv[0]); printf("\t-c <count> - Number of connections to accept before exiting\n"); printf("\t-h <host> - Host to listen on\n"); printf("\t-m - Use the MySQL protocol\n"); printf("\t-p <port> - Port to listen on\n"); printf("\t-v - Increase verbosity level\n"); return 1; } } if (drizzle_create(&drizzle) == NULL) { printf("drizzle_create:NULL\n"); return 1; } drizzle_add_options(&drizzle, DRIZZLE_FREE_OBJECTS); drizzle_set_verbose(&drizzle, verbose); if (drizzle_con_create(&drizzle, &con_listen) == NULL) { printf("drizzle_con_create:NULL\n"); return 1; } drizzle_con_add_options(&con_listen, DRIZZLE_CON_LISTEN); drizzle_con_set_tcp(&con_listen, host, port); if (mysql) drizzle_con_add_options(&con_listen, DRIZZLE_CON_MYSQL); if (drizzle_con_listen(&con_listen) != DRIZZLE_RETURN_OK) { printf("drizzle_con_listen:%s\n", drizzle_error(&drizzle)); return 1; } while (1) { (void)drizzle_con_accept(&drizzle, &con, &ret); if (ret != DRIZZLE_RETURN_OK) { printf("drizzle_con_accept:%s\n", drizzle_error(&drizzle)); return 1; } server(&drizzle, &con, &result, &column); drizzle_con_free(&con); if (count > 0) { count--; if (count == 0) break; } } drizzle_con_free(&con_listen); drizzle_free(&drizzle); return 0; }
static void rb_drizzle_free(net_drizzle_st *context) { drizzle_free(context->drizzle); context->drizzle = NULL; ruby_xfree(context); }