~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/pipe_query.c

  • Committer: Brian Aker
  • Date: 2010-02-14 01:56:51 UTC
  • mto: (1273.16.5 fix_is)
  • mto: This revision was merged to the branch mainline in revision 1300.
  • Revision ID: brian@gaz-20100214015651-ror9j0xu7dccz0ct
Two fixes for "make dist"

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Drizzle Client & Protocol Library
3
 
 *
4
 
 * Copyright (C) 2008 Eric Day (eday@oddments.org)
5
 
 * All rights reserved.
6
 
 *
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
 
 *
35
 
 */
36
 
 
37
 
 
38
 
#include "config.h"
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;
62
 
  drizzle_con_st *con= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
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
 
 
70
 
  if (con == NULL)
71
 
  {
72
 
    printf("Failed to allocate memory for drizzle connection");
73
 
    exit(1);
74
 
  }
75
 
 
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
 
 
83
 
  if (drizzle_con_create(&drizzle, con) == NULL)
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':
94
 
      drizzle_con_set_db(con, optarg);
95
 
      break;
96
 
 
97
 
    case 'h':
98
 
      host= optarg;
99
 
      break;
100
 
 
101
 
    case 'm':
102
 
      drizzle_con_add_options(con, DRIZZLE_CON_MYSQL);
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
 
 
131
 
  drizzle_con_set_tcp(con, host, port);
132
 
  drizzle_con_set_auth(con, user, password);
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
 
 
144
 
    buffer= realloc(buffer, buffer_size + BUFFER_CHUNK);
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
 
 
154
 
  (void)drizzle_query(con, &result, buffer, buffer_size, &ret);
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);
202
 
  drizzle_con_free(con);
203
 
  drizzle_free(&drizzle);
204
 
 
205
 
  free(con);
206
 
  return 0;
207
 
}