update emoji autocorrect entries from po-files
[LibreOffice.git] / connectivity / source / drivers / firebird / Util.cxx
blobc8f182d37924b59cf1de501b540126b743236f0b
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"
11 #include <rtl/ustrbuf.hxx>
13 using namespace ::connectivity;
15 using namespace ::com::sun::star;
16 using namespace ::com::sun::star::sdbc;
17 using namespace ::com::sun::star::uno;
19 OUString firebird::sanitizeIdentifier(const OUString& rIdentifier)
21 OUString sRet = rIdentifier.trim();
22 assert(sRet.getLength() <= 31); // Firebird identifiers cannot be longer than this.
24 return sRet;
27 OUString firebird::StatusVectorToString(const ISC_STATUS_ARRAY& rStatusVector,
28 const OUString& rCause)
30 OUStringBuffer buf;
31 const ISC_STATUS* pStatus = reinterpret_cast<const ISC_STATUS*>(&rStatusVector);
33 buf.appendAscii("firebird_sdbc error:");
34 try
36 char msg[512]; // Size is based on suggestion in docs.
37 while(fb_interpret(msg, sizeof(msg), &pStatus))
39 // TODO: verify encoding
40 buf.appendAscii("\n*");
41 buf.append(OUString(msg, strlen(msg), RTL_TEXTENCODING_UTF8));
44 catch (...)
46 SAL_WARN("connectivity.firebird", "ignore fb_interpret exception");
48 buf.appendAscii("\ncaused by\n'").append(rCause).appendAscii("'\n");
50 OUString error = buf.makeStringAndClear();
51 SAL_WARN("connectivity.firebird", error);
52 return error;
55 void firebird::evaluateStatusVector(const ISC_STATUS_ARRAY& rStatusVector,
56 const OUString& rCause,
57 const uno::Reference< XInterface >& _rxContext)
58 throw(SQLException)
60 if (IndicatesError(rStatusVector))
62 OUString error = StatusVectorToString(rStatusVector, rCause);
63 throw SQLException(error, _rxContext, OUString(), 1, Any());
67 sal_Int32 firebird::getColumnTypeFromFBType(short aType)
69 aType &= ~1; // Remove last bit -- it is used to denote whether column
70 // can store Null, not needed for type determination
71 switch (aType)
73 case SQL_TEXT:
74 return DataType::CHAR;
75 case SQL_VARYING:
76 return DataType::VARCHAR;
77 case SQL_SHORT:
78 return DataType::SMALLINT;
79 case SQL_LONG:
80 return DataType::INTEGER;
81 case SQL_FLOAT:
82 return DataType::FLOAT;
83 case SQL_DOUBLE:
84 return DataType::DOUBLE;
85 case SQL_D_FLOAT:
86 return DataType::DOUBLE;
87 case SQL_TIMESTAMP:
88 return DataType::TIMESTAMP;
89 case SQL_BLOB:
90 return DataType::BLOB;
91 case SQL_ARRAY:
92 return DataType::ARRAY;
93 case SQL_TYPE_TIME:
94 return DataType::TIME;
95 case SQL_TYPE_DATE:
96 return DataType::DATE;
97 case SQL_INT64:
98 return DataType::BIGINT;
99 case SQL_NULL:
100 return DataType::SQLNULL;
101 case SQL_QUAD: // Is a "Blob ID" according to the docs
102 return 0; // TODO: verify
103 default:
104 assert(false); // Should never happen
105 return 0;
109 OUString firebird::getColumnTypeNameFromFBType(short aType)
111 aType &= ~1; // Remove last bit -- it is used to denote whether column
112 // can store Null, not needed for type determination
113 switch (aType)
115 case SQL_TEXT:
116 return OUString("SQL_TEXT");
117 case SQL_VARYING:
118 return OUString("SQL_VARYING");
119 case SQL_SHORT:
120 return OUString("SQL_SHORT");
121 case SQL_LONG:
122 return OUString("SQL_LONG");
123 case SQL_FLOAT:
124 return OUString("SQL_FLOAT");
125 case SQL_DOUBLE:
126 return OUString("SQL_DOUBLE");
127 case SQL_D_FLOAT:
128 return OUString("SQL_D_FLOAT");
129 case SQL_TIMESTAMP:
130 return OUString("SQL_TIMESTAMP");
131 case SQL_BLOB:
132 return OUString("SQL_BLOB");
133 case SQL_ARRAY:
134 return OUString("SQL_ARRAY");
135 case SQL_TYPE_TIME:
136 return OUString("SQL_TYPE_TIME");
137 case SQL_TYPE_DATE:
138 return OUString("SQL_TYPE_DATE");
139 case SQL_INT64:
140 return OUString("SQL_INT64");
141 case SQL_NULL:
142 return OUString("SQL_NULL");
143 case SQL_QUAD:
144 return OUString("SQL_QUAD");
145 default:
146 assert(false); // Should never happen
147 return OUString();
151 short firebird::getFBTypeFromBlrType(short blrType)
153 switch (blrType)
155 case blr_text:
156 return SQL_TEXT;
157 case blr_text2:
158 assert(false);
159 return 0; // No idea if this should be supported
160 case blr_varying:
161 return SQL_VARYING;
162 case blr_varying2:
163 assert(false);
164 return 0; // No idea if this should be supported
165 case blr_short:
166 return SQL_SHORT;
167 case blr_long:
168 return SQL_LONG;
169 case blr_float:
170 return SQL_FLOAT;
171 case blr_double:
172 return SQL_DOUBLE;
173 case blr_d_float:
174 return SQL_D_FLOAT;
175 case blr_timestamp:
176 return SQL_TIMESTAMP;
177 case blr_blob:
178 return SQL_BLOB;
179 // case blr_SQL_ARRAY:
180 // return OUString("SQL_ARRAY");
181 case blr_sql_time:
182 return SQL_TYPE_TIME;
183 case blr_sql_date:
184 return SQL_TYPE_DATE;
185 case blr_int64:
186 return SQL_INT64;
187 // case SQL_NULL:
188 // return OUString("SQL_NULL");
189 case blr_quad:
190 return SQL_QUAD;
191 default:
192 // If this happens we have hit one of the extra types in ibase.h
193 // look up blr_* for a list, e.g. blr_domain_name, blr_not_nullable etc.
194 assert(false);
195 return 0;
199 void firebird::mallocSQLVAR(XSQLDA* pSqlda)
201 // TODO: confirm the sizings below.
202 XSQLVAR* pVar = pSqlda->sqlvar;
203 for (int i=0; i < pSqlda->sqld; i++, pVar++)
205 int dtype = (pVar->sqltype & ~1); /* drop flag bit for now */
206 switch(dtype) {
207 case SQL_TEXT:
208 pVar->sqldata = static_cast<char *>(malloc(sizeof(char)*pVar->sqllen));
209 break;
210 case SQL_VARYING:
211 pVar->sqldata = static_cast<char *>(malloc(sizeof(char)*pVar->sqllen + 2));
212 break;
213 case SQL_SHORT:
214 pVar->sqldata = static_cast<char*>(malloc(sizeof(sal_Int16)));
215 break;
216 case SQL_LONG:
217 pVar->sqldata = static_cast<char*>(malloc(sizeof(sal_Int32)));
218 break;
219 case SQL_FLOAT:
220 pVar->sqldata = static_cast<char *>(malloc(sizeof(float)));
221 break;
222 case SQL_DOUBLE:
223 pVar->sqldata = static_cast<char *>(malloc(sizeof(double)));
224 break;
225 case SQL_D_FLOAT:
226 pVar->sqldata = static_cast<char *>(malloc(sizeof(double)));
227 break;
228 case SQL_TIMESTAMP:
229 pVar->sqldata = static_cast<char*>(malloc(sizeof(ISC_TIMESTAMP)));
230 break;
231 case SQL_BLOB:
232 pVar->sqldata = static_cast<char*>(malloc(sizeof(ISC_QUAD)));
233 break;
234 case SQL_ARRAY:
235 assert(false); // TODO: implement
236 break;
237 case SQL_TYPE_TIME:
238 pVar->sqldata = static_cast<char*>(malloc(sizeof(ISC_TIME)));
239 break;
240 case SQL_TYPE_DATE:
241 pVar->sqldata = static_cast<char*>(malloc(sizeof(ISC_DATE)));
242 break;
243 case SQL_INT64:
244 pVar->sqldata = static_cast<char *>(malloc(sizeof(sal_Int64)));
245 break;
246 case SQL_NULL:
247 assert(false); // TODO: implement
248 break;
249 case SQL_QUAD:
250 assert(false); // TODO: implement
251 break;
252 default:
253 SAL_WARN("connectivity.firebird", "Unknown type: " << dtype);
254 assert(false);
255 break;
257 if (pVar->sqltype & 1)
259 /* allocate variable to hold NULL status */
260 pVar->sqlind = static_cast<short *>(malloc(sizeof(short)));
265 void firebird::freeSQLVAR(XSQLDA* pSqlda)
267 XSQLVAR* pVar = pSqlda->sqlvar;
268 for (int i=0; i < pSqlda->sqld; i++, pVar++)
270 int dtype = (pVar->sqltype & ~1); /* drop flag bit for now */
271 switch(dtype) {
272 case SQL_TEXT:
273 case SQL_VARYING:
274 case SQL_SHORT:
275 case SQL_LONG:
276 case SQL_FLOAT:
277 case SQL_DOUBLE:
278 case SQL_D_FLOAT:
279 case SQL_TIMESTAMP:
280 case SQL_BLOB:
281 case SQL_INT64:
282 case SQL_TYPE_TIME:
283 case SQL_TYPE_DATE:
284 if(pVar->sqldata)
286 free(pVar->sqldata);
287 pVar->sqldata = NULL;
289 break;
290 case SQL_ARRAY:
291 assert(false); // TODO: implement
292 break;
293 case SQL_NULL:
294 assert(false); // TODO: implement
295 break;
296 case SQL_QUAD:
297 assert(false); // TODO: implement
298 break;
299 default:
300 SAL_WARN("connectivity.firebird", "Unknown type: " << dtype);
301 // assert(false);
302 break;
305 if (pVar->sqltype & 1)
307 if(pVar->sqlind)
309 free(pVar->sqlind);
310 pVar->sqlind = NULL;
315 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */