svn commit: samba r7480 - in branches/SAMBA_4_0/source/lib/ldb: common ldb_sqlite3

derrell at samba.org derrell at samba.org
Sat Jun 11 03:20:27 GMT 2005


Author: derrell
Date: 2005-06-11 03:20:26 +0000 (Sat, 11 Jun 2005)
New Revision: 7480

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=7480

Log:
ldb_sqlite3 work in progress
Modified:
   branches/SAMBA_4_0/source/lib/ldb/common/ldb_explode_dn.c
   branches/SAMBA_4_0/source/lib/ldb/ldb_sqlite3/ldb_sqlite3.c


Changeset:
Modified: branches/SAMBA_4_0/source/lib/ldb/common/ldb_explode_dn.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/common/ldb_explode_dn.c	2005-06-11 03:04:40 UTC (rev 7479)
+++ branches/SAMBA_4_0/source/lib/ldb/common/ldb_explode_dn.c	2005-06-11 03:20:26 UTC (rev 7480)
@@ -377,8 +377,7 @@
                  */
 
                 /* allocate space for the normalized component */
-		if ((component->component =
-                     dest = talloc_size(component, size)) == NULL) {
+		if ((dest = talloc_size(component, size)) == NULL) {
 
                         goto failed;
                 }
@@ -396,6 +395,9 @@
                         dest += size;
 		}
 
+                /* Save the just-generated string */
+                component->component = dest;
+
 		ldb_debug(mem_ctx,
                           LDB_DEBUG_TRACE,
                           "component: [%s]\n", component->component);
@@ -432,7 +434,7 @@
 	}
 
 	/* rebuild the normalized DN */
-	if ((dn->dn = dest = talloc_size(dn, size)) == NULL) {
+	if ((dest = talloc_size(dn, size)) == NULL) {
                 goto failed;
         }
 
@@ -453,6 +455,9 @@
                 dest += size;
 	}
 
+        /* Save the just-generated string */
+        dn->dn = dest;
+
 	ldb_debug(mem_ctx, LDB_DEBUG_TRACE, "dn: [%s]\n", dn->dn);
 
         /* we don't need the copy of the DN any more */

Modified: branches/SAMBA_4_0/source/lib/ldb/ldb_sqlite3/ldb_sqlite3.c
===================================================================
--- branches/SAMBA_4_0/source/lib/ldb/ldb_sqlite3/ldb_sqlite3.c	2005-06-11 03:04:40 UTC (rev 7479)
+++ branches/SAMBA_4_0/source/lib/ldb/ldb_sqlite3/ldb_sqlite3.c	2005-06-11 03:20:26 UTC (rev 7480)
@@ -1288,9 +1288,15 @@
        char * pDN,
        long long * pEID)
 {
+        int                         nComponent;
+        int                         bFirst;
+        char *                      p;
+        char *                      pPartialDN;
+        long long                   eid;
         struct ldb_dn *             pExplodedDN;
+        struct ldb_dn_component *   pComponent;
 	struct ldb_context *        ldb = module->ldb;
-//	struct lsqlite3_private *   lsqlite3 = module->private_data;
+	struct lsqlite3_private *   lsqlite3 = module->private_data;
 
         /* Explode and normalize the DN */
         if ((pExplodedDN =
@@ -1301,8 +1307,71 @@
                 return -1;
         }
 
-#warning "*** new_dn() not yet fully implemented ***"
-        return -1;
+        /* Allocate a string to hold the partial DN of each component */
+        if ((pPartialDN = talloc_strdup(ldb, "")) == NULL) {
+                return -1;
+        }
+
+        /* For each component of the DN (starting with the last one)... */
+        eid = 0;
+        for (nComponent = pExplodedDN->comp_num - 1, bFirst = TRUE;
+             nComponent >= 0;
+             nComponent--, bFirst = FALSE) {
+                
+                /* Point to the component */
+                pComponent = pExplodedDN->components[nComponent];
+
+                /* Add this component on to the partial DN to date */
+                if ((p = talloc_asprintf(ldb,
+                                         "%s%s%s",
+                                         pComponent->component,
+                                         bFirst ? "" : ",",
+                                         pPartialDN)) == NULL) {
+                        return -1;
+                }
+
+                /* No need for the old partial DN any more */
+                talloc_free(pPartialDN);
+
+                /* Save the new partial DN */
+                pPartialDN = p;
+
+                /*
+                 * Ensure that an entry is in the ldb_entry table for this
+                 * component.  Any component other than the last one
+                 * (component 0) may already exist.  It is an error if
+                 * component 0 (the full DN requested to be be inserted)
+                 * already exists.
+                 */
+                if (bFirst) {
+                        /* This is a top-level entry.  Parent EID is null. */
+                        QUERY_NOROWS(lsqlite3,
+                                     FALSE,
+                                     "INSERT %s INTO ldb_entry "
+                                     "    (peid, dn) "
+                                     "  VALUES "
+                                     "    (NULL, %q);",
+                                     nComponent == 0 ? "" : "OR IGNORE",
+                                     pPartialDN);
+                } else {
+                        QUERY_NOROWS(lsqlite3,
+                                     FALSE,
+                                     "INSERT %s INTO ldb_entry "
+                                     "    (peid, dn) "
+                                     "  VALUES "
+                                     "    (%lld, %q);",
+                                     nComponent == 0 ? "" : "OR IGNORE",
+                                     eid, pPartialDN);
+                }
+
+                /* Get the EID of the just inserted row (the next parent) */
+                eid = sqlite3_last_insert_rowid(lsqlite3->sqlite);
+        }
+
+        /* Give 'em what they came for! */
+        *pEID = eid;
+
+        return 0;
 }
 
 
@@ -1310,18 +1379,38 @@
 new_attr(struct ldb_module * module,
                   char * pAttrName)
 {
+        long long                   bExists;
 	struct lsqlite3_private *   lsqlite3 = module->private_data;
 
-        /* NOTE: pAttrName is assumed to already be case-folded here! */
-        QUERY_NOROWS(lsqlite3,
-                     FALSE,
-                     "CREATE TABLE ldb_attr_%q "
-                     "("
-                     "  eid        INTEGER REFERENCES ldb_entry, "
-                     "  attr_value TEXT"
-                     ");",
-                     pAttrName);
+        /*
+         * NOTE:
+         *   pAttrName is assumed to already be case-folded here!
+         */
 
+        /* See if the table already exists */
+        QUERY_INT(lsqlite3,
+                  bExists,
+                  FALSE,
+                  "SELECT COUNT(*) <> 0"
+                  "  FROM sqlite_master "
+                  "  WHERE type = 'table' "
+                  "    AND tbl_name = %Q;",
+                  pAttrName);
+
+        /* Did it exist? */
+        if (! bExists) {
+                /* Nope.  Create the table */
+                QUERY_NOROWS(lsqlite3,
+                             FALSE,
+                             "CREATE TABLE ldb_attr_%q "
+                             "("
+                             "  eid        INTEGER REFERENCES ldb_entry, "
+                             "  attr_value TEXT"
+                             ");",
+                             pAttrName);
+        }
+
         return 0;
 }
 
+



More information about the samba-cvs mailing list