]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/tools/ing_test.c
Tweak testls.c
[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 two of the GNU 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 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/bvfs.h"
42 #include "findlib/find.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 /* simple handler for debug output of CRUD example*/
76 static int test_handler(void *ctx, int num_fields, char **row)
77 {
78    Dmsg2(200, "   Values are %d, %s\n", str_to_int64(row[0]), row[1]);
79    return 0;
80 }
81
82
83
84 /* number of thread started */
85
86 int main (int argc, char *argv[])
87 {
88    int ch;
89    char *jobids = (char *)"1";
90    char *path=NULL, *client=NULL;
91    uint64_t limit=0;
92    bool clean=false;
93    setlocale(LC_ALL, "");
94    bindtextdomain("bacula", LOCALEDIR);
95    textdomain("bacula");
96    init_stack_dump();
97
98    Dmsg0(0, "Starting ing_test tool\n");
99    
100    my_name_is(argc, argv, "ing_test");
101    init_msg(NULL, NULL);
102
103    OSDependentInit();
104
105    while ((ch = getopt(argc, argv, "h:c:l:d:n:P:Su:vf:w:?j:p:f:T")) != -1) {
106       switch (ch) {
107       case 'd':                    /* debug level */
108          if (*optarg == 't') {
109             dbg_timestamp = true;
110          } else {
111             debug_level = atoi(optarg);
112             if (debug_level <= 0) {
113                debug_level = 1;
114             }
115          }
116          break;
117       case 'l':
118          limit = str_to_int64(optarg);
119          break;
120
121       case 'c':
122          client = optarg;
123          break;
124
125       case 'h':
126          db_host = optarg;
127          break;
128
129       case 'n':
130          db_name = optarg;
131          break;
132
133       case 'w':
134          working_directory = optarg;
135          break;
136
137       case 'u':
138          db_user = optarg;
139          break;
140
141       case 'P':
142          db_password = optarg;
143          break;
144
145       case 'v':
146          verbose++;
147          break;
148
149       case 'p':
150          path = optarg;
151          break;
152
153       case 'f':
154          file = optarg;
155          break;
156
157       case 'j':
158          jobids = optarg;
159          break;
160
161       case 'T':
162          clean = true;
163          break;
164
165       case '?':
166       default:
167          usage();
168
169       }
170    }
171    argc -= optind;
172    argv += optind;
173
174    if (argc != 0) {
175       Pmsg0(0, _("Wrong number of arguments: \n"));
176       usage();
177    }
178    
179    if ((db=db_init_database(NULL, db_name, db_user, db_password,
180                             db_host, 0, NULL, 0)) == NULL) {
181       Emsg0(M_ERROR_TERM, 0, _("Could not init Bacula database\n"));
182    }
183    Dmsg1(0, "db_type=%s\n", db_get_type());
184
185    if (!db_open_database(NULL, db)) {
186       Emsg0(M_ERROR_TERM, 0, db_strerror(db));
187    }
188    Dmsg0(200, "Database opened\n");
189    if (verbose) {
190       Pmsg2(000, _("Using Database: %s, User: %s\n"), db_name, db_user);
191    }
192    
193
194    /* simple CRUD test including create/drop table */
195
196    Pmsg0(0, "\nsimple CRUD test...\n\n");
197
198    Dmsg0(200, "DB-Statement: CREATE TABLE t1 ( c1 integer, c2 varchar(29))\n");
199    if (!db_sql_query(db, "CREATE TABLE t1 ( c1 integer, c2 varchar(29))", NULL, NULL))
200    {
201       Emsg0(M_ERROR_TERM, 0, _("CREATE-Stmt went wrong\n"));
202    }
203
204    Dmsg0(200, "DB-Statement: INSERT INTO t1 VALUES (1, 'foo')\n");
205    if (!db_sql_query(db, "INSERT INTO t1 VALUES (1, 'foo')", NULL, NULL))
206    {
207       Emsg0(M_ERROR_TERM, 0, _("INSERT-Stmt went wrong\n"));
208    }
209
210    Dmsg0(200, "DB-Statement: SELECT * FROM t1 (should be 1, foo)\n");
211    if (!db_sql_query(db, "SELECT * FROM t1", test_handler, NULL))
212    {
213       Emsg0(M_ERROR_TERM, 0, _("SELECT-Stmt went wrong\n"));
214    }
215
216    Dmsg0(200, "DB-Statement: UPDATE t1 SET c2='bar' WHERE c1=1\n");
217    if (!db_sql_query(db, "UPDATE t1 SET c2='bar' WHERE c1=1", NULL, NULL))
218    {
219       Emsg0(M_ERROR_TERM, 0, _("UPDATE-Stmt went wrong\n"));
220    }
221
222    Dmsg0(200, "DB-Statement: SELECT * FROM t1 (should be 1, bar)\n");
223    if (!db_sql_query(db, "SELECT * FROM t1", test_handler, NULL))
224    {
225       Emsg0(M_ERROR_TERM, 0, _("SELECT-Stmt went wrong\n"));
226    }
227
228    Dmsg0(200, "DB-Statement: DELETE FROM t1 WHERE c2 LIKE '%r'\n");
229    if (!db_sql_query(db, "DELETE FROM t1 WHERE c2 LIKE '%r'", NULL, NULL))
230    {
231       Emsg0(M_ERROR_TERM, 0, _("DELETE-Stmt went wrong\n"));
232    }
233
234    Dmsg0(200, "DB-Statement: SELECT * FROM t1 (should be 0 rows)\n");
235    if (!db_sql_query(db, "SELECT * FROM t1", test_handler, NULL))
236    {
237       Emsg0(M_ERROR_TERM, 0, _("SELECT-Stmt went wrong\n"));
238    }
239
240    Dmsg0(200, "DB-Statement: DROP TABLE t1\n");
241    if (!db_sql_query(db, "DROP TABLE t1", NULL, NULL))
242    {
243       Emsg0(M_ERROR_TERM, 0, _("DROP-Stmt went wrong\n"));
244    }
245
246    /* datatypes test */
247    Pmsg0(0, "\ndatatypes test... (TODO)\n\n");
248
249
250    Dmsg0(200, "DB-Statement: CREATE TABLE for datatypes\n");
251    if (!db_sql_query(db, "CREATE TABLE t2 ("
252      "c1        integer,"
253      "c2        varchar(255),"
254      "c3        char(255)"
255      /* some more datatypes... "c4      ," */
256      ")" , NULL, NULL))
257    {
258       Emsg0(M_ERROR_TERM, 0, _("CREATE-Stmt went wrong\n"));
259    }
260
261    Dmsg0(200, "DB-Statement: DROP TABLE for datatypes\n");
262    if (!db_sql_query(db, "DROP TABLE t2", NULL, NULL))
263    {
264       Emsg0(M_ERROR_TERM, 0, _("DROP-Stmt went wrong\n"));
265    }
266
267
268    db_close_database(NULL, db);
269    Dmsg0(200, "Database closed\n");
270
271    return 0;
272 }