]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/ing_test.c
076b559c88b5da3c9c053a781d3ab9a8ee82eb43
[bacula/bacula] / bacula / src / tools / ing_test.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2009-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *
30  *  Program to test Ingres DB routines
31  *
32  *   Stefan Reddig, February 2010
33  *
34  *   reusing code by
35  *   Eric Bollengier, August 2009
36  *
37  *
38  */
39 #include "bacula.h"
40 #include "cats/cats.h"
41 #include "cats/sql_glue.h"
42  
43 /* Local variables */
44 static B_DB *db;
45 static const char *file = "COPYRIGHT";
46 //static DBId_t fnid=0;
47 static const char *db_name = "bacula";
48 static const char *db_user = "bacula";
49 static const char *db_password = "";
50 static const char *db_host = NULL;
51
52 static void usage()
53 {
54    fprintf(stderr, _(
55 PROG_COPYRIGHT
56 "\nVersion: %s (%s)\n"
57 "       -d <nn>           set debug level to <nn>\n"
58 "       -dt               print timestamp in debug output\n"
59 "       -n <name>         specify the database name (default bacula)\n"
60 "       -u <user>         specify database user name (default bacula)\n"
61 "       -P <password      specify database password (default none)\n"
62 "       -h <host>         specify database host (default NULL)\n"
63 "       -w <working>      specify working directory\n"
64 "       -j <jobids>       specify jobids\n"
65 "       -p <path>         specify path\n"
66 "       -f <file>         specify file\n"
67 "       -l <limit>        maximum tuple to fetch\n"
68 "       -T                truncate cache table before starting\n"
69 "       -v                verbose\n"
70 "       -?                print this message\n\n"), 2001, VERSION, BDATE);
71    exit(1);
72 }
73
74 /*
75  * simple handler for debug output of CRUD example
76  */
77 static int test_handler(void *ctx, int num_fields, char **row)
78 {
79    Pmsg2(0, "   Values are %d, %s\n", str_to_int64(row[0]), row[1]);
80    return 0;
81 }
82
83 /*
84  * string handler for debug output of simple SELECT tests
85  */
86 static int string_handler(void *ctx, int num_fields, char **row)
87 {
88    Pmsg1(0, "   Value is >>%s<<\n", row[0]);
89    return 0;
90 }
91
92
93 /* number of thread started */
94
95 int main (int argc, char *argv[])
96 {
97    int ch;
98    char *jobids = (char *)"1";
99    char *path=NULL, *client=NULL;
100    uint64_t limit=0;
101    bool clean=false;
102    setlocale(LC_ALL, "");
103    bindtextdomain("bacula", LOCALEDIR);
104    textdomain("bacula");
105    init_stack_dump();
106
107    Dmsg0(0, "Starting ing_test tool\n");
108    
109    my_name_is(argc, argv, "ing_test");
110    init_msg(NULL, NULL);
111
112    OSDependentInit();
113
114    while ((ch = getopt(argc, argv, "h:c:l:d:n:P:Su:vf:w:?j:p:f:T")) != -1) {
115       switch (ch) {
116       case 'd':                    /* debug level */
117          if (*optarg == 't') {
118             dbg_timestamp = true;
119          } else {
120             debug_level = atoi(optarg);
121             if (debug_level <= 0) {
122                debug_level = 1;
123             }
124          }
125          break;
126       case 'l':
127          limit = str_to_int64(optarg);
128          break;
129
130       case 'c':
131          client = optarg;
132          break;
133
134       case 'h':
135          db_host = optarg;
136          break;
137
138       case 'n':
139          db_name = optarg;
140          break;
141
142       case 'w':
143          working_directory = optarg;
144          break;
145
146       case 'u':
147          db_user = optarg;
148          break;
149
150       case 'P':
151          db_password = optarg;
152          break;
153
154       case 'v':
155          verbose++;
156          break;
157
158       case 'p':
159          path = optarg;
160          break;
161
162       case 'f':
163          file = optarg;
164          break;
165
166       case 'j':
167          jobids = optarg;
168          break;
169
170       case 'T':
171          clean = true;
172          break;
173
174       case '?':
175       default:
176          usage();
177
178       }
179    }
180    argc -= optind;
181    argv += optind;
182
183    if (argc != 0) {
184       Pmsg0(0, _("Wrong number of arguments: \n"));
185       usage();
186    }
187    
188    if ((db = db_init_database(NULL, NULL, db_name, db_user, db_password,
189                               db_host, 0, NULL, false, false)) == NULL) {
190       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
191    }
192    Dmsg1(0, "db_type=%s\n", db_get_type(db));
193
194    if (!db_open_database(NULL, db)) {
195       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
196    }
197    Dmsg0(200, "Database opened\n");
198    if (verbose) {
199       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
200    }
201
202    /*
203     * simple CRUD test including create/drop table
204     */
205    Pmsg0(0, "\nsimple CRUD test...\n\n");
206    const char *stmt1[8] = {
207       "CREATE TABLE t1 ( c1 integer, c2 varchar(29))",
208       "INSERT INTO t1 VALUES (1, 'foo')",
209       "SELECT c1,c2 FROM t1",
210       "UPDATE t1 SET c2='bar' WHERE c1=1",
211       "SELECT * FROM t1",
212       "DELETE FROM t1 WHERE c2 LIKE '\%r'",
213       "SELECT * FROM t1",
214       "DROP TABLE t1"
215    };
216    int (*hndl1[8])(void*,int,char**) = {
217       NULL,
218       NULL,
219       test_handler,
220       NULL,
221       test_handler,
222       NULL,
223       test_handler,
224       NULL
225    };
226
227    for (int i=0; i<8; ++i) {
228       Pmsg1(0, "DB-Statement: %s\n",stmt1[i]);
229       if (!db_sql_query(db, stmt1[i], hndl1[i], NULL)) {
230          Emsg0(M_ERROR_TERM, 0, _("Stmt went wrong\n"));
231       }
232    }
233
234
235    /*
236     * simple SELECT tests without tables
237     */
238    Pmsg0(0, "\nsimple SELECT tests without tables...\n\n");
239    const char *stmt2[8] = {
240       "SELECT 'Test of simple SELECT!'",
241       "SELECT 'Test of simple SELECT!' as Text",
242       "SELECT VARCHAR(LENGTH('Test of simple SELECT!'))",
243       "SELECT DBMSINFO('_version')",
244       "SELECT 'This is a ''quoting'' test with single quotes'",
245       "SELECT 'This is a \"quoting\" test with double quotes'",
246       "SELECT null",
247       "SELECT ''"
248    };
249    int (*hndl2[8])(void*,int,char**) = {
250       string_handler,
251       string_handler,
252       string_handler,
253       string_handler,
254       string_handler,
255       string_handler,
256       string_handler,
257       string_handler
258    };
259
260    for (int i=0; i<8; ++i) {
261       Pmsg1(0, "DB-Statement: %s\n",stmt2[i]);
262       if (!db_sql_query(db, stmt2[i], hndl2[i], NULL)) {
263          Emsg0(M_ERROR_TERM, 0, _("Stmt went wrong\n"));
264       }
265    }
266
267    /*
268     * testing aggregates like avg, max, sum
269     */
270    Pmsg0(0, "\ntesting aggregates...\n\n");
271    const char *stmt[11] = {
272       "CREATE TABLE t1 (c1 integer, c2 varchar(29))",
273       "INSERT INTO t1 VALUES (1,'foo')",
274       "INSERT INTO t1 VALUES (2,'bar')",
275       "INSERT INTO t1 VALUES (3,'fun')",
276       "INSERT INTO t1 VALUES (4,'egg')",
277       "SELECT max(c1) from t1",
278       "SELECT sum(c1) from t1",
279       "INSERT INTO t1 VALUES (5,NULL)",
280       "SELECT count(*) from t1",
281       "SELECT count(c2) from t1",
282       "DROP TABLE t1"
283    };
284    int (*hndl[11])(void*,int,char**) = {
285       NULL,
286       NULL,
287       NULL,
288       NULL,
289       NULL,
290       string_handler,
291       string_handler,
292       NULL,
293       string_handler,
294       string_handler,
295       NULL
296    };
297
298    for (int i=0; i<11; ++i) {
299       Pmsg1(0, "DB-Statement: %s\n",stmt[i]);
300       if (!db_sql_query(db, stmt[i], hndl[i], NULL)) {
301          Emsg0(M_ERROR_TERM, 0, _("Stmt went wrong\n"));
302       }
303    }
304
305
306    /* 
307     * datatypes test
308     */
309    Pmsg0(0, "\ndatatypes test... (TODO)\n\n");
310
311
312    Dmsg0(200, "DB-Statement: CREATE TABLE for datatypes\n");
313    if (!db_sql_query(db, "CREATE TABLE t2 ("
314      "c1        integer,"
315      "c2        varchar(255),"
316      "c3        char(255)"
317      /* some more datatypes... "c4      ," */
318      ")" , NULL, NULL)) {
319       Emsg0(M_ERROR_TERM, 0, _("CREATE-Stmt went wrong\n"));
320    }
321
322    Dmsg0(200, "DB-Statement: DROP TABLE for datatypes\n");
323    if (!db_sql_query(db, "DROP TABLE t2", NULL, NULL)) {
324       Emsg0(M_ERROR_TERM, 0, _("DROP-Stmt went wrong\n"));
325    }
326
327
328    db_close_database(NULL, db);
329    Dmsg0(200, "Database closed\n");
330
331    return 0;
332 }