Merge lp:~linuxjedi/libdrizzle/5.1-prep-stmt-cleanup into lp:libdrizzle

Proposed by Andrew Hutchings
Status: Merged
Approved by: Andrew Hutchings
Approved revision: 77
Merged at revision: 75
Proposed branch: lp:~linuxjedi/libdrizzle/5.1-prep-stmt-cleanup
Merge into: lp:libdrizzle
Diff against target: 1193 lines (+885/-100)
13 files modified
libdrizzle-5.1/constants.h (+0/-9)
libdrizzle-5.1/return.h (+2/-0)
libdrizzle-5.1/statement.h (+47/-16)
libdrizzle/common.h (+1/-0)
libdrizzle/error.cc (+2/-0)
libdrizzle/include.am (+2/-0)
libdrizzle/statement.cc (+19/-68)
libdrizzle/statement_local.h (+65/-0)
libdrizzle/statement_param.cc (+559/-0)
libdrizzle/structs.h (+8/-1)
tests/unit/include.am (+5/-0)
tests/unit/statement.c (+18/-6)
tests/unit/statement_char.c (+157/-0)
To merge this branch: bzr merge lp:~linuxjedi/libdrizzle/5.1-prep-stmt-cleanup
Reviewer Review Type Date Requested Status
Andrew Hutchings Needs Fixing
Review via email: mp+141510@code.launchpad.net

Description of the change

Adds a JDBC like interface to libdrizzle. Now to set query params there are functions like:

drizzle_return_t drizzle_stmt_set_long(drizzle_stmt_st *stmt, uint16_t param_num, uint32_t value, bool is_unsigned);

and

drizzle_return_t drizzle_stmt_set_string(drizzle_stmt_st *stmt, uint16_t param_num, char *value, size_t length);

And to get result params:

uint32_t drizzle_stmt_get_long(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr);

and

const char *drizzle_stmt_get_string(drizzle_stmt_st *stmt, uint16_t column_number, size_t *len, drizzle_return_t *ret_ptr);

To post a comment you must log in.
Revision history for this message
Andrew Hutchings (linuxjedi) wrote :

Need to change the names from 'long' to 'int'

review: Needs Fixing
77. By Andrew Hutchings

Change long to int in function names

78. By Andrew Hutchings

Fix statement_char test case

79. By Andrew Hutchings

Using wrong source for column_count

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'libdrizzle-5.1/constants.h'
--- libdrizzle-5.1/constants.h 2012-12-30 02:23:50 +0000
+++ libdrizzle-5.1/constants.h 2012-12-31 10:04:21 +0000
@@ -567,15 +567,6 @@
567 DRIZZLE_STMT_FETCHED567 DRIZZLE_STMT_FETCHED
568} drizzle_stmt_state_t;568} drizzle_stmt_state_t;
569569
570typedef enum
571{
572 DRIZZLE_BIND_OPTION_NONE= 0,
573 DRIZZLE_BIND_OPTION_NULL= (1 << 0),
574 DRIZZLE_BIND_OPTION_UNSIGNED= (1 << 1),
575 DRIZZLE_BIND_OPTION_TRUNCATED= (1 << 2),
576 DRIZZLE_BIND_OPTION_LONG_DATA= (1 << 3)
577} drizzle_bind_options_t;
578
579#ifndef __cplusplus570#ifndef __cplusplus
580typedef enum drizzle_column_flags_t drizzle_column_flags_t;571typedef enum drizzle_column_flags_t drizzle_column_flags_t;
581#endif572#endif
582573
=== modified file 'libdrizzle-5.1/return.h'
--- libdrizzle-5.1/return.h 2012-12-29 17:31:57 +0000
+++ libdrizzle-5.1/return.h 2012-12-31 10:04:21 +0000
@@ -71,6 +71,8 @@
71 DRIZZLE_RETURN_EOF,71 DRIZZLE_RETURN_EOF,
72 DRIZZLE_RETURN_STMT_ERROR,72 DRIZZLE_RETURN_STMT_ERROR,
73 DRIZZLE_RETURN_BINLOG_CRC,73 DRIZZLE_RETURN_BINLOG_CRC,
74 DRIZZLE_RETURN_TRUNCATED,
75 DRIZZLE_RETURN_INVALID_CONVERSION,
74 DRIZZLE_RETURN_MAX /* Always add new codes to the end before this one. */76 DRIZZLE_RETURN_MAX /* Always add new codes to the end before this one. */
75};77};
7678
7779
=== modified file 'libdrizzle-5.1/statement.h'
--- libdrizzle-5.1/statement.h 2012-12-23 01:05:57 +0000
+++ libdrizzle-5.1/statement.h 2012-12-31 10:04:21 +0000
@@ -45,9 +45,6 @@
45drizzle_stmt_st *drizzle_stmt_prepare(drizzle_st *con, const char *statement, size_t size, drizzle_return_t *ret_ptr);45drizzle_stmt_st *drizzle_stmt_prepare(drizzle_st *con, const char *statement, size_t size, drizzle_return_t *ret_ptr);
4646
47DRIZZLE_API47DRIZZLE_API
48drizzle_return_t drizzle_stmt_bind_param(drizzle_stmt_st *stmt, uint16_t param_num, drizzle_column_type_t type, void *data, uint32_t length, drizzle_bind_options_t options);
49
50DRIZZLE_API
51drizzle_return_t drizzle_stmt_execute(drizzle_stmt_st *stmt);48drizzle_return_t drizzle_stmt_execute(drizzle_stmt_st *stmt);
5249
53DRIZZLE_API50DRIZZLE_API
@@ -66,18 +63,6 @@
66drizzle_return_t drizzle_stmt_close(drizzle_stmt_st *stmt);63drizzle_return_t drizzle_stmt_close(drizzle_stmt_st *stmt);
6764
68DRIZZLE_API65DRIZZLE_API
69drizzle_column_type_t drizzle_stmt_item_type(drizzle_stmt_st *stmt, uint16_t column_number);
70
71DRIZZLE_API
72void *drizzle_stmt_item_data(drizzle_stmt_st *stmt, uint16_t column_number);
73
74DRIZZLE_API
75uint32_t drizzle_stmt_item_length(drizzle_stmt_st *stmt, uint16_t column_number);
76
77DRIZZLE_API
78drizzle_bind_options_t drizzle_stmt_item_options(drizzle_stmt_st *stmt, uint16_t column_number);
79
80DRIZZLE_API
81uint16_t drizzle_stmt_column_count(drizzle_stmt_st *stmt);66uint16_t drizzle_stmt_column_count(drizzle_stmt_st *stmt);
8267
83DRIZZLE_API68DRIZZLE_API
@@ -92,7 +77,53 @@
92DRIZZLE_API77DRIZZLE_API
93uint64_t drizzle_stmt_row_count(drizzle_stmt_st *stmt);78uint64_t drizzle_stmt_row_count(drizzle_stmt_st *stmt);
9479
9580DRIZZLE_API
81drizzle_return_t drizzle_stmt_set_tiny(drizzle_stmt_st *stmt, uint16_t param_num, uint8_t value, bool is_unsigned);
82
83DRIZZLE_API
84drizzle_return_t drizzle_stmt_set_short(drizzle_stmt_st *stmt, uint16_t param_num, uint16_t value, bool is_unsigned);
85
86DRIZZLE_API
87drizzle_return_t drizzle_stmt_set_int(drizzle_stmt_st *stmt, uint16_t param_num, uint32_t value, bool is_unsigned);
88
89DRIZZLE_API
90drizzle_return_t drizzle_stmt_set_bigint(drizzle_stmt_st *stmt, uint16_t param_num, uint64_t value, bool is_unsigned);
91
92DRIZZLE_API
93drizzle_return_t drizzle_stmt_set_double(drizzle_stmt_st *stmt, uint16_t param_num, double value);
94
95DRIZZLE_API
96drizzle_return_t drizzle_stmt_set_float(drizzle_stmt_st *stmt, uint16_t param_num, float value);
97
98DRIZZLE_API
99drizzle_return_t drizzle_stmt_set_string(drizzle_stmt_st *stmt, uint16_t param_num, char *value, size_t length);
100
101DRIZZLE_API
102drizzle_return_t drizzle_stmt_set_null(drizzle_stmt_st *stmt, uint16_t param_num);
103
104DRIZZLE_API
105drizzle_return_t drizzle_stmt_set_time(drizzle_stmt_st *stmt, uint16_t param_num, uint32_t days, uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t microseconds, bool is_negative);
106
107DRIZZLE_API
108drizzle_return_t drizzle_stmt_set_timestamp(drizzle_stmt_st *stmt, uint16_t param_num, uint16_t year, uint8_t month, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t microseconds);
109
110DRIZZLE_API
111bool drizzle_stmt_get_is_null(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr);
112
113DRIZZLE_API
114bool drizzle_stmt_get_is_unsigned(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr);
115
116DRIZZLE_API
117const char *drizzle_stmt_get_string(drizzle_stmt_st *stmt, uint16_t column_number, size_t *len, drizzle_return_t *ret_ptr);
118
119DRIZZLE_API
120uint32_t drizzle_stmt_get_int(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr);
121
122DRIZZLE_API
123uint64_t drizzle_stmt_get_bigint(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr);
124
125DRIZZLE_API
126double drizzle_stmt_get_double(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr);
96127
97#ifdef __cplusplus128#ifdef __cplusplus
98}129}
99130
=== modified file 'libdrizzle/common.h'
--- libdrizzle/common.h 2012-12-31 00:19:49 +0000
+++ libdrizzle/common.h 2012-12-31 10:04:21 +0000
@@ -68,6 +68,7 @@
68#include "libdrizzle/pack.h"68#include "libdrizzle/pack.h"
69#include "libdrizzle/state.h"69#include "libdrizzle/state.h"
70#include "libdrizzle/sha1.h"70#include "libdrizzle/sha1.h"
71#include "libdrizzle/statement_local.h"
71#include "libdrizzle/column.h"72#include "libdrizzle/column.h"
72#include "libdrizzle/binlog.h"73#include "libdrizzle/binlog.h"
7374
7475
=== modified file 'libdrizzle/error.cc'
--- libdrizzle/error.cc 2012-12-29 17:31:57 +0000
+++ libdrizzle/error.cc 2012-12-31 10:04:21 +0000
@@ -70,6 +70,8 @@
70 case DRIZZLE_RETURN_EOF: return "DRIZZLE_RETURN_EOF";70 case DRIZZLE_RETURN_EOF: return "DRIZZLE_RETURN_EOF";
71 case DRIZZLE_RETURN_STMT_ERROR: return "DRIZZLE_RETURN_STMT_ERROR";71 case DRIZZLE_RETURN_STMT_ERROR: return "DRIZZLE_RETURN_STMT_ERROR";
72 case DRIZZLE_RETURN_BINLOG_CRC: return "DRIZZLE_RETURN_BINLOG_CRC";72 case DRIZZLE_RETURN_BINLOG_CRC: return "DRIZZLE_RETURN_BINLOG_CRC";
73 case DRIZZLE_RETURN_TRUNCATED: return "DRIZZLE_RETURN_TRUNCATED";
74 case DRIZZLE_RETURN_INVALID_CONVERSION: return "DRIZZLE_RETURN_INVALID_CONVERSION";
73 case DRIZZLE_RETURN_MAX: return "DRIZZLE_RETURN_MAX";75 case DRIZZLE_RETURN_MAX: return "DRIZZLE_RETURN_MAX";
74 }76 }
7577
7678
=== modified file 'libdrizzle/include.am'
--- libdrizzle/include.am 2012-12-31 00:19:49 +0000
+++ libdrizzle/include.am 2012-12-31 10:04:21 +0000
@@ -11,6 +11,7 @@
11noinst_HEADERS+= libdrizzle/pack.h11noinst_HEADERS+= libdrizzle/pack.h
12noinst_HEADERS+= libdrizzle/sha1.h12noinst_HEADERS+= libdrizzle/sha1.h
13noinst_HEADERS+= libdrizzle/state.h13noinst_HEADERS+= libdrizzle/state.h
14noinst_HEADERS+= libdrizzle/statement_local.h
14noinst_HEADERS+= libdrizzle/structs.h15noinst_HEADERS+= libdrizzle/structs.h
1516
16lib_LTLIBRARIES+= libdrizzle/libdrizzle.la17lib_LTLIBRARIES+= libdrizzle/libdrizzle.la
@@ -46,5 +47,6 @@
46libdrizzle_libdrizzle_la_SOURCES+= libdrizzle/sha1.cc47libdrizzle_libdrizzle_la_SOURCES+= libdrizzle/sha1.cc
47libdrizzle_libdrizzle_la_SOURCES+= libdrizzle/state.cc48libdrizzle_libdrizzle_la_SOURCES+= libdrizzle/state.cc
48libdrizzle_libdrizzle_la_SOURCES+= libdrizzle/statement.cc49libdrizzle_libdrizzle_la_SOURCES+= libdrizzle/statement.cc
50libdrizzle_libdrizzle_la_SOURCES+= libdrizzle/statement_param.cc
4951
50libdrizzle_libdrizzle_la_LDFLAGS+= -version-info ${LIBDRIZZLE_LIBRARY_VERSION}52libdrizzle_libdrizzle_la_LDFLAGS+= -version-info ${LIBDRIZZLE_LIBRARY_VERSION}
5153
=== modified file 'libdrizzle/statement.cc'
--- libdrizzle/statement.cc 2012-12-29 20:26:36 +0000
+++ libdrizzle/statement.cc 2012-12-31 10:04:21 +0000
@@ -110,27 +110,6 @@
110 return stmt;110 return stmt;
111}111}
112112
113drizzle_return_t drizzle_stmt_bind_param(drizzle_stmt_st *stmt, uint16_t param_num, drizzle_column_type_t type, void *data, uint32_t length, drizzle_bind_options_t options)
114{
115 if ((stmt == NULL) || (param_num >= stmt->param_count) || (data == NULL))
116 {
117 return DRIZZLE_RETURN_INVALID_ARGUMENT;
118 }
119 if (stmt->state < DRIZZLE_STMT_PREPARED)
120 {
121 drizzle_set_error(stmt->con, __func__, "stmt object has bot been prepared");
122 return DRIZZLE_RETURN_STMT_ERROR;
123 }
124
125 stmt->query_params[param_num].type= type;
126 stmt->query_params[param_num].data= data;
127 stmt->query_params[param_num].length= length;
128 stmt->query_params[param_num].options= options;
129 stmt->query_params[param_num].is_bound= true;
130
131 return DRIZZLE_RETURN_OK;
132}
133
134drizzle_return_t drizzle_stmt_execute(drizzle_stmt_st *stmt)113drizzle_return_t drizzle_stmt_execute(drizzle_stmt_st *stmt)
135{114{
136 uint16_t current_param;115 uint16_t current_param;
@@ -213,7 +192,7 @@
213 if (stmt->new_bind)192 if (stmt->new_bind)
214 {193 {
215 uint16_t type= (uint16_t)param_ptr->type;194 uint16_t type= (uint16_t)param_ptr->type;
216 if (param_ptr->options & DRIZZLE_BIND_OPTION_UNSIGNED)195 if (param_ptr->options.is_unsigned)
217 {196 {
218 /* Set the unsigned bit flag on the type data */197 /* Set the unsigned bit flag on the type data */
219 type |= 0x8000;198 type |= 0x8000;
@@ -222,7 +201,7 @@
222 buffer_pos+= 2;201 buffer_pos+= 2;
223 }202 }
224203
225 if (param_ptr->options & DRIZZLE_BIND_OPTION_LONG_DATA)204 if (param_ptr->options.is_long_data)
226 {205 {
227 /* Long data is sent separately, not in this buffer */206 /* Long data is sent separately, not in this buffer */
228 continue;207 continue;
@@ -367,7 +346,7 @@
367 drizzle_command_write(stmt->con, NULL, DRIZZLE_COMMAND_STMT_SEND_LONG_DATA,346 drizzle_command_write(stmt->con, NULL, DRIZZLE_COMMAND_STMT_SEND_LONG_DATA,
368 buffer, len+6, len+6, &ret);347 buffer, len+6, len+6, &ret);
369 stmt->con->options= (drizzle_options_t)((uint8_t)stmt->con->options & (uint8_t)~DRIZZLE_CON_NO_RESULT_READ);348 stmt->con->options= (drizzle_options_t)((uint8_t)stmt->con->options & (uint8_t)~DRIZZLE_CON_NO_RESULT_READ);
370 stmt->query_params[param_num].options= (drizzle_bind_options_t)((uint8_t)stmt->query_params[param_num].options | (uint8_t)DRIZZLE_BIND_OPTION_LONG_DATA);349 stmt->query_params[param_num].options.is_long_data= true;
371350
372 free(buffer);351 free(buffer);
373 return ret;352 return ret;
@@ -386,7 +365,7 @@
386365
387 for (current_param= 0; current_param < stmt->param_count; current_param++)366 for (current_param= 0; current_param < stmt->param_count; current_param++)
388 {367 {
389 stmt->query_params[current_param].options= (drizzle_bind_options_t)((uint8_t)stmt->query_params[current_param].options & (uint8_t)~DRIZZLE_BIND_OPTION_LONG_DATA);368 stmt->query_params[current_param].options.is_long_data= false;
390 }369 }
391370
392 drizzle_set_byte4(buffer, stmt->id);371 drizzle_set_byte4(buffer, stmt->id);
@@ -448,7 +427,7 @@
448 /* if this row is null in the result bitmap */427 /* if this row is null in the result bitmap */
449 if (*stmt->execute_result->null_bitmap & ((column_counter^2) << 2))428 if (*stmt->execute_result->null_bitmap & ((column_counter^2) << 2))
450 {429 {
451 param->options = (drizzle_bind_options_t)((uint8_t)param->options | (uint8_t) DRIZZLE_BIND_OPTION_NULL);430 param->options.is_null= true;
452 param->length= 0;431 param->length= 0;
453 }432 }
454 else433 else
@@ -459,7 +438,7 @@
459 param->length= stmt->execute_result->field_sizes[column_counter];438 param->length= stmt->execute_result->field_sizes[column_counter];
460 if (column->flags & DRIZZLE_COLUMN_FLAGS_UNSIGNED)439 if (column->flags & DRIZZLE_COLUMN_FLAGS_UNSIGNED)
461 {440 {
462 param->options = (drizzle_bind_options_t)((uint8_t)param->options | (uint8_t) DRIZZLE_BIND_OPTION_UNSIGNED);441 param->options.is_unsigned= true;
463 }442 }
464443
465 param->data= realloc(param->data, param->length);444 param->data= realloc(param->data, param->length);
@@ -574,14 +553,22 @@
574 }553 }
575554
576 free(stmt->null_bitmap);555 free(stmt->null_bitmap);
556 for (uint16_t x= 0; x < stmt->param_count; x++)
557 {
558 if (stmt->query_params[x].options.is_allocated)
559 {
560 free(stmt->query_params[x].data);
561 }
562 }
577 free(stmt->query_params);563 free(stmt->query_params);
578 if (stmt->result_params)564 if (stmt->execute_result)
579 {565 {
580 free(stmt->result_params->data);566 for (uint16_t x= 0; x < stmt->execute_result->column_count; x++)
567 {
568 free(stmt->result_params[x].data);
569 free(stmt->result_params[x].converted_data);
570 }
581 free(stmt->result_params);571 free(stmt->result_params);
582 }
583 if (stmt->execute_result)
584 {
585 drizzle_result_free(stmt->execute_result);572 drizzle_result_free(stmt->execute_result);
586 }573 }
587 if (stmt->prepare_result)574 if (stmt->prepare_result)
@@ -598,42 +585,6 @@
598 return ret;585 return ret;
599}586}
600587
601drizzle_column_type_t drizzle_stmt_item_type(drizzle_stmt_st *stmt, uint16_t column_number)
602{
603 if ((stmt == NULL) || (stmt->result_params == NULL))
604 {
605 return DRIZZLE_COLUMN_TYPE_NONE;
606 }
607 return stmt->result_params[column_number].type;
608}
609
610void *drizzle_stmt_item_data(drizzle_stmt_st *stmt, uint16_t column_number)
611{
612 if ((stmt == NULL) || (stmt->result_params == NULL))
613 {
614 return NULL;
615 }
616 return stmt->result_params[column_number].data;
617}
618
619uint32_t drizzle_stmt_item_length(drizzle_stmt_st *stmt, uint16_t column_number)
620{
621 if ((stmt == NULL) || (stmt->result_params == NULL))
622 {
623 return 0;
624 }
625 return stmt->result_params[column_number].length;
626}
627
628drizzle_bind_options_t drizzle_stmt_item_options(drizzle_stmt_st *stmt, uint16_t column_number)
629{
630 if ((stmt == NULL) || (stmt->result_params == NULL))
631 {
632 return DRIZZLE_BIND_OPTION_NONE;
633 }
634 return stmt->result_params[column_number].options;
635}
636
637uint16_t drizzle_stmt_column_count(drizzle_stmt_st *stmt)588uint16_t drizzle_stmt_column_count(drizzle_stmt_st *stmt)
638{589{
639 if ((stmt == NULL) || (stmt->prepare_result == NULL))590 if ((stmt == NULL) || (stmt->prepare_result == NULL))
640591
=== added file 'libdrizzle/statement_local.h'
--- libdrizzle/statement_local.h 1970-01-01 00:00:00 +0000
+++ libdrizzle/statement_local.h 2012-12-31 10:04:21 +0000
@@ -0,0 +1,65 @@
1/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Drizzle Client & Protocol Library
4 *
5 * Copyright (C) 2012 Drizzle Developer Group
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38#pragma once
39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44DRIZZLE_LOCAL
45drizzle_return_t drizzle_stmt_set_param(drizzle_stmt_st *stmt, uint16_t param_num, drizzle_column_type_t type, void *data, uint32_t length, bool is_unsigned, bool is_allocated);
46
47DRIZZLE_LOCAL
48char *long_to_string(drizzle_bind_st *param, uint32_t val);
49
50DRIZZLE_LOCAL
51char *longlong_to_string(drizzle_bind_st *param, uint64_t val);
52
53DRIZZLE_LOCAL
54char *double_to_string(drizzle_bind_st *param, double val);
55
56DRIZZLE_LOCAL
57char *time_to_string(drizzle_bind_st *param, drizzle_datetime_st *time);
58
59DRIZZLE_LOCAL
60char *timestamp_to_string(drizzle_bind_st *param, drizzle_datetime_st *timestamp);
61
62#ifdef __cplusplus
63}
64#endif
65
066
=== added file 'libdrizzle/statement_param.cc'
--- libdrizzle/statement_param.cc 1970-01-01 00:00:00 +0000
+++ libdrizzle/statement_param.cc 2012-12-31 10:04:21 +0000
@@ -0,0 +1,559 @@
1/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Drizzle Client & Protocol Library
4 *
5 * Copyright (C) 2012 Drizzle Developer Group
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38#include "config.h"
39#include "libdrizzle/common.h"
40
41/* Internal function */
42drizzle_return_t drizzle_stmt_set_param(drizzle_stmt_st *stmt, uint16_t param_num, drizzle_column_type_t type, void *data, uint32_t length, bool is_unsigned, bool is_allocated)
43{
44 if ((stmt == NULL) || (param_num >= stmt->param_count) || (data == NULL))
45 {
46 return DRIZZLE_RETURN_INVALID_ARGUMENT;
47 }
48 if (stmt->state < DRIZZLE_STMT_PREPARED)
49 {
50 drizzle_set_error(stmt->con, __func__, "stmt object has bot been prepared");
51 return DRIZZLE_RETURN_STMT_ERROR;
52 }
53
54 stmt->query_params[param_num].type= type;
55 stmt->query_params[param_num].data= data;
56 stmt->query_params[param_num].length= length;
57 stmt->query_params[param_num].options.is_unsigned= is_unsigned;
58 stmt->query_params[param_num].options.is_allocated= is_allocated;
59 stmt->query_params[param_num].is_bound= true;
60
61 return DRIZZLE_RETURN_OK;
62}
63
64
65drizzle_return_t drizzle_stmt_set_tiny(drizzle_stmt_st *stmt, uint16_t param_num, uint8_t value, bool is_unsigned)
66{
67 uint8_t *val;
68 stmt->query_params[param_num].data = realloc(stmt->query_params[param_num].data, sizeof(uint8_t));
69 val= (uint8_t*) stmt->query_params[param_num].data;
70 *val= value;
71
72 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_TINY, val, 1, is_unsigned, false);
73}
74drizzle_return_t drizzle_stmt_set_short(drizzle_stmt_st *stmt, uint16_t param_num, uint16_t value, bool is_unsigned)
75{
76 uint16_t *val;
77 stmt->query_params[param_num].data = realloc(stmt->query_params[param_num].data, sizeof(uint16_t));
78 val= (uint16_t*) stmt->query_params[param_num].data;
79 *val= value;
80
81 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_SHORT, val, 2, is_unsigned, false);
82}
83
84drizzle_return_t drizzle_stmt_set_int(drizzle_stmt_st *stmt, uint16_t param_num, uint32_t value, bool is_unsigned)
85{
86 uint32_t *val;
87 stmt->query_params[param_num].data = realloc(stmt->query_params[param_num].data, sizeof(uint32_t));
88 val= (uint32_t*) stmt->query_params[param_num].data;
89 *val= value;
90
91 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_LONG, val, 4, is_unsigned, true);
92}
93
94drizzle_return_t drizzle_stmt_set_bigint(drizzle_stmt_st *stmt, uint16_t param_num, uint64_t value, bool is_unsigned)
95{
96 uint64_t *val;
97 stmt->query_params[param_num].data = realloc(stmt->query_params[param_num].data, sizeof(uint64_t));
98 val= (uint64_t*) stmt->query_params[param_num].data;
99 *val= value;
100
101 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_LONGLONG, val, 8, is_unsigned, false);
102}
103
104drizzle_return_t drizzle_stmt_set_double(drizzle_stmt_st *stmt, uint16_t param_num, double value)
105{
106 double *val;
107 stmt->query_params[param_num].data = realloc(stmt->query_params[param_num].data, sizeof(double));
108 val= (double*) stmt->query_params[param_num].data;
109 *val= value;
110
111 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_DOUBLE, val, 8, false, false);
112}
113
114drizzle_return_t drizzle_stmt_set_float(drizzle_stmt_st *stmt, uint16_t param_num, float value)
115{
116 float *val;
117 stmt->query_params[param_num].data = realloc(stmt->query_params[param_num].data, sizeof(float));
118 val= (float*) stmt->query_params[param_num].data;
119 *val= value;
120
121 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_FLOAT, val, 4, false, false);
122}
123
124drizzle_return_t drizzle_stmt_set_string(drizzle_stmt_st *stmt, uint16_t param_num, char *value, size_t length)
125{
126 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_STRING, value, length, false, false);
127}
128
129drizzle_return_t drizzle_stmt_set_null(drizzle_stmt_st *stmt, uint16_t param_num)
130{
131 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_NULL, NULL, 0, false, false);
132}
133
134drizzle_return_t drizzle_stmt_set_time(drizzle_stmt_st *stmt, uint16_t param_num, uint32_t days, uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t microseconds, bool is_negative)
135{
136 drizzle_datetime_st *time;
137 stmt->query_params[param_num].data = realloc(stmt->query_params[param_num].data, sizeof(drizzle_datetime_st));
138 time= (drizzle_datetime_st*) stmt->query_params[param_num].data;
139
140 time->negative= is_negative;
141 time->day= days;
142 time->hour= hours;
143 time->minute= minutes;
144 time->second= seconds;
145 time->microsecond= microseconds;
146
147 /* Length not important because we will figure that out when packing */
148 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_TIME, time, 0, false, true);
149}
150
151drizzle_return_t drizzle_stmt_set_timestamp(drizzle_stmt_st *stmt, uint16_t param_num, uint16_t year, uint8_t month, uint8_t day, uint8_t hours, uint8_t minutes, uint8_t seconds, uint32_t microseconds)
152{
153 drizzle_datetime_st *timestamp;
154 stmt->query_params[param_num].data = realloc(stmt->query_params[param_num].data, sizeof(drizzle_datetime_st));
155 timestamp= (drizzle_datetime_st*) stmt->query_params[param_num].data;
156
157 timestamp->negative= false;
158 timestamp->year= year;
159 timestamp->day= day;
160 timestamp->month= month;
161 timestamp->year= year;
162 timestamp->hour= hours;
163 timestamp->minute= minutes;
164 timestamp->second= seconds;
165 timestamp->microsecond= microseconds;
166
167 /* Length not important because we will figure that out when packing */
168 return drizzle_stmt_set_param(stmt, param_num, DRIZZLE_COLUMN_TYPE_TIME, timestamp, 0, false, true);
169}
170
171bool drizzle_stmt_get_is_null(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr)
172{
173 if ((stmt == NULL) || (stmt->result_params == NULL) || (column_number >= stmt->execute_result->column_count))
174 {
175 *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
176 return false;
177 }
178
179 *ret_ptr= DRIZZLE_RETURN_OK;
180 return stmt->result_params[column_number].options.is_null;
181}
182
183bool drizzle_stmt_get_is_unsigned(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr)
184{
185 if ((stmt == NULL) || (stmt->result_params == NULL) || (column_number >= stmt->execute_result->column_count))
186 {
187 *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
188 return false;
189 }
190
191 *ret_ptr= DRIZZLE_RETURN_OK;
192 return stmt->result_params[column_number].options.is_unsigned;
193}
194
195const char *drizzle_stmt_get_string(drizzle_stmt_st *stmt, uint16_t column_number, size_t *len, drizzle_return_t *ret_ptr)
196{
197 char *val;
198 drizzle_bind_st *param;
199 if ((stmt == NULL) || (stmt->result_params == NULL) || (column_number >= stmt->execute_result->column_count))
200 {
201 *len= 0;
202 *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
203 return NULL;
204 }
205
206 param= &stmt->result_params[column_number];
207 *ret_ptr= DRIZZLE_RETURN_OK;
208 switch(param->type)
209 {
210 case DRIZZLE_COLUMN_TYPE_NULL:
211 *ret_ptr= DRIZZLE_RETURN_NULL_SIZE;
212 val= 0;
213 break;
214 case DRIZZLE_COLUMN_TYPE_TINY:
215 val= long_to_string(param, (uint32_t)(*(uint8_t*)param->data));
216 *len= strlen(val);
217 break;
218 case DRIZZLE_COLUMN_TYPE_SHORT:
219 case DRIZZLE_COLUMN_TYPE_YEAR:
220 val= long_to_string(param, (uint32_t)(*(uint16_t*)param->data));
221 *len= strlen(val);
222 break;
223 case DRIZZLE_COLUMN_TYPE_INT24:
224 case DRIZZLE_COLUMN_TYPE_LONG:
225 val= long_to_string(param, *(uint32_t*)param->data);
226 *len= strlen(val);
227 break;
228 case DRIZZLE_COLUMN_TYPE_LONGLONG:
229 val= longlong_to_string(param, *(uint64_t*)param->data);
230 *len= strlen(val);
231 break;
232 case DRIZZLE_COLUMN_TYPE_FLOAT:
233 val= double_to_string(param, (double) (*(float*)param->data));
234 *len= strlen(val);
235 break;
236 case DRIZZLE_COLUMN_TYPE_DOUBLE:
237 val= double_to_string(param, *(double*)param->data);
238 *len= strlen(val);
239 break;
240 case DRIZZLE_COLUMN_TYPE_TIME:
241 val= time_to_string(param, (drizzle_datetime_st*)param->data);
242 *len= strlen(val);
243 break;
244 case DRIZZLE_COLUMN_TYPE_DATE:
245 case DRIZZLE_COLUMN_TYPE_DATETIME:
246 case DRIZZLE_COLUMN_TYPE_TIMESTAMP:
247 val= timestamp_to_string(param, (drizzle_datetime_st*)param->data);
248 *len= strlen(val);
249 break;
250 case DRIZZLE_COLUMN_TYPE_TINY_BLOB:
251 case DRIZZLE_COLUMN_TYPE_MEDIUM_BLOB:
252 case DRIZZLE_COLUMN_TYPE_LONG_BLOB:
253 case DRIZZLE_COLUMN_TYPE_BLOB:
254 case DRIZZLE_COLUMN_TYPE_BIT:
255 case DRIZZLE_COLUMN_TYPE_STRING:
256 case DRIZZLE_COLUMN_TYPE_VAR_STRING:
257 case DRIZZLE_COLUMN_TYPE_DECIMAL:
258 case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:
259 val= (char*)param->data;
260 *len= param->length;
261 break;
262 case DRIZZLE_COLUMN_TYPE_NEWDATE:
263 case DRIZZLE_COLUMN_TYPE_VARCHAR:
264 case DRIZZLE_COLUMN_TYPE_ENUM:
265 case DRIZZLE_COLUMN_TYPE_SET:
266 case DRIZZLE_COLUMN_TYPE_GEOMETRY:
267 case DRIZZLE_COLUMN_TYPE_TIMESTAMP2:
268 case DRIZZLE_COLUMN_TYPE_DATETIME2:
269 case DRIZZLE_COLUMN_TYPE_TIME2:
270 default:
271 *ret_ptr= DRIZZLE_RETURN_INVALID_CONVERSION;
272 val= NULL;
273 *len= 0;
274 }
275
276 return val;
277}
278
279uint32_t drizzle_stmt_get_int(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr)
280{
281 uint32_t val;
282 drizzle_bind_st *param;
283
284 if ((stmt == NULL) || (stmt->result_params == NULL) || (column_number >= stmt->execute_result->column_count))
285 {
286 *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
287 return 0;
288 }
289
290 param= &stmt->result_params[column_number];
291 *ret_ptr= DRIZZLE_RETURN_OK;
292 switch(param->type)
293 {
294 case DRIZZLE_COLUMN_TYPE_NULL:
295 *ret_ptr= DRIZZLE_RETURN_NULL_SIZE;
296 val= 0;
297 break;
298 case DRIZZLE_COLUMN_TYPE_TINY:
299 val= (uint32_t) (*(uint8_t*)param->data);
300 break;
301 case DRIZZLE_COLUMN_TYPE_SHORT:
302 case DRIZZLE_COLUMN_TYPE_YEAR:
303 val= (uint32_t) (*(uint16_t*)param->data);
304 break;
305 case DRIZZLE_COLUMN_TYPE_INT24:
306 case DRIZZLE_COLUMN_TYPE_LONG:
307 val= (uint32_t) (*(uint32_t*)param->data);
308 break;
309 case DRIZZLE_COLUMN_TYPE_LONGLONG:
310 val= (uint32_t) (*(uint64_t*)param->data);
311 if (val > UINT32_MAX)
312 {
313 *ret_ptr= DRIZZLE_RETURN_TRUNCATED;
314 }
315 break;
316 case DRIZZLE_COLUMN_TYPE_FLOAT:
317 *ret_ptr= DRIZZLE_RETURN_TRUNCATED;
318 val= (uint32_t) (*(float*)param->data);
319 break;
320 case DRIZZLE_COLUMN_TYPE_DOUBLE:
321 *ret_ptr= DRIZZLE_RETURN_TRUNCATED;
322 val= (uint32_t) (*(double*)param->data);
323 break;
324 case DRIZZLE_COLUMN_TYPE_TIME:
325 case DRIZZLE_COLUMN_TYPE_DATE:
326 case DRIZZLE_COLUMN_TYPE_DATETIME:
327 case DRIZZLE_COLUMN_TYPE_TIMESTAMP:
328 case DRIZZLE_COLUMN_TYPE_TINY_BLOB:
329 case DRIZZLE_COLUMN_TYPE_MEDIUM_BLOB:
330 case DRIZZLE_COLUMN_TYPE_LONG_BLOB:
331 case DRIZZLE_COLUMN_TYPE_BLOB:
332 case DRIZZLE_COLUMN_TYPE_BIT:
333 case DRIZZLE_COLUMN_TYPE_STRING:
334 case DRIZZLE_COLUMN_TYPE_VAR_STRING:
335 case DRIZZLE_COLUMN_TYPE_DECIMAL:
336 case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:
337 case DRIZZLE_COLUMN_TYPE_NEWDATE:
338 case DRIZZLE_COLUMN_TYPE_VARCHAR:
339 case DRIZZLE_COLUMN_TYPE_ENUM:
340 case DRIZZLE_COLUMN_TYPE_SET:
341 case DRIZZLE_COLUMN_TYPE_GEOMETRY:
342 case DRIZZLE_COLUMN_TYPE_TIMESTAMP2:
343 case DRIZZLE_COLUMN_TYPE_DATETIME2:
344 case DRIZZLE_COLUMN_TYPE_TIME2:
345 default:
346 *ret_ptr= DRIZZLE_RETURN_INVALID_CONVERSION;
347 val= 0;
348 }
349
350 return val;
351}
352
353uint64_t drizzle_stmt_get_bigint(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr)
354{
355 uint32_t val;
356 drizzle_bind_st *param;
357
358 if ((stmt == NULL) || (stmt->result_params == NULL) || (column_number >= stmt->execute_result->column_count))
359 {
360 *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
361 return 0;
362 }
363
364 param= &stmt->result_params[column_number];
365 *ret_ptr= DRIZZLE_RETURN_OK;
366 switch(param->type)
367 {
368 case DRIZZLE_COLUMN_TYPE_NULL:
369 *ret_ptr= DRIZZLE_RETURN_NULL_SIZE;
370 val= 0;
371 break;
372 case DRIZZLE_COLUMN_TYPE_TINY:
373 val= (uint64_t) (*(uint8_t*)param->data);
374 break;
375 case DRIZZLE_COLUMN_TYPE_SHORT:
376 case DRIZZLE_COLUMN_TYPE_YEAR:
377 val= (uint64_t) (*(uint16_t*)param->data);
378 break;
379 case DRIZZLE_COLUMN_TYPE_INT24:
380 case DRIZZLE_COLUMN_TYPE_LONG:
381 val= (uint64_t) (*(uint32_t*)param->data);
382 break;
383 case DRIZZLE_COLUMN_TYPE_LONGLONG:
384 val= (uint64_t) (*(uint64_t*)param->data);
385 break;
386 case DRIZZLE_COLUMN_TYPE_FLOAT:
387 *ret_ptr= DRIZZLE_RETURN_TRUNCATED;
388 val= (uint64_t) (*(float*)param->data);
389 break;
390 case DRIZZLE_COLUMN_TYPE_DOUBLE:
391 *ret_ptr= DRIZZLE_RETURN_TRUNCATED;
392 val= (uint64_t) (*(double*)param->data);
393 break;
394 case DRIZZLE_COLUMN_TYPE_TIME:
395 case DRIZZLE_COLUMN_TYPE_DATE:
396 case DRIZZLE_COLUMN_TYPE_DATETIME:
397 case DRIZZLE_COLUMN_TYPE_TIMESTAMP:
398 case DRIZZLE_COLUMN_TYPE_TINY_BLOB:
399 case DRIZZLE_COLUMN_TYPE_MEDIUM_BLOB:
400 case DRIZZLE_COLUMN_TYPE_LONG_BLOB:
401 case DRIZZLE_COLUMN_TYPE_BLOB:
402 case DRIZZLE_COLUMN_TYPE_BIT:
403 case DRIZZLE_COLUMN_TYPE_STRING:
404 case DRIZZLE_COLUMN_TYPE_VAR_STRING:
405 case DRIZZLE_COLUMN_TYPE_DECIMAL:
406 case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:
407 case DRIZZLE_COLUMN_TYPE_NEWDATE:
408 case DRIZZLE_COLUMN_TYPE_VARCHAR:
409 case DRIZZLE_COLUMN_TYPE_ENUM:
410 case DRIZZLE_COLUMN_TYPE_SET:
411 case DRIZZLE_COLUMN_TYPE_GEOMETRY:
412 case DRIZZLE_COLUMN_TYPE_TIMESTAMP2:
413 case DRIZZLE_COLUMN_TYPE_DATETIME2:
414 case DRIZZLE_COLUMN_TYPE_TIME2:
415 default:
416 *ret_ptr= DRIZZLE_RETURN_INVALID_CONVERSION;
417 val= 0;
418 }
419
420 return val;
421}
422
423double drizzle_stmt_get_double(drizzle_stmt_st *stmt, uint16_t column_number, drizzle_return_t *ret_ptr)
424{
425 double val;
426 drizzle_bind_st *param;
427 if ((stmt == NULL) || (stmt->result_params == NULL) || (column_number >= stmt->execute_result->column_count))
428 {
429 *ret_ptr= DRIZZLE_RETURN_INVALID_ARGUMENT;
430 return 0;
431 }
432 param= &stmt->result_params[column_number];
433 *ret_ptr= DRIZZLE_RETURN_OK;
434 switch(param->type)
435 {
436 case DRIZZLE_COLUMN_TYPE_NULL:
437 *ret_ptr= DRIZZLE_RETURN_NULL_SIZE;
438 val= 0;
439 break;
440 case DRIZZLE_COLUMN_TYPE_TINY:
441 *ret_ptr= DRIZZLE_RETURN_TRUNCATED;
442 val= (double) (*(uint8_t*)param->data);
443 break;
444 case DRIZZLE_COLUMN_TYPE_SHORT:
445 case DRIZZLE_COLUMN_TYPE_YEAR:
446 *ret_ptr= DRIZZLE_RETURN_TRUNCATED;
447 val= (double) (*(uint16_t*)param->data);
448 break;
449 case DRIZZLE_COLUMN_TYPE_INT24:
450 case DRIZZLE_COLUMN_TYPE_LONG:
451 *ret_ptr= DRIZZLE_RETURN_TRUNCATED;
452 val= (double) (*(uint32_t*)param->data);
453 break;
454 case DRIZZLE_COLUMN_TYPE_LONGLONG:
455 *ret_ptr= DRIZZLE_RETURN_TRUNCATED;
456 val= (double) (*(uint64_t*)param->data);
457 break;
458 case DRIZZLE_COLUMN_TYPE_FLOAT:
459 val= (double) (*(float*)param->data);
460 break;
461 case DRIZZLE_COLUMN_TYPE_DOUBLE:
462 val= (uint32_t) (*(double*)param->data);
463 break;
464 case DRIZZLE_COLUMN_TYPE_TIME:
465 case DRIZZLE_COLUMN_TYPE_DATE:
466 case DRIZZLE_COLUMN_TYPE_DATETIME:
467 case DRIZZLE_COLUMN_TYPE_TIMESTAMP:
468 case DRIZZLE_COLUMN_TYPE_TINY_BLOB:
469 case DRIZZLE_COLUMN_TYPE_MEDIUM_BLOB:
470 case DRIZZLE_COLUMN_TYPE_LONG_BLOB:
471 case DRIZZLE_COLUMN_TYPE_BLOB:
472 case DRIZZLE_COLUMN_TYPE_BIT:
473 case DRIZZLE_COLUMN_TYPE_STRING:
474 case DRIZZLE_COLUMN_TYPE_VAR_STRING:
475 case DRIZZLE_COLUMN_TYPE_DECIMAL:
476 case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:
477 case DRIZZLE_COLUMN_TYPE_NEWDATE:
478 case DRIZZLE_COLUMN_TYPE_VARCHAR:
479 case DRIZZLE_COLUMN_TYPE_ENUM:
480 case DRIZZLE_COLUMN_TYPE_SET:
481 case DRIZZLE_COLUMN_TYPE_GEOMETRY:
482 case DRIZZLE_COLUMN_TYPE_TIMESTAMP2:
483 case DRIZZLE_COLUMN_TYPE_DATETIME2:
484 case DRIZZLE_COLUMN_TYPE_TIME2:
485 default:
486 *ret_ptr= DRIZZLE_RETURN_INVALID_CONVERSION;
487 val= 0;
488 }
489
490 return val;
491}
492
493char *long_to_string(drizzle_bind_st *param, uint32_t val)
494{
495 /* Max length is -INT32_MAX + NUL = 12 */
496 param->converted_data= (char*)realloc(param->converted_data, 12);
497 if (param->options.is_unsigned)
498 {
499 snprintf(param->converted_data, 12, "%"PRIu32, val);
500 }
501 else
502 {
503 snprintf(param->converted_data, 12, "%"PRId32, (int32_t)val);
504 }
505 return param->converted_data;
506}
507
508char *longlong_to_string(drizzle_bind_st *param, uint64_t val)
509{
510 /* Max length is -INT64_MAX + NUL = 21 */
511 param->converted_data= (char*)realloc(param->converted_data, 21);
512 if (param->options.is_unsigned)
513 {
514 snprintf(param->converted_data, 21, "%"PRIu64, val);
515 }
516 else
517 {
518 snprintf(param->converted_data, 21, "%"PRId64, (int64_t)val);
519 }
520 return param->converted_data;
521}
522
523char *double_to_string(drizzle_bind_st *param, double val)
524{
525 /* Max length is 23 */
526 param->converted_data= (char*)realloc(param->converted_data, 23);
527 snprintf(param->converted_data, 23, "%f", val);
528 return param->converted_data;
529}
530
531char *time_to_string(drizzle_bind_st *param, drizzle_datetime_st *time)
532{
533 /* Max time is -HHH:MM:SS.ssssss + NUL = 17 */
534 param->converted_data= (char*)realloc(param->converted_data, 17);
535 if (time->microsecond == 0)
536 {
537 snprintf(param->converted_data, 17, "%s%"PRIu16":%"PRIu8":%"PRIu8, (time->negative) ? "-" : "", time->hour, time->minute, time->second);
538 }
539 else
540 {
541 snprintf(param->converted_data, 17, "%s%"PRIu16":%"PRIu8":%"PRIu8".%"PRIu32, (time->negative) ? "-" : "", time->hour, time->minute, time->second, time->microsecond);
542 }
543 return param->converted_data;
544}
545
546char *timestamp_to_string(drizzle_bind_st *param, drizzle_datetime_st *timestamp)
547{
548 /* Max timestamp is YYYY-MM-DD HH:MM:SS.ssssss + NUL = 26 */
549 param->converted_data= (char*)realloc(param->converted_data, 26);
550 if (timestamp->microsecond == 0)
551 {
552 snprintf(param->converted_data, 26, "%"PRIu16"-%"PRIu8"-%"PRIu32" %"PRIu16":%"PRIu8":%"PRIu8, timestamp->year, timestamp->month, timestamp->day, timestamp->hour, timestamp->minute, timestamp->second);
553 }
554 else
555 {
556 snprintf(param->converted_data, 26, "%"PRIu16"-%"PRIu8"-%"PRIu32" %"PRIu16":%"PRIu8":%"PRIu8".%"PRIu32, timestamp->year, timestamp->month, timestamp->day, timestamp->hour, timestamp->minute, timestamp->second, timestamp->microsecond);
557 }
558 return param->converted_data;
559}
0560
=== modified file 'libdrizzle/structs.h'
--- libdrizzle/structs.h 2012-12-30 02:23:50 +0000
+++ libdrizzle/structs.h 2012-12-31 10:04:21 +0000
@@ -357,7 +357,14 @@
357 void *data;357 void *data;
358 uint32_t length;358 uint32_t length;
359 bool is_bound;359 bool is_bound;
360 drizzle_bind_options_t options;360 char *converted_data;
361 struct
362 {
363 bool is_null;
364 bool is_unsigned;
365 bool is_long_data;
366 bool is_allocated;
367 } options;
361};368};
362369
363#ifdef __cplusplus370#ifdef __cplusplus
364371
=== modified file 'tests/unit/include.am'
--- tests/unit/include.am 2012-12-27 22:09:42 +0000
+++ tests/unit/include.am 2012-12-31 10:04:21 +0000
@@ -67,3 +67,8 @@
67check_PROGRAMS+= tests/unit/statement67check_PROGRAMS+= tests/unit/statement
68noinst_PROGRAMS+= tests/unit/statement68noinst_PROGRAMS+= tests/unit/statement
6969
70tests_unit_statement_char_SOURCES= tests/unit/statement_char.c
71tests_unit_statement_char_LDADD= libdrizzle/libdrizzle.la
72check_PROGRAMS+= tests/unit/statement_char
73noinst_PROGRAMS+= tests/unit/statement_char
74
7075
=== modified file 'tests/unit/statement.c'
--- tests/unit/statement.c 2012-12-31 00:06:53 +0000
+++ tests/unit/statement.c 2012-12-31 10:04:21 +0000
@@ -91,7 +91,7 @@
91 ASSERT_EQ_(1, drizzle_stmt_param_count(stmt), "Retrieved bad param count");91 ASSERT_EQ_(1, drizzle_stmt_param_count(stmt), "Retrieved bad param count");
9292
93 uint32_t val= 1;93 uint32_t val= 1;
94 ret = drizzle_stmt_bind_param(stmt, 0, DRIZZLE_COLUMN_TYPE_LONG, &val, 4, DRIZZLE_BIND_OPTION_NONE);94 ret = drizzle_stmt_set_int(stmt, 0, val, false);
95 if (ret != DRIZZLE_RETURN_OK)95 if (ret != DRIZZLE_RETURN_OK)
96 {96 {
97 printf("Bind failure\n");97 printf("Bind failure\n");
@@ -119,12 +119,24 @@
119 uint32_t i= 1;119 uint32_t i= 1;
120 while (drizzle_stmt_fetch(stmt) != DRIZZLE_RETURN_ROW_END)120 while (drizzle_stmt_fetch(stmt) != DRIZZLE_RETURN_ROW_END)
121 {121 {
122 uint32_t *res_val;122 uint32_t res_val;
123 res_val= (uint32_t*)drizzle_stmt_item_data(stmt, 0);123 const char* char_val;
124 char comp_val[3];
125 size_t len;
126 res_val= drizzle_stmt_get_int(stmt, 0, &ret);
127 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "drizzle_stmt_get_int");
128 char_val= drizzle_stmt_get_string(stmt, 0, &len, &ret);
129 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "drizzle_stmt_get_string");
124 i++;130 i++;
125 if (*res_val != i)131 if (res_val != i)
126 {132 {
127 printf("Retrieved unexpected value\n");133 printf("Retrieved unexpected int value\n");
134 return EXIT_FAILURE;
135 }
136 snprintf(comp_val, 3, "%"PRIu32, i);
137 if (strcmp(comp_val, char_val) != 0)
138 {
139 printf("Retrieved unexpected string value\n");
128 return EXIT_FAILURE;140 return EXIT_FAILURE;
129 }141 }
130 }142 }
131143
=== added file 'tests/unit/statement_char.c'
--- tests/unit/statement_char.c 1970-01-01 00:00:00 +0000
+++ tests/unit/statement_char.c 2012-12-31 10:04:21 +0000
@@ -0,0 +1,157 @@
1/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Drizzle Client & Protocol Library
4 *
5 * Copyright (C) 2012 Drizzle Developer Group
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38#include <yatl/lite.h>
39
40#include <libdrizzle-5.1/libdrizzle.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <stdint.h>
44#include <string.h>
45#include <inttypes.h>
46#include <string.h>
47
48int main(int argc, char *argv[])
49{
50 (void) argc;
51 (void) argv;
52 drizzle_stmt_st *stmt;
53
54 drizzle_st *con= drizzle_create_tcp(getenv("MYSQL_SERVER"),
55 getenv("MYSQL_PORT") ? atoi("MYSQL_PORT") : DRIZZLE_DEFAULT_TCP_PORT,
56 getenv("MYSQL_USER"),
57 getenv("MYSQL_PASSWORD"),
58 getenv("MYSQL_SCHEMA"), 0);
59
60 ASSERT_NOT_NULL_(con, "Drizzle connection object creation error");
61
62 drizzle_return_t ret= drizzle_connect(con);
63 if (ret == DRIZZLE_RETURN_COULD_NOT_CONNECT)
64 {
65 const char *error= drizzle_error(con);
66 drizzle_quit(con);
67 SKIP_IF_(ret == DRIZZLE_RETURN_COULD_NOT_CONNECT, "%s(%s)", error, drizzle_strerror(ret));
68 }
69 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "drizzle_connect(): %s(%s)", drizzle_error(con), drizzle_strerror(ret));
70
71 drizzle_query_str(con, "DROP SCHEMA IF EXISTS libdrizzle", &ret);
72 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "CREATE SCHEMA libdrizzle (%s)", drizzle_error(con));
73
74 drizzle_query_str(con, "CREATE SCHEMA libdrizzle", &ret);
75 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "CREATE SCHEMA libdrizzle (%s)", drizzle_error(con));
76
77 drizzle_result_st *result= drizzle_select_db(con, "libdrizzle", &ret);
78 ASSERT_TRUE(result);
79 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "USE libdrizzle");
80
81 drizzle_query_str(con, "create table libdrizzle.t1 (a varchar(50))", &ret);
82 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "create table libdrizzle.t1 (a int): %s", drizzle_error(con));
83
84 drizzle_query_str(con, "insert into libdrizzle.t1 values ('hello'),('drizzle'),('people')", &ret);
85 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "%s", drizzle_error(con));
86
87 const char *query= "select * from libdrizzle.t1 where a = ?";
88 stmt= drizzle_stmt_prepare(con, query, strlen(query), &ret);
89 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "%s", drizzle_error(con));
90
91 /* Query should have 1 param */
92 ASSERT_EQ_(1, drizzle_stmt_param_count(stmt), "Retrieved bad param count");
93
94 char val[]= "hello";
95 ret = drizzle_stmt_set_string(stmt, 0, val, strlen(val));
96 if (ret != DRIZZLE_RETURN_OK)
97 {
98 printf("Bind failure\n");
99 return EXIT_FAILURE;
100 }
101
102 ret = drizzle_stmt_execute(stmt);
103 if (ret != DRIZZLE_RETURN_OK)
104 {
105 printf("Execute failure\n");
106 return EXIT_FAILURE;
107 }
108 ret = drizzle_stmt_buffer(stmt);
109 if (ret != DRIZZLE_RETURN_OK)
110 {
111 printf("Buffer failure\n");
112 return EXIT_FAILURE;
113 }
114 /* Result should have 1 row */
115 if (drizzle_stmt_row_count(stmt) != 1)
116 {
117 printf("Retrieved bad row count\n");
118 return EXIT_FAILURE;
119 }
120 uint32_t i= 0;
121 while (drizzle_stmt_fetch(stmt) != DRIZZLE_RETURN_ROW_END)
122 {
123 const char* char_val;
124 size_t len;
125 char_val= drizzle_stmt_get_string(stmt, 0, &len, &ret);
126 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "drizzle_stmt_get_string");
127 i++;
128 if (strncmp(val, char_val, len) != 0)
129 {
130 printf("Retrieved unexpected string value\n");
131 return EXIT_FAILURE;
132 }
133 }
134 /* Should have cycled through 1 row */
135 if (i != 1)
136 {
137 printf("Retrieved bad number of rows\n");
138 return EXIT_FAILURE;
139 }
140 ret = drizzle_stmt_close(stmt);
141 if (ret != DRIZZLE_RETURN_OK)
142 {
143 printf("Statement close failure ret: %d, err: %d, msg: %s\n", ret, drizzle_errno(con), drizzle_error(con));
144 return EXIT_FAILURE;
145 }
146
147 drizzle_query_str(con, "DROP TABLE libdrizzle.t1", &ret);
148 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "DROP TABLE libdrizzle.t1");
149
150 drizzle_query_str(con, "DROP SCHEMA IF EXISTS libdrizzle", &ret);
151 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "DROP SCHEMA libdrizzle (%s)", drizzle_error(con));
152
153 ret= drizzle_quit(con);
154 ASSERT_EQ_(DRIZZLE_RETURN_OK, ret, "%s", drizzle_strerror(ret));
155
156 return EXIT_SUCCESS;
157}

Subscribers

People subscribed via source and target branches

to all changes:
to status/vote changes: