2
* Drizzle Client & Protocol Library
4
* Copyright (C) 2008 Eric Day (eday@oddments.org)
7
* Use and distribution licensed under the BSD license. See
8
* the COPYING file in this directory for full text.
19
#include <libdrizzle/drizzle_client.h>
21
#define BUFFER_CHUNK 8192
23
int main(int argc, char *argv[])
31
size_t buffer_size= 0;
32
size_t buffer_total= 0;
36
drizzle_result_st result;
38
drizzle_field_t field;
43
/* The docs say this might fail, so check for errors. */
44
if (drizzle_create(&drizzle) == NULL)
46
printf("drizzle_create:failed\n");
50
if (drizzle_con_create(&drizzle, &con) == NULL)
52
printf("drizzle_con_create:%s\n", drizzle_error(&drizzle));
56
while ((c = getopt(argc, argv, "d:h:Hmp:P:u:")) != -1)
61
drizzle_con_set_db(&con, optarg);
69
drizzle_con_add_options(&con, DRIZZLE_CON_MYSQL);
77
port= (in_port_t)atoi(optarg);
86
printf("\nUsage: %s [options] [query]\n", argv[0]);
87
printf("\t-d <db> - Use <db> for the connection\n");
88
printf("\t-h <host> - Connect to <host>\n");
89
printf("\t-H - Print this help menu\n");
90
printf("\t-m - Use MySQL protocol\n");
91
printf("\t-p <password> - Use <password> for authentication\n");
92
printf("\t-P <port> - Connect to <port>\n");
93
printf("\t-u <user> - Use <user> for authentication\n");
98
drizzle_con_set_tcp(&con, host, port);
99
drizzle_con_set_auth(&con, user, password);
105
printf("read:%d\n", errno);
109
buffer_size+= (size_t)read_size;
111
buffer= realloc(buffer, buffer_size + BUFFER_CHUNK);
114
printf("realloc:%d\n", errno);
118
buffer_total= buffer_size + BUFFER_CHUNK;
119
} while ((read_size= read(0, buffer + buffer_size, BUFFER_CHUNK)) != 0);
121
(void)drizzle_query(&con, &result, buffer, buffer_size, &ret);
122
if (ret != DRIZZLE_RETURN_OK)
124
printf("drizzle_query:%s\n", drizzle_error(&drizzle));
130
ret= drizzle_column_skip(&result);
131
if (ret != DRIZZLE_RETURN_OK)
133
printf("drizzle_column_skip:%s\n", drizzle_error(&drizzle));
137
while (drizzle_row_read(&result, &ret) != 0 && ret == DRIZZLE_RETURN_OK)
141
field= drizzle_field_read(&result, &offset, &size, &total, &ret);
142
if (ret == DRIZZLE_RETURN_ROW_END)
144
else if (ret != DRIZZLE_RETURN_OK)
146
printf("drizzle_field_read:%s\n", drizzle_error(&drizzle));
153
printf("%.*s", (int)size, field);
155
if (offset + size == total)
162
if (ret != DRIZZLE_RETURN_OK)
164
printf("drizzle_row_read:%s\n", drizzle_error(&drizzle));
168
drizzle_result_free(&result);
169
drizzle_con_free(&con);
170
drizzle_free(&drizzle);