Variant f_mysql_multi_query(const String& query, CVarRef link_identifier /* = null */) { MYSQL *conn = MySQL::GetConn(link_identifier); if (conn == nullptr) { return false; } MySQL *mySQL = MySQL::Get(link_identifier); if (!mySQL->m_multi_query && !mysql_set_server_option(conn, MYSQL_OPTION_MULTI_STATEMENTS_ON)) { mySQL->m_multi_query = true; } if (mysql_real_query(conn, query.data(), query.size())) { #ifdef HHVM_MYSQL_TRACE_MODE if (RuntimeOption::EnableHipHopSyntax) { raise_notice("runtime/ext_mysql: failed executing [%s] [%s]", query.data(), mysql_error(conn)); } #endif // turning this off clears the errors if (!mysql_set_server_option(conn, MYSQL_OPTION_MULTI_STATEMENTS_OFF)) { mySQL->m_multi_query = false; } return false; } return true; }
// 连接数据库 void ConnectorMySQL::connect() { if (!is_connected()) { int reconnect = 1; mysql_init(&mysql_); mysql_options(&mysql_, MYSQL_OPT_RECONNECT, &reconnect); connected_ = mysql_real_connect(&mysql_, host_.c_str(), user_.c_str(), passwd_.c_str(), NULL, port_, NULL, 0) != NULL; if (mysql_errno(&mysql_) != 0) { throw ConnectError(mysql_error(&mysql_)); } mysql_set_server_option(&mysql_, MYSQL_OPTION_MULTI_STATEMENTS_OFF); } }
void ConnectorMySQL::Connect() { if (!IsConnected()) { int reconnect = 1; mysql_init(&mysql_); mysql_options(&mysql_, MYSQL_OPT_RECONNECT, &reconnect); connected_ = mysql_real_connect(&mysql_, host_.c_str(), user_.c_str(), passwd_.c_str(), nullptr, port_, nullptr, 0) != nullptr; if (mysql_errno(&mysql_) != 0) { throw ConnectionError(); } mysql_set_server_option(&mysql_, MYSQL_OPTION_MULTI_STATEMENTS_ON); } }
static PyObject* wsql_connection_set_server_option(wsql_connection *self, PyObject *args) { int error, flags=0; if (!PyArg_ParseTuple(args, "i", &flags)) return NULL; CHECK_CONNECTION(self, NULL); Py_BEGIN_ALLOW_THREADS error = mysql_set_server_option(&(self->connection), flags); Py_END_ALLOW_THREADS if (error) return wsql_raise_error(self); Py_RETURN_NONE; }
int _oph_odb_update_session_table(ophidiadb * oDB, char *sessionid, int id_user, int *id_session, pthread_mutex_t * flag) { if (!oDB || !sessionid || !id_session) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Null input parameter\n"); return OPH_ODB_NULL_PARAM; } if (oph_odb_check_connection_to_ophidiadb(oDB)) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Unable to reconnect to OphidiaDB.\n"); return OPH_ODB_MYSQL_ERROR; } char session_code[OPH_MAX_STRING_SIZE]; if (oph_get_session_code(sessionid, session_code)) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Unable to extract session code.\n"); return OPH_ODB_MYSQL_ERROR; } int id_folder; if (_oph_odb_update_folder_table(oDB, session_code, &id_folder, flag)) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Unable to create folder.\n"); return OPH_ODB_MYSQL_ERROR; } char insertQuery[MYSQL_BUFLEN]; int n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_SESSION, id_user, sessionid, id_folder); if (n >= MYSQL_BUFLEN) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Size of query exceed query limit.\n"); return OPH_ODB_STR_BUFF_OVERFLOW; } if (mysql_set_server_option(oDB->conn, MYSQL_OPTION_MULTI_STATEMENTS_ON)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "MySQL query error: %s\n", mysql_error(oDB->conn)); return OPH_ODB_MYSQL_ERROR; } if (mysql_query(oDB->conn, insertQuery)) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "MySQL query error: %s\n", mysql_error(oDB->conn)); return OPH_ODB_MYSQL_ERROR; } if (!(*id_session = mysql_insert_id(oDB->conn))) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Unable to find last inserted session id\n"); return OPH_ODB_TOO_MANY_ROWS; } return OPH_ODB_SUCCESS; }
int _oph_odb_update_session_label(ophidiadb * oDB, const char *sessionid, char *label, pthread_mutex_t * flag) { if (!oDB || !sessionid) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Null input parameter\n"); return OPH_ODB_NULL_PARAM; } if (oph_odb_check_connection_to_ophidiadb(oDB)) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Unable to reconnect to OphidiaDB.\n"); return OPH_ODB_MYSQL_ERROR; } int id_session; if (_oph_odb_retrieve_session_id(oDB, sessionid, &id_session, flag)) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Unable to retrieve session id\n"); return OPH_ODB_MYSQL_ERROR; } char insertQuery[MYSQL_BUFLEN]; int n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_SESSION_LABEL, label ? label : "", id_session); if (n >= MYSQL_BUFLEN) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "Size of query exceed query limit.\n"); return OPH_ODB_STR_BUFF_OVERFLOW; } if (mysql_set_server_option(oDB->conn, MYSQL_OPTION_MULTI_STATEMENTS_ON)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "MySQL query error: %s\n", mysql_error(oDB->conn)); return OPH_ODB_MYSQL_ERROR; } if (mysql_query(oDB->conn, insertQuery)) { pmesg_safe(flag, LOG_ERROR, __FILE__, __LINE__, "MySQL query error: %s\n", mysql_error(oDB->conn)); return OPH_ODB_MYSQL_ERROR; } return OPH_ODB_SUCCESS; }
int show_stock_ma_num_ratio(MYSQL*mysql,const char *src_table) { MYSQL_ROW row; MYSQL_RES *rs = NULL; puts(index_basic_table); puts(index_derived_table); if(SAS_OK != get_mysql_table_info(mysql,index_basic_table,"000001",&rs)) return SAS_ERROR; int date = 0; int total_trade_date = 0; memset(&row, 0, sizeof row); while((row = mysql_fetch_row(rs)) != NULL) { strcpy(daily_trade_info[date][TRADE_DATE_POS],row[TRADE_DATE_POS]); strcpy(daily_trade_info[date][CLOSE_PRICE_POS],row[CLOSE_PRICE_POS]); date++; } total_trade_date = date; mysql_free_result(rs); /*store index increase_ratio*/ if(SAS_OK != get_mysql_table_info(mysql,index_derived_table,"000001",&rs)) return SAS_ERROR; date = 0; while((row = mysql_fetch_row(rs)) != NULL) { strcpy(daily_trade_info[date][3],row[3]);//3: close_price date++; } mysql_free_result(rs); printf("\n-----------------------------------------------\n"); printf(" index MA5 MA10 MA20"); printf("\n-----------------------------------------------\n"); for(date = 0; date < total_trade_date ; date++) { char sqlexec[MAX_STR_LEN]; strcpy(sqlexec, "select count(*) from "); strcat(sqlexec, src_table); strcat(sqlexec, " where trade_date='"); strcat(sqlexec, daily_trade_info[date][TRADE_DATE_POS]); strcat(sqlexec,"'"); if(SAS_DB_OK != mysql_exe_select(mysql, sqlexec, &rs)) return SAS_ERROR; int daily_total = atoi((mysql_fetch_row(rs))[0]); mysql_free_result(rs); memset(sqlexec, 0, sizeof sqlexec); strcat(sqlexec, "select count(*) from "); strcat(sqlexec, src_table); strcat(sqlexec, " where trade_date='"); strcat(sqlexec, daily_trade_info[date][TRADE_DATE_POS]); strcat(sqlexec,"' and ma5 <= close_price;"); strcat(sqlexec, "select count(*) from "); strcat(sqlexec, src_table); strcat(sqlexec, " where trade_date='"); strcat(sqlexec, daily_trade_info[date][TRADE_DATE_POS]); strcat(sqlexec,"' and ma10 <= close_price;"); strcat(sqlexec, "select count(*) from "); strcat(sqlexec, src_table); strcat(sqlexec, " where trade_date='"); strcat(sqlexec, daily_trade_info[date][TRADE_DATE_POS]); strcat(sqlexec,"' and ma20 <= close_price;"); mysql_set_server_option(mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON); if (mysql_query(mysql, sqlexec)!=0) { printf( "error at %s,%d:%s", __FILE__, __LINE__,mysql_error(mysql)); exit(0); } int res_next = 0; int ma_id = 0; char cur_ma_str[MAX_MA_NUM][MAX_FILED_STR_LEN]; double cur_ma[MAX_MA_NUM]; memset(cur_ma_str, 0, sizeof cur_ma_str); do { rs = mysql_store_result(mysql); if(rs) { while(NULL != (row = mysql_fetch_row(rs))) { if(NULL != row[0]) { //strcpy(cur_ma_str[ma_id],row[0]); cur_ma[ma_id] = atof(row[0]); } else { printf("\ninfo at %s,%d,get MA info failed!", __FILE__, __LINE__ ); exit(0); } } mysql_free_result(rs); } ma_id++; res_next = mysql_next_result(mysql); } while(res_next == 0); if(0 == date) printf("%8s: %6.2f%%", daily_trade_info[date][TRADE_DATE_POS], atof(daily_trade_info[date][3])); else printf("\n%8s: %6.2f%%", daily_trade_info[date][TRADE_DATE_POS], atof(daily_trade_info[date][3])); //printf(" %6.2f %6.2f %6.2f", cur_ma[0], cur_ma[1], cur_ma[2]); printf(" %6.2f %6.2f %6.2f", 100*cur_ma[0]/daily_total, 100*cur_ma[1]/daily_total, 100*cur_ma[2]/daily_total); fflush(stdout); } return SAS_OK; }
int oph_odb_set_job_status_and_nchildrencompleted(int id_job, enum oph__oph_odb_job_status status, int nchildren, int force_nchildren_saving, ophidiadb * oDB) { if (!oDB) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Null input parameter\n"); return OPH_ODB_NULL_PARAM; } if (oph_odb_check_connection_to_ophidiadb(oDB)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Unable to reconnect to OphidiaDB.\n"); return OPH_ODB_MYSQL_ERROR; } char insertQuery[MYSQL_BUFLEN]; int n; switch (status) { case OPH_ODB_STATUS_UNKNOWN: if (nchildren >= 0) { pmesg(LOG_DEBUG, __FILE__, __LINE__, "Status is not changed\n"); return OPH_ODB_SUCCESS; } n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_DELETE_OPHIDIADB_JOB, id_job); break; case OPH_ODB_STATUS_CLOSED: pmesg(LOG_ERROR, __FILE__, __LINE__, "Status %s is not allowed\n", OPH_ODB_STATUS_UNKNOWN_STR); return OPH_ODB_MYSQL_ERROR; case OPH_ODB_STATUS_PENDING: case OPH_ODB_STATUS_WAIT: case OPH_ODB_STATUS_START: case OPH_ODB_STATUS_SET_ENV: case OPH_ODB_STATUS_INIT: case OPH_ODB_STATUS_DISTRIBUTE: case OPH_ODB_STATUS_EXECUTE: case OPH_ODB_STATUS_REDUCE: case OPH_ODB_STATUS_DESTROY: case OPH_ODB_STATUS_UNSET_ENV: if (nchildren < 0) n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_JOB_STATUS_1, oph_odb_convert_status_to_str(status), id_job); else n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_JOB_STATUS_PARENT_1, oph_odb_convert_status_to_str(status), nchildren, id_job); break; case OPH_ODB_STATUS_RUNNING: if (nchildren < 0) n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_JOB_STATUS_2, oph_odb_convert_status_to_str(status), id_job); else if (force_nchildren_saving) n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_JOB_STATUS_PARENT_1, oph_odb_convert_status_to_str(status), nchildren, id_job); else n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_JOB_STATUS_PARENT_2, oph_odb_convert_status_to_str(status), nchildren, id_job); break; case OPH_ODB_STATUS_COMPLETED: case OPH_ODB_STATUS_ERROR: case OPH_ODB_STATUS_START_ERROR: // Exception due to errors in operator arguments case OPH_ODB_STATUS_ABORTED: case OPH_ODB_STATUS_EXPIRED: if (nchildren < 0) n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_JOB_STATUS_3, oph_odb_convert_status_to_str(status), id_job); else n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_JOB_STATUS_PARENT_3, oph_odb_convert_status_to_str(status), nchildren, id_job); break; default: if (nchildren < 0) n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_JOB_STATUS_4, id_job); else n = snprintf(insertQuery, MYSQL_BUFLEN, MYSQL_QUERY_UPDATE_OPHIDIADB_JOB_STATUS_PARENT_4, nchildren, id_job); } if (n >= MYSQL_BUFLEN) { pmesg(LOG_ERROR, __FILE__, __LINE__, "Size of query exceed query limit.\n"); return OPH_ODB_STR_BUFF_OVERFLOW; } if ((nchildren >= 0) && mysql_set_server_option(oDB->conn, MYSQL_OPTION_MULTI_STATEMENTS_ON)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "MySQL query error: %s\n", mysql_error(oDB->conn)); return OPH_ODB_MYSQL_ERROR; } if (mysql_query(oDB->conn, insertQuery)) { pmesg(LOG_ERROR, __FILE__, __LINE__, "MySQL query error: %s\n", mysql_error(oDB->conn)); return OPH_ODB_MYSQL_ERROR; } pmesg(LOG_DEBUG, __FILE__, __LINE__, "Job status changed into '%s' using: %s\n", oph_odb_convert_status_to_str(status), insertQuery); return OPH_ODB_SUCCESS; }