LDB ASYNC: other backends
[kai/samba.git] / source4 / lib / ldb / ldb_sqlite3 / ldb_sqlite3.c
index efb5c1da3f3009205f39e1a0afff9ffaf6acc642..be99c29d1e6cc948cdd37df5d4bcca51b3bd2a2e 100644 (file)
@@ -19,8 +19,7 @@
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -45,51 +44,14 @@ struct lsqlite3_private {
 
 struct lsql_context {
        struct ldb_module *module;
+       struct ldb_request *req;
 
        /* search stuff */
        long long current_eid;
        const char * const * attrs;
        struct ldb_reply *ares;
-
-       /* async stuff */
-       void *context;
-       int (*callback)(struct ldb_context *, void *, struct ldb_reply *);
 };
 
-static struct ldb_handle *init_handle(struct lsqlite3_private *lsqlite3,
-                                       struct ldb_module *module,
-                                       struct ldb_request *req)
-{
-       struct lsql_context *ac;
-       struct ldb_handle *h;
-
-       h = talloc_zero(lsqlite3, struct ldb_handle);
-       if (h == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
-               return NULL;
-       }
-
-       h->module = module;
-
-       ac = talloc(h, struct lsql_context);
-       if (ac == NULL) {
-               ldb_set_errstring(module->ldb, "Out of Memory");
-               talloc_free(h);
-               return NULL;
-       }
-
-       h->private_data = (void *)ac;
-
-       h->state = LDB_ASYNC_INIT;
-       h->status = LDB_SUCCESS;
-
-       ac->module = module;
-       ac->context = req->context;
-       ac->callback = req->callback;
-
-       return h;
-}
-
 /*
  * Macros used throughout
  */
@@ -350,23 +312,7 @@ static char *parsetree_to_sql(struct ldb_module *module,
                        return NULL;
                }
 
-               if (strcasecmp(t->u.equality.attr, "objectclass") == 0) {
-               /*
-                * For object classes, we want to search for all objectclasses
-                * that are subclasses as well.
-               */
-                       return lsqlite3_tprintf(mem_ctx,
-                                       "SELECT eid  FROM ldb_attribute_values\n"
-                                       "WHERE norm_attr_name = 'OBJECTCLASS' "
-                                       "AND norm_attr_value IN\n"
-                                       "  (SELECT class_name FROM ldb_object_classes\n"
-                                       "   WHERE tree_key GLOB\n"
-                                       "     (SELECT tree_key FROM ldb_object_classes\n"
-                                       "      WHERE class_name = '%q'\n"
-                                       "     ) || '*'\n"
-                                       "  )\n", value.data);
-
-               } else if (strcasecmp(t->u.equality.attr, "dn") == 0) {
+               if (strcasecmp(t->u.equality.attr, "dn") == 0) {
                        /* DN query is a special ldb case */
                        const char *cdn = ldb_dn_get_casefold(
                                                ldb_dn_new(mem_ctx, module->ldb,
@@ -852,11 +798,6 @@ int lsql_search(struct ldb_module *module, struct ldb_request *req)
        char *query = NULL;
         int ret;
 
-       req->handle = init_handle(lsqlite3, module, req);
-       if (req->handle == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
        lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
 
        if ((( ! ldb_dn_is_valid(req->op.search.base)) || ldb_dn_is_null(req->op.search.base)) &&
@@ -1030,26 +971,14 @@ static int lsql_add(struct ldb_module *module, struct ldb_request *req)
        int i;
        int ret = LDB_SUCCESS;
 
-       req->handle = init_handle(lsqlite3, module, req);
-       if (req->handle == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
        lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
        req->handle->state = LDB_ASYNC_DONE;
        req->handle->status = LDB_SUCCESS;
 
         /* See if this is an ltdb special */
        if (ldb_dn_is_special(msg->dn)) {
-               struct ldb_dn *c;
-
-               c = ldb_dn_new(lsql_ac, module->ldb, "@SUBCLASSES");
-               if (ldb_dn_compare(msg->dn, c) == 0) {
-#warning "insert subclasses into object class tree"
-                       ret = LDB_ERR_UNWILLING_TO_PERFORM;
-                       goto done;
-               }
-
 /*
+               struct ldb_dn *c;
                c = ldb_dn_new(local_ctx, module->ldb, "@INDEXLIST");
                if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
 #warning "should we handle indexes somehow ?"
@@ -1168,25 +1097,12 @@ static int lsql_modify(struct ldb_module *module, struct ldb_request *req)
        int i;
        int ret = LDB_SUCCESS;
 
-       req->handle = init_handle(lsqlite3, module, req);
-       if (req->handle == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
        lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
        req->handle->state = LDB_ASYNC_DONE;
        req->handle->status = LDB_SUCCESS;
 
         /* See if this is an ltdb special */
        if (ldb_dn_is_special(msg->dn)) {
-               struct ldb_dn *c;
-
-               c = ldb_dn_new(lsql_ac, module->ldb, "@SUBCLASSES");
-               if (ldb_dn_compare(msg->dn, c) == 0) {
-#warning "modify subclasses into object class tree"
-                       ret = LDB_ERR_UNWILLING_TO_PERFORM;
-                       goto done;
-               }
-
                 /* Others return an error */
                ret = LDB_ERR_UNWILLING_TO_PERFORM;
                 goto done;
@@ -1363,10 +1279,6 @@ static int lsql_delete(struct ldb_module *module, struct ldb_request *req)
        int ret = LDB_SUCCESS;
 
 
-       req->handle = init_handle(lsqlite3, module, req);
-       if (req->handle == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
        lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
        req->handle->state = LDB_ASYNC_DONE;
        req->handle->status = LDB_SUCCESS;
@@ -1416,10 +1328,6 @@ static int lsql_rename(struct ldb_module *module, struct ldb_request *req)
        char *query;
        int ret = LDB_SUCCESS;
 
-       req->handle = init_handle(lsqlite3, module, req);
-       if (req->handle == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
        lsql_ac = talloc_get_type(req->handle->private_data, struct lsql_context);
        req->handle->state = LDB_ASYNC_DONE;
        req->handle->status = LDB_SUCCESS;
@@ -1535,9 +1443,61 @@ static int lsql_request(struct ldb_module *module, struct ldb_request *req)
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
-static int lsql_wait(struct ldb_handle *handle, enum ldb_wait_type type)
+static int lsql_run_request(struct ldb_module *module, struct ldb_request *req)
 {
-       return handle->status;
+       switch (req->operation) {
+       case LDB_SEARCH:
+               return lsql_search(module, req);
+               break;
+       case LDB_ADD:
+               return lsql_add(module, req);
+               break;
+       case LDB_MODIFY:
+               return lsql_modify(module, req);
+               break;
+       case LDB_DELETE:
+               return lsql_delete(module, req);
+               break;
+       case LDB_RENAME:
+               return lsql_rename(module, req);
+               break;
+/* TODO:
+       case LDB_SEQUENCE_NUMBER:
+               return lsql_sequence_number(module, req);
+               break;
+ */
+       default:
+               return lsql_request(module, req);
+               break;
+       }
+
+       return LDB_ERR_OPERATIONS_ERROR;
+}
+
+static int lsql_handle_request(struct ldb_module *module, struct ldb_request *req)
+{
+       struct lsql_context *ac;
+
+       if (check_critical_controls(req->controls)) {
+               return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
+       }
+
+       ac = talloc_zero(req, struct lsql_context);
+       if (ac == NULL) {
+               ldb_set_errstring(module->ldb, "Out of Memory");
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       ac->module = module;
+       ac->req = req;
+
+       req->handle = ldb_handle_new(req, lsql_run_request, ac);
+       if (req->handle == NULL) {
+               talloc_free(ac);
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       return LDB_SUCCESS;
 }
 
 /*
@@ -1545,16 +1505,16 @@ static int lsql_wait(struct ldb_handle *handle, enum ldb_wait_type type)
  */
 static const struct ldb_module_ops lsqlite3_ops = {
        .name              = "sqlite",
-       .search            = lsql_search,
-       .add               = lsql_add,
-        .modify            = lsql_modify,
-        .del               = lsql_delete,
-        .rename            = lsql_rename,
-       .request           = lsql_request,
+       .search            = lsql_handle_request,
+       .add               = lsql_handle_request,
+        .modify            = lsql_handle_request,
+        .del               = lsql_handle_request,
+        .rename            = lsql_handle_request,
+       .request           = lsql_handle_request,
        .start_transaction = lsql_start_trans,
        .end_transaction   = lsql_end_trans,
        .del_transaction   = lsql_del_trans,
-       .wait              = lsql_wait,
+       /* TODO: .sequence_number   = lsql_handle_request */
 };
 
 /*
@@ -1904,7 +1864,7 @@ failed:
        return -1;
 }
 
-int ldb_sqlite3_init(void)
-{
-       return ldb_register_backend("sqlite3", lsqlite3_connect);
-}
+const struct ldb_backend_ops ldb_sqlite3_backend_ops = {
+       .name = "sqlite3",
+       .connect_fn = lsqlite3_connect
+};