1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
1 |
/*
|
2 |
* Drizzle Client & Protocol Library
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Eric Day (eday@oddments.org)
|
|
5 |
* All rights reserved.
|
|
6 |
*
|
|
7 |
* Use and distribution licensed under the BSD license. See
|
|
1799.2.4
by Monty Taylor
Made BSD files reference root BSD file. |
8 |
* the COPYING.BSD file in the root source directory for full text.
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
9 |
*/
|
10 |
||
11 |
#include <stdio.h> |
|
12 |
#include <stdlib.h> |
|
13 |
#include <string.h> |
|
14 |
#include <unistd.h> |
|
1720.1.5
by Monty Taylor
Added libdrizzle to the tree. |
15 |
#include <netdb.h> |
16 |
#include <getopt.h> |
|
1712.1.1
by Monty Taylor
Merged libdrizzle directly into tree. |
17 |
|
18 |
#include <libdrizzle/drizzle_client.h> |
|
19 |
||
20 |
int main(int argc, char *argv[]) |
|
21 |
{
|
|
22 |
int c; |
|
23 |
const char *db= "INFORMATION_SCHEMA"; |
|
24 |
const char *host= NULL; |
|
25 |
bool mysql= false; |
|
26 |
in_port_t port= 0; |
|
27 |
const char *query= "SELECT TABLE_SCHEMA,TABLE_NAME FROM TABLES"; |
|
28 |
drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER; |
|
29 |
drizzle_st drizzle; |
|
30 |
drizzle_con_st con; |
|
31 |
drizzle_result_st result; |
|
32 |
drizzle_return_t ret; |
|
33 |
int x; |
|
34 |
char **row; |
|
35 |
||
36 |
while ((c = getopt(argc, argv, "d:h:mp:q:v")) != -1) |
|
37 |
{
|
|
38 |
switch(c) |
|
39 |
{
|
|
40 |
case 'd': |
|
41 |
db= optarg; |
|
42 |
break; |
|
43 |
||
44 |
case 'h': |
|
45 |
host= optarg; |
|
46 |
break; |
|
47 |
||
48 |
case 'm': |
|
49 |
mysql= true; |
|
50 |
break; |
|
51 |
||
52 |
case 'p': |
|
53 |
port= (in_port_t)atoi(optarg); |
|
54 |
break; |
|
55 |
||
56 |
case 'q': |
|
57 |
query= optarg; |
|
58 |
break; |
|
59 |
||
60 |
case 'v': |
|
61 |
verbose++; |
|
62 |
break; |
|
63 |
||
64 |
default: |
|
65 |
printf("usage: %s [-d <db>] [-h <host>] [-m] [-p <port>] [-q <query>] " |
|
66 |
"[-v]\n", argv[0]); |
|
67 |
printf("\t-d <db> - Database to use for query\n"); |
|
68 |
printf("\t-h <host> - Host to listen on\n"); |
|
69 |
printf("\t-m - Use the MySQL protocol\n"); |
|
70 |
printf("\t-p <port> - Port to listen on\n"); |
|
71 |
printf("\t-q <query> - Query to run\n"); |
|
72 |
printf("\t-v - Increase verbosity level\n"); |
|
73 |
return 1; |
|
74 |
}
|
|
75 |
}
|
|
76 |
||
77 |
if (drizzle_create(&drizzle) == NULL) |
|
78 |
{
|
|
79 |
printf("drizzle_create:NULL\n"); |
|
80 |
return 1; |
|
81 |
}
|
|
82 |
||
83 |
drizzle_set_verbose(&drizzle, verbose); |
|
84 |
||
85 |
if (drizzle_con_create(&drizzle, &con) == NULL) |
|
86 |
{
|
|
87 |
printf("drizzle_con_create:NULL\n"); |
|
88 |
return 1; |
|
89 |
}
|
|
90 |
||
91 |
if (mysql) |
|
92 |
drizzle_con_add_options(&con, DRIZZLE_CON_MYSQL); |
|
93 |
||
94 |
drizzle_con_set_tcp(&con, host, port); |
|
95 |
drizzle_con_set_db(&con, db); |
|
96 |
||
97 |
(void)drizzle_query_str(&con, &result, query, &ret); |
|
98 |
if (ret != DRIZZLE_RETURN_OK) |
|
99 |
{
|
|
100 |
printf("drizzle_query:%s\n", drizzle_con_error(&con)); |
|
101 |
return 1; |
|
102 |
}
|
|
103 |
||
104 |
ret= drizzle_result_buffer(&result); |
|
105 |
if (ret != DRIZZLE_RETURN_OK) |
|
106 |
{
|
|
107 |
printf("drizzle_result_buffer:%s\n", drizzle_con_error(&con)); |
|
108 |
return 1; |
|
109 |
}
|
|
110 |
||
111 |
while ((row= (char **)drizzle_row_next(&result)) != NULL) |
|
112 |
{
|
|
113 |
for (x= 0; x < drizzle_result_column_count(&result); x++) |
|
114 |
printf("%s%s", x == 0 ? "" : ":", row[x] == NULL ? "NULL" : row[x]); |
|
115 |
printf("\n"); |
|
116 |
}
|
|
117 |
||
118 |
drizzle_result_free(&result); |
|
119 |
drizzle_con_free(&con); |
|
120 |
drizzle_free(&drizzle); |
|
121 |
||
122 |
return 0; |
|
123 |
}
|