sqlite3.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2013 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "platform.h"
  14. #include "sqlite3.h"
  15. #include "jexcept.hpp"
  16. #include "jthread.hpp"
  17. #include "hqlplugins.hpp"
  18. #include "deftype.hpp"
  19. #include "eclhelper.hpp"
  20. #include "eclrtl.hpp"
  21. #include "eclrtl_imp.hpp"
  22. #include "rtlds_imp.hpp"
  23. #include "rtlfield_imp.hpp"
  24. #include "nbcd.hpp"
  25. #ifdef _WIN32
  26. #define EXPORT __declspec(dllexport)
  27. #else
  28. #define EXPORT
  29. #endif
  30. #define UNSUPPORTED throw MakeStringException(-1, "UNSUPPORTED feature at %s(%d)", __FILE__, __LINE__)
  31. static const char * compatibleVersions[] = {
  32. "SqLite3 Embed Helper 1.0.0",
  33. NULL };
  34. static const char *version = "SqLite3 Embed Helper 1.0.0";
  35. extern "C" EXPORT bool getECLPluginDefinition(ECLPluginDefinitionBlock *pb)
  36. {
  37. if (pb->size == sizeof(ECLPluginDefinitionBlockEx))
  38. {
  39. ECLPluginDefinitionBlockEx * pbx = (ECLPluginDefinitionBlockEx *) pb;
  40. pbx->compatibleVersions = compatibleVersions;
  41. }
  42. else if (pb->size != sizeof(ECLPluginDefinitionBlock))
  43. return false;
  44. pb->magicVersion = PLUGIN_VERSION;
  45. pb->version = version;
  46. pb->moduleName = "sqlite3";
  47. pb->ECL = NULL;
  48. pb->flags = PLUGIN_MULTIPLE_VERSIONS;
  49. pb->description = "SqLite3 Embed Helper";
  50. return true;
  51. }
  52. namespace sqlite3embed {
  53. // Use class OwnedStatement for a sqlite3 stmt object that needs to be released cleanly
  54. class OwnedStatement
  55. {
  56. sqlite3_stmt *ptr;
  57. public:
  58. inline OwnedStatement() : ptr(NULL) {}
  59. inline OwnedStatement(sqlite3_stmt *_ptr) : ptr(_ptr) {}
  60. inline ~OwnedStatement() { clear(); }
  61. inline sqlite3_stmt * get() const { return ptr; }
  62. inline sqlite3_stmt * getClear() { sqlite3_stmt *ret = ptr; ptr = NULL; return ret; }
  63. inline sqlite3_stmt * operator -> () const { return ptr; }
  64. inline operator sqlite3_stmt *() const { return ptr; }
  65. inline void clear() { sqlite3_finalize(ptr); ptr = NULL; }
  66. inline void setown(sqlite3_stmt *_ptr) { clear(); ptr = _ptr; }
  67. inline sqlite3_stmt **ref() { return &ptr; }
  68. };
  69. // Conversions from SqLite3 values to ECL data
  70. static void checkSqliteError(int rc)
  71. {
  72. if (rc != SQLITE_OK)
  73. rtlFail(rc, "SqLite3 error");
  74. }
  75. static void typeError(const char *expected, const RtlFieldInfo *field) __attribute__((noreturn));
  76. static void typeError(const char *expected, const RtlFieldInfo *field)
  77. {
  78. VStringBuffer msg("sqlite3: type mismatch - %s expected", expected);
  79. if (field)
  80. msg.appendf(" for field %s", field->name->str());
  81. rtlFail(0, msg.str());
  82. }
  83. static bool getBooleanResult(const RtlFieldInfo *field, sqlite3_value *val)
  84. {
  85. assertex(val);
  86. if (sqlite3_value_type(val) != SQLITE_INTEGER)
  87. typeError("boolean", field);
  88. return sqlite3_value_int64(val) != 0;
  89. }
  90. static void getDataResult(const RtlFieldInfo *field, sqlite3_value *val, size32_t &chars, void * &result)
  91. {
  92. assertex(val);
  93. if (sqlite3_value_type(val) != SQLITE_BLOB)
  94. typeError("blob", field);
  95. const void *blob = sqlite3_value_blob(val);
  96. int bytes = sqlite3_value_bytes(val);
  97. rtlStrToDataX(chars, result, bytes, blob);
  98. }
  99. static double getRealResult(const RtlFieldInfo *field, sqlite3_value *val)
  100. {
  101. assertex(val);
  102. if (sqlite3_value_type(val) != SQLITE_FLOAT)
  103. typeError("real", field);
  104. return sqlite3_value_double(val);
  105. }
  106. static __int64 getSignedResult(const RtlFieldInfo *field, sqlite3_value *val)
  107. {
  108. assertex(val);
  109. if (sqlite3_value_type(val) != SQLITE_INTEGER)
  110. typeError("integer", field);
  111. return sqlite3_value_int64(val);
  112. }
  113. static unsigned __int64 getUnsignedResult(const RtlFieldInfo *field, sqlite3_value *val)
  114. {
  115. assertex(val);
  116. if (sqlite3_value_type(val) != SQLITE_INTEGER)
  117. typeError("integer", field);
  118. return (unsigned __int64) sqlite3_value_int64(val);
  119. }
  120. static void getStringResult(const RtlFieldInfo *field, sqlite3_value *val, size32_t &chars, char * &result)
  121. {
  122. assertex(val);
  123. if (sqlite3_value_type(val) != SQLITE_TEXT)
  124. typeError("string", field);
  125. const char *text = (const char *) sqlite3_value_text(val);
  126. int bytes = sqlite3_value_bytes(val);
  127. unsigned numchars = rtlUtf8Length(bytes, text);
  128. rtlUtf8ToStrX(chars, result, numchars, text);
  129. }
  130. static void getUTF8Result(const RtlFieldInfo *field, sqlite3_value *val, size32_t &chars, char * &result)
  131. {
  132. assertex(val);
  133. if (sqlite3_value_type(val) != SQLITE_TEXT)
  134. typeError("string", field);
  135. const char *text = (const char *) sqlite3_value_text(val);
  136. int bytes = sqlite3_value_bytes(val);
  137. unsigned numchars = rtlUtf8Length(bytes, text);
  138. rtlUtf8ToUtf8X(chars, result, numchars, text);
  139. }
  140. static void getUnicodeResult(const RtlFieldInfo *field, sqlite3_value *val, size32_t &chars, UChar * &result)
  141. {
  142. assertex(val);
  143. if (sqlite3_value_type(val) != SQLITE_TEXT)
  144. typeError("string", field);
  145. const UChar *text = (const UChar *) sqlite3_value_text16(val);
  146. int bytes = sqlite3_value_bytes(val);
  147. unsigned numchars = bytes / sizeof(UChar);
  148. rtlUnicodeToUnicodeX(chars, result, numchars, text);
  149. }
  150. // A SqLite3RowBuilder object is used to construct an ECL row from a sqlite result
  151. class SqLite3RowBuilder : public CInterfaceOf<IFieldSource>
  152. {
  153. public:
  154. SqLite3RowBuilder(sqlite3_stmt *_stmt)
  155. : stmt(_stmt), val(NULL), colIdx(0)
  156. {
  157. }
  158. virtual bool getBooleanResult(const RtlFieldInfo *field)
  159. {
  160. nextField(field);
  161. return sqlite3embed::getBooleanResult(field, val);
  162. }
  163. virtual void getDataResult(const RtlFieldInfo *field, size32_t &len, void * &result)
  164. {
  165. nextField(field);
  166. sqlite3embed::getDataResult(field, val, len, result);
  167. }
  168. virtual double getRealResult(const RtlFieldInfo *field)
  169. {
  170. nextField(field);
  171. return sqlite3embed::getRealResult(field, val);
  172. }
  173. virtual __int64 getSignedResult(const RtlFieldInfo *field)
  174. {
  175. nextField(field);
  176. return sqlite3embed::getSignedResult(field, val);
  177. }
  178. virtual unsigned __int64 getUnsignedResult(const RtlFieldInfo *field)
  179. {
  180. nextField(field);
  181. return sqlite3embed::getUnsignedResult(field, val);
  182. }
  183. virtual void getStringResult(const RtlFieldInfo *field, size32_t &chars, char * &result)
  184. {
  185. nextField(field);
  186. sqlite3embed::getStringResult(field, val, chars, result);
  187. }
  188. virtual void getUTF8Result(const RtlFieldInfo *field, size32_t &chars, char * &result)
  189. {
  190. nextField(field);
  191. sqlite3embed::getUTF8Result(field, val, chars, result);
  192. }
  193. virtual void getUnicodeResult(const RtlFieldInfo *field, size32_t &chars, UChar * &result)
  194. {
  195. nextField(field);
  196. sqlite3embed::getUnicodeResult(field, val, chars, result);
  197. }
  198. virtual void getDecimalResult(const RtlFieldInfo *field, Decimal &value)
  199. {
  200. nextField(field);
  201. double ret = sqlite3embed::getRealResult(field, val);
  202. value.setReal(ret);
  203. }
  204. virtual void processBeginSet(const RtlFieldInfo * field, bool &isAll)
  205. {
  206. UNSUPPORTED;
  207. }
  208. virtual bool processNextSet(const RtlFieldInfo * field)
  209. {
  210. throwUnexpected();
  211. }
  212. virtual void processBeginDataset(const RtlFieldInfo * field)
  213. {
  214. UNSUPPORTED;
  215. }
  216. virtual void processBeginRow(const RtlFieldInfo * field)
  217. {
  218. }
  219. virtual bool processNextRow(const RtlFieldInfo * field)
  220. {
  221. UNSUPPORTED;
  222. }
  223. virtual void processEndSet(const RtlFieldInfo * field)
  224. {
  225. throwUnexpected();
  226. }
  227. virtual void processEndDataset(const RtlFieldInfo * field)
  228. {
  229. throwUnexpected();
  230. }
  231. virtual void processEndRow(const RtlFieldInfo * field)
  232. {
  233. }
  234. protected:
  235. void nextField(const RtlFieldInfo * field)
  236. {
  237. val = sqlite3_column_value(stmt, colIdx++);
  238. }
  239. sqlite3_value *val;
  240. sqlite3_stmt *stmt;
  241. unsigned colIdx;
  242. };
  243. // A SqLite3 function that returns a dataset will return a SqLite3RowStream object that can be
  244. // interrogated to return each row of the result in turn
  245. class SqLite3RowStream : public CInterfaceOf<IRowStream>
  246. {
  247. public:
  248. SqLite3RowStream(sqlite3_stmt *_stmt, IEngineRowAllocator *_resultAllocator)
  249. : stmt(_stmt), resultAllocator(_resultAllocator)
  250. {
  251. assertex(stmt);
  252. }
  253. virtual const void *nextRow()
  254. {
  255. if (!stmt)
  256. return NULL;
  257. int rc = sqlite3_step(stmt);
  258. if (rc != SQLITE_ROW)
  259. {
  260. stmt.clear();
  261. return NULL;
  262. }
  263. RtlDynamicRowBuilder rowBuilder(resultAllocator);
  264. SqLite3RowBuilder sqliteRowBuilder(stmt);
  265. const RtlTypeInfo *typeInfo = resultAllocator->queryOutputMeta()->queryTypeInfo();
  266. assertex(typeInfo);
  267. RtlFieldStrInfo dummyField("<row>", NULL, typeInfo);
  268. size32_t len = typeInfo->build(rowBuilder, 0, &dummyField, sqliteRowBuilder);
  269. return rowBuilder.finalizeRowClear(len);
  270. }
  271. virtual void stop()
  272. {
  273. resultAllocator.clear();
  274. stmt.clear();
  275. }
  276. protected:
  277. Linked<IEngineRowAllocator> resultAllocator;
  278. OwnedStatement stmt;
  279. };
  280. // Each call to a SqLite3 function will use a new SqLite3EmbedFunctionContext object
  281. class SqLite3EmbedFunctionContext : public CInterfaceOf<IEmbedFunctionContext>
  282. {
  283. public:
  284. SqLite3EmbedFunctionContext(const char *options) : db(NULL)
  285. {
  286. const char *dbname = NULL;
  287. StringArray opts;
  288. opts.appendList(options, ",");
  289. ForEachItemIn(idx, opts)
  290. {
  291. const char *opt = opts.item(idx);
  292. const char *val = strchr(opt, '=');
  293. if (val)
  294. {
  295. StringBuffer optName(val-opt, opt);
  296. val++;
  297. if (stricmp(optName, "file")==0)
  298. dbname = val; // Note that lifetime of val is adequate for this to be safe
  299. }
  300. }
  301. if (!dbname)
  302. rtlFail(0, "sqlite3: no filename specified");
  303. int rc = sqlite3_open(dbname, &db);
  304. checkSqliteError(rc);
  305. }
  306. ~SqLite3EmbedFunctionContext()
  307. {
  308. if (db)
  309. sqlite3_close(db);
  310. }
  311. virtual bool getBooleanResult()
  312. {
  313. return sqlite3embed::getBooleanResult(NULL, getScalarResult());
  314. }
  315. virtual void getDataResult(size32_t &len, void * &result)
  316. {
  317. sqlite3embed::getDataResult(NULL, getScalarResult(), len, result);
  318. }
  319. virtual double getRealResult()
  320. {
  321. return sqlite3embed::getRealResult(NULL, getScalarResult());
  322. }
  323. virtual __int64 getSignedResult()
  324. {
  325. return sqlite3embed::getSignedResult(NULL, getScalarResult());
  326. }
  327. virtual unsigned __int64 getUnsignedResult()
  328. {
  329. return sqlite3embed::getUnsignedResult(NULL, getScalarResult());
  330. }
  331. virtual void getStringResult(size32_t &chars, char * &result)
  332. {
  333. sqlite3embed::getStringResult(NULL, getScalarResult(), chars, result);
  334. }
  335. virtual void getUTF8Result(size32_t &chars, char * &result)
  336. {
  337. sqlite3embed::getUTF8Result(NULL, getScalarResult(), chars, result);
  338. }
  339. virtual void getUnicodeResult(size32_t &chars, UChar * &result)
  340. {
  341. sqlite3embed::getUnicodeResult(NULL, getScalarResult(), chars, result);
  342. }
  343. virtual void getDecimalResult(Decimal &value)
  344. {
  345. double ret = sqlite3embed::getRealResult(NULL, getScalarResult());
  346. value.setReal(ret);
  347. }
  348. virtual void getSetResult(bool & __isAllResult, size32_t & __resultBytes, void * & __result, int elemType, size32_t elemSize)
  349. {
  350. UNSUPPORTED;
  351. }
  352. virtual IRowStream *getDatasetResult(IEngineRowAllocator * _resultAllocator)
  353. {
  354. return new SqLite3RowStream(stmt.getClear(), _resultAllocator);
  355. }
  356. virtual byte * getRowResult(IEngineRowAllocator * _resultAllocator)
  357. {
  358. RtlDynamicRowBuilder rowBuilder(_resultAllocator);
  359. SqLite3RowBuilder sqliteRowBuilder(stmt);
  360. const RtlTypeInfo *typeInfo = _resultAllocator->queryOutputMeta()->queryTypeInfo();
  361. assertex(typeInfo);
  362. RtlFieldStrInfo dummyField("<row>", NULL, typeInfo);
  363. size32_t len = typeInfo->build(rowBuilder, 0, &dummyField, sqliteRowBuilder);
  364. return (byte *) rowBuilder.finalizeRowClear(len);
  365. }
  366. virtual size32_t getTransformResult(ARowBuilder & rowBuilder)
  367. {
  368. SqLite3RowBuilder sqliteRowBuilder(stmt);
  369. const RtlTypeInfo *typeInfo = rowBuilder.queryAllocator()->queryOutputMeta()->queryTypeInfo();
  370. assertex(typeInfo);
  371. RtlFieldStrInfo dummyField("<row>", NULL, typeInfo);
  372. return typeInfo->build(rowBuilder, 0, &dummyField, sqliteRowBuilder);
  373. }
  374. virtual void bindRowParam(const char *name, IOutputMetaData & metaVal, byte *val)
  375. {
  376. UNSUPPORTED; // Not sure it ever makes sense
  377. }
  378. virtual void bindDatasetParam(const char *name, IOutputMetaData & metaVal, IRowStream * val)
  379. {
  380. UNSUPPORTED; // Would need some thought, if it ever made sense
  381. }
  382. virtual void bindBooleanParam(const char *name, bool val)
  383. {
  384. checkSqliteError(sqlite3_bind_int64(stmt, findParameter(name), val ? 1 : 0));
  385. }
  386. virtual void bindDataParam(const char *name, size32_t len, const void *val)
  387. {
  388. checkSqliteError(sqlite3_bind_blob(stmt, findParameter(name), val, len, SQLITE_TRANSIENT));
  389. }
  390. virtual void bindRealParam(const char *name, double val)
  391. {
  392. checkSqliteError(sqlite3_bind_double(stmt, findParameter(name), val));
  393. }
  394. virtual void bindSignedParam(const char *name, __int64 val)
  395. {
  396. checkSqliteError(sqlite3_bind_int64(stmt, findParameter(name), val));
  397. }
  398. virtual void bindUnsignedParam(const char *name, unsigned __int64 val)
  399. {
  400. checkSqliteError(sqlite3_bind_int64(stmt, findParameter(name), val));
  401. }
  402. virtual void bindStringParam(const char *name, size32_t len, const char *val)
  403. {
  404. size32_t utf8chars;
  405. char *utf8;
  406. rtlStrToUtf8X(utf8chars, utf8, len, val);
  407. checkSqliteError(sqlite3_bind_text(stmt, findParameter(name), utf8, rtlUtf8Size(len, utf8), rtlFree));
  408. }
  409. virtual void bindVStringParam(const char *name, const char *val)
  410. {
  411. bindStringParam(name, strlen(val), val);
  412. }
  413. virtual void bindUTF8Param(const char *name, size32_t chars, const char *val)
  414. {
  415. size32_t sizeBytes = rtlUtf8Size(chars, val);
  416. checkSqliteError(sqlite3_bind_text(stmt, findParameter(name), val, sizeBytes, SQLITE_TRANSIENT)); // NOTE - requires size in bytes not chars
  417. }
  418. virtual void bindUnicodeParam(const char *name, size32_t chars, const UChar *val)
  419. {
  420. size32_t sizeBytes = chars * sizeof(UChar);
  421. checkSqliteError(sqlite3_bind_text16(stmt, findParameter(name), val, sizeBytes, SQLITE_TRANSIENT)); // NOTE - requires size in bytes not chars
  422. }
  423. virtual void bindSetParam(const char *name, int elemType, size32_t elemSize, bool isAll, size32_t totalBytes, void *setData)
  424. {
  425. UNSUPPORTED;
  426. }
  427. virtual void importFunction(size32_t lenChars, const char *text)
  428. {
  429. UNSUPPORTED;
  430. }
  431. virtual void compileEmbeddedScript(size32_t len, const char *script)
  432. {
  433. int rc = sqlite3_prepare_v2(db, script, len, stmt.ref(), NULL);
  434. checkSqliteError(rc);
  435. }
  436. virtual void callFunction()
  437. {
  438. assertex(stmt);
  439. int rc = sqlite3_reset(stmt);
  440. checkSqliteError(rc);
  441. }
  442. protected:
  443. sqlite3_value *getScalarResult()
  444. {
  445. int rc = sqlite3_step(stmt);
  446. if (rc != SQLITE_ROW || sqlite3_column_count(stmt) != 1)
  447. typeError("scalar", NULL);
  448. return sqlite3_column_value(stmt, 0);
  449. }
  450. inline int findParameter(const char *name)
  451. {
  452. VStringBuffer pname(":%s", name);
  453. int idx = sqlite3_bind_parameter_index(stmt, pname);
  454. if (!idx)
  455. {
  456. VStringBuffer msg("sqlite3: bound parameter %s not found", name);
  457. rtlFail(0, msg.str());
  458. }
  459. return idx;
  460. }
  461. OwnedStatement stmt;
  462. sqlite3 *db;
  463. };
  464. class SqLite3EmbedContext : public CInterfaceOf<IEmbedContext>
  465. {
  466. public:
  467. virtual IEmbedFunctionContext *createFunctionContext(bool isImport, const char *options)
  468. {
  469. if (isImport)
  470. UNSUPPORTED;
  471. else
  472. return new SqLite3EmbedFunctionContext(options);
  473. }
  474. };
  475. extern IEmbedContext* getEmbedContext()
  476. {
  477. return new SqLite3EmbedContext();
  478. }
  479. extern bool syntaxCheck(const char *script)
  480. {
  481. return true; // MORE
  482. }
  483. } // namespace