~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to libdrizzle-2.0/query.cc

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

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 <libdrizzle-2.0/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= strlen(query);
 
58
 
 
59
  return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY, (uint8_t *)query, size, size, ret_ptr);
 
60
}
 
61
 
 
62
drizzle_result_st *drizzle_query_inc(drizzle_con_st *con,
 
63
                                     drizzle_result_st *result,
 
64
                                     const char *query, size_t size,
 
65
                                     size_t total, drizzle_return_t *ret_ptr)
 
66
{
 
67
  return drizzle_con_command_write(con, result, DRIZZLE_COMMAND_QUERY, (uint8_t *)query, size, total, ret_ptr);
 
68
}
 
69
 
 
70
drizzle_query_st *drizzle_query_add(drizzle_st *drizzle,
 
71
                                    drizzle_query_st *query,
 
72
                                    drizzle_con_st *con,
 
73
                                    drizzle_result_st *result,
 
74
                                    const char *query_string, size_t size,
 
75
                                    drizzle_query_options_t,
 
76
                                    void *context)
 
77
{
 
78
  // @note drizzle_query_st handle the null drizzle case
 
79
  query= drizzle_query_create(drizzle, query);
 
80
  if (query == NULL)
 
81
  {
 
82
    return NULL;
 
83
  }
 
84
 
 
85
  drizzle_query_set_con(query, con);
 
86
  drizzle_query_set_result(query, result);
 
87
  drizzle_query_set_string(query, query_string, size);
 
88
  drizzle_query_set_context(query, context);
 
89
 
 
90
  return query;
 
91
}
 
92
 
 
93
drizzle_query_st *drizzle_query_create(drizzle_st *drizzle, drizzle_query_st *query)
 
94
{
 
95
  if (drizzle == NULL)
 
96
  {
 
97
    return NULL;
 
98
  }
 
99
 
 
100
  if (query == NULL)
 
101
  {
 
102
    query= new (std::nothrow) drizzle_query_st;
 
103
 
 
104
    if (query == NULL)
 
105
    {
 
106
      return NULL;
 
107
    }
 
108
    query->options.is_allocated= true;
 
109
  }
 
110
  else
 
111
  {
 
112
    query->prev= NULL;
 
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
    query->options.is_allocated= false;
 
121
  }
 
122
 
 
123
  query->drizzle= drizzle;
 
124
 
 
125
  if (drizzle->query_list)
 
126
  {
 
127
    drizzle->query_list->prev= query;
 
128
  }
 
129
  query->next= drizzle->query_list;
 
130
  drizzle->query_list= query;
 
131
  drizzle->query_count++;
 
132
  drizzle->query_new++;
 
133
 
 
134
  return query;
 
135
}
 
136
 
 
137
void drizzle_query_free(drizzle_query_st *query)
 
138
{
 
139
  if (query == NULL)
 
140
  {
 
141
    return;
 
142
  }
 
143
 
 
144
  if (query->context != NULL && query->context_free_fn != NULL)
 
145
  {
 
146
    query->context_free_fn(query, query->context);
 
147
  }
 
148
 
 
149
  if (query->drizzle->query_list == query)
 
150
  {
 
151
    query->drizzle->query_list= query->next;
 
152
  }
 
153
 
 
154
  if (query->prev)
 
155
  {
 
156
    query->prev->next= query->next;
 
157
  }
 
158
 
 
159
  if (query->next)
 
160
  {
 
161
    query->next->prev= query->prev;
 
162
  }
 
163
 
 
164
  query->drizzle->query_count--;
 
165
 
 
166
  if (query->options.is_allocated)
 
167
  {
 
168
    delete query;
 
169
  }
 
170
}
 
171
 
 
172
void drizzle_query_free_all(drizzle_st *drizzle)
 
173
{
 
174
  while (drizzle->query_list != NULL)
 
175
  {
 
176
    drizzle_query_free(drizzle->query_list);
 
177
  }
 
178
}
 
179
 
 
180
drizzle_con_st *drizzle_query_con(drizzle_query_st *query)
 
181
{
 
182
  if (query == NULL)
 
183
  {
 
184
    return NULL;
 
185
  }
 
186
 
 
187
  return query->con;
 
188
}
 
189
 
 
190
void drizzle_query_set_con(drizzle_query_st *query, drizzle_con_st *con)
 
191
{
 
192
  if (query == NULL)
 
193
  {
 
194
    return;
 
195
  }
 
196
 
 
197
  query->con= con;
 
198
}
 
199
 
 
200
drizzle_result_st *drizzle_query_result(drizzle_query_st *query)
 
201
{
 
202
  if (query == NULL)
 
203
  {
 
204
    return NULL;
 
205
  }
 
206
 
 
207
  return query->result;
 
208
}
 
209
 
 
210
void drizzle_query_set_result(drizzle_query_st *query,
 
211
                              drizzle_result_st *result)
 
212
{
 
213
  if (query == NULL)
 
214
  {
 
215
    return;
 
216
  }
 
217
 
 
218
  query->result= result;
 
219
}
 
220
 
 
221
char *drizzle_query_string(drizzle_query_st *query, size_t *size)
 
222
{
 
223
  if (query == NULL)
 
224
  {
 
225
    return NULL;
 
226
  }
 
227
 
 
228
  *size= query->size;
 
229
  return (char *)(query->string);
 
230
}
 
231
 
 
232
void drizzle_query_set_string(drizzle_query_st *query, const char *string,
 
233
                              size_t size)
 
234
{
 
235
  if (query == NULL)
 
236
  {
 
237
    return;
 
238
  }
 
239
 
 
240
  query->string= string;
 
241
  query->size= size;
 
242
}
 
243
 
 
244
int drizzle_query_options(drizzle_query_st *)
 
245
{
 
246
  return 0;
 
247
}
 
248
 
 
249
void drizzle_query_set_options(drizzle_query_st *, int)
 
250
{
 
251
}
 
252
 
 
253
void drizzle_query_add_options(drizzle_query_st *, int)
 
254
{
 
255
}
 
256
 
 
257
void drizzle_query_remove_options(drizzle_query_st *, int)
 
258
{
 
259
}
 
260
 
 
261
void *drizzle_query_context(drizzle_query_st *query)
 
262
{
 
263
  if (query == NULL)
 
264
  {
 
265
    return NULL;
 
266
  }
 
267
 
 
268
  return query->context;
 
269
}
 
270
 
 
271
void drizzle_query_set_context(drizzle_query_st *query, void *context)
 
272
{
 
273
  if (query == NULL)
 
274
  {
 
275
    return;
 
276
  }
 
277
 
 
278
  query->context= context;
 
279
}
 
280
 
 
281
void drizzle_query_set_context_free_fn(drizzle_query_st *query,
 
282
                                       drizzle_query_context_free_fn *function)
 
283
{
 
284
  if (query == NULL)
 
285
  {
 
286
    return;
 
287
  }
 
288
 
 
289
  query->context_free_fn= function;
 
290
}
 
291
 
 
292
static void drizzle_query_run_state(drizzle_query_st* query,
 
293
                                    drizzle_return_t* ret_ptr)
 
294
{
 
295
  if (query == NULL)
 
296
  {
 
297
    return;
 
298
  }
 
299
 
 
300
  switch (query->state)
 
301
  {
 
302
  case DRIZZLE_QUERY_STATE_INIT:
 
303
    query->state= DRIZZLE_QUERY_STATE_QUERY;
 
304
 
 
305
  case DRIZZLE_QUERY_STATE_QUERY:
 
306
    query->result= drizzle_query(query->con, query->result, query->string,
 
307
                                 query->size, ret_ptr);
 
308
    if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
 
309
    {
 
310
      return;
 
311
    }
 
312
    else if (*ret_ptr != DRIZZLE_RETURN_OK)
 
313
    {
 
314
      query->state= DRIZZLE_QUERY_STATE_DONE;
 
315
      return;
 
316
    }
 
317
 
 
318
    query->state= DRIZZLE_QUERY_STATE_RESULT;
 
319
 
 
320
  case DRIZZLE_QUERY_STATE_RESULT:
 
321
    *ret_ptr= drizzle_result_buffer(query->result);
 
322
    if (*ret_ptr == DRIZZLE_RETURN_IO_WAIT)
 
323
    {
 
324
      return;
 
325
    }
 
326
 
 
327
    query->state= DRIZZLE_QUERY_STATE_DONE;
 
328
    return;
 
329
 
 
330
  default:
 
331
  case DRIZZLE_QUERY_STATE_DONE:
 
332
    return;
 
333
  }
 
334
}
 
335
 
 
336
drizzle_query_st *drizzle_query_run(drizzle_st *drizzle,
 
337
                                    drizzle_return_t *ret_ptr)
 
338
{
 
339
  drizzle_return_t unused;
 
340
  if (ret_ptr == NULL)
 
341
  {
 
342
    ret_ptr= &unused;
 
343
  }
 
344
 
 
345
  if (drizzle == NULL)
 
346
  {
 
347
    *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
 
348
    return NULL;
 
349
  }
 
350
 
 
351
  if (drizzle->query_new == 0 && drizzle->query_running == 0)
 
352
  {
 
353
    *ret_ptr= DRIZZLE_RETURN_OK;
 
354
    return NULL;
 
355
  }
 
356
 
 
357
  drizzle_st::options_t options= drizzle->options;
 
358
  drizzle->options.is_non_blocking= false;
 
359
 
 
360
  /* Check to see if any queries need to be started. */
 
361
  if (drizzle->query_new > 0)
 
362
  {
 
363
    for (drizzle_query_st *query= drizzle->query_list; query != NULL; query= query->next)
 
364
    {
 
365
      if (query->state != DRIZZLE_QUERY_STATE_INIT)
 
366
      {
 
367
        continue;
 
368
      }
 
369
 
 
370
      drizzle->query_new--;
 
371
      drizzle->query_running++;
 
372
      assert(query->con->query == NULL);
 
373
      query->con->query= query;
 
374
 
 
375
      drizzle_query_run_state(query, ret_ptr);
 
376
      if (*ret_ptr != DRIZZLE_RETURN_IO_WAIT)
 
377
      {
 
378
        assert(query->state == DRIZZLE_QUERY_STATE_DONE);
 
379
        drizzle->query_running--;
 
380
        drizzle->options= options;
 
381
        query->con->query= NULL;
 
382
        if (*ret_ptr == DRIZZLE_RETURN_ERROR_CODE || *ret_ptr == DRIZZLE_RETURN_OK)
 
383
        {
 
384
          return query;
 
385
        }
 
386
        return NULL;
 
387
      }
 
388
    }
 
389
    assert(drizzle->query_new == 0);
 
390
  }
 
391
 
 
392
  while (1)
 
393
  {
 
394
    drizzle_con_st *con;
 
395
 
 
396
    /* Loop through each active connection. */
 
397
    while ((con= drizzle_con_ready(drizzle)) != NULL)
 
398
    {
 
399
      drizzle_query_st *query= con->query;
 
400
      drizzle_query_run_state(query, ret_ptr);
 
401
      if (query->state == DRIZZLE_QUERY_STATE_DONE)
 
402
      {
 
403
        drizzle->query_running--;
 
404
        drizzle->options= options;
 
405
        con->query= NULL;
 
406
        return query;
 
407
      }
 
408
      assert(*ret_ptr == DRIZZLE_RETURN_IO_WAIT);
 
409
    }
 
410
 
 
411
    if (options.is_non_blocking)
 
412
    {
 
413
      *ret_ptr= DRIZZLE_RETURN_IO_WAIT;
 
414
      return NULL;
 
415
    }
 
416
 
 
417
    *ret_ptr= drizzle_con_wait(drizzle);
 
418
    if (*ret_ptr != DRIZZLE_RETURN_OK)
 
419
    {
 
420
      drizzle->options= options;
 
421
      return NULL;
 
422
    }
 
423
  }
 
424
}
 
425
 
 
426
drizzle_return_t drizzle_query_run_all(drizzle_st *drizzle)
 
427
{
 
428
  if (drizzle == NULL)
 
429
  {
 
430
    return DRIZZLE_RETURN_INVALID_ARGUMENT;
 
431
  }
 
432
 
 
433
  while (drizzle->query_new > 0 || drizzle->query_running > 0)
 
434
  {
 
435
    drizzle_return_t ret;
 
436
 
 
437
    (void)drizzle_query_run(drizzle, &ret);
 
438
    if (ret != DRIZZLE_RETURN_OK && ret != DRIZZLE_RETURN_ERROR_CODE)
 
439
    {
 
440
      return ret;
 
441
    }
 
442
  }
 
443
 
 
444
  return DRIZZLE_RETURN_OK;
 
445
}
 
446
 
 
447
ssize_t drizzle_escape_string(char *to, size_t max_to_size, const char *from, size_t from_size)
 
448
{
 
449
  ssize_t to_size= 0;
 
450
  char newchar;
 
451
  const char *end;
 
452
 
 
453
  for (end= from + from_size; from < end; from++)
 
454
  {
 
455
    newchar= 0;
 
456
    /* All multi-byte UTF8 characters have the high bit set for all bytes. */
 
457
    if (!(*from & 0x80))
 
458
    {
 
459
      switch (*from)
 
460
      {
 
461
      case 0:
 
462
        newchar= '0';
 
463
        break;
 
464
      case '\n':
 
465
        newchar= 'n';
 
466
        break;
 
467
      case '\r':
 
468
        newchar= 'r';
 
469
        break;
 
470
      case '\032':
 
471
        newchar= 'Z';
 
472
        break;
 
473
      case '\\':
 
474
        newchar= '\\';
 
475
        break;
 
476
      case '\'':
 
477
        newchar= '\'';
 
478
        break;
 
479
      case '"':
 
480
        newchar= '"';
 
481
        break;
 
482
      default:
 
483
        break;
 
484
      }
 
485
    }
 
486
    if (newchar != '\0')
 
487
    {
 
488
      if ((size_t)to_size + 2 > max_to_size)
 
489
      {
 
490
        return -1;
 
491
      }
 
492
 
 
493
      *to++= '\\';
 
494
      *to++= newchar;
 
495
      to_size++;
 
496
    }
 
497
    else
 
498
    {
 
499
      if ((size_t)to_size + 1 > max_to_size)
 
500
      {
 
501
        return -1;
 
502
      }
 
503
 
 
504
      *to++= *from;
 
505
    }
 
506
    to_size++;
 
507
  }
 
508
 
 
509
  *to= 0;
 
510
 
 
511
  return to_size;
 
512
}
 
513
 
 
514
size_t drizzle_hex_string(char *to, const char *from, size_t from_size)
 
515
{
 
516
  static const char hex_map[]= "0123456789ABCDEF";
 
517
  const char *from_end;
 
518
 
 
519
  for (from_end= from + from_size; from != from_end; from++)
 
520
  {
 
521
    *to++= hex_map[((unsigned char) *from) >> 4];
 
522
    *to++= hex_map[((unsigned char) *from) & 0xF];
 
523
  }
 
524
 
 
525
  *to= 0;
 
526
 
 
527
  return from_size * 2;
 
528
}
 
529
 
 
530
void drizzle_mysql_password_hash(char *to, const char *from, size_t from_size)
 
531
{
 
532
  SHA1_CTX ctx;
 
533
  uint8_t hash_tmp1[SHA1_DIGEST_LENGTH];
 
534
  uint8_t hash_tmp2[SHA1_DIGEST_LENGTH];
 
535
 
 
536
  SHA1Init(&ctx);
 
537
  SHA1Update(&ctx, (const uint8_t*)from, from_size);
 
538
  SHA1Final(hash_tmp1, &ctx);
 
539
 
 
540
  SHA1Init(&ctx);
 
541
  SHA1Update(&ctx, hash_tmp1, SHA1_DIGEST_LENGTH);
 
542
  SHA1Final(hash_tmp2, &ctx);
 
543
 
 
544
  (void)drizzle_hex_string(to, (char*)hash_tmp2, SHA1_DIGEST_LENGTH);
 
545
}