#include <murphy/common.h>
#include <murphy/common/debug.h>
+#include <murphy-db/mqi.h>
#include <murphy-db/mql.h>
#include <murphy/core/context.h>
TABLE,
CONDITION,
STATEMENT,
- SINGLEVAL
+ SINGLEVAL,
+ CREATE
};
static void adjust_lua_table_size(lua_State *, int, void *, size_t, size_t,
const char *);
+static bool create_mdb_table(mrp_lua_mdb_table_t *);
MRP_LUA_METHOD_LIST_TABLE (
tbl = (mrp_lua_mdb_table_t *)mrp_lua_create_object(L, TABLE_CLASS, NULL);
- tbl->builtin = false;
+ tbl->builtin = true;
+ tbl->handle = MQI_HANDLE_INVALID;
MRP_LUA_FOREACH_FIELD(L, 2, fldnam, fldnamlen) {
case NAME:
tbl->name = mrp_strdup(luaL_checkstring(L, -1));
+ tbl->handle = mqi_get_table_handle((char *)tbl->name);
break;
case INDEX:
tbl->columns = check_coldefs(L, -1, &tbl->ncolumn);
break;
+ case CREATE:
+ if (!lua_isboolean(L, -1)) {
+ luaL_error(L, "attempt to assign non-boolean "
+ "value to 'create' field");
+ }
+ tbl->builtin = !lua_toboolean(L, -1);
+ break;
+
default:
luaL_error(L, "unexpected field '%s'", fldnam);
break;
if (!tbl->name)
luaL_error(L, "mandatory 'name' field is unspecified");
- if (!tbl->columns || !tbl->ncolumn)
- luaL_error(L, "mandatory 'column' field is unspecified or invalid");
+
+ if (tbl->builtin) {
+ if (tbl->handle == MQI_HANDLE_INVALID)
+ luaL_error(L, "table '%s' do not exist", tbl->name);
+ if (tbl->columns && tbl->ncolumn > 0)
+ luaL_error(L, "can't specify columns for an existing table");
+ }
+ else {
+ if (tbl->handle != MQI_HANDLE_INVALID) {
+ luaL_error(L, "attempt to create an already existing table '%s'",
+ tbl->name);
+ }
+ if (tbl->columns && tbl->ncolumn > 0) {
+ if (!create_mdb_table(tbl))
+ luaL_error(L, "failed to create MDB table '%s'", tbl->name);
+ }
+ else {
+ luaL_error(L,"mandatory 'column' field is unspecified or invalid");
+ }
+ }
mrp_lua_set_object_name(L, TABLE_CLASS, tbl->name);
return TABLE;
break;
+ case 6:
+ if (!strcmp(name, "create"))
+ return CREATE;
+ break;
+
case 7:
if (!strcmp(name, "columns"))
return COLUMNS;
}
}
+static bool create_mdb_table(mrp_lua_mdb_table_t *tbl)
+{
+ char **index;
+
+ if (!tbl->columns || !tbl->ncolumn)
+ tbl->handle = MQI_HANDLE_INVALID;
+ else {
+ if (!tbl->index || !tbl->index->nstring)
+ index = NULL;
+ else
+ index = (char **)tbl->index->strings;
+
+ tbl->handle = mqi_create_table((char *)tbl->name, MQI_TEMPORARY,
+ index, tbl->columns);
+
+ if (tbl->handle == MQI_HANDLE_INVALID)
+ mrp_debug("failed to create table '%s'", tbl->name);
+ else
+ mrp_debug("table '%s' has been sucessfully created", tbl->name);
+ }
+
+ return (tbl->handle != MQI_HANDLE_INVALID);
+}
/*