]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/ing_test.c
Make cd accept wildcards
[bacula/bacula] / bacula / src / tools / ing_test.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2009-2011 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 #ifdef needed
40 #include "bacula.h"
41 #include "cats/cats.h"
42 #include "cats/sql_glue.h"
43  
44 /* Local variables */
45 static B_DB *db;
46 static const char *file = "COPYRIGHT";
47 //static DBId_t fnid=0;
48 static const char *db_name = "bacula";
49 static const char *db_user = "bacula";
50 static const char *db_password = "";
51 static const char *db_host = NULL;
52
53 static void usage()
54 {
55    fprintf(stderr, _(
56 PROG_COPYRIGHT
57 "\nVersion: %s (%s)\n"
58 "       -d <nn>           set debug level to <nn>\n"
59 "       -dt               print timestamp in debug output\n"
60 "       -n <name>         specify the database name (default bacula)\n"
61 "       -u <user>         specify database user name (default bacula)\n"
62 "       -P <password      specify database password (default none)\n"
63 "       -h <host>         specify database host (default NULL)\n"
64 "       -w <working>      specify working directory\n"
65 "       -j <jobids>       specify jobids\n"
66 "       -p <path>         specify path\n"
67 "       -f <file>         specify file\n"
68 "       -l <limit>        maximum tuple to fetch\n"
69 "       -T                truncate cache table before starting\n"
70 "       -v                verbose\n"
71 "       -?                print this message\n\n"), 2001, VERSION, BDATE);
72    exit(1);
73 }
74
75 /*
76  * simple handler for debug output of CRUD example
77  */
78 static int test_handler(void *ctx, int num_fields, char **row)
79 {
80    Pmsg2(0, "   Values are %d, %s\n", str_to_int64(row[0]), row[1]);
81    return 0;
82 }
83
84 /*
85  * string handler for debug output of simple SELECT tests
86  */
87 static int string_handler(void *ctx, int num_fields, char **row)
88 {
89    Pmsg1(0, "   Value is >>%s<<\n", row[0]);
90    return 0;
91 }
92
93
94 /* number of thread started */
95
96 int main (int argc, char *argv[])
97 {
98    int ch;
99    char *jobids = (char *)"1";
100    char *path=NULL, *client=NULL;
101    uint64_t limit=0;
102    bool clean=false;
103    setlocale(LC_ALL, "");
104    bindtextdomain("bacula", LOCALEDIR);
105    textdomain("bacula");
106    init_stack_dump();
107
108    Dmsg0(0, "Starting ing_test tool\n");
109    
110    my_name_is(argc, argv, "ing_test");
111    init_msg(NULL, NULL);
112
113    OSDependentInit();
114
115    while ((ch = getopt(argc, argv, "h:c:l:d:n:P:Su:vf:w:?j:p:f:T")) != -1) {
116       switch (ch) {
117       case 'd':                    /* debug level */
118          if (*optarg == 't') {
119             dbg_timestamp = true;
120          } else {
121             debug_level = atoi(optarg);
122             if (debug_level <= 0) {
123                debug_level = 1;
124             }
125          }
126          break;
127       case 'l':
128          limit = str_to_int64(optarg);
129          break;
130
131       case 'c':
132          client = optarg;
133          break;
134
135       case 'h':
136          db_host = optarg;
137          break;
138
139       case 'n':
140          db_name = optarg;
141          break;
142
143       case 'w':
144          working_directory = optarg;
145          break;
146
147       case 'u':
148          db_user = optarg;
149          break;
150
151       case 'P':
152          db_password = optarg;
153          break;
154
155       case 'v':
156          verbose++;
157          break;
158
159       case 'p':
160          path = optarg;
161          break;
162
163       case 'f':
164          file = optarg;
165          break;
166
167       case 'j':
168          jobids = optarg;
169          break;
170
171       case 'T':
172          clean = true;
173          break;
174
175       case '?':
176       default:
177          usage();
178
179       }
180    }
181    argc -= optind;
182    argv += optind;
183
184    if (argc != 0) {
185       Pmsg0(0, _("Wrong number of arguments: \n"));
186       usage();
187    }
188    
189    if ((db = db_init_database(NULL, NULL, db_name, db_user, db_password,
190                               db_host, 0, NULL, false, false)) == NULL) {
191       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
192    }
193    Dmsg1(0, "db_type=%s\n", db_get_type(db));
194
195    if (!db_open_database(NULL, db)) {
196       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
197    }
198    Dmsg0(200, "Database opened\n");
199    if (verbose) {
200       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
201    }
202
203    /*
204     * simple CRUD test including create/drop table
205     */
206    Pmsg0(0, "\nsimple CRUD test...\n\n");
207    const char *stmt1[8] = {
208       "CREATE TABLE t1 ( c1 integer, c2 varchar(29))",
209       "INSERT INTO t1 VALUES (1, 'foo')",
210       "SELECT c1,c2 FROM t1",
211       "UPDATE t1 SET c2='bar' WHERE c1=1",
212       "SELECT * FROM t1",
213       "DELETE FROM t1 WHERE c2 LIKE '\%r'",
214       "SELECT * FROM t1",
215       "DROP TABLE t1"
216    };
217    int (*hndl1[8])(void*,int,char**) = {
218       NULL,
219       NULL,
220       test_handler,
221       NULL,
222       test_handler,
223       NULL,
224       test_handler,
225       NULL
226    };
227
228    for (int i=0; i<8; ++i) {
229       Pmsg1(0, "DB-Statement: %s\n",stmt1[i]);
230       if (!db_sql_query(db, stmt1[i], hndl1[i], NULL)) {
231          Emsg0(M_ERROR_TERM, 0, _("Stmt went wrong\n"));
232       }
233    }
234
235
236    /*
237     * simple SELECT tests without tables
238     */
239    Pmsg0(0, "\nsimple SELECT tests without tables...\n\n");
240    const char *stmt2[8] = {
241       "SELECT 'Test of simple SELECT!'",
242       "SELECT 'Test of simple SELECT!' as Text",
243       "SELECT VARCHAR(LENGTH('Test of simple SELECT!'))",
244       "SELECT DBMSINFO('_version')",
245       "SELECT 'This is a ''quoting'' test with single quotes'",
246       "SELECT 'This is a \"quoting\" test with double quotes'",
247       "SELECT null",
248       "SELECT ''"
249    };
250    int (*hndl2[8])(void*,int,char**) = {
251       string_handler,
252       string_handler,
253       string_handler,
254       string_handler,
255       string_handler,
256       string_handler,
257       string_handler,
258       string_handler
259    };
260
261    for (int i=0; i<8; ++i) {
262       Pmsg1(0, "DB-Statement: %s\n",stmt2[i]);
263       if (!db_sql_query(db, stmt2[i], hndl2[i], NULL)) {
264          Emsg0(M_ERROR_TERM, 0, _("Stmt went wrong\n"));
265       }
266    }
267
268    /*
269     * testing aggregates like avg, max, sum
270     */
271    Pmsg0(0, "\ntesting aggregates...\n\n");
272    const char *stmt[11] = {
273       "CREATE TABLE t1 (c1 integer, c2 varchar(29))",
274       "INSERT INTO t1 VALUES (1,'foo')",
275       "INSERT INTO t1 VALUES (2,'bar')",
276       "INSERT INTO t1 VALUES (3,'fun')",
277       "INSERT INTO t1 VALUES (4,'egg')",
278       "SELECT max(c1) from t1",
279       "SELECT sum(c1) from t1",
280       "INSERT INTO t1 VALUES (5,NULL)",
281       "SELECT count(*) from t1",
282       "SELECT count(c2) from t1",
283       "DROP TABLE t1"
284    };
285    int (*hndl[11])(void*,int,char**) = {
286       NULL,
287       NULL,
288       NULL,
289       NULL,
290       NULL,
291       string_handler,
292       string_handler,
293       NULL,
294       string_handler,
295       string_handler,
296       NULL
297    };
298
299    for (int i=0; i<11; ++i) {
300       Pmsg1(0, "DB-Statement: %s\n",stmt[i]);
301       if (!db_sql_query(db, stmt[i], hndl[i], NULL)) {
302          Emsg0(M_ERROR_TERM, 0, _("Stmt went wrong\n"));
303       }
304    }
305
306
307    /* 
308     * datatypes test
309     */
310    Pmsg0(0, "\ndatatypes test... (TODO)\n\n");
311
312
313    Dmsg0(200, "DB-Statement: CREATE TABLE for datatypes\n");
314    if (!db_sql_query(db, "CREATE TABLE t2 ("
315      "c1        integer,"
316      "c2        varchar(255),"
317      "c3        char(255)"
318      /* some more datatypes... "c4      ," */
319      ")" , NULL, NULL)) {
320       Emsg0(M_ERROR_TERM, 0, _("CREATE-Stmt went wrong\n"));
321    }
322
323    Dmsg0(200, "DB-Statement: DROP TABLE for datatypes\n");
324    if (!db_sql_query(db, "DROP TABLE t2", NULL, NULL)) {
325       Emsg0(M_ERROR_TERM, 0, _("DROP-Stmt went wrong\n"));
326    }
327
328
329    db_close_database(NULL, db);
330    Dmsg0(200, "Database closed\n");
331
332    return 0;
333 }
334 #else  /* needed */
335 int main (int argc, char *argv[])
336 {
337    return 1;
338 }
339 #endif /* needed */