~drizzle-trunk/drizzle/development

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
 *
1971.2.1 by kalebral at gmail
update files that did not have license or had incorrect license structure
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions are
9
 * met:
10
 *
11
 *     * Redistributions of source code must retain the above copyright
12
 * notice, this list of conditions and the following disclaimer.
13
 *
14
 *     * Redistributions in binary form must reproduce the above
15
 * copyright notice, this list of conditions and the following disclaimer
16
 * in the documentation and/or other materials provided with the
17
 * distribution.
18
 *
19
 *     * The names of its contributors may not be used to endorse or
20
 * promote products derived from this software without specific prior
21
 * written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
 *
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
35
 */
36
1971.2.1 by kalebral at gmail
update files that did not have license or had incorrect license structure
37
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
38
#include <config.h>
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
39
40
#include <errno.h>
41
#include <stdio.h>
42
#include <stdlib.h>
43
#include <string.h>
44
#include <unistd.h>
45
46
#include <libdrizzle/drizzle_client.h>
47
48
#define BUFFER_CHUNK 8192
49
50
int main(int argc, char *argv[])
51
{
52
  int c;
53
  char *host= NULL;
54
  in_port_t port= 0;
55
  char *user= NULL;
56
  char *password= NULL;
57
  char *buffer= NULL;
58
  size_t buffer_size= 0;
59
  size_t buffer_total= 0;
60
  ssize_t read_size= 0;
61
  drizzle_st drizzle;
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
62
  drizzle_con_st *con= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
63
  drizzle_result_st result;
64
  drizzle_return_t ret;
65
  drizzle_field_t field;
66
  size_t offset;
67
  size_t size;
68
  size_t total;
69
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
70
  if (con == NULL)
71
  {
72
    printf("Failed to allocate memory for drizzle connection");
73
    exit(1);
74
  }
75
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
76
  /* The docs say this might fail, so check for errors. */
77
  if (drizzle_create(&drizzle) == NULL)
78
  {
79
    printf("drizzle_create:failed\n");
80
    exit(1);
81
  }
82
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
83
  if (drizzle_con_create(&drizzle, con) == NULL)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
84
  {
85
    printf("drizzle_con_create:%s\n", drizzle_error(&drizzle));
86
    exit(1);
87
  }
88
89
  while ((c = getopt(argc, argv, "d:h:Hmp:P:u:")) != -1)
90
  {
91
    switch(c)
92
    {
93
    case 'd':
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
94
      drizzle_con_set_db(con, optarg);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
95
      break;
96
97
    case 'h':
98
      host= optarg;
99
      break;
100
101
    case 'm':
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
102
      drizzle_con_add_options(con, DRIZZLE_CON_MYSQL);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
103
      break;
104
105
    case 'p':
106
      password= optarg;
107
      break;
108
109
    case 'P':
110
      port= (in_port_t)atoi(optarg);
111
      break;
112
113
    case 'u':
114
      user= optarg;
115
      break;
116
117
    case 'H':
118
    default:
119
      printf("\nUsage: %s [options] [query]\n", argv[0]);
120
      printf("\t-d <db>       - Use <db> for the connection\n");
121
      printf("\t-h <host>     - Connect to <host>\n");
122
      printf("\t-H            - Print this help menu\n");
123
      printf("\t-m            - Use MySQL protocol\n");
124
      printf("\t-p <password> - Use <password> for authentication\n");
125
      printf("\t-P <port>     - Connect to <port>\n");
126
      printf("\t-u <user>     - Use <user> for authentication\n");
127
      exit(0);
128
    }
129
  }
130
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
131
  drizzle_con_set_tcp(con, host, port);
132
  drizzle_con_set_auth(con, user, password);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
133
134
  do
135
  {
136
    if (read_size == -1)
137
    {
138
      printf("read:%d\n", errno);
139
      return 1;
140
    }
141
142
    buffer_size+= (size_t)read_size;
143
1992.6.2 by Monty Taylor
Cleaned up for additional gcc 4.5 warnings.
144
    buffer= (char *)realloc(buffer, buffer_size + BUFFER_CHUNK);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
145
    if (buffer == NULL)
146
    {
147
      printf("realloc:%d\n", errno);
148
      return 1;
149
    }
150
151
    buffer_total= buffer_size + BUFFER_CHUNK;
152
  } while ((read_size= read(0, buffer + buffer_size, BUFFER_CHUNK)) != 0);
153
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
154
  (void)drizzle_query(con, &result, buffer, buffer_size, &ret);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
155
  if (ret != DRIZZLE_RETURN_OK)
156
  {
157
    printf("drizzle_query:%s\n", drizzle_error(&drizzle));
158
    return 1;
159
  }
160
161
  free(buffer);
162
163
  ret= drizzle_column_skip(&result);
164
  if (ret != DRIZZLE_RETURN_OK)
165
  {
166
    printf("drizzle_column_skip:%s\n", drizzle_error(&drizzle));
167
    return 1;
168
  }
169
170
  while (drizzle_row_read(&result, &ret) != 0 && ret == DRIZZLE_RETURN_OK)
171
  {
172
    while (1)
173
    {
174
      field= drizzle_field_read(&result, &offset, &size, &total, &ret);
175
      if (ret == DRIZZLE_RETURN_ROW_END)
176
        break;
177
      else if (ret != DRIZZLE_RETURN_OK)
178
      {
179
        printf("drizzle_field_read:%s\n", drizzle_error(&drizzle));
180
        return 1;
181
      }
182
183
      if (field == NULL)
184
        printf("NULL");
185
      else
186
        printf("%.*s", (int)size, field);
187
188
      if (offset + size == total)
189
        printf("\t");
190
    }
191
192
    printf("\n");
193
  }
194
195
  if (ret != DRIZZLE_RETURN_OK)
196
  {
197
    printf("drizzle_row_read:%s\n", drizzle_error(&drizzle));
198
    return 1;
199
  }
200
201
  drizzle_result_free(&result);
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
202
  drizzle_con_free(con);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
203
  drizzle_free(&drizzle);
204
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
205
  free(con);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
206
  return 0;
207
}