From 94d99612b9093ca83da3b6b66f875915eeabb389 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 15 Jul 2005 11:10:38 +0000 Subject: r8488: after discussions with simo, moved to a full OO interface, so you don't need to keep a 'db' variable around. The ldb object knows what it is connected to. Added a simple ldb testsuite in testprogs/ldb.js (This used to be commit cf35818648b5b649d0cd25f115a04b7b5b5311aa) --- source4/scripting/ejs/mprutil.c | 7 +++- source4/scripting/ejs/smbcalls_ldb.c | 78 ++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 41 deletions(-) (limited to 'source4/scripting/ejs') diff --git a/source4/scripting/ejs/mprutil.c b/source4/scripting/ejs/mprutil.c index 247cea0b06..f25064f245 100644 --- a/source4/scripting/ejs/mprutil.c +++ b/source4/scripting/ejs/mprutil.c @@ -202,6 +202,9 @@ struct MprVar mprLdbArray(struct ldb_message **msg, int count, const char *name) for (i=0;i 3) { + if (argc < 1 || argc > 2) { ejsSetErrorMsg(eid, "ldb.search invalid arguments"); goto failed; } - if (argc == 3 && argv[2]->type != MPR_TYPE_OBJECT) { + if (argc == 2 && argv[1]->type != MPR_TYPE_OBJECT) { ejsSetErrorMsg(eid, "ldb.search attributes must be an object"); goto failed; } - ldb = ejs_ldb_db(eid, argv[0]); + ldb = ejs_ldb_db(eid); if (ldb == NULL) { return -1; } - expression = mprToString(argv[1]); + expression = mprToString(argv[0]); if (expression == NULL) { ejsSetErrorMsg(eid, "ldb.search invalid arguments"); goto failed; } - if (argc > 2) { - attrs = mprToList(tmp_ctx, argv[2]); + if (argc == 2) { + attrs = mprToList(tmp_ctx, argv[1]); } ret = ldb_search(ldb, NULL, LDB_SCOPE_DEFAULT, expression, attrs, &res); @@ -106,18 +107,18 @@ static int ejs_ldbAddModify(MprVarHandle eid, int argc, struct MprVar **argv, struct ldb_ldif *ldif; int ret; - if (argc != 2) { + if (argc != 1) { ejsSetErrorMsg(eid, "ldb.add/modify invalid arguments"); return -1; } - ldifstring = mprToString(argv[1]); + ldifstring = mprToString(argv[0]); if (ldifstring == NULL) { ejsSetErrorMsg(eid, "ldb.add/modify invalid arguments"); return -1; } - ldb = ejs_ldb_db(eid, argv[0]); + ldb = ejs_ldb_db(eid); if (ldb == NULL) { return -1; } @@ -136,7 +137,7 @@ static int ejs_ldbAddModify(MprVarHandle eid, int argc, struct MprVar **argv, /* perform an ldb delete usage: - ok = ldb.delete(db, dn); + ok = ldb.delete(dn); */ static int ejs_ldbDelete(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -144,14 +145,14 @@ static int ejs_ldbDelete(MprVarHandle eid, int argc, struct MprVar **argv) struct ldb_context *ldb; int ret; - if (argc != 2) { + if (argc != 1) { ejsSetErrorMsg(eid, "ldb.delete invalid arguments"); return -1; } - dn = mprToString(argv[1]); + dn = mprToString(argv[0]); - ldb = ejs_ldb_db(eid, argv[0]); + ldb = ejs_ldb_db(eid); if (ldb == NULL) { return -1; } @@ -164,7 +165,7 @@ static int ejs_ldbDelete(MprVarHandle eid, int argc, struct MprVar **argv) /* perform an ldb rename usage: - ok = ldb.rename(db, dn1, dn2); + ok = ldb.rename(dn1, dn2); */ static int ejs_ldbRename(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -172,19 +173,19 @@ static int ejs_ldbRename(MprVarHandle eid, int argc, struct MprVar **argv) struct ldb_context *ldb; int ret; - if (argc != 3) { + if (argc != 2) { ejsSetErrorMsg(eid, "ldb.rename invalid arguments"); return -1; } - dn1 = mprToString(argv[1]); - dn2 = mprToString(argv[2]); + dn1 = mprToString(argv[0]); + dn2 = mprToString(argv[1]); if (dn1 == NULL || dn2 == NULL) { ejsSetErrorMsg(eid, "ldb.rename invalid arguments"); return -1; } - ldb = ejs_ldb_db(eid, argv[0]); + ldb = ejs_ldb_db(eid); if (ldb == NULL) { return -1; } @@ -199,7 +200,7 @@ static int ejs_ldbRename(MprVarHandle eid, int argc, struct MprVar **argv) perform an ldb modify syntax: - ok = ldb.modify(db, ldifstring); + ok = ldb.modify(ldifstring); */ static int ejs_ldbAdd(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -210,7 +211,7 @@ static int ejs_ldbAdd(MprVarHandle eid, int argc, struct MprVar **argv) perform an ldb add syntax: - ok = ldb.add(db, ldifstring); + ok = ldb.add(ldifstring); */ static int ejs_ldbModify(MprVarHandle eid, int argc, struct MprVar **argv) { @@ -220,13 +221,13 @@ static int ejs_ldbModify(MprVarHandle eid, int argc, struct MprVar **argv) /* connect to a database usage: - db = ldb.connect(dbfile); + ok = ldb.connect(dbfile); */ static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv) { struct ldb_context *ldb; const char *dbfile; - struct MprVar v; + struct MprVar *this = mprGetProperty(ejsGetLocalObject(eid), "this", 0); if (argc != 1) { ejsSetErrorMsg(eid, "ldb.connect invalid arguments"); @@ -238,13 +239,10 @@ static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv) ldb = ldb_wrap_connect(mprMemCtx(), dbfile, 0, NULL); if (ldb == NULL) { ejsSetErrorMsg(eid, "ldb.connect failed to open %s", dbfile); - mpr_Return(eid, mprCreateUndefinedVar()); } - v = mprObject("db"); - mprSetPtrChild(&v, "ldb", ldb); - - mpr_Return(eid, v); + mprSetPtrChild(this, "db", ldb); + mpr_Return(eid, mprCreateBoolVar(ldb != NULL)); return 0; } @@ -254,16 +252,18 @@ static int ejs_ldbConnect(MprVarHandle eid, int argc, char **argv) */ static int ejs_ldb_init(MprVarHandle eid, int argc, struct MprVar **argv) { - struct MprVar ldb = mprObject("ldb"); + struct MprVar *ldb; + mpr_Return(eid, mprObject("ldb")); + + ldb = ejsGetReturnValue(eid); - mprSetStringCFunction(&ldb, "connect", ejs_ldbConnect); - mprSetCFunction(&ldb, "search", ejs_ldbSearch); - mprSetCFunction(&ldb, "add", ejs_ldbAdd); - mprSetCFunction(&ldb, "modify", ejs_ldbModify); - mprSetCFunction(&ldb, "delete", ejs_ldbDelete); - mprSetCFunction(&ldb, "rename", ejs_ldbRename); + mprSetStringCFunction(ldb, "connect", ejs_ldbConnect); + mprSetCFunction(ldb, "search", ejs_ldbSearch); + mprSetCFunction(ldb, "add", ejs_ldbAdd); + mprSetCFunction(ldb, "modify", ejs_ldbModify); + mprSetCFunction(ldb, "delete", ejs_ldbDelete); + mprSetCFunction(ldb, "rename", ejs_ldbRename); - mpr_Return(eid, ldb); return 0; } -- cgit