~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle-2.0/libdrizzle/libdrizzle.hpp

  • Committer: Mark Atwood
  • Date: 2011-08-11 03:05:03 UTC
  • mfrom: (2385.1.12 refactor4)
  • Revision ID: me@mark.atwood.name-20110811030503-rp9xjihc5x3y0x4q
mergeĀ lp:~olafvdspek/drizzle/refactor4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Drizzle Client Library
 
2
 * Copyright (C) 2011 Olaf van der Spek
 
3
 * All rights reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions are
 
7
 * met:
 
8
 *
 
9
 *     * Redistributions of source code must retain the above copyright
 
10
 * notice, this list of conditions and the following disclaimer.
 
11
 *
 
12
 *     * Redistributions in binary form must reproduce the above
 
13
 * copyright notice, this list of conditions and the following disclaimer
 
14
 * in the documentation and/or other materials provided with the
 
15
 * distribution.
 
16
 *
 
17
 *     * The names of its contributors may not be used to endorse or
 
18
 * promote products derived from this software without specific prior
 
19
 * written permission.
 
20
 *
 
21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
22
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
23
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
24
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
25
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
27
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
28
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
29
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
30
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
31
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
32
 */
 
33
 
 
34
#pragma once
 
35
 
 
36
#include <cstring>
 
37
#include <libdrizzle/libdrizzle.h>
 
38
#include <stdexcept>
 
39
 
 
40
namespace drizzle {
 
41
 
 
42
class bad_query : public std::runtime_error
 
43
{
 
44
};
 
45
 
 
46
class drizzle_c
 
47
{
 
48
public:
 
49
  drizzle_c()
 
50
  {
 
51
    drizzle_create(&b_);
 
52
  }
 
53
 
 
54
  ~drizzle_c()
 
55
  {
 
56
    drizzle_free(&b_);
 
57
  }
 
58
 
 
59
  drizzle_st b_;
 
60
};
 
61
 
 
62
class result_c
 
63
{
 
64
public:
 
65
  result_c()
 
66
  {
 
67
    memset(&b_, 0, sizeof(b_));
 
68
  }
 
69
 
 
70
  ~result_c()
 
71
  {
 
72
    drizzle_result_free(&b_);
 
73
  }
 
74
 
 
75
  const char* error()
 
76
  {
 
77
    return drizzle_result_error(&b_);
 
78
  }
 
79
 
 
80
  uint16_t error_code()
 
81
  {
 
82
    return drizzle_result_error_code(&b_);
 
83
  }
 
84
 
 
85
  uint16_t column_count()
 
86
  {
 
87
    return drizzle_result_column_count(&b_);    
 
88
  }
 
89
 
 
90
  uint64_t row_count()
 
91
  {
 
92
    return drizzle_result_row_count(&b_);
 
93
  }
 
94
 
 
95
  drizzle_column_st* column_next()
 
96
  {
 
97
    return drizzle_column_next(&b_);
 
98
  }
 
99
 
 
100
  drizzle_row_t row_next()
 
101
  {
 
102
    return drizzle_row_next(&b_);
 
103
  }
 
104
 
 
105
  void column_seek(uint16_t i)
 
106
  {
 
107
    drizzle_column_seek(&b_, i);
 
108
  }
 
109
 
 
110
  void row_seek(uint64_t i)
 
111
  {
 
112
    drizzle_row_seek(&b_, i);
 
113
  }
 
114
 
 
115
  size_t* row_field_sizes()
 
116
  {
 
117
    return drizzle_row_field_sizes(&b_);
 
118
  }
 
119
 
 
120
  drizzle_result_st b_;
 
121
};
 
122
 
 
123
class connection_c
 
124
{
 
125
public:
 
126
  explicit connection_c(drizzle_c& drizzle)
 
127
  {
 
128
    drizzle_con_create(&drizzle.b_, &b_);
 
129
  }
 
130
 
 
131
  ~connection_c()
 
132
  {
 
133
    drizzle_con_free(&b_);
 
134
  }
 
135
 
 
136
  const char* error()
 
137
  {
 
138
    return drizzle_con_error(&b_);
 
139
  }
 
140
 
 
141
  void set_tcp(const char* host, in_port_t port)
 
142
  {
 
143
    drizzle_con_set_tcp(&b_, host, port);
 
144
  }
 
145
 
 
146
  void set_auth(const char* user, const char* password)
 
147
  {
 
148
    drizzle_con_set_auth(&b_, user, password);
 
149
  }
 
150
 
 
151
  void set_db(const char* db)
 
152
  {
 
153
    drizzle_con_set_db(&b_, db);
 
154
  }
 
155
 
 
156
  drizzle_return_t query(result_c& result, const char* str, size_t str_size)
 
157
  {
 
158
    drizzle_return_t ret;
 
159
    drizzle_query(&b_, &result.b_, str, str_size, &ret);
 
160
    if (ret == DRIZZLE_RETURN_OK)
 
161
      ret = drizzle_result_buffer(&result.b_);
 
162
    return ret;
 
163
  }
 
164
 
 
165
  drizzle_return_t query(result_c& result, const std::string& str)
 
166
  {
 
167
    return query(result, str.data(), str.size());
 
168
  }
 
169
 
 
170
  drizzle_return_t query(result_c& result, const char* str)
 
171
  {
 
172
    return query(result, str, strlen(str));
 
173
  }
 
174
 
 
175
  drizzle_con_st b_;
 
176
};
 
177
 
 
178
/*
 
179
inline drizzle_return_t query(drizzle_con_st* con, result_c& result, const char* str, size_t str_size)
 
180
{
 
181
  drizzle_return_t ret;
 
182
  drizzle_query(con, &result.b_, str, str_size, &ret);
 
183
  if (ret == DRIZZLE_RETURN_OK)
 
184
    ret = drizzle_result_buffer(&result.b_);
 
185
  return ret;
 
186
}
 
187
 
 
188
inline drizzle_return_t query(drizzle_con_st* con, result_c& result, const std::string& str)
 
189
{
 
190
  return query(con, result, str.data(), str.size());
 
191
}
 
192
 
 
193
inline drizzle_return_t query(drizzle_con_st* con, result_c& result, const char* str)
 
194
{
 
195
  return query(con, result, str, strlen(str));
 
196
}
 
197
*/
 
198
 
 
199
}