diff --git a/src/MySQL_Generic_Connection.h b/src/MySQL_Generic_Connection.h index c6ed53da..0948ede3 100644 --- a/src/MySQL_Generic_Connection.h +++ b/src/MySQL_Generic_Connection.h @@ -13,12 +13,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - WiFiNINA and ESP8266/ESP32-AT shields + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ /********************************************************************************************************************************* diff --git a/src/MySQL_Generic_Connection_Impl.h b/src/MySQL_Generic_Connection_Impl.h index ce9e15e0..c368e00d 100644 --- a/src/MySQL_Generic_Connection_Impl.h +++ b/src/MySQL_Generic_Connection_Impl.h @@ -13,12 +13,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - WiFiNINA and ESP8266/ESP32-AT shields + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ /********************************************************************************************************************************* @@ -70,6 +71,9 @@ bool MySQL_Connection::connect(IPAddress server, int port, char *user, char *pas int retries = MAX_CONNECT_ATTEMPTS; MYSQL_LOGERROR3("Connecting to Server:", server, ", Port = ", port); + + if (db) + MYSQL_LOGERROR1("Using Database:", db); // Retry up to MAX_CONNECT_ATTEMPTS times. while (retries--) @@ -127,6 +131,9 @@ Connection_Result MySQL_Connection::connectNonBlocking(IPAddress server, int por MYSQL_LOGERROR3("Connecting to Server:", server, ", Port = ", port); + if (db) + MYSQL_LOGERROR1("Using Database:", db); + while (retries--) { if ( (now == 0) || ( millis() - now ) > CONNECT_DELAY_MS ) diff --git a/src/MySQL_Generic_Debug.h b/src/MySQL_Generic_Debug.h index 3eed0b21..846fb14d 100644 --- a/src/MySQL_Generic_Debug.h +++ b/src/MySQL_Generic_Debug.h @@ -13,12 +13,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - WiFiNINA and ESP8266/ESP32-AT shields + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ /********************************************************************************************************************************* diff --git a/src/MySQL_Generic_Encrypt_Sha1.h b/src/MySQL_Generic_Encrypt_Sha1.h index f2177787..4c02f86b 100644 --- a/src/MySQL_Generic_Encrypt_Sha1.h +++ b/src/MySQL_Generic_Encrypt_Sha1.h @@ -14,12 +14,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - WiFiNINA and ESP8266/ESP32-AT shields + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ #ifndef MYSQL_GENERIC_ENCRYPT_SHA1_H diff --git a/src/MySQL_Generic_Encrypt_Sha1_Impl.h b/src/MySQL_Generic_Encrypt_Sha1_Impl.h index 4e10be1c..de3f3d72 100644 --- a/src/MySQL_Generic_Encrypt_Sha1_Impl.h +++ b/src/MySQL_Generic_Encrypt_Sha1_Impl.h @@ -14,12 +14,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - WiFiNINA and ESP8266/ESP32-AT shields + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ #ifndef MYSQL_GENERIC_ENCRYPT_SHA1_IMPL_H diff --git a/src/MySQL_Generic_Ethernet.h b/src/MySQL_Generic_Ethernet.h index d7b0d6ed..00dc2e1d 100644 --- a/src/MySQL_Generic_Ethernet.h +++ b/src/MySQL_Generic_Ethernet.h @@ -11,12 +11,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - (using Ethernet, EthernetLarge, Ethernet2, Ethernet3 library) and WiFiNINA + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ #ifndef MYSQL_GENERIC_ETHERNET_H @@ -27,38 +28,47 @@ #if USE_ETHERNET #include "Ethernet.h" EthernetClient client; - #warning Use Ethernet lib + #warning Using Ethernet lib #elif USE_ETHERNET_LARGE #include "EthernetLarge.h" EthernetClient client; - #warning Use EthernetLarge lib + #warning Using EthernetLarge lib #elif USE_ETHERNET2 #include "Ethernet2.h" EthernetClient client; - #warning Use Ethernet2 lib + #warning Using Ethernet2 lib #elif USE_ETHERNET3 #include "Ethernet3.h" EthernetClient client; - #warning Use Ethernet3 lib + #warning Using Ethernet3 lib #elif USE_ETHERNET_ESP8266 #include "Ethernet_ESP8266.h" EthernetClient client; - #warning Use Ethernet_ESP8266 lib + #warning Using Ethernet_ESP8266 lib #elif USE_ETHERNET_LAN8742A #include #include EthernetClient client; - #warning Use Ethernet_LAN8742A lib + #warning Using Ethernet_LAN8742A lib + +// KH, from v1.0.1 +#elif USE_UIP_ETHERNET + #include + #include + EthernetClient client; + #warning Using ENC28J60 UIPEthernet lib +////// + #elif USE_CUSTOM_ETHERNET #include "Ethernet_XYZ.h" EthernetClient client; - #warning Use Custom Ethernet library from EthernetWrapper. You must include a library here or error. + #warning Using Custom Ethernet library from EthernetWrapper. You must include a library here or error. #else // Backup if none is selected #define USE_ETHERNET true #include "Ethernet.h" EthernetClient client; - #warning Use Ethernet lib + #warning Using Ethernet lib #endif #include diff --git a/src/MySQL_Generic_Packet.h b/src/MySQL_Generic_Packet.h index 928bf3e5..0156081d 100644 --- a/src/MySQL_Generic_Packet.h +++ b/src/MySQL_Generic_Packet.h @@ -13,12 +13,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - WiFiNINA and ESP8266/ESP32-AT shields + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ /********************************************************************************************************************************* @@ -46,12 +47,17 @@ #define MYSQL_EOF_PACKET 0xfe #define MYSQL_ERROR_PACKET 0xff -#define MYSQL_GENERIC_VERSION "1.0.0" +#define MYSQL_GENERIC_VERSION "1.0.1" class MySQL_Packet { public: byte *buffer; // buffer for reading packets + + // KH, from v1.0.1 + uint16_t largest_buffer_size = 0; + ////// + int packet_len; // length of current packet Client *client; // instance of client class (e.g. EthernetClient) char *server_version; // save server version from handshake diff --git a/src/MySQL_Generic_Packet_Impl.h b/src/MySQL_Generic_Packet_Impl.h index 8b3e21b8..3511f5bb 100644 --- a/src/MySQL_Generic_Packet_Impl.h +++ b/src/MySQL_Generic_Packet_Impl.h @@ -13,12 +13,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - WiFiNINA and ESP8266/ESP32-AT shields + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ /********************************************************************************************************************************* @@ -42,7 +43,14 @@ #include -#define MYSQL_DATA_TIMEOUT 3000 // Wifi client wait in milliseconds +// KH, from v1.0.1 +#if ( USE_UIP_ETHERNET || USING_WIFIESPAT_LIB || USING_WIFI_ESP_AT ) + #define MYSQL_DATA_TIMEOUT 30000 // UIPEthernet client wait in milliseconds !!!! +#else + #define MYSQL_DATA_TIMEOUT 6000 // Client wait in milliseconds +#endif +////// + #define MYSQL_WAIT_INTERVAL 300 // WiFi client wait interval /* @@ -84,83 +92,76 @@ MySQL_Packet::MySQL_Packet(Client *client_instance) password[in] password db[in] default database */ -void MySQL_Packet::send_authentication_packet(char *user, char *password, char *db) -{ - if (buffer != NULL) - free(buffer); - buffer = (byte *) malloc(256); +void MySQL_Packet::send_authentication_packet(char *user, char *password, char *db) +{ + byte this_buffer[256]; + byte scramble[20]; int size_send = 4; // client flags - buffer[size_send] = byte(0x0D); - buffer[size_send + 1] = byte(0xa6); - buffer[size_send + 2] = byte(0x03); - buffer[size_send + 3] = byte(0x00); + this_buffer[size_send] = byte(0x0D); + this_buffer[size_send + 1] = byte(0xa6); + this_buffer[size_send + 2] = byte(0x03); + this_buffer[size_send + 3] = byte(0x00); size_send += 4; // max_allowed_packet - buffer[size_send] = 0; - buffer[size_send + 1] = 0; - buffer[size_send + 2] = 0; - buffer[size_send + 3] = 1; + this_buffer[size_send] = 0; + this_buffer[size_send + 1] = 0; + this_buffer[size_send + 2] = 0; + this_buffer[size_send + 3] = 1; size_send += 4; // charset - default is 8 - buffer[size_send] = byte(0x08); + this_buffer[size_send] = byte(0x08); size_send += 1; for (int i = 0; i < 24; i++) - buffer[size_send + i] = 0x00; + this_buffer[size_send + i] = 0x00; size_send += 23; // user name - memcpy((char *) &buffer[size_send], user, strlen(user)); + memcpy((char *) &this_buffer[size_send], user, strlen(user)); size_send += strlen(user) + 1; - buffer[size_send - 1] = 0x00; - - // password - see scramble password - byte *scramble = (uint8_t *)malloc(20); - + this_buffer[size_send - 1] = 0x00; + if (scramble_password(password, scramble)) { - buffer[size_send] = 0x14; + this_buffer[size_send] = 0x14; size_send += 1; for (int i = 0; i < 20; i++) - buffer[i + size_send] = scramble[i]; + this_buffer[i + size_send] = scramble[i]; size_send += 20; - buffer[size_send] = 0x00; + this_buffer[size_send] = 0x00; } - free(scramble); - if (db) { - memcpy((char *)&buffer[size_send], db, strlen(db)); + memcpy((char *)&this_buffer[size_send], db, strlen(db)); size_send += strlen(db) + 1; - buffer[size_send - 1] = 0x00; + this_buffer[size_send - 1] = 0x00; } else { - buffer[size_send + 1] = 0x00; + this_buffer[size_send + 1] = 0x00; size_send += 1; } // Write packet size int p_size = size_send - 4; - store_int(&buffer[0], p_size, 3); - buffer[3] = byte(0x01); + store_int(&this_buffer[0], p_size, 3); + this_buffer[3] = byte(0x01); // Write the packet - client->write((uint8_t*)buffer, size_send); + client->write((uint8_t*)this_buffer, size_send); client->flush(); } - /* scramble_password - Build a SHA1 scramble of the user password @@ -248,13 +249,16 @@ int MySQL_Packet::wait_for_bytes(int bytes_need) if (num >= bytes_need) break; } + + yield(); + //delay(0); } while (now < wait_till); if (num == 0 && now >= wait_till) { MYSQL_LOGDEBUG("MySQL_Packet::wait_for_bytes: client->stop"); - client->stop(); + //client->stop(); } MYSQL_LOGDEBUG1("MySQL_Packet::wait_for_bytes: OK, Num bytes= ", num); @@ -278,20 +282,19 @@ int MySQL_Packet::wait_for_bytes(int bytes_need) be found by reading the first 4 bytes from the server then reading N bytes for the packet payload. */ + +//KH, from v1.0.1. Use largest alocated buffer and keep until larger packet is received +// TODO: Pass buffer pointer instead of using global buffer void MySQL_Packet::read_packet() { - byte local[4]; - - if (buffer) - { - free(buffer); - buffer = NULL; - } - + #define PACKET_HEADER_SZ 4 + + byte local[PACKET_HEADER_SZ]; + MYSQL_LOGDEBUG("MySQL_Packet::read_packet: step 1"); // Read packet header - if (wait_for_bytes(4) < 4) + if (wait_for_bytes(PACKET_HEADER_SZ) < PACKET_HEADER_SZ) { MYSQL_LOGERROR(READ_TIMEOUT); @@ -300,7 +303,7 @@ void MySQL_Packet::read_packet() MYSQL_LOGDEBUG("MySQL_Packet::read_packet: step 2"); - for (int i = 0; i < 4; i++) + for (int i = 0; i < PACKET_HEADER_SZ; i++) local[i] = client->read(); // Get packet length @@ -317,7 +320,7 @@ void MySQL_Packet::read_packet() } */ - MYSQL_LOGDEBUG1("MySQL_Packet::read_packet: packet_len= ", packet_len); + MYSQL_LOGWARN1("MySQL_Packet::read_packet: packet_len= ", packet_len); // Check for valid packet. if (packet_len < 0) @@ -326,18 +329,42 @@ void MySQL_Packet::read_packet() packet_len = 0; } - buffer = (byte *)malloc(packet_len + 4); - + if ( largest_buffer_size < packet_len + PACKET_HEADER_SZ ) + { + if (largest_buffer_size == 0 ) + { + // Check if we need to allocate buffer the first time + largest_buffer_size = packet_len + PACKET_HEADER_SZ; + MYSQL_LOGWARN1("MySQL_Packet::read_packet: First time allocate buffer, size = ", largest_buffer_size); + + buffer = (byte *) malloc(largest_buffer_size); + } + else + { + // Check if we need to reallocate buffer + largest_buffer_size = packet_len + PACKET_HEADER_SZ; + MYSQL_LOGWARN1("MySQL_Packet::read_packet: Reallocate buffer, size = ", largest_buffer_size); + + buffer = (byte *) realloc(buffer, largest_buffer_size); + } + } + if (buffer == NULL) { MYSQL_LOGERROR(MEMORY_ERROR); + largest_buffer_size = 0; + return; } + else + { + memset(buffer, 0, largest_buffer_size); + } - for (int i = 0; i < 4; i++) + for (int i = 0; i < PACKET_HEADER_SZ; i++) buffer[i] = local[i]; - for (int i = 4; i < packet_len + 4; i++) + for (int i = PACKET_HEADER_SZ; i < packet_len + PACKET_HEADER_SZ; i++) buffer[i] = client->read(); MYSQL_LOGDEBUG("MySQL_Packet::read_packet: exit"); @@ -368,6 +395,7 @@ void MySQL_Packet::read_packet() 1 \0 byte, terminating the second part of a scramble seed */ +// KH, TODO: Pass buffer pointer instead of using global buffer void MySQL_Packet::parse_handshake_packet() { if (!buffer) @@ -418,6 +446,7 @@ void MySQL_Packet::parse_handshake_packet() 5 sqlstate (5 characters) n message */ +// KH, TODO: Pass buffer pointer instead of using global buffer void MySQL_Packet::parse_error_packet() { MYSQL_LOGDEBUG2("Error: ", read_int(5, 2), " = "); @@ -448,6 +477,7 @@ void MySQL_Packet::parse_error_packet() Returns integer - 0 = successful parse, packet type if not an Ok packet */ +// KH, TODO: Pass buffer pointer instead of using global buffer int MySQL_Packet::get_packet_type() { if (!buffer) @@ -486,6 +516,7 @@ int MySQL_Packet::get_packet_type() Returns integer - number of bytes integer consumes */ +// KH, TODO: Pass buffer pointer instead of using global buffer int MySQL_Packet::get_lcb_len(int offset) { if (!buffer) @@ -526,6 +557,7 @@ int MySQL_Packet::get_lcb_len(int offset) Returns integer - integer from the buffer */ +// KH, TODO: Pass buffer pointer instead of using global buffer int MySQL_Packet::read_int(int offset, int size) { int value = 0; @@ -569,20 +601,21 @@ void MySQL_Packet::store_int(byte *buff, long value, int size) { memset(buff, 0, size); - if (value < 0xff) + if (value <= 0xff) buff[0] = (byte)value; - else if (value < 0xffff) + else if (value <= 0xffff) { buff[0] = (byte)value; buff[1] = (byte)(value >> 8); } - else if (value < 0xffffff) + else if (value <= 0xffffff) { buff[0] = (byte)value; buff[1] = (byte)(value >> 8); buff[2] = (byte)(value >> 16); } - else if (value < 0xffffff) + //else if (value < 0xffffff) + else if (value > 0xffffff) { buff[0] = (byte)value; buff[1] = (byte)(value >> 8); @@ -601,6 +634,7 @@ void MySQL_Packet::store_int(byte *buff, long value, int size) Returns integer - integer from the buffer */ +// KH, TODO: Pass buffer pointer instead of using global buffer int MySQL_Packet::read_lcb_int(int offset) { int len_size = 0; @@ -648,6 +682,7 @@ int MySQL_Packet::read_lcb_int(int offset) are looking for additional program memory space, you can safely delete this method. */ +// KH, TODO: Pass buffer pointer instead of using global buffer void MySQL_Packet::print_packet() { if (!buffer) diff --git a/src/MySQL_Generic_Query.h b/src/MySQL_Generic_Query.h index 792a50e4..c82c73c6 100644 --- a/src/MySQL_Generic_Query.h +++ b/src/MySQL_Generic_Query.h @@ -14,12 +14,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - WiFiNINA and ESP8266/ESP32-AT shields + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ /********************************************************************************************************************************* @@ -82,7 +83,7 @@ class MySQL_Query private: bool execute_query(int query_len); - + #ifdef WITH_SELECT public: diff --git a/src/MySQL_Generic_Query_Impl.h b/src/MySQL_Generic_Query_Impl.h index 1e73b95e..f1690a37 100644 --- a/src/MySQL_Generic_Query_Impl.h +++ b/src/MySQL_Generic_Query_Impl.h @@ -14,12 +14,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - WiFiNINA and ESP8266/ESP32-AT shields + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ /********************************************************************************************************************************* @@ -39,6 +40,8 @@ #ifndef MYSQL_GENERIC_QUERY_IMPL_H #define MYSQL_GENERIC_QUERY_IMPL_H +#define COMMAND_HEADER_LEN 5 + /* Constructor @@ -57,12 +60,12 @@ MySQL_Query::MySQL_Query(MySQL_Connection *connection) for (int f = 0; f < MAX_FIELDS; f++) { columns.fields[f] = NULL; - row.values[f] = NULL; + row.values[f] = NULL; } - columns_read = false; - rows_affected = -1; - last_insert_id = -1; + columns_read = false; + rows_affected = -1; + last_insert_id = -1; #endif } @@ -76,7 +79,6 @@ MySQL_Query::~MySQL_Query() #endif } - /* execute - Execute a SQL statement @@ -93,6 +95,8 @@ MySQL_Query::~MySQL_Query() Returns bool - True = a result set is available for reading */ + +// TODO: Pass buffer pointer instead of using global buffer bool MySQL_Query::execute(const char *query, bool progmem) { int query_len; // length of query @@ -113,20 +117,52 @@ bool MySQL_Query::execute(const char *query, bool progmem) query_len = (int) strlen(query); } - if (conn->buffer != NULL) - free(conn->buffer); - - conn->buffer = (byte *) malloc(query_len + 5); - + // KH, new from v1.0.1 + if ( conn->largest_buffer_size < query_len + COMMAND_HEADER_LEN ) + { + if ( conn->largest_buffer_size == 0 ) + { + // Check if we need to allocate buffer the first time. Don't need, but to be safe + conn->largest_buffer_size = query_len + COMMAND_HEADER_LEN; + MYSQL_LOGWARN1("MySQL_Query::execute: First time allocate buffer, size = ", conn->largest_buffer_size); + + conn->buffer = (byte *) malloc(conn->largest_buffer_size); + } + else + { + // Check if we need to reallocate buffer + conn->largest_buffer_size = query_len + COMMAND_HEADER_LEN; + MYSQL_LOGWARN1("MySQL_Query::execute: Reallocate buffer, size = ", conn->largest_buffer_size); + + conn->buffer = (byte *) realloc(conn->buffer, conn->largest_buffer_size); + } + } + else + { + MYSQL_LOGWARN3("MySQL_Query::execute: Reuse allocated buffer, conn->largest_buffer_size = ", conn->largest_buffer_size, " > ", query_len + COMMAND_HEADER_LEN); + } + + if (conn->buffer == NULL) + { + MYSQL_LOGERROR(MEMORY_ERROR); + + return false; + } + else + { + memset(conn->buffer, 0, conn->largest_buffer_size); + } + ////// + // Write query to packet if (progmem) { for (int c = 0; c < query_len; c++) - conn->buffer[c + 5] = pgm_read_byte_near(query + c); + conn->buffer[c + COMMAND_HEADER_LEN] = pgm_read_byte_near(query + c); } else { - memcpy(&conn->buffer[5], query, query_len); + memcpy(&conn->buffer[COMMAND_HEADER_LEN], query, query_len); } // Send the query @@ -148,6 +184,7 @@ bool MySQL_Query::execute(const char *query, bool progmem) Returns bool - true = result set available, false = no result set returned. */ +// TODO: Pass buffer pointer instead of using global buffer bool MySQL_Query::execute_query(int query_len) { if (!conn->buffer) @@ -214,7 +251,6 @@ bool MySQL_Query::execute_query(int query_len) return true; } - #ifdef WITH_SELECT /* Close @@ -453,6 +489,9 @@ char *MySQL_Query::read_string(int *offset) int len_bytes = conn->get_lcb_len(conn->buffer[*offset]); int len = conn->read_int(*offset, len_bytes); + MYSQL_LOGINFO1("MySQL_Query::read_string: offset = ", *offset); + MYSQL_LOGINFO3("MySQL_Query::read_string: len = ", len, "len_bytes =", len_bytes); + if (len == 251) { // This is a null field. @@ -460,16 +499,25 @@ char *MySQL_Query::read_string(int *offset) strncpy(str, "NULL", 4); str[4] = 0x00; *offset += len_bytes; - } - else + } + + // KH, new from v1.0.1 + if ( (len < 251) && (len > 0) ) { - str = (char *)malloc(len + 1); + // TODO, KH, check where str is freed + str = (char *) malloc(len + 1); + + MYSQL_LOGINFO3("MySQL_Query::read_string: len = ", len, "conn->buffer size =", conn->largest_buffer_size); + strncpy(str, (char *)&conn->buffer[*offset + len_bytes], len); str[len] = 0x00; *offset += len_bytes + len; + + return str; } - return str; + return NULL; + ////// } diff --git a/src/MySQL_Generic_WiFi.h b/src/MySQL_Generic_WiFi.h index 6e191ea8..a86bc70d 100644 --- a/src/MySQL_Generic_WiFi.h +++ b/src/MySQL_Generic_WiFi.h @@ -11,12 +11,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - (using Ethernet, EthernetLarge, Ethernet2, Ethernet3 library) and WiFiNINA + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ #ifndef MYSQL_GENERIC_WIFI_H diff --git a/src/MySQL_Generic_WiFiNINA.h b/src/MySQL_Generic_WiFiNINA.h index 6bee0548..e0e637c3 100644 --- a/src/MySQL_Generic_WiFiNINA.h +++ b/src/MySQL_Generic_WiFiNINA.h @@ -11,12 +11,13 @@ Built by Khoi Hoang https://github.com/khoih-prog/MySQL_MariaDB_Generic Licensed under MIT license - Version: 1.0.0 + Version: 1.0.1 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.0.0 K Hoang 13/08/2020 Initial coding/porting to support nRF52, SAM DUE and SAMD21/SAMD51 boards using W5x00 Ethernet - (using Ethernet, EthernetLarge, Ethernet2, Ethernet3 library) and WiFiNINA + (Ethernet, EthernetLarge, Ethernet2, Ethernet3 library), WiFiNINA and ESP8266/ESP32-AT shields + 1.0.1 K Hoang 18/08/2020 Add support to Ethernet ENC28J60. Fix bug, optimize code. **********************************************************************************************************************************/ #ifndef MYSQL_GENERIC_WIFININA_H