1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // CegoCPlusTest.cc
4 // --------------
5 // Test and demo program for teh cego C++ API
6 //
7 // Design and Implementation by Bjoern Lemke
8 //
9 // (C)opyright 2010 Bjoern Lemke
10 //
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; see the file COPYING.  If not, write to
23 // the Free Software Foundation, 59 Temple Place - Suite 330,
24 // Boston, MA 02111-1307, USA.
25 //
26 // IMPLEMENTATION MODULE
27 //
28 // Class: main
29 //
30 // Description:
31 //
32 // Status: XXX
33 //
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 #include <locale.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <readline/readline.h>
41 #include <readline/history.h>
42 
43 #include <lfcbase/Chain.h>
44 #include <lfcbase/Exception.h>
45 #include <lfcbase/GetLongOpt.h>
46 #include <lfcbase/File.h>
47 #include <lfcbase/Tokenizer.h>
48 #include <lfcbase/Net.h>
49 
50 #include "CegoNet.h"
51 
52 #define CGPLUSTEST_PRODUCT "CegoCPlusTest"
53 #define CGPLUSTEST_VERSION "1.0.0"
54 #define CGPLUSTEST_COPYRIGHT "Copyright (C) 2010 by Bjoern Lemke. All rights reserved"
55 
56 #define USAGE "Usage: cgplustest --user=<user>/<password>\n\
57           [ --server=<host>]\n\
58           [ --port=<port> ]\n\
59           [ --protocol={serial|xml} ]\n\
60 	  [ --tableset=<tableset> ]\n\
61           [ --logfile=<logfile> ]\n\
62           [ --debug ] [ --version  ] [ --help ]"
63 
64 
65 #define DEFAULTPORT 2200
66 #define DEFAULTSERVER "localhost"
67 #define DEFAULTMYSQLFILE "mysqldump.sql"
68 #define DEFAULTPROTOCOL "xml"
69 
70 // size info buffer len
71 #define SIZEBUFLEN 10
72 #define MSGBUFLEN 500
73 
74 #define LANGUAGE_ENV "LANG"
75 #define LOCALE_CATEGORY LC_TIME
76 
77 extern char __lfcVersionString[];
78 extern char __lfcxmlVersionString[];
79 
main(int argc,char ** argv)80 int main(int argc, char **argv)
81 {
82 
83     GetLongOpt longOpt(argc, argv);
84 
85     longOpt.addOpt("version");
86     longOpt.addOpt("help");
87     longOpt.addOpt("logfile");
88     longOpt.addOpt("tableset");
89     longOpt.addOpt("user");
90     longOpt.addOpt("debug");
91     longOpt.addOpt("server", DEFAULTSERVER);
92     longOpt.addOpt("protocol", DEFAULTPROTOCOL);
93     longOpt.addOpt("port", Chain(DEFAULTPORT));
94 
95     try
96     {
97 	longOpt.parseOpt();
98     }
99     catch ( Exception e )
100     {
101 	Chain msg;
102 	e.pop(msg);
103 	cerr << msg << endl;
104 	cerr << USAGE << endl;
105 	exit(1);
106     }
107 
108     Chain user;
109     Chain password;
110 
111     int lineNo=0;
112 
113     CegoNet *pCegoNet = 0;
114 
115     try
116     {
117 
118 	bool debug=false;
119 	if ( longOpt.isSet("debug") )
120 	    debug=true;
121 
122 	if ( longOpt.isSet( Chain("help") ) )
123 	{
124 	    cerr << USAGE << endl;
125 	    exit(0);
126 	}
127 
128 	if ( longOpt.isSet( Chain("version") ) )
129 	{
130 	    cout << CGPLUSTEST_PRODUCT << " (" << sizeof(long) * 8 << " bit), Version " << CGPLUSTEST_VERSION
131 		 << " [ lfc : " << __lfcVersionString
132 		 << ", lfcxml : " <<  __lfcxmlVersionString << " ]" << endl;
133 	    cout << CGPLUSTEST_COPYRIGHT << endl;
134 	    exit(0);
135 	}
136 
137 	Chain logFile = longOpt.getOptValue("logfile");
138 	Chain serverName = longOpt.getOptValue("server");
139 	Chain tableSet = longOpt.getOptValue("tableset");
140 	int portNo = longOpt.getOptValue("port").asInteger();
141 
142 	Chain authString = longOpt.getOptValue("user");
143 
144 	Tokenizer authTok(authString, Chain("/"));
145 	authTok.nextToken(user);
146 	authTok.nextToken(password);
147 
148 	if ( user.length() == 0 )
149 	{
150 	    cerr << "User not set" << endl;
151 	    exit (1);
152 	}
153 	if ( password.length() == 0 )
154 	{
155 	    cerr << "Password not set" << endl;
156 	    exit (1);
157 	}
158 	if ( tableSet.length() == 0 )
159 	{
160 	    cerr << "Tableset not set" << endl;
161 	    exit (1);
162 	}
163 
164 	CegoDbHandler::ProtocolType protType;
165 	Chain prot = longOpt.getOptValue("protocol");
166 
167 	if ( prot == Chain("serial") )
168 	{
169 	    protType = CegoDbHandler::SERIAL;
170 	}
171 	else if ( prot == Chain("xml") )
172 	{
173 	    protType = CegoDbHandler::XML;
174 	}
175 	else
176 	{
177 	    cerr << "Invalid protocol " << prot;
178 	    exit (1);
179 	}
180 
181 
182 	// for localization
183 	char* lang = 0;
184 	if ( ( lang = getenv(LANGUAGE_ENV) ) != 0)
185 	{
186 	    if ( setlocale(LOCALE_CATEGORY, lang) == 0)
187 	    {
188 		Chain msg = Chain("Cannot set locale ") + Chain(lang);
189 		cerr << msg << endl;
190 		exit(1);
191 	    }
192 	}
193 
194 	Chain logMode("notice");
195 	if ( debug )
196 	    logMode = Chain("debug");
197 
198 	pCegoNet = new CegoNet( protType, logFile, logMode );
199 	pCegoNet->connect(serverName, portNo, tableSet, user, password);
200 
201 	// sample query 1 : create table
202 
203 	Chain stmt = Chain("create table t1 ( a int,  b string(10));");
204 	pCegoNet->doQuery(stmt);
205 	cout << "Result :" << pCegoNet->getMsg() << endl;
206 
207 	// sample query 2 : create procedure
208 
209 	stmt = Chain("create procedure p1 ( a in int) return int \
210 begin\
211   var b int;\
212   insert into t1 values ( :a, 'XXX');\
213   :a = :a +1;\
214   return :a;\
215 end;");
216 	cout << "ResultCode=" << pCegoNet->doQuery(stmt) << endl;
217 	cout << "ResultMsg=" << pCegoNet->getMsg() << endl;
218 
219 	stmt = Chain("call p1(11);");
220 	cout << "ResultCode=" << pCegoNet->doQuery(stmt) << endl;
221 	cout << "ResultMsg=" << pCegoNet->getMsg() << endl;
222 
223 	stmt = Chain("call p1(12);");
224 	cout << "ResultCode=" << pCegoNet->doQuery(stmt) << endl;
225 	cout << "ResultMsg=" << pCegoNet->getMsg() << endl;
226 
227 	stmt = Chain("select * from t1;");
228 	cout << "ResultCode=" << pCegoNet->doQuery(stmt) << endl;
229 	cout << "ResultMsg=" << pCegoNet->getMsg() << endl;
230 
231 	ListT<CegoField> schema;
232 	pCegoNet->getSchema(schema);
233 
234 	ListT<CegoFieldValue> fvl;
235 	while ( pCegoNet->fetchData(schema, fvl)  )
236 	{
237 	    CegoFieldValue *pFV = fvl.First();
238 	    while ( pFV )
239 	    {
240 		cout << *pFV << "\t";
241 		pFV = fvl.Next();
242 	    }
243 	    cout << endl;
244 
245 	    fvl.Empty();
246 	}
247 
248 	pCegoNet->disconnect();
249 	delete pCegoNet;
250 
251     }
252     catch ( Exception e )
253     {
254 	Chain msg;
255 	e.pop(msg);
256 	cout << "Exception occured : " << msg << endl;
257 
258 	if ( pCegoNet )
259 	{
260 	    pCegoNet->disconnect();
261 	    delete pCegoNet;
262 	}
263 
264     }
265 
266     exit(0);
267 }
268 
269 
270