/ Exec the Select SQL command and get back the result size in rows. */ /
| 706 | /* Exec the Select SQL command and get back the result size in rows. */ |
| 707 | /***********************************************************************/ |
| 708 | int MYSQLC::ExecSQL(PGLOBAL g, const char *query, int *w) |
| 709 | { |
| 710 | int rc = RC_OK; |
| 711 | |
| 712 | if (!m_DB) { |
| 713 | strcpy(g->Message, "MySQL not connected"); |
| 714 | return RC_FX; |
| 715 | } // endif m_DB |
| 716 | |
| 717 | if (w) |
| 718 | *w = 0; |
| 719 | |
| 720 | if (m_Rows >= 0) |
| 721 | return RC_OK; // Already done |
| 722 | |
| 723 | //if (mysql_query(m_DB, query) != 0) { |
| 724 | if (mysql_real_query(m_DB, query, strlen(query))) { |
| 725 | size_t msg_size = 512 + strlen(query); |
| 726 | char *msg = (char*)PlugSubAlloc(g, NULL, msg_size); |
| 727 | |
| 728 | snprintf(msg, msg_size, "(%d) %s [%s]", mysql_errno(m_DB), |
| 729 | mysql_error(m_DB), query); |
| 730 | strncpy(g->Message, msg, sizeof(g->Message) - 1); |
| 731 | g->Message[sizeof(g->Message) - 1] = 0; |
| 732 | rc = RC_FX; |
| 733 | //} else if (mysql_field_count(m_DB) > 0) { |
| 734 | } else if (m_DB->field_count > 0) { |
| 735 | if (m_Use) |
| 736 | #if defined(MYSQL_PREPARED_STATEMENTS) |
| 737 | m_Res = mysql_use_result(m_DB); |
| 738 | #else // !MYSQL_PREPARED_STATEMENTS) |
| 739 | m_Res = connect_use_result(m_DB); |
| 740 | #endif // !MYSQL_PREPARED_STATEMENTS |
| 741 | else |
| 742 | m_Res = mysql_store_result(m_DB); |
| 743 | |
| 744 | if (!m_Res) { |
| 745 | size_t msg_size = 512 + strlen(query); |
| 746 | char *msg = (char*)PlugSubAlloc(g, NULL, msg_size); |
| 747 | |
| 748 | snprintf(msg, msg_size, "mysql_store_result failed: %s", mysql_error(m_DB)); |
| 749 | strncpy(g->Message, msg, sizeof(g->Message) - 1); |
| 750 | g->Message[sizeof(g->Message) - 1] = 0; |
| 751 | rc = RC_FX; |
| 752 | } else { |
| 753 | m_Fields = mysql_num_fields(m_Res); |
| 754 | m_Rows = (!m_Use) ? (int)mysql_num_rows(m_Res) : 0; |
| 755 | |
| 756 | if (trace(1)) |
| 757 | htrc("ExecSQL: m_Res=%.4X size=%d m_Fields=%d m_Rows=%d\n", |
| 758 | m_Res, sizeof(*m_Res), m_Fields, m_Rows); |
| 759 | |
| 760 | } // endif m_Res |
| 761 | |
| 762 | } else { |
| 763 | // m_Rows = (int)mysql_affected_rows(m_DB); |
| 764 | m_Rows = (int)m_DB->affected_rows; |
| 765 | snprintf(g->Message, sizeof(g->Message), "Affected rows: %d", m_Rows); |
no test coverage detected