svc_db.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #ifndef _SVC_DB_H
  2. #define _SVC_DB_H
  3. #include <bfc/dispatch.h>
  4. #include <api/service/services.h>
  5. typedef enum {
  6. DBSVC_DATATYPE_UNKNOWN = 0,
  7. DBSVC_DATATYPE_INT = 1,
  8. DBSVC_DATATYPE_STRING = 2,
  9. DBSVC_DATATYPE_BINARY = 3,
  10. DBSVC_DATATYPE_GUID = 4,
  11. DBSVC_DATATYPE_FLOAT = 5,
  12. } dbSvcDatatypeEnum;
  13. typedef struct {
  14. void *data;
  15. dbSvcDatatypeEnum datatype;
  16. int datalen;
  17. } dbSvcDatachunk;
  18. typedef struct {
  19. const char *name;
  20. dbSvcDatatypeEnum datatype;
  21. int id;
  22. int indexed;
  23. int uniques_indexed;
  24. } dbSvcColInfo;
  25. class dbSvcScanner;
  26. class NOVTABLE dbSvcScanner : public Dispatchable {
  27. public:
  28. void first();
  29. void last();
  30. void block_next(); // blocking call for baclkward compat
  31. void block_previous(); // blocking call for baclkward compat
  32. int next(); // if non blocking, returns 0 for "HOLD ON!" and 1 for "GOT A ROW!"
  33. int previous(); // if non blocking, returns 0 for "HOLD ON!" and 1 for "GOT A ROW!"
  34. void push();
  35. void pop();
  36. int eof();
  37. int bof();
  38. int getNumRows();
  39. void moveToRow(int row);
  40. int getCurRow();
  41. int locateByName(const char *col, int from_row, dbSvcDatachunk *data);
  42. int locateById(int id, int from_row, dbSvcDatachunk *data);
  43. int getNumCols();
  44. dbSvcColInfo *enumCol(int n);
  45. dbSvcColInfo *getColById(int n);
  46. dbSvcColInfo *getColByName(const char *col);
  47. dbSvcDatachunk *getFieldByName(const char *col);
  48. dbSvcDatachunk *getFieldById(int id);
  49. void setIndexByName(const char *col);
  50. void setIndexById(int id);
  51. dbSvcScanner *newUniqueScannerByName(const char *col);
  52. dbSvcScanner *newUniqueScannerById(int colid);
  53. void deleteUniqueScanner(dbSvcScanner *);
  54. int query(const char *query);
  55. const char *getLastQuery();
  56. void cancelQuery();
  57. int hasIndexChanged();
  58. void clearDirtyBit();
  59. void joinScanner(dbSvcScanner *scanner, const char *field);
  60. void unjoinScanner(dbSvcScanner *scanner);
  61. void setBlocking(int b); // blocking is the default behavior
  62. enum {
  63. CBFIRST = 100,
  64. CBLAST = 110,
  65. CBNEXT = 120, // retired
  66. CBPREVIOUS = 130, // retired
  67. CBNEXT2 = 121,
  68. CBPREVIOUS2 = 131,
  69. CBPUSH = 140,
  70. CBPOP = 150,
  71. CBEOF = 160,
  72. CBBOF = 170,
  73. GETNUMROWS = 180,
  74. MOVETOROW = 190,
  75. GETCURROW = 200,
  76. LOCATEBYNAME = 210,
  77. LOCATEBYID = 220,
  78. GETNUMCOLS = 230,
  79. ENUMCOL = 240,
  80. GETCOLBYID = 250,
  81. GETCOLBYNAME = 260,
  82. GETFIELDBYNAME = 270,
  83. GETFIELDBYID = 280,
  84. SETINDEXBYNAME = 290,
  85. SETINDEXBYID = 300,
  86. QUERY = 310,
  87. CANCELQUERY = 320,
  88. INDEXCHANGED = 330,
  89. CLEARDIRTYBIT = 340,
  90. UNIQUEBYNAME = 350,
  91. UNIQUEBYID = 360,
  92. DELETEUNIQUE = 370,
  93. GETLASTQUERY = 380,
  94. JOINSCANNER = 390,
  95. UNJOINSCANNER = 400,
  96. SETBLOCKING = 410,
  97. };
  98. };
  99. inline void dbSvcScanner::first() {
  100. _voidcall(CBFIRST);
  101. }
  102. inline void dbSvcScanner::last() {
  103. _voidcall(CBLAST);
  104. }
  105. inline void dbSvcScanner::block_next() {
  106. _voidcall(CBNEXT);
  107. }
  108. inline int dbSvcScanner::next() {
  109. return _call(CBNEXT2, 0);
  110. }
  111. inline void dbSvcScanner::block_previous() {
  112. _voidcall(CBPREVIOUS);
  113. }
  114. inline int dbSvcScanner::previous() {
  115. return _call(CBPREVIOUS2, 0);
  116. }
  117. inline void dbSvcScanner::push() {
  118. _voidcall(CBPUSH);
  119. }
  120. inline void dbSvcScanner::pop() {
  121. _voidcall(CBPOP);
  122. }
  123. inline int dbSvcScanner::eof() {
  124. return _call(CBEOF, 0);
  125. }
  126. inline int dbSvcScanner::bof() {
  127. return _call(CBBOF, 0);
  128. }
  129. inline int dbSvcScanner::getNumRows() {
  130. return _call(GETNUMROWS, 0);
  131. }
  132. inline void dbSvcScanner::moveToRow(int row) {
  133. _voidcall(MOVETOROW, row);
  134. }
  135. inline int dbSvcScanner::getCurRow() {
  136. return _call(GETCURROW, 0);
  137. }
  138. inline int dbSvcScanner::locateByName(const char *col, int from_row, dbSvcDatachunk *data) {
  139. return _call(LOCATEBYNAME, 0, col, from_row, data);
  140. }
  141. inline int dbSvcScanner::locateById(int colid, int from_row, dbSvcDatachunk *data) {
  142. return _call(LOCATEBYNAME, 0, colid, from_row, data);
  143. }
  144. inline int dbSvcScanner::getNumCols() {
  145. return _call(GETNUMCOLS, 0);
  146. }
  147. inline dbSvcColInfo *dbSvcScanner::enumCol(int n) {
  148. return _call(ENUMCOL, ((dbSvcColInfo *)NULL), n);
  149. }
  150. inline dbSvcColInfo *dbSvcScanner::getColByName(const char *col) {
  151. return _call(GETCOLBYNAME, ((dbSvcColInfo *)NULL), col);
  152. }
  153. inline dbSvcColInfo *dbSvcScanner::getColById(int colid) {
  154. return _call(GETCOLBYID, ((dbSvcColInfo *)NULL), colid);
  155. }
  156. inline dbSvcDatachunk *dbSvcScanner::getFieldByName(const char *col) {
  157. return _call(GETFIELDBYNAME, ((dbSvcDatachunk *)NULL), col);
  158. }
  159. inline dbSvcDatachunk *dbSvcScanner::getFieldById(int colid) {
  160. return _call(GETFIELDBYNAME, ((dbSvcDatachunk *)NULL), colid);
  161. }
  162. inline void dbSvcScanner::setIndexByName(const char *col) {
  163. _voidcall(SETINDEXBYNAME, col);
  164. }
  165. inline void dbSvcScanner::setIndexById(int colid) {
  166. _voidcall(SETINDEXBYID, colid);
  167. }
  168. inline dbSvcScanner *dbSvcScanner::newUniqueScannerByName(const char *col) {
  169. return _call(UNIQUEBYNAME, (dbSvcScanner *)NULL, col);
  170. }
  171. inline dbSvcScanner *dbSvcScanner::newUniqueScannerById(int colid) {
  172. return _call(UNIQUEBYID, (dbSvcScanner *)NULL, colid);
  173. }
  174. inline void dbSvcScanner::deleteUniqueScanner(dbSvcScanner *s) {
  175. _voidcall(DELETEUNIQUE, s);
  176. }
  177. inline int dbSvcScanner::query(const char *q) {
  178. return _call(QUERY, 0, q);
  179. }
  180. inline void dbSvcScanner::cancelQuery() {
  181. _voidcall(CANCELQUERY);
  182. }
  183. inline int dbSvcScanner::hasIndexChanged() {
  184. return _call(INDEXCHANGED, 0);
  185. }
  186. inline void dbSvcScanner::clearDirtyBit() {
  187. _voidcall(CLEARDIRTYBIT);
  188. }
  189. inline const char *dbSvcScanner::getLastQuery() {
  190. return _call(GETLASTQUERY, (const char *)NULL);
  191. }
  192. inline void dbSvcScanner::joinScanner(dbSvcScanner *scanner, const char *field) {
  193. _voidcall(JOINSCANNER, scanner, field);
  194. }
  195. inline void dbSvcScanner::unjoinScanner(dbSvcScanner *scanner) {
  196. _voidcall(UNJOINSCANNER, scanner);
  197. }
  198. inline void dbSvcScanner::setBlocking(int b) {
  199. _voidcall(SETBLOCKING, b);
  200. }
  201. class NOVTABLE dbSvcScannerI : public dbSvcScanner {
  202. public:
  203. virtual void first()=0;
  204. virtual void last()=0;
  205. virtual void block_next()=0;
  206. virtual void block_previous()=0;
  207. virtual int next()=0;
  208. virtual int previous()=0;
  209. virtual void push()=0;
  210. virtual void pop()=0;
  211. virtual int eof()=0;
  212. virtual int bof()=0;
  213. virtual int getNumRows()=0;
  214. virtual void moveToRow(int row)=0;
  215. virtual int getCurRow()=0;
  216. virtual int locateByName(const char *col, int from_row, dbSvcDatachunk *data)=0;
  217. virtual int locateById(int id, int from_row, dbSvcDatachunk *data)=0;
  218. virtual int getNumCols()=0;
  219. virtual dbSvcColInfo *enumCol(int n)=0;
  220. virtual dbSvcColInfo *getColById(int n)=0;
  221. virtual dbSvcColInfo *getColByName(const char *col)=0;
  222. virtual dbSvcDatachunk *getFieldByName(const char *col)=0;
  223. virtual dbSvcDatachunk *getFieldById(int id)=0;
  224. virtual void setIndexByName(const char *col)=0;
  225. virtual void setIndexById(int id)=0;
  226. virtual dbSvcScanner *newUniqueScannerByName(const char *col)=0;
  227. virtual dbSvcScanner *newUniqueScannerById(int colid)=0;
  228. virtual void deleteUniqueScanner(dbSvcScanner *)=0;
  229. virtual int query(const char *query)=0;
  230. virtual void cancelQuery()=0;
  231. virtual int hasIndexChanged()=0;
  232. virtual void clearDirtyBit()=0;
  233. virtual const char *getLastQuery()=0;
  234. virtual void joinScanner(dbSvcScanner *scanner, const char *field)=0;
  235. virtual void unjoinScanner(dbSvcScanner *scanner)=0;
  236. virtual void setBlocking(int block)=0;
  237. protected:
  238. RECVS_DISPATCH;
  239. };
  240. class NOVTABLE dbSvcTable : public Dispatchable {
  241. public:
  242. dbSvcScanner *getScanner();
  243. dbSvcScanner *newScanner();
  244. void deleteScanner(dbSvcScanner *scanner);
  245. void _new();
  246. void insert();
  247. void cancel();
  248. void edit();
  249. void post();
  250. void _delete();
  251. int editing();
  252. void setFieldByName(const char *col, dbSvcDatachunk *data);
  253. void setFieldById(int colid, dbSvcDatachunk *data);
  254. void deleteFieldByName(const char *col);
  255. void deleteFieldById(int colid);
  256. void addColumn(const char *colname, int colid, int datatype, int uniques_indexed);
  257. void addIndexByName(const char *col);
  258. void addIndexById(int colid);
  259. void dropIndexByName(const char *col);
  260. void dropIndexById(int colid);
  261. void sync();
  262. enum {
  263. GETSCANNER = 100,
  264. NEWSCANNER = 110,
  265. DELETESCANNER = 111,
  266. CBNEW = 120,
  267. CBINSERT = 130,
  268. CBCANCEL = 140,
  269. CBEDIT = 150,
  270. CBPOST = 160,
  271. CBDELETE = 170,
  272. EDITING = 180,
  273. SETFIELDBYNAME = 190,
  274. SETFIELDBYID = 200,
  275. DELETEFIELDBYNAME = 210,
  276. DELETEFIELDBYID = 220,
  277. ADDCOLUMN = 230,
  278. ADDINDEXBYNAME = 240,
  279. ADDINDEXBYID = 250,
  280. SYNC = 260,
  281. DROPINDEXBYNAME = 270,
  282. DROPINDEXBYID = 280,
  283. };
  284. };
  285. inline dbSvcScanner *dbSvcTable::getScanner() {
  286. return _call(GETSCANNER, static_cast<dbSvcScanner *>(NULL));
  287. }
  288. inline dbSvcScanner *dbSvcTable::newScanner() {
  289. return _call(NEWSCANNER, static_cast<dbSvcScanner *>(NULL));
  290. }
  291. inline void dbSvcTable::deleteScanner(dbSvcScanner *scanner) {
  292. _voidcall(DELETESCANNER, scanner);
  293. }
  294. inline void dbSvcTable::_new() {
  295. _voidcall(CBNEW);
  296. }
  297. inline void dbSvcTable::insert() {
  298. _voidcall(CBINSERT);
  299. }
  300. inline void dbSvcTable::cancel() {
  301. _voidcall(CBCANCEL);
  302. }
  303. inline void dbSvcTable::edit() {
  304. _voidcall(CBEDIT);
  305. }
  306. inline void dbSvcTable::post() {
  307. _voidcall(CBPOST);
  308. }
  309. inline void dbSvcTable::_delete() {
  310. _voidcall(CBDELETE);
  311. }
  312. inline int dbSvcTable::editing() {
  313. return _call(EDITING, 0);
  314. }
  315. inline void dbSvcTable::setFieldByName(const char *col, dbSvcDatachunk *data) {
  316. _voidcall(SETFIELDBYNAME, col, data);
  317. }
  318. inline void dbSvcTable::setFieldById(int colid, dbSvcDatachunk *data) {
  319. _voidcall(SETFIELDBYID, colid, data);
  320. }
  321. inline void dbSvcTable::deleteFieldByName(const char *col) {
  322. _voidcall(DELETEFIELDBYNAME, col);
  323. }
  324. inline void dbSvcTable::deleteFieldById(int colid) {
  325. _voidcall(DELETEFIELDBYID, colid);
  326. }
  327. inline void dbSvcTable::addColumn(const char *colname, int colid, int datatype, int index_uniques) {
  328. _voidcall(ADDCOLUMN, colname, colid, datatype, index_uniques);
  329. }
  330. inline void dbSvcTable::addIndexByName(const char *col) {
  331. _voidcall(ADDINDEXBYNAME, col);
  332. }
  333. inline void dbSvcTable::addIndexById(int colid) {
  334. _voidcall(ADDINDEXBYID, colid);
  335. }
  336. inline void dbSvcTable::dropIndexByName(const char *col) {
  337. _voidcall(DROPINDEXBYNAME, col);
  338. }
  339. inline void dbSvcTable::dropIndexById(int colid) {
  340. _voidcall(DROPINDEXBYID, colid);
  341. }
  342. inline void dbSvcTable::sync() {
  343. _voidcall(SYNC);
  344. }
  345. class NOVTABLE dbSvcTableI : public dbSvcTable {
  346. public:
  347. virtual dbSvcScanner *getScanner()=0;
  348. virtual dbSvcScanner *newScanner()=0;
  349. virtual void deleteScanner(dbSvcScanner *scanner)=0;
  350. virtual void _new()=0;
  351. virtual void insert()=0;
  352. virtual void cancel()=0;
  353. virtual void edit()=0;
  354. virtual void post()=0;
  355. virtual void _delete()=0;
  356. virtual int editing()=0;
  357. virtual void setFieldByName(const char *col, dbSvcDatachunk *data)=0;
  358. virtual void setFieldById(int colid, dbSvcDatachunk *data)=0;
  359. virtual void deleteFieldByName(const char *col)=0;
  360. virtual void deleteFieldById(int colid)=0;
  361. virtual void addColumn(const char *colname, int colid, int datatype, int index_uniques)=0;
  362. virtual void addIndexByName(const char *col)=0;
  363. virtual void addIndexById(int colid)=0;
  364. virtual void dropIndexByName(const char *col)=0;
  365. virtual void dropIndexById(int colid)=0;
  366. virtual void sync()=0;
  367. protected:
  368. RECVS_DISPATCH;
  369. };
  370. class NOVTABLE svc_db : public Dispatchable {
  371. protected:
  372. svc_db() {}
  373. ~svc_db() {}
  374. public:
  375. static FOURCC getServiceType() { return WaSvc::DB; }
  376. int testQueryFormat(int queryformat);
  377. dbSvcTable *openTable(const char *tablefilename, int create_if_not_exist, int cache_in_memory);
  378. void closeTable(dbSvcTable *table);
  379. enum {
  380. TESTQUERYFORMAT = 10,
  381. OPENTABLE = 20,
  382. CLOSETABLE = 30,
  383. };
  384. };
  385. inline int svc_db::testQueryFormat(int queryformat) {
  386. return _call(TESTQUERYFORMAT, 0, queryformat);
  387. }
  388. inline dbSvcTable *svc_db::openTable(const char *tablename, int create_if_not_exist, int cache_in_memory) {
  389. return _call(OPENTABLE, static_cast<dbSvcTable *>(NULL), tablename, create_if_not_exist, cache_in_memory);
  390. }
  391. inline void svc_db::closeTable(dbSvcTable *table) {
  392. _voidcall(CLOSETABLE, table);
  393. }
  394. // derive from this one
  395. class NOVTABLE svc_dbI : public svc_db{
  396. public:
  397. virtual int testQueryFormat(int queryformat)=0;
  398. virtual dbSvcTable *openTable(const char *filename, int create_if_not_exist, int cache_in_memory)=0;
  399. virtual void closeTable(dbSvcTable *table)=0;
  400. protected:
  401. RECVS_DISPATCH;
  402. };
  403. #include <api/service/svc_enum.h>
  404. enum {
  405. DBQFORMAT_MINIQUERY =1,
  406. DBQFORMAT_SQL =2,
  407. };
  408. class DatabaseEnum : public SvcEnumT<svc_db> {
  409. public:
  410. DatabaseEnum(int queryformat=DBQFORMAT_MINIQUERY) :
  411. query_format(queryformat) {}
  412. protected:
  413. virtual int testService(svc_db *svc) {
  414. return svc->testQueryFormat(query_format);
  415. }
  416. private:
  417. int query_format;
  418. };
  419. #endif