📄 ldb_sqlite3.c
字号:
" FROM ldb_entry AS entry\n" " LEFT OUTER JOIN ldb_attribute_values AS av\n" " ON av.eid = entry.eid\n" " WHERE entry.eid IN\n" " (SELECT DISTINCT ldb_entry.eid\n" " FROM ldb_entry\n" " WHERE norm_dn GLOB('*,%q')\n" " AND NOT norm_dn GLOB('*,*,%q')\n" " AND ldb_entry.eid IN\n(%s)\n" " )\n" " ORDER BY entry.eid ASC;", norm_basedn, norm_basedn, sqlfilter); break; } if (query == NULL) { goto failed; } /* * / printf ("%s\n", query); / * */ lsql_ac->current_eid = 0; lsql_ac->attrs = req->op.search.attrs; lsql_ac->ares = NULL; req->handle->state = LDB_ASYNC_PENDING; ret = sqlite3_exec(lsqlite3->sqlite, query, lsqlite3_search_callback, req->handle, &errmsg); if (ret != SQLITE_OK) { if (errmsg) { ldb_set_errstring(module->ldb, errmsg); free(errmsg); } goto failed; } /* complete the last message if any */ if (lsql_ac->ares) { lsql_ac->ares->message = ldb_msg_canonicalize(module->ldb, lsql_ac->ares->message); if (lsql_ac->ares->message == NULL) goto failed; req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, lsql_ac->ares); if (req->handle->status != LDB_SUCCESS) goto failed; } req->handle->state = LDB_ASYNC_DONE; return LDB_SUCCESS;failed: return LDB_ERR_OPERATIONS_ERROR;}/* add a record */static int lsql_add(struct ldb_module *module, struct ldb_request *req){ struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private); struct lsql_context *lsql_ac; struct ldb_message *msg = req->op.add.message; long long eid; char *dn, *ndn; char *errmsg; char *query; 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; }/* 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 ?" ret = LDB_ERR_UNWILLING_TO_PERFORM; goto done; }*/ /* Others return an error */ ret = LDB_ERR_UNWILLING_TO_PERFORM; goto done; } /* create linearized and normalized dns */ dn = ldb_dn_alloc_linearized(lsql_ac, msg->dn); ndn = ldb_dn_alloc_casefold(lsql_ac, msg->dn); if (dn == NULL || ndn == NULL) { ret = LDB_ERR_OTHER; goto done; } query = lsqlite3_tprintf(lsql_ac, /* Add new entry */ "INSERT OR ABORT INTO ldb_entry " "('dn', 'norm_dn') " "VALUES ('%q', '%q');", dn, ndn); if (query == NULL) { ret = LDB_ERR_OTHER; goto done; } ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg); if (ret != SQLITE_OK) { if (errmsg) { ldb_set_errstring(module->ldb, errmsg); free(errmsg); } ret = LDB_ERR_OTHER; goto done; } eid = lsqlite3_get_eid_ndn(lsqlite3->sqlite, lsql_ac, ndn); if (eid == -1) { ret = LDB_ERR_OTHER; goto done; } for (i = 0; i < msg->num_elements; i++) { const struct ldb_message_element *el = &msg->elements[i]; const struct ldb_schema_attribute *a; char *attr; int j; /* Get a case-folded copy of the attribute name */ attr = ldb_attr_casefold(lsql_ac, el->name); if (attr == NULL) { ret = LDB_ERR_OTHER; goto done; } a = ldb_schema_attribute_by_name(module->ldb, el->name); /* For each value of the specified attribute name... */ for (j = 0; j < el->num_values; j++) { struct ldb_val value; char *insert; /* Get a canonicalised copy of the data */ a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value); if (value.data == NULL) { ret = LDB_ERR_OTHER; goto done; } insert = lsqlite3_tprintf(lsql_ac, "INSERT OR ROLLBACK INTO ldb_attribute_values " "('eid', 'attr_name', 'norm_attr_name'," " 'attr_value', 'norm_attr_value') " "VALUES ('%lld', '%q', '%q', '%q', '%q');", eid, el->name, attr, el->values[j].data, value.data); if (insert == NULL) { ret = LDB_ERR_OTHER; goto done; } ret = sqlite3_exec(lsqlite3->sqlite, insert, NULL, NULL, &errmsg); if (ret != SQLITE_OK) { if (errmsg) { ldb_set_errstring(module->ldb, errmsg); free(errmsg); } ret = LDB_ERR_OTHER; goto done; } } } if (lsql_ac->callback) { req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL); } done: req->handle->state = LDB_ASYNC_DONE; return ret;}/* modify a record */static int lsql_modify(struct ldb_module *module, struct ldb_request *req){ struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private); struct lsql_context *lsql_ac; struct ldb_message *msg = req->op.mod.message; long long eid; char *errmsg; 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; } eid = lsqlite3_get_eid(module, msg->dn); if (eid == -1) { ret = LDB_ERR_OTHER; goto done; } for (i = 0; i < msg->num_elements; i++) { const struct ldb_message_element *el = &msg->elements[i]; const struct ldb_schema_attribute *a; int flags = el->flags & LDB_FLAG_MOD_MASK; char *attr; char *mod; int j; /* Get a case-folded copy of the attribute name */ attr = ldb_attr_casefold(lsql_ac, el->name); if (attr == NULL) { ret = LDB_ERR_OTHER; goto done; } a = ldb_schema_attribute_by_name(module->ldb, el->name); switch (flags) { case LDB_FLAG_MOD_REPLACE: /* remove all attributes before adding the replacements */ mod = lsqlite3_tprintf(lsql_ac, "DELETE FROM ldb_attribute_values " "WHERE eid = '%lld' " "AND norm_attr_name = '%q';", eid, attr); if (mod == NULL) { ret = LDB_ERR_OTHER; goto done; } ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg); if (ret != SQLITE_OK) { if (errmsg) { ldb_set_errstring(module->ldb, errmsg); free(errmsg); } ret = LDB_ERR_OTHER; goto done; } /* MISSING break is INTENTIONAL */ case LDB_FLAG_MOD_ADD:#warning "We should throw an error if no value is provided!" /* For each value of the specified attribute name... */ for (j = 0; j < el->num_values; j++) { struct ldb_val value; /* Get a canonicalised copy of the data */ a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value); if (value.data == NULL) { ret = LDB_ERR_OTHER; goto done; } mod = lsqlite3_tprintf(lsql_ac, "INSERT OR ROLLBACK INTO ldb_attribute_values " "('eid', 'attr_name', 'norm_attr_name'," " 'attr_value', 'norm_attr_value') " "VALUES ('%lld', '%q', '%q', '%q', '%q');", eid, el->name, attr, el->values[j].data, value.data); if (mod == NULL) { ret = LDB_ERR_OTHER; goto done; } ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg); if (ret != SQLITE_OK) { if (errmsg) { ldb_set_errstring(module->ldb, errmsg); free(errmsg); } ret = LDB_ERR_OTHER; goto done; } } break; case LDB_FLAG_MOD_DELETE:#warning "We should throw an error if the attribute we are trying to delete does not exist!" if (el->num_values == 0) { mod = lsqlite3_tprintf(lsql_ac, "DELETE FROM ldb_attribute_values " "WHERE eid = '%lld' " "AND norm_attr_name = '%q';", eid, attr); if (mod == NULL) { ret = LDB_ERR_OTHER; goto done; } ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg); if (ret != SQLITE_OK) { if (errmsg) { ldb_set_errstring(module->ldb, errmsg); free(errmsg); } ret = LDB_ERR_OTHER; goto done; } } /* For each value of the specified attribute name... */ for (j = 0; j < el->num_values; j++) { struct ldb_val value; /* Get a canonicalised copy of the data */ a->syntax->canonicalise_fn(module->ldb, lsql_ac, &(el->values[j]), &value); if (value.data == NULL) { ret = LDB_ERR_OTHER; goto done; } mod = lsqlite3_tprintf(lsql_ac, "DELETE FROM ldb_attribute_values " "WHERE eid = '%lld' " "AND norm_attr_name = '%q' " "AND norm_attr_value = '%q';", eid, attr, value.data); if (mod == NULL) { ret = LDB_ERR_OTHER; goto done; } ret = sqlite3_exec(lsqlite3->sqlite, mod, NULL, NULL, &errmsg); if (ret != SQLITE_OK) { if (errmsg) { ldb_set_errstring(module->ldb, errmsg); free(errmsg); } ret = LDB_ERR_OTHER; goto done; } } break; } } if (lsql_ac->callback) { req->handle->status = lsql_ac->callback(module->ldb, lsql_ac->context, NULL); } done: req->handle->state = LDB_ASYNC_DONE; return ret;}/* delete a record */static int lsql_delete(struct ldb_module *module, struct ldb_request *req){ struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private); struct lsql_context *lsql_ac; long long eid; char *errmsg; 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; eid = lsqlite3_get_eid(module, req->op.del.dn); if (eid == -1) { goto done; } query = lsqlite3_tprintf(lsql_ac, /* Delete entry */ "DELETE FROM ldb_entry WHERE eid = %lld; " /* Delete attributes */ "DELETE FROM ldb_attribute_values WHERE eid = %lld; ", eid, eid); if (query == NULL) { ret = LDB_ERR_OTHER; goto done; } ret = sqlite3_exec(lsqlite3->sqlite, query, NULL, NULL, &errmsg); if (ret != SQLITE_OK) { if (errmsg) { ldb_set_errstring(module->ldb, errmsg); free(errmsg); } req->handle->status = LDB_ERR_OPERATIONS_ERROR; goto done; } if (lsql_ac->callback) { ret = lsql_ac->callback(module->ldb, lsql_ac->context, NULL); } done: req->handle->state = LDB_ASYNC_DONE; return ret;}/* rename a record */static int lsql_rename(struct ldb_module *module, struct ldb_request *req){ struct lsqlite3_private *lsqlite3 = talloc_get_type(module->private_data, struct lsqlite3_private); struct lsql_context *lsql_ac; char *new_dn, *new_cdn, *old_cdn; char *errmsg; 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; /* create linearized and normalized dns */ old_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.olddn); new_cdn = ldb_dn_alloc_casefold(lsql_ac, req->op.rename.newdn); new_dn = ldb_dn_alloc_linearized(lsql_ac, req->op.rename.newdn); if (old_cdn == NULL || new_cdn == NULL || new_dn == NULL) { goto done; } /* build the SQL query */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -