summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authordeva <deva>2008-05-06 15:26:39 +0000
committerdeva <deva>2008-05-06 15:26:39 +0000
commit27af2d0a32351de63977280e6deaa6d3aea8edd5 (patch)
tree43a9cf5c2cc6014edcf82b0527a8a265fc3d18db /server/src
parentcb3f60dbe46166b22ef4060c7d3d6c9073e95e27 (diff)
Added LUA parser for the mapping of external data query results.
Diffstat (limited to 'server/src')
-rw-r--r--server/src/Makefile.am10
-rw-r--r--server/src/luaquerymapper.cc134
-rw-r--r--server/src/luaquerymapper.h56
-rw-r--r--server/src/queryresult.h39
4 files changed, 237 insertions, 2 deletions
diff --git a/server/src/Makefile.am b/server/src/Makefile.am
index f06cbb9..faf6aa1 100644
--- a/server/src/Makefile.am
+++ b/server/src/Makefile.am
@@ -1,9 +1,9 @@
bin_PROGRAMS = pracrod
-pracrod_LDADD = $(LD_EFENCE) $(PQXX_LIBS) $(CONFIG_LIBS)
+pracrod_LDADD = $(LD_EFENCE) $(PQXX_LIBS) $(CONFIG_LIBS) $(LUA_LIBS)
-pracrod_CXXFLAGS = $(PQXX_CXXFLAGS) $(CONFIG_CXXFLAGS)
+pracrod_CXXFLAGS = $(PQXX_CXXFLAGS) $(CONFIG_CXXFLAGS) $(LUA_CXXFLAGS)
pracrod_SOURCES = \
pracrod.cc \
@@ -11,8 +11,11 @@ pracrod_SOURCES = \
database.cc \
configuration.cc \
exception.cc \
+ externaldataqueryhandler.cc \
+ externaldataqueryparser.cc \
journal_commit.cc \
log.cc \
+ luaquerymapper.cc \
macro_parser.cc \
resumeparser.cc \
sax_parser.cc \
@@ -28,8 +31,11 @@ EXTRA_DIST = \
database.h \
debug.h \
exception.h \
+ externaldataqueryhandler.h \
+ externaldataqueryparser.h \
journal_commit.h \
log.h \
+ luaquerymapper.h \
macro.h \
macro_parser.h \
resumeparser.h \
diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc
new file mode 100644
index 0000000..f4f6f20
--- /dev/null
+++ b/server/src/luaquerymapper.cc
@@ -0,0 +1,134 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * luaquerymapper.cc
+ *
+ * Mon May 5 15:43:52 CEST 2008
+ * Copyright 2008 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup
+ * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "luaquerymapper.h"
+
+static std::string loadresultstring(QueryResult &res, std::string group = "")
+{
+ std::string s;
+
+ std::map< std::string, std::string >::iterator v = res.values.begin();
+ while(v != res.values.end()) {
+ s += group + (*v).first + " = \"" + (*v).second + "\"\n";
+ v++;
+ }
+
+ std::map< std::string, QueryResult >::iterator g = res.groups.begin();
+ while(g != res.groups.end()) {
+ s += group + (*g).first + " = {}\n";
+ s += loadresultstring((*g).second, group + (*g).first + ".");
+ g++;
+ }
+
+ return s;
+}
+
+LUAQueryMapper::LUAQueryMapper(QueryResult &res)
+{
+ L = luaL_newstate();
+ if(L == NULL) {
+ // throw LUADataParserException("Could not create LUA state.");
+ }
+
+ luaL_openlibs(L);
+
+ // FIXME: Insert these variables directly into the LUA stack instead of parsing them.
+ std::string preload = loadresultstring(res);
+
+ int s = luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload");
+ switch(s) {
+ case 0: //no errors;
+ break;
+ case LUA_ERRSYNTAX: //syntax error during pre-compilation;
+ case LUA_ERRMEM: //memory allocation error.
+ case LUA_ERRFILE: //cannot open/read the file.
+ //throw LUADataParserException(lua_tostring(L, lua_gettop(L)));
+ break;
+ default:
+ //throw LUADataParserException("Unknown return value of luaL_loadfile.");
+ break;
+ }
+
+ // Run program (init)
+ lua_pcall(L, 0, LUA_MULTRET, 0);
+}
+
+LUAQueryMapper::~LUAQueryMapper()
+{
+ lua_close(L);
+}
+
+std::string LUAQueryMapper::map(const std::string &mapper)
+{
+ int s = luaL_loadbuffer(L, mapper.c_str(), mapper.size(), "mapper");
+ switch(s) {
+ case 0: //no errors;
+ break;
+ case LUA_ERRSYNTAX: //syntax error during pre-compilation;
+ case LUA_ERRMEM: //memory allocation error.
+ case LUA_ERRFILE: //cannot open/read the file.
+ //throw LUADataParserException(lua_tostring(L, lua_gettop(L)));
+ break;
+ default:
+ //throw LUADataParserException("Unknown return value of luaL_loadfile.");
+ break;
+ }
+
+ // Run the loaded code
+ lua_pcall(L, 0, LUA_MULTRET, 0);
+
+ return lua_tostring(L, lua_gettop(L));
+}
+
+#ifdef TEST_LUAQUERYMAPPER
+
+int main()
+{
+ QueryResult res;
+
+ QueryResult group1;
+ group1.values["dims"] = "42";
+
+ QueryResult group2;
+ group2.values["foo"] = "bar";
+ group2.values["bar"] = "foo";
+ group2.groups["fnuller"] = group1;
+
+ res.values["dims"] = "42";
+ res.groups["dimmer"] = group2;
+
+ LUAQueryMapper mapper(res);
+
+ std::string luamap = "return dimmer.fnuller.dims * 2 + dims";
+ printf("%s\n", mapper.map(luamap).c_str());
+
+ luamap = "return math.sin(dimmer.fnuller.dims * 2 + dims)";
+ printf("%s\n", mapper.map(luamap).c_str());
+
+ return 0;
+}
+
+#endif/*TEST_LUAQUERYMAPPER*/
diff --git a/server/src/luaquerymapper.h b/server/src/luaquerymapper.h
new file mode 100644
index 0000000..bd1cdd3
--- /dev/null
+++ b/server/src/luaquerymapper.h
@@ -0,0 +1,56 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * luaquerymapper.h
+ *
+ * Mon May 5 15:43:51 CEST 2008
+ * Copyright 2008 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup
+ * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __PRACRO_LUAQUERYMAPPER_H__
+#define __PRACRO_LUAQUERYMAPPER_H__
+
+#include "queryresult.h"
+
+#include <lua.hpp>
+#include <lauxlib.h>
+
+/**
+ * The LUAQueryMapper class takes the result of an external data query and
+ * applies the associated map.
+ */
+class LUAQueryMapper {
+public:
+ LUAQueryMapper(QueryResult &res);
+ ~LUAQueryMapper();
+
+ /**
+ * Applies the mapping program to the result-namespace, and returns the
+ * resulting value.
+ */
+ std::string map(const std::string &mapper);
+
+private:
+ lua_State *L;
+
+};
+
+
+#endif/*__PRACRO_LUAQUERYMAPPER_H__*/
diff --git a/server/src/queryresult.h b/server/src/queryresult.h
new file mode 100644
index 0000000..632ff22
--- /dev/null
+++ b/server/src/queryresult.h
@@ -0,0 +1,39 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * queryresult.h
+ *
+ * Mon May 5 15:47:41 CEST 2008
+ * Copyright 2008 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup
+ * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __PRACRO_QUERYRESULT_H__
+#define __PRACRO_QUERYRESULT_H__
+
+#include <string>
+#include <map>
+
+class QueryResult {
+public:
+ std::map< std::string, std::string > values;
+ std::map< std::string, QueryResult > groups;
+};
+
+#endif/*__PRACRO_QUERYRESULT_H__*/