예제 #1
0
int MysqlResultSet_next(T R) {
	assert(R);
        if (R->stop)
                return false;
        if (R->maxRows && (R->currentRow++ >= R->maxRows)) {
                R->stop = true;
#if MYSQL_VERSION_ID >= 50002
                /* Seems to need a cursor to work */
                mysql_stmt_reset(R->stmt); 
#else
                /* Bhaa! Where's my cancel method? 
                   Pencil neck mysql developers! */
                while (mysql_stmt_fetch(R->stmt) == 0); 
#endif
                return false;
        }
        if (R->needRebind) {
                if ((R->lastError = mysql_stmt_bind_result(R->stmt, R->bind)))
                        THROW(SQLException, "mysql_stmt_bind_result -- %s", mysql_stmt_error(R->stmt));
                R->needRebind = false;
        }
        R->lastError = mysql_stmt_fetch(R->stmt);
        if (R->lastError == 1)
                THROW(SQLException, "mysql_stmt_fetch -- %s", mysql_stmt_error(R->stmt));
        return ((R->lastError == MYSQL_OK) || (R->lastError == MYSQL_DATA_TRUNCATED));
}
예제 #2
0
파일: database.c 프로젝트: HalosGhost/lwan
static bool db_stmt_bind_mysql(const struct db_stmt *stmt,
        struct db_row *rows, size_t n_rows)
{
    struct db_stmt_mysql *stmt_mysql = (struct db_stmt_mysql *)stmt;

    stmt_mysql->must_execute_again = true;

    if (!stmt_mysql->param_bind) {
        stmt_mysql->param_bind = calloc(n_rows, sizeof(*stmt_mysql->param_bind));
        if (!stmt_mysql->param_bind)
            return false;
    } else {
        mysql_stmt_reset(stmt_mysql->stmt);
    }

    for (size_t row = 0; row < n_rows; row++) {
        if (rows[row].kind == '\0') break;

        MYSQL_BIND *param = &stmt_mysql->param_bind[row];
        if (rows[row].kind == 's') {
            param->buffer_type = MYSQL_TYPE_STRING;
            param->buffer = rows[row].u.s;
        } else if (rows[row].kind == 'i') {
            param->buffer_type = MYSQL_TYPE_LONG;
            param->buffer = &rows[row].u.i;
        }
        param->is_null = false;
        param->length = 0;
    }

    return !mysql_stmt_bind_param(stmt_mysql->stmt, stmt_mysql->param_bind);
}
예제 #3
0
int test_sp_reset1(MYSQL *mysql)
{
  int rc;
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];

  char tmp[20];
  char *stmtstr= "CALL P1(?)";

  rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "CREATE PROCEDURE p1(OUT p_out VARCHAR(19))" 
                         "BEGIN "
                          "  SET p_out = 'foo';"
                          "  SELECT 'foo' FROM DUAL;"
                          "  SELECT 'bar' FROM DUAL;"
                         "END");
  check_mysql_rc(rc, mysql);

  stmt= mysql_stmt_init(mysql);
  check_mysql_rc(rc, mysql);

  rc= mysql_stmt_prepare(stmt, stmtstr, strlen(stmtstr));
  check_stmt_rc(rc, stmt);

  memset(tmp, 0, sizeof(tmp));
  memset(bind, 0, sizeof(MYSQL_BIND));
  bind[0].buffer= tmp;
  bind[0].buffer_type= MYSQL_TYPE_STRING;
  bind[0].buffer_length= 4;

  mysql_stmt_bind_param(stmt, bind);

  rc= mysql_stmt_execute(stmt);
  check_stmt_rc(rc, stmt);

  rc= mysql_stmt_store_result(stmt);
  check_stmt_rc(rc, stmt);

  rc= mysql_stmt_next_result(stmt);
  check_stmt_rc(rc, stmt);

  rc= mysql_stmt_fetch(stmt);
  check_stmt_rc(rc, stmt);

  /* mysql_stmt_reset should set statement in prepared state.
   * this means: all subsequent result sets should be flushed.
   * Let's try!
   */ 
  rc= mysql_stmt_reset(stmt);
  check_stmt_rc(rc, stmt);

  rc= mysql_query(mysql, "DROP PROCEDURE p1");
  check_mysql_rc(rc, mysql);

  mysql_stmt_close(stmt);
  return OK;
}
예제 #4
0
void Statement::ResetParameters() { 
	while (! _params.empty()) {
		ParamBuffer *buf = _params.back();
		_params.pop_back();
		delete buf;
	}	

	mysql_stmt_reset(_stmt);
	mysql_stmt_free_result(_stmt);
	_eof = true;
}
예제 #5
0
bool SqlStatement::reset()
{
	if (mysql_stmt_reset(stmt_) == 0) {
		currentRow_ = 0;
		error_ = NULL;
		return true;
	} else {
		error_ = mysql_stmt_error(stmt_);
		return false;
	}
}
예제 #6
0
int test_sp_reset(MYSQL *mysql)
{
 int i, rc;
  MYSQL_STMT *stmt;
  int a[] = {10,20,30};
  MYSQL_BIND bind[3];
  char *stmtstr= "CALL P1(?,?,?)";

  rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
  check_mysql_rc(rc, mysql);

  rc= mysql_query(mysql, "CREATE PROCEDURE p1(OUT p_out VARCHAR(19), IN p_in INT, INOUT p_inout INT)" 
                         "BEGIN "
                          "  SET p_in = 300, p_out := 'This is OUT param', p_inout = 200; "
                          "  SELECT p_inout, p_in, substring(p_out, 9);"
                         "END");
  check_mysql_rc(rc, mysql);

  stmt= mysql_stmt_init(mysql);
  check_mysql_rc(rc, mysql);

  rc= mysql_stmt_prepare(stmt, stmtstr, strlen(stmtstr));
  check_stmt_rc(rc, stmt);

  FAIL_IF(mysql_stmt_param_count(stmt) != 3, "expected param_count=3");

  memset(bind, 0, sizeof(MYSQL_BIND) * 3);
  for (i=0; i < 3; i++)
  {
    bind[i].buffer= &a[i];
    bind[i].buffer_type= MYSQL_TYPE_LONG;
  }
  bind[0].buffer_type= MYSQL_TYPE_NULL;
  rc= mysql_stmt_bind_param(stmt, bind);
  check_stmt_rc(rc, stmt);

  rc= mysql_stmt_execute(stmt);
  check_stmt_rc(rc, stmt);

  rc= mysql_stmt_fetch(stmt);
  check_stmt_rc(rc, stmt);

  rc= mysql_stmt_reset(stmt);
  check_stmt_rc(rc, stmt);

  /*connection shouldn't be blocked now */

  rc= mysql_query(mysql, "DROP PROCEDURE p1");
  check_mysql_rc(rc, mysql);

  rc= mysql_stmt_close(stmt);
  return OK;
}
예제 #7
0
void MysqlPreparedStatement_execute(T P) {
        assert(P);
        if (P->parameterCount > 0)
                if ((P->lastError = mysql_stmt_bind_param(P->stmt, P->bind)))
                        THROW(SQLException, "%s", mysql_stmt_error(P->stmt));
#if MYSQL_VERSION_ID >= 50002
        unsigned long cursor = CURSOR_TYPE_NO_CURSOR;
        mysql_stmt_attr_set(P->stmt, STMT_ATTR_CURSOR_TYPE, &cursor);
#endif
        if ((P->lastError = mysql_stmt_execute(P->stmt)))
                THROW(SQLException, "%s", mysql_stmt_error(P->stmt));
        if (P->lastError == MYSQL_OK) {
                /* Discard prepared param data in client/server */
                P->lastError = mysql_stmt_reset(P->stmt);
        }
}
예제 #8
0
static int gt_rdb_stmt_mysql_reset(GtRDBStmt *st, GtError *err)
{
  GtRDBStmtMySQL *stm;
  int rval, had_err = 0;
  gt_assert(st);
  gt_error_check(err);
  stm = gt_rdb_stmt_mysql_cast(st);
  gt_hashtable_reset(stm->buffers);
  gt_hashtable_reset(stm->returned_strings);
  mysql_stmt_free_result(stm->stmt);
  if ((rval = mysql_stmt_reset(stm->stmt))) {
    gt_error_set(err, GT_MYSQL_ERRMSG, rval, mysql_stmt_error(stm->stmt));
    had_err = -1;
  }
  memset(stm->params, 0, stm->num_params*sizeof (MYSQL_BIND));
  gt_free(stm->results);
  stm->results = NULL;
  if (!had_err)
    stm->executed = false;
  return had_err;
}
예제 #9
0
    int exec() {

      id = rand()%10000;

      // reset statement for reuse
      if(mysql_stmt_reset(stmt)) throw runtime_error(mysql_stmt_error(stmt));

      // create and use input parameter
      memset(param, 0, sizeof(param));
      param[0].buffer_type = MYSQL_TYPE_LONG;
      param[0].buffer = (char *)&id;
      param[0].buffer_length = sizeof(id);
      param[0].is_null = 0;
      param[0].length = NULL;
      if(mysql_stmt_bind_param(stmt,param)) throw runtime_error(mysql_stmt_error(stmt));

      // create and fill result buffer
      memset(results, 0, sizeof(results));
      results[0].buffer_type= MYSQL_TYPE_LONG;
      results[0].buffer = &randomNumber;
      results[0].buffer_length = sizeof(randomNumber);
      results[0].is_null = 0;
      results[0].length = &len1;
      if(mysql_stmt_bind_result(stmt, results)) throw runtime_error(mysql_stmt_error(stmt));

      // execute statement
      if(mysql_stmt_execute(stmt)) throw runtime_error(mysql_stmt_error(stmt));
      if(mysql_stmt_fetch(stmt)) randomNumber = 0;

      // output json
      JsonObj obj;
      obj.add("id",id);
      obj.add("randomNumber",randomNumber);
      res << obj.to_s();

      return res.end();
    }
예제 #10
0
int main (int argc, char *argv[]) {

    MYSQL *mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;
    my_bool reconnect = 0;
    mysql = mysql_init(NULL);
    mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);

    CONN(0);

    MYSQL_STMT    *stmt;
    MYSQL_BIND    bind[1];
    MYSQL_BIND    bResult[13];
    unsigned long length[3];

    long long       long_data;
    char        tiny_data;
    char        blob[1000];
    MYSQL_TIME   date;
    char        decimal[1000];
    double      d;
    float       f;
    int         i;
    short       s;
    int         m;
    char        str[1000];
    MYSQL_TIME  time;
    MYSQL_TIME  timestamp;
    // TODO datetime
    // year type 
    
    length[0] = sizeof(str);
    length[1] = sizeof(str);
    length[2] = sizeof(str);

    stmt = mysql_stmt_init(mysql);

    char *normalSql = "select * from test1";
    char *stmtSql = "select longcol, bytecol, blobcol, datecol, decimalcol, doublecol, floatcol, intcol, nullcol, shortcol, medcol, stringcol, timecol, timestampcol from test1 where bytecol = ?";

    mysql_stmt_prepare(stmt, stmtSql, strlen(stmtSql));

    /* Bind the data for all 3 parameters */

    memset(bind, 0, sizeof(bind));

    bind[0].buffer_type= MYSQL_TYPE_TINY;
    bind[0].buffer= (char *)&tiny_data;
    bind[0].is_null= 0;

    mysql_stmt_bind_param(stmt, bind);

    mysql_query(mysql, normalSql);

    result = mysql_store_result(mysql);

    while(mysql_fetch_row(result));

    mysql_free_result(result);

    /* --- */
    memset(bResult, 0, sizeof(bResult));

    bResult[0].buffer_type= MYSQL_TYPE_LONGLONG;
    bResult[0].buffer= (char *)&long_data;

    bResult[1].buffer_type= MYSQL_TYPE_TINY;
    bResult[1].buffer= (char *)&tiny_data;

    bResult[2].buffer_type= MYSQL_TYPE_BLOB;
    bResult[2].buffer= (char *)&blob;
    bResult[2].buffer_length = length[0];

    bResult[3].buffer_type= MYSQL_TYPE_DATE;
    bResult[3].buffer= (char *)&date;

    bResult[4].buffer_type= MYSQL_TYPE_DECIMAL;
    bResult[4].buffer= (char *)&decimal;
    bResult[4].buffer_length= length[1];

    bResult[5].buffer_type= MYSQL_TYPE_DOUBLE;
    bResult[5].buffer= (char *)&d;

    bResult[6].buffer_type= MYSQL_TYPE_FLOAT;
    bResult[6].buffer= (char *)&f;

    bResult[7].buffer_type= MYSQL_TYPE_LONG;
    bResult[7].buffer= (char *)&i;

    bResult[8].buffer_type= MYSQL_TYPE_NULL;

    bResult[9].buffer_type= MYSQL_TYPE_SHORT;
    bResult[9].buffer= (char *)&s;

    bResult[10].buffer_type= MYSQL_TYPE_INT24;
    bResult[10].buffer= (char *)&m;

    bResult[11].buffer_type= MYSQL_TYPE_STRING;
    bResult[11].buffer= (char *)&str;
    bResult[11].buffer_length = length[2];

    bResult[12].buffer_type= MYSQL_TYPE_TIME;
    bResult[12].buffer= (char *)&time;

    bResult[13].buffer_type= MYSQL_TYPE_TIMESTAMP;
    bResult[13].buffer= (char *)&timestamp;

    mysql_stmt_bind_result(stmt, bResult);

    //singnode
    tiny_data = 1;
    mysql_stmt_execute(stmt);

    mysql_stmt_store_result(stmt); 

    int ii = 0;
    while(!mysql_stmt_fetch(stmt)) {
        printf("%d[%lld, %d, %s, %lf, %f, %d, %hd, %d, %s]\n",  ii++,
            long_data, tiny_data, decimal, d, f, i, s, m, str); 
    }

    mysql_query(mysql, normalSql);

    result = mysql_store_result(mysql);

    while(mysql_fetch_row(result));

    mysql_free_result(result);

    //singnode;
    tiny_data = 2;

    mysql_stmt_execute(stmt);

    mysql_stmt_store_result(stmt); 

    while(!mysql_stmt_fetch(stmt)) {
        printf("%d[%lld, %d, %s, %lf, %f, %d, %hd, %d, %s, %u, %u, %u, %u,%u,%u, %u, %u, %u, %u, %u, %u ]\n", ii++,
            long_data, tiny_data, decimal, d, f, i, s, m, str, 
            date.year, date.month, date.day, time.hour, time.minute, time.second, 
            timestamp.year, timestamp.month, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second); 
    }

    mysql_stmt_reset(stmt);
    mysql_stmt_close(stmt);
}
예제 #11
0
파일: mysql.c 프로젝트: d-cent/shardcached
static int st_fetch(void *key, size_t klen, void **value, size_t *vlen, void *priv)
{
    storage_mysql_t *st = (storage_mysql_t *)priv;

    char *keystr = malloc((klen*2)+ 1);
    char *p = (char *)key;
    char *o = (char *)keystr;
    int i;
    for (i = 0; i < klen; i++) {
        snprintf(o, 3, "%02x", p[i]);
        o += 2;
    }
    *o = 0;


    db_connection_t *dbc = st_get_dbconnection(st);
    if (!dbc) {
        free(keystr);
        return -1;
    }

    MYSQL_BIND bnd = {
        .buffer_type = MYSQL_TYPE_STRING,
        .buffer = keystr,
        .buffer_length = strlen(keystr)
    };

    if (mysql_stmt_bind_param(dbc->select_stmt, &bnd) != 0) {
        // TODO - error messages
        SPIN_UNLOCK(&dbc->lock);
        free(keystr);
        return -1;
    }

    if (mysql_stmt_execute(dbc->select_stmt) != 0) {
        // TODO - error messages
        fprintf(stderr, "Can't execute fetch statement : %s\n", mysql_stmt_error(dbc->select_stmt));
        SPIN_UNLOCK(&dbc->lock);
        free(keystr);
        return -1;
    }

    size_t size = 256;
    void *data = malloc(size);
    my_bool error = 0;

    MYSQL_BIND obnd = {
        .buffer_type = MYSQL_TYPE_LONG_BLOB,
        .buffer = data,
        .buffer_length = size,
        .length = &size,
        .error = &error
    };

    mysql_stmt_bind_result(dbc->select_stmt, &obnd);

    int rc = mysql_stmt_fetch(dbc->select_stmt);

    if (error == 1) {
        data = realloc(data, size);
        obnd.buffer = data;
        obnd.buffer_length = size;
        error = 0;
        mysql_stmt_bind_result(dbc->select_stmt, &obnd);
        mysql_stmt_fetch(dbc->select_stmt);
    }

    if (rc != 0 || obnd.is_null) {
        free(data);
        data = NULL;
    } else {

        if (value) {
            *value = data;
        } else {
            free(data);
        }

        if (vlen) {
            *vlen = size;
        }
    }

    mysql_stmt_free_result(dbc->select_stmt);
    mysql_stmt_reset(dbc->select_stmt);

    SPIN_UNLOCK(&dbc->lock);

    free(keystr);
    return 0;
}
예제 #12
0
int main (int argc, char *argv[]) {

    MYSQL *mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;
    my_bool reconnect = 0;
    mysql = mysql_init(NULL);
    mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);

    CONN(0);

    MYSQL_STMT    *stmt, *stmt2;
    MYSQL_BIND    bind[1];
    MYSQL_BIND    bResult[1];
    MYSQL_BIND    bResult2[1];
    unsigned long length[1];
    my_ulonglong  affected_rows;
    short         small_data;
    long long   long_data;
    char       int_data;
    my_bool       is_null;

    stmt = mysql_stmt_init(mysql);
    stmt2 = mysql_stmt_init(mysql);

    char *normalSql = "select longcol from test1 where bytecol = 1";
    char *stmtSql = "select longcol from test1 where bytecol = ?";
    //1
    mysql_stmt_prepare(stmt, stmtSql, strlen(stmtSql));
    memset(bind, 0, sizeof(bind));
    bind[0].buffer_type= MYSQL_TYPE_TINY;
    bind[0].buffer= (char *)&int_data;
    bind[0].is_null= 0;
    mysql_stmt_bind_param(stmt, bind);
    //2
    mysql_stmt_prepare(stmt2, normalSql, strlen(normalSql));
    //3
    mysql_query(mysql, normalSql);
    result = mysql_store_result(mysql);
    while(mysql_fetch_row(result));
    mysql_free_result(result);

    //2
    memset(bResult2, 0, sizeof(bResult2));
    bResult2[0].buffer_type= MYSQL_TYPE_LONGLONG;
    bResult2[0].buffer= (char *)&long_data;
    bResult2[0].is_null= &is_null;
    bResult2[0].length= &length[0];
    mysql_stmt_bind_result(stmt2, bResult2);

    //1
    memset(bResult, 0, sizeof(bResult));
    bResult[0].buffer_type= MYSQL_TYPE_LONGLONG;
    bResult[0].buffer= (char *)&long_data;
    bResult[0].is_null= &is_null;
    bResult[0].length= &length[0];
    mysql_stmt_bind_result(stmt, bResult);

    int_data= 1;
    mysql_stmt_execute(stmt2);
    mysql_stmt_store_result(stmt2); 

    while(!mysql_stmt_fetch(stmt2)) {
        printf("2[%lld]\n", long_data); 
    }
    //must execute -> store->fetch over,then mysql is ready status
    // other execute 
    mysql_stmt_execute(stmt);
    mysql_stmt_store_result(stmt); 

    while(!mysql_stmt_fetch(stmt)) {
        printf("1[%lld]\n", long_data); 
    }

    mysql_query(mysql, normalSql);

    result = mysql_store_result(mysql);

    while(mysql_fetch_row(result));

    mysql_free_result(result);

    int_data= 1;
    mysql_stmt_execute(stmt);
    mysql_stmt_store_result(stmt); 
    while(!mysql_stmt_fetch(stmt)) {
        printf("3[%lld]\n", long_data); 
    }

    mysql_stmt_execute(stmt2);

    mysql_stmt_store_result(stmt2); 

    while(!mysql_stmt_fetch(stmt2)) {
        printf("4[%lld]\n", long_data); 
    }

    mysql_stmt_reset(stmt);
    mysql_stmt_close(stmt);
}
예제 #13
0
void mysql_statement::reset()
{
  mysql_stmt_reset(stmt);
}
/**
 * Get all of the keys in the datastore.
 *
 * @param cls closure
 * @param proc function to call on each key
 * @param proc_cls closure for proc
 */
static void
mysql_plugin_get_keys (void *cls,
			PluginKeyProcessor proc,
			void *proc_cls)
{
  struct Plugin *plugin = cls;
  const char *query = "SELECT hash FROM gn090";
  int ret;
  MYSQL_STMT *statement;
  struct GNUNET_HashCode key;
  MYSQL_BIND cbind[1];
  unsigned long length;

  statement = GNUNET_MYSQL_statement_get_stmt (plugin->mc,
					       plugin->get_all_keys);
  if (statement == NULL)
  {
    GNUNET_MYSQL_statements_invalidate (plugin->mc);
    return;
  }
  if (mysql_stmt_prepare (statement, query, strlen (query)))
  {
    GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "mysql",
                     _("Failed to prepare statement `%s'\n"), query);
    GNUNET_MYSQL_statements_invalidate (plugin->mc);
    return;
  }
  GNUNET_assert (proc != NULL);
  if (mysql_stmt_execute (statement))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("`%s' for `%s' failed at %s:%d with error: %s\n"),
                "mysql_stmt_execute", query, __FILE__, __LINE__,
                mysql_stmt_error (statement));
    GNUNET_MYSQL_statements_invalidate (plugin->mc);
    return;
  }
  memset (cbind, 0, sizeof (cbind));
  cbind[0].buffer_type = MYSQL_TYPE_BLOB;
  cbind[0].buffer = &key;
  cbind[0].buffer_length = sizeof (key);
  cbind[0].length = &length;
  cbind[0].is_unsigned = GNUNET_NO;
  if (mysql_stmt_bind_result (statement, cbind))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("`%s' failed at %s:%d with error: %s\n"),
                "mysql_stmt_bind_result", __FILE__, __LINE__,
                mysql_stmt_error (statement));
    GNUNET_MYSQL_statements_invalidate (plugin->mc);
    return;
  }
  while (0 == (ret = mysql_stmt_fetch (statement)))
  {
    if (sizeof (struct GNUNET_HashCode) == length)
      proc (proc_cls, &key, 1);
  }
  if (ret != MYSQL_NO_DATA)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("`%s' failed at %s:%d with error: %s\n"),
		     "mysql_stmt_fetch", __FILE__, __LINE__,
		     mysql_stmt_error (statement));
    GNUNET_MYSQL_statements_invalidate (plugin->mc);
    return;
  }
  mysql_stmt_reset (statement);
}
예제 #15
0
파일: mysql_cursor.c 프로젝트: antono/gsql
GSQLCursorState
mysql_cursor_open (GSQLCursor *cursor)
{
	GSQL_TRACE_FUNC;
	MYSQL *mysql;
	GSQLEMySQLSession *e_session = NULL;
	GSQLEMySQLCursor  *e_cursor = NULL;
	GSQLEMySQLVariable *e_var;
	GSQLVariable *var;
	GSQLWorkspace *workspace = NULL;
	MYSQL_BIND *binds;
	MYSQL_FIELD *fields;
	gulong n, n_fields, is_null = 1;
	gchar error_str[2048];
	
	
	e_session = cursor->session->spec;
	workspace = gsql_session_get_workspace (cursor->session);
	
	mysql = e_session->mysql;
	
	if (!mysql_cursor_prepare (cursor))
	{
		return GSQL_CURSOR_STATE_ERROR;
	}
	
	e_cursor = cursor->spec;
	
	e_cursor->result = mysql_stmt_result_metadata(e_cursor->statement);

	if (mysql_stmt_execute(e_cursor->statement)) 
	{
		g_sprintf (error_str, "Error occured: %s", mysql_stmt_error (e_cursor->statement));
		GSQL_DEBUG ("%s", error_str);
		gsql_message_add (workspace, GSQL_MESSAGE_ERROR, error_str);
		mysql_stmt_reset (e_cursor->statement);

		return GSQL_CURSOR_STATE_ERROR;		
	}
	
	mysql_cursor_statement_detect (cursor);
	
	n_fields =  mysql_field_count (mysql);	
	fields = e_cursor->statement->fields;
	
	if (n_fields == 0)
		return GSQL_CURSOR_STATE_OPEN;
	
	binds = g_new0 (MYSQL_BIND, n_fields);
	e_cursor->binds = binds;
	
	for (n = 0; n < n_fields; n++)
	{
		GSQL_DEBUG ("field[%d] = %s", n, fields[n].name);
		var = gsql_variable_new ();
		mysql_variable_init (var, &fields[n], &binds[n]);
		cursor->var_list = g_list_append (cursor->var_list, var);
	}
	
	
	
	if (mysql_stmt_bind_result (e_cursor->statement, binds))
	{
		g_sprintf (error_str, "Error occured: %s", mysql_stmt_error (e_cursor->statement));
		gsql_message_add (workspace, GSQL_MESSAGE_ERROR, error_str);
		g_free (binds);
		mysql_stmt_reset (e_cursor->statement);

		return GSQL_CURSOR_STATE_ERROR;
	}

	return GSQL_CURSOR_STATE_OPEN;
}
예제 #16
0
파일: mysql_cursor.c 프로젝트: antono/gsql
GSQLCursorState
mysql_cursor_open_bind (GSQLCursor *cursor, GList *args)
{
	GSQL_TRACE_FUNC;
	MYSQL *mysql;
	GSQLEMySQLSession *e_session = NULL;
	GSQLEMySQLCursor  *e_cursor = NULL;
	GSQLEMySQLVariable *e_var;
	GSQLVariable *var;
	GSQLWorkspace *workspace = NULL;
	MYSQL_BIND *binds;
	MYSQL_FIELD *fields;
	gulong binds_count = 0, binds_arg, n, n_fields, is_null = 1;
	gulong str_len;
	GList *vlist = args;
	GType vtype;
	gdouble affect = 0;
	gchar error_str[2048];
	
	e_session = cursor->session->spec;
	workspace = gsql_session_get_workspace (cursor->session);
	
	mysql = e_session->mysql;
	
	if (!mysql_cursor_prepare (cursor))
	{
		return GSQL_CURSOR_STATE_ERROR;
	}
	
	e_cursor = cursor->spec;
	
	binds_count = mysql_stmt_param_count(e_cursor->statement);
	binds_arg = g_list_length (args) / 2;
	
	if (binds_arg != binds_count)
	{
		GSQL_DEBUG ("Bind count is wrong. Need [%d]. Got [%f]", binds_count, binds_arg);
		mysql_stmt_reset (e_cursor->statement);

		return GSQL_CURSOR_STATE_ERROR;
	}

	binds = g_new0 (MYSQL_BIND, binds_count);
	n = 0;

	while (vlist)
	{
		vtype = (GType) vlist->data;
		vlist = g_list_next (vlist);
		if (vlist->data == NULL)
			is_null = 1;
		else 
			is_null = 0;
		switch (vtype)
		{
			case G_TYPE_STRING:
			case G_TYPE_POINTER:
				binds[n].buffer_type = MYSQL_TYPE_STRING;
				binds[n].buffer = (char *) vlist->data;
				binds[n].buffer_length = g_utf8_strlen((gchar *) vlist->data, 1048576);
				binds[n].is_null= 0;
				binds[n].length = NULL;
				break;
			
			case G_TYPE_INT:
			case G_TYPE_UINT:
				binds[n].buffer_type = MYSQL_TYPE_LONG;
				binds[n].buffer = (char *) &vlist->data;
				binds[n].is_null= (my_bool*) &is_null;
				break;
			
			case G_TYPE_UINT64:
			case G_TYPE_INT64:
				binds[n].buffer_type = MYSQL_TYPE_LONGLONG;
				binds[n].buffer = (char *) &vlist->data;
				binds[n].is_null= (my_bool*) &is_null;
				break;
			
			case G_TYPE_DOUBLE:
				binds[n].buffer_type = MYSQL_TYPE_DOUBLE;
				binds[n].buffer = (char *) &vlist->data;
				binds[n].is_null= (my_bool*) &is_null;
				break;
				
		}
		
		vlist = g_list_next (vlist);
		n++;
	}

	if ((mysql_stmt_bind_param (e_cursor->statement, binds)) ||
		(!(e_cursor->result = mysql_stmt_result_metadata(e_cursor->statement))) ||
		(mysql_stmt_execute(e_cursor->statement)) )
		
	{
		g_sprintf (error_str, "Error occured: %s", mysql_stmt_error (e_cursor->statement));
		gsql_message_add (workspace, GSQL_MESSAGE_ERROR, error_str);
		g_free (binds);
		mysql_stmt_reset (e_cursor->statement);

		return GSQL_CURSOR_STATE_ERROR;		
	}
	
	mysql_cursor_statement_detect (cursor);
	
	g_free (binds);
	
	n_fields =  mysql_field_count (mysql);
	
	if (n_fields == 0)
		return GSQL_CURSOR_STATE_OPEN;
	
	fields = e_cursor->statement->fields;
	binds = g_new0 (MYSQL_BIND, n_fields);
	
	e_cursor->binds = binds;
	
	for (n = 0; n < n_fields; n++)
	{
		GSQL_DEBUG ("field[%d] = %s", n, fields[n].name);
		var = gsql_variable_new ();
		mysql_variable_init (var, &fields[n], &binds[n]);
		cursor->var_list = g_list_append (cursor->var_list, var);
	}
	
	if (mysql_stmt_bind_result (e_cursor->statement, binds))
	{
		g_sprintf (error_str, "Error occured: %s", mysql_stmt_error (e_cursor->statement));
		gsql_message_add (workspace, GSQL_MESSAGE_ERROR, error_str);
		g_free (binds);
		mysql_stmt_reset (e_cursor->statement);

		return GSQL_CURSOR_STATE_ERROR;
	}
	
	return GSQL_CURSOR_STATE_OPEN;
}
예제 #17
0
int test_query(MYSQL *mysql)
{
  int rc;
  int i;
  MYSQL_STMT *stmt;
  MYSQL_BIND bind[1];

  char tmp[20];
  char *stmtstr= "CALL P1(?)";

  rc= mysql_query(mysql, "DROP PROCEDURE IF EXISTS p1");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "CREATE PROCEDURE p1(OUT p_out VARCHAR(19))" 
                         "BEGIN "
                          "  SET p_out = 'foo';"
                          "  SELECT 1 FROM DUAL;"
                         "END");
  check_mysql_rc(rc, mysql);

  stmt= mysql_stmt_init(mysql);
  check_mysql_rc(rc, mysql);

  rc= mysql_stmt_prepare(stmt, stmtstr, strlen(stmtstr));
  check_stmt_rc(rc, stmt);

  for (i=0; i < 1000; i++)
  {
    int status;
    memset(tmp, 0, sizeof(tmp));
    memset(bind, 0, sizeof(MYSQL_BIND));
    bind[0].buffer= tmp;
    bind[0].buffer_type= MYSQL_TYPE_STRING;
    bind[0].buffer_length= 4;

    mysql_stmt_bind_param(stmt, bind);

    rc= mysql_stmt_execute(stmt);
    check_stmt_rc(rc, stmt);
    do {
      if (stmt->field_count)
      {
        mysql_stmt_bind_result(stmt, bind);
        rc= mysql_stmt_store_result(stmt);
        check_stmt_rc(rc, stmt);
        while(mysql_stmt_fetch(stmt) == 0);

        rc= mysql_stmt_free_result(stmt);
        check_stmt_rc(rc, stmt);
      }
      status= mysql_stmt_next_result(stmt);
      if (status == 1)
        check_stmt_rc(status, stmt);
    } while (status == 0);

    rc= mysql_stmt_reset(stmt);
    if (rc)
      diag("reset failed after %d iterations", i);
    check_stmt_rc(rc, stmt);
  }
  mysql_stmt_close(stmt);

  return OK;
}
예제 #18
0
파일: misc.c 프로젝트: dparnell/MariaDB
static int test_wl4166_2(MYSQL *mysql)
{
  MYSQL_STMT *stmt;
  int        c_int;
  MYSQL_TIME d_date;
  MYSQL_BIND bind_out[2];
  int rc;

  if (mysql_get_server_version(mysql) < 50100) {
    diag("Test requires MySQL Server version 5.1 or above");
    return SKIP;
  }

  rc= mysql_query(mysql, "drop table if exists t1");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql, "create table t1 (c_int int, d_date date)");
  check_mysql_rc(rc, mysql);
  rc= mysql_query(mysql,
                  "insert into t1 (c_int, d_date) values (42, '1948-05-15')");
  check_mysql_rc(rc, mysql);

  stmt= mysql_stmt_init(mysql);
  FAIL_IF(!stmt, mysql_error(mysql));
  rc= mysql_stmt_prepare(stmt, "select * from t1", strlen("select * from t1"));
  check_stmt_rc(rc, stmt);

  memset(bind_out, '\0', sizeof(bind_out));
  bind_out[0].buffer_type= MYSQL_TYPE_LONG;
  bind_out[0].buffer= (void*) &c_int;

  bind_out[1].buffer_type= MYSQL_TYPE_DATE;
  bind_out[1].buffer= (void*) &d_date;

  rc= mysql_stmt_bind_result(stmt, bind_out);
  check_stmt_rc(rc, stmt);

  /* int -> varchar transition */

  rc= mysql_query(mysql,
                  "alter table t1 change column c_int c_int varchar(11)");
  check_mysql_rc(rc, mysql);

  rc= mysql_stmt_execute(stmt);
  check_stmt_rc(rc, stmt);

  rc= mysql_stmt_fetch(stmt);
  check_stmt_rc(rc, stmt);

  FAIL_UNLESS(c_int == 42, "c_int != 42");
  FAIL_UNLESS(d_date.year == 1948, "y!=1948");
  FAIL_UNLESS(d_date.month == 5, "m != 5");
  FAIL_UNLESS(d_date.day == 15, "d != 15");

  rc= mysql_stmt_fetch(stmt);
  FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");

  /* varchar to int retrieval with truncation */

  rc= mysql_query(mysql, "update t1 set c_int='abcde'");
  check_mysql_rc(rc, mysql);

  rc= mysql_stmt_execute(stmt);
  check_stmt_rc(rc, stmt);

  rc= mysql_stmt_fetch(stmt);
  FAIL_IF(!rc, "Error expected");

  FAIL_UNLESS(c_int == 0, "c != 0");

  rc= mysql_stmt_fetch(stmt);
  FAIL_UNLESS(rc == MYSQL_NO_DATA, "rc != MYSQL_NO_DATA");

  /* alter table and increase the number of columns */
  rc= mysql_query(mysql, "alter table t1 add column d_int int");
  check_mysql_rc(rc, mysql);

  rc= mysql_stmt_execute(stmt);
  FAIL_IF(!rc, "Error expected");

  rc= mysql_stmt_reset(stmt);
  check_stmt_rc(rc, stmt);

  /* decrease the number of columns */
  rc= mysql_query(mysql, "alter table t1 drop d_date, drop d_int");
  check_mysql_rc(rc, mysql);
  rc= mysql_stmt_execute(stmt);
  diag("rc=%d error: %d\n", rc, mysql_stmt_errno(stmt));
  FAIL_IF(!rc, "Error expected");

  mysql_stmt_close(stmt);
  rc= mysql_query(mysql, "drop table t1");
  check_mysql_rc(rc, mysql);

  return OK;
}
예제 #19
0
    int exec() {

      int n = res.get_i("queries=");
      if(n<1) n = 1;
      if(n>500) n = 500;

      vw array;

      while(n--) {

        id = rand()%10000;
        newNumber = rand()%10000+1;

        // reset statement for reuse
        if(mysql_stmt_reset(stmt)) throw runtime_error(mysql_stmt_error(stmt));
        if(mysql_stmt_reset(ustmt)) throw runtime_error(mysql_stmt_error(ustmt));

        // create and use input parameter
        memset(param, 0, sizeof(param));
        param[0].buffer_type = MYSQL_TYPE_LONG;
        param[0].buffer = (char *)&id;
        param[0].buffer_length = sizeof(id);
        param[0].is_null = 0;
        param[0].length = NULL;
        if(mysql_stmt_bind_param(stmt,param)) throw runtime_error(mysql_stmt_error(stmt));

        // create and fill result buffer
        memset(results, 0, sizeof(results));
        results[0].buffer_type= MYSQL_TYPE_LONG;
        results[0].buffer = &randomNumber;
        results[0].buffer_length = sizeof(randomNumber);
        results[0].is_null = 0;
        results[0].length = &len1;
        if(mysql_stmt_bind_result(stmt, results)) throw runtime_error(mysql_stmt_error(stmt));

        // create and use update parameter
        memset(param_update, 0, sizeof(param_update));
        param_update[0].buffer_type = MYSQL_TYPE_LONG;
        param_update[0].buffer = (char *)&newNumber;
        param_update[0].buffer_length = sizeof(newNumber);
        param_update[0].is_null = 0;
        param_update[0].length = 0;
        param_update[1].buffer_type = MYSQL_TYPE_LONG;
        param_update[1].buffer = (char *)&id;
        param_update[1].buffer_length = sizeof(id);
        param_update[1].is_null = 0;
        param_update[1].length = 0;

        // execute statement
        if(mysql_stmt_execute(stmt)) throw runtime_error(mysql_stmt_error(stmt));
        if(mysql_stmt_bind_param(ustmt,param_update)) throw runtime_error(mysql_stmt_error(ustmt));
        if(mysql_stmt_fetch(stmt)) randomNumber = 0;

        // add to array (required by specification)
        array.push_back(World(id,newNumber));
      }

      // output json
      res << '[';
      for(unsigned z=0;z<array.size();++z) {
        if(z>0) res << ',';
        World &az = array[z];
        JsonObj obj;
        obj.add("id",az.id);
        obj.add("randomNumber",az.randomNumber);
        res << obj.to_s();
      }
      res << ']';

      return res.end();
    }
예제 #20
0
파일: p95.c 프로젝트: KISSMonX/tcpcollect
int main (int argc, char *argv[]) {

    MYSQL *mysql;
    MYSQL_RES *result;
    MYSQL_ROW row;
    my_bool reconnect = 0;
    mysql = mysql_init(NULL);
    mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);

    //CONN(0);
    mysql_real_connect(mysql, "10.250.7.14", "test", "test", "test", 3306, NULL, 0);

    //mysql_query(mysql, DROP_SAMPLE_TABLE);
    //mysql_query(mysql, CREATE_SAMPLE_TABLE);
    //mysql_query(mysql, INSERT_SAMPLE);
    //mysql_query(mysql, INSERT_SAMPLE1);

    MYSQL_STMT    *stmt;
    MYSQL_BIND    bind[4];
    MYSQL_BIND    bResult[4];
    unsigned long length[4];
    my_ulonglong  affected_rows;
    int           param_count;
    short         small_data;
    //long int_data;
    char int_data[100] = "";
    char l2[100] = "";
    long long l1;
    long long f1;
    length[0] = sizeof(int_data);
    length[2] = sizeof(l2);
    my_bool       is_null[1];

    is_null[0] = 0;
    stmt = mysql_stmt_init(mysql);
    if (!stmt)
    {
      fprintf(stderr, " mysql_stmt_init(), out of memory\n");
      exit(0);
    }

    #define SELECT_EXAMPLE "select bytecol, blobcol, datecol, decimalcol, doublecol, floatcol, intcol, longcol, null, shortcol, medcol, stringCol, timecol, timestampcol from test1"

    if (mysql_stmt_prepare(stmt, SELECT_EXAMPLE, strlen(SELECT_EXAMPLE)))
    {
      fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n");
      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
      exit(0);
    }
    fprintf(stdout, " prepare, INSERT successful\n");

    if (mysql_stmt_execute(stmt))
    {
      fprintf(stderr, " mysql_stmt_execute(), 1 failed\n");
      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
      exit(0);
    }

    while(!mysql_stmt_fetch(stmt)) {
        printf("[%s] [%lld] [%s] [%lld]\n",  int_data, l1, l2, f1);
    }

    mysql_stmt_reset(stmt);
    mysql_stmt_close(stmt);
    sleep(3);
}