Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / firebird / Util.cxx
blob74eb00c8b38588f6d1320e05f6c579dd805b3fed
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include "Util.hxx"
12 #include <rtl/ustrbuf.hxx>
14 using namespace ::connectivity;
16 using namespace ::rtl;
18 using namespace ::com::sun::star;
19 using namespace ::com::sun::star::sdbc;
20 using namespace ::com::sun::star::uno;
22 OUString firebird::sanitizeIdentifier(const OUString& rIdentifier)
24 OUString sRet = rIdentifier.trim();
25 assert(sRet.getLength() <= 31); // Firebird identifiers cannot be longer than this.
27 return sRet;
30 void firebird::evaluateStatusVector(ISC_STATUS_ARRAY& aStatusVector,
31 const OUString& aCause,
32 const uno::Reference< XInterface >& _rxContext)
33 throw(SQLException)
35 if (aStatusVector[0]==1 && aStatusVector[1]) // indicates error
37 OUStringBuffer buf;
38 char msg[512]; // Size is based on suggestion in docs.
39 const ISC_STATUS* pStatus = (const ISC_STATUS*) &aStatusVector;
41 buf.appendAscii("firebird_sdbc error:");
42 while(fb_interpret(msg, sizeof(msg), &pStatus))
44 // TODO: verify encoding
45 buf.appendAscii("\n*");
46 buf.append(OUString(msg, strlen(msg), RTL_TEXTENCODING_UTF8));
48 buf.appendAscii("\ncaused by\n'").append(aCause).appendAscii("'\n");
50 OUString error = buf.makeStringAndClear();
51 SAL_WARN("connectivity.firebird", error);
53 throw SQLException( error, _rxContext, OUString(), 1, Any() );
57 sal_Int32 firebird::getColumnTypeFromFBType(short aType)
59 aType &= ~1; // Remove last bit -- it is used to denote whether column
60 // can store Null, not needed for type determination
61 switch (aType)
63 case SQL_TEXT:
64 return DataType::CHAR;
65 case SQL_VARYING:
66 return DataType::VARCHAR;
67 case SQL_SHORT:
68 return DataType::SMALLINT;
69 case SQL_LONG:
70 return DataType::INTEGER;
71 case SQL_FLOAT:
72 return DataType::FLOAT;
73 case SQL_DOUBLE:
74 return DataType::DOUBLE;
75 case SQL_D_FLOAT:
76 return DataType::DOUBLE;
77 case SQL_TIMESTAMP:
78 return DataType::TIMESTAMP;
79 case SQL_BLOB:
80 return DataType::BLOB;
81 case SQL_ARRAY:
82 return DataType::ARRAY;
83 case SQL_TYPE_TIME:
84 return DataType::TIME;
85 case SQL_TYPE_DATE:
86 return DataType::DATE;
87 case SQL_INT64:
88 return DataType::BIGINT;
89 case SQL_NULL:
90 return DataType::SQLNULL;
91 case SQL_QUAD: // Is a "Blob ID" according to the docs
92 return 0; // TODO: verify
93 default:
94 assert(false); // Should never happen
95 return 0;
99 OUString firebird::getColumnTypeNameFromFBType(short aType)
101 aType &= ~1; // Remove last bit -- it is used to denote whether column
102 // can store Null, not needed for type determination
103 switch (aType)
105 case SQL_TEXT:
106 return OUString("SQL_TEXT");
107 case SQL_VARYING:
108 return OUString("SQL_VARYING");
109 case SQL_SHORT:
110 return OUString("SQL_SHORT");
111 case SQL_LONG:
112 return OUString("SQL_LONG");
113 case SQL_FLOAT:
114 return OUString("SQL_FLOAT");
115 case SQL_DOUBLE:
116 return OUString("SQL_DOUBLE");
117 case SQL_D_FLOAT:
118 return OUString("SQL_D_FLOAT");
119 case SQL_TIMESTAMP:
120 return OUString("SQL_TIMESTAMP");
121 case SQL_BLOB:
122 return OUString("SQL_BLOB");
123 case SQL_ARRAY:
124 return OUString("SQL_ARRAY");
125 case SQL_TYPE_TIME:
126 return OUString("SQL_TYPE_TIME");
127 case SQL_TYPE_DATE:
128 return OUString("SQL_TYPE_DATE");
129 case SQL_INT64:
130 return OUString("SQL_INT64");
131 case SQL_NULL:
132 return OUString("SQL_NULL");
133 case SQL_QUAD:
134 return OUString("SQL_QUAD");
135 default:
136 assert(false); // Should never happen
137 return OUString();
141 short firebird::getFBTypeFromBlrType(short blrType)
143 switch (blrType)
145 case blr_text:
146 return SQL_TEXT;
147 case blr_text2:
148 assert(false);
149 return 0; // No idea if this should be supported
150 case blr_varying:
151 return SQL_VARYING;
152 case blr_varying2:
153 assert(false);
154 return 0; // No idea if this should be supported
155 case blr_short:
156 return SQL_SHORT;
157 case blr_long:
158 return SQL_LONG;
159 case blr_float:
160 return SQL_FLOAT;
161 case blr_double:
162 return SQL_DOUBLE;
163 case blr_d_float:
164 return SQL_D_FLOAT;
165 case blr_timestamp:
166 return SQL_TIMESTAMP;
167 case blr_blob:
168 return SQL_BLOB;
169 // case blr_SQL_ARRAY:
170 // return OUString("SQL_ARRAY");
171 case blr_sql_time:
172 return SQL_TYPE_TIME;
173 case blr_sql_date:
174 return SQL_TYPE_DATE;
175 case blr_int64:
176 return SQL_INT64;
177 // case SQL_NULL:
178 // return OUString("SQL_NULL");
179 case blr_quad:
180 return SQL_QUAD;
181 default:
182 // If this happens we have hit one of the extra types in ibase.h
183 // look up blr_* for a list, e.g. blr_domain_name, blr_not_nullable etc.
184 assert(false);
185 return 0;
189 void firebird::mallocSQLVAR(XSQLDA* pSqlda)
191 // TODO: confirm the sizings below.
192 XSQLVAR* pVar = pSqlda->sqlvar;
193 for (int i=0; i < pSqlda->sqld; i++, pVar++)
195 int dtype = (pVar->sqltype & ~1); /* drop flag bit for now */
196 switch(dtype) {
197 case SQL_TEXT:
198 pVar->sqldata = (char *)malloc(sizeof(char)*pVar->sqllen);
199 break;
200 case SQL_VARYING:
201 pVar->sqldata = (char *)malloc(sizeof(char)*pVar->sqllen + 2);
202 break;
203 case SQL_SHORT:
204 pVar->sqldata = (char*) malloc(sizeof(sal_Int16));
205 break;
206 case SQL_LONG:
207 pVar->sqldata = (char*) malloc(sizeof(sal_Int32));
208 break;
209 case SQL_FLOAT:
210 pVar->sqldata = (char *)malloc(sizeof(float));
211 break;
212 case SQL_DOUBLE:
213 pVar->sqldata = (char *)malloc(sizeof(double));
214 break;
215 case SQL_D_FLOAT:
216 pVar->sqldata = (char *)malloc(sizeof(double));
217 break;
218 case SQL_TIMESTAMP:
219 pVar->sqldata = (char*) malloc(sizeof(ISC_TIMESTAMP));
220 break;
221 case SQL_BLOB:
222 pVar->sqldata = (char*) malloc(sizeof(ISC_QUAD));
223 break;
224 case SQL_ARRAY:
225 assert(false); // TODO: implement
226 break;
227 case SQL_TYPE_TIME:
228 pVar->sqldata = (char*) malloc(sizeof(ISC_TIME));
229 break;
230 case SQL_TYPE_DATE:
231 pVar->sqldata = (char*) malloc(sizeof(ISC_DATE));
232 break;
233 case SQL_INT64:
234 pVar->sqldata = (char *)malloc(sizeof(sal_Int64));
235 break;
236 case SQL_NULL:
237 assert(false); // TODO: implement
238 break;
239 case SQL_QUAD:
240 assert(false); // TODO: implement
241 break;
242 default:
243 SAL_WARN("connectivity.firebird", "Unknown type: " << dtype);
244 assert(false);
245 break;
247 if (pVar->sqltype & 1)
249 /* allocate variable to hold NULL status */
250 pVar->sqlind = (short *)malloc(sizeof(short));
255 void firebird::freeSQLVAR(XSQLDA* pSqlda)
257 XSQLVAR* pVar = pSqlda->sqlvar;
258 for (int i=0; i < pSqlda->sqld; i++, pVar++)
260 int dtype = (pVar->sqltype & ~1); /* drop flag bit for now */
261 switch(dtype) {
262 case SQL_TEXT:
263 case SQL_VARYING:
264 case SQL_SHORT:
265 case SQL_LONG:
266 case SQL_FLOAT:
267 case SQL_DOUBLE:
268 case SQL_D_FLOAT:
269 case SQL_TIMESTAMP:
270 case SQL_BLOB:
271 case SQL_INT64:
272 case SQL_TYPE_TIME:
273 case SQL_TYPE_DATE:
274 free(pVar->sqldata);
275 break;
276 case SQL_ARRAY:
277 assert(false); // TODO: implement
278 break;
279 case SQL_NULL:
280 assert(false); // TODO: implement
281 break;
282 case SQL_QUAD:
283 assert(false); // TODO: implement
284 break;
285 default:
286 SAL_WARN("connectivity.firebird", "Unknown type: " << dtype);
287 assert(false);
288 break;
291 if (pVar->sqltype & 1)
293 free(pVar->sqlind);
297 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */