mysql efforts...

This commit is contained in:
Brad Arant 2024-09-18 16:52:08 -07:00
parent 3ba81a7d0a
commit 7453db9bd9
12 changed files with 120 additions and 13 deletions

View File

@ -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;
}

View File

@ -1,4 +1,5 @@
#include "Global.h"
#include "Exception.h"
#include <iostream>
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);
}
}

View File

@ -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<coreutils::MString, coreutils::MString> variables;
std::map<coreutils::MString, __mysql *> sessions;
};

View File

@ -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

View File

@ -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"

5
Tag.h
View File

@ -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);

View File

@ -42,9 +42,8 @@ namespace jet {
}
if(booleanResult)
processContainer(container);
else
if(hasContainer2)
processContainer(container2);
else if(hasContainer2)
processContainer(container2);
}

View File

@ -1,10 +1,44 @@
#include "__mysql.h"
#include "Exception.h"
#include <iostream>
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);
}
}
}

View File

@ -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 <sstream>
#include <mysql/mysql.h>
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;
};
}

22
__sql.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "__sql.h"
#include "Exception.h"
#include "MString.h"
#include "__mysql.h"
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
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);
}
}

17
__sql.h Normal file
View File

@ -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

View File

@ -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"