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_explode_dn.h"
40 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
43 * Macros used throughout
48 # define TRUE (! FALSE)
51 #define FILTER_ATTR_TABLE "temp_filter_attrs"
52 #define RESULT_ATTR_TABLE "temp_result_attrs"
54 //#define TEMPTAB /* for testing, create non-temporary table */
55 #define TEMPTAB "TEMPORARY"
60 # define LOCK_DB(mod, name) lsqlite3_lock(mod, name)
61 # define UNLOCK_DB(mod, name) lsqlite3_unlock(mod, name)
63 # define LOCK_DB(mod, name) lock_debug(mod, name, __FILE__, __LINE__)
64 # define UNLOCK_DB(mod, name) unlock_debug(mod, name, __FILE__, __LINE__)
67 #define QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...) \
69 if (query_norows(lsqlite3, sql) != 0) { \
70 if (bRollbackOnError) { \
71 UNLOCK_DB(module, "rollback"); \
77 #define QUERY_INT(lsqlite3, result_var, bRollbackOnError, sql...) \
79 if (query_int(lsqlite3, &result_var, sql) != 0) { \
80 if (bRollbackOnError) { \
81 UNLOCK_DB(module, "rollback"); \
88 #define SQLITE3_DEBUG_QUERY (1 << 0)
89 #define SQLITE3_DEBUG_INIT (1 << 1)
90 #define SQLITE3_DEBUG_ADD (1 << 2)
91 #define SQLITE3_DEBUG_NEWDN (1 << 3)
92 #define SQLITE3_DEBUG_SEARCH (1 << 4)
97 static int lsqlite3_debug = FALSE;
100 * Forward declarations
103 lsqlite3_rename(struct ldb_module * module,
108 lsqlite3_delete(struct ldb_module *module,
112 lsqlite3_search_bytree(struct ldb_module * module,
113 const char * pBaseDN,
114 enum ldb_scope scope,
115 struct ldb_parse_tree * pTree,
116 const char * const * attrs,
117 struct ldb_message *** pppRes);
120 lsqlite3_search(struct ldb_module * module,
121 const char * pBaseDN,
122 enum ldb_scope scope,
123 const char * pExpression,
124 const char * const attrs[],
125 struct ldb_message *** pppRes);
128 lsqlite3_add(struct ldb_module *module,
129 const struct ldb_message *msg);
132 lsqlite3_modify(struct ldb_module *module,
133 const struct ldb_message *msg);
136 lsqlite3_lock(struct ldb_module *module,
137 const char *lockname);
140 lsqlite3_unlock(struct ldb_module *module,
141 const char *lockname);
144 lsqlite3_errstring(struct ldb_module *module);
147 initialize(struct lsqlite3_private *lsqlite3,
154 query_norows(const struct lsqlite3_private *lsqlite3,
159 query_int(const struct lsqlite3_private * lsqlite3,
164 static int case_fold_attr_required(void * hUserData,
167 static int case_fold_attr_not_required(void * hUserData,
171 add_msg_attr(void * hTalloc,
174 const char * pAttrName,
175 const char * pAttrValue,
178 struct ldb_message *** pppRes);
181 parsetree_to_sql(struct ldb_module *module,
183 const struct ldb_parse_tree *t);
186 parsetree_to_attrlist(struct ldb_module *module,
187 const struct ldb_parse_tree * t);
190 msg_to_sql(struct ldb_module * module,
191 const struct ldb_message * msg,
196 new_dn(struct ldb_module * module,
201 base160_sql(sqlite3_context * hContext,
203 sqlite3_value ** argv);
206 base160next_sql(sqlite3_context * hContext,
208 sqlite3_value ** argv);
211 static int lock_debug(struct ldb_module * module,
212 const char * lockname,
213 const char * pFileName,
216 static int unlock_debug(struct ldb_module * module,
217 const char * lockname,
218 const char * pFileName,
224 * Table of operations for the sqlite3 backend
226 static const struct ldb_module_ops lsqlite3_ops = {
228 .search = lsqlite3_search,
229 .search_bytree = lsqlite3_search_bytree,
230 .add_record = lsqlite3_add,
231 .modify_record = lsqlite3_modify,
232 .delete_record = lsqlite3_delete,
233 .rename_record = lsqlite3_rename,
234 .named_lock = lsqlite3_lock,
235 .named_unlock = lsqlite3_unlock,
236 .errstring = lsqlite3_errstring
248 * connect to the database
250 int lsqlite3_connect(struct ldb_context *ldb,
253 const char *options[])
257 struct lsqlite3_private * lsqlite3 = NULL;
259 lsqlite3 = talloc(ldb, struct lsqlite3_private);
264 lsqlite3->sqlite = NULL;
265 lsqlite3->options = NULL;
266 lsqlite3->lock_count = 0;
268 ret = initialize(lsqlite3, url);
269 if (ret != SQLITE_OK) {
273 talloc_set_destructor(lsqlite3, destructor);
275 ldb->modules = talloc(ldb, struct ldb_module);
279 ldb->modules->ldb = ldb;
280 ldb->modules->prev = ldb->modules->next = NULL;
281 ldb->modules->private_data = lsqlite3;
282 ldb->modules->ops = &lsqlite3_ops;
286 * take a copy of the options array, so we don't have to rely
287 * on the caller keeping it around (it might be dynamic)
289 for (i=0;options[i];i++) ;
291 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
292 if (!lsqlite3->options) {
296 for (i=0;options[i];i++) {
298 lsqlite3->options[i+1] = NULL;
299 lsqlite3->options[i] =
300 talloc_strdup(lsqlite3->options, options[i]);
301 if (!lsqlite3->options[i]) {
310 if (lsqlite3->sqlite != NULL) {
311 (void) sqlite3_close(lsqlite3->sqlite);
313 talloc_free(lsqlite3);
319 * Interface functions referenced by lsqlite3_ops
322 /* rename a record */
324 lsqlite3_rename(struct ldb_module * module,
328 const char *pOldNormalizedDN;
329 const char *pNewNormalizedDN;
331 struct lsqlite3_private * lsqlite3 = module->private_data;
333 /* ignore ltdb specials */
334 if (*pOldDN == '@' || *pNewDN == '@') {
338 /* Case-fold each of the DNs */
339 pOldNormalizedDN = ldb_dn_fold(module->ldb, pOldDN,
340 module, case_fold_attr_required);
341 pNewNormalizedDN = ldb_dn_fold(module->ldb, pNewDN,
342 module, case_fold_attr_required);
344 /* Begin a transaction */
345 if (LOCK_DB(module, "transaction") < 0) {
349 /* Determine the eid of the DN being renamed */
355 " WHERE normalized_dn = %Q;",
358 QUERY_NOROWS(lsqlite3,
362 " normalized_dn = %Q "
363 " WHERE eid = %lld;",
364 pNewDN, pNewNormalizedDN, eid);
366 QUERY_NOROWS(lsqlite3,
368 "UPDATE ldb_attribute_values "
369 " SET attr_value = %Q, "
370 " attr_value_normalized = %Q "
372 " AND attr_name = 'DN';",
377 /* Commit the transaction */
378 if (UNLOCK_DB(module, "transaction") < 0) {
379 UNLOCK_DB(module, "rollback");
386 /* delete a record */
388 lsqlite3_delete(struct ldb_module * module,
393 struct lsqlite3_private * lsqlite3 = module->private_data;
395 /* ignore ltdb specials */
400 /* Begin a transaction */
401 if (LOCK_DB(module, "transaction") < 0) {
405 /* Case-fold the DNs */
406 pNormalizedDN = ldb_dn_fold(module->ldb, pDN, module, case_fold_attr_required);
408 /* Determine the eid of the DN being deleted */
413 " FROM ldb_attribute_values\n"
414 " WHERE attr_name = 'DN'\n"
415 " AND attr_value_normalized = %Q;",
418 /* Delete attribute/value table entries pertaining to this DN */
419 QUERY_NOROWS(lsqlite3,
421 "DELETE FROM ldb_attribute_values "
422 " WHERE eid = %lld;",
425 /* Delete this entry */
426 QUERY_NOROWS(lsqlite3,
428 "DELETE FROM ldb_entry "
429 " WHERE eid = %lld;",
432 /* Commit the transaction */
433 if (UNLOCK_DB(module, "transaction") < 0) {
434 UNLOCK_DB(module, "rollback");
441 /* search for matching records, by tree */
443 lsqlite3_search_bytree(struct ldb_module * module,
444 const char * pBaseDN,
445 enum ldb_scope scope,
446 struct ldb_parse_tree * pTree,
447 const char * const * attrs,
448 struct ldb_message *** pppRes)
456 char * pSqlConstraints;
457 char * hTalloc = NULL;
459 const char * pNormalizedBaseDN;
460 const char * pAttrName;
461 const char * pAttrValue;
462 const char * pResultAttrList;
463 const char * const * pRequestedAttrs;
464 sqlite3_stmt * pStmt;
465 struct lsqlite3_private * lsqlite3 = module->private_data;
467 /* Allocate a temporary talloc context */
468 if ((hTalloc = talloc_new(module->ldb)) == NULL) {
472 /* Case-fold the base DN */
473 if ((pNormalizedBaseDN = ldb_dn_fold(hTalloc, pBaseDN?pBaseDN:"",
474 module, case_fold_attr_required)) == NULL) {
475 talloc_free(hTalloc);
479 /* Begin a transaction */
480 if (LOCK_DB(module, "transaction") < 0) {
485 * Obtain the eid of the base DN
487 if ((ret = query_int(lsqlite3,
491 " WHERE normalized_dn = %Q;",
492 pNormalizedBaseDN)) == SQLITE_DONE) {
493 UNLOCK_DB(module, "rollback");
494 talloc_free(hTalloc);
496 } else if (ret != SQLITE_OK) {
497 UNLOCK_DB(module, "rollback");
498 talloc_free(hTalloc);
502 /* Convert filter into a series of SQL conditions (constraints) */
503 pSqlConstraints = parsetree_to_sql(module, hTalloc, pTree);
505 /* Ensure we're starting with an empty result attribute table */
506 QUERY_NOROWS(lsqlite3,
508 "DELETE FROM " RESULT_ATTR_TABLE "\n"
509 " WHERE 1;");/* avoid a schema change with WHERE 1 */
511 /* Initially, we don't know what the requested attributes are */
512 pResultAttrList = NULL;
514 /* Insert the list of requested attributes into this table */
515 for (pRequestedAttrs = (const char * const *) attrs;
516 pRequestedAttrs != NULL && *pRequestedAttrs != NULL;
519 /* If any attribute in the list is "*" then... */
520 if (strcmp(*pRequestedAttrs, "*") == 0) {
521 /* we want all attribute types */
522 pResultAttrList = "";
526 /* otherwise, add this name to the resuult list */
527 QUERY_NOROWS(lsqlite3,
530 " INTO " RESULT_ATTR_TABLE "\n"
538 /* If we didn't get a "*" for all attributes in the result list... */
539 if (pResultAttrList == NULL) {
540 /* ... then we'll use the result attribute table */
542 " AND upper(av.attr_name) IN\n"
543 " (SELECT attr_name\n"
544 " FROM " RESULT_ATTR_TABLE ") ";
547 /* Ensure we're starting with an empty filter attribute table */
548 QUERY_NOROWS(lsqlite3,
550 "DELETE FROM " FILTER_ATTR_TABLE "\n"
551 " WHERE 1;");/* avoid a schema change with WHERE 1 */
554 * Create a table of unique attribute names for our extra table list
556 if ((ret = parsetree_to_attrlist(module, pTree)) != 0) {
562 case LDB_SCOPE_DEFAULT:
563 case LDB_SCOPE_SUBTREE:
564 pSql = sqlite3_mprintf(
565 "SELECT entry.eid,\n"
569 " FROM ldb_entry AS entry\n"
571 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
572 " ON av.eid = entry.eid\n"
575 " WHERE entry.eid IN\n"
576 " (SELECT DISTINCT ldb_entry.eid\n"
578 " WHERE ldb_entry.tree_key >=\n"
579 " (SELECT tree_key\n"
581 " WHERE eid = %lld)\n"
582 " AND ldb_entry.tree_key <\n"
583 " (SELECT base160_next(tree_key)\n"
585 " WHERE eid = %lld)\n"
586 " AND ldb_entry.eid IN\n(%s)\n"
588 " ORDER BY entry.tree_key DESC,\n"
589 " COALESCE(av.attr_name, '');",
597 pSql = sqlite3_mprintf(
598 "SELECT entry.eid,\n"
602 " FROM ldb_entry AS entry\n"
604 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
605 " ON av.eid = entry.eid\n"
608 " WHERE entry.eid IN\n"
609 " (SELECT DISTINCT ldb_entry.eid\n"
611 " WHERE ldb_entry.eid = %lld\n"
612 " AND ldb_entry.eid IN\n(%s)\n"
614 " ORDER BY entry.tree_key DESC,\n"
615 " COALESCE(av.attr_name, '');",
621 case LDB_SCOPE_ONELEVEL:
622 pSql = sqlite3_mprintf(
623 "SELECT entry.eid,\n"
627 " FROM ldb_entry AS entry\n"
629 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
630 " ON av.eid = entry.eid\n"
633 " WHERE entry.eid IN\n"
634 " (SELECT DISTINCT ldb_entry.eid\n"
636 " WHERE ldb_entry.tree_key >=\n"
637 " (SELECT tree_key\n"
639 " WHERE eid = %lld)\n"
640 " AND ldb_entry.tree_key <\n"
641 " (SELECT base160_next(tree_key)\n"
643 " WHERE eid = %lld)\n"
644 " AND length(ldb_entry.tree_key) =\n"
645 " (SELECT length(tree_key) + 4\n"
647 " WHERE eid = %lld)\n"
648 " AND ldb_entry.eid IN\n(%s)\n"
651 " ORDER BY entry.tree_key DESC,\n"
652 " COALESCE(av.attr_name, '');\n",
666 if (lsqlite3_debug & SQLITE3_DEBUG_SEARCH) {
667 printf("%s\n", pSql);
671 * Prepare and execute the SQL statement. Loop allows retrying on
672 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
673 * requiring retrying the operation.
675 for (bLoop = TRUE; bLoop; ) {
676 /* There are no allocate message structures yet */
678 if (pppRes != NULL) {
682 /* Compile the SQL statement into sqlite virtual machine */
683 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
687 NULL)) == SQLITE_SCHEMA) {
688 if (pppRes != NULL && *pppRes != NULL) {
689 talloc_free(*pppRes);
692 } else if (ret != SQLITE_OK) {
697 /* Initially, we have no previous eid */
700 /* Loop through the returned rows */
701 for (ret = SQLITE_ROW; ret == SQLITE_ROW; ) {
703 /* Get the next row */
704 if ((ret = sqlite3_step(pStmt)) == SQLITE_ROW) {
706 /* Get the values from this row */
707 eid = sqlite3_column_int64(pStmt, 0);
708 pDN = sqlite3_column_text(pStmt, 1);
709 pAttrName = sqlite3_column_text(pStmt, 2);
710 pAttrValue = sqlite3_column_text(pStmt, 3);
712 /* Add this result to the result set */
713 if (add_msg_attr(hTalloc,
722 (void) sqlite3_finalize(pStmt);
727 /* Save the most recent EID */
732 if (ret == SQLITE_SCHEMA) {
733 (void) sqlite3_finalize(pStmt);
734 if (pppRes != NULL && *pppRes != NULL) {
735 talloc_free(*pppRes);
738 } else if (ret != SQLITE_DONE) {
739 (void) sqlite3_finalize(pStmt);
744 /* Free the virtual machine */
745 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
746 (void) sqlite3_finalize(pStmt);
747 if (pppRes != NULL && *pppRes != NULL) {
748 talloc_free(*pppRes);
751 } else if (ret != SQLITE_OK) {
752 (void) sqlite3_finalize(pStmt);
758 * Normal condition is only one time through loop. Loop is
759 * rerun in error conditions, via "continue", above.
765 /* We're alll done with this query */
768 /* End the transaction */
769 UNLOCK_DB(module, "rollback");
771 /* Were there any results? */
772 if (ret != 0 || allocated == 0) {
773 /* Nope. We can free the results. */
774 if (pppRes != NULL && *pppRes != NULL) {
775 talloc_free(*pppRes);
780 /* Clean up our temporary tables */
781 QUERY_NOROWS(lsqlite3,
783 "DELETE FROM " RESULT_ATTR_TABLE "\n"
784 " WHERE 1;");/* avoid a schema change with WHERE 1 */
786 QUERY_NOROWS(lsqlite3,
788 "DELETE FROM " FILTER_ATTR_TABLE "\n"
789 " WHERE 1;");/* avoid a schema change with WHERE 1 */
792 if (hTalloc != NULL) {
793 talloc_free(hTalloc);
796 /* If error, return error code; otherwise return number of results */
797 return ret == 0 ? allocated : ret;
800 /* search for matching records, by expression */
802 lsqlite3_search(struct ldb_module * module,
803 const char * pBaseDN,
804 enum ldb_scope scope,
805 const char * pExpression,
806 const char * const * attrs,
807 struct ldb_message *** pppRes)
810 struct ldb_parse_tree * pTree;
812 /* Handle tdb specials */
813 if (pBaseDN != NULL && *pBaseDN == '@') {
814 #warning "handle tdb specials"
819 /* (|(objectclass=*)(dn=*)) is passed by the command line tool now instead */
820 /* Handle the special case of requesting all */
821 if (pExpression != NULL && *pExpression == '\0') {
822 pExpression = "dn=*";
826 /* Parse the filter expression into a tree we can work with */
827 if ((pTree = ldb_parse_tree(module->ldb, pExpression)) == NULL) {
831 /* Now use the bytree function for the remainder of processing */
832 ret = lsqlite3_search_bytree(module, pBaseDN, scope,
833 pTree, attrs, pppRes);
835 /* Free the parse tree */
845 lsqlite3_add(struct ldb_module *module,
846 const struct ldb_message *msg)
850 /* See if this is an ltdb special */
851 if (*msg->dn == '@') {
852 /* Yup. We handle a few of these and ignore others */
853 if (strcmp(msg->dn, "@SUBCLASSES") == 0) {
854 #warning "insert subclasses into object class tree"
857 if (strcmp(msg->dn, "@INDEXLIST") == 0) {
858 /* explicitly ignored */
862 /* Others are implicitly ignored */
866 /* Begin a transaction */
867 if (LOCK_DB(module, "transaction") < 0) {
872 * Build any portions of the directory tree that don't exist. If the
873 * final component already exists, it's an error.
875 if (new_dn(module, msg->dn, &eid) != 0) {
876 UNLOCK_DB(module, "rollback");
880 /* Add attributes to this new entry */
881 if (msg_to_sql(module, msg, eid, FALSE) != 0) {
882 UNLOCK_DB(module, "rollback");
886 /* Everything worked. Commit it! */
887 if (UNLOCK_DB(module, "transaction") < 0) {
888 UNLOCK_DB(module, "rollback");
895 /* modify a record */
897 lsqlite3_modify(struct ldb_module * module,
898 const struct ldb_message * msg)
900 char * pNormalizedDN;
902 struct lsqlite3_private * lsqlite3 = module->private_data;
904 /* ignore ltdb specials */
905 if (*msg->dn == '@') {
909 /* Begin a transaction */
910 if (LOCK_DB(module, "transaction") < 0) {
914 /* Case-fold the DN so we can compare it to what's in the database */
915 pNormalizedDN = ldb_dn_fold(module->ldb, msg->dn,
916 module, case_fold_attr_required);
918 /* Determine the eid of the DN being deleted */
924 " WHERE normalized_dn = %Q;",
927 /* Apply the message attributes */
928 if (msg_to_sql(module, msg, eid, TRUE) != 0) {
929 UNLOCK_DB(module, "rollback");
934 /* Everything worked. Commit it! */
935 if (UNLOCK_DB(module, "transaction") < 0) {
936 UNLOCK_DB(module, "rollback");
942 /* obtain a named lock */
944 lsqlite3_lock(struct ldb_module * module,
945 const char * lockname)
947 struct lsqlite3_private * lsqlite3 = module->private_data;
949 if (lockname == NULL) {
953 if (strcmp(lockname, "transaction") == 0) {
954 if (lsqlite3->lock_count == 0) {
955 if (query_norows(lsqlite3, "BEGIN EXCLUSIVE;") != 0) {
959 ++lsqlite3->lock_count;
965 /* release a named lock */
967 lsqlite3_unlock(struct ldb_module *module,
968 const char *lockname)
970 struct lsqlite3_private * lsqlite3 = module->private_data;
972 if (lockname == NULL) {
976 if (strcmp(lockname, "transaction") == 0) {
977 if (lsqlite3->lock_count == 1) {
978 if (query_norows(lsqlite3, "COMMIT;") != 0) {
979 query_norows(lsqlite3, "ROLLBACK;");
981 } else if (lsqlite3->lock_count > 0) {
982 --lsqlite3->lock_count;
984 } else if (strcmp(lockname, "rollback") == 0) {
985 query_norows(lsqlite3, "ROLLBACK;");
991 /* return extended error information */
993 lsqlite3_errstring(struct ldb_module *module)
995 struct lsqlite3_private * lsqlite3 = module->private_data;
997 return sqlite3_errmsg(lsqlite3->sqlite);
1008 initialize(struct lsqlite3_private *lsqlite3,
1014 sqlite3_stmt * stmt;
1015 const char * schema =
1018 "CREATE TABLE ldb_info AS "
1019 " SELECT 'LDB' AS database_type,"
1020 " '1.0' AS version;"
1023 * The entry table holds the information about an entry.
1024 * This table is used to obtain the EID of the entry and to
1025 * support scope=one and scope=base. The parent and child
1026 * table is included in the entry table since all the other
1027 * attributes are dependent on EID.
1029 "CREATE TABLE ldb_entry "
1031 " eid INTEGER PRIMARY KEY,"
1032 " peid INTEGER REFERENCES ldb_entry,"
1033 " dn TEXT UNIQUE NOT NULL,"
1034 " normalized_dn TEXT UNIQUE NOT NULL,"
1035 " tree_key TEXT UNIQUE,"
1036 " max_child_num INTEGER DEFAULT 0,"
1037 " create_timestamp INTEGER,"
1038 " modify_timestamp INTEGER"
1042 "CREATE TABLE ldb_object_classes"
1044 " class_name TEXT PRIMARY KEY,"
1045 " parent_class_name TEXT,"
1046 " tree_key TEXT UNIQUE,"
1047 " max_child_num INTEGER DEFAULT 0"
1051 * We keep a full listing of attribute/value pairs here
1053 "CREATE TABLE ldb_attribute_values"
1055 " eid INTEGER REFERENCES ldb_entry,"
1058 " attr_value_normalized TEXT "
1065 "CREATE INDEX ldb_entry_tree_key_idx "
1066 " ON ldb_entry (tree_key);"
1068 "CREATE INDEX ldb_attribute_values_eid_idx "
1069 " ON ldb_attribute_values (eid);"
1077 "CREATE TRIGGER ldb_entry_insert_tr"
1084 " SET create_timestamp = strftime('%s', 'now'),"
1085 " modify_timestamp = strftime('%s', 'now')"
1087 " tree_key = COALESCE(tree_key, "
1089 " SELECT tree_key || "
1090 " (SELECT base160(max_child_num + 1)"
1092 " WHERE eid = new.peid)"
1094 " WHERE eid = new.peid "
1096 " UPDATE ldb_entry "
1097 " SET max_child_num = max_child_num + 1"
1098 " WHERE eid = new.peid;"
1101 "CREATE TRIGGER ldb_entry_update_tr"
1107 " SET modify_timestamp = strftime('%s', 'now')"
1108 " WHERE eid = old.eid;"
1111 "CREATE TRIGGER ldb_object_classes_insert_tr"
1113 " ON ldb_object_classes"
1116 " UPDATE ldb_object_classes"
1117 " SET tree_key = COALESCE(tree_key, "
1119 " SELECT tree_key || "
1120 " (SELECT base160(max_child_num + 1)"
1121 " FROM ldb_object_classes"
1122 " WHERE class_name = "
1123 " new.parent_class_name)"
1124 " FROM ldb_object_classes "
1125 " WHERE class_name = new.parent_class_name "
1127 " UPDATE ldb_object_classes "
1128 " SET max_child_num = max_child_num + 1"
1129 " WHERE class_name = new.parent_class_name;"
1133 * Table initialization
1137 "INSERT INTO ldb_entry "
1138 " (eid, peid, dn, normalized_dn, tree_key) "
1140 " (0, NULL, '', '', '0001');"
1142 /* And the root node "dn" attribute */
1143 "INSERT INTO ldb_attribute_values "
1144 " (eid, attr_name, attr_value, attr_value_normalized) "
1146 " (0, 'DN', '', '');"
1148 "INSERT INTO ldb_object_classes "
1149 " (class_name, tree_key) "
1155 /* Skip protocol indicator of url */
1156 if (strncmp(url, "sqlite://", 9) != 0) {
1157 return SQLITE_MISUSE;
1160 /* Update pointer to just after the protocol indicator */
1163 /* Try to open the (possibly empty/non-existent) database */
1164 if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
1168 /* In case this is a new database, enable auto_vacuum */
1169 if (query_norows(lsqlite3, "PRAGMA auto_vacuum=1;") != 0) {
1173 /* Establish a busy timeout of 30 seconds */
1174 if ((ret = sqlite3_busy_timeout(lsqlite3->sqlite,
1175 30000)) != SQLITE_OK) {
1179 /* Create a function, callable from sql, to increment a tree_key */
1181 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1182 "base160_next", /* function name */
1183 1, /* number of args */
1184 SQLITE_ANY, /* preferred text type */
1185 NULL, /* user data */
1186 base160next_sql, /* called func */
1187 NULL, /* step func */
1188 NULL /* final func */
1193 /* Create a function, callable from sql, to convert int to base160 */
1195 sqlite3_create_function(lsqlite3->sqlite,/* handle */
1196 "base160", /* function name */
1197 1, /* number of args */
1198 SQLITE_ANY, /* preferred text type */
1199 NULL, /* user data */
1200 base160_sql, /* called func */
1201 NULL, /* step func */
1202 NULL /* final func */
1207 /* Begin a transaction */
1208 if ((ret = query_norows(lsqlite3, "BEGIN EXCLUSIVE;")) != 0) {
1212 /* Determine if this is a new database. No tables means it is. */
1213 if (query_int(lsqlite3,
1216 " FROM sqlite_master\n"
1217 " WHERE type = 'table';") != 0) {
1218 query_norows(lsqlite3, "ROLLBACK;");
1222 if (queryInt == 0) {
1224 * Create the database schema
1226 for (pTail = discard_const_p(char, schema);
1227 pTail != NULL && *pTail != '\0';
1230 if (lsqlite3_debug & SQLITE3_DEBUG_INIT) {
1231 printf("Execute first query in:\n%s\n", pTail);
1234 if ((ret = sqlite3_prepare(
1239 &pTail)) != SQLITE_OK ||
1240 (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
1241 (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
1243 if (lsqlite3_debug & SQLITE3_DEBUG_INIT) {
1245 sqlite3_errmsg(lsqlite3->sqlite));
1246 printf("pTail = [%s]\n", pTail);
1249 query_norows(lsqlite3, "ROLLBACK;");
1250 (void) sqlite3_close(lsqlite3->sqlite);
1256 * Ensure that the database we opened is one of ours
1258 if (query_int(lsqlite3,
1261 " (SELECT COUNT(*) = 2"
1262 " FROM sqlite_master "
1263 " WHERE type = 'table' "
1267 " 'ldb_object_classes' "
1273 " WHERE database_type = 'LDB' "
1274 " AND version = '1.0'"
1278 /* It's not one that we created. See ya! */
1279 query_norows(lsqlite3, "ROLLBACK;");
1280 (void) sqlite3_close(lsqlite3->sqlite);
1281 return SQLITE_MISUSE;
1286 * Create a temporary table to hold attributes requested in the result
1289 query_norows(lsqlite3, "DROP TABLE " RESULT_ATTR_TABLE ";\n");
1291 query_norows(lsqlite3,
1292 "CREATE " TEMPTAB " TABLE " RESULT_ATTR_TABLE "\n"
1294 " attr_name TEXT PRIMARY KEY\n"
1296 query_norows(lsqlite3, "ROLLBACK;");
1301 * Create a temporary table to hold the attributes used by filters
1304 query_norows(lsqlite3, "DROP TABLE " FILTER_ATTR_TABLE ";\n");
1306 query_norows(lsqlite3,
1307 "CREATE " TEMPTAB " TABLE " FILTER_ATTR_TABLE "\n"
1309 " attr_name TEXT PRIMARY KEY\n"
1311 query_norows(lsqlite3, "ROLLBACK;");
1315 /* Commit the transaction */
1316 if ((ret = query_norows(lsqlite3, "COMMIT;")) != 0) {
1317 query_norows(lsqlite3, "ROLLBACK;");
1327 struct lsqlite3_private * lsqlite3 = p;
1329 if (lsqlite3->sqlite) {
1330 sqlite3_close(lsqlite3->sqlite);
1339 * This function is used for queries that are not expected to return any rows,
1340 * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
1341 * There are no provisions here for returning data from rows in a table, so do
1342 * not pass SELECT queries to this function.
1345 query_norows(const struct lsqlite3_private *lsqlite3,
1352 sqlite3_stmt * pStmt;
1359 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1360 gettimeofday(&tv, &tz);
1361 t0 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1364 /* Begin access to variable argument list */
1365 va_start(args, pSql);
1367 /* Format the query */
1368 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
1373 * Prepare and execute the SQL statement. Loop allows retrying on
1374 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1375 * requiring retrying the operation.
1377 for (bLoop = TRUE; bLoop; ) {
1379 /* Compile the SQL statement into sqlite virtual machine */
1380 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
1384 NULL)) == SQLITE_SCHEMA) {
1386 } else if (ret != SQLITE_OK) {
1391 /* No rows expected, so just step through machine code once */
1392 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
1393 (void) sqlite3_finalize(pStmt);
1395 } else if (ret != SQLITE_DONE) {
1396 (void) sqlite3_finalize(pStmt);
1401 /* Free the virtual machine */
1402 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1403 (void) sqlite3_finalize(pStmt);
1405 } else if (ret != SQLITE_OK) {
1406 (void) sqlite3_finalize(pStmt);
1412 * Normal condition is only one time through loop. Loop is
1413 * rerun in error conditions, via "continue", above.
1419 /* All done with variable argument list */
1422 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1423 gettimeofday(&tv, NULL);
1424 t1 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1425 printf("%1.6lf %s\n%s\n\n", t1 - t0,
1426 ret == 0 ? "SUCCESS" : "FAIL",
1430 /* Free the memory we allocated for our query string */
1440 * This function is used for the common case of queries that return a single
1443 * NOTE: If more than one value is returned by the query, all but the first
1444 * one will be ignored.
1447 query_int(const struct lsqlite3_private * lsqlite3,
1455 sqlite3_stmt * pStmt;
1462 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1463 gettimeofday(&tv, &tz);
1464 t0 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1467 /* Begin access to variable argument list */
1468 va_start(args, pSql);
1470 /* Format the query */
1471 if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
1472 return SQLITE_NOMEM;
1475 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1480 * Prepare and execute the SQL statement. Loop allows retrying on
1481 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1482 * requiring retrying the operation.
1484 for (bLoop = TRUE; bLoop; ) {
1486 /* Compile the SQL statement into sqlite virtual machine */
1487 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
1491 NULL)) == SQLITE_SCHEMA) {
1493 } else if (ret != SQLITE_OK) {
1497 /* One row expected */
1498 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
1499 (void) sqlite3_finalize(pStmt);
1501 } else if (ret != SQLITE_ROW) {
1502 (void) sqlite3_finalize(pStmt);
1506 /* Get the value to be returned */
1507 *pRet = sqlite3_column_int64(pStmt, 0);
1509 /* Free the virtual machine */
1510 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1511 (void) sqlite3_finalize(pStmt);
1513 } else if (ret != SQLITE_OK) {
1514 (void) sqlite3_finalize(pStmt);
1519 * Normal condition is only one time through loop. Loop is
1520 * rerun in error conditions, via "continue", above.
1525 /* All done with variable argument list */
1529 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1530 gettimeofday(&tv, NULL);
1531 t1 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
1532 printf("%1.6lf %s\n%s\n\n", t1 - t0,
1533 ret == 0 ? "SUCCESS" : "FAIL",
1537 /* Free the memory we allocated for our query string */
1545 callback function used in call to ldb_dn_fold() for determining whether an
1546 attribute type requires case folding.
1549 case_fold_attr_required(void * hUserData,
1552 // struct ldb_module * module = hUserData;
1558 case_fold_attr_not_required(void * hUserData,
1561 // struct ldb_module * module = hUserData;
1568 * add a single set of ldap message values to a ldb_message
1572 add_msg_attr(void * hTalloc,
1575 const char * pAttrName,
1576 const char * pAttrValue,
1579 struct ldb_message *** pppRes)
1582 struct ldb_message * msg;
1583 struct ldb_message_element * el;
1585 /* Is this a different EID than the previous one? */
1586 if (eid != prevEID) {
1587 /* Yup. Add another result to the result array */
1588 if ((x = talloc_realloc(hTalloc,
1589 *pAllocated == 0 ? NULL : *pppRes,
1590 struct ldb_message *,
1591 *pAllocated + 1)) == NULL) {
1596 /* Save the new result list */
1599 /* Allocate a new result structure */
1600 if ((x = talloc(*pppRes, struct ldb_message)) == NULL) {
1604 /* Save the new result */
1605 (*pppRes)[*pAllocated] = x;
1607 /* Steal the initial result and put it in its own context */
1608 talloc_steal(NULL, *pppRes);
1610 /* We've allocated one more result */
1613 /* Ensure that the message is initialized */
1615 if ((msg->dn = talloc_strdup(msg, pDN)) == NULL) {
1618 msg->num_elements = 0;
1619 msg->elements = NULL;
1620 msg->private_data = NULL;
1622 /* Same EID. Point to the previous most-recent message */
1623 msg = (*pppRes)[*pAllocated - 1];
1626 if (pAttrName != NULL && pAttrValue != NULL) {
1628 * Point to the most recent previous element. (If there are none,
1629 * this will point to non-allocated memory, but the pointer will
1630 * never be dereferenced.)
1632 el = &msg->elements[msg->num_elements - 1];
1635 * See if the most recent previous element has the same attr_name
1637 if (msg->num_elements == 0 || strcmp(el->name, pAttrName) != 0) {
1639 /* It's a new attr_name. Allocate another message element */
1640 if ((el = talloc_realloc(msg,
1642 struct ldb_message_element,
1643 msg->num_elements + 1)) == NULL) {
1647 /* Save the new element */
1650 /* Save the attribute name */
1652 talloc_strdup(msg->elements, pAttrName)) == NULL) {
1657 /* There's now one additional element */
1658 msg->num_elements++;
1663 /* Initialize number of attribute values for this type */
1668 /* Increase the value array size by 1 */
1671 el->num_values == 0 ? NULL : el->values,
1673 el->num_values + 1)) == NULL) {
1677 /* Save the new attribute value length */
1678 el->values[el->num_values].length = strlen(pAttrValue);
1680 /* Copy the new attribute value */
1681 if ((el->values[el->num_values].data =
1682 talloc_memdup(el->values,
1684 el->values[el->num_values].length)) == NULL) {
1688 /* We now have one additional value of this type */
1696 parsetree_to_sql(struct ldb_module *module,
1698 const struct ldb_parse_tree *t)
1701 char * pNormalizedDN;
1708 switch(t->operation) {
1712 case LDB_OP_EXTENDED:
1713 #warning "work out how to handle bitops"
1717 ret = parsetree_to_sql(module,
1719 t->u.list.elements[0]);
1721 for (i = 1; i < t->u.list.num_elements; i++) {
1726 t->u.list.elements[i]);
1727 ret = talloc_asprintf_append(ret,
1735 ret = talloc_asprintf(hTalloc,
1744 ret = parsetree_to_sql(module,
1746 t->u.list.elements[0]);
1748 for (i = 1; i < t->u.list.num_elements; i++) {
1753 t->u.list.elements[i]);
1754 ret = talloc_asprintf_append(ret,
1761 ret = talloc_asprintf(hTalloc,
1775 ret = talloc_asprintf(hTalloc,
1778 " WHERE eid NOT IN (%s)\n",
1784 /* should never occur */
1788 /* Get a case-folded copy of the attribute name */
1789 pAttrName = ldb_casefold((struct ldb_context *) module,
1793 * For simple searches, we want to retrieve the list of EIDs that
1794 * match the criteria.
1796 if (t->u.simple.value.length == 1 &&
1797 (*(const char *) t->u.simple.value.data) == '*') {
1799 * Special case for "attr_name=*". In this case, we want the
1800 * eid corresponding to all values in the specified attribute
1803 if ((p = sqlite3_mprintf(" SELECT eid\n"
1804 " FROM ldb_attribute_values\n"
1805 " WHERE attr_name = %Q",
1806 pAttrName)) == NULL) {
1810 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1814 ret = talloc_strdup(hTalloc, p);
1817 } else if (strcasecmp(t->u.simple.attr, "objectclass") == 0) {
1819 * For object classes, we want to search for all objectclasses
1820 * that are subclasses as well.
1822 if ((p = sqlite3_mprintf(
1824 " FROM ldb_attribute_values\n"
1825 " WHERE attr_name = 'OBJECTCLASS' "
1826 " AND attr_value_normalized IN\n"
1827 " (SELECT class_name\n"
1828 " FROM ldb_object_classes\n"
1829 " WHERE tree_key GLOB\n"
1830 " (SELECT tree_key\n"
1831 " FROM ldb_object_classes\n"
1832 " WHERE class_name = upper(%Q)) "
1834 t->u.simple.value.data)) == NULL) {
1838 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1842 ret = talloc_strdup(hTalloc, p);
1845 } else if (strcasecmp(t->u.simple.attr, "dn") == 0) {
1846 pNormalizedDN = ldb_dn_fold(module->ldb, t->u.simple.value.data,
1847 module, case_fold_attr_required);
1848 if ((p = sqlite3_mprintf(
1850 " FROM ldb_attribute_values\n"
1851 " WHERE attr_name = %Q\n"
1852 " AND attr_value_normalized = %Q\n",
1854 pNormalizedDN)) == NULL) {
1858 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1862 ret = talloc_strdup(hTalloc, p);
1865 /* A normal query. */
1866 if ((p = sqlite3_mprintf(
1868 " FROM ldb_attribute_values\n"
1869 " WHERE attr_name = %Q\n"
1870 " AND attr_value_normalized = upper(%Q)\n",
1872 t->u.simple.value.data)) == NULL) {
1876 if (lsqlite3_debug & SQLITE3_DEBUG_QUERY) {
1880 ret = talloc_strdup(hTalloc, p);
1889 parsetree_to_attrlist(struct ldb_module *module,
1890 const struct ldb_parse_tree * t)
1893 struct lsqlite3_private * lsqlite3 = module->private_data;
1895 switch(t->operation) {
1899 case LDB_OP_EXTENDED:
1900 #warning "work out how to handle bitops"
1904 if (parsetree_to_attrlist(
1906 t->u.list.elements[0]) != 0) {
1910 for (i = 1; i < t->u.list.num_elements; i++) {
1911 if (parsetree_to_attrlist(
1913 t->u.list.elements[i]) != 0) {
1921 if (parsetree_to_attrlist(
1923 t->u.list.elements[0]) != 0) {
1927 for (i = 1; i < t->u.list.num_elements; i++) {
1928 if (parsetree_to_attrlist(
1930 t->u.list.elements[i]) != 0) {
1938 if (parsetree_to_attrlist(module,
1939 t->u.not.child) != 0) {
1946 /* should never occur */
1950 QUERY_NOROWS(lsqlite3,
1952 "INSERT OR IGNORE INTO " FILTER_ATTR_TABLE "\n"
1962 * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
1963 * requests in the ldb_message
1966 msg_to_sql(struct ldb_module * module,
1967 const struct ldb_message * msg,
1975 struct lsqlite3_private * lsqlite3 = module->private_data;
1977 for (i = 0; i < msg->num_elements; i++) {
1978 const struct ldb_message_element *el = &msg->elements[i];
1981 flags = LDB_FLAG_MOD_ADD;
1983 flags = el->flags & LDB_FLAG_MOD_MASK;
1986 /* Get a case-folded copy of the attribute name */
1987 pAttrName = ldb_casefold((struct ldb_context *) module,
1990 /* For each value of the specified attribute name... */
1991 for (j = 0; j < el->num_values; j++) {
1993 /* ... bind the attribute value, if necessary */
1995 case LDB_FLAG_MOD_ADD:
1999 "INSERT INTO ldb_attribute_values\n"
2003 " attr_value_normalized)\n"
2005 " (%lld, %Q, %Q, upper(%Q));",
2008 el->values[j].data, /* FIX ME */
2009 el->values[j].data);
2011 /* Is this a special "objectclass"? */
2012 if (strcasecmp(pAttrName,
2013 "objectclass") != 0) {
2018 /* Handle special "objectclass" type */
2019 QUERY_NOROWS(lsqlite3,
2022 " INTO ldb_object_classes "
2024 " parent_class_name) "
2026 " (upper(%Q), 'TOP');",
2027 ldb_casefold(module,
2028 el->values[j].data));
2031 case LDB_FLAG_MOD_REPLACE:
2035 "UPDATE ldb_attribute_values\n"
2036 " SET attr_value = %Q,\n"
2037 " attr_value_normalized =\n"
2039 " WHERE eid = %lld\n"
2040 " AND attr_name = %Q;",
2041 el->values[j].data, /* FIX ME */
2047 case LDB_FLAG_MOD_DELETE:
2048 /* No additional parameters to this query */
2052 "DELETE FROM ldb_attribute_values"
2053 " WHERE eid = %lld "
2054 " AND attr_name = %Q "
2055 " AND attr_value_normalized =\n"
2059 el->values[j].data);
2071 new_dn(struct ldb_module * module,
2079 char * pPartialNormalizedDN;
2086 struct ldb_dn * pExplodedDN;
2087 struct ldb_dn_component * pComponent;
2088 struct ldb_context * ldb = module->ldb;
2089 struct lsqlite3_private * lsqlite3 = module->private_data;
2091 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2092 gettimeofday(&tv, &tz);
2093 t0 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
2096 /* Explode the DN */
2101 case_fold_attr_not_required)) == NULL) {
2105 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2106 gettimeofday(&tv, NULL);
2107 t1 = (double) tv.tv_sec + ((double) tv.tv_usec / 1000000.0);
2108 printf("%1.6lf loc 1\n", t1 - t0);
2112 /* Allocate a string to hold the partial DN of each component */
2113 if ((pPartialDN = talloc_strdup(ldb, "")) == NULL) {
2117 if ((pPartialNormalizedDN = talloc_strdup(pPartialDN, "")) == NULL) {
2121 /* For each component of the DN (starting with the last one)... */
2122 #warning "convert this loop to recursive, and search backwards instead"
2125 for (nComponent = pExplodedDN->comp_num - 1, bFirst = TRUE;
2127 nComponent--, bFirst = FALSE) {
2129 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2130 gettimeofday(&tv, NULL);
2131 t1 = ((double) tv.tv_sec +
2132 ((double) tv.tv_usec / 1000000.0));
2133 printf("%1.6lf loc 2\n", t1 - t0);
2137 /* Point to the component */
2138 pComponent = pExplodedDN->components[nComponent];
2140 /* Add this component on to the partial DN to date */
2141 if ((p = talloc_asprintf(ldb,
2143 pComponent->component,
2145 pPartialDN)) == NULL) {
2149 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2150 gettimeofday(&tv, NULL);
2151 t1 = ((double) tv.tv_sec +
2152 ((double) tv.tv_usec / 1000000.0));
2153 printf("%1.6lf loc 3\n", t1 - t0);
2157 /* No need for the old partial DN any more */
2158 talloc_free(pPartialDN);
2160 /* Save the new partial DN */
2162 pPartialNormalizedDN = ldb_dn_fold(pPartialDN, p, module, case_fold_attr_required);
2164 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2165 gettimeofday(&tv, NULL);
2166 t1 = ((double) tv.tv_sec +
2167 ((double) tv.tv_usec / 1000000.0));
2168 printf("%1.6lf loc 4\n", t1 - t0);
2173 * Ensure that an entry is in the ldb_entry table for this
2174 * component. Any component other than the last one
2175 * (component 0) may already exist. It is an error if
2176 * component 0 (the full DN requested to be be inserted)
2179 QUERY_NOROWS(lsqlite3,
2181 "INSERT %s INTO ldb_entry\n"
2182 " (peid, dn, normalized_dn)\n"
2185 nComponent == 0 ? "" : "OR IGNORE",
2186 eid, pPartialDN, pPartialNormalizedDN);
2188 /* Save the parent EID */
2191 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2192 gettimeofday(&tv, NULL);
2193 t1 = ((double) tv.tv_sec +
2194 ((double) tv.tv_usec / 1000000.0));
2195 printf("%1.6lf loc 5\n", t1 - t0);
2199 /* Get the EID of the just inserted row */
2205 " WHERE normalized_dn = %Q;",
2206 pPartialNormalizedDN);
2208 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2209 gettimeofday(&tv, NULL);
2210 t1 = ((double) tv.tv_sec +
2211 ((double) tv.tv_usec / 1000000.0));
2212 printf("%1.6lf loc 8\n", t1 - t0);
2216 /* Also add DN attribute */
2217 QUERY_NOROWS(lsqlite3,
2219 "INSERT %s INTO ldb_attribute_values\n"
2223 " attr_value_normalized) "
2225 " (%lld, 'DN', %Q, %Q);",
2226 nComponent == 0 ? "" : "OR IGNORE",
2228 pPartialDN, /* FIX ME */
2229 pPartialNormalizedDN);
2232 if (lsqlite3_debug & SQLITE3_DEBUG_NEWDN) {
2233 gettimeofday(&tv, NULL);
2234 t1 = ((double) tv.tv_sec +
2235 ((double) tv.tv_usec / 1000000.0));
2236 printf("%1.6lf loc 9\n", t1 - t0);
2240 /* Give 'em what they came for! */
2247 static unsigned char base160tab[161] = {
2248 48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
2249 58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
2250 73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
2251 83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
2252 99 ,100,101,102,103,104,105,106,107,108, /* c-l */
2253 109,110,111,112,113,114,115,116,117,118, /* m-v */
2254 119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
2255 166,167,168,169,170,171,172,173,174,175, /* latin1 */
2256 176,177,178,179,180,181,182,183,184,185, /* latin1 */
2257 186,187,188,189,190,191,192,193,194,195, /* latin1 */
2258 196,197,198,199,200,201,202,203,204,205, /* latin1 */
2259 206,207,208,209,210,211,212,213,214,215, /* latin1 */
2260 216,217,218,219,220,221,222,223,224,225, /* latin1 */
2261 226,227,228,229,230,231,232,233,234,235, /* latin1 */
2262 236,237,238,239,240,241,242,243,244,245, /* latin1 */
2263 246,247,248,249,250,251,252,253,254,255, /* latin1 */
2271 * Convert an unsigned long integer into a base160 representation of the
2276 * value to be converted
2279 * character array, 5 bytes long, into which the base160 representation
2280 * will be placed. The result will be a four-digit representation of the
2281 * number (with leading zeros prepended as necessary), and null
2288 base160_sql(sqlite3_context * hContext,
2290 sqlite3_value ** argv)
2296 val = sqlite3_value_int64(argv[0]);
2298 for (i = 3; i >= 0; i--) {
2300 result[i] = base160tab[val % 160];
2306 sqlite3_result_text(hContext, result, -1, SQLITE_TRANSIENT);
2313 * This function enhances sqlite by adding a "base160_next()" function which is
2314 * accessible via queries.
2316 * Retrieve the next-greater number in the base160 sequence for the terminal
2317 * tree node (the last four digits). Only one tree level (four digits) is
2321 * A character string: either an empty string (in which case no operation is
2322 * performed), or a string of base160 digits with a length of a multiple of
2326 * Upon return, the trailing four digits (one tree level) will have been
2330 base160next_sql(sqlite3_context * hContext,
2332 sqlite3_value ** argv)
2336 unsigned char * pTab;
2337 unsigned char * pBase160 =
2338 strdup(sqlite3_value_text(argv[0]));
2339 unsigned char * pStart = pBase160;
2342 * We need a minimum of four digits, and we will always get a multiple
2345 if (pBase160 != NULL &&
2346 (len = strlen(pBase160)) >= 4 &&
2349 if (pBase160 == NULL) {
2351 sqlite3_result_null(hContext);
2355 pBase160 += strlen(pBase160) - 1;
2357 /* We only carry through four digits: one level in the tree */
2358 for (i = 0; i < 4; i++) {
2360 /* What base160 value does this digit have? */
2361 pTab = strchr(base160tab, *pBase160);
2363 /* Is there a carry? */
2364 if (pTab < base160tab + sizeof(base160tab) - 1) {
2367 * Nope. Just increment this value and we're
2370 *pBase160 = *++pTab;
2375 * There's a carry. This value gets
2376 * base160tab[0], we decrement the buffer
2377 * pointer to get the next higher-order digit,
2378 * and continue in the loop.
2380 *pBase160-- = base160tab[0];
2384 sqlite3_result_text(hContext,
2389 sqlite3_result_value(hContext, argv[0]);
2390 if (pBase160 != NULL) {
2398 static int lock_debug(struct ldb_module * module,
2399 const char * lockname,
2400 const char * pFileName,
2404 struct lsqlite3_private * lsqlite3 = module->private_data;
2406 printf("%s(%d): LOCK (%d) ",
2407 pFileName, linenum, lsqlite3->lock_count);
2408 ret = lsqlite3_lock(module, lockname);
2409 printf("got %d\n", ret);
2415 static int unlock_debug(struct ldb_module * module,
2416 const char * lockname,
2417 const char * pFileName,
2421 struct lsqlite3_private * lsqlite3 = module->private_data;
2423 ret = lsqlite3_unlock(module, lockname);
2424 printf("%s(%d): UNLOCK (%d) got %d\n",
2425 pFileName, linenum, lsqlite3->lock_count, ret);