diff --git a/Expression.cpp b/Expression.cpp index 66e962d..cf24baa 100644 --- a/Expression.cpp +++ b/Expression.cpp @@ -14,7 +14,7 @@ namespace jet { string = op1.string; if(in.unparsed().getLength() > 0) { - std::cout << "1)" << in.unparsed() << std::endl; + std::cout << "1)" << in.unparsed() << "(" << std::endl; if(in.ifNext("+")) operation = (char *)"+"; else if(in.ifNext("-")) @@ -27,7 +27,9 @@ namespace jet { Operand op2(in); std::cout << op1.string << ":" << operation << ":" << op2.string << std::endl; } - + + + std::cout << "Leaving expression string: " << string << std::endl; std::cout << "Unparsed: [" << in.unparsed() << "]" << std::endl; } diff --git a/Global.cpp b/Global.cpp index e4f97c9..a1c4112 100644 --- a/Global.cpp +++ b/Global.cpp @@ -1,4 +1,5 @@ #include "Global.h" +#include "Exception.h" #include namespace jet { @@ -16,4 +17,18 @@ namespace jet { std::cout << i->first << "=[" << i->second << "]" << std::endl; } + bool Global::sessionExists(coreutils::MString sessionId) { + return sessions.find(sessionId) != sessions.end(); + } + + void Global::addSession(coreutils::MString sessionId, __mysql *mysql) { + if(sessionExists(sessionId)) + coreutils::Exception("sessionid already exists."); + sessions[sessionId] = mysql; + } + + void Global::removeSession(coreutils::MString sessionId) { + sessions.erase(sessionId); + } + } diff --git a/Global.h b/Global.h index 15b4e20..fb444f3 100644 --- a/Global.h +++ b/Global.h @@ -6,6 +6,8 @@ namespace jet { + class __mysql; + class Global { public: @@ -13,8 +15,13 @@ namespace jet { virtual ~Global(); void dump(); + bool sessionExists(coreutils::MString sessionId); + void addSession(coreutils::MString sessionId, __mysql *mysql); + void removeSession(coreutils::MString sessionId); + void getVariable(ZString variable); std::map variables; + std::map sessions; }; diff --git a/Operand.h b/Operand.h index d4ffb38..2f58c86 100644 --- a/Operand.h +++ b/Operand.h @@ -11,10 +11,7 @@ namespace jet { Operand(coreutils::ZString &in); bool isNumber(); - bool isInteger(); - bool isFloat(); bool isString(); - bool isVariable(); /// /// boolean is set by internal processes to return the boolean diff --git a/Tag.cpp b/Tag.cpp index be5e096..633589f 100644 --- a/Tag.cpp +++ b/Tag.cpp @@ -3,6 +3,7 @@ #include "KeywordValue.h" #include "Log.h" #include "__mysql.h" +#include "__sql.h" #include "__comment.h" #include "__for.h" #include "__if.h" diff --git a/Tag.h b/Tag.h index 29d416a..09c0ea4 100644 --- a/Tag.h +++ b/Tag.h @@ -26,6 +26,9 @@ namespace jet { void processContainer(coreutils::ZString &container); void copyContainer(coreutils::ZString &in, coreutils::MString &out); + Global &global; + coreutils::MString &parent; + coreutils::MString out; bool output = true; @@ -35,8 +38,6 @@ namespace jet { bool cleanWhitespace = false; private: - Global &global; - coreutils::MString &parent; coreutils::ZString splitTagName; int skipBlankLine(coreutils::ZString in); diff --git a/__if.cpp b/__if.cpp index 9453c85..d7b5ab1 100644 --- a/__if.cpp +++ b/__if.cpp @@ -42,9 +42,8 @@ namespace jet { } if(booleanResult) processContainer(container); - else - if(hasContainer2) - processContainer(container2); + else if(hasContainer2) + processContainer(container2); } diff --git a/__mysql.cpp b/__mysql.cpp index ea1b380..23391de 100644 --- a/__mysql.cpp +++ b/__mysql.cpp @@ -1,10 +1,44 @@ #include "__mysql.h" +#include "Exception.h" #include 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."); + if(!variableDefined("database")) + coreutils::Exception("database must be specified for mysql tag."); + if(!variableDefined("user")) + coreutils::Exception("user must be specified for mysql tag."); + if(!variableDefined("password")) + coreutils::Exception("password must be specified for mysql tag."); + + sessionId = variables["sessionid"]; + + global.addSession(sessionId, this); + + 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); + } + __mysql::~__mysql() { + global.removeSession(sessionId); + mysql_free_result(result); + mysql_close(mysql); + } + + void __mysql::query(coreutils::MString query) { + mysql_real_query(mysql, query.getData(), query.getLength()); + result = mysql_store_result(mysql); + if(result) { + row = mysql_fetch_row(result); + fieldLength = mysql_fetch_lengths(result); + qFields = mysql_num_fields(result); + } + } + } diff --git a/__mysql.h b/__mysql.h index a51f15b..3d5e746 100644 --- a/__mysql.h +++ b/__mysql.h @@ -1,10 +1,11 @@ -#ifndef __mysql_h__ -#define __mysql_h__ +#ifndef ____mysql_h__ +#define ____mysql_h__ #include "Tag.h" #include "ZString.h" #include "MString.h" #include +#include namespace jet { @@ -12,7 +13,18 @@ namespace jet { public: __mysql(coreutils::ZString &in, coreutils::MString &parent, Global &global); + ~__mysql(); + void query(coreutils::MString query); + + private: + MYSQL *mysql; + MYSQL_RES *result; + MYSQL_ROW row; + unsigned long *fieldLength; + unsigned int qFields; + coreutils::MString sessionId; + }; } diff --git a/__sql.cpp b/__sql.cpp new file mode 100644 index 0000000..f1c4be6 --- /dev/null +++ b/__sql.cpp @@ -0,0 +1,22 @@ +#include "__sql.h" +#include "Exception.h" +#include "MString.h" +#include "__mysql.h" +#include +#include +#include +#include + +namespace jet { + + __sql::__sql(coreutils::ZString &in, coreutils::MString &out, Global &global) : Tag(in, out, global) { + 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); + + } + +} diff --git a/__sql.h b/__sql.h new file mode 100644 index 0000000..634e2c3 --- /dev/null +++ b/__sql.h @@ -0,0 +1,17 @@ +#ifndef ____sql_h__ +#define ____sql_h__ + +#include "Tag.h" + +namespace jet { + + class __sql : public Tag { + + public: + __sql(coreutils::ZString &in, coreutils::MString &out, Global &global); + + }; + +} + +#endif diff --git a/compile b/compile index 603e3f3..4b6777c 100755 --- a/compile +++ b/compile @@ -18,7 +18,7 @@ done wait echo -n "Building executable testjet..." -g++ -g -o testjet $list -std=c++23 -L../CoreUtils -lCoreUtils +g++ -g -o testjet $list -std=c++23 -L../CoreUtils -lCoreUtils -lmysqlclient if [ $? = '0' ] then echo "OK"