r7443: reorg functions for readability
[kai/samba.git] / source4 / lib / ldb / ldb_sqlite3 / ldb_sqlite3.c
1 /* 
2    ldb database library
3    
4    Copyright (C) Derrell Lipman  2005
5    
6    ** NOTE! The following LGPL license applies to the ldb
7    ** library. This does NOT imply that all of Samba is released
8    ** under the LGPL
9    
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.
14    
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.
19    
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
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb sqlite3 backend
29  *
30  *  Description: core files for SQLITE3 backend
31  *
32  *  Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
33  */
34
35 #include <stdarg.h>
36 #include "includes.h"
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"
42
43 /*
44  * Macros used throughout
45  */
46
47 #ifndef FALSE
48 # define FALSE  (0)
49 # define TRUE   (! FALSE)
50 #endif
51
52 #define QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...)        \
53     do {                                                        \
54             if (query_norows(lsqlite3, sql) != 0) {             \
55                 if (bRollbackOnError) {                         \
56                         query_norows(lsqlite3,                  \
57                                        "ROLLBACK;");            \
58                 }                                               \
59                 return -1;                                      \
60         }                                                       \
61     } while (0)
62
63 #define QUERY_INT(lsqlite3, result_var, bRollbackOnError, sql...)       \
64     do {                                                                \
65             if (query_int(lsqlite3, &result_var, sql) != 0) {           \
66                     if (bRollbackOnError) {                             \
67                             query_norows(lsqlite3,                      \
68                                                   "ROLLBACK;");         \
69                     }                                                   \
70                     return -1;                                          \
71             }                                                           \
72     } while (0)
73
74
75 /*
76  * Forward declarations
77  */
78 static int
79 lsqlite3_rename(struct ldb_module * module,
80                 const char * olddn,
81                 const char * newdn);
82
83 static int
84 lsqlite3_delete(struct ldb_module *module,
85                 const char *dn);
86
87 static int
88 lsqlite3_search(struct ldb_module * module,
89                 const char * pBaseDN,
90                 enum ldb_scope scope,
91                 const char * pExpression,
92                 const char * const attrs[],
93                 struct ldb_message *** res);
94
95 static int
96 lsqlite3_add(struct ldb_module *module,
97              const struct ldb_message *msg);
98
99 static int
100 lsqlite3_modify(struct ldb_module *module,
101                 const struct ldb_message *msg);
102
103 static int
104 lsqlite3_lock(struct ldb_module *module,
105               const char *lockname);
106
107 static int
108 lsqlite3_unlock(struct ldb_module *module,
109                 const char *lockname);
110
111 static const char *
112 lsqlite3_errstring(struct ldb_module *module);
113
114 static int
115 initialize(struct lsqlite3_private *lsqlite3,
116            const char *url);
117
118 static int
119 destructor(void *p);
120
121 static int
122 query_norows(const struct lsqlite3_private *lsqlite3,
123              const char *pSql,
124              ...);
125
126 static int
127 query_int(const struct lsqlite3_private * lsqlite3,
128           long long * pRet,
129           const char * pSql,
130           ...);
131
132 static int case_fold_attr_required(void * hUserData,
133                                    char *attr);
134
135 static char *
136 parsetree_to_sql(struct ldb_module *module,
137                           char * hTalloc,
138                  const struct ldb_parse_tree *t);
139
140 static char *
141 parsetree_to_tablelist(struct ldb_module *module,
142                        char * hTalloc,
143                        const struct ldb_parse_tree *t);
144
145 static int
146 new_attr(struct ldb_module * module,
147          char * pAttrName);
148
149 static int
150 msg_to_sql(struct ldb_module * module,
151            const struct ldb_message * msg,
152            long long eid,
153            int use_flags);
154
155 static int
156 new_dn(struct ldb_module * module,
157        char * pDN,
158        long long * pEID);
159
160
161 /*
162  * Table of operations for the sqlite3 backend
163  */
164 static const struct ldb_module_ops lsqlite3_ops = {
165         "sqlite",
166         lsqlite3_search,
167         lsqlite3_add,
168         lsqlite3_modify,
169         lsqlite3_delete,
170         lsqlite3_rename,
171         lsqlite3_lock,
172         lsqlite3_unlock,
173         lsqlite3_errstring
174 };
175
176
177
178
179 /*
180  * Public functions
181  */
182
183
184 /*
185  * connect to the database
186  */
187 struct ldb_context *
188 lsqlite3_connect(const char *url, 
189                  unsigned int flags, 
190                  const char *options[])
191 {
192         int                         i;
193         int                         ret;
194         struct ldb_context *        ldb = NULL;
195         struct lsqlite3_private *   lsqlite3 = NULL;
196
197         ldb = talloc(NULL, struct ldb_context);
198         if (!ldb) {
199                 errno = ENOMEM;
200                 goto failed;
201         }
202
203         lsqlite3 = talloc(ldb, struct lsqlite3_private);
204         if (!lsqlite3) {
205                 errno = ENOMEM;
206                 goto failed;
207         }
208
209         lsqlite3->sqlite = NULL;
210         lsqlite3->options = NULL;
211         lsqlite3->lock_count = 0;
212
213         ret = initialize(lsqlite3, url);
214         if (ret != SQLITE_OK) {
215                 goto failed;
216         }
217
218         talloc_set_destructor(lsqlite3, destructor);
219
220         ldb->modules = talloc(ldb, struct ldb_module);
221         if (!ldb->modules) {
222                 errno = ENOMEM;
223                 goto failed;
224         }
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;
229
230         if (options) {
231                 /*
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)
234                  */
235                 for (i=0;options[i];i++) ;
236
237                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
238                 if (!lsqlite3->options) {
239                         goto failed;
240                 }
241                 
242                 for (i=0;options[i];i++) {
243
244                         lsqlite3->options[i+1] = NULL;
245                         lsqlite3->options[i] =
246                                 talloc_strdup(lsqlite3->options, options[i]);
247                         if (!lsqlite3->options[i]) {
248                                 goto failed;
249                         }
250                 }
251         }
252
253         return ldb;
254
255 failed:
256         if (lsqlite3->sqlite != NULL) {
257                 (void) sqlite3_close(lsqlite3->sqlite);
258         }
259         talloc_free(ldb);
260         return NULL;
261 }
262
263
264 /*
265  * Interface functions referenced by lsqlite3_ops
266  */
267
268 /* rename a record */
269 static int
270 lsqlite3_rename(struct ldb_module * module,
271                 const char * olddn,
272                 const char * newdn)
273 {
274         /* ignore ltdb specials */
275         if (olddn[0] == '@' ||newdn[0] == '@') {
276                 return 0;
277         }
278
279 #warning "lsqlite3_rename() is not yet supported"
280         return -1;
281 }
282
283 /* delete a record */
284 static int
285 lsqlite3_delete(struct ldb_module *module,
286                 const char *dn)
287 {
288         /* ignore ltdb specials */
289         if (dn[0] == '@') {
290                 return 0;
291         }
292         
293         return -1;
294 }
295
296 /* search for matching records */
297 static int
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)
304 {
305         long long                   eid = 0;
306         char *                      sql;
307         char *                      sql_constraints;
308         char *                      table_list;
309         char *                      hTalloc;
310         struct ldb_parse_tree *     pTree;
311         struct lsqlite3_private *   lsqlite3 = module->private_data;
312         
313         if (pBaseDN == NULL) {
314                 pBaseDN = "";
315         }
316
317         /* Begin a transaction */
318         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN IMMEDIATE;");
319
320         /*
321          * Obtain the eid of the base DN
322          */
323         QUERY_INT(lsqlite3,
324                   eid,
325                   TRUE,
326                   "SELECT eid "
327                   "  FROM ldb_attr_dn "
328                   "  WHERE attr_value = %Q;",
329                   pBaseDN);
330
331         /* Parse the filter expression into a tree we can work with */
332         if ((pTree = ldb_parse_tree(module->ldb, pExpression)) == NULL) {
333                 return -1;
334         }
335         
336         /* Allocate a temporary talloc context */
337         hTalloc = talloc_new(module->ldb);
338
339         /* Move the parse tree to our temporary context */
340         talloc_steal(hTalloc, pTree);
341         
342         /* Convert filter into a series of SQL statements (constraints) */
343         sql_constraints = parsetree_to_sql(module, hTalloc, pTree);
344         
345         /* Get the list of attribute names to use as our extra table list */
346         table_list = parsetree_to_tablelist(module, hTalloc, pTree);
347
348         switch(scope) {
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"
356                         "       FROM ldb_entry,\n"
357                         "            ldb_descendants,\n"
358                         "            %q\n"
359                         "       WHERE ldb_descendants.aeid = %lld\n"
360                         "         AND ldb_entry.eid = ldb_descendants.deid\n"
361                         "         AND ldap_entry.eid IN\n"
362                         "%s"
363                         ");",
364                         table_list,
365                         eid,
366                         sql_constraints);
367                 break;
368
369         case LDB_SCOPE_BASE:
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"
375                         "       FROM %q\n"
376                         "       WHERE ldb_entry.eid = %lld\n"
377                         "         AND ldb_entry.eid IN\n"
378                         "%s"
379                         ");",
380                         table_list,
381                         eid,
382                         sql_constraints);
383                 break;
384
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, "
392                         "            %q\n"
393                         "       WHERE ldb_entry.eid = pchild.eid "
394                         "         AND pchild.peid = %lld "
395                         "         AND ldb_entry.eid IN\n"
396                         "%s"
397                         ");",
398                         table_list,
399                         eid,
400                         sql_constraints);
401                 break;
402         }
403
404 #warning "retrieve and return the result set of the search here"
405
406         /* End the transaction */
407         QUERY_NOROWS(lsqlite3, FALSE, "END TRANSACTION;");
408
409         return 0;
410 }
411
412
413 /* add a record */
414 static int
415 lsqlite3_add(struct ldb_module *module,
416              const struct ldb_message *msg)
417 {
418         long long                   eid;
419         struct lsqlite3_private *   lsqlite3 = module->private_data;
420
421         /* ignore ltdb specials */
422         if (msg->dn[0] == '@') {
423                 return 0;
424         }
425
426         /* Begin a transaction */
427         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
428
429         /*
430          * Build any portions of the directory tree that don't exist.  If the
431          * final component already exists, it's an error.
432          */
433         if (new_dn(module, msg->dn, &eid) != 0) {
434                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
435                 return -1;
436         }
437
438         /* Add attributes to this new entry */
439         if (msg_to_sql(module, msg, eid, FALSE) != 0) {
440                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
441                 return -1;
442         }
443
444         /* Everything worked.  Commit it! */
445         QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
446         return 0;
447 }
448
449
450 /* modify a record */
451 static int
452 lsqlite3_modify(struct ldb_module *module,
453                 const struct ldb_message *msg)
454 {
455         struct lsqlite3_private *   lsqlite3 = module->private_data;
456
457         /* ignore ltdb specials */
458         if (msg->dn[0] == '@') {
459                 return 0;
460         }
461
462         /* Begin a transaction */
463         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
464
465         /* Everything worked.  Commit it! */
466         QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
467         return 0 ;
468 }
469
470 /* obtain a named lock */
471 static int
472 lsqlite3_lock(struct ldb_module *module,
473               const char *lockname)
474 {
475         if (lockname == NULL) {
476                 return -1;
477         }
478
479         /* TODO implement a local locking mechanism here */
480
481         return 0;
482 }
483
484 /* release a named lock */
485 static int
486 lsqlite3_unlock(struct ldb_module *module,
487                 const char *lockname)
488 {
489         if (lockname == NULL) {
490                 return -1;
491         }
492
493         /* TODO implement a local locking mechanism here */
494
495         return 0;
496 }
497
498 /* return extended error information */
499 static const char *
500 lsqlite3_errstring(struct ldb_module *module)
501 {
502         struct lsqlite3_private *   lsqlite3 = module->private_data;
503
504         return sqlite3_errmsg(lsqlite3->sqlite);
505 }
506
507
508
509
510 /*
511  * Static functions
512  */
513
514 static int
515 initialize(struct lsqlite3_private *lsqlite3,
516            const char *url)
517 {
518         int             ret;
519         long long       queryInt;
520         const char *    pTail;
521         sqlite3_stmt *  stmt;
522         const char *    schema =       
523                 "-- ------------------------------------------------------"
524
525                 "PRAGMA auto_vacuum=1;"
526
527                 "-- ------------------------------------------------------"
528
529                 "BEGIN EXCLUSIVE;"
530
531                 "-- ------------------------------------------------------"
532
533                 "CREATE TABLE ldb_info AS"
534                 "  SELECT 'LDB' AS database_type,"
535                 "         '1.0' AS version;"
536
537                 "-- ------------------------------------------------------"
538                 "-- Schema"
539
540                 "/*"
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."
546                 " */"
547                 "CREATE TABLE ldb_entry"
548                 "("
549                 "  -- Unique identifier of this LDB entry"
550                 "  eid                   INTEGER PRIMARY KEY,"
551
552                 "  -- Unique identifier of the parent LDB entry"
553                 "  peid                  INTEGER REFERENCES ldb_entry,"
554
555                 "  -- Distinguished name of this entry"
556                 "  dn                    TEXT,"
557
558                 "  -- Time when the entry was created"
559                 "  create_timestamp      INTEGER,"
560
561                 "  -- Time when the entry was last modified"
562                 "  modify_timestamp      INTEGER,"
563
564                 "  -- Attributes of this entry, in the form"
565                 "  --   attr\1value\0[attr\1value\0]*\0"
566                 "  entry_data            TEXT"
567                 ");"
568
569
570                 "/*"
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."
575                 " *"
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. "
583                 " */"
584                 "CREATE TABLE ldb_descendants"
585                 "("
586                 "  -- The unique identifier of the ancestor LDB entry"
587                 "  aeid                  INTEGER REFERENCES ldb_entry,"
588
589                 "  -- The unique identifier of the descendant LDB entry"
590                 "  deid                  INTEGER REFERENCES ldb_entry"
591                 ");"
592
593
594                 "CREATE TABLE ldb_object_classes"
595                 "("
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,"
600
601                 "  -- tree_key tracks the position of the class in"
602                 "  -- the hierarchy"
603                 "  tree_key              TEXT UNIQUE"
604                 ");"
605
606                 "/*"
607                 " * There is one attribute table per searchable attribute."
608                 " */"
609                 "/*"
610                 "CREATE TABLE ldb_attr_ATTRIBUTE_NAME"
611                 "("
612                 "  -- The unique identifier of the LDB entry"
613                 "  eid                   INTEGER REFERENCES ldb_entry,"
614
615                 "  -- Normalized attribute value"
616                 "  attr_value            TEXT"
617                 ");"
618                 "*/"
619
620
621                 "-- ------------------------------------------------------"
622                 "-- Indexes"
623
624
625                 "-- ------------------------------------------------------"
626                 "-- Triggers"
627
628                 "CREATE TRIGGER ldb_entry_insert_tr"
629                 "  AFTER INSERT"
630                 "  ON ldb_entry"
631                 "  FOR EACH ROW"
632                 "    BEGIN"
633                 "      UPDATE ldb_entry"
634                 "        SET create_timestamp = strftime('%s', 'now'),"
635                 "            modify_timestamp = strftime('%s', 'now')"
636                 "        WHERE eid = new.eid;"
637                 "    END;"
638
639                 "CREATE TRIGGER ldb_entry_update_tr"
640                 "  AFTER UPDATE"
641                 "  ON ldb_entry"
642                 "  FOR EACH ROW"
643                 "    BEGIN"
644                 "      UPDATE ldb_entry"
645                 "        SET modify_timestamp = strftime('%s', 'now')"
646                 "        WHERE eid = old.eid;"
647                 "    END;"
648
649                 "-- ------------------------------------------------------"
650                 "-- Table initialization"
651
652                 "/* We need an implicit 'top' level object class */"
653                 "INSERT INTO ldb_attributes (attr_name,"
654                 "                            parent_tree_key)"
655                 "  SELECT 'top', '';"
656
657                 "-- ------------------------------------------------------"
658
659                 "COMMIT;"
660
661                 "-- ------------------------------------------------------"
662                 ;
663         
664         /* Skip protocol indicator of url  */
665         if (strncmp(url, "sqlite://", 9) != 0) {
666                 return SQLITE_MISUSE;
667         }
668
669         /* Update pointer to just after the protocol indicator */
670         url += 9;
671                 
672         /* Try to open the (possibly empty/non-existent) database */
673         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
674                 return ret;
675         }
676
677         /* Begin a transaction */
678         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
679
680         /* Determine if this is a new database.  No tables means it is. */
681         QUERY_INT(lsqlite3,
682                   queryInt,
683                   TRUE,
684                   "SELECT COUNT(*) "
685                   "  FROM sqlite_master "
686                   "  WHERE type = 'table';");
687
688         if (queryInt == 0) {
689                 /*
690                  * Create the database schema
691                  */
692                 for (pTail = discard_const_p(char, schema); pTail != NULL; ) {
693
694                         if ((ret = sqlite3_prepare(
695                                      lsqlite3->sqlite,
696                                      pTail,
697                                      -1,
698                                      &stmt,
699                                      &pTail)) != SQLITE_OK ||
700                             (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
701                             (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
702
703                                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
704                                 (void) sqlite3_close(lsqlite3->sqlite);
705                                 return ret;
706                         }
707                 }
708         } else {
709                 /*
710                  * Ensure that the database we opened is one of ours
711                  */
712                 if (query_int(lsqlite3,
713                               &queryInt,
714                               "SELECT "
715                               "  (SELECT COUNT(*) = 3"
716                               "     FROM sqlite_master "
717                               "     WHERE type = 'table' "
718                               "       AND name IN "
719                               "         ("
720                               "           'ldb_entry', "
721                               "           'ldb_descendants', "
722                               "           'ldb_object_classes' "
723                               "         ) "
724                               "  ) "
725                               "  AND "
726                               "  (SELECT 1 "
727                               "     FROM ldb_info "
728                               "     WHERE database_type = 'LDB' "
729                               "       AND version = '1.0'"
730                               "  );") != 0 ||
731                     queryInt != 1) {
732                 
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;
737                 }
738         }
739
740         /* Commit the transaction */
741         QUERY_NOROWS(lsqlite3, FALSE, "COMMIT;");
742
743         return SQLITE_OK;
744 }
745
746 static int
747 destructor(void *p)
748 {
749         struct lsqlite3_private *   lsqlite3 = p;
750
751         (void) sqlite3_close(lsqlite3->sqlite);
752         return 0;
753 }
754
755
756 /*
757  * query_norows()
758  *
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.
763  */
764 static int
765 query_norows(const struct lsqlite3_private *lsqlite3,
766              const char *pSql,
767              ...)
768 {
769         int             ret;
770         int             bLoop;
771         char *          p;
772         const char *    pTail;
773         sqlite3_stmt *  pStmt;
774         va_list         args;
775         
776         /* Begin access to variable argument list */
777         va_start(args, pSql);
778
779         /* Format the query */
780         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
781                 return -1;
782         }
783
784         /*
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.
788          */
789         for (bLoop = TRUE; bLoop; ) {
790
791                 /* Compile the SQL statement into sqlite virtual machine */
792                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
793                                            pTail,
794                                            -1,
795                                            &pStmt,
796                                            &pTail)) != SQLITE_OK) {
797                         ret = -1;
798                         break;
799                 }
800                 
801                 /* No rows expected, so just step through machine code once */
802                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
803                         (void) sqlite3_finalize(pStmt);
804                         continue;
805                 } else if (ret != SQLITE_DONE) {
806                         (void) sqlite3_finalize(pStmt);
807                         ret = -1;
808                         break;
809                 }
810
811                 /* Free the virtual machine */
812                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
813                         (void) sqlite3_finalize(pStmt);
814                         continue;
815                 } else if (ret != SQLITE_OK) {
816                         (void) sqlite3_finalize(pStmt);
817                         ret = -1;
818                         break;
819                 }
820
821                 /*
822                  * Normal condition is only one time through loop.  Loop is
823                  * rerun in error conditions, via "continue", above.
824                  */
825                 ret = 0;
826                 bLoop = FALSE;
827         }
828
829         /* All done with variable argument list */
830         va_end(args);
831
832         /* Free the memory we allocated for our query string */
833         sqlite3_free(p);
834
835         return ret;
836 }
837
838
839 /*
840  * query_int()
841  *
842  * This function is used for the common case of queries that return a single
843  * integer value.
844  *
845  * NOTE: If more than one value is returned by the query, all but the first
846  * one will be ignored.
847  */
848 static int
849 query_int(const struct lsqlite3_private * lsqlite3,
850           long long * pRet,
851           const char * pSql,
852           ...)
853 {
854         int             ret;
855         int             bLoop;
856         char *          p;
857         const char *    pTail;
858         sqlite3_stmt *  pStmt;
859         va_list         args;
860         
861         /* Begin access to variable argument list */
862         va_start(args, pSql);
863
864         /* Format the query */
865         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
866                 return -1;
867         }
868
869         /*
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.
873          */
874         for (bLoop = TRUE; bLoop; ) {
875
876                 /* Compile the SQL statement into sqlite virtual machine */
877                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
878                                            pTail,
879                                            -1,
880                                            &pStmt,
881                                            &pTail)) != SQLITE_OK) {
882                         ret = -1;
883                         break;
884                 }
885                 
886                 /* No rows expected, so just step through machine code once */
887                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
888                         (void) sqlite3_finalize(pStmt);
889                         continue;
890                 } else if (ret != SQLITE_ROW) {
891                         (void) sqlite3_finalize(pStmt);
892                         ret = -1;
893                         break;
894                 }
895
896                 /* Get the value to be returned */
897                 *pRet = sqlite3_column_int64(pStmt, 0);
898
899                 /* Free the virtual machine */
900                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
901                         (void) sqlite3_finalize(pStmt);
902                         continue;
903                 } else if (ret != SQLITE_OK) {
904                         (void) sqlite3_finalize(pStmt);
905                         ret = -1;
906                         break;
907                 }
908
909                 /*
910                  * Normal condition is only one time through loop.  Loop is
911                  * rerun in error conditions, via "continue", above.
912                  */
913                 ret = 0;
914                 bLoop = FALSE;
915         }
916
917         /* All done with variable argument list */
918         va_end(args);
919
920         /* Free the memory we allocated for our query string */
921         sqlite3_free(p);
922
923         return ret;
924 }
925
926
927 /*
928   callback function used in call to ldb_dn_fold() for determining whether an
929   attribute type requires case folding.
930 */
931 static int
932 case_fold_attr_required(void * hUserData,
933                         char *attr)
934 {
935 //        struct ldb_module * module = hUserData;
936         
937 #warning "currently, all attributes require case folding"
938         return TRUE;
939 }
940
941
942 /*
943  * add a single set of ldap message values to a ldb_message
944  */
945
946 #warning "add_msg_attr() not yet implemented or used"
947 #if 0
948 static int
949 add_msg_attr(struct ldb_context *ldb,
950                       struct ldb_message *msg, 
951                       const char *attr,
952                       struct berval **bval)
953 {
954         int                          i;
955         int                          count;
956         struct ldb_message_element * el;
957
958         count = ldap_count_values_len(bval);
959
960         if (count <= 0) {
961                 return -1;
962         }
963
964         el = talloc_realloc(msg, msg->elements, struct ldb_message_element, 
965                               msg->num_elements + 1);
966         if (!el) {
967                 errno = ENOMEM;
968                 return -1;
969         }
970
971         msg->elements = el;
972
973         el = &msg->elements[msg->num_elements];
974
975         el->name = talloc_strdup(msg->elements, attr);
976         if (!el->name) {
977                 errno = ENOMEM;
978                 return -1;
979         }
980         el->flags = 0;
981
982         el->num_values = 0;
983         el->values = talloc_array(msg->elements, struct ldb_val, count);
984         if (!el->values) {
985                 errno = ENOMEM;
986                 return -1;
987         }
988
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) {
992                         return -1;
993                 }
994                 el->values[i].length = bval[i]->bv_len;
995                 el->num_values++;
996         }
997
998         msg->num_elements++;
999
1000         return 0;
1001 }
1002 #endif
1003
1004 static char *
1005 parsetree_to_sql(struct ldb_module *module,
1006                           char * hTalloc,
1007                           const struct ldb_parse_tree *t)
1008 {
1009         int                     i;
1010         char *                  child;
1011         char *                  p;
1012         char *                  ret = NULL;
1013         char *                  pAttrName;
1014         
1015
1016         switch(t->operation) {
1017                 case LDB_OP_SIMPLE:
1018                         break;
1019
1020                 case LDB_OP_AND:
1021                         ret = parsetree_to_sql(module,
1022                                                hTalloc,
1023                                                t->u.list.elements[0]);
1024
1025                         for (i = 1; i < t->u.list.num_elements; i++) {
1026                                 child =
1027                                         parsetree_to_sql(
1028                                                 module,
1029                                                 hTalloc,
1030                                                 t->u.list.elements[i]);
1031                                 ret = talloc_asprintf_append(ret,
1032                                                              "INTERSECT\n"
1033                                                              "%s\n",
1034                                                              child);
1035                                 talloc_free(child);
1036                         }
1037
1038                         child = ret;
1039                         ret = talloc_asprintf("(\n"
1040                                               "%s\n"
1041                                               ")\n",
1042                                               child);
1043                         talloc_free(child);
1044                         return ret;
1045
1046                 case LDB_OP_OR:
1047                         child =
1048                                 parsetree_to_sql(
1049                                         module,
1050                                         hTalloc,
1051                                         t->u.list.elements[0]);
1052
1053                         for (i = 1; i < t->u.list.num_elements; i++) {
1054                                 child =
1055                                         parsetree_to_sql(
1056                                                 module,
1057                                                 hTalloc,
1058                                                 t->u.list.elements[i]);
1059                                 ret = talloc_asprintf_append(ret,
1060                                                              "UNION\n"
1061                                                              "%s\n",
1062                                                              child);
1063                                 talloc_free(child);
1064                         }
1065                         child = ret;
1066                         ret = talloc_asprintf("(\n"
1067                                               "%s\n"
1068                                               ")\n",
1069                                               child);
1070                         talloc_free(child);
1071                         return ret;
1072
1073                 case LDB_OP_NOT:
1074                         child =
1075                                 parsetree_to_sql(
1076                                         module,
1077                                         hTalloc,
1078                                         t->u.not.child);
1079                         ret = talloc_asprintf(hTalloc,
1080                                               "(\n"
1081                                               "  SELECT eid\n"
1082                                               "    FROM ldb_entry\n"
1083                                               "    WHERE eid NOT IN %s\n"
1084                                               ")\n",
1085                                               child);
1086                         talloc_free(child);
1087                         return ret;
1088
1089                 default:
1090                         /* should never occur */
1091                         abort();
1092         };
1093         
1094         /* Get a case-folded copy of the attribute name */
1095         pAttrName = ldb_casefold((struct ldb_context *) module,
1096                                  t->u.simple.attr);
1097
1098         /*
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.
1103          */
1104         if (t->u.simple.value.length == 1 &&
1105             (*(const char *) t->u.simple.value.data) == '*') {
1106                 /*
1107                  * Special case for "attr_name=*".  In this case, we want the
1108                  * eid corresponding to all values in the specified attribute
1109                  * table.
1110                  */
1111                 if ((p = sqlite3_mprintf("(\n"
1112                                          "  SELECT eid\n"
1113                                          "    FROM ldb_attr_%q\n"
1114                                          ")\n",
1115                                          pAttrName)) == NULL) {
1116                         return NULL;
1117                 }
1118
1119                 ret = talloc_strdup(hTalloc, p);
1120                 sqlite3_free(p);
1121
1122         } else if (strcasecmp(t->u.simple.attr, "objectclass") == 0) {
1123                 /*
1124                  * For object classes, we want to search for all objectclasses
1125                  * that are subclasses as well.
1126                  */
1127                 if ((p = sqlite3_mprintf(
1128                              "(\n"
1129                              "  SELECT eid\n"
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"
1138                              ")\n",
1139                              t->u.simple.value.data)) == NULL) {
1140                         return NULL;
1141                 }
1142
1143                 ret = talloc_strdup(hTalloc, p);
1144                 sqlite3_free(p);
1145
1146         } else {
1147                 /* A normal query. */
1148                 if ((p = sqlite3_mprintf("(\n"
1149                                          "  SELECT eid\n"
1150                                          "    FROM ldb_attr_%q\n"
1151                                          "    WHERE attr_value = %Q\n"
1152                                          ")\n",
1153                                          pAttrName,
1154                                          t->u.simple.value.data)) == NULL) {
1155                         return NULL;
1156                 }
1157
1158                 ret = talloc_strdup(hTalloc, p);
1159                 sqlite3_free(p);
1160         }
1161         return ret;
1162 }
1163
1164
1165 static char *
1166 parsetree_to_tablelist(struct ldb_module *module,
1167                                 char * hTalloc,
1168                                 const struct ldb_parse_tree *t)
1169 {
1170 #warning "obtain talloc'ed array of attribute names for table list"
1171         return NULL;
1172 }
1173
1174
1175 static int
1176 new_attr(struct ldb_module * module,
1177                   char * pAttrName)
1178 {
1179         struct lsqlite3_private *   lsqlite3 = module->private_data;
1180
1181         /* NOTE: pAttrName is assumed to already be case-folded here! */
1182         QUERY_NOROWS(lsqlite3,
1183                      FALSE,
1184                      "CREATE TABLE ldb_attr_%q "
1185                      "("
1186                      "  eid        INTEGER REFERENCES ldb_entry, "
1187                      "  attr_value TEXT"
1188                      ");",
1189                      pAttrName);
1190
1191         return 0;
1192 }
1193
1194 /*
1195  * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
1196  * requests in the ldb_message
1197  */
1198 static int
1199 msg_to_sql(struct ldb_module * module,
1200            const struct ldb_message * msg,
1201            long long eid,
1202            int use_flags)
1203 {
1204         int                         flags;
1205         char *                      pAttrName;
1206         unsigned int                i;
1207         unsigned int                j;
1208         struct lsqlite3_private *   lsqlite3 = module->private_data;
1209
1210         for (i = 0; i < msg->num_elements; i++) {
1211                 const struct ldb_message_element *el = &msg->elements[i];
1212
1213                 if (! use_flags) {
1214                         flags = LDB_FLAG_MOD_ADD;
1215                 } else {
1216                         flags = el->flags & LDB_FLAG_MOD_MASK;
1217                 }
1218
1219                 /* Get a case-folded copy of the attribute name */
1220                 pAttrName = ldb_casefold((struct ldb_context *) module,
1221                                          el->name);
1222
1223                 if (flags == LDB_FLAG_MOD_ADD) {
1224                         /* Create the attribute table if it doesn't exist */
1225                         if (new_attr(module, pAttrName) != 0) {
1226                                 return -1;
1227                         }
1228                 }
1229
1230                 /* For each value of the specified attribute name... */
1231                 for (j = 0; j < el->num_values; j++) {
1232
1233                         /* ... bind the attribute value, if necessary */
1234                         switch (flags) {
1235                         case LDB_FLAG_MOD_ADD:
1236                                 QUERY_NOROWS(lsqlite3,
1237                                              FALSE,
1238                                              "INSERT INTO ldb_attr_%q "
1239                                              "    (eid, attr_value) "
1240                                              "  VALUES "
1241                                              "    (%lld, %Q);",
1242                                              pAttrName,
1243                                              eid, el->values[j].data);
1244                                 QUERY_NOROWS(lsqlite3,
1245                                              FALSE,
1246                                              "UPDATE ldb_entry "
1247                                              "  SET entry_data = "
1248                                              "        add_attr(entry_data, "
1249                                              "                 %Q, %Q) "
1250                                              "  WHERE eid = %lld;",
1251                                              el->name, el->values[j].data,
1252                                              eid);
1253                                       
1254                                 break;
1255
1256                         case LDB_FLAG_MOD_REPLACE:
1257                                 QUERY_NOROWS(lsqlite3,
1258                                              FALSE,
1259                                              "UPDATE ldb_attr_%q "
1260                                              "  SET attr_value = %Q "
1261                                              "  WHERE eid = %lld;",
1262                                              pAttrName,
1263                                              el->values[j].data,
1264                                              eid);
1265                                 QUERY_NOROWS(lsqlite3,
1266                                              FALSE,
1267                                              "UPDATE ldb_entry "
1268                                              "  SET entry_data = "
1269                                              "        mod_attr(entry_data, "
1270                                              "                 %Q, %Q) "
1271                                              "  WHERE eid = %lld;",
1272                                              el->name, el->values[j].data,
1273                                              eid);
1274                                 break;
1275
1276                         case LDB_FLAG_MOD_DELETE:
1277                                 /* No additional parameters to this query */
1278                                 QUERY_NOROWS(lsqlite3,
1279                                              FALSE,
1280                                              "DELETE FROM ldb_attr_%q "
1281                                              "  WHERE eid = %lld "
1282                                              "    AND attr_value = %Q;",
1283                                              pAttrName,
1284                                              eid,
1285                                              el->values[j].data);
1286                                 QUERY_NOROWS(lsqlite3,
1287                                              FALSE,
1288                                              "UPDATE ldb_entry "
1289                                              "  SET entry_data = "
1290                                              "        del_attr(entry_data, "
1291                                              "                 %Q, %Q) "
1292                                              "  WHERE eid = %lld;",
1293                                              el->name, el->values[j].data,
1294                                              eid);
1295                                 break;
1296                         }
1297                 }
1298         }
1299
1300         return 0;
1301 }
1302
1303
1304 static int
1305 new_dn(struct ldb_module * module,
1306        char * pDN,
1307        long long * pEID)
1308 {
1309         struct ldb_dn *             pExplodedDN;
1310         struct ldb_context *        ldb = module->ldb;
1311 //      struct lsqlite3_private *   lsqlite3 = module->private_data;
1312
1313         /* Explode and normalize the DN */
1314         if ((pExplodedDN =
1315              ldb_explode_dn(ldb,
1316                             pDN,
1317                             ldb,
1318                             case_fold_attr_required)) == NULL) {
1319                 return -1;
1320         }
1321
1322 #warning "*** new_dn() not yet fully implemented ***"
1323         return -1;
1324 }
1325
1326