4 Copyright (C) Derrell Lipman 2005
5 Copyright (C) Simo Sorce 2005
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Component: ldb sqlite3 backend
31 * Description: core files for SQLITE3 backend
33 * Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
37 #include "ldb/include/includes.h"
41 struct lsqlite3_private {
47 struct lsql_async_context {
48 struct ldb_module *module;
51 long long current_eid;
52 const char * const * attrs;
53 struct ldb_async_result *ares;
57 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *);
60 static struct ldb_async_handle *init_lsql_handle(struct lsqlite3_private *lsqlite3, struct ldb_module *module,
62 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *))
64 struct lsql_async_context *ac;
65 struct ldb_async_handle *h;
67 h = talloc_zero(lsqlite3, struct ldb_async_handle);
69 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
73 ac = talloc(h, struct lsql_async_context);
75 ldb_set_errstring(module->ldb, talloc_asprintf(module, "Out of Memory"));
80 h->private_data = (void *)ac;
83 ac->context = context;
84 ac->callback = callback;
90 * Macros used throughout
95 # define TRUE (! FALSE)
98 #define RESULT_ATTR_TABLE "temp_result_attrs"
100 //#define TEMPTAB /* for testing, create non-temporary table */
101 #define TEMPTAB "TEMPORARY"
106 sqlite3_stmt * stmtGetEID = NULL;
108 static char *lsqlite3_tprintf(TALLOC_CTX *mem_ctx, const char *fmt, ...)
114 str = sqlite3_vmprintf(fmt, ap);
117 if (str == NULL) return NULL;
119 ret = talloc_strdup(mem_ctx, str);
129 static char base160tab[161] = {
130 48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
131 58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
132 73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
133 83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
134 99 ,100,101,102,103,104,105,106,107,108, /* c-l */
135 109,110,111,112,113,114,115,116,117,118, /* m-v */
136 119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
137 166,167,168,169,170,171,172,173,174,175, /* latin1 */
138 176,177,178,179,180,181,182,183,184,185, /* latin1 */
139 186,187,188,189,190,191,192,193,194,195, /* latin1 */
140 196,197,198,199,200,201,202,203,204,205, /* latin1 */
141 206,207,208,209,210,211,212,213,214,215, /* latin1 */
142 216,217,218,219,220,221,222,223,224,225, /* latin1 */
143 226,227,228,229,230,231,232,233,234,235, /* latin1 */
144 236,237,238,239,240,241,242,243,244,245, /* latin1 */
145 246,247,248,249,250,251,252,253,254,255, /* latin1 */
153 * Convert an unsigned long integer into a base160 representation of the
158 * value to be converted
161 * character array, 5 bytes long, into which the base160 representation
162 * will be placed. The result will be a four-digit representation of the
163 * number (with leading zeros prepended as necessary), and null
170 base160_sql(sqlite3_context * hContext,
172 sqlite3_value ** argv)
178 val = sqlite3_value_int64(argv[0]);
180 for (i = 3; i >= 0; i--) {
182 result[i] = base160tab[val % 160];
188 sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
195 * This function enhances sqlite by adding a "base160_next()" function which is
196 * accessible via queries.
198 * Retrieve the next-greater number in the base160 sequence for the terminal
199 * tree node (the last four digits). Only one tree level (four digits) is
203 * A character string: either an empty string (in which case no operation is
204 * performed), or a string of base160 digits with a length of a multiple of
208 * Upon return, the trailing four digits (one tree level) will have been
212 base160next_sql(sqlite3_context * hContext,
214 sqlite3_value ** argv)
219 char * pBase160 = strdup((const char *)sqlite3_value_text(argv[0]));
220 char * pStart = pBase160;
223 * We need a minimum of four digits, and we will always get a multiple
226 if (pBase160 != NULL &&
227 (len = strlen(pBase160)) >= 4 &&
230 if (pBase160 == NULL) {
232 sqlite3_result_null(hContext);
236 pBase160 += strlen(pBase160) - 1;
238 /* We only carry through four digits: one level in the tree */
239 for (i = 0; i < 4; i++) {
241 /* What base160 value does this digit have? */
242 pTab = strchr(base160tab, *pBase160);
244 /* Is there a carry? */
245 if (pTab < base160tab + sizeof(base160tab) - 1) {
248 * Nope. Just increment this value and we're
256 * There's a carry. This value gets
257 * base160tab[0], we decrement the buffer
258 * pointer to get the next higher-order digit,
259 * and continue in the loop.
261 *pBase160-- = base160tab[0];
265 sqlite3_result_text(hContext,
270 sqlite3_result_value(hContext, argv[0]);
271 if (pBase160 != NULL) {
277 static char *parsetree_to_sql(struct ldb_module *module,
279 const struct ldb_parse_tree *t)
281 const struct ldb_attrib_handler *h;
282 struct ldb_val value, subval;
283 char *wild_card_string;
290 switch(t->operation) {
293 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
294 if (tmp == NULL) return NULL;
296 for (i = 1; i < t->u.list.num_elements; i++) {
298 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
299 if (child == NULL) return NULL;
301 tmp = talloc_asprintf_append(tmp, " INTERSECT %s ", child);
302 if (tmp == NULL) return NULL;
305 ret = talloc_asprintf(mem_ctx, "SELECT * FROM ( %s )\n", tmp);
311 tmp = parsetree_to_sql(module, mem_ctx, t->u.list.elements[0]);
312 if (tmp == NULL) return NULL;
314 for (i = 1; i < t->u.list.num_elements; i++) {
316 child = parsetree_to_sql(module, mem_ctx, t->u.list.elements[i]);
317 if (child == NULL) return NULL;
319 tmp = talloc_asprintf_append(tmp, " UNION %s ", child);
320 if (tmp == NULL) return NULL;
323 return talloc_asprintf(mem_ctx, "SELECT * FROM ( %s ) ", tmp);
327 child = parsetree_to_sql(module, mem_ctx, t->u.isnot.child);
328 if (child == NULL) return NULL;
330 return talloc_asprintf(mem_ctx,
331 "SELECT eid FROM ldb_entry "
332 "WHERE eid NOT IN ( %s ) ", child);
334 case LDB_OP_EQUALITY:
336 * For simple searches, we want to retrieve the list of EIDs that
337 * match the criteria.
339 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
340 if (attr == NULL) return NULL;
341 h = ldb_attrib_handler(module->ldb, attr);
343 /* Get a canonicalised copy of the data */
344 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
345 if (value.data == NULL) {
349 if (strcasecmp(t->u.equality.attr, "objectclass") == 0) {
351 * For object classes, we want to search for all objectclasses
352 * that are subclasses as well.
354 return lsqlite3_tprintf(mem_ctx,
355 "SELECT eid FROM ldb_attribute_values\n"
356 "WHERE norm_attr_name = 'OBJECTCLASS' "
357 "AND norm_attr_value IN\n"
358 " (SELECT class_name FROM ldb_object_classes\n"
359 " WHERE tree_key GLOB\n"
360 " (SELECT tree_key FROM ldb_object_classes\n"
361 " WHERE class_name = '%q'\n"
365 } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
366 /* DN query is a special ldb case */
367 char *cdn = ldb_dn_linearize_casefold(module->ldb,
368 ldb_dn_explode(module->ldb,
369 (const char *)value.data));
371 return lsqlite3_tprintf(mem_ctx,
372 "SELECT eid FROM ldb_entry "
373 "WHERE norm_dn = '%q'", cdn);
376 /* A normal query. */
377 return lsqlite3_tprintf(mem_ctx,
378 "SELECT eid FROM ldb_attribute_values "
379 "WHERE norm_attr_name = '%q' "
380 "AND norm_attr_value = '%q'",
386 case LDB_OP_SUBSTRING:
388 wild_card_string = talloc_strdup(mem_ctx,
389 (t->u.substring.start_with_wildcard)?"*":"");
390 if (wild_card_string == NULL) return NULL;
392 for (i = 0; t->u.substring.chunks[i]; i++) {
393 wild_card_string = talloc_asprintf_append(wild_card_string, "%s*",
394 t->u.substring.chunks[i]->data);
395 if (wild_card_string == NULL) return NULL;
398 if ( ! t->u.substring.end_with_wildcard ) {
399 /* remove last wildcard */
400 wild_card_string[strlen(wild_card_string) - 1] = '\0';
403 attr = ldb_attr_casefold(mem_ctx, t->u.substring.attr);
404 if (attr == NULL) return NULL;
405 h = ldb_attrib_handler(module->ldb, attr);
407 subval.data = (void *)wild_card_string;
408 subval.length = strlen(wild_card_string) + 1;
410 /* Get a canonicalised copy of the data */
411 h->canonicalise_fn(module->ldb, mem_ctx, &(subval), &value);
412 if (value.data == NULL) {
416 return lsqlite3_tprintf(mem_ctx,
417 "SELECT eid FROM ldb_attribute_values "
418 "WHERE norm_attr_name = '%q' "
419 "AND norm_attr_value GLOB '%q'",
424 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
425 if (attr == NULL) return NULL;
426 h = ldb_attrib_handler(module->ldb, attr);
428 /* Get a canonicalised copy of the data */
429 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
430 if (value.data == NULL) {
434 return lsqlite3_tprintf(mem_ctx,
435 "SELECT eid FROM ldb_attribute_values "
436 "WHERE norm_attr_name = '%q' "
437 "AND ldap_compare(norm_attr_value, '>=', '%q', '%q') ",
443 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
444 if (attr == NULL) return NULL;
445 h = ldb_attrib_handler(module->ldb, attr);
447 /* Get a canonicalised copy of the data */
448 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
449 if (value.data == NULL) {
453 return lsqlite3_tprintf(mem_ctx,
454 "SELECT eid FROM ldb_attribute_values "
455 "WHERE norm_attr_name = '%q' "
456 "AND ldap_compare(norm_attr_value, '<=', '%q', '%q') ",
462 if (strcasecmp(t->u.present.attr, "dn") == 0) {
463 return talloc_strdup(mem_ctx, "SELECT eid FROM ldb_entry");
466 attr = ldb_attr_casefold(mem_ctx, t->u.present.attr);
467 if (attr == NULL) return NULL;
469 return lsqlite3_tprintf(mem_ctx,
470 "SELECT eid FROM ldb_attribute_values "
471 "WHERE norm_attr_name = '%q' ",
475 attr = ldb_attr_casefold(mem_ctx, t->u.equality.attr);
476 if (attr == NULL) return NULL;
477 h = ldb_attrib_handler(module->ldb, attr);
479 /* Get a canonicalised copy of the data */
480 h->canonicalise_fn(module->ldb, mem_ctx, &(t->u.equality.value), &value);
481 if (value.data == NULL) {
485 return lsqlite3_tprintf(mem_ctx,
486 "SELECT eid FROM ldb_attribute_values "
487 "WHERE norm_attr_name = '%q' "
488 "AND ldap_compare(norm_attr_value, '~%', 'q', '%q') ",
493 case LDB_OP_EXTENDED:
494 #warning "work out how to handle bitops"
501 /* should never occur */
509 * This function is used for the common case of queries that return a single
512 * NOTE: If more than one value is returned by the query, all but the first
513 * one will be ignored.
516 query_int(const struct lsqlite3_private * lsqlite3,
524 sqlite3_stmt * pStmt;
527 /* Begin access to variable argument list */
528 va_start(args, pSql);
530 /* Format the query */
531 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
536 * Prepare and execute the SQL statement. Loop allows retrying on
537 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
538 * requiring retrying the operation.
540 for (bLoop = TRUE; bLoop; ) {
542 /* Compile the SQL statement into sqlite virtual machine */
543 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
547 NULL)) == SQLITE_SCHEMA) {
548 if (stmtGetEID != NULL) {
549 sqlite3_finalize(stmtGetEID);
553 } else if (ret != SQLITE_OK) {
557 /* One row expected */
558 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
559 if (stmtGetEID != NULL) {
560 sqlite3_finalize(stmtGetEID);
563 (void) sqlite3_finalize(pStmt);
565 } else if (ret != SQLITE_ROW) {
566 (void) sqlite3_finalize(pStmt);
570 /* Get the value to be returned */
571 *pRet = sqlite3_column_int64(pStmt, 0);
573 /* Free the virtual machine */
574 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
575 if (stmtGetEID != NULL) {
576 sqlite3_finalize(stmtGetEID);
580 } else if (ret != SQLITE_OK) {
581 (void) sqlite3_finalize(pStmt);
586 * Normal condition is only one time through loop. Loop is
587 * rerun in error conditions, via "continue", above.
592 /* All done with variable argument list */
596 /* Free the memory we allocated for our query string */
603 * This is a bad hack to support ldap style comparisons whithin sqlite.
604 * val is the attribute in the row currently under test
605 * func is the desired test "<=" ">=" "~" ":"
606 * cmp is the value to compare against (eg: "test")
607 * attr is the attribute name the value of which we want to test
610 static void lsqlite3_compare(sqlite3_context *ctx, int argc,
611 sqlite3_value **argv)
613 struct ldb_context *ldb = (struct ldb_context *)sqlite3_user_data(ctx);
614 const char *val = (const char *)sqlite3_value_text(argv[0]);
615 const char *func = (const char *)sqlite3_value_text(argv[1]);
616 const char *cmp = (const char *)sqlite3_value_text(argv[2]);
617 const char *attr = (const char *)sqlite3_value_text(argv[3]);
618 const struct ldb_attrib_handler *h;
626 h = ldb_attrib_handler(ldb, attr);
627 valX.data = (void *)cmp;
628 valX.length = strlen(cmp);
629 valY.data = (void *)val;
630 valY.length = strlen(val);
631 ret = h->comparison_fn(ldb, ldb, &valY, &valX);
633 sqlite3_result_int(ctx, 1);
635 sqlite3_result_int(ctx, 0);
640 h = ldb_attrib_handler(ldb, attr);
641 valX.data = (void *)cmp;
642 valX.length = strlen(cmp);
643 valY.data = (void *)val;
644 valY.length = strlen(val);
645 ret = h->comparison_fn(ldb, ldb, &valY, &valX);
647 sqlite3_result_int(ctx, 1);
649 sqlite3_result_int(ctx, 0);
655 sqlite3_result_int(ctx, 0);
661 sqlite3_result_int(ctx, 0);
668 sqlite3_result_error(ctx, "Value must start with a special operation char (<>~:)!", -1);
673 /* rename a record */
674 static int lsqlite3_safe_rollback(sqlite3 *sqlite)
680 ret = sqlite3_exec(sqlite, "ROLLBACK;", NULL, NULL, &errmsg);
681 if (ret != SQLITE_OK) {
683 printf("lsqlite3_safe_rollback: Error: %s\n", errmsg);
692 /* return an eid as result */
693 static int lsqlite3_eid_callback(void *result, int col_num, char **cols, char **names)
695 long long *eid = (long long *)result;
697 if (col_num != 1) return SQLITE_ABORT;
698 if (strcasecmp(names[0], "eid") != 0) return SQLITE_ABORT;
700 *eid = atoll(cols[0]);
705 * add a single set of ldap message values to a ldb_message
707 static int lsqlite3_search_callback(void *result, int col_num, char **cols, char **names)
709 struct ldb_async_handle *handle = talloc_get_type(result, struct ldb_async_handle);
710 struct lsql_async_context *ac = talloc_get_type(handle->private_data, struct lsql_async_context);
711 struct ldb_message *msg;
715 /* eid, dn, attr_name, attr_value */
719 eid = atoll(cols[0]);
721 if (eid != ac->current_eid) { /* here begin a new entry */
723 /* call the async callback for the last entry
724 * except the first time */
725 if (ac->current_eid != 0) {
726 ac->ares->message = ldb_msg_canonicalize(ac->module->ldb, ac->ares->message);
727 if (ac->ares->message == NULL)
730 handle->status = ac->callback(ac->module->ldb, ac->context, ac->ares);
731 if (handle->status != LDB_SUCCESS)
736 ac->ares = talloc_zero(ac, struct ldb_async_result);
740 ac->ares->message = ldb_msg_new(ac->ares);
741 if (!ac->ares->message)
744 ac->ares->type = LDB_REPLY_ENTRY;
745 ac->current_eid = eid;
748 msg = ac->ares->message;
750 if (msg->dn == NULL) {
751 msg->dn = ldb_dn_explode(msg, cols[1]);
758 for (i = 0; ac->attrs[i]; i++) {
759 if (strcasecmp(cols[2], ac->attrs[i]) == 0) {
764 if (!found) return SQLITE_OK;
767 if (ldb_msg_add_string(msg, cols[2], cols[3]) != 0) {
777 * lsqlite3_get_eid_ndn()
779 * These functions are used for the very common case of retrieving an EID value
780 * given a (normalized) DN.
783 static long long lsqlite3_get_eid_ndn(sqlite3 *sqlite, void *mem_ctx, const char *norm_dn)
791 query = lsqlite3_tprintf(mem_ctx, "SELECT eid "
793 "WHERE norm_dn = '%q';", norm_dn);
794 if (query == NULL) return -1;
796 ret = sqlite3_exec(sqlite, query, lsqlite3_eid_callback, &eid, &errmsg);
797 if (ret != SQLITE_OK) {
799 printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg);
808 static long long lsqlite3_get_eid(struct ldb_module *module, const struct ldb_dn *dn)
810 TALLOC_CTX *local_ctx;
811 struct lsqlite3_private *lsqlite3 = module->private_data;
815 /* ignore ltdb specials */
816 if (ldb_dn_is_special(dn)) {
820 /* create a local ctx */
821 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_get_eid local context");
822 if (local_ctx == NULL) {
826 cdn = ldb_dn_linearize(local_ctx, ldb_dn_casefold(module->ldb, dn));
829 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, local_ctx, cdn);
832 talloc_free(local_ctx);
837 * Interface functions referenced by lsqlite3_ops
840 static int lsql_search_sync_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
842 struct ldb_result *res = NULL;
845 ldb_set_errstring(ldb, talloc_strdup(ldb, "NULL Context in callback"));
849 res = *((struct ldb_result **)context);
855 if (ares->type == LDB_REPLY_ENTRY) {
856 res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
861 res->msgs[res->count + 1] = NULL;
863 res->msgs[res->count] = talloc_steal(res->msgs, ares->message);
864 if (! res->msgs[res->count]) {
870 ldb_debug(ldb, LDB_DEBUG_ERROR, "unrecognized async reply in ltdb_search_sync_callback!\n");
878 if (ares) talloc_free(ares);
879 if (res) talloc_free(res);
880 if (context) *((struct ldb_result **)context) = NULL;
881 return LDB_ERR_OPERATIONS_ERROR;
884 /* search for matching records, by tree */
885 int lsql_search_async(struct ldb_module *module, const struct ldb_dn *base,
886 enum ldb_scope scope, struct ldb_parse_tree *tree,
887 const char * const *attrs,
889 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
890 struct ldb_async_handle **handle)
892 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
893 struct lsql_async_context *lsql_ac;
900 *handle = init_lsql_handle(lsqlite3, module, context, callback);
901 if (*handle == NULL) {
902 talloc_free(*handle);
903 return LDB_ERR_OPERATIONS_ERROR;
906 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
909 norm_basedn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, base));
910 if (norm_basedn == NULL) {
911 ret = LDB_ERR_INVALID_DN_SYNTAX;
914 } else norm_basedn = talloc_strdup(lsql_ac, "");
916 if (*norm_basedn == '\0' &&
917 (scope == LDB_SCOPE_BASE || scope == LDB_SCOPE_ONELEVEL)) {
918 ret = LDB_ERR_UNWILLING_TO_PERFORM;
922 /* Convert filter into a series of SQL conditions (constraints) */
923 sqlfilter = parsetree_to_sql(module, lsql_ac, tree);
926 case LDB_SCOPE_DEFAULT:
927 case LDB_SCOPE_SUBTREE:
928 if (*norm_basedn != '\0') {
929 query = lsqlite3_tprintf(lsql_ac,
930 "SELECT entry.eid,\n"
934 " FROM ldb_entry AS entry\n"
936 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
937 " ON av.eid = entry.eid\n"
939 " WHERE entry.eid IN\n"
940 " (SELECT DISTINCT ldb_entry.eid\n"
942 " WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
943 " OR ldb_entry.norm_dn = '%q')\n"
944 " AND ldb_entry.eid IN\n"
948 " ORDER BY entry.eid ASC;",
953 query = lsqlite3_tprintf(lsql_ac,
954 "SELECT entry.eid,\n"
958 " FROM ldb_entry AS entry\n"
960 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
961 " ON av.eid = entry.eid\n"
963 " WHERE entry.eid IN\n"
964 " (SELECT DISTINCT ldb_entry.eid\n"
966 " WHERE ldb_entry.eid IN\n"
970 " ORDER BY entry.eid ASC;",
977 query = lsqlite3_tprintf(lsql_ac,
978 "SELECT entry.eid,\n"
982 " FROM ldb_entry AS entry\n"
984 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
985 " ON av.eid = entry.eid\n"
987 " WHERE entry.eid IN\n"
988 " (SELECT DISTINCT ldb_entry.eid\n"
990 " WHERE ldb_entry.norm_dn = '%q'\n"
991 " AND ldb_entry.eid IN\n"
995 " ORDER BY entry.eid ASC;",
1000 case LDB_SCOPE_ONELEVEL:
1001 query = lsqlite3_tprintf(lsql_ac,
1002 "SELECT entry.eid,\n"
1006 " FROM ldb_entry AS entry\n"
1008 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
1009 " ON av.eid = entry.eid\n"
1011 " WHERE entry.eid IN\n"
1012 " (SELECT DISTINCT ldb_entry.eid\n"
1014 " WHERE norm_dn GLOB('*,%q')\n"
1015 " AND NOT norm_dn GLOB('*,*,%q')\n"
1016 " AND ldb_entry.eid IN\n(%s)\n"
1019 " ORDER BY entry.eid ASC;",
1026 if (query == NULL) {
1031 printf ("%s\n", query);
1034 lsql_ac->current_eid = 0;
1035 lsql_ac->attrs = attrs;
1036 lsql_ac->ares = NULL;
1038 (*handle)->state = LDB_ASYNC_PENDING;
1040 ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, *handle, &errmsg);
1041 if (ret != SQLITE_OK) {
1043 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1049 /* complete the last message if any */
1050 if (lsql_ac->ares) {
1051 lsql_ac->ares->message = ldb_msg_canonicalize(module->ldb, lsql_ac->ares->message);
1052 if (lsql_ac->ares->message == NULL)
1055 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares);
1056 if ((*handle)->status != LDB_SUCCESS)
1060 (*handle)->state = LDB_ASYNC_DONE;
1065 talloc_free(*handle);
1066 return LDB_ERR_OPERATIONS_ERROR;
1069 static int lsql_search_bytree(struct ldb_module * module, const struct ldb_dn* base,
1070 enum ldb_scope scope, struct ldb_parse_tree * tree,
1071 const char * const * attrs, struct ldb_result ** res)
1073 struct ldb_async_handle *handle;
1076 *res = talloc_zero(module, struct ldb_result);
1078 return LDB_ERR_OPERATIONS_ERROR;
1081 ret = lsql_search_async(module, base, scope, tree, attrs,
1082 res, &lsql_search_sync_callback,
1085 if (ret == LDB_SUCCESS) {
1086 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
1087 talloc_free(handle);
1090 if (ret != LDB_SUCCESS) {
1098 static int lsql_add_async(struct ldb_module *module, struct ldb_message *msg,
1100 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1101 struct ldb_async_handle **handle)
1103 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1104 struct lsql_async_context *lsql_ac;
1110 int ret = LDB_ERR_OPERATIONS_ERROR;
1112 *handle = init_lsql_handle(lsqlite3, module, context, callback);
1113 if (*handle == NULL) {
1116 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1117 (*handle)->state = LDB_ASYNC_DONE;
1118 (*handle)->status = LDB_SUCCESS;
1120 /* See if this is an ltdb special */
1121 if (ldb_dn_is_special(msg->dn)) {
1124 c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
1125 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1126 #warning "insert subclasses into object class tree"
1127 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1132 c = ldb_dn_explode(local_ctx, "@INDEXLIST");
1133 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1134 #warning "should we handle indexes somehow ?"
1138 /* Others are implicitly ignored */
1142 /* create linearized and normalized dns */
1143 dn = ldb_dn_linearize(lsql_ac, msg->dn);
1144 ndn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, msg->dn));
1145 if (dn == NULL || ndn == NULL) {
1146 ret = LDB_ERR_OTHER;
1150 query = lsqlite3_tprintf(lsql_ac,
1152 "INSERT OR ABORT INTO ldb_entry "
1153 "('dn', 'norm_dn') "
1154 "VALUES ('%q', '%q');",
1156 if (query == NULL) {
1157 ret = LDB_ERR_OTHER;
1161 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1162 if (ret != SQLITE_OK) {
1164 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1167 ret = LDB_ERR_OTHER;
1171 eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, lsql_ac, ndn);
1173 ret = LDB_ERR_OTHER;
1177 for (i = 0; i < msg->num_elements; i++) {
1178 const struct ldb_message_element *el = &msg->elements[i];
1179 const struct ldb_attrib_handler *h;
1183 /* Get a case-folded copy of the attribute name */
1184 attr = ldb_attr_casefold(lsql_ac, el->name);
1186 ret = LDB_ERR_OTHER;
1190 h = ldb_attrib_handler(module->ldb, el->name);
1192 /* For each value of the specified attribute name... */
1193 for (j = 0; j < el->num_values; j++) {
1194 struct ldb_val value;
1197 /* Get a canonicalised copy of the data */
1198 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1199 if (value.data == NULL) {
1200 ret = LDB_ERR_OTHER;
1204 insert = lsqlite3_tprintf(lsql_ac,
1205 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1206 "('eid', 'attr_name', 'norm_attr_name',"
1207 " 'attr_value', 'norm_attr_value') "
1208 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1209 eid, el->name, attr,
1210 el->values[j].data, value.data);
1211 if (insert == NULL) {
1212 ret = LDB_ERR_OTHER;
1216 ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg);
1217 if (ret != SQLITE_OK) {
1219 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1222 ret = LDB_ERR_OTHER;
1228 if (lsql_ac->callback)
1229 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1234 talloc_free(*handle);
1238 static int lsql_add(struct ldb_module *module, const struct ldb_message *msg)
1240 struct ldb_async_handle *handle;
1243 ret = lsql_add_async(module, msg, NULL, NULL, &handle);
1245 if (ret != LDB_SUCCESS)
1248 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
1250 talloc_free(handle);
1255 /* modify a record */
1256 static int lsql_modify_async(struct ldb_module *module, const struct ldb_message *msg,
1258 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1259 struct ldb_async_handle **handle)
1261 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1262 struct lsql_async_context *lsql_ac;
1266 int ret = LDB_ERR_OPERATIONS_ERROR;
1268 *handle = init_lsql_handle(lsqlite3, module, context, callback);
1269 if (*handle == NULL) {
1272 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1273 (*handle)->state = LDB_ASYNC_DONE;
1274 (*handle)->status = LDB_SUCCESS;
1276 /* See if this is an ltdb special */
1277 if (ldb_dn_is_special(msg->dn)) {
1280 c = ldb_dn_explode(lsql_ac, "@SUBCLASSES");
1281 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1282 #warning "modify subclasses into object class tree"
1283 ret = LDB_ERR_UNWILLING_TO_PERFORM;
1287 /* Others are implicitly ignored */
1291 eid = lsqlite3_get_eid(module, msg->dn);
1293 ret = LDB_ERR_OTHER;
1297 for (i = 0; i < msg->num_elements; i++) {
1298 const struct ldb_message_element *el = &msg->elements[i];
1299 const struct ldb_attrib_handler *h;
1300 int flags = el->flags & LDB_FLAG_MOD_MASK;
1305 /* Get a case-folded copy of the attribute name */
1306 attr = ldb_attr_casefold(lsql_ac, el->name);
1308 ret = LDB_ERR_OTHER;
1312 h = ldb_attrib_handler(module->ldb, el->name);
1316 case LDB_FLAG_MOD_REPLACE:
1318 /* remove all attributes before adding the replacements */
1319 mod = lsqlite3_tprintf(lsql_ac,
1320 "DELETE FROM ldb_attribute_values "
1321 "WHERE eid = '%lld' "
1322 "AND norm_attr_name = '%q';",
1325 ret = LDB_ERR_OTHER;
1329 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1330 if (ret != SQLITE_OK) {
1332 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1335 ret = LDB_ERR_OTHER;
1339 /* MISSING break is INTENTIONAL */
1341 case LDB_FLAG_MOD_ADD:
1342 #warning "We should throw an error if no value is provided!"
1343 /* For each value of the specified attribute name... */
1344 for (j = 0; j < el->num_values; j++) {
1345 struct ldb_val value;
1347 /* Get a canonicalised copy of the data */
1348 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1349 if (value.data == NULL) {
1350 ret = LDB_ERR_OTHER;
1354 mod = lsqlite3_tprintf(lsql_ac,
1355 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1356 "('eid', 'attr_name', 'norm_attr_name',"
1357 " 'attr_value', 'norm_attr_value') "
1358 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1359 eid, el->name, attr,
1360 el->values[j].data, value.data);
1363 ret = LDB_ERR_OTHER;
1367 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1368 if (ret != SQLITE_OK) {
1370 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1373 ret = LDB_ERR_OTHER;
1380 case LDB_FLAG_MOD_DELETE:
1381 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1382 if (el->num_values == 0) {
1383 mod = lsqlite3_tprintf(lsql_ac,
1384 "DELETE FROM ldb_attribute_values "
1385 "WHERE eid = '%lld' "
1386 "AND norm_attr_name = '%q';",
1389 ret = LDB_ERR_OTHER;
1393 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1394 if (ret != SQLITE_OK) {
1396 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1399 ret = LDB_ERR_OTHER;
1404 /* For each value of the specified attribute name... */
1405 for (j = 0; j < el->num_values; j++) {
1406 struct ldb_val value;
1408 /* Get a canonicalised copy of the data */
1409 h->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value);
1410 if (value.data == NULL) {
1411 ret = LDB_ERR_OTHER;
1415 mod = lsqlite3_tprintf(lsql_ac,
1416 "DELETE FROM ldb_attribute_values "
1417 "WHERE eid = '%lld' "
1418 "AND norm_attr_name = '%q' "
1419 "AND norm_attr_value = '%q';",
1420 eid, attr, value.data);
1423 ret = LDB_ERR_OTHER;
1427 ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg);
1428 if (ret != SQLITE_OK) {
1430 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1433 ret = LDB_ERR_OTHER;
1442 if (lsql_ac->callback)
1443 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1448 talloc_free(*handle);
1452 static int lsql_modify(struct ldb_module *module, const struct ldb_message *msg)
1454 struct ldb_async_handle *handle;
1457 ret = lsql_modify_async(module, msg, NULL, NULL, &handle);
1459 if (ret != LDB_SUCCESS)
1462 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
1464 talloc_free(handle);
1468 /* delete a record */
1469 static int lsql_delete_async(struct ldb_module *module, const struct ldb_dn *dn,
1471 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1472 struct ldb_async_handle **handle)
1474 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1475 struct lsql_async_context *lsql_ac;
1479 int ret = LDB_ERR_OPERATIONS_ERROR;
1482 *handle = init_lsql_handle(lsqlite3, module, context, callback);
1483 if (*handle == NULL) {
1486 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1487 (*handle)->state = LDB_ASYNC_DONE;
1488 (*handle)->status = LDB_SUCCESS;
1490 eid = lsqlite3_get_eid(module, dn);
1495 query = lsqlite3_tprintf(lsql_ac,
1497 "DELETE FROM ldb_entry WHERE eid = %lld; "
1498 /* Delete attributes */
1499 "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1501 if (query == NULL) {
1502 ret = LDB_ERR_OTHER;
1506 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1507 if (ret != SQLITE_OK) {
1509 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1512 ret = LDB_ERR_OPERATIONS_ERROR;
1516 if (lsql_ac->callback)
1517 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1522 talloc_free(*handle);
1526 static int lsql_delete(struct ldb_module *module, const struct ldb_dn *dn)
1528 struct ldb_async_handle *handle;
1531 /* ignore ltdb specials */
1532 if (ldb_dn_is_special(dn)) {
1536 ret = lsql_delete_async(module, dn, NULL, NULL, &handle);
1538 if (ret != LDB_SUCCESS)
1541 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
1543 talloc_free(handle);
1547 /* rename a record */
1548 static int lsql_rename_async(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn,
1550 int (*callback)(struct ldb_context *, void *, struct ldb_async_result *),
1551 struct ldb_async_handle **handle)
1553 struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private);
1554 struct lsql_async_context *lsql_ac;
1555 char *new_dn, *new_cdn, *old_cdn;
1558 int ret = LDB_ERR_OPERATIONS_ERROR;
1560 *handle = init_lsql_handle(lsqlite3, module, context, callback);
1561 if (*handle == NULL) {
1564 lsql_ac = talloc_get_type((*handle)->private_data, struct lsql_async_context);
1565 (*handle)->state = LDB_ASYNC_DONE;
1566 (*handle)->status = LDB_SUCCESS;
1568 /* create linearized and normalized dns */
1569 old_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, olddn));
1570 new_cdn = ldb_dn_linearize(lsql_ac, ldb_dn_casefold(module->ldb, newdn));
1571 new_dn = ldb_dn_linearize(lsql_ac, newdn);
1572 if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) {
1576 /* build the SQL query */
1577 query = lsqlite3_tprintf(lsql_ac,
1578 "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1579 "WHERE norm_dn = '%q';",
1580 new_dn, new_cdn, old_cdn);
1581 if (query == NULL) {
1586 ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg);
1587 if (ret != SQLITE_OK) {
1589 ldb_set_errstring(module->ldb, talloc_strdup(module, errmsg));
1592 ret = LDB_ERR_OPERATIONS_ERROR;
1596 if (lsql_ac->callback)
1597 (*handle)->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL);
1602 talloc_free(*handle);
1606 static int lsql_rename(struct ldb_module *module, const struct ldb_dn *olddn, const struct ldb_dn *newdn)
1608 struct ldb_async_handle *handle;
1611 /* ignore ltdb specials */
1612 if (ldb_dn_is_special(olddn) || ldb_dn_is_special(newdn)) {
1617 ret = lsql_rename_async(module, olddn, newdn, NULL, NULL, &handle);
1619 if (ret != LDB_SUCCESS)
1622 ret = ldb_async_wait(module->ldb, handle, LDB_WAIT_ALL);
1624 talloc_free(handle);
1628 static int lsql_start_trans(struct ldb_module * module)
1632 struct lsqlite3_private * lsqlite3 = module->private_data;
1634 if (lsqlite3->trans_count == 0) {
1635 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN IMMEDIATE;", NULL, NULL, &errmsg);
1636 if (ret != SQLITE_OK) {
1638 printf("lsqlite3_start_trans: error: %s\n", errmsg);
1645 lsqlite3->trans_count++;
1650 static int lsql_end_trans(struct ldb_module *module)
1654 struct lsqlite3_private *lsqlite3 = module->private_data;
1656 if (lsqlite3->trans_count > 0) {
1657 lsqlite3->trans_count--;
1660 if (lsqlite3->trans_count == 0) {
1661 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1662 if (ret != SQLITE_OK) {
1664 printf("lsqlite3_end_trans: error: %s\n", errmsg);
1674 static int lsql_del_trans(struct ldb_module *module)
1676 struct lsqlite3_private *lsqlite3 = module->private_data;
1678 if (lsqlite3->trans_count > 0) {
1679 lsqlite3->trans_count--;
1682 if (lsqlite3->trans_count == 0) {
1683 return lsqlite3_safe_rollback(lsqlite3->sqlite);
1693 static int initialize(struct lsqlite3_private *lsqlite3,
1694 struct ldb_context *ldb, const char *url, int flags)
1696 TALLOC_CTX *local_ctx;
1703 /* create a local ctx */
1704 local_ctx = talloc_named(lsqlite3, 0, "lsqlite3_rename local context");
1705 if (local_ctx == NULL) {
1709 schema = lsqlite3_tprintf(local_ctx,
1712 "CREATE TABLE ldb_info AS "
1713 " SELECT 'LDB' AS database_type,"
1714 " '1.0' AS version;"
1717 * The entry table holds the information about an entry.
1718 * This table is used to obtain the EID of the entry and to
1719 * support scope=one and scope=base. The parent and child
1720 * table is included in the entry table since all the other
1721 * attributes are dependent on EID.
1723 "CREATE TABLE ldb_entry "
1725 " eid INTEGER PRIMARY KEY AUTOINCREMENT,"
1726 " dn TEXT UNIQUE NOT NULL,"
1727 " norm_dn TEXT UNIQUE NOT NULL"
1731 "CREATE TABLE ldb_object_classes"
1733 " class_name TEXT PRIMARY KEY,"
1734 " parent_class_name TEXT,"
1735 " tree_key TEXT UNIQUE,"
1736 " max_child_num INTEGER DEFAULT 0"
1740 * We keep a full listing of attribute/value pairs here
1742 "CREATE TABLE ldb_attribute_values"
1744 " eid INTEGER REFERENCES ldb_entry,"
1746 " norm_attr_name TEXT,"
1748 " norm_attr_value TEXT "
1755 "CREATE INDEX ldb_attribute_values_eid_idx "
1756 " ON ldb_attribute_values (eid);"
1758 "CREATE INDEX ldb_attribute_values_name_value_idx "
1759 " ON ldb_attribute_values (attr_name, norm_attr_value);"
1767 "CREATE TRIGGER ldb_object_classes_insert_tr"
1769 " ON ldb_object_classes"
1772 " UPDATE ldb_object_classes"
1773 " SET tree_key = COALESCE(tree_key, "
1775 " SELECT tree_key || "
1776 " (SELECT base160(max_child_num + 1)"
1777 " FROM ldb_object_classes"
1778 " WHERE class_name = "
1779 " new.parent_class_name)"
1780 " FROM ldb_object_classes "
1781 " WHERE class_name = new.parent_class_name "
1783 " UPDATE ldb_object_classes "
1784 " SET max_child_num = max_child_num + 1"
1785 " WHERE class_name = new.parent_class_name;"
1789 * Table initialization
1792 "INSERT INTO ldb_object_classes "
1793 " (class_name, tree_key) "
1795 " ('TOP', '0001');");
1797 /* Skip protocol indicator of url */
1798 if (strncmp(url, "sqlite://", 9) != 0) {
1799 return SQLITE_MISUSE;
1802 /* Update pointer to just after the protocol indicator */
1805 /* Try to open the (possibly empty/non-existent) database */
1806 if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1810 /* In case this is a new database, enable auto_vacuum */
1811 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA auto_vacuum = 1;", NULL, NULL, &errmsg);
1812 if (ret != SQLITE_OK) {
1814 printf("lsqlite3 initializaion error: %s\n", errmsg);
1820 if (flags & LDB_FLG_NOSYNC) {
1822 ret = sqlite3_exec(lsqlite3->sqlite, "PRAGMA synchronous = OFF;", NULL, NULL, &errmsg);
1823 if (ret != SQLITE_OK) {
1825 printf("lsqlite3 initializaion error: %s\n", errmsg);
1834 /* Establish a busy timeout of 30 seconds */
1835 if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1836 30000)) != SQLITE_OK) {
1840 /* Create a function, callable from sql, to increment a tree_key */
1842 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1843 "base160_next", /* function name */
1844 1, /* number of args */
1845 SQLITE_ANY, /* preferred text type */
1846 NULL, /* user data */
1847 base160next_sql, /* called func */
1848 NULL, /* step func */
1849 NULL /* final func */
1854 /* Create a function, callable from sql, to convert int to base160 */
1856 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1857 "base160", /* function name */
1858 1, /* number of args */
1859 SQLITE_ANY, /* preferred text type */
1860 NULL, /* user data */
1861 base160_sql, /* called func */
1862 NULL, /* step func */
1863 NULL /* final func */
1868 /* Create a function, callable from sql, to perform various comparisons */
1870 sqlite3_create_function(lsqlite3->sqlite, /* handle */
1871 "ldap_compare", /* function name */
1872 4, /* number of args */
1873 SQLITE_ANY, /* preferred text type */
1874 ldb , /* user data */
1875 lsqlite3_compare, /* called func */
1876 NULL, /* step func */
1877 NULL /* final func */
1882 /* Begin a transaction */
1883 ret = sqlite3_exec(lsqlite3->sqlite, "BEGIN EXCLUSIVE;", NULL, NULL, &errmsg);
1884 if (ret != SQLITE_OK) {
1886 printf("lsqlite3: initialization error: %s\n", errmsg);
1893 /* Determine if this is a new database. No tables means it is. */
1894 if (query_int(lsqlite3,
1897 " FROM sqlite_master\n"
1898 " WHERE type = 'table';") != 0) {
1902 if (queryInt == 0) {
1904 * Create the database schema
1906 ret = sqlite3_exec(lsqlite3->sqlite, schema, NULL, NULL, &errmsg);
1907 if (ret != SQLITE_OK) {
1909 printf("lsqlite3 initializaion error: %s\n", errmsg);
1916 * Ensure that the database we opened is one of ours
1918 if (query_int(lsqlite3,
1921 " (SELECT COUNT(*) = 2"
1922 " FROM sqlite_master "
1923 " WHERE type = 'table' "
1927 " 'ldb_object_classes' "
1933 " WHERE database_type = 'LDB' "
1934 " AND version = '1.0'"
1938 /* It's not one that we created. See ya! */
1943 /* Commit the transaction */
1944 ret = sqlite3_exec(lsqlite3->sqlite, "COMMIT;", NULL, NULL, &errmsg);
1945 if (ret != SQLITE_OK) {
1947 printf("lsqlite3: iniialization error: %s\n", errmsg);
1956 if (rollback) lsqlite3_safe_rollback(lsqlite3->sqlite);
1957 sqlite3_close(lsqlite3->sqlite);
1964 struct lsqlite3_private *lsqlite3 = p;
1966 if (lsqlite3->sqlite) {
1967 sqlite3_close(lsqlite3->sqlite);
1972 static int lsql_async_wait(struct ldb_module *module, struct ldb_async_handle *handle, enum ldb_async_wait_type type)
1974 return handle->status;
1977 static int lsql_request(struct ldb_module *module, struct ldb_request *req)
1979 /* check for oustanding critical controls and return an error if found */
1981 if (req->controls != NULL) {
1982 ldb_debug(module->ldb, LDB_DEBUG_WARNING, "Controls should not reach the ldb_sqlite3 backend!\n");
1985 if (check_critical_controls(req->controls)) {
1986 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
1989 switch (req->operation) {
1991 case LDB_REQ_SEARCH:
1992 return lsql_search_bytree(module,
1993 req->op.search.base,
1994 req->op.search.scope,
1995 req->op.search.tree,
1996 req->op.search.attrs,
1997 &req->op.search.res);
2000 return lsql_add(module, req->op.add.message);
2002 case LDB_REQ_MODIFY:
2003 return lsql_modify(module, req->op.mod.message);
2005 case LDB_REQ_DELETE:
2006 return lsql_delete(module, req->op.del.dn);
2008 case LDB_REQ_RENAME:
2009 return lsql_rename(module,
2010 req->op.rename.olddn,
2011 req->op.rename.newdn);
2013 case LDB_ASYNC_SEARCH:
2014 return lsql_search_async(module,
2015 req->op.search.base,
2016 req->op.search.scope,
2017 req->op.search.tree,
2018 req->op.search.attrs,
2020 req->async.callback,
2021 &req->async.handle);
2024 return lsql_add_async(module,
2025 req->op.add.message,
2027 req->async.callback,
2028 &req->async.handle);
2030 case LDB_ASYNC_MODIFY:
2031 return lsql_modify_async(module,
2032 req->op.mod.message,
2034 req->async.callback,
2035 &req->async.handle);
2037 case LDB_ASYNC_DELETE:
2038 return lsql_delete_async(module,
2041 req->async.callback,
2042 &req->async.handle);
2044 case LDB_ASYNC_RENAME:
2045 return lsql_rename_async(module,
2046 req->op.rename.olddn,
2047 req->op.rename.newdn,
2049 req->async.callback,
2050 &req->async.handle);
2053 return LDB_ERR_OPERATIONS_ERROR;
2059 * Table of operations for the sqlite3 backend
2061 static const struct ldb_module_ops lsqlite3_ops = {
2063 .request = lsql_request,
2064 .start_transaction = lsql_start_trans,
2065 .end_transaction = lsql_end_trans,
2066 .del_transaction = lsql_del_trans,
2067 .async_wait = lsql_async_wait,
2071 * connect to the database
2073 static int lsqlite3_connect(struct ldb_context *ldb,
2076 const char *options[])
2080 struct lsqlite3_private * lsqlite3 = NULL;
2082 lsqlite3 = talloc(ldb, struct lsqlite3_private);
2087 lsqlite3->sqlite = NULL;
2088 lsqlite3->options = NULL;
2089 lsqlite3->trans_count = 0;
2091 ret = initialize(lsqlite3, ldb, url, flags);
2092 if (ret != SQLITE_OK) {
2096 talloc_set_destructor(lsqlite3, destructor);
2098 ldb->modules = talloc(ldb, struct ldb_module);
2099 if (!ldb->modules) {
2102 ldb->modules->ldb = ldb;
2103 ldb->modules->prev = ldb->modules->next = NULL;
2104 ldb->modules->private_data = lsqlite3;
2105 ldb->modules->ops = &lsqlite3_ops;
2109 * take a copy of the options array, so we don't have to rely
2110 * on the caller keeping it around (it might be dynamic)
2112 for (i=0;options[i];i++) ;
2114 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
2115 if (!lsqlite3->options) {
2119 for (i=0;options[i];i++) {
2121 lsqlite3->options[i+1] = NULL;
2122 lsqlite3->options[i] =
2123 talloc_strdup(lsqlite3->options, options[i]);
2124 if (!lsqlite3->options[i]) {
2133 if (lsqlite3->sqlite != NULL) {
2134 (void) sqlite3_close(lsqlite3->sqlite);
2136 talloc_free(lsqlite3);
2140 int ldb_sqlite3_init(void)
2142 return ldb_register_backend("sqlite3", lsqlite3_connect);