Ejemplo n.º 1
0
static PyObject*
ConnectionObject_autocommit(ConnectionObject *self, PyObject *args)
{
    int flag, status = 1;
    char query[256];

    drizzle_return_t ret;
    drizzle_result_st result;

    if (!PyArg_ParseTuple(args, "i", &flag)) {
        return NULL;
    }

    DEBUG("autocommit %d", flag);
    snprintf(query, 256, "SET AUTOCOMMIT=%d;", flag);
    BDEBUG("query %s", query);
    
    while(status){
        (void)drizzle_query(self->con, &result, query, 256, &ret);
        status = io_wait(self->con, ret);
        if (status == -1){
            goto error;
        }
    }

    drizzle_result_free(&result);
    Py_RETURN_NONE;
error:
    drizzle_result_free(&result);
    PyErr_SetString(PyExc_IOError, drizzle_error(drizzle));
    return NULL;
}
Ejemplo n.º 2
0
void accept_cb(EV_P_ struct ev_io *w, int revent)
{
    client_t *client = (client_t *) w->data;
    drizzle_con_st     *dc = (drizzle_con_st *) malloc(sizeof(drizzle_con_st));
    drizzle_return_t    ret;
    int                 fd;
    connection_t       *con;

    dbgin();

    /* Manually set connection to ready */
    drizzle_con_add_options(client->dc, DRIZZLE_CON_IO_READY);

    drizzle_con_accept(client->drizzle, dc, &ret);
    if (ret != DRIZZLE_RETURN_OK)
    {
        if (ret == DRIZZLE_RETURN_IO_WAIT) {
            printf("io_wait\n");
            return;
        }
        printf("drizzle_con_accpet error:%s\n", drizzle_error(client->drizzle));
        return;
    }

    fd = drizzle_con_fd(dc);
    printf("Accepted. fd:%d\n", fd);

    con = (connection_t *) calloc(1, sizeof(connection_t));
    con->fd = fd;
    con->client = client;
    con->dc = dc;

    add_connection(client, con);

}
Ejemplo n.º 3
0
static PyObject*
ConnectionObject_ping(ConnectionObject *self, PyObject *args)
{

    int status = 1;
    drizzle_return_t ret;
    drizzle_result_st result;

    drizzle_con_ping(self->con, &result, &ret);

    status = io_wait(self->con, ret);
    if (status == -1) {
        goto error;
    }

    Py_RETURN_NONE;
error:
    PyErr_SetString(PyExc_IOError, drizzle_error(drizzle));
    return NULL;
}
Ejemplo n.º 4
0
static PyObject*
ConnectionObject_rollback(ConnectionObject *self, PyObject *args)
{
    int status = 1;
    drizzle_return_t ret;
    drizzle_result_st result;
    
    while(status){
        (void)drizzle_query(self->con, &result, "ROLLBACK;", 9, &ret);
        status = io_wait(self->con, ret);
        if (status == -1){
            goto error;
        }
    }

    drizzle_result_free(&result);
    Py_RETURN_NONE;
error:
    drizzle_result_free(&result);
    PyErr_SetString(PyExc_IOError, drizzle_error(drizzle));
    return NULL;
}
Ejemplo n.º 5
0
void do_process(connection_t *con)
{
    drizzle_st          *drizzle;
    drizzle_return_t     ret;
    drizzle_con_st      *dc = con->dc;
    drizzle_command_t    command;
    uint8_t             *data = NULL;
    size_t               total;

    dbgin();

    drizzle = con->client->drizzle;

    /*
    ret = drizzle_con_wait(drizzle);
    if (ret != DRIZZLE_RETURN_OK) {
        printf("drizzle_con_wait failed:%s\n", drizzle_error(drizzle));
        return;
    }
    */

    while (1) {
        switch (con->state) {
            case INIT:
                /* Handshake packets */
                drizzle_con_set_protocol_version(dc, 10);
                drizzle_con_set_server_version(dc, "ev_drizzle_server demo 0.0.1");
                drizzle_con_set_thread_id(dc, 1);
                drizzle_con_set_scramble(dc, (const uint8_t *)"ABCDEFGHIJKLMNOPQRST");
                drizzle_con_set_capabilities(dc, DRIZZLE_CAPABILITIES_NONE);
                drizzle_con_set_charset(dc, 8);
                drizzle_con_set_status(dc, DRIZZLE_CON_STATUS_NONE);
                drizzle_con_set_max_packet_size(dc, DRIZZLE_MAX_PACKET_SIZE);

                con->state = HANDSHAKE_WRITE;
                break;

            case HANDSHAKE_WRITE:
                printf("handshake write\n");

                ret = drizzle_handshake_server_write(dc);

                if (ret == DRIZZLE_RETURN_IO_WAIT) {
                    return;
                }

                if (ret == DRIZZLE_RETURN_OK) {
                    stop_watch_write(con);
                    con->state = HANDSHAKE_READ;
                    break;
                }
                printf("drizzle_handshake_server_write error:%s",
                        drizzle_error(drizzle));
                exit(1);
                break;
            case HANDSHAKE_READ:
                printf("handshake read\n");

                /* prepare libdrizzle internal event */
                dc->revents |= POLLIN;

                ret = drizzle_handshake_client_read(dc);

                if (ret == DRIZZLE_RETURN_IO_WAIT) {
                    return;
                }

                if (ret == DRIZZLE_RETURN_OK) {
                    con->state = HANDSHAKE_DONE;
                    break;
                }
                printf("drizzle_handshake_server_read error:%s",
                        drizzle_error(drizzle));
                exit(1);
                break;

            case HANDSHAKE_DONE:
                printf("handshake done\n");
                con->result = (drizzle_result_st *)
                    malloc(sizeof(drizzle_result_st));
                if (drizzle_result_create(dc, con->result) == NULL) {
                    printf("drizzle_result_create error:%s",
                            drizzle_error(drizzle));
                    exit(1);
                }

                ret = drizzle_result_write(dc, con->result, true);
                if (ret != DRIZZLE_RETURN_OK) {
                    printf("drizzle_result_write error:%s",
                            drizzle_error(drizzle));
                    exit(1);
                }
                con->state = PREPARE_COMMAND;
                break;
            case PREPARE_COMMAND:
                printf("prepare command\n");

                /* prepare libdrizzle internal event */
                dc->revents |= POLLIN;

                drizzle_result_free(dc->result);

                data = (uint8_t *) drizzle_con_command_buffer(dc, &command,
                        &total, &ret);


                if (ret == DRIZZLE_RETURN_LOST_CONNECTION ||
                        (ret == DRIZZLE_RETURN_OK &&
                         command == DRIZZLE_COMMAND_QUIT))
                {
                    free(data);

                    do_finalize(con);
                    return;
                }

                if (ret == DRIZZLE_RETURN_IO_WAIT) {
                    return;
                }

                printf("Command data:%s\n", data);

                if (drizzle_result_create(dc, con->result) == NULL) {
                    printf("drizzle_result_create error:%s",
                            drizzle_error(drizzle));
                    exit(1);
                }

                if (command != DRIZZLE_COMMAND_QUERY) {
                    printf("command is not QUERY command\n");
                    ret = drizzle_result_write(dc, dc->result, true);
                    if (ret != DRIZZLE_RETURN_OK) {
                        printf("drizzle_result_write error:%s",
                                drizzle_error(drizzle));
                        exit(1);
                    }
                    continue;
                }

                ret = drizzle_result_write(dc, dc->result, true);
                if (ret != DRIZZLE_RETURN_OK) {
                    printf("drizzle_result_write error:%s",
                            drizzle_error(drizzle));
                    exit(1);
                }

                return;
                break;
            default:
                printf("Unsupported state %d\n", con->state);
                //sleep(10);
        }
    }

}
Ejemplo n.º 6
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;
}
Ejemplo n.º 7
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;
}
Ejemplo n.º 8
0
int main(int argc, char *argv[])
{
  (void) argc;
  (void) argv;
  drizzle_row_t row;
  int num_fields;
  drizzle_result_st *result;
  int table_size;
#ifdef TEST_PREPARED_STATEMENTS
  drizzle_stmt_st *sth;
#endif
  
  drizzle_st *con= drizzle_create(getenv("MYSQL_SERVER"),
                                  getenv("MYSQL_PORT") ? atoi("MYSQL_PORT") : DRIZZLE_DEFAULT_TCP_PORT,
                                  getenv("MYSQL_USER"),
                                  getenv("MYSQL_PASSWORD"),
                                  getenv("MYSQL_SCHEMA"), 0);
  ASSERT_NOT_NULL_(con, "Drizzle connection object creation error");
  
  drizzle_return_t driz_ret= drizzle_connect(con);
  if (driz_ret == DRIZZLE_RETURN_COULD_NOT_CONNECT)
  {
    char error[DRIZZLE_MAX_ERROR_SIZE];
    strncpy(error, drizzle_error(con), DRIZZLE_MAX_ERROR_SIZE);
    drizzle_quit(con);
    SKIP_IF_(driz_ret == DRIZZLE_RETURN_COULD_NOT_CONNECT, "%s(%s)", error, drizzle_strerror(driz_ret));
  }
  ASSERT_EQ_(DRIZZLE_RETURN_OK, driz_ret, "drizzle_connect(): %s(%s)", drizzle_error(con), drizzle_strerror(driz_ret));
  
  CHECKED_QUERY("DROP SCHEMA IF EXISTS test_nulls");
  
  CHECKED_QUERY("CREATE SCHEMA test_nulls");
  
  driz_ret= drizzle_select_db(con, "test_nulls");
  ASSERT_EQ_(DRIZZLE_RETURN_OK, driz_ret, "USE test_nulls");
  
  CHECKED_QUERY("create table test_nulls.t1 (a int, b int, c int, d int, e int, f int, g int, h int, i int, j int, k int)");
  
#define NCOLS 11
  
  char *querybuf = calloc(256 + 60*64, 1);
  strcpy(querybuf, "insert into test_nulls.t1 values ");
  char *p = querybuf + strlen(querybuf);
  
  for(int sym = 0; sym < 64; sym++) {
    int cn = 0;
    *p++ = '(';
#define APPENDMAYBE(b)  if (sym & b) { strcpy(p, "NULL"); p += 4; } else { p += sprintf(p, "%d", sym*NCOLS + cn); }
    APPENDMAYBE(0x01); *p++ = ','; cn++;
    APPENDMAYBE(0x02); *p++ = ','; cn++;
    APPENDMAYBE(0x04);             cn++;
    p += sprintf(p, ",%d,%d,%d,%d,", sym*NCOLS + 3, sym*NCOLS + 4, sym*NCOLS + 5, sym*NCOLS + 6);
    cn += 4;
    APPENDMAYBE(0x08);             cn++;
    p += sprintf(p, ",%d,", sym*NCOLS + 8); cn++;
    APPENDMAYBE(0x10); *p++ = ','; cn++;
    APPENDMAYBE(0x20);             cn++;
    *p++ = ')';
    if (sym < 63)
      *p++ = ',';
#undef APPENDMAYBE
  }
  CHECKED_QUERY(querybuf);
  
#ifdef TEST_PREPARED_STATEMENTS
  strcpy(querybuf, "insert into test_nulls.t1 values (?,?,?,?,?,?,?,?,?,?,?)");
  sth = drizzle_stmt_prepare(con, querybuf, strlen(querybuf), &driz_ret);
  ASSERT_EQ_(driz_ret, DRIZZLE_RETURN_OK, "Error (%s): %s, preparing \"%s\"", drizzle_strerror(driz_ret), drizzle_error(con), querybuf);
  
  for(int sym = 0; sym < 64; sym++) {
    // driz_ret= drizzle_stmt_reset(sth);
    // ASSERT_EQ_(driz_ret, DRIZZLE_RETURN_OK, "Error (%s): %s, resetting statement", drizzle_strerror(driz_ret), drizzle_error(con));
    
#define SETMAYBE(cn,b) if (sym & b) driz_ret = drizzle_stmt_set_null(sth, cn); else SETALWAYS(cn);
#define SETALWAYS(cn) driz_ret = drizzle_stmt_set_short(sth, cn, sym*NCOLS + cn + 1000, 0)
    ASSERT_EQ(driz_ret, DRIZZLE_RETURN_OK);
    SETMAYBE(0,0x01);
    SETMAYBE(1,0x02);
    SETMAYBE(2,0x04);
    SETALWAYS(3);
    SETALWAYS(4);
    SETALWAYS(5);
    SETALWAYS(6);
    SETMAYBE(7,0x08);
    SETALWAYS(8);
    SETMAYBE(9,0x10);
    SETMAYBE(10,0x20);
#undef SETMAYBE
    
    driz_ret = drizzle_stmt_execute(sth);
    ASSERT_EQ_(driz_ret, DRIZZLE_RETURN_OK, "Error (%s): %s, executing insert, sym=%d", drizzle_strerror(driz_ret), drizzle_error(con), sym);
    
    driz_ret = drizzle_stmt_buffer(sth);
    ASSERT_EQ_(driz_ret, DRIZZLE_RETURN_OK, "Error (%s): %s, buffering result, sym=%d", drizzle_strerror(driz_ret), drizzle_error(con), sym);
    
    // ASSERT_EQ(drizzle_stmt_affected_rows, 1);
  }
  
  ASSERT_EQ(drizzle_stmt_close(sth), DRIZZLE_RETURN_OK);
  
  table_size = 128;
#else
  table_size = 64;
#endif
  
  CHECKED_QUERY("select a,b,c,d,e,f,g,h,i,j,k from test_nulls.t1 order by e");
  drizzle_result_buffer(result);
  num_fields= drizzle_result_column_count(result);
  ASSERT_EQ_(num_fields, NCOLS, "Bad number of fields, expected %d, got %d", 11, num_fields);
  
  int cur_row = 0;
  char nbuf[16];
  while ((row = drizzle_row_next(result))) {
    cur_row ++;
    int sym, rowbase;
    
    /* 'sym' is the value used to decide which fields have NULLs or not.
     * 'rowbase' is the number that would be stored in the first field of this
     * row (assuming it's non-NULL).
     */
    
    if (cur_row <= 64) {
      sym = cur_row - 1;
      rowbase = sym*NCOLS;
    } else {
      sym = cur_row - 65;
      rowbase = 1000 + sym*NCOLS;
    }
    
#define NULLMAYBE(cn, b)  if (sym & b) { ASSERT_NULL_(row[cn], "Column %d, row %d should be NULL", cn+1, cur_row); } else { ASSERT_NOT_NULL_(row[cn], "Column %d, row %d should not be NULL", cn+1, cur_row); sprintf(nbuf, "%d", rowbase+cn); ASSERT_STREQ(row[cn], nbuf); }
#define NULLNEVER(cn) NULLMAYBE(cn, 0)
    
    NULLMAYBE(0, 0x01);
    NULLMAYBE(1, 0x02);
    NULLMAYBE(2, 0x04);
    NULLNEVER(3);
    NULLNEVER(4);
    NULLNEVER(5);
    NULLNEVER(6);
    NULLMAYBE(7, 0x08);
    NULLNEVER(8);
    NULLMAYBE(9, 0x10);
    NULLMAYBE(10, 0x20);
    
#undef NULLMAYBE
#undef NULLNEVER
  }
  ASSERT_EQ(cur_row, table_size);
  
  drizzle_result_free(result);
  
#ifdef TEST_PREPARED_STATEMENTS
  strcpy(querybuf, "select a,b,c,d,e,f,g,h,i,j,k from test_nulls.t1 order by e");
  sth = drizzle_stmt_prepare(con, querybuf, strlen(querybuf), &driz_ret);
  ASSERT_EQ_(driz_ret, DRIZZLE_RETURN_OK, "Error (%s): %s, preparing \"%s\"", drizzle_strerror(driz_ret), drizzle_error(con), querybuf);
  driz_ret = drizzle_stmt_execute(sth);
  ASSERT_EQ_(driz_ret, DRIZZLE_RETURN_OK, "Error (%s): %s, executing \"%s\"", drizzle_strerror(driz_ret), drizzle_error(con), querybuf);
  
  cur_row = 0;
  for (;;) {
    driz_ret = drizzle_stmt_fetch(sth);
    if (driz_ret == DRIZZLE_RETURN_ROW_END)
      break;
    cur_row ++;
    ASSERT_EQ_(driz_ret, DRIZZLE_RETURN_OK, "Error (%s): %s, fetching row #%d", drizzle_strerror(driz_ret), drizzle_error(con), cur_row);
    
    /* 'sym' is the value used to decide which fields have NULLs or not.
     * 'rowbase' is the number that would be stored in the first field of this
     * row (assuming it's non-NULL).
     */
    
    int sym, rowbase;
    
    if (cur_row <= 64) {
      sym = cur_row - 1;
      rowbase = sym*NCOLS;
    } else {
      sym = cur_row - 65;
      rowbase = 1000 + sym*NCOLS;
    }
    
    bool isNull;
    uint32_t rowvalue;
    
#define GETNULLNESS(cn) isNull = drizzle_stmt_get_is_null(sth, cn, &driz_ret); ASSERT_EQ(driz_ret, DRIZZLE_RETURN_OK);
#define NULLNOTNOW(cn) ASSERT_FALSE_(isNull, "Column %d, row %d should not be NULL", cn+1, cur_row); rowvalue = drizzle_stmt_get_int(sth, cn, &driz_ret); ASSERT_EQ(driz_ret,DRIZZLE_RETURN_OK); ASSERT_EQ_(rowvalue, (unsigned)(rowbase + cn), "Column %d, row %d has unexpected data, expected: %d, got: %d", cn+1, cur_row, rowbase+cn, rowvalue);
#define NULLMAYBE(cn, b) GETNULLNESS(cn); if (sym & b) { ASSERT_TRUE_(isNull, "Column %d, row %d should be NULL", cn+1, cur_row); } else { NULLNOTNOW(cn); }
#define NULLNEVER(cn) GETNULLNESS(cn); NULLNOTNOW(cn);
    
    NULLMAYBE(0, 0x01);
    NULLMAYBE(1, 0x02);
    NULLMAYBE(2, 0x04);
    NULLNEVER(3);
    NULLNEVER(4);
    NULLNEVER(5);
    NULLNEVER(6);
    NULLMAYBE(7, 0x08);
    NULLNEVER(8);
    NULLMAYBE(9, 0x10);
    NULLMAYBE(10, 0x20);
    
#undef NULLMAYBE
#undef NULLNEVER
#undef GETNULLNESS
#undef NULLNOTNOW
  }
  ASSERT_EQ_(cur_row, table_size, "Current row not equal to table size, expected %d, got %d", table_size, cur_row);
  ASSERT_EQ(drizzle_stmt_close(sth), DRIZZLE_RETURN_OK);
#endif
  
  CHECKED_QUERY("DROP TABLE test_nulls.t1");
  ASSERT_EQ_(DRIZZLE_RETURN_OK, driz_ret, "DROP TABLE test_nulls.t1");
   
  CHECKED_QUERY("DROP SCHEMA IF EXISTS test_nulls");

  driz_ret= drizzle_quit(con);
  ASSERT_EQ_(DRIZZLE_RETURN_OK, driz_ret, "%s", drizzle_strerror(driz_ret));
 
  free(querybuf); 
  return EXIT_SUCCESS;
}
Ejemplo n.º 9
0
static int
ConnectionObject_init(ConnectionObject *self, PyObject *args, PyObject *kwargs)
{

    drizzle_con_st *con;
    
    //Not support
    PyObject *ssl = NULL;
    char *host = DRIZZLE_DEFAULT_TCP_HOST;
    char *user = NULL, *passwd = NULL, *db = NULL, *unix_socket = NULL;
    unsigned int port = 3306;
    unsigned int client_flag = 0;
    int connect_timeout = 0;

    static char *kwlist[] = { "host", "user", "passwd", "db", "port", "unix_socket", "connect_timeout", NULL } ;
  
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ssssisi:connect",
           kwlist,
           &host, &user, &passwd, &db,
           &port, &unix_socket,
           &connect_timeout
           )){
        return -1;
    }

    DEBUG("init %p", self);  


    //set NON_BLOKING
    /* drizzle_add_options(self->drizzle, DRIZZLE_NON_BLOCKING); */

    Py_BEGIN_ALLOW_THREADS ;
    if (connect_timeout) {
        drizzle_set_timeout(drizzle, connect_timeout);
        DEBUG("set timeout %d", connect_timeout);  
    }

    DEBUG("host %s", host);
    DEBUG("port %d", port);
    DEBUG("user %s", user);
    DEBUG("passwd %s", passwd);
    DEBUG("db %s", db);

    if (!unix_socket) {
      con = drizzle_con_add_tcp(drizzle, NULL, host, port, user, passwd, db, DRIZZLE_CON_MYSQL);
    } else {
      con = drizzle_con_add_uds(drizzle, NULL, unix_socket, user, passwd, db, DRIZZLE_CON_MYSQL);
    }
    Py_END_ALLOW_THREADS;
    DEBUG("created drizzle conn %p", con);  
    DEBUG("connect %p", con);  

    if (!con) {
        PyErr_SetString(PyExc_IOError, drizzle_error(drizzle));
        RDEBUG("drizzle connection failed");
        return -1;
    }
    self->con = con;
    if (wait_connect(con) == -1) {
        return -1;
    }

    return 0;
}
Ejemplo n.º 10
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;
}
Ejemplo n.º 11
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;
}
Ejemplo n.º 12
0
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;
}
Ejemplo n.º 13
0
int
io_wait(drizzle_con_st *con, drizzle_return_t ret)
{
    drizzle_return_t dret;
    int fd = 0, events = 0;
    PyObject *fileno, *state, *args, *res;

    if (ret == DRIZZLE_RETURN_OK) {
        return 0;
    }else if (ret == DRIZZLE_RETURN_IO_WAIT) {
        events = con->events;

        YDEBUG("IO_WAIT con:%p events:%d", con, events);
        if (external_io_wait) {
            fd = drizzle_con_fd(con);
            if (fd == -1){
                return -1;
            }

            fileno = PyLong_FromLong((long)fd);
            if (fileno == NULL) {
                return -1;
            }
            state = PyLong_FromLong((long)events);
            if (state == NULL) {
                Py_DECREF(fileno);
                return -1;
            }

            args = PyTuple_Pack(2, fileno, state);
            if (args == NULL) {
                Py_DECREF(fileno);
                Py_DECREF(state);
                return -1;
            }

            YDEBUG("call external_io_wait ...");
            res = PyObject_CallObject(external_io_wait, args);
            Py_DECREF(args);

            if (res == NULL) {
                return -1;
            }
            Py_XDECREF(res);
            dret = drizzle_con_set_revents(con, events);
            if (dret != DRIZZLE_RETURN_OK){
                RDEBUG("ret %d:%s", dret, drizzle_error(drizzle));
                return -1;
            }
            return 1;
        } else {
            DEBUG("call drizzle_con_wait ...");
            dret = drizzle_con_wait(drizzle);

            if (dret != DRIZZLE_RETURN_OK){
                RDEBUG("ret %d:%s", dret, drizzle_error(drizzle));
                return -1;
            }
            return 1;
        }
    }else{
        RDEBUG("ret %d:%s", ret, drizzle_error(drizzle));
        return -1;
    }
    return 0;
}