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