1 /*
2  * Copyright (C) Tildeslash Ltd. All rights reserved.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 3.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14  *
15  * In addition, as a special exception, the copyright holders give
16  * permission to link the code of portions of this program with the
17  * OpenSSL library under certain conditions as described in each
18  * individual source file, and distribute linked combinations
19  * including the two.
20  *
21  * You must obey the GNU General Public License in all respects
22  * for all of the code used other than OpenSSL.
23  */
24 
25 
26 #include "Config.h"
27 
28 #include <stdio.h>
29 
30 #include "ResultSet.h"
31 #include "PreparedStatement.h"
32 
33 
34 /**
35  * Implementation of the PreparedStatement interface
36  *
37  * @file
38  */
39 
40 
41 /* ----------------------------------------------------------- Definitions */
42 
43 
44 #define T PreparedStatement_T
45 struct PreparedStatement_S {
46         Pop_T op;
47         ResultSet_T resultSet;
48         PreparedStatementDelegate_T D;
49 };
50 
51 
52 /* ------------------------------------------------------- Private methods */
53 
54 
_clearResultSet(T P)55 static void _clearResultSet(T P) {
56         if (P->resultSet)
57                 ResultSet_free(&P->resultSet);
58 }
59 
60 
61 /* ----------------------------------------------------- Protected methods */
62 
63 
64 #ifdef PACKAGE_PROTECTED
65 #pragma GCC visibility push(hidden)
66 #endif
67 
PreparedStatement_new(PreparedStatementDelegate_T D,Pop_T op)68 T PreparedStatement_new(PreparedStatementDelegate_T D, Pop_T op) {
69 	T P;
70 	assert(D);
71 	assert(op);
72         NEW(P);
73 	P->D = D;
74 	P->op = op;
75 	return P;
76 }
77 
78 
PreparedStatement_free(T * P)79 void PreparedStatement_free(T *P) {
80 	assert(P && *P);
81         _clearResultSet((*P));
82         (*P)->op->free(&((*P)->D));
83 	FREE(*P);
84 }
85 
86 #ifdef PACKAGE_PROTECTED
87 #pragma GCC visibility pop
88 #endif
89 
90 
91 /* ------------------------------------------------------------ Parameters */
92 
93 
PreparedStatement_setString(T P,int parameterIndex,const char * x)94 void PreparedStatement_setString(T P, int parameterIndex, const char *x) {
95 	assert(P);
96         P->op->setString(P->D, parameterIndex, x);
97 }
98 
99 
PreparedStatement_setInt(T P,int parameterIndex,int x)100 void PreparedStatement_setInt(T P, int parameterIndex, int x) {
101 	assert(P);
102         P->op->setInt(P->D, parameterIndex, x);
103 }
104 
105 
PreparedStatement_setLLong(T P,int parameterIndex,long long x)106 void PreparedStatement_setLLong(T P, int parameterIndex, long long x) {
107 	assert(P);
108         P->op->setLLong(P->D, parameterIndex, x);
109 }
110 
111 
PreparedStatement_setDouble(T P,int parameterIndex,double x)112 void PreparedStatement_setDouble(T P, int parameterIndex, double x) {
113 	assert(P);
114         P->op->setDouble(P->D, parameterIndex, x);
115 }
116 
117 
PreparedStatement_setBlob(T P,int parameterIndex,const void * x,int size)118 void PreparedStatement_setBlob(T P, int parameterIndex, const void *x, int size) {
119 	assert(P);
120         P->op->setBlob(P->D, parameterIndex, x, size);
121 }
122 
123 
PreparedStatement_setTimestamp(T P,int parameterIndex,time_t x)124 void PreparedStatement_setTimestamp(T P, int parameterIndex, time_t x) {
125         assert(P);
126         P->op->setTimestamp(P->D, parameterIndex, x);
127 }
128 
129 
130 /* -------------------------------------------------------- Public methods */
131 
132 
PreparedStatement_execute(T P)133 void PreparedStatement_execute(T P) {
134 	assert(P);
135         _clearResultSet(P);
136         P->op->execute(P->D);
137 }
138 
139 
PreparedStatement_executeQuery(T P)140 ResultSet_T PreparedStatement_executeQuery(T P) {
141 	assert(P);
142         _clearResultSet(P);
143 	P->resultSet = P->op->executeQuery(P->D);
144         if (! P->resultSet)
145                 THROW(SQLException, "PreparedStatement_executeQuery");
146         return P->resultSet;
147 }
148 
149 
PreparedStatement_rowsChanged(T P)150 long long PreparedStatement_rowsChanged(T P) {
151         assert(P);
152         return P->op->rowsChanged(P->D);
153 }
154 
155 
156 /* ------------------------------------------------------------ Properties */
157 
158 
PreparedStatement_getParameterCount(T P)159 int PreparedStatement_getParameterCount(T P) {
160         assert(P);
161         return P->op->parameterCount(P->D);
162 }
163