Skip to content

Commit 0c85eb4

Browse files
Merge pull request #170 from chrismaeda/master
MySQL and Postgres user file system modules that support custom db schemas
2 parents 6d601ae + 8d173aa commit 0c85eb4

File tree

11 files changed

+731
-57
lines changed

11 files changed

+731
-57
lines changed

SRC/Makefile

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,103 @@
1-
.PHONY = all 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+
# 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
310

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
15+
executable.server=../BINARIES/ChatScript
416
server: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1
517
server: PGLOAD= -pthread
618
server: INCLUDEDIRS=-Ievserver
7-
server: all
8-
server: EXECUTABLE=../BINARIES/ChatScript
19+
server: binary
20+
server: EXECUTABLE=$(executable.server)
921
server: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing
1022

23+
executable.debugserver=../BINARIES/ChatScriptDebug
1124
debugserver: DEFINES+= -DLOCKUSERFILE=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1 -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1
1225
debugserver: PGLOAD= -pthread
1326
debugserver: INCLUDEDIRS=-Ievserver
14-
debugserver: all
15-
debugserver: EXECUTABLE=../BINARIES/ChatScriptDebug
27+
debugserver: binary
28+
debugserver: EXECUTABLE=$(executable.debugserver)
1629
debugserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g
1730

31+
executable.standalone=../BINARIES/ChatScript
1832
standalone: DEFINES+= -DDISCARDSERVER=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1 -DEVSERVER=1 -DEVSERVER_FORK=1
1933
standalone: PGLOAD= -pthread
2034
standalone: INCLUDEDIRS=-Ievserver
21-
standalone: all
22-
standalone: EXECUTABLE=../BINARIES/ChatScript
35+
standalone: binary
36+
standalone: EXECUTABLE=$(executable.standalone)
2337
standalone: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing
2438

39+
executable.pgserver=../BINARIES/ChatScriptpg
2540
pgserver: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1
2641
pgserver: PGLOAD= -lpq -pthread
27-
pgserver: all
28-
pgserver: EXECUTABLE=../BINARIES/ChatScriptpg
42+
pgserver: binary
43+
pgserver: EXECUTABLE=$(executable.pgserver)
2944
pgserver: INCLUDEDIRS=-Ievserver -Ipostgres
3045
pgserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing
3146

32-
debugpgserver: DEFINES+= -DLOCKUSERFILE=1 -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDMONGO=1
47+
executable.debugpgserver=../BINARIES/ChatScriptpgDebug
48+
debugpgserver: DEFINES+= -DLOCKUSERFILE=1 -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1
3349
debugpgserver: PGLOAD= -lpq -pthread
34-
debugpgserver: INCLUDEDIRS=-Ievserver
35-
debugpgserver: all
36-
debugpgserver: EXECUTABLE=../BINARIES/ChatScriptpgDebug
50+
debugpgserver: INCLUDEDIRS=-Ievserver -Ipostgres
51+
debugpgserver: binary
52+
debugpgserver: EXECUTABLE=$(executable.debugpgserver)
3753
debugpgserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g
3854

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+
71+
executable.mongoserver=../BINARIES/ChatScriptMongo
3972
mongoserver: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMYSQL=1
4073
mongoserver: PGLOAD= -pthread
41-
mongoserver: all
42-
mongoserver: EXECUTABLE=../BINARIES/ChatScriptMongo
74+
mongoserver: binary
75+
mongoserver: EXECUTABLE=$(executable.mongoserver)
4376
mongoserver: INCLUDEDIRS=-Iinclude -I/usr/local/include/libbson-1.0 -I/usr/local/include/libmongoc-1.0
4477
mongoserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g
4578

79+
executable.debugmongoserver=../BINARIES/ChatScriptMongoDebug
4680
debugmongoserver: DEFINES+= -DLOCKUSERFILE=1 -DDEBUG -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMYSQL=1
4781
debugmongoserver: PGLOAD= -lpq -pthread
48-
debugmongoserver: all
82+
debugmongoserver: binary
4983
debugmongoserver: INCLUDEDIRS=-Iinclude -I/usr/local/include/libbson-1.0 -I/usr/local/include/libmongoc-1.0
50-
debugmongoserver: EXECUTABLE=../BINARIES/ChatScriptMongoDebug
84+
debugmongoserver: EXECUTABLE=$(executable.debugmongoserver)
5185
debugmongoserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing -g
5286

87+
executable.foreignserver=../BINARIES/ChatScriptForeign
5388
foreignserver: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMONGO=1 -DDISCARDMYSQL=1 -DTREETAGGER=1
5489
foreignserver: PGLOAD= -pthread -L../treetagger -L../BINARIES -ltreetagger
5590
foreignserver: INCLUDEDIRS=-Ievserver
56-
foreignserver: all
57-
foreignserver: EXECUTABLE=../BINARIES/ChatScriptForeign
91+
foreignserver: binary
92+
foreignserver: EXECUTABLE=$(executable.foreignserver)
5893
foreignserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing
5994

95+
executable.mongotreetaggerserver=../BINARIES/ChatScriptMongoForeign
6096
mongotreetaggerserver: DEFINES+= -DLOCKUSERFILE=1 -DEVSERVER=1 -DEVSERVER_FORK=1 -DDISCARDPOSTGRES=1 -DDISCARDMYSQL=1 -DTREETAGGER=1
6197
mongotreetaggerserver: PGLOAD= -pthread -L../treetagger -L../BINARIES -ltreetagger
6298
mongotreetaggerserver: INCLUDEDIRS=-Iinclude -I/usr/local/include/libbson-1.0 -I/usr/local/include/libmongoc-1.0
63-
mongotreetaggerserver: all
64-
mongotreetaggerserver: EXECUTABLE=../BINARIES/ChatScriptMongoForeign
99+
mongotreetaggerserver: binary
100+
mongotreetaggerserver: EXECUTABLE=$(executable.mongotreetaggerserver)
65101
mongotreetaggerserver: CFLAGS=-c -std=c++11 -Wall -funsigned-char -Wno-write-strings -Wno-char-subscripts -Wno-strict-aliasing
66102

67103
UNAME := $(shell uname)
@@ -93,16 +129,27 @@ debugmongoserver: LDFLAGS+= -lgcrypt -lsasl2 -lmongoc-1.0 -lbson-1.0
93129
mongotreetaggerserver: LDFLAGS+= -lgcrypt -lsasl2 -lmongoc-1.0 -lbson-1.0
94130
endif
95131

96-
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
97133
OBJECTS=$(SOURCES:.cpp=.o)
98-
99-
all: $(SOURCES) binary
134+
135+
default: server
100136

101137
clean:
102-
-rm -f $(EXECUTABLE)
103-
-rm -f ../$(EXECUTABLE)
104138
-rm -f *.o
105139

140+
cleanall: clean
141+
-rm -f $(executable.server)
142+
-rm -f $(executable.debugserver)
143+
-rm -f $(executable.standalone)
144+
-rm -f $(executable.pgserver)
145+
-rm -f $(executable.debugpgserver)
146+
-rm -f $(executable.mysqlserver)
147+
-rm -f $(executable.debugmysqlserver)
148+
-rm -f $(executable.mongoserver)
149+
-rm -f $(executable.debugmongoserver)
150+
-rm -f $(executable.foreignserver)
151+
-rm -f $(executable.mongotreetaggerserver)
152+
106153
binary: $(OBJECTS)
107154
$(CC) $(OBJECTS) $(LDFLAGS) $(DEFINES) $(INCLUDEDIRS) -o $(EXECUTABLE)
108155

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)