~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to examples/simple.c

  • Committer: Brian Aker
  • Date: 2010-07-16 05:40:08 UTC
  • mto: This revision was merged to the branch mainline in revision 1661.
  • Revision ID: brian@gaz-20100716054008-cfcvyu0akpwqnwqd
Encapsulate Table in field

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.BSD file in the root source directory for full text.
9
 
 */
10
 
 
11
 
#include <stdio.h>
12
 
#include <stdlib.h>
13
 
#include <string.h>
14
 
#include <unistd.h>
15
 
#include <netdb.h>
16
 
#include <getopt.h>
17
 
 
18
 
#include <libdrizzle/drizzle_client.h>
19
 
 
20
 
int main(int argc, char *argv[])
21
 
{
22
 
  int c;
23
 
  const char *db= "INFORMATION_SCHEMA";
24
 
  const char *host= NULL;
25
 
  bool mysql= false;
26
 
  in_port_t port= 0;
27
 
  const char *query= "SELECT TABLE_SCHEMA,TABLE_NAME FROM TABLES";
28
 
  drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER;
29
 
  drizzle_st drizzle;
30
 
  drizzle_con_st *con= (drizzle_con_st*)malloc(sizeof(drizzle_con_st));
31
 
  drizzle_result_st result;
32
 
  drizzle_return_t ret;
33
 
  int x;
34
 
  char **row;
35
 
 
36
 
  while ((c = getopt(argc, argv, "d:h:mp:q:v")) != -1)
37
 
  {
38
 
    switch(c)
39
 
    {
40
 
    case 'd':
41
 
      db= optarg;
42
 
      break;
43
 
 
44
 
    case 'h':
45
 
      host= optarg;
46
 
      break;
47
 
 
48
 
    case 'm':
49
 
      mysql= true;
50
 
      break;
51
 
 
52
 
    case 'p':
53
 
      port= (in_port_t)atoi(optarg);
54
 
      break;
55
 
 
56
 
    case 'q':
57
 
      query= optarg;
58
 
      break;
59
 
 
60
 
    case 'v':
61
 
      verbose++;
62
 
      break;
63
 
 
64
 
    default:
65
 
      printf("usage: %s [-d <db>] [-h <host>] [-m] [-p <port>] [-q <query>] "
66
 
             "[-v]\n", argv[0]);
67
 
      printf("\t-d <db>    - Database to use for query\n");
68
 
      printf("\t-h <host>  - Host to listen on\n");
69
 
      printf("\t-m         - Use the MySQL protocol\n");
70
 
      printf("\t-p <port>  - Port to listen on\n");
71
 
      printf("\t-q <query> - Query to run\n");
72
 
      printf("\t-v         - Increase verbosity level\n");
73
 
      return 1;
74
 
    }
75
 
  }
76
 
 
77
 
  if (drizzle_create(&drizzle) == NULL)
78
 
  {
79
 
    printf("drizzle_create:NULL\n");
80
 
    return 1;
81
 
  }
82
 
 
83
 
  drizzle_set_verbose(&drizzle, verbose);
84
 
 
85
 
  if (drizzle_con_create(&drizzle, con) == NULL)
86
 
  {
87
 
    printf("drizzle_con_create:NULL\n");
88
 
    return 1;
89
 
  }
90
 
 
91
 
  if (mysql)
92
 
    drizzle_con_add_options(con, DRIZZLE_CON_MYSQL);
93
 
 
94
 
  drizzle_con_set_tcp(con, host, port);
95
 
  drizzle_con_set_db(con, db);
96
 
 
97
 
  (void)drizzle_query_str(con, &result, query, &ret);
98
 
  if (ret != DRIZZLE_RETURN_OK)
99
 
  {
100
 
    printf("drizzle_query:%s\n", drizzle_con_error(con));
101
 
    return 1;
102
 
  }
103
 
 
104
 
  ret= drizzle_result_buffer(&result);
105
 
  if (ret != DRIZZLE_RETURN_OK)
106
 
  {
107
 
    printf("drizzle_result_buffer:%s\n", drizzle_con_error(con));
108
 
    return 1;
109
 
  }
110
 
 
111
 
  while ((row= (char **)drizzle_row_next(&result)) != NULL)
112
 
  {
113
 
    for (x= 0; x < drizzle_result_column_count(&result); x++)
114
 
      printf("%s%s", x == 0 ? "" : ":", row[x] == NULL ? "NULL" : row[x]);
115
 
    printf("\n");
116
 
  }
117
 
 
118
 
  drizzle_result_free(&result);
119
 
  drizzle_con_free(con);
120
 
  drizzle_free(&drizzle);
121
 
 
122
 
  free(con);
123
 
  return 0;
124
 
}