1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
|
/*
* db.h
*
* See the README file for copyright information and how to reach the author.
*
*/
#ifndef __DB_H
#define __DB_H
#include <linux/unistd.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <mysql/mysql.h>
#include <list>
#include <sstream>
#include "common.h"
class cDbTable;
class cDbConnection;
using namespace std;
//***************************************************************************
// cDbService
//***************************************************************************
class cDbService
{
public:
enum Misc
{
maxIndexFields = 20
};
enum FieldFormat
{
ffUnknown = na,
ffInt,
ffUInt,
ffAscii, // -> VARCHAR
ffText,
ffMlob, // -> MEDIUMBLOB
ffFloat,
ffDateTime,
ffCount
};
enum FieldType
{
ftUnknown = na,
ftData = 1,
ftPrimary = 2,
ftMeta = 4,
ftCalc = 8,
ftAutoinc = 16,
ftDef0 = 32
};
struct FieldDef
{
const char* name;
FieldFormat format;
int size;
int index;
int type;
};
enum BindType
{
bndIn = 0x001,
bndOut = 0x002,
bndSet = 0x004
};
enum ProcType
{
ptProcedure,
ptFunction
};
struct IndexDef
{
const char* name;
int fields[maxIndexFields+1];
int order; // not implemented yet
};
static const char* toString(FieldFormat t);
static FieldFormat toDictFormat(const char* format);
static const char* formats[];
static const char* dictFormats[];
static FieldType toType(const char* type);
static const char* types[];
};
typedef cDbService cDBS;
//***************************************************************************
// cDbValue
//***************************************************************************
class cDbValue : public cDbService
{
public:
cDbValue(FieldDef* f = 0)
{
field = 0;
strValue = 0;
ownField = 0;
if (f) setField(f);
}
cDbValue(const char* name, FieldFormat format, int size)
{
strValue = 0;
ownField = new FieldDef;
ownField->name = strdup(name);
ownField->format = format;
ownField->size = size;
ownField->type = ftData;
field = ownField;
clear();
}
virtual ~cDbValue()
{
free();
}
void free()
{
clear();
::free(strValue);
strValue = 0;
if (ownField)
{
::free((char*)ownField->name); // böser cast ;)
delete ownField;
ownField = 0;
}
field = 0;
}
void clear()
{
if (strValue)
*strValue = 0;
strValueSize = 0;
numValue = 0;
floatValue = 0;
memset(&timeValue, 0, sizeof(timeValue));
nullValue = 1;
initialized = no;
}
virtual void setField(FieldDef* f)
{
free();
field = f;
if (field)
strValue = (char*)calloc(field->size+TB, sizeof(char));
}
virtual FieldDef* getField() { return field; }
virtual const char* getName() { return field->name; }
void setValue(const char* value, int size = 0)
{
clear();
if (field->format != ffAscii && field->format != ffText && field->format != ffMlob)
{
tell(0, "Setting invalid field format for '%s', expected ASCII or MLOB", field->name);
return;
}
if (field->format == ffMlob && !size)
{
tell(0, "Missing size for MLOB field '%s'", field->name);
return;
}
if (value && size)
{
if (size > field->size)
{
tell(0, "Warning, size of %d for '%s' exeeded, got %d bytes!",
field->size, field->name, size);
size = field->size;
}
memcpy(strValue, value, size);
strValue[size] = 0;
strValueSize = size;
nullValue = 0;
}
else if (value)
{
if (strlen(value) > (size_t)field->size)
tell(0, "Warning, size of %d for '%s' exeeded [%s]",
field->size, field->name, value);
sprintf(strValue, "%.*s", field->size, value);
strValueSize = strlen(strValue);
nullValue = 0;
}
}
void setCharValue(char value)
{
char tmp[2];
tmp[0] = value;
tmp[1] = 0;
setValue(tmp);
}
void setValue(int value)
{
setValue((long)value);
}
void setValue(long value)
{
if (field->format == ffInt || field->format == ffUInt)
{
numValue = value;
nullValue = 0;
}
else if (field->format == ffDateTime)
{
struct tm tm;
time_t v = value;
memset(&tm, 0, sizeof(tm));
localtime_r(&v, &tm);
timeValue.year = tm.tm_year + 1900;
timeValue.month = tm.tm_mon + 1;
timeValue.day = tm.tm_mday;
timeValue.hour = tm.tm_hour;
timeValue.minute = tm.tm_min;
timeValue.second = tm.tm_sec;
nullValue = 0;
}
else
{
tell(0, "Setting invalid field format for '%s'", field->name);
}
}
void setValue(double value)
{
if (field->format == ffInt || field->format == ffUInt)
{
numValue = value;
nullValue = 0;
}
else if (field->format == ffFloat)
{
floatValue = value;
nullValue = 0;
}
else
{
tell(0, "Setting invalid field format for '%s'", field->name);
}
}
int hasValue(long value)
{
if (field->format == ffInt || field->format == ffUInt)
return numValue == value;
if (field->format == ffDateTime)
return no; // to be implemented!
tell(0, "Setting invalid field format for '%s'", field->name);
return no;
}
int hasValue(double value)
{
if (field->format == ffInt || field->format == ffUInt)
return numValue == value;
if (field->format == ffFloat)
return floatValue == value;
tell(0, "Setting invalid field format for '%s'", field->name);
return no;
}
int hasValue(const char* value)
{
if (!value)
value = "";
if (field->format != ffAscii && field->format != ffText)
{
tell(0, "Checking invalid field format for '%s', expected ASCII or MLOB", field->name);
return no;
}
return strcmp(getStrValue(), value) == 0;
}
time_t getTimeValue()
{
struct tm tm;
memset(&tm, 0, sizeof(tm));
tm.tm_isdst = -1; // force DST auto detect
tm.tm_year = timeValue.year - 1900;
tm.tm_mon = timeValue.month - 1;
tm.tm_mday = timeValue.day;
tm.tm_hour = timeValue.hour;
tm.tm_min = timeValue.minute;
tm.tm_sec = timeValue.second;
return mktime(&tm);
}
unsigned long* getStrValueSizeRef() { return &strValueSize; }
unsigned long getStrValueSize() { return strValueSize; }
const char* getStrValue() { return !isNull() && strValue ? strValue : ""; }
long getIntValue() { return !isNull() ? numValue : 0; }
float getFloatValue() { return !isNull() ? floatValue : 0; }
int isNull() { return nullValue; }
char* getStrValueRef() { return strValue; }
long* getIntValueRef() { return &numValue; }
MYSQL_TIME* getTimeValueRef() { return &timeValue; }
float* getFloatValueRef() { return &floatValue; }
my_bool* getNullRef() { return &nullValue; }
private:
FieldDef* ownField;
FieldDef* field;
long numValue;
float floatValue;
MYSQL_TIME timeValue;
char* strValue;
unsigned long strValueSize;
my_bool nullValue;
int initialized;
};
//***************************************************************************
// cDbStatement
//***************************************************************************
class cDbStatement : public cDbService
{
public:
cDbStatement(cDbTable* aTable);
cDbStatement(cDbConnection* aConnection, const char* stmt = "");
virtual ~cDbStatement();
int execute(int noResult = no);
int find();
int fetch();
int freeResult();
// interface
int build(const char* format, ...);
void setBindPrefix(const char* p) { bindPrefix = p; }
void clrBindPrefix() { bindPrefix = 0; }
int bind(cDbValue* value, int mode, const char* delim = 0);
int bind(cDbTable* aTable, int field, int mode, const char* delim);
int bind(int field, int mode, const char* delim = 0);
int bindCmp(const char* table, cDbValue* value,
const char* comp, const char* delim = 0);
int bindCmp(const char* table, int field, cDbValue* value,
const char* comp, const char* delim = 0);
// ..
int prepare();
int getAffected() { return affected; }
int getResultCount();
const char* asText() { return stmtTxt.c_str(); }
void showStat();
// data
static int explain; // debug explain
private:
void clear();
int appendBinding(cDbValue* value, BindType bt);
string stmtTxt;
MYSQL_STMT* stmt;
int affected;
cDbConnection* connection;
cDbTable* table;
int inCount;
MYSQL_BIND* inBind; // to db
int outCount;
MYSQL_BIND* outBind; // from db (result)
MYSQL_RES* metaResult;
const char* bindPrefix;
int firstExec; // debug explain
unsigned long callsPeriod;
unsigned long callsTotal;
double duration;
};
//***************************************************************************
// cDbStatements
//***************************************************************************
class cDbStatements
{
public:
cDbStatements() { statisticPeriod = time(0); }
~cDbStatements() {};
void append(cDbStatement* s) { statements.push_back(s); }
void remove(cDbStatement* s) { statements.remove(s); }
void showStat(const char* name)
{
tell(0, "Statement statistic of last %ld seconds from '%s':", time(0) - statisticPeriod, name);
for (std::list<cDbStatement*>::iterator it = statements.begin() ; it != statements.end(); ++it)
{
if (*it)
(*it)->showStat();
}
statisticPeriod = time(0);
}
private:
time_t statisticPeriod;
std::list<cDbStatement*> statements;
};
//***************************************************************************
// Class Database Row
//***************************************************************************
class cDbRow : public cDbService
{
public:
cDbRow(FieldDef* f)
{
count = 0;
fieldDef = 0;
useFields(f);
dbValues = new cDbValue[count];
for (int f = 0; f < count; f++)
dbValues[f].setField(getField(f));
}
virtual ~cDbRow() { delete[] dbValues; }
void clear()
{
for (int f = 0; f < count; f++)
dbValues[f].clear();
}
virtual FieldDef* getField(int f) { return f < 0 ? 0 : fieldDef+f; }
virtual int fieldCount() { return count; }
void setValue(int f, const char* value,
int size = 0) { dbValues[f].setValue(value, size); }
void setValue(int f, int value) { dbValues[f].setValue(value); }
void setValue(int f, long value) { dbValues[f].setValue(value); }
void setValue(int f, double value) { dbValues[f].setValue(value); }
void setCharValue(int f, char value) { dbValues[f].setCharValue(value); }
int hasValue(int f, const char* value) const { return dbValues[f].hasValue(value); }
int hasValue(int f, long value) const { return dbValues[f].hasValue(value); }
int hasValue(int f, double value) const { return dbValues[f].hasValue(value); }
cDbValue* getValue(int f) { return &dbValues[f]; }
const char* getStrValue(int f) const { return dbValues[f].getStrValue(); }
long getIntValue(int f) const { return dbValues[f].getIntValue(); }
float getFloatValue(int f) const { return dbValues[f].getFloatValue(); }
int isNull(int f) const { return dbValues[f].isNull(); }
protected:
virtual void useFields(FieldDef* f) { fieldDef = f; for (count = 0; (fieldDef+count)->name; count++); }
int count; // field count
FieldDef* fieldDef;
cDbValue* dbValues;
};
//***************************************************************************
// Connection
//***************************************************************************
class cDbConnection
{
public:
cDbConnection()
{
mysql = 0;
attached = 0;
inTact = no;
connectDropped = yes;
}
virtual ~cDbConnection()
{
close();
}
int isConnected() { return getMySql() > 0; }
int attachConnection()
{
static int first = yes;
if (!mysql)
{
connectDropped = yes;
tell(0, "Calling mysql_init(%ld)", syscall(__NR_gettid));
if (!(mysql = mysql_init(0)))
return errorSql(this, "attachConnection(init)");
if (!mysql_real_connect(mysql, dbHost,
dbUser, dbPass, dbName, dbPort, 0, 0))
{
close();
tell(0, "Error, connecting to database at '%s' on port (%d) failed",
dbHost, dbPort);
return fail;
}
connectDropped = no;
// init encoding
if (encoding && *encoding)
{
if (mysql_set_character_set(mysql, encoding))
errorSql(this, "init(character_set)");
if (first)
{
tell(0, "SQL client character now '%s'", mysql_character_set_name(mysql));
first = no;
}
}
}
attached++;
return success;
}
void close()
{
if (mysql)
{
tell(0, "Closing mysql connection and calling mysql_thread_end(%ld)", syscall(__NR_gettid));
mysql_close(mysql);
mysql_thread_end();
mysql = 0;
}
}
void detachConnection()
{
attached--;
if (!attached)
close();
}
int check()
{
if (!isConnected())
return fail;
query("SELECT SYSDATE();");
queryReset();
return isConnected() ? success : fail;
}
virtual int query(const char* format, ...)
{
int status = 1;
MYSQL* h = getMySql();
if (h && format)
{
char* stmt;
va_list more;
va_start(more, format);
vasprintf(&stmt, format, more);
if ((status = mysql_query(h, stmt)))
errorSql(this, stmt);
free(stmt);
}
return status ? fail : success;
}
virtual void queryReset()
{
if (getMySql())
{
MYSQL_RES* result = mysql_use_result(getMySql());
mysql_free_result(result);
}
}
virtual int executeSqlFile(const char* file)
{
FILE* f;
int res;
char* buffer;
int size = 1000;
int nread = 0;
if (!getMySql())
return fail;
if (!(f = fopen(file, "r")))
{
tell(0, "Fatal: Can't access '%s'; %s", file, strerror(errno));
return fail;
}
buffer = (char*)malloc(size+1);
while (res = fread(buffer+nread, 1, 1000, f))
{
nread += res;
size += 1000;
buffer = srealloc(buffer, size+1);
}
fclose(f);
buffer[nread] = 0;
// execute statement
tell(2, "Executing '%s'", buffer);
if (query("%s", buffer))
{
free(buffer);
return errorSql(this, "executeSqlFile()");
}
free(buffer);
return success;
}
virtual int startTransaction()
{
inTact = yes;
return query("START TRANSACTION");
}
virtual int commit()
{
inTact = no;
return query("COMMIT");
}
virtual int rollback()
{
inTact = no;
return query("ROLLBACK");
}
virtual int inTransaction() { return inTact; }
MYSQL* getMySql()
{
if (connectDropped && mysql)
close();
return mysql;
}
int getAttachedCount() { return attached; }
// --------------
// static stuff
// set/get connecting data
static void setHost(const char* s) { free(dbHost); dbHost = strdup(s); }
static const char* getHost() { return dbHost; }
static void setName(const char* s) { free(dbName); dbName = strdup(s); }
static const char* getName() { return dbName; }
static void setUser(const char* s) { free(dbUser); dbUser = strdup(s); }
static const char* getUser() { return dbUser; }
static void setPass(const char* s) { free(dbPass); dbPass = strdup(s); }
static const char* getPass() { return dbPass; }
static void setPort(int port) { dbPort = port; }
static int getPort() { return dbPort; }
static void setEncoding(const char* enc) { free(encoding); encoding = strdup(enc); }
static const char* getEncoding() { return encoding; }
int errorSql(cDbConnection* mysql, const char* prefix, MYSQL_STMT* stmt = 0, const char* stmtTxt = 0);
void showStat(const char* name = "") { statements.showStat(name); }
// -----------------------------------------------------------
// init() and exit() must exactly called 'once' per process
static int init(key_t semKey)
{
if (semKey && !sem)
sem = new Sem(semKey);
if (!sem || sem->check() == success)
{
// call only once per process
if (sem)
tell(1, "Info: Calling mysql_library_init()");
if (mysql_library_init(0, 0, 0))
{
tell(0, "Error: mysql_library_init() failed");
return fail;
}
}
else if (sem)
{
tell(1, "Info: Skipping calling mysql_library_init(), it's already done!");
}
if (sem)
sem->inc(); // count usage per process
return success;
}
static int exit()
{
mysql_thread_end();
if (sem)
sem->dec();
if (!sem || sem->check() == success)
{
if (sem)
tell(1, "Info: Released the last usage of mysql_lib, calling mysql_library_end() now");
mysql_library_end();
}
else if (sem)
{
tell(1, "Info: The mysql_lib is still in use, skipping mysql_library_end() call");
}
free(dbHost);
free(dbUser);
free(dbPass);
free(dbName);
free(encoding);
return done;
}
MYSQL* mysql;
cDbStatements statements; // all statements of this connection
private:
int initialized;
int attached;
int inTact;
int connectDropped;
static Sem* sem;
static char* encoding;
// connecting data
static char* dbHost;
static int dbPort;
static char* dbName; // database name
static char* dbUser;
static char* dbPass;
};
//***************************************************************************
// cDbTable
//***************************************************************************
class cDbTable : public cDbService
{
public:
cDbTable(cDbConnection* aConnection, FieldDef* f, IndexDef* i = 0);
virtual ~cDbTable();
virtual const char* TableName() = 0;
virtual int open();
virtual int close();
virtual int find();
virtual void reset() { reset(stmtSelect); }
virtual int find(cDbStatement* stmt);
virtual int fetch(cDbStatement* stmt);
virtual void reset(cDbStatement* stmt);
virtual int insert();
virtual int update();
virtual int store();
virtual int deleteWhere(const char* where);
virtual int countWhere(const char* where, int& count, const char* what = 0);
virtual int truncate();
// interface to cDbRow
void clear() { row->clear(); }
void setValue(int f, const char* value, int size = 0) { row->setValue(f, value, size); }
void setValue(int f, int value) { row->setValue(f, value); }
void setValue(int f, long value) { row->setValue(f, value); }
void setValue(int f, double value) { row->setValue(f, value); }
void setCharValue(int f, char value) { row->setCharValue(f, value); }
int hasValue(int f, const char* value) { return row->hasValue(f, value); }
int hasValue(int f, long value) { return row->hasValue(f, value); }
int hasValue(int f, double value) { return row->hasValue(f, value); }
const char* getStrValue(int f) const { return row->getStrValue(f); }
long getIntValue(int f) const { return row->getIntValue(f); }
float getFloatValue(int f) const { return row->getFloatValue(f); }
int isNull(int f) const { return row->isNull(f); }
FieldDef* getField(int f) { return row->getField(f); }
cDbValue* getValue(int f) { return row->getValue(f); }
int fieldCount() { return row->fieldCount(); }
cDbRow* getRow() { return row; }
cDbConnection* getConnection() { return connection; }
MYSQL* getMySql() { return connection->getMySql(); }
int isConnected() { return connection && connection->getMySql(); }
virtual IndexDef* getIndex(int i) { return indices+i; }
virtual int exist(const char* name = 0);
virtual int createTable();
// static stuff
static void setConfPath(const char* cpath) { free(confPath); confPath = strdup(cpath); }
protected:
virtual int init();
virtual int createIndices();
virtual int checkIndex(const char* idxName, int& fieldCount);
virtual void copyValues(cDbRow* r);
// data
cDbRow* row;
int holdInMemory; // hold table additionally in memory (not implemented yet)
IndexDef* indices;
// basic statements
cDbStatement* stmtSelect;
cDbStatement* stmtInsert;
cDbStatement* stmtUpdate;
cDbConnection* connection;
// statics
static char* confPath;
};
//***************************************************************************
// cDbView
//***************************************************************************
class cDbView : public cDbService
{
public:
cDbView(cDbConnection* c, const char* aName)
{
connection = c;
name = strdup(aName);
}
~cDbView() { free(name); }
int exist()
{
if (connection->getMySql())
{
MYSQL_RES* result = mysql_list_tables(connection->getMySql(), name);
MYSQL_ROW tabRow = mysql_fetch_row(result);
mysql_free_result(result);
return tabRow ? yes : no;
}
return no;
}
int create(const char* path, const char* sqlFile)
{
int status;
char* file = 0;
asprintf(&file, "%s/%s", path, sqlFile);
tell(0, "Creating view '%s' using definition in '%s'",
name, file);
status = connection->executeSqlFile(file);
free(file);
return status;
}
int drop()
{
tell(0, "Drop view '%s'", name);
return connection->query("drop view %s", name);
}
protected:
cDbConnection* connection;
char* name;
};
//***************************************************************************
// cDbProcedure
//***************************************************************************
class cDbProcedure : public cDbService
{
public:
cDbProcedure(cDbConnection* c, const char* aName, ProcType pt = ptProcedure)
{
connection = c;
type = pt;
name = strdup(aName);
}
~cDbProcedure() { free(name); }
const char* getName() { return name; }
int call(int ll = 1)
{
if (!connection || !connection->getMySql())
return fail;
cDbStatement stmt(connection);
tell(ll, "Calling '%s'", name);
stmt.build("call %s", name);
if (stmt.prepare() != success || stmt.execute() != success)
return fail;
tell(ll, "'%s' suceeded", name);
return success;
}
int created()
{
if (!connection || !connection->getMySql())
return fail;
cDbStatement stmt(connection);
stmt.build("show %s status where name = '%s'",
type == ptProcedure ? "procedure" : "function", name);
if (stmt.prepare() != success || stmt.execute() != success)
{
tell(0, "%s check of '%s' failed",
type == ptProcedure ? "Procedure" : "Function", name);
return no;
}
else
{
if (stmt.getResultCount() != 1)
return no;
}
return yes;
}
int create(const char* path)
{
int status;
char* file = 0;
asprintf(&file, "%s/%s.sql", path, name);
tell(1, "Creating %s '%s'",
type == ptProcedure ? "procedure" : "function", name);
status = connection->executeSqlFile(file);
free(file);
return status;
}
int drop()
{
tell(1, "Drop %s '%s'", type == ptProcedure ? "procedure" : "function", name);
return connection->query("drop %s %s", type == ptProcedure ? "procedure" : "function", name);
}
static int existOnFs(const char* path, const char* name)
{
int state;
char* file = 0;
asprintf(&file, "%s/%s.sql", path, name);
state = fileExists(file);
free(file);
return state;
}
protected:
cDbConnection* connection;
ProcType type;
char* name;
};
//***************************************************************************
#endif //__DB_H
|