~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
  ssize_t read_size= 0;
60
  drizzle_st drizzle;
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
61
  drizzle_con_st *con= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
62
  drizzle_result_st result;
63
  drizzle_return_t ret;
64
  drizzle_field_t field;
65
  size_t offset;
66
  size_t size;
67
  size_t total;
68
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
69
  if (con == NULL)
70
  {
71
    printf("Failed to allocate memory for drizzle connection");
72
    exit(1);
73
  }
74
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
75
  /* The docs say this might fail, so check for errors. */
76
  if (drizzle_create(&drizzle) == NULL)
77
  {
78
    printf("drizzle_create:failed\n");
79
    exit(1);
80
  }
81
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
82
  if (drizzle_con_create(&drizzle, con) == NULL)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
83
  {
84
    printf("drizzle_con_create:%s\n", drizzle_error(&drizzle));
85
    exit(1);
86
  }
87
88
  while ((c = getopt(argc, argv, "d:h:Hmp:P:u:")) != -1)
89
  {
90
    switch(c)
91
    {
92
    case 'd':
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
93
      drizzle_con_set_db(con, optarg);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
94
      break;
95
96
    case 'h':
97
      host= optarg;
98
      break;
99
100
    case 'm':
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
101
      drizzle_con_add_options(con, DRIZZLE_CON_MYSQL);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
102
      break;
103
104
    case 'p':
105
      password= optarg;
106
      break;
107
108
    case 'P':
109
      port= (in_port_t)atoi(optarg);
110
      break;
111
112
    case 'u':
113
      user= optarg;
114
      break;
115
116
    case 'H':
117
    default:
118
      printf("\nUsage: %s [options] [query]\n", argv[0]);
119
      printf("\t-d <db>       - Use <db> for the connection\n");
120
      printf("\t-h <host>     - Connect to <host>\n");
121
      printf("\t-H            - Print this help menu\n");
122
      printf("\t-m            - Use MySQL protocol\n");
123
      printf("\t-p <password> - Use <password> for authentication\n");
124
      printf("\t-P <port>     - Connect to <port>\n");
125
      printf("\t-u <user>     - Use <user> for authentication\n");
126
      exit(0);
127
    }
128
  }
129
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
130
  drizzle_con_set_tcp(con, host, port);
131
  drizzle_con_set_auth(con, user, password);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
132
133
  do
134
  {
135
    if (read_size == -1)
136
    {
137
      printf("read:%d\n", errno);
138
      return 1;
139
    }
140
141
    buffer_size+= (size_t)read_size;
142
1992.6.2 by Monty Taylor
Cleaned up for additional gcc 4.5 warnings.
143
    buffer= (char *)realloc(buffer, buffer_size + BUFFER_CHUNK);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
144
    if (buffer == NULL)
145
    {
146
      printf("realloc:%d\n", errno);
147
      return 1;
148
    }
149
150
  } while ((read_size= read(0, buffer + buffer_size, BUFFER_CHUNK)) != 0);
151
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
152
  (void)drizzle_query(con, &result, buffer, buffer_size, &ret);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
153
  if (ret != DRIZZLE_RETURN_OK)
154
  {
155
    printf("drizzle_query:%s\n", drizzle_error(&drizzle));
156
    return 1;
157
  }
158
159
  free(buffer);
160
161
  ret= drizzle_column_skip(&result);
162
  if (ret != DRIZZLE_RETURN_OK)
163
  {
164
    printf("drizzle_column_skip:%s\n", drizzle_error(&drizzle));
165
    return 1;
166
  }
167
168
  while (drizzle_row_read(&result, &ret) != 0 && ret == DRIZZLE_RETURN_OK)
169
  {
170
    while (1)
171
    {
172
      field= drizzle_field_read(&result, &offset, &size, &total, &ret);
173
      if (ret == DRIZZLE_RETURN_ROW_END)
174
        break;
175
      else if (ret != DRIZZLE_RETURN_OK)
176
      {
177
        printf("drizzle_field_read:%s\n", drizzle_error(&drizzle));
178
        return 1;
179
      }
180
181
      if (field == NULL)
182
        printf("NULL");
183
      else
184
        printf("%.*s", (int)size, field);
185
186
      if (offset + size == total)
187
        printf("\t");
188
    }
189
190
    printf("\n");
191
  }
192
193
  if (ret != DRIZZLE_RETURN_OK)
194
  {
195
    printf("drizzle_row_read:%s\n", drizzle_error(&drizzle));
196
    return 1;
197
  }
198
199
  drizzle_result_free(&result);
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
200
  drizzle_con_free(con);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
201
  drizzle_free(&drizzle);
202
1929.1.24 by Stewart Smith
fix large stack usage in example/pipe_query.c
203
  free(con);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
204
  return 0;
205
}