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"
44 * Macros used throughout
49 # define TRUE (! FALSE)
52 #define QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...) \
54 if (query_norows(lsqlite3, sql) != 0) { \
55 if (bRollbackOnError) { \
56 query_norows(lsqlite3, \
63 #define QUERY_INT(lsqlite3, result_var, bRollbackOnError, sql...) \
65 if (query_int(lsqlite3, &result_var, sql) != 0) { \
66 if (bRollbackOnError) { \
67 query_norows(lsqlite3, \
76 * Forward declarations
79 lsqlite3_rename(struct ldb_module * module,
84 lsqlite3_delete(struct ldb_module *module,
88 lsqlite3_search(struct ldb_module * module,
91 const char * pExpression,
92 const char * const attrs[],
93 struct ldb_message *** res);
96 lsqlite3_add(struct ldb_module *module,
97 const struct ldb_message *msg);
100 lsqlite3_modify(struct ldb_module *module,
101 const struct ldb_message *msg);
104 lsqlite3_lock(struct ldb_module *module,
105 const char *lockname);
108 lsqlite3_unlock(struct ldb_module *module,
109 const char *lockname);
112 lsqlite3_errstring(struct ldb_module *module);
115 initialize(struct lsqlite3_private *lsqlite3,
122 query_norows(const struct lsqlite3_private *lsqlite3,
127 query_int(const struct lsqlite3_private * lsqlite3,
132 static int case_fold_attr_required(void * hUserData,
136 parsetree_to_sql(struct ldb_module *module,
138 const struct ldb_parse_tree *t);
141 parsetree_to_tablelist(struct ldb_module *module,
143 const struct ldb_parse_tree *t);
146 msg_to_sql(struct ldb_module * module,
147 const struct ldb_message * msg,
152 new_dn(struct ldb_module * module,
157 new_attr(struct ldb_module * module,
162 * Table of operations for the sqlite3 backend
164 static const struct ldb_module_ops lsqlite3_ops = {
185 * connect to the database
188 lsqlite3_connect(const char *url,
190 const char *options[])
194 struct ldb_context * ldb = NULL;
195 struct lsqlite3_private * lsqlite3 = NULL;
197 ldb = talloc(NULL, struct ldb_context);
203 lsqlite3 = talloc(ldb, struct lsqlite3_private);
209 lsqlite3->sqlite = NULL;
210 lsqlite3->options = NULL;
211 lsqlite3->lock_count = 0;
213 ret = initialize(lsqlite3, url);
214 if (ret != SQLITE_OK) {
218 talloc_set_destructor(lsqlite3, destructor);
220 ldb->modules = talloc(ldb, struct ldb_module);
225 ldb->modules->ldb = ldb;
226 ldb->modules->prev = ldb->modules->next = NULL;
227 ldb->modules->private_data = lsqlite3;
228 ldb->modules->ops = &lsqlite3_ops;
232 * take a copy of the options array, so we don't have to rely
233 * on the caller keeping it around (it might be dynamic)
235 for (i=0;options[i];i++) ;
237 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
238 if (!lsqlite3->options) {
242 for (i=0;options[i];i++) {
244 lsqlite3->options[i+1] = NULL;
245 lsqlite3->options[i] =
246 talloc_strdup(lsqlite3->options, options[i]);
247 if (!lsqlite3->options[i]) {
256 if (lsqlite3->sqlite != NULL) {
257 (void) sqlite3_close(lsqlite3->sqlite);
265 * Interface functions referenced by lsqlite3_ops
268 /* rename a record */
270 lsqlite3_rename(struct ldb_module * module,
274 /* ignore ltdb specials */
275 if (olddn[0] == '@' ||newdn[0] == '@') {
279 #warning "lsqlite3_rename() is not yet supported"
283 /* delete a record */
285 lsqlite3_delete(struct ldb_module *module,
288 /* ignore ltdb specials */
293 #warning "lsqlite3_delete() is not yet supported"
297 /* search for matching records */
299 lsqlite3_search(struct ldb_module * module,
300 const char * pBaseDN,
301 enum ldb_scope scope,
302 const char * pExpression,
303 const char * const attrs[],
304 struct ldb_message *** res)
311 char * pSqlConstraints;
315 const char * pAttrName;
316 const char * pAttrValue;
317 sqlite3_stmt * pStmt;
318 struct ldb_parse_tree * pTree;
319 struct lsqlite3_private * lsqlite3 = module->private_data;
321 if (pBaseDN == NULL) {
325 /* Begin a transaction */
326 QUERY_NOROWS(lsqlite3, FALSE, "BEGIN IMMEDIATE;");
329 * Obtain the eid of the base DN
336 " WHERE attr_value = %Q;",
339 /* Parse the filter expression into a tree we can work with */
340 if ((pTree = ldb_parse_tree(module->ldb, pExpression)) == NULL) {
344 /* Allocate a temporary talloc context */
345 hTalloc = talloc_new(module->ldb);
347 /* Move the parse tree to our temporary context */
348 talloc_steal(hTalloc, pTree);
350 /* Convert filter into a series of SQL statements (constraints) */
351 pSqlConstraints = parsetree_to_sql(module, hTalloc, pTree);
353 /* Get the list of attribute names to use as our extra table list */
354 pTableList = parsetree_to_tablelist(module, hTalloc, pTree);
357 case LDB_SCOPE_DEFAULT:
358 case LDB_SCOPE_SUBTREE:
359 pSql = sqlite3_mprintf(
360 "SELECT entry.eid,\n"
364 " FROM ldb_entry AS entry,\n"
365 " ldb_attribute_values AS av\n"
366 " WHERE entry.eid IN\n"
367 " (SELECT DISTINCT ldb_entry.eid\n"
369 " ldb_descendants,\n"
371 " WHERE ldb_descendants.aeid = %lld\n"
372 " AND ldb_entry.eid = ldb_descendants.deid\n"
373 " AND ldap_entry.eid IN\n%s\n"
375 " AND av.eid = entry.eid "
376 " ORDER BY av.eid, av.attr_name;",
383 pSql = sqlite3_mprintf(
384 "SELECT entry.eid,\n"
388 " FROM ldb_entry AS entry,\n"
389 " ldb_attribute_values AS av\n"
390 " WHERE entry.eid IN\n"
391 " (SELECT DISTINCT ldb_entry.eid\n"
393 " WHERE ldb_entry.eid = %lld\n"
394 " AND ldb_entry.eid IN\n%s\n"
396 " AND av.eid = entry.eid "
397 " ORDER BY av.eid, av.attr_name;",
403 case LDB_SCOPE_ONELEVEL:
404 pSql = sqlite3_mprintf(
405 "SELECT entry.eid,\n"
409 " FROM ldb_entry AS entry,\n"
410 " ldb_attribute_values AS av\n"
411 " WHERE entry.eid IN\n"
412 " (SELECT DISTINCT ldb_entry.eid\n"
413 " FROM ldb_entry AS pchild, "
415 " WHERE ldb_entry.eid = pchild.eid "
416 " AND pchild.peid = %lld "
417 " AND ldb_entry.eid IN\n%s\n"
419 " AND av.eid = entry.eid "
420 " ORDER BY av.eid, av.attr_name;",
427 /* Initially, we have no old eid */
431 * Prepare and execute the SQL statement. Loop allows retrying on
432 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
433 * requiring retrying the operation.
435 for (bLoop = TRUE; bLoop; ) {
437 /* Compile the SQL statement into sqlite virtual machine */
438 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
442 NULL)) == SQLITE_SCHEMA) {
444 } else if (ret != SQLITE_OK) {
449 /* Loop through the returned rows */
451 /* Get the next row */
452 if ((ret = sqlite3_step(pStmt)) == SQLITE_ROW) {
454 /* Get the values from this row */
455 eid = sqlite3_column_int64(pStmt, 0);
456 pDN = sqlite3_column_text(pStmt, 1);
457 pAttrName = sqlite3_column_text(pStmt, 2);
458 pAttrValue = sqlite3_column_text(pStmt, 3);
460 /* Add this row data to the return results */
461 #warning "finish returning the result set of the search here"
463 } while (ret == SQLITE_ROW);
465 if (ret == SQLITE_SCHEMA) {
466 (void) sqlite3_finalize(pStmt);
468 } else if (ret != SQLITE_DONE) {
469 (void) sqlite3_finalize(pStmt);
474 /* Free the virtual machine */
475 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
476 (void) sqlite3_finalize(pStmt);
478 } else if (ret != SQLITE_OK) {
479 (void) sqlite3_finalize(pStmt);
485 * Normal condition is only one time through loop. Loop is
486 * rerun in error conditions, via "continue", above.
493 /* End the transaction */
494 QUERY_NOROWS(lsqlite3, FALSE, "END TRANSACTION;");
496 /* We're alll done with this query */
505 lsqlite3_add(struct ldb_module *module,
506 const struct ldb_message *msg)
509 struct lsqlite3_private * lsqlite3 = module->private_data;
511 /* ignore ltdb specials */
512 if (msg->dn[0] == '@') {
516 /* Begin a transaction */
517 QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
520 * Build any portions of the directory tree that don't exist. If the
521 * final component already exists, it's an error.
523 if (new_dn(module, msg->dn, &eid) != 0) {
524 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
528 /* Add attributes to this new entry */
529 if (msg_to_sql(module, msg, eid, FALSE) != 0) {
530 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
534 /* Everything worked. Commit it! */
535 QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
540 /* modify a record */
542 lsqlite3_modify(struct ldb_module *module,
543 const struct ldb_message *msg)
545 struct lsqlite3_private * lsqlite3 = module->private_data;
547 /* ignore ltdb specials */
548 if (msg->dn[0] == '@') {
552 /* Begin a transaction */
553 QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
555 /* Everything worked. Commit it! */
556 QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
560 /* obtain a named lock */
562 lsqlite3_lock(struct ldb_module *module,
563 const char *lockname)
565 if (lockname == NULL) {
569 /* TODO implement a local locking mechanism here */
574 /* release a named lock */
576 lsqlite3_unlock(struct ldb_module *module,
577 const char *lockname)
579 if (lockname == NULL) {
583 /* TODO implement a local locking mechanism here */
588 /* return extended error information */
590 lsqlite3_errstring(struct ldb_module *module)
592 struct lsqlite3_private * lsqlite3 = module->private_data;
594 return sqlite3_errmsg(lsqlite3->sqlite);
605 initialize(struct lsqlite3_private *lsqlite3,
612 const char * schema =
613 "-- ------------------------------------------------------"
615 "PRAGMA auto_vacuum=1;"
617 "-- ------------------------------------------------------"
621 "-- ------------------------------------------------------"
623 "CREATE TABLE ldb_info AS"
624 " SELECT 'LDB' AS database_type,"
627 "-- ------------------------------------------------------"
631 " * The entry table holds the information about an entry. "
632 " * This table is used to obtain the EID of the entry and to "
633 " * support scope=one and scope=base. The parent and child"
634 " * table is included in the entry table since all the other"
635 " * attributes are dependent on EID."
637 "CREATE TABLE ldb_entry"
639 " -- Unique identifier of this LDB entry"
640 " eid INTEGER PRIMARY KEY,"
642 " -- Unique identifier of the parent LDB entry"
643 " peid INTEGER REFERENCES ldb_entry,"
645 " -- Distinguished name of this entry"
648 " -- Time when the entry was created"
649 " create_timestamp INTEGER,"
651 " -- Time when the entry was last modified"
652 " modify_timestamp INTEGER"
657 " * The purpose of the descendant table is to support the"
658 " * subtree search feature. For each LDB entry with a unique"
659 " * ID (AEID), this table contains the unique identifiers"
660 " * (DEID) of the descendant entries."
662 " * For evern entry in the directory, a row exists in this"
663 " * table for each of its ancestors including itself. The "
664 " * size of the table depends on the depth of each entry. In "
665 " * the worst case, if all the entries were at the same "
666 " * depth, the number of rows in the table is O(nm) where "
667 " * n is the number of nodes in the directory and m is the "
668 " * depth of the tree. "
670 "CREATE TABLE ldb_descendants"
672 " -- The unique identifier of the ancestor LDB entry"
673 " aeid INTEGER REFERENCES ldb_entry,"
675 " -- The unique identifier of the descendant LDB entry"
676 " deid INTEGER REFERENCES ldb_entry"
680 "CREATE TABLE ldb_object_classes"
682 " -- Object classes are inserted into this table to track"
683 " -- their class hierarchy. 'top' is the top-level class"
684 " -- of which all other classes are subclasses."
685 " class_name TEXT PRIMARY KEY,"
687 " -- tree_key tracks the position of the class in"
689 " tree_key TEXT UNIQUE"
693 " * We keep a full listing of attribute/value pairs here"
695 "CREATE TABLE ldb_attribute_values"
697 " eid INTEGER REFERENCES ldb_entry,"
698 " attr_name TEXT, -- see ldb_attr_ATTRIBUTE_NAME"
703 " * There is one attribute table per searchable attribute."
706 "CREATE TABLE ldb_attr_ATTRIBUTE_NAME"
708 " -- The unique identifier of the LDB entry"
709 " eid INTEGER REFERENCES ldb_entry,"
711 " -- Normalized attribute value"
717 "-- ------------------------------------------------------"
721 "-- ------------------------------------------------------"
724 "CREATE TRIGGER ldb_entry_insert_tr"
730 " SET create_timestamp = strftime('%s', 'now'),"
731 " modify_timestamp = strftime('%s', 'now')"
732 " WHERE eid = new.eid;"
735 "CREATE TRIGGER ldb_entry_update_tr"
741 " SET modify_timestamp = strftime('%s', 'now')"
742 " WHERE eid = old.eid;"
745 "-- ------------------------------------------------------"
746 "-- Table initialization"
748 "/* We need an implicit 'top' level object class */"
749 "INSERT INTO ldb_attributes (attr_name,"
753 "-- ------------------------------------------------------"
757 "-- ------------------------------------------------------"
760 /* Skip protocol indicator of url */
761 if (strncmp(url, "sqlite://", 9) != 0) {
762 return SQLITE_MISUSE;
765 /* Update pointer to just after the protocol indicator */
768 /* Try to open the (possibly empty/non-existent) database */
769 if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
773 /* Begin a transaction */
774 QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
776 /* Determine if this is a new database. No tables means it is. */
781 " FROM sqlite_master "
782 " WHERE type = 'table';");
786 * Create the database schema
788 for (pTail = discard_const_p(char, schema); pTail != NULL; ) {
790 if ((ret = sqlite3_prepare(
795 &pTail)) != SQLITE_OK ||
796 (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
797 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
799 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
800 (void) sqlite3_close(lsqlite3->sqlite);
806 * Ensure that the database we opened is one of ours
808 if (query_int(lsqlite3,
811 " (SELECT COUNT(*) = 3"
812 " FROM sqlite_master "
813 " WHERE type = 'table' "
817 " 'ldb_descendants', "
818 " 'ldb_object_classes' "
824 " WHERE database_type = 'LDB' "
825 " AND version = '1.0'"
829 /* It's not one that we created. See ya! */
830 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
831 (void) sqlite3_close(lsqlite3->sqlite);
832 return SQLITE_MISUSE;
836 /* Commit the transaction */
837 QUERY_NOROWS(lsqlite3, FALSE, "COMMIT;");
845 struct lsqlite3_private * lsqlite3 = p;
847 (void) sqlite3_close(lsqlite3->sqlite);
855 * This function is used for queries that are not expected to return any rows,
856 * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
857 * There are no provisions here for returning data from rows in a table, so do
858 * not pass SELECT queries to this function.
861 query_norows(const struct lsqlite3_private *lsqlite3,
869 sqlite3_stmt * pStmt;
872 /* Begin access to variable argument list */
873 va_start(args, pSql);
875 /* Format the query */
876 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
881 * Prepare and execute the SQL statement. Loop allows retrying on
882 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
883 * requiring retrying the operation.
885 for (bLoop = TRUE; bLoop; ) {
887 /* Compile the SQL statement into sqlite virtual machine */
888 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
892 &pTail)) == SQLITE_SCHEMA) {
894 } else if (ret != SQLITE_OK) {
899 /* No rows expected, so just step through machine code once */
900 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
901 (void) sqlite3_finalize(pStmt);
903 } else if (ret != SQLITE_DONE) {
904 (void) sqlite3_finalize(pStmt);
909 /* Free the virtual machine */
910 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
911 (void) sqlite3_finalize(pStmt);
913 } else if (ret != SQLITE_OK) {
914 (void) sqlite3_finalize(pStmt);
920 * Normal condition is only one time through loop. Loop is
921 * rerun in error conditions, via "continue", above.
927 /* All done with variable argument list */
930 /* Free the memory we allocated for our query string */
940 * This function is used for the common case of queries that return a single
943 * NOTE: If more than one value is returned by the query, all but the first
944 * one will be ignored.
947 query_int(const struct lsqlite3_private * lsqlite3,
956 sqlite3_stmt * pStmt;
959 /* Begin access to variable argument list */
960 va_start(args, pSql);
962 /* Format the query */
963 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
968 * Prepare and execute the SQL statement. Loop allows retrying on
969 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
970 * requiring retrying the operation.
972 for (bLoop = TRUE; bLoop; ) {
974 /* Compile the SQL statement into sqlite virtual machine */
975 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
979 &pTail)) == SQLITE_SCHEMA) {
981 } else if (ret != SQLITE_OK) {
986 /* One row expected */
987 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
988 (void) sqlite3_finalize(pStmt);
990 } else if (ret != SQLITE_ROW) {
991 (void) sqlite3_finalize(pStmt);
996 /* Get the value to be returned */
997 *pRet = sqlite3_column_int64(pStmt, 0);
999 /* Free the virtual machine */
1000 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1001 (void) sqlite3_finalize(pStmt);
1003 } else if (ret != SQLITE_OK) {
1004 (void) sqlite3_finalize(pStmt);
1010 * Normal condition is only one time through loop. Loop is
1011 * rerun in error conditions, via "continue", above.
1017 /* All done with variable argument list */
1020 /* Free the memory we allocated for our query string */
1028 callback function used in call to ldb_dn_fold() for determining whether an
1029 attribute type requires case folding.
1032 case_fold_attr_required(void * hUserData,
1035 // struct ldb_module * module = hUserData;
1037 #warning "currently, all attributes require case folding"
1043 * add a single set of ldap message values to a ldb_message
1046 #warning "add_msg_attr() not yet implemented or used"
1049 add_msg_attr(struct ldb_context *ldb,
1050 struct ldb_message *msg,
1052 struct berval **bval)
1056 struct ldb_message_element * el;
1058 count = ldap_count_values_len(bval);
1064 el = talloc_realloc(msg, msg->elements, struct ldb_message_element,
1065 msg->num_elements + 1);
1073 el = &msg->elements[msg->num_elements];
1075 el->name = talloc_strdup(msg->elements, attr);
1083 el->values = talloc_array(msg->elements, struct ldb_val, count);
1089 for (i=0;i<count;i++) {
1090 el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
1091 if (!el->values[i].data) {
1094 el->values[i].length = bval[i]->bv_len;
1098 msg->num_elements++;
1105 parsetree_to_sql(struct ldb_module *module,
1107 const struct ldb_parse_tree *t)
1116 switch(t->operation) {
1121 ret = parsetree_to_sql(module,
1123 t->u.list.elements[0]);
1125 for (i = 1; i < t->u.list.num_elements; i++) {
1130 t->u.list.elements[i]);
1131 ret = talloc_asprintf_append(ret,
1139 ret = talloc_asprintf("(\n"
1151 t->u.list.elements[0]);
1153 for (i = 1; i < t->u.list.num_elements; i++) {
1158 t->u.list.elements[i]);
1159 ret = talloc_asprintf_append(ret,
1166 ret = talloc_asprintf("(\n"
1179 ret = talloc_asprintf(hTalloc,
1183 " WHERE eid NOT IN %s\n"
1190 /* should never occur */
1194 /* Get a case-folded copy of the attribute name */
1195 pAttrName = ldb_casefold((struct ldb_context *) module,
1199 * For simple searches, we want to retrieve the list of EIDs that
1200 * match the criteria. We accomplish this by searching the
1201 * appropriate table, ldb_attr_<attributeName>, for the eid
1202 * corresponding to all matching values.
1204 if (t->u.simple.value.length == 1 &&
1205 (*(const char *) t->u.simple.value.data) == '*') {
1207 * Special case for "attr_name=*". In this case, we want the
1208 * eid corresponding to all values in the specified attribute
1211 if ((p = sqlite3_mprintf("(\n"
1213 " FROM ldb_attr_%q\n"
1215 pAttrName)) == NULL) {
1219 ret = talloc_strdup(hTalloc, p);
1222 } else if (strcasecmp(t->u.simple.attr, "objectclass") == 0) {
1224 * For object classes, we want to search for all objectclasses
1225 * that are subclasses as well.
1227 if ((p = sqlite3_mprintf(
1230 " FROM ldb_attr_objectclass\n"
1231 " WHERE attr_name IN\n"
1232 " (SELECT class_name\n"
1233 " FROM ldb_objectclasses\n"
1234 " WHERE tree_key GLOB\n"
1235 " (SELECT tree_key\n"
1236 " FROM ldb_objectclasses\n"
1237 " WHERE class_name = %Q) || '*')\n"
1239 t->u.simple.value.data)) == NULL) {
1243 ret = talloc_strdup(hTalloc, p);
1247 /* A normal query. */
1248 if ((p = sqlite3_mprintf("(\n"
1250 " FROM ldb_attr_%q\n"
1251 " WHERE attr_value = %Q\n"
1254 t->u.simple.value.data)) == NULL) {
1258 ret = talloc_strdup(hTalloc, p);
1266 parsetree_to_tablelist(struct ldb_module *module,
1268 const struct ldb_parse_tree *t)
1270 #warning "obtain talloc'ed array of attribute names for table list"
1276 * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
1277 * requests in the ldb_message
1280 msg_to_sql(struct ldb_module * module,
1281 const struct ldb_message * msg,
1289 struct lsqlite3_private * lsqlite3 = module->private_data;
1291 for (i = 0; i < msg->num_elements; i++) {
1292 const struct ldb_message_element *el = &msg->elements[i];
1295 flags = LDB_FLAG_MOD_ADD;
1297 flags = el->flags & LDB_FLAG_MOD_MASK;
1300 /* Get a case-folded copy of the attribute name */
1301 pAttrName = ldb_casefold((struct ldb_context *) module,
1304 if (flags == LDB_FLAG_MOD_ADD) {
1305 /* Create the attribute table if it doesn't exist */
1306 if (new_attr(module, pAttrName) != 0) {
1311 /* For each value of the specified attribute name... */
1312 for (j = 0; j < el->num_values; j++) {
1314 /* ... bind the attribute value, if necessary */
1316 case LDB_FLAG_MOD_ADD:
1317 QUERY_NOROWS(lsqlite3,
1319 "INSERT INTO ldb_attr_%q "
1320 " (eid, attr_value) "
1324 eid, el->values[j].data);
1325 QUERY_NOROWS(lsqlite3,
1328 " SET entry_data = "
1329 " add_attr(entry_data, "
1331 " WHERE eid = %lld;",
1332 el->name, el->values[j].data,
1337 case LDB_FLAG_MOD_REPLACE:
1338 QUERY_NOROWS(lsqlite3,
1340 "UPDATE ldb_attr_%q "
1341 " SET attr_value = %Q "
1342 " WHERE eid = %lld;",
1346 QUERY_NOROWS(lsqlite3,
1349 " SET entry_data = "
1350 " mod_attr(entry_data, "
1352 " WHERE eid = %lld;",
1353 el->name, el->values[j].data,
1357 case LDB_FLAG_MOD_DELETE:
1358 /* No additional parameters to this query */
1359 QUERY_NOROWS(lsqlite3,
1361 "DELETE FROM ldb_attr_%q "
1362 " WHERE eid = %lld "
1363 " AND attr_value = %Q;",
1366 el->values[j].data);
1367 QUERY_NOROWS(lsqlite3,
1370 " SET entry_data = "
1371 " del_attr(entry_data, "
1373 " WHERE eid = %lld;",
1374 el->name, el->values[j].data,
1387 new_dn(struct ldb_module * module,
1396 struct ldb_dn * pExplodedDN;
1397 struct ldb_dn_component * pComponent;
1398 struct ldb_context * ldb = module->ldb;
1399 struct lsqlite3_private * lsqlite3 = module->private_data;
1401 /* Explode and normalize the DN */
1406 case_fold_attr_required)) == NULL) {
1410 /* Allocate a string to hold the partial DN of each component */
1411 if ((pPartialDN = talloc_strdup(ldb, "")) == NULL) {
1415 /* For each component of the DN (starting with the last one)... */
1417 for (nComponent = pExplodedDN->comp_num - 1, bFirst = TRUE;
1419 nComponent--, bFirst = FALSE) {
1421 /* Point to the component */
1422 pComponent = pExplodedDN->components[nComponent];
1424 /* Add this component on to the partial DN to date */
1425 if ((p = talloc_asprintf(ldb,
1427 pComponent->component,
1429 pPartialDN)) == NULL) {
1433 /* No need for the old partial DN any more */
1434 talloc_free(pPartialDN);
1436 /* Save the new partial DN */
1440 * Ensure that an entry is in the ldb_entry table for this
1441 * component. Any component other than the last one
1442 * (component 0) may already exist. It is an error if
1443 * component 0 (the full DN requested to be be inserted)
1447 /* This is a top-level entry. Parent EID is null. */
1448 QUERY_NOROWS(lsqlite3,
1450 "INSERT %s INTO ldb_entry "
1454 nComponent == 0 ? "" : "OR IGNORE",
1457 QUERY_NOROWS(lsqlite3,
1459 "INSERT %s INTO ldb_entry "
1463 nComponent == 0 ? "" : "OR IGNORE",
1467 /* Get the EID of the just inserted row (the next parent) */
1468 eid = sqlite3_last_insert_rowid(lsqlite3->sqlite);
1471 /* Give 'em what they came for! */
1479 new_attr(struct ldb_module * module,
1483 struct lsqlite3_private * lsqlite3 = module->private_data;
1487 * pAttrName is assumed to already be case-folded here!
1490 /* See if the table already exists */
1494 "SELECT COUNT(*) <> 0"
1495 " FROM sqlite_master "
1496 " WHERE type = 'table' "
1497 " AND tbl_name = %Q;",
1502 /* Nope. Create the table */
1503 QUERY_NOROWS(lsqlite3,
1505 "CREATE TABLE ldb_attr_%q "
1507 " eid INTEGER REFERENCES ldb_entry, "