~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>
2449.1.2 by Brian Aker
Additional fixes for libdrizzle.
41
#include <libdrizzle-2.0/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* host= NULL;
2367.2.4 by Olaf van der Spek
Add options for user and password
50
  const char* user= NULL;
51
  const char* password= NULL;
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
52
  in_port_t port= 0;
53
2367.2.4 by Olaf van der Spek
Add options for user and password
54
  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.
55
  {
2318.5.15 by Olaf van der Spek
Refactor
56
    switch (c)
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
57
    {
58
    case 'h':
59
      host= optarg;
60
      break;
61
62
    case 'p':
2367.2.5 by Olaf van der Spek
Simplify verbose option
63
      port= static_cast<in_port_t>(atoi(optarg));
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
64
      break;
65
2367.2.4 by Olaf van der Spek
Add options for user and password
66
    case 'u':
67
      user= optarg;
68
      break;
69
70
    case 'P':
71
      password = optarg;
72
      break;
73
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
74
    default:
2367.2.2 by Olaf van der Spek
Use C++ API
75
      cout << 
2385.3.2 by Olaf van der Spek
Add drizzle::query_c
76
        "usage:\n"
2367.2.2 by Olaf van der Spek
Use C++ API
77
        "\t-h <host>  - Host to connect to\n"
78
        "\t-p <port>  - Port to connect to\n"
2367.2.4 by Olaf van der Spek
Add options for user and password
79
        "\t-u <user>  - User\n"
2385.3.3 by Olaf van der Spek
Remove mysql and verbose options
80
        "\t-P <pass>  - Password\n";
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
81
      return 1;
82
    }
83
  }
84
2367.2.1 by Olaf van der Spek
Use C++ API
85
  drizzle::drizzle_c drizzle;
86
  drizzle::connection_c* con= new drizzle::connection_c(drizzle);
2407.1.4 by Olaf van der Spek
Read conf files
87
  if (host || port)
88
    con->set_tcp(host, port);
89
  if (user || password)
90
    con->set_auth(user, password);
2385.3.4 by Olaf van der Spek
drizzle::noncopyable
91
  con->set_db("information_schema");
2385.3.2 by Olaf van der Spek
Add drizzle::query_c
92
  drizzle::query_c q(*con, "select table_schema, table_name from tables where table_name like ?");
93
  q.p("%");
2407.1.3 by Olaf van der Spek
Add throwing variants of query()
94
  try
95
  {
96
    drizzle::result_c result= q.execute();
2407.1.4 by Olaf van der Spek
Read conf files
97
    cout << q.read() << endl;
2407.1.3 by Olaf van der Spek
Add throwing variants of query()
98
    while (drizzle_row_t row= result.row_next())
99
    {
100
      for (int x= 0; x < result.column_count(); x++)
101
      {
102
        if (x)
103
          cout << ", ";
104
        cout << (row[x] ? row[x] : "NULL");
105
      }
106
      cout << endl;
107
    }
108
  }
109
  catch (const drizzle::bad_query& e)
110
  {
111
    cerr << e.what() << endl;
2367.2.2 by Olaf van der Spek
Use C++ API
112
    return 1;
113
  }
1712.1.1 by Monty Taylor
Merged libdrizzle directly into tree.
114
  return 0;
115
}