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