~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/pipe_query.c

Renamed more stuff to drizzle.

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
 
 * Use and distribution licensed under the BSD license.  See
8
 
 * the COPYING file in this directory for full text.
9
 
 */
10
 
 
11
 
#include "config.h"
12
 
 
13
 
#include <errno.h>
14
 
#include <stdio.h>
15
 
#include <stdlib.h>
16
 
#include <string.h>
17
 
#include <unistd.h>
18
 
 
19
 
#include <libdrizzle/drizzle_client.h>
20
 
 
21
 
#define BUFFER_CHUNK 8192
22
 
 
23
 
int main(int argc, char *argv[])
24
 
{
25
 
  int c;
26
 
  char *host= NULL;
27
 
  in_port_t port= 0;
28
 
  char *user= NULL;
29
 
  char *password= NULL;
30
 
  char *buffer= NULL;
31
 
  size_t buffer_size= 0;
32
 
  size_t buffer_total= 0;
33
 
  ssize_t read_size= 0;
34
 
  drizzle_st drizzle;
35
 
  drizzle_con_st con;
36
 
  drizzle_result_st result;
37
 
  drizzle_return_t ret;
38
 
  drizzle_field_t field;
39
 
  size_t offset;
40
 
  size_t size;
41
 
  size_t total;
42
 
 
43
 
  /* The docs say this might fail, so check for errors. */
44
 
  if (drizzle_create(&drizzle) == NULL)
45
 
  {
46
 
    printf("drizzle_create:failed\n");
47
 
    exit(1);
48
 
  }
49
 
 
50
 
  if (drizzle_con_create(&drizzle, &con) == NULL)
51
 
  {
52
 
    printf("drizzle_con_create:%s\n", drizzle_error(&drizzle));
53
 
    exit(1);
54
 
  }
55
 
 
56
 
  while ((c = getopt(argc, argv, "d:h:Hmp:P:u:")) != -1)
57
 
  {
58
 
    switch(c)
59
 
    {
60
 
    case 'd':
61
 
      drizzle_con_set_db(&con, optarg);
62
 
      break;
63
 
 
64
 
    case 'h':
65
 
      host= optarg;
66
 
      break;
67
 
 
68
 
    case 'm':
69
 
      drizzle_con_add_options(&con, DRIZZLE_CON_MYSQL);
70
 
      break;
71
 
 
72
 
    case 'p':
73
 
      password= optarg;
74
 
      break;
75
 
 
76
 
    case 'P':
77
 
      port= (in_port_t)atoi(optarg);
78
 
      break;
79
 
 
80
 
    case 'u':
81
 
      user= optarg;
82
 
      break;
83
 
 
84
 
    case 'H':
85
 
    default:
86
 
      printf("\nUsage: %s [options] [query]\n", argv[0]);
87
 
      printf("\t-d <db>       - Use <db> for the connection\n");
88
 
      printf("\t-h <host>     - Connect to <host>\n");
89
 
      printf("\t-H            - Print this help menu\n");
90
 
      printf("\t-m            - Use MySQL protocol\n");
91
 
      printf("\t-p <password> - Use <password> for authentication\n");
92
 
      printf("\t-P <port>     - Connect to <port>\n");
93
 
      printf("\t-u <user>     - Use <user> for authentication\n");
94
 
      exit(0);
95
 
    }
96
 
  }
97
 
 
98
 
  drizzle_con_set_tcp(&con, host, port);
99
 
  drizzle_con_set_auth(&con, user, password);
100
 
 
101
 
  do
102
 
  {
103
 
    if (read_size == -1)
104
 
    {
105
 
      printf("read:%d\n", errno);
106
 
      return 1;
107
 
    }
108
 
 
109
 
    buffer_size+= (size_t)read_size;
110
 
 
111
 
    buffer= realloc(buffer, buffer_size + BUFFER_CHUNK);
112
 
    if (buffer == NULL)
113
 
    {
114
 
      printf("realloc:%d\n", errno);
115
 
      return 1;
116
 
    }
117
 
 
118
 
    buffer_total= buffer_size + BUFFER_CHUNK;
119
 
  } while ((read_size= read(0, buffer + buffer_size, BUFFER_CHUNK)) != 0);
120
 
 
121
 
  (void)drizzle_query(&con, &result, buffer, buffer_size, &ret);
122
 
  if (ret != DRIZZLE_RETURN_OK)
123
 
  {
124
 
    printf("drizzle_query:%s\n", drizzle_error(&drizzle));
125
 
    return 1;
126
 
  }
127
 
 
128
 
  free(buffer);
129
 
 
130
 
  ret= drizzle_column_skip(&result);
131
 
  if (ret != DRIZZLE_RETURN_OK)
132
 
  {
133
 
    printf("drizzle_column_skip:%s\n", drizzle_error(&drizzle));
134
 
    return 1;
135
 
  }
136
 
 
137
 
  while (drizzle_row_read(&result, &ret) != 0 && ret == DRIZZLE_RETURN_OK)
138
 
  {
139
 
    while (1)
140
 
    {
141
 
      field= drizzle_field_read(&result, &offset, &size, &total, &ret);
142
 
      if (ret == DRIZZLE_RETURN_ROW_END)
143
 
        break;
144
 
      else if (ret != DRIZZLE_RETURN_OK)
145
 
      {
146
 
        printf("drizzle_field_read:%s\n", drizzle_error(&drizzle));
147
 
        return 1;
148
 
      }
149
 
 
150
 
      if (field == NULL)
151
 
        printf("NULL");
152
 
      else
153
 
        printf("%.*s", (int)size, field);
154
 
 
155
 
      if (offset + size == total)
156
 
        printf("\t");
157
 
    }
158
 
 
159
 
    printf("\n");
160
 
  }
161
 
 
162
 
  if (ret != DRIZZLE_RETURN_OK)
163
 
  {
164
 
    printf("drizzle_row_read:%s\n", drizzle_error(&drizzle));
165
 
    return 1;
166
 
  }
167
 
 
168
 
  drizzle_result_free(&result);
169
 
  drizzle_con_free(&con);
170
 
  drizzle_free(&drizzle);
171
 
 
172
 
  return 0;
173
 
}