cpix_Error * CreateError(cpix_ErrorType errorType, const char * msg) { ErrorInfo * errorInfo = NULL; try { errorInfo = AcquireErrorInfo(); errorInfo->setInfo(errorType, msg); } catch (...) { logMsg(CPIX_LL_ERROR, "!!! Failed to create error struct for type %d ...", errorType); logMsg(CPIX_LL_ERROR, "!!! ... (cont'd) msg: %s. Will report CPIX error.", msg == NULL ? "(null)" : msg); errorInfo = & SyncErrorInfo; errorInfo->setInfo(ET_CPIX_EXC); } cpix_Error * rv = static_cast<cpix_Error*>(errorInfo); return rv; }
bool SqlConnection::Connect(const Param& param) { m_param = param; m_mysql = mysql_init(nullptr); if (m_mysql == nullptr) { SetLastError("SqlConnection Connect Error << mysql init failed\n"); return false; } const char* charset = m_defaultCharset.c_str(); auto ret = mysql_options(m_mysql, MYSQL_SET_CHARSET_NAME, charset); if (ret != 0) { AcquireErrorInfo(); return false; } unsigned int timeout = m_timeoutSeconds; ret = mysql_options(m_mysql, MYSQL_OPT_CONNECT_TIMEOUT, &timeout); if (ret != 0) { AcquireErrorInfo(); return false; } my_bool reconnect = m_autoReconnect ? 1 : 0; ret = mysql_options(m_mysql, MYSQL_OPT_RECONNECT, &reconnect); if (ret != 0) { AcquireErrorInfo(); return false; } auto ptr = mysql_real_connect(m_mysql , m_param.m_host.c_str() , m_param.m_username.c_str() , m_param.m_password.c_str() , m_param.m_dbName.c_str() , m_param.m_port , nullptr , CLIENT_MULTI_STATEMENTS); if (ptr == nullptr) { AcquireErrorInfo(); return false; } m_connectTime = NS_MZ_SHARE::GetNowMicrosecond(); return true; }
bool SqlConnection::SelectDatabase(const char* dbName) { if (m_mysql == nullptr) { SetLastError("SqlConnection SelectDatabase Error << no mysql connection\n"); return false; } const auto ret = mysql_select_db(m_mysql, dbName); if (ret != 0) { AcquireErrorInfo(); return false; } return true; }
bool SqlCommand::Execute() { if (m_stmt == nullptr) { m_sqlConnection.SetLastError("SqlCommand Execute Error << stmt has not been parepared"); return false; } const auto ret = mysql_stmt_execute(m_stmt); if (ret != 0) { AcquireErrorInfo(); return false; } return true; }
bool SqlCommand::BindParams() { int totalLength = 0; for (auto index = 0; index < m_fieldNumber; ++index) { auto& fieldParam = m_fieldParams[index]; fieldParam.m_buffer = nullptr; fieldParam.m_acturalLength = 0; fieldParam.m_isNull = 0; fieldParam.m_error = 0; totalLength += fieldParam.m_length; } m_bindBuffer.Reset(m_fieldNumber); m_dataBuffer.Reset(totalLength); int offset = 0; for (auto index = 0; index < m_fieldNumber; ++index) { auto& fieldParam = m_fieldParams[index]; fieldParam.m_buffer = m_dataBuffer.GetValue(offset); offset += fieldParam.m_length; auto fieldBind = m_bindBuffer.GetValue(index); MZ_ASSERT_TRUE(fieldBind != nullptr); fieldBind->buffer_type = SqlLocalToFieldType(fieldParam.m_sqlType); fieldBind->buffer = fieldParam.m_buffer; fieldBind->buffer_length = fieldParam.m_length; fieldBind->length = &fieldParam.m_acturalLength; fieldBind->is_null = &fieldParam.m_isNull; fieldBind->is_unsigned = SqlIsLocalTypeUnsigned(fieldParam.m_sqlType); fieldBind->error = &fieldParam.m_error; } auto binds = m_bindBuffer.GetValue(0); const auto ret = mysql_stmt_bind_param(m_stmt, binds); if (ret != 0) { AcquireErrorInfo(); return false; } return true; }
bool SqlCommand::PrepareStatement() { const auto sql = m_sql.c_str(); const auto length = m_sql.size(); const auto mysql = m_sqlConnection.GetMysql(); MZ_ASSERT_TRUE(mysql != nullptr); m_stmt = mysql_stmt_init(mysql); if (m_stmt == nullptr) { m_sqlConnection.AcquireErrorInfo(); return false; } const auto ret = mysql_stmt_prepare(m_stmt, sql, (unsigned long)length); if (ret != 0) { AcquireErrorInfo(); return false; } return true; }