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 */
296 /* search for matching records */
298 lsqlite3_search(struct ldb_module
* module
,
299 const char * pBaseDN
,
300 enum ldb_scope scope
,
301 const char * pExpression
,
302 const char * const attrs
[],
303 struct ldb_message
*** res
)
307 char * sql_constraints
;
310 struct ldb_parse_tree
* pTree
;
311 struct lsqlite3_private
* lsqlite3
= module
->private_data
;
313 if (pBaseDN
== NULL
) {
317 /* Begin a transaction */
318 QUERY_NOROWS(lsqlite3
, FALSE
, "BEGIN IMMEDIATE;");
321 * Obtain the eid of the base DN
328 " WHERE attr_value = %Q;",
331 /* Parse the filter expression into a tree we can work with */
332 if ((pTree
= ldb_parse_tree(module
->ldb
, pExpression
)) == NULL
) {
336 /* Allocate a temporary talloc context */
337 hTalloc
= talloc_new(module
->ldb
);
339 /* Move the parse tree to our temporary context */
340 talloc_steal(hTalloc
, pTree
);
342 /* Convert filter into a series of SQL statements (constraints) */
343 sql_constraints
= parsetree_to_sql(module
, hTalloc
, pTree
);
345 /* Get the list of attribute names to use as our extra table list */
346 table_list
= parsetree_to_tablelist(module
, hTalloc
, pTree
);
349 case LDB_SCOPE_DEFAULT
:
350 case LDB_SCOPE_SUBTREE
:
351 sql
= sqlite3_mprintf(
352 "SELECT entry.entry_data\n"
353 " FROM ldb_entry AS entry\n"
354 " WHERE entry.eid IN\n"
355 " (SELECT DISTINCT ldb_entry.eid\n"
357 " ldb_descendants,\n"
359 " WHERE ldb_descendants.aeid = %lld\n"
360 " AND ldb_entry.eid = ldb_descendants.deid\n"
361 " AND ldap_entry.eid IN\n"
370 sql
= sqlite3_mprintf(
371 "SELECT entry.entry_data\n"
372 " FROM ldb_entry AS entry\n"
373 " WHERE entry.eid IN\n"
374 " (SELECT DISTINCT ldb_entry.eid\n"
376 " WHERE ldb_entry.eid = %lld\n"
377 " AND ldb_entry.eid IN\n"
385 case LDB_SCOPE_ONELEVEL
:
386 sql
= sqlite3_mprintf(
387 "SELECT entry.entry_data\n"
388 " FROM ldb_entry AS entry\n"
389 " WHERE entry.eid IN\n"
390 " (SELECT DISTINCT ldb_entry.eid\n"
391 " FROM ldb_entry AS pchild, "
393 " WHERE ldb_entry.eid = pchild.eid "
394 " AND pchild.peid = %lld "
395 " AND ldb_entry.eid IN\n"
404 #warning "retrieve and return the result set of the search here"
406 /* End the transaction */
407 QUERY_NOROWS(lsqlite3
, FALSE
, "END TRANSACTION;");
415 lsqlite3_add(struct ldb_module
*module
,
416 const struct ldb_message
*msg
)
419 struct lsqlite3_private
* lsqlite3
= module
->private_data
;
421 /* ignore ltdb specials */
422 if (msg
->dn
[0] == '@') {
426 /* Begin a transaction */
427 QUERY_NOROWS(lsqlite3
, FALSE
, "BEGIN EXCLUSIVE;");
430 * Build any portions of the directory tree that don't exist. If the
431 * final component already exists, it's an error.
433 if (new_dn(module
, msg
->dn
, &eid
) != 0) {
434 QUERY_NOROWS(lsqlite3
, FALSE
, "ROLLBACK;");
438 /* Add attributes to this new entry */
439 if (msg_to_sql(module
, msg
, eid
, FALSE
) != 0) {
440 QUERY_NOROWS(lsqlite3
, FALSE
, "ROLLBACK;");
444 /* Everything worked. Commit it! */
445 QUERY_NOROWS(lsqlite3
, TRUE
, "COMMIT;");
450 /* modify a record */
452 lsqlite3_modify(struct ldb_module
*module
,
453 const struct ldb_message
*msg
)
455 struct lsqlite3_private
* lsqlite3
= module
->private_data
;
457 /* ignore ltdb specials */
458 if (msg
->dn
[0] == '@') {
462 /* Begin a transaction */
463 QUERY_NOROWS(lsqlite3
, FALSE
, "BEGIN EXCLUSIVE;");
465 /* Everything worked. Commit it! */
466 QUERY_NOROWS(lsqlite3
, TRUE
, "COMMIT;");
470 /* obtain a named lock */
472 lsqlite3_lock(struct ldb_module
*module
,
473 const char *lockname
)
475 if (lockname
== NULL
) {
479 /* TODO implement a local locking mechanism here */
484 /* release a named lock */
486 lsqlite3_unlock(struct ldb_module
*module
,
487 const char *lockname
)
489 if (lockname
== NULL
) {
493 /* TODO implement a local locking mechanism here */
498 /* return extended error information */
500 lsqlite3_errstring(struct ldb_module
*module
)
502 struct lsqlite3_private
* lsqlite3
= module
->private_data
;
504 return sqlite3_errmsg(lsqlite3
->sqlite
);
515 initialize(struct lsqlite3_private
*lsqlite3
,
522 const char * schema
=
523 "-- ------------------------------------------------------"
525 "PRAGMA auto_vacuum=1;"
527 "-- ------------------------------------------------------"
531 "-- ------------------------------------------------------"
533 "CREATE TABLE ldb_info AS"
534 " SELECT 'LDB' AS database_type,"
537 "-- ------------------------------------------------------"
541 " * The entry table holds the information about an entry. "
542 " * This table is used to obtain the EID of the entry and to "
543 " * support scope=one and scope=base. The parent and child"
544 " * table is included in the entry table since all the other"
545 " * attributes are dependent on EID."
547 "CREATE TABLE ldb_entry"
549 " -- Unique identifier of this LDB entry"
550 " eid INTEGER PRIMARY KEY,"
552 " -- Unique identifier of the parent LDB entry"
553 " peid INTEGER REFERENCES ldb_entry,"
555 " -- Distinguished name of this entry"
558 " -- Time when the entry was created"
559 " create_timestamp INTEGER,"
561 " -- Time when the entry was last modified"
562 " modify_timestamp INTEGER,"
564 " -- Attributes of this entry, in the form"
565 " -- attr\1value\0[attr\1value\0]*\0"
571 " * The purpose of the descendant table is to support the"
572 " * subtree search feature. For each LDB entry with a unique"
573 " * ID (AEID), this table contains the unique identifiers"
574 " * (DEID) of the descendant entries."
576 " * For evern entry in the directory, a row exists in this"
577 " * table for each of its ancestors including itself. The "
578 " * size of the table depends on the depth of each entry. In "
579 " * the worst case, if all the entries were at the same "
580 " * depth, the number of rows in the table is O(nm) where "
581 " * n is the number of nodes in the directory and m is the "
582 " * depth of the tree. "
584 "CREATE TABLE ldb_descendants"
586 " -- The unique identifier of the ancestor LDB entry"
587 " aeid INTEGER REFERENCES ldb_entry,"
589 " -- The unique identifier of the descendant LDB entry"
590 " deid INTEGER REFERENCES ldb_entry"
594 "CREATE TABLE ldb_object_classes"
596 " -- Object classes are inserted into this table to track"
597 " -- their class hierarchy. 'top' is the top-level class"
598 " -- of which all other classes are subclasses."
599 " class_name TEXT PRIMARY KEY,"
601 " -- tree_key tracks the position of the class in"
603 " tree_key TEXT UNIQUE"
607 " * There is one attribute table per searchable attribute."
610 "CREATE TABLE ldb_attr_ATTRIBUTE_NAME"
612 " -- The unique identifier of the LDB entry"
613 " eid INTEGER REFERENCES ldb_entry,"
615 " -- Normalized attribute value"
621 "-- ------------------------------------------------------"
625 "-- ------------------------------------------------------"
628 "CREATE TRIGGER ldb_entry_insert_tr"
634 " SET create_timestamp = strftime('%s', 'now'),"
635 " modify_timestamp = strftime('%s', 'now')"
636 " WHERE eid = new.eid;"
639 "CREATE TRIGGER ldb_entry_update_tr"
645 " SET modify_timestamp = strftime('%s', 'now')"
646 " WHERE eid = old.eid;"
649 "-- ------------------------------------------------------"
650 "-- Table initialization"
652 "/* We need an implicit 'top' level object class */"
653 "INSERT INTO ldb_attributes (attr_name,"
657 "-- ------------------------------------------------------"
661 "-- ------------------------------------------------------"
664 /* Skip protocol indicator of url */
665 if (strncmp(url
, "sqlite://", 9) != 0) {
666 return SQLITE_MISUSE
;
669 /* Update pointer to just after the protocol indicator */
672 /* Try to open the (possibly empty/non-existent) database */
673 if ((ret
= sqlite3_open(url
, &lsqlite3
->sqlite
)) != SQLITE_OK
) {
677 /* Begin a transaction */
678 QUERY_NOROWS(lsqlite3
, FALSE
, "BEGIN EXCLUSIVE;");
680 /* Determine if this is a new database. No tables means it is. */
685 " FROM sqlite_master "
686 " WHERE type = 'table';");
690 * Create the database schema
692 for (pTail
= discard_const_p(char, schema
); pTail
!= NULL
; ) {
694 if ((ret
= sqlite3_prepare(
699 &pTail
)) != SQLITE_OK
||
700 (ret
= sqlite3_step(stmt
)) != SQLITE_DONE
||
701 (ret
= sqlite3_finalize(stmt
)) != SQLITE_OK
) {
703 QUERY_NOROWS(lsqlite3
, FALSE
, "ROLLBACK;");
704 (void) sqlite3_close(lsqlite3
->sqlite
);
710 * Ensure that the database we opened is one of ours
712 if (query_int(lsqlite3
,
715 " (SELECT COUNT(*) = 3"
716 " FROM sqlite_master "
717 " WHERE type = 'table' "
721 " 'ldb_descendants', "
722 " 'ldb_object_classes' "
728 " WHERE database_type = 'LDB' "
729 " AND version = '1.0'"
733 /* It's not one that we created. See ya! */
734 QUERY_NOROWS(lsqlite3
, FALSE
, "ROLLBACK;");
735 (void) sqlite3_close(lsqlite3
->sqlite
);
736 return SQLITE_MISUSE
;
740 /* Commit the transaction */
741 QUERY_NOROWS(lsqlite3
, FALSE
, "COMMIT;");
749 struct lsqlite3_private
* lsqlite3
= p
;
751 (void) sqlite3_close(lsqlite3
->sqlite
);
759 * This function is used for queries that are not expected to return any rows,
760 * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
761 * There are no provisions here for returning data from rows in a table, so do
762 * not pass SELECT queries to this function.
765 query_norows(const struct lsqlite3_private
*lsqlite3
,
773 sqlite3_stmt
* pStmt
;
776 /* Begin access to variable argument list */
777 va_start(args
, pSql
);
779 /* Format the query */
780 if ((p
= sqlite3_vmprintf(pSql
, args
)) == NULL
) {
785 * Prepare and execute the SQL statement. Loop allows retrying on
786 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
787 * requiring retrying the operation.
789 for (bLoop
= TRUE
; bLoop
; ) {
791 /* Compile the SQL statement into sqlite virtual machine */
792 if ((ret
= sqlite3_prepare(lsqlite3
->sqlite
,
796 &pTail
)) != SQLITE_OK
) {
801 /* No rows expected, so just step through machine code once */
802 if ((ret
= sqlite3_step(pStmt
)) == SQLITE_SCHEMA
) {
803 (void) sqlite3_finalize(pStmt
);
805 } else if (ret
!= SQLITE_DONE
) {
806 (void) sqlite3_finalize(pStmt
);
811 /* Free the virtual machine */
812 if ((ret
= sqlite3_finalize(pStmt
)) == SQLITE_SCHEMA
) {
813 (void) sqlite3_finalize(pStmt
);
815 } else if (ret
!= SQLITE_OK
) {
816 (void) sqlite3_finalize(pStmt
);
822 * Normal condition is only one time through loop. Loop is
823 * rerun in error conditions, via "continue", above.
829 /* All done with variable argument list */
832 /* Free the memory we allocated for our query string */
842 * This function is used for the common case of queries that return a single
845 * NOTE: If more than one value is returned by the query, all but the first
846 * one will be ignored.
849 query_int(const struct lsqlite3_private
* lsqlite3
,
858 sqlite3_stmt
* pStmt
;
861 /* Begin access to variable argument list */
862 va_start(args
, pSql
);
864 /* Format the query */
865 if ((p
= sqlite3_vmprintf(pSql
, args
)) == NULL
) {
870 * Prepare and execute the SQL statement. Loop allows retrying on
871 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
872 * requiring retrying the operation.
874 for (bLoop
= TRUE
; bLoop
; ) {
876 /* Compile the SQL statement into sqlite virtual machine */
877 if ((ret
= sqlite3_prepare(lsqlite3
->sqlite
,
881 &pTail
)) != SQLITE_OK
) {
886 /* No rows expected, so just step through machine code once */
887 if ((ret
= sqlite3_step(pStmt
)) == SQLITE_SCHEMA
) {
888 (void) sqlite3_finalize(pStmt
);
890 } else if (ret
!= SQLITE_ROW
) {
891 (void) sqlite3_finalize(pStmt
);
896 /* Get the value to be returned */
897 *pRet
= sqlite3_column_int64(pStmt
, 0);
899 /* Free the virtual machine */
900 if ((ret
= sqlite3_finalize(pStmt
)) == SQLITE_SCHEMA
) {
901 (void) sqlite3_finalize(pStmt
);
903 } else if (ret
!= SQLITE_OK
) {
904 (void) sqlite3_finalize(pStmt
);
910 * Normal condition is only one time through loop. Loop is
911 * rerun in error conditions, via "continue", above.
917 /* All done with variable argument list */
920 /* Free the memory we allocated for our query string */
928 callback function used in call to ldb_dn_fold() for determining whether an
929 attribute type requires case folding.
932 case_fold_attr_required(void * hUserData
,
935 // struct ldb_module * module = hUserData;
937 #warning "currently, all attributes require case folding"
943 * add a single set of ldap message values to a ldb_message
946 #warning "add_msg_attr() not yet implemented or used"
949 add_msg_attr(struct ldb_context
*ldb
,
950 struct ldb_message
*msg
,
952 struct berval
**bval
)
956 struct ldb_message_element
* el
;
958 count
= ldap_count_values_len(bval
);
964 el
= talloc_realloc(msg
, msg
->elements
, struct ldb_message_element
,
965 msg
->num_elements
+ 1);
973 el
= &msg
->elements
[msg
->num_elements
];
975 el
->name
= talloc_strdup(msg
->elements
, attr
);
983 el
->values
= talloc_array(msg
->elements
, struct ldb_val
, count
);
989 for (i
=0;i
<count
;i
++) {
990 el
->values
[i
].data
= talloc_memdup(el
->values
, bval
[i
]->bv_val
, bval
[i
]->bv_len
);
991 if (!el
->values
[i
].data
) {
994 el
->values
[i
].length
= bval
[i
]->bv_len
;
1005 parsetree_to_sql(struct ldb_module
*module
,
1007 const struct ldb_parse_tree
*t
)
1016 switch(t
->operation
) {
1021 ret
= parsetree_to_sql(module
,
1023 t
->u
.list
.elements
[0]);
1025 for (i
= 1; i
< t
->u
.list
.num_elements
; i
++) {
1030 t
->u
.list
.elements
[i
]);
1031 ret
= talloc_asprintf_append(ret
,
1039 ret
= talloc_asprintf("(\n"
1051 t
->u
.list
.elements
[0]);
1053 for (i
= 1; i
< t
->u
.list
.num_elements
; i
++) {
1058 t
->u
.list
.elements
[i
]);
1059 ret
= talloc_asprintf_append(ret
,
1066 ret
= talloc_asprintf("(\n"
1079 ret
= talloc_asprintf(hTalloc
,
1083 " WHERE eid NOT IN %s\n"
1090 /* should never occur */
1094 /* Get a case-folded copy of the attribute name */
1095 pAttrName
= ldb_casefold((struct ldb_context
*) module
,
1099 * For simple searches, we want to retrieve the list of EIDs that
1100 * match the criteria. We accomplish this by searching the
1101 * appropriate table, ldb_attr_<attributeName>, for the eid
1102 * corresponding to all matching values.
1104 if (t
->u
.simple
.value
.length
== 1 &&
1105 (*(const char *) t
->u
.simple
.value
.data
) == '*') {
1107 * Special case for "attr_name=*". In this case, we want the
1108 * eid corresponding to all values in the specified attribute
1111 if ((p
= sqlite3_mprintf("(\n"
1113 " FROM ldb_attr_%q\n"
1115 pAttrName
)) == NULL
) {
1119 ret
= talloc_strdup(hTalloc
, p
);
1122 } else if (strcasecmp(t
->u
.simple
.attr
, "objectclass") == 0) {
1124 * For object classes, we want to search for all objectclasses
1125 * that are subclasses as well.
1127 if ((p
= sqlite3_mprintf(
1130 " FROM ldb_attr_objectclass\n"
1131 " WHERE attr_name IN\n"
1132 " (SELECT class_name\n"
1133 " FROM ldb_objectclasses\n"
1134 " WHERE tree_key GLOB\n"
1135 " (SELECT tree_key\n"
1136 " FROM ldb_objectclasses\n"
1137 " WHERE class_name = %Q) || '*')\n"
1139 t
->u
.simple
.value
.data
)) == NULL
) {
1143 ret
= talloc_strdup(hTalloc
, p
);
1147 /* A normal query. */
1148 if ((p
= sqlite3_mprintf("(\n"
1150 " FROM ldb_attr_%q\n"
1151 " WHERE attr_value = %Q\n"
1154 t
->u
.simple
.value
.data
)) == NULL
) {
1158 ret
= talloc_strdup(hTalloc
, p
);
1166 parsetree_to_tablelist(struct ldb_module
*module
,
1168 const struct ldb_parse_tree
*t
)
1170 #warning "obtain talloc'ed array of attribute names for table list"
1176 * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
1177 * requests in the ldb_message
1180 msg_to_sql(struct ldb_module
* module
,
1181 const struct ldb_message
* msg
,
1189 struct lsqlite3_private
* lsqlite3
= module
->private_data
;
1191 for (i
= 0; i
< msg
->num_elements
; i
++) {
1192 const struct ldb_message_element
*el
= &msg
->elements
[i
];
1195 flags
= LDB_FLAG_MOD_ADD
;
1197 flags
= el
->flags
& LDB_FLAG_MOD_MASK
;
1200 /* Get a case-folded copy of the attribute name */
1201 pAttrName
= ldb_casefold((struct ldb_context
*) module
,
1204 if (flags
== LDB_FLAG_MOD_ADD
) {
1205 /* Create the attribute table if it doesn't exist */
1206 if (new_attr(module
, pAttrName
) != 0) {
1211 /* For each value of the specified attribute name... */
1212 for (j
= 0; j
< el
->num_values
; j
++) {
1214 /* ... bind the attribute value, if necessary */
1216 case LDB_FLAG_MOD_ADD
:
1217 QUERY_NOROWS(lsqlite3
,
1219 "INSERT INTO ldb_attr_%q "
1220 " (eid, attr_value) "
1224 eid
, el
->values
[j
].data
);
1225 QUERY_NOROWS(lsqlite3
,
1228 " SET entry_data = "
1229 " add_attr(entry_data, "
1231 " WHERE eid = %lld;",
1232 el
->name
, el
->values
[j
].data
,
1237 case LDB_FLAG_MOD_REPLACE
:
1238 QUERY_NOROWS(lsqlite3
,
1240 "UPDATE ldb_attr_%q "
1241 " SET attr_value = %Q "
1242 " WHERE eid = %lld;",
1246 QUERY_NOROWS(lsqlite3
,
1249 " SET entry_data = "
1250 " mod_attr(entry_data, "
1252 " WHERE eid = %lld;",
1253 el
->name
, el
->values
[j
].data
,
1257 case LDB_FLAG_MOD_DELETE
:
1258 /* No additional parameters to this query */
1259 QUERY_NOROWS(lsqlite3
,
1261 "DELETE FROM ldb_attr_%q "
1262 " WHERE eid = %lld "
1263 " AND attr_value = %Q;",
1266 el
->values
[j
].data
);
1267 QUERY_NOROWS(lsqlite3
,
1270 " SET entry_data = "
1271 " del_attr(entry_data, "
1273 " WHERE eid = %lld;",
1274 el
->name
, el
->values
[j
].data
,
1287 new_dn(struct ldb_module
* module
,
1296 struct ldb_dn
* pExplodedDN
;
1297 struct ldb_dn_component
* pComponent
;
1298 struct ldb_context
* ldb
= module
->ldb
;
1299 struct lsqlite3_private
* lsqlite3
= module
->private_data
;
1301 /* Explode and normalize the DN */
1306 case_fold_attr_required
)) == NULL
) {
1310 /* Allocate a string to hold the partial DN of each component */
1311 if ((pPartialDN
= talloc_strdup(ldb
, "")) == NULL
) {
1315 /* For each component of the DN (starting with the last one)... */
1317 for (nComponent
= pExplodedDN
->comp_num
- 1, bFirst
= TRUE
;
1319 nComponent
--, bFirst
= FALSE
) {
1321 /* Point to the component */
1322 pComponent
= pExplodedDN
->components
[nComponent
];
1324 /* Add this component on to the partial DN to date */
1325 if ((p
= talloc_asprintf(ldb
,
1327 pComponent
->component
,
1329 pPartialDN
)) == NULL
) {
1333 /* No need for the old partial DN any more */
1334 talloc_free(pPartialDN
);
1336 /* Save the new partial DN */
1340 * Ensure that an entry is in the ldb_entry table for this
1341 * component. Any component other than the last one
1342 * (component 0) may already exist. It is an error if
1343 * component 0 (the full DN requested to be be inserted)
1347 /* This is a top-level entry. Parent EID is null. */
1348 QUERY_NOROWS(lsqlite3
,
1350 "INSERT %s INTO ldb_entry "
1354 nComponent
== 0 ? "" : "OR IGNORE",
1357 QUERY_NOROWS(lsqlite3
,
1359 "INSERT %s INTO ldb_entry "
1363 nComponent
== 0 ? "" : "OR IGNORE",
1367 /* Get the EID of the just inserted row (the next parent) */
1368 eid
= sqlite3_last_insert_rowid(lsqlite3
->sqlite
);
1371 /* Give 'em what they came for! */
1379 new_attr(struct ldb_module
* module
,
1383 struct lsqlite3_private
* lsqlite3
= module
->private_data
;
1387 * pAttrName is assumed to already be case-folded here!
1390 /* See if the table already exists */
1394 "SELECT COUNT(*) <> 0"
1395 " FROM sqlite_master "
1396 " WHERE type = 'table' "
1397 " AND tbl_name = %Q;",
1402 /* Nope. Create the table */
1403 QUERY_NOROWS(lsqlite3
,
1405 "CREATE TABLE ldb_attr_%q "
1407 " eid INTEGER REFERENCES ldb_entry, "