Skip to content

Commit 3ad381b

Browse files
committed
1. Change postgres db code to use parameter queries instead of hardcoded, dynamic sql
2. Allow postgres sql to be overridden for user read, user insert, and user update 3. Allow dbname to be specified at config time instead of hardcoded to 'users' 4. Implement MySQL user file store using prepared statements 5. Allow MySQL sql to be overridden for user read, user insert, and user update 6. Add config params for mysql host, post, db, user, password 7. Add make targets for mysql; fix clean targets
1 parent 4e6e32c commit 3ad381b

File tree

9 files changed

+614
-39
lines changed

9 files changed

+614
-39
lines changed

SRC/Makefile

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
.PHONY = clean binary server debugserver standalone pgserver debugpgserver mongoserver debugmongoserver foreignserver mongotreetaggerserver
1+
.PHONY = clean binary server debugserver standalone pgserver debugpgserver mysqlserver debugmysqlserver mongoserver debugmongoserver foreignserver mongotreetaggerserver
22

3-
# store executable names in make variables so that clean target works
3+
# Removed the 'all' target. It does not make sense since each build target requires a different set of defines.
4+
# So we need to clean before building each target in order compile in the right stuff.
5+
#
6+
# E.g. to build pg and mysql:
7+
#
8+
# 1. make clean pgserver
9+
# 2. make clean mysqlserver
410

11+
# Note: to build mysql targets on Ubuntu:
12+
# sudo apt-get install libmysqlclient-dev
13+
14+
# store executable names in make variables so that cleanall target works
515
executable.server=../BINARIES/ChatScript
616
server: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1
717
server: PGLOAD= -pthread
@@ -35,13 +45,29 @@ pgserver: INCLUDEDIRS=-Ievserver -Ipostgres
3545
pgserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing
3646

3747
executable.debugpgserver=../BINARIES/ChatScriptpgDebug
38-
debugpgserver: DEFINES+= -DLOCKUSERFILE=1 -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDMONGO=1
48+
debugpgserver: DEFINES+= -DLOCKUSERFILE=1 -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1
3949
debugpgserver: PGLOAD= -lpq -pthread
40-
debugpgserver: INCLUDEDIRS=-Ievserver
50+
debugpgserver: INCLUDEDIRS=-Ievserver -Ipostgres
4151
debugpgserver: binary
4252
debugpgserver: EXECUTABLE=$(executable.debugpgserver)
4353
debugpgserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g
4454

55+
executable.mysqlserver=../BINARIES/ChatScriptMysql
56+
mysqlserver: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1
57+
mysqlserver: PGLOAD= -lmysqlclient -pthread
58+
mysqlserver: binary
59+
mysqlserver: EXECUTABLE=$(executable.mysqlserver)
60+
mysqlserver: INCLUDEDIRS=-Ievserver
61+
mysqlserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing
62+
63+
executable.debugmysqlserver=../BINARIES/ChatScriptMysqlDebug
64+
debugmysqlserver: DEFINES+= -DLOCKUSERFILE=1 -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1
65+
debugmysqlserver: PGLOAD= -lmysqlclient -pthread
66+
debugmysqlserver: INCLUDEDIRS=-Ievserver
67+
debugmysqlserver: binary
68+
debugmysqlserver: EXECUTABLE=$(executable.debugmysqlserver)
69+
debugmysqlserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g
70+
4571
executable.mongoserver=../BINARIES/ChatScriptMongo
4672
mongoserver: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMYSQL=1
4773
mongoserver: PGLOAD= -pthread
@@ -103,21 +129,22 @@ debugmongoserver: LDFLAGS+= -lgcrypt -lsasl2 -lmongoc-1.0 -lbson-1.0
103129
mongotreetaggerserver: LDFLAGS+= -lgcrypt -lsasl2 -lmongoc-1.0 -lbson-1.0
104130
endif
105131

106-
SOURCES=constructCode.cpp duktape/duktape.c evserver.cpp csocket.cpp cs_ev.c dictionarySystem.cpp englishTagger.cpp factSystem.cpp json.cpp functionExecute.cpp english.cpp infer.cpp javascript.cpp jsmn.cpp markSystem.cpp mongodb.cpp os.cpp outputSystem.cpp patternSystem.cpp postgres.cpp privatesrc.cpp scriptCompile.cpp spellcheck.cpp secure.cpp systemVariables.cpp tagger.cpp testing.cpp textUtilities.cpp tokenSystem.cpp topicSystem.cpp userCache.cpp userSystem.cpp variableSystem.cpp mainSystem.cpp
132+
SOURCES=constructCode.cpp duktape/duktape.c evserver.cpp csocket.cpp cs_ev.c dictionarySystem.cpp englishTagger.cpp factSystem.cpp json.cpp functionExecute.cpp english.cpp infer.cpp javascript.cpp jsmn.cpp markSystem.cpp mysql.cpp mongodb.cpp os.cpp outputSystem.cpp patternSystem.cpp postgres.cpp privatesrc.cpp scriptCompile.cpp spellcheck.cpp secure.cpp systemVariables.cpp tagger.cpp testing.cpp textUtilities.cpp tokenSystem.cpp topicSystem.cpp userCache.cpp userSystem.cpp variableSystem.cpp mainSystem.cpp
107133
OBJECTS=$(SOURCES:.cpp=.o)
108134

109-
# all target does make sense since each build target requires a different set of defines
110-
# so we need to clean before building each target in order compile the right stuff
111-
112135
default: server
113136

114137
clean:
115138
-rm -f *.o
139+
140+
cleanall: clean
116141
-rm -f $(executable.server)
117142
-rm -f $(executable.debugserver)
118143
-rm -f $(executable.standalone)
119144
-rm -f $(executable.pgserver)
120145
-rm -f $(executable.debugpgserver)
146+
-rm -f $(executable.mysqlserver)
147+
-rm -f $(executable.debugmysqlserver)
121148
-rm -f $(executable.mongoserver)
122149
-rm -f $(executable.debugmongoserver)
123150
-rm -f $(executable.foreignserver)

SRC/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2525
//#define DISCARDMONGO 1
2626
//#define DISCARDJSONOPEN 1
2727
//#define DISCARDJAVASCRIPT 1
28-
#define DISCARDMYSQL 1
28+
//#define DISCARDMYSQL 1
2929

3030
// these can add components
3131
//#define TREETAGGER 1
@@ -220,4 +220,4 @@ using namespace std;
220220
#define GetCurrentDir getcwd
221221
#endif
222222

223-
#endif
223+
#endif

SRC/evserver.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ ev_io ev_accept_r_g;
7272
ev_timer tt_g;
7373

7474
bool postgresInited = false;
75+
bool mysqlInited = false;
7576

7677
#ifdef EVSERVER_FORK
7778
// child monitors
@@ -622,6 +623,14 @@ int evsrv_do_chat(Client_t *client)
622623
postgresInited = true;
623624
}
624625
#endif
626+
#ifndef DISCARDMYSQL
627+
if (mysqlconf && !mysqlInited)
628+
{
629+
MySQLUserFilesCode(); //Forked must hook uniquely AFTER forking
630+
mysqlInited = true;
631+
}
632+
#endif
633+
625634
if (!client->data) client->data = (char*) malloc(outputsize);
626635
if (!client->data) printf("Malloc failed for child data\r\n");
627636

SRC/mainSystem.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,66 @@ static void ProcessArgument(char* arg)
624624
else if (!strnicmp(arg,(char*)"english=",8) ) strcpy(languageFolder,arg+8);
625625
#ifndef DISCARDPOSTGRES
626626
else if (!strnicmp(arg,(char*)"pguser=",7) ) strcpy(postgresparams, arg+7);
627+
// Postgres Override SQL
628+
else if (!strnicmp(arg,(char*)"pguserread=",11) )
629+
{
630+
strcpy(postgresuserread, arg+11);
631+
pguserread = postgresuserread;
632+
}
633+
else if (!strnicmp(arg,(char*)"pguserinsert=",13) )
634+
{
635+
strcpy(postgresuserinsert, arg+13);
636+
pguserinsert = postgresuserinsert;
637+
}
638+
else if (!strnicmp(arg,(char*)"pguserupdate=",13) )
639+
{
640+
strcpy(postgresuserupdate, arg+13);
641+
pguserupdate = postgresuserupdate;
642+
}
643+
#endif
644+
#ifndef DISCARDMYSQL
645+
else if (!strnicmp(arg,(char*)"mysqlhost=",10) )
646+
{
647+
mysqlconf = true;
648+
strcpy(mysqlhost, arg+10);
649+
}
650+
else if (!strnicmp(arg,(char*)"mysqlport=",10) )
651+
{
652+
mysqlconf = true;
653+
unsigned int port = atoi(arg+10);
654+
mysqlport = port;
655+
}
656+
else if (!strnicmp(arg,(char*)"mysqldb=",8) )
657+
{
658+
mysqlconf = true;
659+
strcpy(mysqldb, arg+8);
660+
}
661+
else if (!strnicmp(arg,(char*)"mysqluser=",10) )
662+
{
663+
mysqlconf = true;
664+
strcpy(mysqluser, arg+10);
665+
}
666+
else if (!strnicmp(arg,(char*)"mysqlpasswd=",12) )
667+
{
668+
mysqlconf = true;
669+
strcpy(mysqlpasswd, arg+12);
670+
}
671+
// MySQL Query Override SQL
672+
else if (!strnicmp(arg,(char*)"mysqluserread=",14) )
673+
{
674+
strcpy(my_userread_sql, arg+14);
675+
mysql_userread = my_userread_sql;
676+
}
677+
else if (!strnicmp(arg,(char*)"mysqluserinsert=",16) )
678+
{
679+
strcpy(my_userinsert_sql, arg+16);
680+
mysql_userinsert = my_userinsert_sql;
681+
}
682+
else if (!strnicmp(arg,(char*)"mysqluserupdate=",16) )
683+
{
684+
strcpy(my_userupdate_sql, arg+16);
685+
mysql_userupdate = my_userupdate_sql;
686+
}
627687
#endif
628688
#ifndef DISCARDMONGO
629689
else if (!strnicmp(arg,(char*)"mongo=",6) ) strcpy(mongodbparams,arg+6);

SRC/my_sql.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,29 @@ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1515
#endif
1616

1717
#ifndef DISCARDMYSQL
18-
void MySQLShutDown();
18+
19+
extern bool mysqlconf;
20+
extern char mysqlhost[300];
21+
extern unsigned int mysqlport;
22+
extern char mysqldb[300];
23+
extern char mysqluser[300];
24+
extern char mysqlpasswd[300];
25+
26+
extern char *mysql_userread;
27+
extern char *mysql_userinsert;
28+
extern char *mysql_userupdate;
29+
extern char my_userread_sql[300];
30+
extern char my_userinsert_sql[300];
31+
extern char my_userupdate_sql[300];
32+
1933
void MySQLUserFilesCode();
34+
void MySQLShutDown();
2035
void MySQLserFilesCloseCode();
2136
extern char* mySQLparams;
37+
2238
FunctionResult MySQLInitCode(char* buffer);
2339
FunctionResult MySQLCloseCode(char* buffer);
2440
FunctionResult MySQLExecuteCode(char* buffer);
2541
#endif
2642

27-
#endif
43+
#endif

0 commit comments

Comments
 (0)