4 Copyright (C) Derrell Lipman 2005
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * Component: ldb sqlite3 backend
30 * Description: core files for SQLITE3 backend
32 * Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
37 #include "ldb/include/ldb.h"
38 #include "ldb/include/ldb_private.h"
39 #include "ldb/include/ldb_parse.h"
40 #include "ldb/include/ldb_explode_dn.h"
41 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
45 # define TRUE (! FALSE)
48 #define QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...) \
50 if (lsqlite3_query_norows(lsqlite3, sql) != 0) { \
51 if (bRollbackOnError) { \
52 lsqlite3_query_norows(lsqlite3, \
61 * lsqlite3_query_norows()
63 * This function is used for queries that are not expected to return any rows,
64 * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
65 * There are no provisions here for returning data from rows in a table, so do
66 * not pass SELECT queries to this function.
69 lsqlite3_query_norows(const struct lsqlite3_private *lsqlite3,
80 /* Begin access to variable argument list */
83 /* Format the query */
84 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
89 * Prepare and execute the SQL statement. Loop allows retrying on
90 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
91 * requiring retrying the operation.
93 for (bLoop = TRUE; bLoop; ) {
95 /* Compile the SQL statement into sqlite virtual machine */
96 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
100 &pTail)) != SQLITE_OK) {
105 /* No rows expected, so just step through machine code once */
106 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
107 (void) sqlite3_finalize(pStmt);
109 } else if (ret != SQLITE_DONE) {
110 (void) sqlite3_finalize(pStmt);
115 /* Free the virtual machine */
116 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
117 (void) sqlite3_finalize(pStmt);
119 } else if (ret != SQLITE_OK) {
120 (void) sqlite3_finalize(pStmt);
126 * Normal condition is only one time through loop. Loop is
127 * rerun in error conditions, via "continue", above.
133 /* All done with variable argument list */
136 /* Free the memory we allocated for our query string */
145 * we don't need this right now, but will once we add some backend options
147 * find an option in an option list (a null terminated list of strings)
149 * this assumes the list is short. If it ever gets long then we really should
150 * do this in some smarter way
153 lsqlite3_option_find(const struct lsqlite3_private *lsqlite3,
157 size_t len = strlen(name);
159 if (!lsqlite3->options) return NULL;
161 for (i=0;lsqlite3->options[i];i++) {
162 if (strncmp(lsqlite3->options[i], name, len) == 0 &&
163 lsqlite3->options[i][len] == '=') {
164 return &lsqlite3->options[i][len+1];
173 callback function used in call to ldb_dn_fold() for determining whether an
174 attribute type requires case folding.
176 static int lsqlite3_case_fold_attr_required(void * hUserData,
179 // struct ldb_module * module = hUserData;
181 #warning "currently, all attributes require case folding"
190 lsqlite3_rename(struct ldb_module * module,
194 /* ignore ltdb specials */
195 if (olddn[0] == '@' ||newdn[0] == '@') {
199 #warning "lsqlite3_rename() is not yet supported"
207 lsqlite3_delete(struct ldb_module *module,
210 /* ignore ltdb specials */
218 #if 0 /* not currently used */
220 * free a search result
223 lsqlite3_search_free(struct ldb_module *module,
224 struct ldb_message **res)
233 * add a single set of ldap message values to a ldb_message
236 /* get things to compile before we actually implement this function */
242 #warning "lsqlite3_add_msg_attr() not yet implemented or used"
245 lsqlite3_add_msg_attr(struct ldb_context *ldb,
246 struct ldb_message *msg,
248 struct berval **bval)
252 struct ldb_message_element * el;
254 count = ldap_count_values_len(bval);
260 el = talloc_realloc(msg, msg->elements, struct ldb_message_element,
261 msg->num_elements + 1);
269 el = &msg->elements[msg->num_elements];
271 el->name = talloc_strdup(msg->elements, attr);
279 el->values = talloc_array(msg->elements, struct ldb_val, count);
285 for (i=0;i<count;i++) {
286 el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
287 if (!el->values[i].data) {
290 el->values[i].length = bval[i]->bv_len;
301 lsqlite3_parsetree_to_sql(struct ldb_module *module,
303 const struct ldb_parse_tree *t)
312 switch(t->operation) {
317 ret = lsqlite3_parsetree_to_sql(module,
319 t->u.list.elements[0]);
321 for (i = 1; i < t->u.list.num_elements; i++) {
323 lsqlite3_parsetree_to_sql(
326 t->u.list.elements[i]);
327 ret = talloc_asprintf_append(ret,
335 ret = talloc_asprintf("(\n"
344 lsqlite3_parsetree_to_sql(
347 t->u.list.elements[0]);
349 for (i = 1; i < t->u.list.num_elements; i++) {
351 lsqlite3_parsetree_to_sql(
354 t->u.list.elements[i]);
355 ret = talloc_asprintf_append(ret,
362 ret = talloc_asprintf("(\n"
371 lsqlite3_parsetree_to_sql(
375 ret = talloc_asprintf(hTalloc,
379 " WHERE eid NOT IN %s\n"
386 /* should never occur */
390 /* Get a case-folded copy of the attribute name */
391 pAttrName = ldb_casefold((struct ldb_context *) module,
395 * For simple searches, we want to retrieve the list of EIDs that
396 * match the criteria. We accomplish this by searching the
397 * appropriate table, ldb_attr_<attributeName>, for the eid
398 * corresponding to all matching values.
400 if (t->u.simple.value.length == 1 &&
401 (*(const char *) t->u.simple.value.data) == '*') {
403 * Special case for "attr_name=*". In this case, we want the
404 * eid corresponding to all values in the specified attribute
407 if ((p = sqlite3_mprintf("(\n"
409 " FROM ldb_attr_%q\n"
411 pAttrName)) == NULL) {
415 ret = talloc_strdup(hTalloc, p);
418 } else if (strcasecmp(t->u.simple.attr, "objectclass") == 0) {
420 * For object classes, we want to search for all objectclasses
421 * that are subclasses as well.
423 if ((p = sqlite3_mprintf(
426 " FROM ldb_attr_objectclass\n"
427 " WHERE attr_name IN\n"
428 " (SELECT class_name\n"
429 " FROM ldb_objectclasses\n"
430 " WHERE tree_key GLOB\n"
431 " (SELECT tree_key\n"
432 " FROM ldb_objectclasses\n"
433 " WHERE class_name = %Q) || '*')\n"
435 t->u.simple.value.data)) == NULL) {
439 ret = talloc_strdup(hTalloc, p);
443 /* A normal query. */
444 if ((p = sqlite3_mprintf("(\n"
446 " FROM ldb_attr_%q\n"
447 " WHERE attr_value = %Q\n"
450 t->u.simple.value.data)) == NULL) {
454 ret = talloc_strdup(hTalloc, p);
462 lsqlite3_parsetree_to_tablelist(struct ldb_module *module,
464 const struct ldb_parse_tree *t)
466 #warning "obtain talloc'ed array of attribute names for table list"
472 * search for matching records
475 lsqlite3_search(struct ldb_module * module,
476 const char * pBaseDN,
477 enum ldb_scope scope,
478 const char * pExpression,
479 const char * const attrs[],
480 struct ldb_message *** res)
486 char * sql_constraints;
490 sqlite3_stmt * pStmt;
491 struct ldb_parse_tree * pTree;
492 struct lsqlite3_private * lsqlite3 = module->private_data;
494 if (pBaseDN == NULL) {
499 * Obtain the eid of the base DN
501 if ((pTail = sqlite3_mprintf("SELECT eid "
503 " WHERE attr_value = %Q;",
508 for (bLoop = TRUE; bLoop; ) {
510 /* Compile the SQL statement into sqlite virtual machine */
511 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
515 NULL)) != SQLITE_OK) {
520 /* One row expected */
521 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
522 (void) sqlite3_finalize(pStmt);
524 } else if (ret != SQLITE_ROW) {
525 (void) sqlite3_finalize(pStmt);
530 /* Retrieve the EID */
531 eid = sqlite3_column_int64(pStmt, 0);
533 /* Free the virtual machine */
534 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
535 (void) sqlite3_finalize(pStmt);
537 } else if (ret != SQLITE_OK) {
538 (void) sqlite3_finalize(pStmt);
544 * Normal condition is only one time through loop. Loop is
545 * rerun in error conditions, via "continue", above.
547 sqlite3_free(discard_const_p(char, pTail));
553 sqlite3_free(discard_const_p(char, pTail));
557 /* Parse the filter expression into a tree we can work with */
558 if ((pTree = ldb_parse_tree(module->ldb, pExpression)) == NULL) {
559 sqlite3_free(discard_const_p(char, pTail));
563 /* Allocate a temporary talloc context */
564 hTalloc = talloc_new(module->ldb);
566 /* Move the parse tree to our temporary context */
567 talloc_steal(hTalloc, pTree);
569 /* Convert filter into a series of SQL statements (constraints) */
570 sql_constraints = lsqlite3_parsetree_to_sql(module, hTalloc, pTree);
572 /* Get the list of attribute names to use as our extra table list */
573 table_list = lsqlite3_parsetree_to_tablelist(module, hTalloc, pTree);
576 case LDB_SCOPE_DEFAULT:
577 case LDB_SCOPE_SUBTREE:
578 sql = sqlite3_mprintf(
579 "SELECT entry.entry_data\n"
580 " FROM ldb_entry AS entry\n"
581 " WHERE entry.eid IN\n"
582 " (SELECT DISTINCT ldb_entry.eid\n"
584 " ldb_descendants,\n"
586 " WHERE ldb_descendants.aeid = %lld\n"
587 " AND ldb_entry.eid = ldb_descendants.deid\n"
588 " AND ldap_entry.eid IN\n"
597 sql = sqlite3_mprintf(
598 "SELECT entry.entry_data\n"
599 " FROM ldb_entry AS entry\n"
600 " WHERE entry.eid IN\n"
601 " (SELECT DISTINCT ldb_entry.eid\n"
603 " WHERE ldb_entry.eid = %lld\n"
604 " AND ldb_entry.eid IN\n"
612 case LDB_SCOPE_ONELEVEL:
613 sql = sqlite3_mprintf(
614 "SELECT entry.entry_data\n"
615 " FROM ldb_entry AS entry\n"
616 " WHERE entry.eid IN\n"
617 " (SELECT DISTINCT ldb_entry.eid\n"
618 " FROM ldb_entry AS pchild, "
620 " WHERE ldb_entry.eid = pchild.eid "
621 " AND pchild.peid = %lld "
622 " AND ldb_entry.eid IN\n"
636 lsqlite3_new_attr(struct ldb_module * module,
639 struct lsqlite3_private * lsqlite3 = module->private_data;
641 /* NOTE: pAttrName is assumed to already be case-folded here! */
642 QUERY_NOROWS(lsqlite3,
644 "CREATE TABLE ldb_attr_%q "
646 " eid INTEGER REFERENCES ldb_entry, "
655 * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
656 * requests in the ldb_message
659 lsqlite3_msg_to_sql(struct ldb_module * module,
660 const struct ldb_message * msg,
668 struct lsqlite3_private * lsqlite3 = module->private_data;
670 for (i = 0; i < msg->num_elements; i++) {
671 const struct ldb_message_element *el = &msg->elements[i];
674 flags = LDB_FLAG_MOD_ADD;
676 flags = el->flags & LDB_FLAG_MOD_MASK;
679 /* Get a case-folded copy of the attribute name */
680 pAttrName = ldb_casefold((struct ldb_context *) module,
683 if (flags == LDB_FLAG_MOD_ADD) {
684 /* Create the attribute table if it doesn't exist */
685 if (lsqlite3_new_attr(module, pAttrName) != 0) {
690 /* For each value of the specified attribute name... */
691 for (j = 0; j < el->num_values; j++) {
693 /* ... bind the attribute value, if necessary */
695 case LDB_FLAG_MOD_ADD:
696 QUERY_NOROWS(lsqlite3,
698 "INSERT INTO ldb_attr_%q "
699 " (eid, attr_value) "
703 eid, el->values[j].data);
704 QUERY_NOROWS(lsqlite3,
708 " add_attr(entry_data, "
710 " WHERE eid = %lld;",
711 el->name, el->values[j].data,
716 case LDB_FLAG_MOD_REPLACE:
717 QUERY_NOROWS(lsqlite3,
719 "UPDATE ldb_attr_%q "
720 " SET attr_value = %Q "
721 " WHERE eid = %lld;",
725 QUERY_NOROWS(lsqlite3,
729 " mod_attr(entry_data, "
731 " WHERE eid = %lld;",
732 el->name, el->values[j].data,
736 case LDB_FLAG_MOD_DELETE:
737 /* No additional parameters to this query */
738 QUERY_NOROWS(lsqlite3,
740 "DELETE FROM ldb_attr_%q "
742 " AND attr_value = %Q;",
746 QUERY_NOROWS(lsqlite3,
750 " del_attr(entry_data, "
752 " WHERE eid = %lld;",
753 el->name, el->values[j].data,
765 lsqlite3_new_dn(struct ldb_module * module,
769 struct ldb_dn * pExplodedDN;
770 struct ldb_context * ldb = module->ldb;
771 // struct lsqlite3_private * lsqlite3 = module->private_data;
773 /* Explode and normalize the DN */
778 lsqlite3_case_fold_attr_required)) == NULL) {
782 #warning "*** lsqlite3_new_dn() not yet fully implemented ***"
791 lsqlite3_add(struct ldb_module *module,
792 const struct ldb_message *msg)
795 struct lsqlite3_private * lsqlite3 = module->private_data;
797 /* ignore ltdb specials */
798 if (msg->dn[0] == '@') {
802 /* Begin a transaction */
803 QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
806 * Build any portions of the directory tree that don't exist. If the
807 * final component already exists, it's an error.
809 if (lsqlite3_new_dn(module, msg->dn, &eid) != 0) {
810 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
814 /* Add attributes to this new entry */
815 if (lsqlite3_msg_to_sql(module, msg, eid, FALSE) != 0) {
816 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
820 /* Everything worked. Commit it! */
821 QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
830 lsqlite3_modify(struct ldb_module *module,
831 const struct ldb_message *msg)
838 sqlite3_stmt * pStmt;
839 struct lsqlite3_private * lsqlite3 = module->private_data;
841 /* ignore ltdb specials */
842 if (msg->dn[0] == '@') {
846 /* Begin a transaction */
847 QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
849 /* Format the query */
850 if ((p = sqlite3_mprintf(
856 lsqlite3_case_fold_attr_required))) == NULL) {
860 /* Get the id of this DN. */
861 for (bLoop = TRUE; bLoop; ) {
863 /* Compile the SQL statement into sqlite virtual machine */
864 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
868 &pTail)) != SQLITE_OK) {
873 /* One row expected */
874 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
875 (void) sqlite3_finalize(pStmt);
877 } else if (ret != SQLITE_ROW) {
878 (void) sqlite3_finalize(pStmt);
883 /* Retrieve the EID */
884 eid = sqlite3_column_int64(pStmt, 0);
886 /* Free the virtual machine */
887 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
888 (void) sqlite3_finalize(pStmt);
890 } else if (ret != SQLITE_OK) {
891 (void) sqlite3_finalize(pStmt);
896 /* Modify attributes as specified */
897 if (lsqlite3_msg_to_sql(module, msg, eid, FALSE) != 0) {
898 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
903 * Normal condition is only one time through loop. Loop is
904 * rerun in error conditions, via "continue", above.
911 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
915 /* Everything worked. Commit it! */
916 QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
921 lsqlite3_lock(struct ldb_module *module,
922 const char *lockname)
924 if (lockname == NULL) {
928 /* TODO implement a local locking mechanism here */
934 lsqlite3_unlock(struct ldb_module *module,
935 const char *lockname)
937 if (lockname == NULL) {
941 /* TODO implement a local locking mechanism here */
947 * return extended error information
950 lsqlite3_errstring(struct ldb_module *module)
952 struct lsqlite3_private * lsqlite3 = module->private_data;
954 return sqlite3_errmsg(lsqlite3->sqlite);
958 static const struct ldb_module_ops lsqlite3_ops = {
972 lsqlite3_destructor(void *p)
974 struct lsqlite3_private * lsqlite3 = p;
976 (void) sqlite3_close(lsqlite3->sqlite);
981 lsqlite3_initialize(struct lsqlite3_private *lsqlite3,
985 int bNewDatabase = FALSE;
990 const char * schema =
991 "-- ------------------------------------------------------"
993 "PRAGMA auto_vacuum=1;"
995 "-- ------------------------------------------------------"
999 "-- ------------------------------------------------------"
1001 "CREATE TABLE ldb_info AS"
1002 " SELECT 'LDB' AS database_type,"
1003 " '1.0' AS version;"
1005 "-- ------------------------------------------------------"
1009 " * The entry table holds the information about an entry. "
1010 " * This table is used to obtain the EID of the entry and to "
1011 " * support scope=one and scope=base. The parent and child"
1012 " * table is included in the entry table since all the other"
1013 " * attributes are dependent on EID."
1015 "CREATE TABLE ldb_entry"
1017 " -- Unique identifier of this LDB entry"
1018 " eid INTEGER PRIMARY KEY,"
1020 " -- Unique identifier of the parent LDB entry"
1021 " peid INTEGER REFERENCES ldb_entry,"
1023 " -- Distinguished name of this entry"
1026 " -- Time when the entry was created"
1027 " create_timestamp INTEGER,"
1029 " -- Time when the entry was last modified"
1030 " modify_timestamp INTEGER,"
1032 " -- Attributes of this entry, in the form"
1033 " -- attr\1value\0[attr\1value\0]*\0"
1039 " * The purpose of the descendant table is to support the"
1040 " * subtree search feature. For each LDB entry with a unique"
1041 " * ID (AEID), this table contains the unique identifiers"
1042 " * (DEID) of the descendant entries."
1044 " * For evern entry in the directory, a row exists in this"
1045 " * table for each of its ancestors including itself. The "
1046 " * size of the table depends on the depth of each entry. In "
1047 " * the worst case, if all the entries were at the same "
1048 " * depth, the number of rows in the table is O(nm) where "
1049 " * n is the number of nodes in the directory and m is the "
1050 " * depth of the tree. "
1052 "CREATE TABLE ldb_descendants"
1054 " -- The unique identifier of the ancestor LDB entry"
1055 " aeid INTEGER REFERENCES ldb_entry,"
1057 " -- The unique identifier of the descendant LDB entry"
1058 " deid INTEGER REFERENCES ldb_entry"
1062 "CREATE TABLE ldb_object_classes"
1064 " -- Object classes are inserted into this table to track"
1065 " -- their class hierarchy. 'top' is the top-level class"
1066 " -- of which all other classes are subclasses."
1067 " class_name TEXT PRIMARY KEY,"
1069 " -- tree_key tracks the position of the class in"
1071 " tree_key TEXT UNIQUE"
1075 " * There is one attribute table per searchable attribute."
1078 "CREATE TABLE ldb_attr_ATTRIBUTE_NAME"
1080 " -- The unique identifier of the LDB entry"
1081 " eid INTEGER REFERENCES ldb_entry,"
1083 " -- Normalized attribute value"
1089 "-- ------------------------------------------------------"
1093 "-- ------------------------------------------------------"
1096 "CREATE TRIGGER ldb_entry_insert_tr"
1102 " SET create_timestamp = strftime('%s', 'now'),"
1103 " modify_timestamp = strftime('%s', 'now')"
1104 " WHERE eid = new.eid;"
1107 "CREATE TRIGGER ldb_entry_update_tr"
1113 " SET modify_timestamp = strftime('%s', 'now')"
1114 " WHERE eid = old.eid;"
1117 "-- ------------------------------------------------------"
1118 "-- Table initialization"
1120 "/* We need an implicit 'top' level object class */"
1121 "INSERT INTO ldb_attributes (attr_name,"
1123 " SELECT 'top', '';"
1125 "-- ------------------------------------------------------"
1129 "-- ------------------------------------------------------"
1132 /* Skip protocol indicator of url */
1133 if ((p = strchr(url, ':')) == NULL) {
1134 return SQLITE_MISUSE;
1140 * See if we'll be creating a new database, or opening an existing one
1142 #warning "eliminate stat() here; concurrent processes could conflict"
1143 if ((stat(p, &statbuf) < 0 && errno == ENOENT) ||
1144 statbuf.st_size == 0) {
1146 bNewDatabase = TRUE;
1149 /* Try to open the (possibly empty/non-existent) database */
1150 if ((ret = sqlite3_open(p, &lsqlite3->sqlite)) != SQLITE_OK) {
1156 * Create the database schema
1158 for (pTail = discard_const_p(char, schema); pTail != NULL; ) {
1160 if ((ret = sqlite3_prepare(
1165 &pTail)) != SQLITE_OK ||
1166 (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
1167 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
1169 (void) sqlite3_close(lsqlite3->sqlite);
1175 * Ensure that the database we opened is one of ours
1177 if ((ret = sqlite3_prepare(
1180 " FROM sqlite_master "
1181 " WHERE type = 'table' "
1185 " 'ldb_descendants', "
1186 " 'ldb_object_classes' "
1190 &pTail)) != SQLITE_OK ||
1191 (ret = sqlite3_step(stmt)) != SQLITE_ROW ||
1192 sqlite3_column_int(stmt, 0) != 3 ||
1193 (ret = sqlite3_finalize(stmt)) != SQLITE_OK ||
1195 (ret = sqlite3_prepare(
1199 " WHERE database_type = 'LDB' "
1200 " AND version = '1.0';",
1203 &pTail)) != SQLITE_OK ||
1204 (ret = sqlite3_step(stmt)) != SQLITE_ROW ||
1205 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
1207 /* It's not one that we created. See ya! */
1208 (void) sqlite3_close(lsqlite3->sqlite);
1209 return SQLITE_MISUSE;
1217 * connect to the database
1219 struct ldb_context *
1220 lsqlite3_connect(const char *url,
1222 const char *options[])
1226 struct ldb_context * ldb = NULL;
1227 struct lsqlite3_private * lsqlite3 = NULL;
1229 ldb = talloc(NULL, struct ldb_context);
1235 lsqlite3 = talloc(ldb, struct lsqlite3_private);
1241 lsqlite3->sqlite = NULL;
1242 lsqlite3->options = NULL;
1243 lsqlite3->lock_count = 0;
1245 ret = lsqlite3_initialize(lsqlite3, url);
1246 if (ret != SQLITE_OK) {
1250 talloc_set_destructor(lsqlite3, lsqlite3_destructor);
1252 ldb->modules = talloc(ldb, struct ldb_module);
1253 if (!ldb->modules) {
1257 ldb->modules->ldb = ldb;
1258 ldb->modules->prev = ldb->modules->next = NULL;
1259 ldb->modules->private_data = lsqlite3;
1260 ldb->modules->ops = &lsqlite3_ops;
1264 * take a copy of the options array, so we don't have to rely
1265 * on the caller keeping it around (it might be dynamic)
1267 for (i=0;options[i];i++) ;
1269 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1270 if (!lsqlite3->options) {
1274 for (i=0;options[i];i++) {
1276 lsqlite3->options[i+1] = NULL;
1277 lsqlite3->options[i] =
1278 talloc_strdup(lsqlite3->options, options[i]);
1279 if (!lsqlite3->options[i]) {
1288 if (lsqlite3->sqlite != NULL) {
1289 (void) sqlite3_close(lsqlite3->sqlite);