forked from trilogy-libraries/trilogy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrilogy_query.c
235 lines (191 loc) · 6.22 KB
/
trilogy_query.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <getopt.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "trilogy.h"
#define DEFAULT_HOST "127.0.0.1"
#define DEFAULT_PORT 3306
#define DEFAULT_USER "nobody"
static int execute_query(trilogy_conn_t *conn, const char *sql)
{
fprintf(stderr, "\nsending query command\n");
uint64_t column_count = 0;
int rc = trilogy_query(conn, sql, strlen(sql), &column_count);
switch (rc) {
case TRILOGY_OK:
fprintf(stderr, "(no results)\n");
return rc;
case TRILOGY_HAVE_RESULTS:
break;
default:
return rc;
}
bool *binary_columns = calloc(column_count, sizeof(bool));
for (uint64_t i = 0; i < column_count; i++) {
trilogy_column_packet_t column;
rc = trilogy_read_full_column(conn, &column);
if (rc < 0) {
free(binary_columns);
return rc;
}
printf("%.*s", (int)column.name_len, column.name);
if (i + 1 < column_count) {
printf(",");
}
binary_columns[i] = false;
if (column.flags & TRILOGY_COLUMN_FLAG_BINARY) {
binary_columns[i] = true;
}
}
printf("\n");
// shut scan-build up
if (column_count == 0) {
free(binary_columns);
return TRILOGY_OK;
}
trilogy_value_t *values = calloc(column_count, sizeof(trilogy_value_t));
while ((rc = trilogy_read_full_row(conn, values)) == TRILOGY_OK) {
for (uint64_t i = 0; i < column_count; i++) {
if (values[i].is_null) {
printf("NULL");
} else {
if (binary_columns[i]) {
printf("\"<<<binary value - %zu bytes>>>\"", values[i].data_len);
} else {
printf("\"%.*s\"", (int)values[i].data_len, (const char *)values[i].data);
}
}
if (i + 1 < column_count) {
printf(",");
}
}
printf("\n");
}
free(binary_columns);
free(values);
if (rc == TRILOGY_EOF) {
rc = TRILOGY_OK;
}
return rc;
}
void fail_on_error(const trilogy_conn_t *conn, int err, const char *description)
{
if (err < 0) {
fprintf(stderr, "%s error: %s %d\n", description, trilogy_error(err), err);
if (err == TRILOGY_ERR) {
fprintf(stderr, "%d %.*s\n", conn->error_code, (int)conn->error_message_len, conn->error_message);
} else if (err == TRILOGY_SYSERR) {
perror("");
}
exit(EXIT_FAILURE);
}
}
int main(int argc, char *argv[])
{
trilogy_sockopt_t connopt = {0};
char *sql = NULL;
static struct option longopts[] = {{"host", optional_argument, NULL, 'h'},
{"port", optional_argument, NULL, 'P'},
{"sql", optional_argument, NULL, 's'},
{"database", optional_argument, NULL, 'd'},
{"user", optional_argument, NULL, 'u'},
{"pass", optional_argument, NULL, 'p'},
{NULL, 0, NULL, 0}};
if (!(connopt.hostname = getenv("MYSQL_HOST"))) {
connopt.hostname = DEFAULT_HOST;
}
connopt.hostname = strdup(connopt.hostname);
const char *port = getenv("MYSQL_TCP_PORT");
if (port != NULL) {
connopt.port = atoi(port);
}
if (connopt.port == 0) {
connopt.port = DEFAULT_PORT;
}
if (!(connopt.username = getenv("USER"))) {
connopt.username = DEFAULT_USER;
}
connopt.username = strdup(connopt.username);
int opt = 0;
while ((opt = getopt_long(argc, argv, "h:P:s:d:u:p:", longopts, NULL)) != -1) {
switch (opt) {
case 'h':
if (optarg) {
free(connopt.hostname);
connopt.hostname = strdup(optarg);
}
break;
case 'P':
if (optarg) {
connopt.port = atoi(optarg);
}
break;
case 's':
if (optarg) {
free(sql);
sql = strdup(optarg);
}
break;
case 'd':
if (optarg) {
free(connopt.database);
connopt.database = strdup(optarg);
}
break;
case 'u':
if (optarg) {
free(connopt.username);
connopt.username = strdup(optarg);
}
break;
case 'p':
if (optarg) {
free(connopt.password);
connopt.password = strdup(optarg);
connopt.password_len = strlen(optarg);
}
break;
}
}
int err;
trilogy_conn_t conn;
trilogy_init(&conn);
fprintf(stderr, "connecting to %s:%hu as %s...\n", connopt.hostname, connopt.port, connopt.username);
err = trilogy_connect(&conn, &connopt);
fail_on_error(&conn, err, "connect");
fprintf(stderr, "connected\n");
fprintf(stderr, "\nsending ping command\n");
err = trilogy_ping(&conn);
fail_on_error(&conn, err, "ping");
fprintf(stderr, "ping success\n");
if (connopt.database) {
fprintf(stderr, "\nsending change db command\n");
err = trilogy_change_db(&conn, connopt.database, strlen(connopt.database));
fail_on_error(&conn, err, "change db");
fprintf(stderr, "change db success\n");
}
if (sql) {
switch ((err = execute_query(&conn, sql))) {
case TRILOGY_OK:
break;
case TRILOGY_ERR:
fprintf(stderr, "error executing query: mysql said: %d %.*s\n", conn.error_code,
(int)conn.error_message_len, conn.error_message);
break;
default:
fail_on_error(&conn, err, "executing query");
}
}
fprintf(stderr, "\nsending quit command and closing connection\n");
err = trilogy_close(&conn);
fail_on_error(&conn, err, "closing connection");
fprintf(stderr, "connection closed\n");
free(connopt.hostname);
free(connopt.username);
free(sql);
free(connopt.database);
free(connopt.password);
trilogy_free(&conn);
exit(EXIT_SUCCESS);
}