12 #include <csutil/scf.h> 13 #include <csutil/scf_implementation.h> 14 #include <csutil/threading/thread.h> 16 #include "iutil/comp.h" 22 #include <csutil/csstring.h> 28 struct iObjectRegistry;
32 #ifdef USE_DELAY_QUERY 33 #define THREADED_BUFFER_SIZE 300 34 class DelayedQueryManager :
public CS::Threading::Runnable
37 DelayedQueryManager(
const char *host,
unsigned int port,
const char *database,
38 const char *user,
const char *pwd);
41 void Push(csString query);
45 csString arr[THREADED_BUFFER_SIZE];
47 CS::Threading::Mutex mutex;
48 CS::Threading::RecursiveMutex mutexArray;
49 CS::Threading::Condition datacondition;
60 class psMysqlConnection :
public scfImplementation2<psMysqlConnection, iComponent, iDataConnection>
66 iObjectRegistry *objectReg;
72 psMysqlConnection(
iBase *iParent);
73 virtual ~psMysqlConnection();
76 bool Initialize(
const char *host,
unsigned int port,
const char *database,
77 const char *user,
const char *pwd,
LogCSV* logcsv);
86 void Escape(csString& to,
const char *from);
88 iResultSet *Select(
const char *sql,...);
89 int SelectSingleNumber(
const char *sql, ...);
90 unsigned long Command(
const char *sql,...);
91 unsigned long CommandPump(
const char *sql,...);
93 uint64 GenericInsertWithID(
const char *table,
const char **fieldnames,
psStringArray& fieldvalues);
94 bool GenericUpdateWithID(
const char *table,
const char *idfield,
const char *
id,
const char **fieldnames,
psStringArray& fieldvalues);
95 bool GenericUpdateWithID(
const char *table,
const char *idfield,
const char *
id,
psStringArray& fields);
97 const char *GetLastError(
void);
98 const char *GetLastQuery(
void)
102 uint64 GetLastInsertID();
104 const char *uint64tostring(uint64 value,csString& recv);
106 virtual const char* DumpProfile();
107 virtual void ResetProfile();
109 iRecord* NewUpdatePreparedStatement(
const char* table,
const char* idfield,
unsigned int count,
const char* file,
unsigned int line);
110 iRecord* NewInsertPreparedStatement(
const char* table,
unsigned int count,
const char* file,
unsigned int line);
112 #ifdef USE_DELAY_QUERY 113 csRef<DelayedQueryManager> dqm;
114 csRef<Thread> dqmThread;
119 class psResultRow :
public iResultRow
135 void SetMaxFields(
int fields) { max = fields; };
136 void SetResultSet(
void* resultsettoken);
140 const char *operator[](
int whichfield);
141 const char *operator[](
const char *fieldname);
143 const char* GetString(
int whichfield);
144 const char* GetString(
const char *fieldname);
146 int GetInt(
int whichfield);
147 int GetInt(
const char *fieldname);
149 unsigned long GetUInt32(
int whichfield);
150 unsigned long GetUInt32(
const char *fieldname);
152 float GetFloat(
int whichfield);
153 float GetFloat(
const char *fieldname);
155 uint64 GetUInt64(
int whichfield);
156 uint64 GetUInt64(
const char *fieldname);
158 uint64 stringtouint64(
const char *stringbuf);
159 const char *uint64tostring(uint64 value,
char *stringbuf,
int buflen);
162 class psResultSet :
public iResultSet
166 unsigned long rows, fields, current;
170 psResultSet(MYSQL *conn);
171 virtual ~psResultSet();
173 void Release(
void) {
delete this; };
175 iResultRow& operator[](
unsigned long whichrow);
177 unsigned long Count(
void) {
return rows; };
180 class dbRecord :
public iRecord
207 unsigned int uiValue;
208 unsigned short usValue;
211 unsigned long length;
216 virtual void AddToStatement(
const char* fname) = 0;
218 virtual void SetID(uint32 uid) = 0;
221 dbRecord(MYSQL*
db,
const char* Table,
const char* Idfield,
unsigned int count,
LogCSV* logcsv,
const char* file,
unsigned int line)
226 bind =
new MYSQL_BIND[count];
227 memset(bind, 0,
sizeof(MYSQL_BIND) * count);
228 temp =
new dataType[count];
231 this->logcsv = logcsv;
235 stmt = (MYSQL_STMT*) mysql_stmt_init(conn);
241 mysql_stmt_close(stmt);
249 memset(bind, 0,
sizeof(MYSQL_BIND) * count);
254 void AddField(
const char* fname,
float fValue);
256 void AddField(
const char* fname,
int iValue);
258 void AddField(
const char* fname,
unsigned int uiValue);
260 void AddField(
const char* fname,
unsigned short usValue);
262 void AddField(
const char* fname,
const char* sValue);
264 void AddFieldNull(
const char* fname);
266 virtual bool Prepare() = 0;
268 virtual bool Execute(uint32 uid);
271 class dbInsert :
public dbRecord
273 virtual void AddToStatement(
const char* fname)
279 virtual void SetID(uint32 ) { };
282 dbInsert(MYSQL*
db,
const char* Table,
unsigned int count,
LogCSV* logcsv,
const char* file,
unsigned int line)
283 : dbRecord(db, Table,
"", count, logcsv, file, line) { }
285 virtual bool Prepare();
288 class dbUpdate :
public dbRecord
290 virtual void AddToStatement(
const char* fname)
293 command.FormatPush(
"%s = ?", fname);
296 virtual void SetID(uint32 uid)
298 temp[index].uiValue = uid;
299 bind[index].buffer_type = MYSQL_TYPE_LONG;
300 bind[index].buffer = &(temp[index].uiValue);
301 bind[index].is_unsigned =
true;
305 dbUpdate(MYSQL*
db,
const char* Table,
const char* Idfield,
unsigned int count,
LogCSV* logcsv,
const char* file,
unsigned int line)
306 : dbRecord(db, Table, Idfield, count, logcsv, file, line) { }
308 virtual bool Prepare();
312 CS_PLUGIN_NAMESPACE_END(dbmysql)
iDataConnection * db
Global connection to the Database. Set from the psDatabase class.
CS_PLUGIN_NAMESPACE_BEGIN(dbmysql)
Statistics of time consumed by SQL statements.
void void Initialize(iObjectRegistry *object_reg)
A slightly improved version of csStringArray, sporting the handy FormatPush method.