static int perf1(MYSQL *mysql) { int rc; MYSQL_STMT *stmt; const char *stmtstr= "SELECT s.emp_no, s.salary, e.emp_no, e.first_name, e.last_name, e.gender FROM salaries s, employees e WHERE s.emp_no = e.emp_no"; rc= mysql_select_db(mysql, "employees"); if (rc) { diag("Employees database not installed"); return SKIP; } stmt= mysql_stmt_init(mysql); diag("prepare"); rc= mysql_stmt_prepare(stmt, stmtstr, (unsigned long)strlen(stmtstr)); check_stmt_rc(rc, stmt); diag("execute"); rc= mysql_stmt_execute(stmt); check_stmt_rc(rc, stmt); diag("store"); rc= mysql_stmt_store_result(stmt); check_stmt_rc(rc, stmt); diag("fetch"); while (!mysql_stmt_fetch(stmt)); mysql_stmt_close(stmt); return OK; }
int CMySQLDynamicRecordSet::NextResults() { if(m_pDBConnection==NULL||m_hStmt==NULL) return DBERR_INVALID_PARAM; int RetCode; RetCode=mysql_stmt_next_result(m_hStmt); if(RetCode>0) return DBERR_FETCH_RESULT_FAIL; if(mysql_stmt_bind_result(m_hStmt,&(m_FetchBuffer[0]))) { m_pDBConnection->ProcessErrorMsg(m_hStmt,"绑定结果集失败"); return DBERR_BINDCOLFAIL; } if(m_CacheAllData) { if(mysql_stmt_store_result(m_hStmt)) { m_pDBConnection->ProcessErrorMsg(m_hStmt,"缓存结果集失败"); return DBERR_BUFFER_OVERFLOW; } } int Ret=FetchRow(); return Ret; }
int run_stmt(MYSQL_STMT *stmt, int int_data) { MYSQL_BIND bind[1]; MYSQL_RES *prepare_meta_result; bind[0].buffer_type= MYSQL_TYPE_LONG; bind[0].buffer= (char *)&int_data; bind[0].is_null= 0; bind[0].length= 0; if (mysql_stmt_bind_param(stmt, bind)) { fprintf(stderr, " mysql_stmt_bind_param() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(EXIT_FAILURE); } prepare_meta_result = mysql_stmt_result_metadata(stmt); // FIXME: no error check if (mysql_stmt_execute(stmt)) { fprintf(stderr, " mysql_stmt_execute(), 1 failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(EXIT_FAILURE); } // memset(bind, 0, sizeof(bind)); if (mysql_stmt_store_result(stmt)) { fprintf(stderr, " mysql_stmt_store_result() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(EXIT_FAILURE); } mysql_free_result(prepare_meta_result); return 0; }
bool database::get_oauth_user(char *access_token, int *OAuthUserID, int *UserID, int *expires_at) { MYSQL_STMT *stmt; MYSQL_BIND pbind[1]; memset(pbind, 0, sizeof(pbind)); bool result = false; pbind[0].buffer_type = MYSQL_TYPE_STRING; pbind[0].buffer = (char *)access_token; pbind[0].buffer_length = strnlen(access_token, 512); const char sql[] = "SELECT t.user_id, c.parent_id, t.expires_at FROM " "`supla_oauth_access_tokens` AS t, `supla_oauth_clients` AS c WHERE c.id " "= t.client_id AND c.parent_id != 0 AND t.expires_at > " "UNIX_TIMESTAMP(NOW()) AND t.scope = 'restapi' AND token = ? LIMIT 1"; if (stmt_execute((void **)&stmt, sql, pbind, 1, true)) { mysql_stmt_store_result(stmt); if (mysql_stmt_num_rows(stmt) > 0) { MYSQL_BIND rbind[3]; memset(rbind, 0, sizeof(rbind)); int _OAuthUserID, _UserID, _expires_at; rbind[0].buffer_type = MYSQL_TYPE_LONG; rbind[0].buffer = (char *)&_OAuthUserID; rbind[1].buffer_type = MYSQL_TYPE_LONG; rbind[1].buffer = (char *)&_UserID; rbind[2].buffer_type = MYSQL_TYPE_LONG; rbind[2].buffer = (char *)&_expires_at; if (mysql_stmt_bind_result(stmt, rbind)) { supla_log(LOG_ERR, "MySQL - stmt bind error - %s", mysql_stmt_error(stmt)); } else if (mysql_stmt_fetch(stmt) == 0) { if (OAuthUserID != NULL) *OAuthUserID = _OAuthUserID; if (UserID != NULL) *UserID = _UserID; if (expires_at != NULL) { *expires_at = _expires_at; } result = true; } } mysql_stmt_free_result(stmt); mysql_stmt_close(stmt); } return result; }
static bool database_bind_result(database_handler_t* handler, MYSQL_BIND* b) { if (mysql_stmt_bind_result(handler->stmt, b)) { if (mysql_stmt_errno(handler->stmt) == CR_SERVER_GONE_ERROR) { debug_log("try to reopen mysql\n"); if (!database_handler_reopen(handler)) { error_log("reopen failure:%d,%s\n", mysql_stmt_errno(handler->stmt), mysql_stmt_error(handler->stmt)); return false; } debug_log("reopen success\n"); if (mysql_stmt_bind_result(handler->stmt, b)) { error_log("mysql_stmt_bind_result error:%d,%s\n", mysql_stmt_errno(handler->stmt), mysql_stmt_error(handler->stmt)); return false; } return true; } error_log("mysql_stmt_bind_result error:%d,%s\n", mysql_stmt_errno(handler->stmt), mysql_stmt_error(handler->stmt)); return false; } if (mysql_stmt_store_result(handler->stmt)) { if (mysql_stmt_errno(handler->stmt) == CR_SERVER_GONE_ERROR) { debug_log("try to reopen mysql\n"); if (!database_handler_reopen(handler)) { error_log("reopen failure:%d,%s\n", mysql_stmt_errno(handler->stmt), mysql_stmt_error(handler->stmt)); return false; } debug_log("reopen success\n"); if (mysql_stmt_store_result(handler->stmt)) { error_log("mysql_stmt_store_result error:%d,%s\n", mysql_stmt_errno(handler->stmt), mysql_stmt_error(handler->stmt)); return false; } return true; } error_log("mysql_stmt_store_result error:%d,%s\n", mysql_stmt_errno(handler->stmt), mysql_stmt_error(handler->stmt)); return false; } return true; }
MError MMysqlCommand::BindResult() { if (mysql_stmt_store_result(p_stmt_) != 0) { MLOG(MGetLibLogger(), MERR, "store result failed errno:", mysql_stmt_errno(p_stmt_), " error:", mysql_stmt_error(p_stmt_)); return MError::Unknown; } MYSQL_RES *p_meta_res = mysql_stmt_result_metadata(p_stmt_); if (!p_meta_res) { MLOG(MGetLibLogger(), MERR, "have not meta res errno:", mysql_stmt_errno(p_stmt_), " error:", mysql_stmt_error(p_stmt_)); return MError::Unknown; } MError err = MError::No; do { MYSQL_FIELD *p_field_list = mysql_fetch_fields(p_meta_res); if (!p_field_list) { MLOG(MGetLibLogger(), MERR, "mysql_fetch_fields is null unknown error"); err = MError::Unknown; break; } unsigned long field_count = mysql_num_fields(p_meta_res); if (field_count == 0) { MLOG(MGetLibLogger(), MERR, "mysql field_count is 0"); err = MError::Unknown; break; } out_params_.resize(field_count); out_datas_.resize(field_count); out_lengths_.resize(field_count); out_is_nulls_.resize(field_count); for (size_t i = 0; i < field_count; ++i) { MBlob &out_data = out_datas_[i]; out_data.Resize(p_field_list[i].max_length); memset(&(out_params_[i]), 0, sizeof(out_params_[i])); out_params_[i].buffer_type = p_field_list[i].type; out_params_[i].buffer = out_data.GetData(); out_params_[i].buffer_length = out_data.GetSize(); out_params_[i].length = &(out_lengths_[i]); out_params_[i].is_null = &(out_is_nulls_[i]); out_params_[i].is_unsigned = (p_field_list[i].flags & UNSIGNED_FLAG); out_params_[i].error = nullptr; } if (mysql_stmt_bind_result(p_stmt_, &out_params_[0]) != 0) { MLOG(MGetLibLogger(), MERR, "bind result failed errno:", mysql_stmt_errno(p_stmt_), " error:", mysql_stmt_error(p_stmt_)); err = MError::Unknown; break; } err = MError::No; } while (0); mysql_free_result(p_meta_res); return err; }
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; }
bool selectStmt() { std::unique_ptr<MYSQL, MysqlDestroy> mysql{mysql_init(nullptr)}; if (mysql_options(&*mysql, MYSQL_SET_CHARSET_NAME, "utf8") != 0) { printf("error mysql_options\n"); return false; } if (!mysql_real_connect(&*mysql, IP.c_str(), USER.c_str(), PASSWORD.c_str(), "mytest", PORT,nullptr,0)) { printf("error mysql_real_connect\n"); return false; } const std::string sql = "select fid, fname, fsid from test where fid = 1"; std::unique_ptr<MYSQL_STMT, MysqlStmtDestroy> mysql_stmt{mysql_stmt_init(&*mysql)}; mysql_stmt_prepare(&*mysql_stmt, sql.data(), sql.size()); if (mysql_stmt_execute(&*mysql_stmt) != 0) { printf("error mysql_stmt_execute\n"); return false; } mysql_stmt_store_result(&*mysql_stmt); std::array<MYSQL_BIND,3> bind{}; std::array<std::array<char, 50>, 3> bind_buffer{}; std::array<unsigned long, 3> bind_length{}; std::memset(bind.data(), 0, sizeof(bind)); bind[0].buffer = bind_buffer[0].data(); bind[0].buffer_length = 50; bind[0].length = &bind_length[0]; bind[1].buffer = bind_buffer[1].data(); bind[1].buffer_length = 50; bind[1].length = &bind_length[1]; bind[2].buffer = bind_buffer[2].data(); bind[2].buffer_length = 50; bind[2].length = &bind_length[2]; if (mysql_stmt_bind_result(&*mysql_stmt, bind.data())) { printf("mysql_stmt_bind_result error\n"); return false; } while (mysql_stmt_fetch(&*mysql_stmt) == 0) { int fid = 0; std::memcpy(&fid, bind[0].buffer, *bind[0].length); char* p = (char*)bind[1].buffer; for (unsigned long i = 0; i != *bind[1].length; ++i) { printf("%c ", p[i]); } printf("\n"); } return true; }
std::vector<int> DBManager::check_jobs() { std::vector<int> job_ids; char *stmt_str = "SELECT id FROM wkdd_ldp_jobs WHERE status = 'pending' ORDER BY start_time ASC"; MYSQL_BIND result[1]; int my_job_id; if (mysql_stmt_prepare(m_stmt, stmt_str, strlen(stmt_str)) != 0) { print_stmt_error("could not prepare select statement"); return job_ids; } if (mysql_stmt_field_count(m_stmt) != 1) { print_stmt_error("unexpected column count from select"); return job_ids; } // initialize result column structures memset((void *)result, 0, sizeof(result)); // zero the structures // set up my_id parameter result[0].buffer_type = MYSQL_TYPE_LONG; result[0].buffer = (void *) &my_job_id; result[0].is_unsigned = 0; result[0].is_null = 0; if (mysql_stmt_bind_result(m_stmt, result) != 0) { print_stmt_error("could not bind parameters for select"); return job_ids; } if (mysql_stmt_execute(m_stmt) != 0) { print_stmt_error("could not execute select"); return job_ids; } // fetch result set into client memory; this is optional, but it allows mysql_stmt_num_rows() to be called to determine the number of rows in the result set if (mysql_stmt_store_result(m_stmt) != 0) { print_stmt_error("could not buffer result set"); return job_ids; } while (mysql_stmt_fetch(m_stmt) == 0) { // fetch each row // store into vector job_ids.push_back(my_job_id); } // deallocate result set mysql_stmt_free_result(m_stmt); return job_ids; }
bool wxMySQLRecordset::Execute() { printf("MYSQL_STMT_EXECUTE\r\n"); bool res = !mysql_stmt_execute(m_Stmt); if(res) { if(m_HasResult) { mysql_stmt_store_result(m_Stmt); } } return res; }
void database::get_client_locations(int ClientID, supla_client_locations *locs) { MYSQL_STMT *stmt; const char sql[] = "SELECT `id`, `caption` FROM `supla_v_client_location` WHERE " "`client_id` = ?"; MYSQL_BIND pbind[1]; memset(pbind, 0, sizeof(pbind)); pbind[0].buffer_type = MYSQL_TYPE_LONG; pbind[0].buffer = (char *)&ClientID; if (stmt_execute((void **)&stmt, sql, pbind, 1, true)) { my_bool is_null[2]; MYSQL_BIND rbind[2]; memset(rbind, 0, sizeof(rbind)); int id; unsigned long size; char caption[401]; // utf8 rbind[0].buffer_type = MYSQL_TYPE_LONG; rbind[0].buffer = (char *)&id; rbind[0].is_null = &is_null[0]; rbind[1].buffer_type = MYSQL_TYPE_STRING; rbind[1].buffer = caption; rbind[1].buffer_length = 401; rbind[1].length = &size; rbind[1].is_null = &is_null[1]; if (mysql_stmt_bind_result(stmt, rbind)) { supla_log(LOG_ERR, "MySQL - stmt bind error - %s", mysql_stmt_error(stmt)); } else { mysql_stmt_store_result(stmt); if (mysql_stmt_num_rows(stmt) > 0) { while (!mysql_stmt_fetch(stmt)) { caption[size] = 0; locs->add_location(id, caption); } } } mysql_stmt_close(stmt); } }
void set_mysql_results(MYSQL_STMT *handle) { int i; /* Clear any old statement handles. */ if (sth) { numrows = 0; mysql_stmt_close(sth); } sth = handle; resultoffset = 0; /* Get result metadata. */ if (results) { mysql_free_result(results); } results = mysql_stmt_result_metadata(sth); if (results == NULL) { return; } /* Buffer results. */ if (r_bind) { for (i = 0; i < numfields; i++) { free(r_bind[i].buffer); free(r_bind[i].is_null); free(r_bind[i].length); free(r_bind[i].error); } free(r_bind); } numfields = mysql_num_fields(results); fields = mysql_fetch_fields(results); r_bind = safe_malloc(numfields * sizeof(MYSQL_BIND)); memset(r_bind, 0, numfields * sizeof(MYSQL_BIND)); for (i = 0; i < numfields; i++) { r_bind[i].buffer_type = fields[i].type; r_bind[i].buffer_length = fields[i].length; r_bind[i].buffer = safe_malloc(fields[i].length); r_bind[i].is_null = safe_malloc(sizeof(*r_bind[i].is_null)); r_bind[i].length = safe_malloc(sizeof(*r_bind[i].length)); r_bind[i].error = safe_malloc(sizeof(*r_bind[i].error)); } mysql_stmt_bind_result(sth, r_bind); mysql_stmt_store_result(sth); numrows = mysql_stmt_num_rows(sth); }
void Statement::Execute() { if (! _db.IsConnected()) { throw DatabaseException("Error in Statement::Execute", 0, "----", "Database is not connected"); } _numberAffectedRows = 0; _eof = true; _currentColumn = 0; if (ParameterCount() != _params.size()) { throw DatabaseException("Error in Statement::Execute", 0, "----", "Have not yet assigned all parameters"); } if (mysql_stmt_bind_param(_stmt, _bind) != 0) { throw DatabaseException(_stmt, "Error in Statement::Execute while binding parameters"); } if (mysql_stmt_execute(_stmt) != 0) { throw DatabaseException(_stmt, "Error in Statement::Execute while executing statement"); } if (_numberResultColumns > 0) { if (mysql_stmt_store_result(_stmt) != 0) { throw DatabaseException(_stmt, "Error in Statement::Execute while storing results"); } _eof = false; } else { _numberAffectedRows = mysql_stmt_affected_rows(_stmt); } _resultWasStored = true; if (_hasBlobField && _resultBind != NULL) { MYSQL_RES *res = mysql_stmt_result_metadata(_stmt); if (res != NULL) { for (unsigned int i = 0; i < _resultParams.size(); i++) { MYSQL_FIELD *column = mysql_fetch_field_direct(res, i); if (_resultBind[i].buffer_type == MYSQL_TYPE_BLOB) { _resultParams[i]->ResizeBlob(column->max_length); _resultBind[i].buffer = _resultParams[i]->Buffer(); _resultBind[i].buffer_length = _resultParams[i]->BufferSize(); _resultBind[i].length = _resultParams[i]->BufferLength(); } } mysql_free_result(res); } } }
bool MyStatement::Execute() { /* Clear any past result first! */ m_Results = false; /* Bind the parameters */ if (m_Params) { if (mysql_stmt_bind_param(m_stmt, m_bind) != 0) { return false; } } if (mysql_stmt_execute(m_stmt) != 0) { return false; } /* Skip away if we don't have data */ if (!m_pRes) { return true; } /* If we don't have a result manager, create one. */ if (!m_rs) { m_rs = new MyBoundResults(m_stmt, m_pRes); } /* Tell the result set to update its bind info, * and initialize itself if necessary. */ if (!(m_Results = m_rs->Initialize())) { return false; } /* Try precaching the results. */ m_Results = (mysql_stmt_store_result(m_stmt) == 0); /* Update now that the data is known. */ m_rs->Update(); /* Return indicator */ return m_Results; }
bool database::get_authkey_hash(int ID, char *buffer, unsigned int buffer_size, bool *is_null, const char *sql) { MYSQL_STMT *stmt = NULL; MYSQL_BIND pbind[1]; memset(pbind, 0, sizeof(pbind)); pbind[0].buffer_type = MYSQL_TYPE_LONG; pbind[0].buffer = (char *)&ID; bool result = false; if (stmt_execute((void **)&stmt, sql, pbind, 1, true)) { my_bool _is_null = false; unsigned long size = 0; MYSQL_BIND rbind[1]; memset(rbind, 0, sizeof(rbind)); rbind[0].buffer_type = MYSQL_TYPE_STRING; rbind[0].buffer = buffer; rbind[0].buffer_length = buffer_size; rbind[0].length = &size; rbind[0].is_null = &_is_null; if (mysql_stmt_bind_result(stmt, rbind)) { supla_log(LOG_ERR, "MySQL - stmt bind error - %s", mysql_stmt_error(stmt)); } else { mysql_stmt_store_result(stmt); if (mysql_stmt_num_rows(stmt) > 0 && !mysql_stmt_fetch(stmt) && buffer_size > size) { buffer[size] = 0; *is_null = _is_null > 0; result = true; } } mysql_stmt_close(stmt); } return result; }
/*========================================== * プリペアドステートメントの実行 *------------------------------------------ */ bool sqldbs_stmt_execute(struct sqldbs_stmt *st) { nullpo_retr(false, st); if( (st->bind_params && mysql_stmt_bind_param(st->stmt, st->params)) || mysql_stmt_execute(st->stmt) ) { printf("DB server Error - %s\n %s\n\n", mysql_stmt_error(st->stmt), st->query); return false; } if( mysql_stmt_store_result(st->stmt) ) { printf("DB server Error - %s\n %s\n\n", mysql_stmt_error(st->stmt), st->query); return false; } return true; }
result* mysql_statement::execute() { if (host_array) { int res = mysql_stmt_bind_param(stmt, host_array); if (res > 0) { throw_stmt_error(res, stmt, "mysql", str()); } } int res = mysql_stmt_execute(stmt); if (res > 0) { throw_stmt_error(res, stmt, "mysql", str()); } res = mysql_stmt_store_result(stmt); if (res > 0) { throw_stmt_error(res, stmt, "mysql", str()); } return new mysql_prepared_result(stmt, result_size); }
int gsc_mysql_stmt_store_result() { int mysql_stmt; int result; int helper = 0; helper += stackGetParamInt(1, &mysql_stmt); #if DEBUG_MYSQL printf_hide("gsc_mysql_stmt_store_result(mysql_stmt=%d)\n", mysql_stmt); #endif if (helper != 1) { printf_hide("scriptengine> wrongs args for gsc_mysql_stmt_store_result(mysql_stmt);\n"); return stackReturnInt(0); } int ret = mysql_stmt_store_result((MYSQL_STMT*)mysql_stmt); return stackReturnInt(ret); }
/// Executes the prepared statement. int SqlStmt_Execute(SqlStmt* self) { if( self == NULL ) return SQL_ERROR; SqlStmt_FreeResult(self); if( (self->bind_params && mysql_stmt_bind_param(self->stmt, self->params)) || mysql_stmt_execute(self->stmt) ) { ShowSQL("DB error - %s\n", mysql_stmt_error(self->stmt)); return SQL_ERROR; } self->bind_columns = false; if( mysql_stmt_store_result(self->stmt) )// store all the data { ShowSQL("DB error - %s\n", mysql_stmt_error(self->stmt)); return SQL_ERROR; } return SQL_SUCCESS; }
bool SqlStatement::run() { TRACE("run: %s", query_); if (bindOffset_ != mysql_stmt_param_count(stmt_)) { error_ = "Invalid parameter count"; fprintf(stderr, "Cannot run argument with invalid parameter count.\n"); return false; } if (paramCount_ != 0 && mysql_stmt_bind_param(stmt_, params_) != 0) { error_ = mysql_stmt_error(stmt_); return false; } unsigned i = 0; for (bool done = false; !done; i = 1 + i % 10) { switch (mysql_stmt_execute(stmt_)) { case CR_SERVER_GONE_ERROR: case CR_SERVER_LOST_EXTENDED: case CR_SERVER_LOST: if (i) sleep(i); break; case 0: done = true; break; default: error_ = mysql_stmt_error(stmt_); fprintf(stderr, "sql: statement execution error: %s\n", error_); return false; } } mysql_stmt_store_result(stmt_); error_ = nullptr; return true; }
/** * store_stmt_results - Buffer all results of a query on the client * Returns -1 on error, number_of_rows >= 0 if ok. */ static int store_stmt_results(MYSQL_STMT *stmt, const char *query, MYSQL_BIND bind_col[], unsigned long ncols) { my_ulonglong nrows; int i; if (stmt == NULL || ncols == 0) return -1; if (mysql_stmt_store_result(stmt)) { error("can not store query result for \"%s\": %s", query, mysql_stmt_error(stmt)); return -1; } nrows = mysql_stmt_affected_rows(stmt); if (nrows == (my_ulonglong)-1) { error("query \"%s\" returned an error: %s", query, mysql_stmt_error(stmt)); return -1; } while (mysql_stmt_fetch(stmt) == 0) for (i = 0; i < ncols; i++) { if (bind_col[i].error && *bind_col[i].error) { error("result value in column %d truncated: %s", i, mysql_stmt_error(stmt)); return -1; } } /* Seek back to begin of data set */ mysql_stmt_data_seek(stmt, 0); return nrows; }
static DWORD GroupSyncIncrChanges(DB_CONTEXT *db, SYNC_CONTEXT *sync) { CHAR *query; BYTE syncEvent; CHAR syncInfo[255]; CHAR groupName[_MAX_NAME + 1]; DWORD error; GROUPFILE groupFile; INT result; MYSQL_BIND bindInput[2]; MYSQL_BIND bindOutput[3]; MYSQL_RES *metadata; MYSQL_STMT *stmt; ASSERT(db != NULL); ASSERT(sync != NULL); // // Prepare statement and bind parameters // stmt = db->stmt[7]; query = "SELECT name, type, info FROM io_group_changes" " WHERE time BETWEEN ? AND ?" " ORDER BY id ASC"; result = mysql_stmt_prepare(stmt, query, strlen(query)); if (result != 0) { TRACE("Unable to prepare statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } DB_CHECK_PARAMS(bindInput, stmt); ZeroMemory(&bindInput, sizeof(bindInput)); bindInput[0].buffer_type = MYSQL_TYPE_LONG; bindInput[0].buffer = &sync->prevUpdate; bindInput[0].is_unsigned = TRUE; bindInput[1].buffer_type = MYSQL_TYPE_LONG; bindInput[1].buffer = &sync->currUpdate; bindInput[1].is_unsigned = TRUE; result = mysql_stmt_bind_param(stmt, bindInput); if (result != 0) { TRACE("Unable to bind parameters: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } metadata = mysql_stmt_result_metadata(stmt); if (metadata == NULL) { TRACE("Unable to prepare statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Execute prepared statement // result = mysql_stmt_execute(stmt); if (result != 0) { LOG_ERROR("Unable to execute statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Bind and fetch results // DB_CHECK_RESULTS(bindOutput, metadata); ZeroMemory(&bindOutput, sizeof(bindOutput)); bindOutput[0].buffer_type = MYSQL_TYPE_STRING; bindOutput[0].buffer = groupName; bindOutput[0].buffer_length = sizeof(groupName); bindOutput[1].buffer_type = MYSQL_TYPE_TINY; bindOutput[1].buffer = &syncEvent; bindOutput[1].is_unsigned = TRUE; bindOutput[2].buffer_type = MYSQL_TYPE_STRING; bindOutput[2].buffer = syncInfo; bindOutput[2].buffer_length = sizeof(syncInfo); result = mysql_stmt_bind_result(stmt, bindOutput); if (result != 0) { TRACE("Unable to bind results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } result = mysql_stmt_store_result(stmt); if (result != 0) { TRACE("Unable to buffer results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Process result set // for (;;) { if (mysql_stmt_fetch(stmt) != 0) { break; } switch ((SYNC_EVENT)syncEvent) { case SYNC_EVENT_CREATE: TRACE("GroupSyncIncr: Create(%s)", groupName); // Read group file from database ZeroMemory(&groupFile, sizeof(GROUPFILE)); error = DbGroupRead(db, groupName, &groupFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to read group \"%s\" (error %lu).", groupName, error); } else { // Create local user error = GroupEventCreate(groupName, &groupFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to create group \"%s\" (error %lu).", groupName, error); } } break; case SYNC_EVENT_RENAME: TRACE("GroupSyncIncr: Rename(%s,%s)", groupName, syncInfo); error = GroupEventRename(groupName, syncInfo); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to rename group \"%s\" to \"%s\" (error %lu).", groupName, syncInfo, error); } break; case SYNC_EVENT_DELETE: TRACE("GroupSyncIncr: Delete(%s)", groupName); error = GroupEventDelete(groupName); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to delete group \"%s\" (error %lu).", groupName, error); } break; default: LOG_ERROR("Unknown sync event %d.", syncEvent); break; } } mysql_free_result(metadata); return ERROR_SUCCESS; }
static DWORD GroupSyncFull(DB_CONTEXT *db) { CHAR *query; CHAR groupName[_MAX_NAME + 1]; DWORD error; DWORD i; GROUPFILE groupFile; INT result; NAME_ENTRY *entry; NAME_LIST list; MYSQL_BIND bind[5]; MYSQL_RES *metadata; MYSQL_STMT *stmt; ASSERT(db != NULL); TRACE("db=%p", db); // // Build list of group IDs // error = NameListCreateGroups(&list); if (error != ERROR_SUCCESS) { LOG_ERROR("Unable to create group ID list (error %lu).", error); return error; } // // Prepare and execute statement // stmt = db->stmt[7]; query = "SELECT name, description, slots, users, vfsfile" " FROM io_group"; result = mysql_stmt_prepare(stmt, query, strlen(query)); if (result != 0) { TRACE("Unable to prepare statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } metadata = mysql_stmt_result_metadata(stmt); if (metadata == NULL) { TRACE("Unable to prepare statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } result = mysql_stmt_execute(stmt); if (result != 0) { LOG_ERROR("Unable to execute statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Bind and fetch results // DB_CHECK_RESULTS(bind, metadata); ZeroMemory(&bind, sizeof(bind)); bind[0].buffer_type = MYSQL_TYPE_STRING; bind[0].buffer = groupName; bind[0].buffer_length = sizeof(groupName); bind[1].buffer_type = MYSQL_TYPE_STRING; bind[1].buffer = groupFile.szDescription; bind[1].buffer_length = sizeof(groupFile.szDescription); bind[2].buffer_type = MYSQL_TYPE_BLOB; bind[2].buffer = groupFile.Slots; bind[2].buffer_length = sizeof(groupFile.Slots); bind[3].buffer_type = MYSQL_TYPE_LONG; bind[3].buffer = &groupFile.Users; bind[4].buffer_type = MYSQL_TYPE_STRING; bind[4].buffer = groupFile.szVfsFile; bind[4].buffer_length = sizeof(groupFile.szVfsFile); result = mysql_stmt_bind_result(stmt, bind); if (result != 0) { TRACE("Unable to bind results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } result = mysql_stmt_store_result(stmt); if (result != 0) { TRACE("Unable to buffer results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Process result set // for (;;) { ZeroMemory(&groupFile, sizeof(GROUPFILE)); if (mysql_stmt_fetch(stmt) != 0) { break; } // // If ioFTPD fails to open a group at start-up, the group will still // have an entry in the GroupIdTable file but ioFTPD considers them // gone. The call to GroupExists() is done to check for this. // if (!NameListRemove(&list, groupName) || !GroupExists(groupName)) { TRACE("GroupSyncFull: Create(%s)", groupName); // Group does not exist locally, create it. error = GroupEventCreate(groupName, &groupFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to create group \"%s\" (error %lu).", groupName, error); } } else { TRACE("GroupSyncFull: Update(%s)", groupName); // Group already exists locally, update it. error = GroupEventUpdate(groupName, &groupFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to update group \"%s\" (error %lu).", groupName, error); } } } mysql_free_result(metadata); // // Delete remaining groups // for (i = 0; i < list.count; i++) { entry = list.array[i]; TRACE("GroupSyncFull: Delete(%s,%d)", entry->name, entry->id); // Group does not exist on database, delete it. error = GroupEventDeleteEx(entry->name, entry->id); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to delete group \"%s\" (error %lu).", entry->name, error); } } NameListDestroy(&list); return ERROR_SUCCESS; }
DBJob DBManager::get_job(const int& id) { DBJob data; char *stmt_str = "SELECT term, type FROM wkdd_ldp_jobs WHERE id = ?"; MYSQL_BIND param[1]; MYSQL_BIND result[2]; int my_job_id; int my_type; char my_term[2000]; unsigned long my_term_length; if (mysql_stmt_prepare(m_stmt, stmt_str, strlen(stmt_str)) != 0) { print_stmt_error("could not prepare select statement"); return data; } if (mysql_stmt_field_count(m_stmt) != 2) { print_stmt_error("unexpected column count from select"); return data; } // initialize result column structures memset((void *)param, 0, sizeof(param)); memset((void *)result, 0, sizeof(result)); // zero the structures // set up my_id parameter param[0].buffer_type = MYSQL_TYPE_LONG; param[0].buffer = (void *) &my_job_id; param[0].is_unsigned = 0; param[0].is_null = 0; // set up my_term parameter result[0].buffer_type = MYSQL_TYPE_STRING; result[0].buffer = (void *) &my_term; result[0].buffer_length = sizeof(my_term); result[0].length = &my_term_length; result[0].is_null = 0; // set up my_type parameter result[1].buffer_type = MYSQL_TYPE_LONG; result[1].buffer = (void *) &my_type; result[1].is_unsigned = 0; result[1].is_null = 0; if (mysql_stmt_bind_param(m_stmt, param) != 0) { print_stmt_error("could not bind parameters for select"); return data; } if (mysql_stmt_bind_result(m_stmt, result) != 0) { print_stmt_error("could not bind parameters for select"); return data; } // set variable my_job_id = id; if (mysql_stmt_execute(m_stmt) != 0) { print_stmt_error("could not execute select"); return data; } // fetch result set into client memory; this is optional, but it allows mysql_stmt_num_rows() to be called to determine the number of rows in the result set if (mysql_stmt_store_result(m_stmt) != 0) { print_stmt_error("could not buffer result set"); return data; } mysql_stmt_fetch(m_stmt); // fetch the single result data = DBJob(my_term, my_type); // deallocate result set mysql_stmt_free_result(m_stmt); return data; }
static int dbd_mysql_pselect_internal(apr_pool_t *pool, apr_dbd_t *sql, apr_dbd_results_t **res, apr_dbd_prepared_t *statement, int random, MYSQL_BIND *bind) { int nfields, i; my_bool *is_nullr; #if MYSQL_VERSION_ID >= 50000 my_bool *error; #endif int ret; unsigned long *length, maxlen; ret = mysql_stmt_bind_param(statement->stmt, bind); if (ret == 0) { ret = mysql_stmt_execute(statement->stmt); if (!ret) { if (!*res) { *res = apr_pcalloc(pool, sizeof(apr_dbd_results_t)); } (*res)->random = random; (*res)->statement = statement->stmt; (*res)->res = mysql_stmt_result_metadata(statement->stmt); (*res)->pool = pool; apr_pool_cleanup_register(pool, (*res)->res, free_result, apr_pool_cleanup_null); nfields = mysql_num_fields((*res)->res); if (!(*res)->bind) { (*res)->bind = apr_palloc(pool, nfields*sizeof(MYSQL_BIND)); length = apr_pcalloc(pool, nfields*sizeof(unsigned long)); #if MYSQL_VERSION_ID >= 50000 error = apr_palloc(pool, nfields*sizeof(my_bool)); #endif is_nullr = apr_pcalloc(pool, nfields*sizeof(my_bool)); for ( i = 0; i < nfields; ++i ) { maxlen = ((*res)->res->fields[i].length < sql->fldsz ? (*res)->res->fields[i].length : sql->fldsz) + 1; if ((*res)->res->fields[i].type == MYSQL_TYPE_BLOB) { (*res)->bind[i].buffer_type = MYSQL_TYPE_LONG_BLOB; } else { (*res)->bind[i].buffer_type = MYSQL_TYPE_VAR_STRING; } (*res)->bind[i].buffer_length = maxlen; (*res)->bind[i].length = &length[i]; (*res)->bind[i].buffer = apr_palloc(pool, maxlen); (*res)->bind[i].is_null = is_nullr+i; #if MYSQL_VERSION_ID >= 50000 (*res)->bind[i].error = error+i; #endif } } ret = mysql_stmt_bind_result(statement->stmt, (*res)->bind); if (!ret) { ret = mysql_stmt_store_result(statement->stmt); } } } if (ret != 0) { ret = mysql_stmt_errno(statement->stmt); } return ret; }
static DWORD UserSyncIncrUpdates(DB_CONTEXT *db, SYNC_CONTEXT *sync) { CHAR *query; CHAR userName[_MAX_NAME + 1]; DWORD error; INT result; USERFILE userFile; MYSQL_BIND bindInput[2]; MYSQL_BIND bindOutput[17]; MYSQL_RES *metadata; MYSQL_STMT *stmt; ASSERT(db != NULL); ASSERT(sync != NULL); // // Prepare statement and bind parameters // stmt = db->stmt[7]; query = "SELECT name,description,flags,home,limits,password,vfsfile,credits," " ratio,alldn,allup,daydn,dayup,monthdn,monthup,wkdn,wkup" " FROM io_user" " WHERE updated BETWEEN ? AND ?"; result = mysql_stmt_prepare(stmt, query, strlen(query)); if (result != 0) { LOG_WARN("Unable to prepare statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } DB_CHECK_PARAMS(bindInput, stmt); ZeroMemory(&bindInput, sizeof(bindInput)); bindInput[0].buffer_type = MYSQL_TYPE_LONG; bindInput[0].buffer = &sync->prevUpdate; bindInput[0].is_unsigned = TRUE; bindInput[1].buffer_type = MYSQL_TYPE_LONG; bindInput[1].buffer = &sync->currUpdate; bindInput[1].is_unsigned = TRUE; result = mysql_stmt_bind_param(stmt, bindInput); if (result != 0) { LOG_WARN("Unable to bind parameters: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } metadata = mysql_stmt_result_metadata(stmt); if (metadata == NULL) { LOG_WARN("Unable to retrieve result metadata: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Execute prepared statement // result = mysql_stmt_execute(stmt); if (result != 0) { LOG_ERROR("Unable to execute statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Bind and fetch results // DB_CHECK_RESULTS(bindOutput, metadata); ZeroMemory(&bindOutput, sizeof(bindOutput)); bindOutput[0].buffer_type = MYSQL_TYPE_STRING; bindOutput[0].buffer = userName; bindOutput[0].buffer_length = sizeof(userName); bindOutput[1].buffer_type = MYSQL_TYPE_STRING; bindOutput[1].buffer = userFile.Tagline; bindOutput[1].buffer_length = sizeof(userFile.Tagline); bindOutput[2].buffer_type = MYSQL_TYPE_STRING; bindOutput[2].buffer = userFile.Flags; bindOutput[2].buffer_length = sizeof(userFile.Flags); bindOutput[3].buffer_type = MYSQL_TYPE_STRING; bindOutput[3].buffer = userFile.Home; bindOutput[3].buffer_length = sizeof(userFile.Home); bindOutput[4].buffer_type = MYSQL_TYPE_BLOB; bindOutput[4].buffer = &userFile.Limits; bindOutput[4].buffer_length = sizeof(userFile.Limits); bindOutput[5].buffer_type = MYSQL_TYPE_BLOB; bindOutput[5].buffer = &userFile.Password; bindOutput[5].buffer_length = sizeof(userFile.Password); bindOutput[6].buffer_type = MYSQL_TYPE_STRING; bindOutput[6].buffer = userFile.MountFile; bindOutput[6].buffer_length = sizeof(userFile.MountFile); bindOutput[7].buffer_type = MYSQL_TYPE_BLOB; bindOutput[7].buffer = &userFile.Ratio; bindOutput[7].buffer_length = sizeof(userFile.Ratio); bindOutput[8].buffer_type = MYSQL_TYPE_BLOB; bindOutput[8].buffer = &userFile.Credits; bindOutput[8].buffer_length = sizeof(userFile.Credits); bindOutput[9].buffer_type = MYSQL_TYPE_BLOB; bindOutput[9].buffer = &userFile.DayUp; bindOutput[9].buffer_length = sizeof(userFile.DayUp); bindOutput[10].buffer_type = MYSQL_TYPE_BLOB; bindOutput[10].buffer = &userFile.DayDn; bindOutput[10].buffer_length = sizeof(userFile.DayDn); bindOutput[11].buffer_type = MYSQL_TYPE_BLOB; bindOutput[11].buffer = &userFile.WkUp; bindOutput[11].buffer_length = sizeof(userFile.WkUp); bindOutput[12].buffer_type = MYSQL_TYPE_BLOB; bindOutput[12].buffer = &userFile.WkDn; bindOutput[12].buffer_length = sizeof(userFile.WkDn); bindOutput[13].buffer_type = MYSQL_TYPE_BLOB; bindOutput[13].buffer = &userFile.MonthUp; bindOutput[13].buffer_length = sizeof(userFile.MonthUp); bindOutput[14].buffer_type = MYSQL_TYPE_BLOB; bindOutput[14].buffer = &userFile.MonthDn; bindOutput[14].buffer_length = sizeof(userFile.MonthDn); bindOutput[15].buffer_type = MYSQL_TYPE_BLOB; bindOutput[15].buffer = &userFile.AllUp; bindOutput[15].buffer_length = sizeof(userFile.AllUp); bindOutput[16].buffer_type = MYSQL_TYPE_BLOB; bindOutput[16].buffer = &userFile.AllDn; bindOutput[16].buffer_length = sizeof(userFile.AllDn); result = mysql_stmt_bind_result(stmt, bindOutput); if (result != 0) { LOG_WARN("Unable to bind results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } result = mysql_stmt_store_result(stmt); if (result != 0) { LOG_WARN("Unable to buffer results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Process result set // for (;;) { ZeroMemory(&userFile, sizeof(USERFILE)); if (mysql_stmt_fetch(stmt) != 0) { break; } TRACE("UserSyncIncr: Update(%s)", userName); // Read the user's admin-groups, groups, and hosts error = DbUserReadExtra(db, userName, &userFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to read user \"%s\" (error %lu).", userName, error); } else { // Update user file error = UserEventUpdate(userName, &userFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to update user \"%s\" (error %lu).", userName, error); } } } mysql_free_result(metadata); return ERROR_SUCCESS; }
static int my_process_stmt_result(MYSQL_STMT *stmt) { int field_count; int row_count= 0; MYSQL_BIND buffer[MAX_RES_FIELDS]; MYSQL_FIELD *field; MYSQL_RES *result; char data[MAX_RES_FIELDS][MAX_FIELD_DATA_SIZE]; ulong length[MAX_RES_FIELDS]; my_bool is_null[MAX_RES_FIELDS]; int rc, i; if (!(result= mysql_stmt_result_metadata(stmt))) /* No meta info */ { while (!mysql_stmt_fetch(stmt)) row_count++; return row_count; } field_count= MY_MIN(mysql_num_fields(result), MAX_RES_FIELDS); memset(buffer, 0, sizeof(buffer)); memset(length, 0, sizeof(length)); memset(is_null, 0, sizeof(is_null)); for(i= 0; i < field_count; i++) { buffer[i].buffer_type= MYSQL_TYPE_STRING; buffer[i].buffer_length= MAX_FIELD_DATA_SIZE; buffer[i].length= &length[i]; buffer[i].buffer= (void *) data[i]; buffer[i].is_null= &is_null[i]; } rc= mysql_stmt_bind_result(stmt, buffer); check_execute(stmt, rc); rc= 1; mysql_stmt_attr_set(stmt, STMT_ATTR_UPDATE_MAX_LENGTH, (void*)&rc); rc= mysql_stmt_store_result(stmt); check_execute(stmt, rc); my_print_result_metadata(result); mysql_field_seek(result, 0); while ((rc= mysql_stmt_fetch(stmt)) == 0) { if (!opt_silent) { fputc('\t', stdout); fputc('|', stdout); } mysql_field_seek(result, 0); for (i= 0; i < field_count; i++) { field= mysql_fetch_field(result); if (!opt_silent) { if (is_null[i]) fprintf(stdout, " %-*s |", (int) field->max_length, "NULL"); else if (length[i] == 0) { data[i][0]= '\0'; /* unmodified buffer */ fprintf(stdout, " %*s |", (int) field->max_length, data[i]); } else if (IS_NUM(field->type)) fprintf(stdout, " %*s |", (int) field->max_length, data[i]); else fprintf(stdout, " %-*s |", (int) field->max_length, data[i]); } } if (!opt_silent) { fputc('\t', stdout); fputc('\n', stdout); } row_count++; } DIE_UNLESS(rc == MYSQL_NO_DATA); if (!opt_silent) { if (row_count) my_print_dashes(result); fprintf(stdout, "\n\t%d %s returned\n", row_count, row_count == 1 ? "row" : "rows"); } mysql_free_result(result); return row_count; }
static DWORD GroupSyncIncrUpdates(DB_CONTEXT *db, SYNC_CONTEXT *sync) { CHAR *query; CHAR groupName[_MAX_NAME + 1]; DWORD error; GROUPFILE groupFile; INT result; MYSQL_BIND bindInput[2]; MYSQL_BIND bindOutput[5]; MYSQL_RES *metadata; MYSQL_STMT *stmt; ASSERT(db != NULL); ASSERT(sync != NULL); // // Prepare statement and bind parameters // stmt = db->stmt[7]; query = "SELECT name, description, slots, users, vfsfile" " FROM io_group" " WHERE updated BETWEEN ? AND ?"; result = mysql_stmt_prepare(stmt, query, strlen(query)); if (result != 0) { TRACE("Unable to prepare statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } DB_CHECK_PARAMS(bindInput, stmt); ZeroMemory(&bindInput, sizeof(bindInput)); bindInput[0].buffer_type = MYSQL_TYPE_LONG; bindInput[0].buffer = &sync->prevUpdate; bindInput[0].is_unsigned = TRUE; bindInput[1].buffer_type = MYSQL_TYPE_LONG; bindInput[1].buffer = &sync->currUpdate; bindInput[1].is_unsigned = TRUE; result = mysql_stmt_bind_param(stmt, bindInput); if (result != 0) { TRACE("Unable to bind parameters: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } metadata = mysql_stmt_result_metadata(stmt); if (metadata == NULL) { TRACE("Unable to prepare statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Execute prepared statement // result = mysql_stmt_execute(stmt); if (result != 0) { LOG_ERROR("Unable to execute statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Bind and fetch results // DB_CHECK_RESULTS(bindOutput, metadata); ZeroMemory(&bindOutput, sizeof(bindOutput)); bindOutput[0].buffer_type = MYSQL_TYPE_STRING; bindOutput[0].buffer = groupName; bindOutput[0].buffer_length = sizeof(groupName); bindOutput[1].buffer_type = MYSQL_TYPE_STRING; bindOutput[1].buffer = groupFile.szDescription; bindOutput[1].buffer_length = sizeof(groupFile.szDescription); bindOutput[2].buffer_type = MYSQL_TYPE_BLOB; bindOutput[2].buffer = groupFile.Slots; bindOutput[2].buffer_length = sizeof(groupFile.Slots); bindOutput[3].buffer_type = MYSQL_TYPE_LONG; bindOutput[3].buffer = &groupFile.Users; bindOutput[4].buffer_type = MYSQL_TYPE_STRING; bindOutput[4].buffer = groupFile.szVfsFile; bindOutput[4].buffer_length = sizeof(groupFile.szVfsFile); result = mysql_stmt_bind_result(stmt, bindOutput); if (result != 0) { TRACE("Unable to bind results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } result = mysql_stmt_store_result(stmt); if (result != 0) { TRACE("Unable to buffer results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Process result set // for (;;) { ZeroMemory(&groupFile, sizeof(GROUPFILE)); if (mysql_stmt_fetch(stmt) != 0) { break; } TRACE("GroupSyncIncr: Update(%s)", groupName); error = GroupEventUpdate(groupName, &groupFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to update group \"%s\" (error %lu).", groupName, error); } } mysql_free_result(metadata); return ERROR_SUCCESS; }
PreparedResultSet::PreparedResultSet(MYSQL_STMT* stmt, MYSQL_RES *result, uint64 rowCount, uint32 fieldCount) : m_rowCount(rowCount), m_rowPosition(0), m_fieldCount(fieldCount), m_rBind(NULL), m_stmt(stmt), m_res(result), m_isNull(NULL), m_length(NULL) { if (!m_res) return; if (m_stmt->bind_result_done) { delete[] m_stmt->bind->length; delete[] m_stmt->bind->is_null; } m_rBind = new MYSQL_BIND[m_fieldCount]; m_isNull = new my_bool[m_fieldCount]; m_length = new unsigned long[m_fieldCount]; memset(m_isNull, 0, sizeof(my_bool) * m_fieldCount); memset(m_rBind, 0, sizeof(MYSQL_BIND) * m_fieldCount); memset(m_length, 0, sizeof(unsigned long) * m_fieldCount); //- This is where we store the (entire) resultset if (mysql_stmt_store_result(m_stmt)) { TC_LOG_WARN(LOG_FILTER_SQL, "%s:mysql_stmt_store_result, cannot bind result from MySQL server. Error: %s", __FUNCTION__, mysql_stmt_error(m_stmt)); return; } //- This is where we prepare the buffer based on metadata uint32 i = 0; MYSQL_FIELD* field = mysql_fetch_field(m_res); while (field) { size_t size = Field::SizeForType(field); m_rBind[i].buffer_type = field->type; m_rBind[i].buffer = malloc(size); memset(m_rBind[i].buffer, 0, size); m_rBind[i].buffer_length = size; m_rBind[i].length = &m_length[i]; m_rBind[i].is_null = &m_isNull[i]; m_rBind[i].error = NULL; m_rBind[i].is_unsigned = field->flags & UNSIGNED_FLAG; ++i; field = mysql_fetch_field(m_res); } //- This is where we bind the bind the buffer to the statement if (mysql_stmt_bind_result(m_stmt, m_rBind)) { TC_LOG_WARN(LOG_FILTER_SQL, "%s:mysql_stmt_bind_result, cannot bind result from MySQL server. Error: %s", __FUNCTION__, mysql_stmt_error(m_stmt)); delete[] m_rBind; delete[] m_isNull; delete[] m_length; return; } m_rowCount = mysql_stmt_num_rows(m_stmt); m_rows.resize(uint32(m_rowCount)); while (_NextRow()) { m_rows[uint32(m_rowPosition)] = new Field[m_fieldCount]; for (uint64 fIndex = 0; fIndex < m_fieldCount; ++fIndex) { if (!*m_rBind[fIndex].is_null) m_rows[uint32(m_rowPosition)][fIndex].SetByteValue( m_rBind[fIndex].buffer, m_rBind[fIndex].buffer_length, m_rBind[fIndex].buffer_type, *m_rBind[fIndex].length ); else switch (m_rBind[fIndex].buffer_type) { case MYSQL_TYPE_TINY_BLOB: case MYSQL_TYPE_MEDIUM_BLOB: case MYSQL_TYPE_LONG_BLOB: case MYSQL_TYPE_BLOB: case MYSQL_TYPE_STRING: case MYSQL_TYPE_VAR_STRING: m_rows[uint32(m_rowPosition)][fIndex].SetByteValue( "", m_rBind[fIndex].buffer_length, m_rBind[fIndex].buffer_type, *m_rBind[fIndex].length ); break; default: m_rows[uint32(m_rowPosition)][fIndex].SetByteValue( 0, m_rBind[fIndex].buffer_length, m_rBind[fIndex].buffer_type, *m_rBind[fIndex].length ); } } m_rowPosition++; } m_rowPosition = 0; /// All data is buffered, let go of mysql c api structures CleanUp(); }
static DWORD UserSyncFull(DB_CONTEXT *db) { BOOL removed; CHAR *query; CHAR userName[_MAX_NAME + 1]; DWORD error; DWORD i; USERFILE userFile; INT result; NAME_ENTRY *entry; NAME_LIST list; MYSQL_BIND bind[17]; MYSQL_RES *metadata; MYSQL_STMT *stmt; ASSERT(db != NULL); TRACE("db=%p", db); // // Build list of user IDs // error = NameListCreateUsers(&list); if (error != ERROR_SUCCESS) { LOG_ERROR("Unable to create user ID list (error %lu).", error); return error; } // // Prepare and execute statement // stmt = db->stmt[7]; query = "SELECT name,description,flags,home,limits,password,vfsfile,credits," " ratio,alldn,allup,daydn,dayup,monthdn,monthup,wkdn,wkup" " FROM io_user"; result = mysql_stmt_prepare(stmt, query, strlen(query)); if (result != 0) { LOG_WARN("Unable to prepare statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } metadata = mysql_stmt_result_metadata(stmt); if (metadata == NULL) { LOG_WARN("Unable to retrieve result metadata: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } result = mysql_stmt_execute(stmt); if (result != 0) { LOG_ERROR("Unable to execute statement: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Bind and fetch results // DB_CHECK_RESULTS(bind, metadata); ZeroMemory(&bind, sizeof(bind)); bind[0].buffer_type = MYSQL_TYPE_STRING; bind[0].buffer = userName; bind[0].buffer_length = sizeof(userName); bind[1].buffer_type = MYSQL_TYPE_STRING; bind[1].buffer = userFile.Tagline; bind[1].buffer_length = sizeof(userFile.Tagline); bind[2].buffer_type = MYSQL_TYPE_STRING; bind[2].buffer = userFile.Flags; bind[2].buffer_length = sizeof(userFile.Flags); bind[3].buffer_type = MYSQL_TYPE_STRING; bind[3].buffer = userFile.Home; bind[3].buffer_length = sizeof(userFile.Home); bind[4].buffer_type = MYSQL_TYPE_BLOB; bind[4].buffer = &userFile.Limits; bind[4].buffer_length = sizeof(userFile.Limits); bind[5].buffer_type = MYSQL_TYPE_BLOB; bind[5].buffer = &userFile.Password; bind[5].buffer_length = sizeof(userFile.Password); bind[6].buffer_type = MYSQL_TYPE_STRING; bind[6].buffer = userFile.MountFile; bind[6].buffer_length = sizeof(userFile.MountFile); bind[7].buffer_type = MYSQL_TYPE_BLOB; bind[7].buffer = &userFile.Ratio; bind[7].buffer_length = sizeof(userFile.Ratio); bind[8].buffer_type = MYSQL_TYPE_BLOB; bind[8].buffer = &userFile.Credits; bind[8].buffer_length = sizeof(userFile.Credits); bind[9].buffer_type = MYSQL_TYPE_BLOB; bind[9].buffer = &userFile.DayUp; bind[9].buffer_length = sizeof(userFile.DayUp); bind[10].buffer_type = MYSQL_TYPE_BLOB; bind[10].buffer = &userFile.DayDn; bind[10].buffer_length = sizeof(userFile.DayDn); bind[11].buffer_type = MYSQL_TYPE_BLOB; bind[11].buffer = &userFile.WkUp; bind[11].buffer_length = sizeof(userFile.WkUp); bind[12].buffer_type = MYSQL_TYPE_BLOB; bind[12].buffer = &userFile.WkDn; bind[12].buffer_length = sizeof(userFile.WkDn); bind[13].buffer_type = MYSQL_TYPE_BLOB; bind[13].buffer = &userFile.MonthUp; bind[13].buffer_length = sizeof(userFile.MonthUp); bind[14].buffer_type = MYSQL_TYPE_BLOB; bind[14].buffer = &userFile.MonthDn; bind[14].buffer_length = sizeof(userFile.MonthDn); bind[15].buffer_type = MYSQL_TYPE_BLOB; bind[15].buffer = &userFile.AllUp; bind[15].buffer_length = sizeof(userFile.AllUp); bind[16].buffer_type = MYSQL_TYPE_BLOB; bind[16].buffer = &userFile.AllDn; bind[16].buffer_length = sizeof(userFile.AllDn); result = mysql_stmt_bind_result(stmt, bind); if (result != 0) { LOG_WARN("Unable to bind results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } result = mysql_stmt_store_result(stmt); if (result != 0) { LOG_WARN("Unable to buffer results: %s", mysql_stmt_error(stmt)); return DbMapErrorFromStmt(stmt); } // // Process result set // for (;;) { ZeroMemory(&userFile, sizeof(USERFILE)); if (mysql_stmt_fetch(stmt) != 0) { break; } // Remove user from the list beforehand in case DbUserReadExtra() fails removed = NameListRemove(&list, userName); // Read the user's admin-groups, groups, and hosts error = DbUserReadExtra(db, userName, &userFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to read user \"%s\" (error %lu).", userName, error); continue; } // // If ioFTPD fails to open a user at start-up, the user will still // have an entry in the UserIdTable file but ioFTPD considers them // gone. The call to UserExists() is done to check for this. // if (!removed || !UserExists(userName)) { TRACE("UserSyncFull: Create(%s)", userName); // User does not exist locally, create it. error = UserEventCreate(userName, &userFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to create user \"%s\" (error %lu).", userName, error); } } else { TRACE("UserSyncFull: Update(%s)", userName); // User already exists locally, update it. error = UserEventUpdate(userName, &userFile); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to update user \"%s\" (error %lu).", userName, error); } } } mysql_free_result(metadata); // // Delete remaining users // for (i = 0; i < list.count; i++) { entry = list.array[i]; TRACE("UserSyncFull: Delete(%s,%d)", entry->name, entry->id); // User does not exist on database, delete it. error = UserEventDeleteEx(entry->name, entry->id); if (error != ERROR_SUCCESS) { LOG_WARN("Unable to delete user \"%s\" (error %lu).", entry->name, error); } } NameListDestroy(&list); return ERROR_SUCCESS; }