/** * get_row_values - reads the row values from the read buffer * * This method is used to read the row column values * from the read buffer and store them in the row structure * in the class. * */ boolean Connector::get_row_values() { int res = 0; int offset = 0; // It is an error to try to read rows before columns // are read. if (!columns_read) { Serial.println("ERROR: You must read the columns first!"); return true; } // Drop any row data already read free_row_buffer(); // Read a row res = get_row(); if (res != EOF_PACKET) { offset = 4; for (int f = 0; f < num_cols; f++) { row.values[f] = read_string(&offset); } } else { return res; } return true; }
/** * show_results - Show a result set from the server via Serial.print * * This method reads a result from the server and displays it via the * via the Serial.print methods. It can be used in cases where * you may want to issue a SELECT or SHOW and see the results on your * computer from the Arduino. * * It is also a good example of how to read a result set from the * because it uses the public methods designed to return result * sets from the server. */ void Connector::show_results() { column_names *cols; int rows = 0; // Get the columns cols = get_columns(); if (cols == NULL) { return; } for (int f = 0; f < columns.num_fields; f++) { Serial.print(columns.fields[f]->name); if (f < columns.num_fields-1) Serial.print(','); } Serial.println(); // Read the rows while (get_next_row()) { rows++; for (int f = 0; f < columns.num_fields; f++) { Serial.print(row.values[f]); if (f < columns.num_fields-1) Serial.print(','); } free_row_buffer(); Serial.println(); } // Report how many rows were read Serial.print(rows); Serial.println(" rows in result."); free_columns_buffer(); }
/* get_next_row - Iterator for reading rows from a result set This method returns an instance of a structure (row_values) that contains an array of strings representing the row values returned from the server. The caller can use the values however needed - by first converting them to a specific type or as a string. */ row_values *MySQL_Cursor::get_next_row() { int res = 0; free_row_buffer(); // Read the rows res = get_row_values(); if (res != MYSQL_EOF_PACKET) { return &row; } return NULL; }
/* get_columns - Get a list of the columns (fields) This method returns an instance of the column_names structure that contains an array of fields. Note: you should call free_columns_buffer() after consuming the field data to free memory. */ column_names *MySQL_Cursor::get_columns() { free_columns_buffer(); free_row_buffer(); num_cols = 0; if (get_fields()) { columns_read = true; return &columns; } else { return NULL; } }
/* get_row_values - reads the row values from the read buffer This method is used to read the row column values from the read buffer and store them in the row structure in the class. */ int MySQL_Cursor::get_row_values() { int res = 0; int offset = 0; // It is an error to try to read rows before columns // are read. if (!columns_read) { conn->show_error(READ_COLS, true); return MYSQL_EOF_PACKET; } // Drop any row data already read free_row_buffer(); // Read a row res = get_row(); if (res != MYSQL_EOF_PACKET) { offset = 4; for (int f = 0; f < num_cols; f++) { row.values[f] = read_string(&offset); } } return res; }
/* show_results - Show a result set from the server via Serial.print This method reads a result from the server and displays it via the via the Serial.print methods. It can be used in cases where you may want to issue a SELECT or SHOW and see the results on your computer from the Arduino. It is also a good example of how to read a result set from the because it uses the public methods designed to return result sets from the server. */ void MySQL_Cursor::show_results() { column_names *cols; int rows = 0; // Get the columns cols = get_columns(); if (cols == NULL) { return; } for (int f = 0; f < columns.num_fields; f++) { Serial.print(columns.fields[f]->name); if (f < columns.num_fields-1) Serial.print(','); } Serial.println(); // Read the rows while (get_next_row()) { rows++; for (int f = 0; f < columns.num_fields; f++) { Serial.print(row.values[f]); if (f < columns.num_fields-1) Serial.print(','); } free_row_buffer(); Serial.println(); } // Report how many rows were read Serial.print(rows); conn->show_error(ROWS, true); free_columns_buffer(); // Free any post-query messages in queue for stored procedures clear_ok_packet(); }
/* Close Takes care of removing allocated memory. */ void MySQL_Cursor::close() { free_columns_buffer(); free_row_buffer(); }