From 387203f4b37a2429aa6ac4dd7fba7f410c4f9467 Mon Sep 17 00:00:00 2001 From: brad Arant Date: Tue, 24 Sep 2024 20:20:29 -0700 Subject: [PATCH] Work on mysql and sql. --- Global.cpp | 12 ++++++++++++ Global.h | 1 + Tag.cpp | 3 +++ __if.h | 4 ++-- __mysql.cpp | 20 +++++++++++++------- __sql.cpp | 9 ++++++--- testjet.cpp | 9 +++++---- 7 files changed, 42 insertions(+), 16 deletions(-) diff --git a/Global.cpp b/Global.cpp index fec7f4e..e57e7e6 100644 --- a/Global.cpp +++ b/Global.cpp @@ -23,9 +23,13 @@ namespace jet { } void Global::addSession(coreutils::MString sessionId, __mysql *mysql) { + std::cout << "sessionId: " << sessionId << std::endl; if(sessionExists(sessionId)) coreutils::Exception("sessionid already exists."); sessions[sessionId] = mysql; + + std::cout << "::count: " << sessions.size() << std::endl; + std::cout << "::" << sessionId << std::endl; } void Global::removeSession(coreutils::MString sessionId) { @@ -70,6 +74,14 @@ namespace jet { return name; } + __mysql * Global::getSession(coreutils::MString sessionId) { + std::cout << "sessionId: " << sessionId << std::endl; + std::cout << "count: " << sessions.size() << std::endl; + if(sessions.find(sessionId) == sessions.end()) + throw coreutils::Exception("requested session is not available."); + return sessions[sessionId]; + } + coreutils::ZString Global::getSessionVariable(coreutils::MString &splitName) { if(sessions.find(splitName[0]) == sessions.end()) throw coreutils::Exception("requested session is not available in variable."); diff --git a/Global.h b/Global.h index e362bad..83d6de6 100644 --- a/Global.h +++ b/Global.h @@ -20,6 +20,7 @@ namespace jet { void removeSession(coreutils::MString sessionId); coreutils::ZString getVariable(coreutils::ZString &variable); coreutils::MString renderVariableName(coreutils::MString &name, coreutils::ZString &variable); + __mysql * getSession(coreutils::MString sessionId); coreutils::ZString getSessionVariable(coreutils::MString &splitName); std::map variables; diff --git a/Tag.cpp b/Tag.cpp index d6943f0..4437df8 100644 --- a/Tag.cpp +++ b/Tag.cpp @@ -113,6 +113,9 @@ namespace jet { } else if(ifTagName(in, "comment")) { __comment _comment(in, out, global); continue; + } else if(ifTagName(in, "sql")) { + __sql _sql(in, out, global); + continue; } else if(ifTagName(in, "for")) { __for _for(in, out, global); continue; diff --git a/__if.h b/__if.h index 0d0ee1b..6d5bec0 100644 --- a/__if.h +++ b/__if.h @@ -1,5 +1,5 @@ -#ifndef __if_h__ -#define __if_h__ +#ifndef ____if_h__ +#define ____if_h__ #include "Tag.h" #include "ZString.h" diff --git a/__mysql.cpp b/__mysql.cpp index c9818d9..13aa5a7 100644 --- a/__mysql.cpp +++ b/__mysql.cpp @@ -5,16 +5,15 @@ namespace jet { __mysql::__mysql(coreutils::ZString &in, coreutils::MString &parent, Global &global) : Tag(in, parent, global) { - processContainer(container); - + if(!variableDefined("host")) - coreutils::Exception("host must be specified for mysql tag."); + throw coreutils::Exception("host must be specified for mysql tag."); if(!variableDefined("database")) - coreutils::Exception("database must be specified for mysql tag."); + throw coreutils::Exception("database must be specified for mysql tag."); if(!variableDefined("user")) - coreutils::Exception("user must be specified for mysql tag."); + throw coreutils::Exception("user must be specified for mysql tag."); if(!variableDefined("password")) - coreutils::Exception("password must be specified for mysql tag."); + throw coreutils::Exception("password must be specified for mysql tag."); sessionId = variables["sessionid"]; @@ -22,6 +21,10 @@ namespace jet { mysql = mysql_init(NULL); mysql = mysql_real_connect(mysql, variables["host"].c_str(), variables["user"].c_str(), variables["password"].c_str(), variables["database"].c_str(), 0, NULL, 0); + + std::cout << "mysql: " << mysql << std::endl; + + processContainer(container); } @@ -32,8 +35,11 @@ namespace jet { } void __mysql::query(coreutils::MString query) { - mysql_real_query(mysql, query.getData(), query.getLength()); + std::cout << "q: " << query << std::endl; + int rc = mysql_real_query(mysql, query.getData(), query.getLength()); + std::cout << "rc: " << rc << std::endl; result = mysql_store_result(mysql); + std::cout << "result: " << result << std::endl; if(result) { row = mysql_fetch_row(result); fieldLength = mysql_fetch_lengths(result); diff --git a/__sql.cpp b/__sql.cpp index f1c4be6..8511bcc 100644 --- a/__sql.cpp +++ b/__sql.cpp @@ -10,12 +10,15 @@ namespace jet { __sql::__sql(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { + output = false; if(!hasContainer) throw coreutils::Exception("sql tag must have a container."); if(!global.sessionExists(variables["sessionid"])) - coreutils::Exception("sessionid does not exist."); - - global.sessions[variables["sessionid"]]->query(container); + throw coreutils::Exception("sessionid does not exist."); + std::cout << "cont: " << container << std::endl; + processContainer(container); + std::cout << "out: " << out << std::endl; + global.getSession(variables["sessionid"])->query(out); } diff --git a/testjet.cpp b/testjet.cpp index f1292dc..1bdec03 100644 --- a/testjet.cpp +++ b/testjet.cpp @@ -10,8 +10,11 @@ int main(int argc, char **argv) { " This is a comment and should not show up in the output.\n" " \n" " $[nonexistant]\n" - " $[session.column]\n" - " $[.column]\n" + " \n" + " select * from testdata\n" + " $[1.text]\n" + " $[1.value]\n" + " \n" " \n" " \n" " \n" @@ -27,7 +30,6 @@ int main(int argc, char **argv) { " \n" " \n" " $[test$[ix$[ix1]]$[iz]]\n" - " \n" " \n" " 789\n" " \n" @@ -36,7 +38,6 @@ int main(int argc, char **argv) { " 456\n" " \n" " \n" - " \n" " \n" " -->#[ix]<--\n" " \n"