~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
2318.6.6 by Olaf van der Spek
EOLs
37
#include <cstdlib>
38
#include <cstring>
39
#include <getopt.h>
2367.2.2 by Olaf van der Spek
Use C++ API
40
#include <iostream>
2367.2.1 by Olaf van der Spek
Use C++ API
41
#include <libdrizzle/libdrizzle.hpp>
2318.6.6 by Olaf van der Spek
EOLs
42
#include <netdb.h>
43
#include <unistd.h>
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
44
2367.2.2 by Olaf van der Spek
Use C++ API
45
using namespace std;
46
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
47
int main(int argc, char *argv[])
48
{
2318.5.15 by Olaf van der Spek
Refactor
49
  const char* db= "information_schema";
50
  const char* host= NULL;
2367.2.4 by Olaf van der Spek
Add options for user and password
51
  const char* user= NULL;
52
  const char* password= NULL;
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
53
  bool mysql= false;
54
  in_port_t port= 0;
2318.5.15 by Olaf van der Spek
Refactor
55
  const char* query= "select table_schema, table_name from tables";
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
56
  drizzle_verbose_t verbose= DRIZZLE_VERBOSE_NEVER;
57
2367.2.4 by Olaf van der Spek
Add options for user and password
58
  for (int c; (c = getopt(argc, argv, "d:h:mp:u:P:q:v")) != -1; )
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
59
  {
2318.5.15 by Olaf van der Spek
Refactor
60
    switch (c)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
61
    {
62
    case 'd':
63
      db= optarg;
64
      break;
65
66
    case 'h':
67
      host= optarg;
68
      break;
69
70
    case 'm':
71
      mysql= true;
72
      break;
73
74
    case 'p':
2367.2.5 by Olaf van der Spek
Simplify verbose option
75
      port= static_cast<in_port_t>(atoi(optarg));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
76
      break;
77
2367.2.4 by Olaf van der Spek
Add options for user and password
78
    case 'u':
79
      user= optarg;
80
      break;
81
82
    case 'P':
83
      password = optarg;
84
      break;
85
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
86
    case 'q':
87
      query= optarg;
88
      break;
89
90
    case 'v':
2367.2.5 by Olaf van der Spek
Simplify verbose option
91
      if (verbose < DRIZZLE_VERBOSE_MAX)
92
        verbose= static_cast<drizzle_verbose_t>(verbose + 1);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
93
      break;
94
95
    default:
2367.2.2 by Olaf van der Spek
Use C++ API
96
      cout << 
97
        "usage: " << argv[0] << " [-d <db>] [-h <host>] [-m] [-p <port>] [-q <query>] [-v]\n"
98
        "\t-d <db>    - Database to use for query\n"
99
        "\t-h <host>  - Host to connect to\n"
100
        "\t-m         - Use the MySQL protocol\n"
101
        "\t-p <port>  - Port to connect to\n"
2367.2.4 by Olaf van der Spek
Add options for user and password
102
        "\t-u <user>  - User\n"
103
        "\t-P <pass>  - Password\n"
2367.2.2 by Olaf van der Spek
Use C++ API
104
        "\t-q <query> - Query to run\n"
105
        "\t-v         - Increase verbosity level\n";
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
106
      return 1;
107
    }
108
  }
109
2367.2.1 by Olaf van der Spek
Use C++ API
110
  drizzle::drizzle_c drizzle;
111
  drizzle_set_verbose(&drizzle.b_, verbose);
112
  drizzle::connection_c* con= new drizzle::connection_c(drizzle);
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
113
  if (mysql)
2367.2.1 by Olaf van der Spek
Use C++ API
114
    drizzle_con_add_options(&con->b_, DRIZZLE_CON_MYSQL);
2367.2.3 by Olaf van der Spek
Use C++ API
115
  con->set_tcp(host, port);
2367.2.4 by Olaf van der Spek
Add options for user and password
116
  con->set_auth(user, password);
2367.2.3 by Olaf van der Spek
Use C++ API
117
  con->set_db(db);
2367.2.2 by Olaf van der Spek
Use C++ API
118
  drizzle::result_c result;
119
  if (con->query(result, query))
120
  {
2367.2.4 by Olaf van der Spek
Add options for user and password
121
    cerr << "query: " << con->error() << endl;
2367.2.2 by Olaf van der Spek
Use C++ API
122
    return 1;
123
  }
124
  while (drizzle_row_t row= result.row_next())
125
  {
126
    for (int x= 0; x < result.column_count(); x++)
127
    {
128
      if (x)
129
        cout << ", ";
130
      cout << (row[x] ? row[x] : "NULL");
131
    }
132
    cout << endl;
133
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
134
  return 0;
135
}