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