~patrick-crews/drizzle/dbqp_server_setup

« back to all changes in this revision

Viewing changes to libdrizzle-2.0/libdrizzle/query.cc

  • Committer: Monty Taylor
  • Date: 2011-03-22 18:39:54 UTC
  • mto: (2246.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2247.
  • Revision ID: mordred@inaugust.com-20110322183954-fz8ciuywjz2llbyo
Split libdrizzle into 1.0 and 2.0. Applied the C++ changes to 2.0 branch.

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
 * 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
 *
 
35
 */
 
36
 
 
37
/**
 
38
 * @file
 
39
 * @brief Query definitions
 
40
 */
 
41
 
 
42
#include "common.h"
 
43
 
 
44
drizzle_result_st *drizzle_query(drizzle_con_st *con, drizzle_result_st *result,
 
45
                                 const char *query, size_t size,
 
46
                                 drizzle_return_t *ret_ptr)
 
47
{
 
48
  return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY,
 
49
                                   (uint8_t *)query, size, size, ret_ptr);
 
50
}
 
51
 
 
52
drizzle_result_st *drizzle_query_str(drizzle_con_st *con,
 
53
                                     drizzle_result_st *result,
 
54
                                     const char *query, 
 
55
                                     drizzle_return_t *ret_ptr)
 
56
{
 
57
  size_t size;
 
58
 
 
59
  size= strlen(query);
 
60
 
 
61
  return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY,
 
62
                                   (uint8_t *)query, size, size, ret_ptr);
 
63
}
 
64
 
 
65
drizzle_result_st *drizzle_query_inc(drizzle_con_st *con,
 
66
                                     drizzle_result_st *result,
 
67
                                     const char *query, size_t size,
 
68
                                     size_t total, drizzle_return_t *ret_ptr)
 
69
{
 
70
  return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY,
 
71
                                   (uint8_t *)query, size, total, ret_ptr);
 
72
}
 
73
 
 
74
drizzle_query_st *drizzle_query_add(drizzle_st *drizzle,
 
75
                                    drizzle_query_st *query,
 
76
                                    drizzle_con_st *con,
 
77
                                    drizzle_result_st *result,
 
78
                                    const char *query_string, size_t size,
 
79
                                    drizzle_query_options_t options,
 
80
                                    void *context)
 
81
{
 
82
  query= drizzle_query_create(drizzle, query);
 
83
  if (query == NULL)
 
84
    return NULL;
 
85
 
 
86
  drizzle_query_set_con(query, con);
 
87
  drizzle_query_set_result(query, result);
 
88
  drizzle_query_set_string(query, query_string, size);
 
89
  drizzle_query_add_options(query, options);
 
90
  drizzle_query_set_context(query, context);
 
91
 
 
92
  return query;
 
93
}
 
94
 
 
95
drizzle_query_st *drizzle_query_create(drizzle_st *drizzle,
 
96
                                       drizzle_query_st *query)
 
97
{
 
98
  if (query == NULL)
 
99
  {
 
100
    query= new drizzle_query_st;
 
101
    if (query == NULL)
 
102
    {
 
103
      drizzle_set_error(drizzle, "drizzle_query_create", "malloc");
 
104
      return NULL;
 
105
    }
 
106
 
 
107
    query->options|= DRIZZLE_CON_ALLOCATED;
 
108
  }
 
109
  else
 
110
  {
 
111
    query->prev= NULL;
 
112
    query->options= 0;
 
113
    query->state= DRIZZLE_QUERY_STATE_INIT;
 
114
    query->con= NULL;
 
115
    query->result= NULL;
 
116
    query->string= NULL;
 
117
    query->size= 0;
 
118
    query->context= NULL;
 
119
    query->context_free_fn= NULL;
 
120
  }
 
121
 
 
122
  query->drizzle= drizzle;
 
123
 
 
124
  if (drizzle->query_list)
 
125
    drizzle->query_list->prev= query;
 
126
  query->next= drizzle->query_list;
 
127
  drizzle->query_list= query;
 
128
  drizzle->query_count++;
 
129
  drizzle->query_new++;
 
130
 
 
131
  return query;
 
132
}
 
133
 
 
134
void drizzle_query_free(drizzle_query_st *query)
 
135
{
 
136
  if (query->context != NULL && query->context_free_fn != NULL)
 
137
    query->context_free_fn(query, query->context);
 
138
 
 
139
  if (query->drizzle->query_list == query)
 
140
    query->drizzle->query_list= query->next;
 
141
  if (query->prev)
 
142
    query->prev->next= query->next;
 
143
  if (query->next)
 
144
    query->next->prev= query->prev;
 
145
  query->drizzle->query_count--;
 
146
 
 
147
  if (query->options & DRIZZLE_QUERY_ALLOCATED)
 
148
    delete query;
 
149
}
 
150
 
 
151
void drizzle_query_free_all(drizzle_st *drizzle)
 
152
{
 
153
  while (drizzle->query_list != NULL)
 
154
    drizzle_query_free(drizzle->query_list);
 
155
}
 
156
 
 
157
drizzle_con_st *drizzle_query_con(drizzle_query_st *query)
 
158
{
 
159
  return query->con;
 
160
}
 
161
 
 
162
void drizzle_query_set_con(drizzle_query_st *query, drizzle_con_st *con)
 
163
{
 
164
  query->con= con;
 
165
}
 
166
 
 
167
drizzle_result_st *drizzle_query_result(drizzle_query_st *query)
 
168
{
 
169
  return query->result;
 
170
}
 
171
 
 
172
void drizzle_query_set_result(drizzle_query_st *query,
 
173
                              drizzle_result_st *result)
 
174
{
 
175
  query->result= result;
 
176
}
 
177
 
 
178
char *drizzle_query_string(drizzle_query_st *query, size_t *size)
 
179
{
 
180
  *size= query->size;
 
181
  return (char *)(query->string);
 
182
}
 
183
 
 
184
void drizzle_query_set_string(drizzle_query_st *query, const char *string,
 
185
                              size_t size)
 
186
{
 
187
  query->string= string;
 
188
  query->size= size;
 
189
}
 
190
 
 
191
int drizzle_query_options(drizzle_query_st *query)
 
192
{
 
193
  return query->options;
 
194
}
 
195
 
 
196
void drizzle_query_set_options(drizzle_query_st *query,
 
197
                               int options)
 
198
{
 
199
  query->options= options;
 
200
}
 
201
 
 
202
void drizzle_query_add_options(drizzle_query_st *query,
 
203
                               int options)
 
204
{
 
205
  query->options|= options;
 
206
}
 
207
 
 
208
void drizzle_query_remove_options(drizzle_query_st *query,
 
209
                                  int options)
 
210
{
 
211
  query->options&= ~options;
 
212
}
 
213
 
 
214
void *drizzle_query_context(drizzle_query_st *query)
 
215
{
 
216
  return query->context;
 
217
}
 
218
 
 
219
void drizzle_query_set_context(drizzle_query_st *query, void *context)
 
220
{
 
221
  query->context= context;
 
222
}
 
223
 
 
224
void drizzle_query_set_context_free_fn(drizzle_query_st *query,
 
225
                                       drizzle_query_context_free_fn *function)
 
226
{
 
227
  query->context_free_fn= function;
 
228
}
 
229
 
 
230
static void drizzle_query_run_state(drizzle_query_st* query,
 
231
                                    drizzle_return_t* ret_ptr)
 
232
{
 
233
  switch (query->state)
 
234
  {
 
235
  case DRIZZLE_QUERY_STATE_INIT:
 
236
    query->state= DRIZZLE_QUERY_STATE_QUERY;
 
237
  case DRIZZLE_QUERY_STATE_QUERY:
 
238
    query->result= drizzle_query(query->con, query->result, query->string,
 
239
                                 query->size, ret_ptr);
 
240
    if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
 
241
    {
 
242
      return;
 
243
    }
 
244
    else if (*ret_ptr != DRIZZLE_RETURN_OK)
 
245
    {
 
246
      query->state= DRIZZLE_QUERY_STATE_DONE;
 
247
      return;
 
248
    }
 
249
 
 
250
    query->state= DRIZZLE_QUERY_STATE_RESULT;
 
251
 
 
252
  case DRIZZLE_QUERY_STATE_RESULT:
 
253
    *ret_ptr= drizzle_result_buffer(query->result);
 
254
    if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
 
255
    {
 
256
      return;
 
257
    }
 
258
 
 
259
    query->state= DRIZZLE_QUERY_STATE_DONE;
 
260
    return;
 
261
 
 
262
  default:
 
263
  case DRIZZLE_QUERY_STATE_DONE:
 
264
    return;
 
265
  }
 
266
}
 
267
 
 
268
drizzle_query_st *drizzle_query_run(drizzle_st *drizzle,
 
269
                                    drizzle_return_t *ret_ptr)
 
270
{
 
271
  int options;
 
272
  drizzle_query_st *query;
 
273
  drizzle_con_st *con;
 
274
 
 
275
  if (drizzle->query_new == 0 && drizzle->query_running == 0)
 
276
  {
 
277
    *ret_ptr= DRIZZLE_RETURN_OK;
 
278
    return NULL;
 
279
  }
 
280
 
 
281
  options= drizzle->options;
 
282
  drizzle->options|= DRIZZLE_NON_BLOCKING;
 
283
 
 
284
  /* Check to see if any queries need to be started. */
 
285
  if (drizzle->query_new > 0)
 
286
  {
 
287
    for (query= drizzle->query_list; query != NULL; query= query->next)
 
288
    {
 
289
      if (query->state != DRIZZLE_QUERY_STATE_INIT)
 
290
        continue;
 
291
 
 
292
      drizzle->query_new--;
 
293
      drizzle->query_running++;
 
294
      assert(query->con->query == NULL);
 
295
      query->con->query= query;
 
296
 
 
297
      drizzle_query_run_state(query, ret_ptr);
 
298
      if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
 
299
      {
 
300
        assert(query->state == DRIZZLE_QUERY_STATE_DONE);
 
301
        drizzle->query_running--;
 
302
        drizzle->options= options;
 
303
        query->con->query= NULL;
 
304
        if (*ret_ptr == DRIZZLE_RETURN_ERROR_CODE || *ret_ptr == DRIZZLE_RETURN_OK)
 
305
        {
 
306
          return query;
 
307
        }
 
308
        return NULL;
 
309
      }
 
310
    }
 
311
    assert(drizzle->query_new == 0);
 
312
  }
 
313
 
 
314
  while (1)
 
315
  {
 
316
    /* Loop through each active connection. */
 
317
    while ((con= drizzle_con_ready(drizzle)) != NULL)
 
318
    {
 
319
      query= con->query;
 
320
      drizzle_query_run_state(query, ret_ptr);
 
321
      if (query->state == DRIZZLE_QUERY_STATE_DONE)
 
322
      {
 
323
        drizzle->query_running--;
 
324
        drizzle->options= options;
 
325
        con->query= NULL;
 
326
        return query;
 
327
      }
 
328
      assert(*ret_ptr == DRIZZLE_RETURN_IO_WAIT);
 
329
    }
 
330
 
 
331
    if (options & DRIZZLE_NON_BLOCKING)
 
332
    {
 
333
      *ret_ptr= DRIZZLE_RETURN_IO_WAIT;
 
334
      return NULL;
 
335
    }
 
336
 
 
337
    *ret_ptr= drizzle_con_wait(drizzle);
 
338
    if (*ret_ptr != DRIZZLE_RETURN_OK)
 
339
    {
 
340
      drizzle->options= options;
 
341
      return NULL;
 
342
    }
 
343
  }
 
344
}
 
345
 
 
346
drizzle_return_t drizzle_query_run_all(drizzle_st *drizzle)
 
347
{
 
348
  drizzle_return_t ret;
 
349
 
 
350
  while (drizzle->query_new > 0 || drizzle->query_running > 0)
 
351
  {
 
352
    (void)drizzle_query_run(drizzle, &ret);
 
353
    if (ret != DRIZZLE_RETURN_OK && ret != DRIZZLE_RETURN_ERROR_CODE)
 
354
      return ret;
 
355
  }
 
356
 
 
357
  return DRIZZLE_RETURN_OK;
 
358
}
 
359
 
 
360
ssize_t drizzle_safe_escape_string(char *to, size_t max_to_size, const char *from, size_t from_size)
 
361
{
 
362
  ssize_t to_size= 0;
 
363
  char newchar;
 
364
  const char *end;
 
365
 
 
366
  for (end= from + from_size; from < end; from++)
 
367
  {
 
368
    newchar= 0;
 
369
    /* All multi-byte UTF8 characters have the high bit set for all bytes. */
 
370
    if (!(*from & 0x80))
 
371
    {
 
372
      switch (*from)
 
373
      {
 
374
      case 0:
 
375
        newchar= '0';
 
376
        break;
 
377
      case '\n':
 
378
        newchar= 'n';
 
379
        break;
 
380
      case '\r':
 
381
        newchar= 'r';
 
382
        break;
 
383
      case '\032':
 
384
        newchar= 'Z';
 
385
        break;
 
386
      case '\\':
 
387
        newchar= '\\';
 
388
        break;
 
389
      case '\'':
 
390
        newchar= '\'';
 
391
        break;
 
392
      case '"':
 
393
        newchar= '"';
 
394
        break;
 
395
      default:
 
396
        break;
 
397
      }
 
398
    }
 
399
    if (newchar != '\0')
 
400
    {
 
401
      if ((size_t)to_size + 2 > max_to_size)
 
402
        return -1;
 
403
 
 
404
      *to++= '\\';
 
405
      *to++= newchar;
 
406
      to_size++;
 
407
    }
 
408
    else
 
409
    {
 
410
      if ((size_t)to_size + 1 > max_to_size)
 
411
        return -1;
 
412
 
 
413
      *to++= *from;
 
414
    }
 
415
    to_size++;
 
416
  }
 
417
 
 
418
  *to= 0;
 
419
 
 
420
  return to_size;
 
421
}
 
422
 
 
423
size_t drizzle_escape_string(char *to, const char *from, size_t from_size)
 
424
{
 
425
  return (size_t) drizzle_safe_escape_string(to, (from_size * 2), from, from_size);
 
426
}
 
427
 
 
428
size_t drizzle_hex_string(char *to, const char *from, size_t from_size)
 
429
{
 
430
  static const char hex_map[]= "0123456789ABCDEF";
 
431
  const char *from_end;
 
432
 
 
433
  for (from_end= from + from_size; from != from_end; from++)
 
434
  {
 
435
    *to++= hex_map[((unsigned char) *from) >> 4];
 
436
    *to++= hex_map[((unsigned char) *from) & 0xF];
 
437
  }
 
438
 
 
439
  *to= 0;
 
440
 
 
441
  return from_size * 2;
 
442
}
 
443
 
 
444
void drizzle_mysql_password_hash(char *to, const char *from, size_t from_size)
 
445
{
 
446
  SHA1_CTX ctx;
 
447
  uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
 
448
  uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];
 
449
 
 
450
  SHA1Init(&ctx);
 
451
  SHA1Update(&ctx, (const uint8_t*)from, from_size);
 
452
  SHA1Final(hash_tmp1, &ctx);
 
453
 
 
454
  SHA1Init(&ctx);
 
455
  SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
 
456
  SHA1Final(hash_tmp2, &ctx);
 
457
 
 
458
  (void)drizzle_hex_string(to, (char*)hash_tmp2, SHA1_DIGEST_LENGTH);
 
459
}