Files

56 lines
1.8 KiB
C++
Raw Permalink Normal View History

2024-09-18 16:52:08 -07:00
#ifndef ____mysql_h__
#define ____mysql_h__
2022-05-19 11:43:15 -07:00
#include "Tag.h"
#include "ZString.h"
#include "MString.h"
#include <sstream>
#include <string>
#include <unordered_map>
2024-09-18 16:52:08 -07:00
#include <mysql/mysql.h>
2022-05-19 11:43:15 -07:00
namespace jet {
class __mysql : public Tag {
public:
2024-11-27 09:47:17 -08:00
__mysql(coreutils::ZString &in, coreutils::MString &parentOut, Global &global, Tag *parent, Tag *local);
2024-09-18 16:52:08 -07:00
~__mysql();
2022-05-19 11:43:15 -07:00
2024-09-18 16:52:08 -07:00
void query(coreutils::MString query);
2024-09-26 10:29:33 -07:00
void nextRow();
bool hasRow();
coreutils::ZString getColumnValue(coreutils::ZString column);
2024-09-18 16:52:08 -07:00
private:
// Explicitly initialized - result/row/fieldLength were otherwise
// left as garbage stack values until query() first ran. A
// <mysql>...</mysql> block that connects successfully but never
// runs a <sql> query inside it (e.g. just a connectivity check)
// would previously reach ~__mysql()'s mysql_free_result(result)
// with an uninitialized pointer - real undefined behavior, found
// while root-causing a live crash in a related part of this same
// class's constructor.
MYSQL *mysql = NULL;
MYSQL_RES *result = NULL;
MYSQL_ROW row = NULL;
unsigned long *fieldLength = NULL;
unsigned int qFields = 0;
2024-09-18 16:52:08 -07:00
coreutils::MString sessionId;
// Column-name -> row[] index, built once per query() call (see its
// own comment) instead of getColumnValue() re-deriving it via a
// fresh mysql_fetch_field_direct() linear scan on every single
// named lookup - a template reading N columns off one row used to
// pay that scan N times over, even though the schema is fixed for
// the lifetime of one result set.
std::unordered_map<std::string, int> fieldIndex;
coreutils::MString nbrOfRows = "0";
coreutils::MString nbrOfColumns = "0";
2024-09-18 16:52:08 -07:00
2022-05-19 11:43:15 -07:00
};
}
#endif