LibreOffice Module connectivity (master) 1
mysqlc_general.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19#include "mysqlc_general.hxx"
20
21#include <sal/log.hxx>
22#include <rtl/ustring.hxx>
23#include <o3tl/string_view.hxx>
24
25#include <com/sun/star/sdbc/DataType.hpp>
26
27using com::sun::star::sdbc::SQLException;
28
29using com::sun::star::uno::Any;
31using com::sun::star::uno::XInterface;
32
33using namespace rtl;
34
36{
37void allocateSqlVar(void** mem, enum_field_types eType, unsigned nSize)
38{
39 assert(mem);
40 switch (eType)
41 {
42 case MYSQL_TYPE_LONG:
43 case MYSQL_TYPE_INT24:
44 *mem = malloc(sizeof(sal_Int32));
45 break;
46 case MYSQL_TYPE_SHORT:
47 case MYSQL_TYPE_YEAR:
48 *mem = malloc(sizeof(sal_Int16));
49 break;
50 case MYSQL_TYPE_BIT:
51 case MYSQL_TYPE_TINY:
52 *mem = malloc(sizeof(sal_Int8));
53 break;
54 case MYSQL_TYPE_LONGLONG:
55 *mem = malloc(sizeof(sal_Int64));
56 break;
57 case MYSQL_TYPE_FLOAT:
58 *mem = malloc(sizeof(float));
59 break;
60 case MYSQL_TYPE_DOUBLE:
61 *mem = malloc(sizeof(double));
62 break;
63 case MYSQL_TYPE_DATE:
64 case MYSQL_TYPE_TIME:
65 case MYSQL_TYPE_DATETIME:
66 case MYSQL_TYPE_TIMESTAMP:
67 case MYSQL_TYPE_NEWDATE:
68 case MYSQL_TYPE_ENUM:
69 case MYSQL_TYPE_SET:
70 case MYSQL_TYPE_GEOMETRY:
71 *mem = malloc(sizeof(MYSQL_TIME));
72 break;
73 case MYSQL_TYPE_STRING:
74 case MYSQL_TYPE_VAR_STRING:
75 case MYSQL_TYPE_VARCHAR:
76 case MYSQL_TYPE_DECIMAL:
77 case MYSQL_TYPE_NEWDECIMAL:
78 *mem = malloc(sizeof(char) * nSize);
79 break;
80 case MYSQL_TYPE_NULL:
81 case MYSQL_TYPE_BLOB:
82 case MYSQL_TYPE_TINY_BLOB:
83 case MYSQL_TYPE_MEDIUM_BLOB:
84 case MYSQL_TYPE_LONG_BLOB:
85 *mem = nullptr;
86 break;
87 default:
88 SAL_WARN("connectivity.mysqlc", "unknown enum_field_type");
89 }
90}
91
92void throwFeatureNotImplementedException(const char* _pAsciiFeatureName,
93 const css::uno::Reference<XInterface>& _rxContext)
94{
95 const OUString sMessage
96 = OUString::createFromAscii(_pAsciiFeatureName) + ": feature not implemented.";
97 throw SQLException(sMessage, _rxContext, "HYC00", 0, Any());
98}
99
100void throwInvalidArgumentException(const char* _pAsciiFeatureName,
101 const css::uno::Reference<XInterface>& _rxContext)
102{
103 const OUString sMessage
104 = OUString::createFromAscii(_pAsciiFeatureName) + ": invalid arguments.";
105 throw SQLException(sMessage, _rxContext, "HYC00", 0, Any());
106}
107
108void throwSQLExceptionWithMsg(const char* msg, const char* SQLSTATE, unsigned int errorNum,
109 const css::uno::Reference<css::uno::XInterface>& _context,
110 const rtl_TextEncoding encoding)
111{
112 OString errorMsg{ msg };
113 throwSQLExceptionWithMsg(OStringToOUString(errorMsg, encoding), SQLSTATE, errorNum, _context);
114}
115
116void throwSQLExceptionWithMsg(const OUString& msg, const char* SQLSTATE, unsigned int errorNum,
117 const css::uno::Reference<css::uno::XInterface>& _context)
118{
119 throw SQLException(msg, _context, OStringToOUString(SQLSTATE, RTL_TEXTENCODING_ASCII_US),
120 errorNum, Any());
121}
122
123sal_Int32 mysqlToOOOType(int eType, int charsetnr) noexcept
124{
125 // charset number 63 indicates binary
126 switch (eType)
127 {
128 case MYSQL_TYPE_BIT:
129 return css::sdbc::DataType::BIT;
130
131 case MYSQL_TYPE_TINY:
132 return css::sdbc::DataType::TINYINT;
133
134 case MYSQL_TYPE_SHORT:
135 case MYSQL_TYPE_YEAR:
136 return css::sdbc::DataType::SMALLINT;
137
138 case MYSQL_TYPE_INT24:
139 case MYSQL_TYPE_LONG:
140 return css::sdbc::DataType::INTEGER;
141
142 case MYSQL_TYPE_LONGLONG:
143 return css::sdbc::DataType::BIGINT;
144
145 case MYSQL_TYPE_FLOAT:
146 return css::sdbc::DataType::REAL;
147
148 case MYSQL_TYPE_DOUBLE:
149 return css::sdbc::DataType::DOUBLE;
150
151 case MYSQL_TYPE_DECIMAL:
152 case MYSQL_TYPE_NEWDECIMAL:
153 return css::sdbc::DataType::DECIMAL;
154
155 case MYSQL_TYPE_STRING:
156 if (charsetnr == 63)
157 return css::sdbc::DataType::BINARY;
158 return css::sdbc::DataType::CHAR;
159
160 case MYSQL_TYPE_ENUM:
161 case MYSQL_TYPE_SET:
162 case MYSQL_TYPE_VAR_STRING:
163 if (charsetnr == 63)
164 return css::sdbc::DataType::VARBINARY;
165 return css::sdbc::DataType::VARCHAR;
166
167 case MYSQL_TYPE_BLOB:
168 case MYSQL_TYPE_TINY_BLOB:
169 case MYSQL_TYPE_MEDIUM_BLOB:
170 case MYSQL_TYPE_LONG_BLOB:
171 if (charsetnr == 63)
172 return css::sdbc::DataType::LONGVARBINARY;
173 return css::sdbc::DataType::LONGVARCHAR;
174
175 case MYSQL_TYPE_TIMESTAMP:
176 case MYSQL_TYPE_DATETIME:
177 return css::sdbc::DataType::TIMESTAMP;
178
179 case MYSQL_TYPE_DATE:
180 return css::sdbc::DataType::DATE;
181
182 case MYSQL_TYPE_TIME:
183 return css::sdbc::DataType::TIME;
184
185 case MYSQL_TYPE_GEOMETRY:
186 return css::sdbc::DataType::VARCHAR;
187
188 case MYSQL_TYPE_NULL:
189 return css::sdbc::DataType::SQLNULL;
190 }
191
192 OSL_FAIL("mysqlToOOOType: unhandled case, falling back to VARCHAR");
193 return css::sdbc::DataType::VARCHAR;
194}
195
196sal_Int32 mysqlStrToOOOType(std::u16string_view sType)
197{
198 // TODO other types.
200 || o3tl::equalsIgnoreAsciiCase(sType, u"tinyint"))
201 return css::sdbc::DataType::TINYINT;
202 if (o3tl::equalsIgnoreAsciiCase(sType, u"smallint")
204 return css::sdbc::DataType::SMALLINT;
206 || o3tl::equalsIgnoreAsciiCase(sType, u"mediumint"))
207 return css::sdbc::DataType::INTEGER;
210 return css::sdbc::DataType::VARCHAR;
211 if (o3tl::equalsIgnoreAsciiCase(sType, u"bigint"))
212 return css::sdbc::DataType::BIGINT;
214 || o3tl::equalsIgnoreAsciiCase(sType, u"longblob")
215 || o3tl::equalsIgnoreAsciiCase(sType, u"tinyblob")
216 || o3tl::equalsIgnoreAsciiCase(sType, u"mediumblob"))
217 return css::sdbc::DataType::BLOB;
218 if (o3tl::equalsIgnoreAsciiCase(sType, u"varbinary"))
219 return css::sdbc::DataType::VARBINARY;
221 return css::sdbc::DataType::CHAR;
222 if (o3tl::equalsIgnoreAsciiCase(sType, u"tinytext"))
223 return css::sdbc::DataType::VARCHAR;
225 return css::sdbc::DataType::LONGVARCHAR;
226 if (o3tl::equalsIgnoreAsciiCase(sType, u"mediumtext")
227 || o3tl::equalsIgnoreAsciiCase(sType, u"longtext"))
228 return css::sdbc::DataType::CLOB;
229 if (o3tl::equalsIgnoreAsciiCase(sType, u"binary"))
230 return css::sdbc::DataType::BINARY;
232 return css::sdbc::DataType::TIME;
234 return css::sdbc::DataType::DATE;
235 if (o3tl::equalsIgnoreAsciiCase(sType, u"datetime")
236 || o3tl::equalsIgnoreAsciiCase(sType, u"timestamp"))
237 return css::sdbc::DataType::TIMESTAMP;
238 if (o3tl::equalsIgnoreAsciiCase(sType, u"decimal"))
239 return css::sdbc::DataType::DECIMAL;
241 return css::sdbc::DataType::REAL;
242 if (o3tl::equalsIgnoreAsciiCase(sType, u"double"))
243 return css::sdbc::DataType::DOUBLE;
245 return css::sdbc::DataType::BIT;
247 || o3tl::equalsIgnoreAsciiCase(sType, u"boolean"))
248 return css::sdbc::DataType::BOOLEAN;
249 OSL_FAIL("Unknown type name from string, failing back to varchar.");
250 return css::sdbc::DataType::VARCHAR;
251}
252
253OUString mysqlTypeToStr(unsigned type, unsigned flags)
254{
255 bool isUnsigned = (flags & UNSIGNED_FLAG) != 0;
256 bool isZerofill = (flags & ZEROFILL_FLAG) != 0;
257 switch (type)
258 {
259 case MYSQL_TYPE_BIT:
260 return OUString{ "BIT" };
261 case MYSQL_TYPE_DECIMAL:
262 case MYSQL_TYPE_NEWDECIMAL:
263 return isUnsigned ? (isZerofill ? OUString{ "DECIMAL UNSIGNED ZEROFILL" }
264 : OUString{ "DECIMAL UNSIGNED" })
265 : OUString{ "DECIMAL" };
266 case MYSQL_TYPE_TINY:
267 return isUnsigned ? (isZerofill ? OUString{ "TINYINT UNSIGNED ZEROFILL" }
268 : OUString{ "TINYINT UNSIGNED" })
269 : OUString{ "TINYINT" };
270 case MYSQL_TYPE_SHORT:
271 return isUnsigned ? (isZerofill ? OUString{ "SMALLINT UNSIGNED ZEROFILL" }
272 : OUString{ "SMALLINT UNSIGNED" })
273 : OUString{ "SMALLINT" };
274 case MYSQL_TYPE_LONG:
275 return isUnsigned ? (isZerofill ? OUString{ "INT UNSIGNED ZEROFILL" }
276 : OUString{ "INT UNSIGNED" })
277 : OUString{ "INT" };
278 case MYSQL_TYPE_FLOAT:
279 return isUnsigned ? (isZerofill ? OUString{ "FLOAT UNSIGNED ZEROFILL" }
280 : OUString{ "FLOAT UNSIGNED" })
281 : OUString{ "FLOAT" };
282 case MYSQL_TYPE_DOUBLE:
283 return isUnsigned ? (isZerofill ? OUString{ "DOUBLE UNSIGNED ZEROFILL" }
284 : OUString{ "DOUBLE UNSIGNED" })
285 : OUString{ "DOUBLE" };
286 case MYSQL_TYPE_NULL:
287 return OUString{ "NULL" };
288 case MYSQL_TYPE_TIMESTAMP:
289 return OUString{ "TIMESTAMP" };
290 case MYSQL_TYPE_LONGLONG:
291 return isUnsigned ? (isZerofill ? OUString{ "BIGINT UNSIGNED ZEROFILL" }
292 : OUString{ "BIGINT UNSIGNED" })
293 : OUString{ "BIGINT" };
294 case MYSQL_TYPE_INT24:
295 return isUnsigned ? (isZerofill ? OUString{ "MEDIUMINT UNSIGNED ZEROFILL" }
296 : OUString{ "MEDIUMINT UNSIGNED" })
297 : OUString{ "MEDIUMINT" };
298 case MYSQL_TYPE_DATE:
299 return OUString{ "DATE" };
300 case MYSQL_TYPE_TIME:
301 return OUString{ "TIME" };
302 case MYSQL_TYPE_DATETIME:
303 return OUString{ "DATETIME" };
304 case MYSQL_TYPE_TINY_BLOB:
305 {
306 return OUString{ "TINYBLOB" };
307 }
308 case MYSQL_TYPE_MEDIUM_BLOB:
309 {
310 return OUString{ "MEDIUMBLOB" };
311 }
312 case MYSQL_TYPE_LONG_BLOB:
313 {
314 return OUString{ "LONGBLOB" };
315 }
316 case MYSQL_TYPE_BLOB:
317 {
318 return OUString{ "BLOB" };
319 }
320 case MYSQL_TYPE_VARCHAR:
321 case MYSQL_TYPE_VAR_STRING:
322 if (flags & ENUM_FLAG)
323 {
324 return OUString{ "ENUM" };
325 }
326 if (flags & SET_FLAG)
327 {
328 return OUString{ "SET" };
329 }
330 return OUString{ "VARCHAR" };
331 case MYSQL_TYPE_STRING:
332 if (flags & ENUM_FLAG)
333 {
334 return OUString{ "ENUM" };
335 }
336 if (flags & SET_FLAG)
337 {
338 return OUString{ "SET" };
339 }
340 return OUString{ "CHAR" };
341 case MYSQL_TYPE_YEAR:
342 return OUString{ "YEAR" };
343 case MYSQL_TYPE_GEOMETRY:
344 return OUString{ "GEOMETRY" };
345 default:
346 return OUString{ "UNKNOWN" };
347 }
348}
349
350OUString convert(const ::std::string& _string, const rtl_TextEncoding encoding)
351{
352 return OUString(_string.c_str(), _string.size(), encoding);
353}
354
355} /* namespace */
356
357/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sType
float u
DocumentType eType
#define SAL_WARN(area, stream)
OUString convert(const ::std::string &_string, const rtl_TextEncoding encoding)
sal_Int32 mysqlStrToOOOType(std::u16string_view sType)
void throwInvalidArgumentException(const char *_pAsciiFeatureName, const css::uno::Reference< XInterface > &_rxContext)
void allocateSqlVar(void **mem, enum_field_types eType, unsigned nSize)
sal_Int32 mysqlToOOOType(int eType, int charsetnr) noexcept
void throwSQLExceptionWithMsg(const char *msg, const char *SQLSTATE, unsigned int errorNum, const css::uno::Reference< css::uno::XInterface > &_context, const rtl_TextEncoding encoding)
OUString mysqlTypeToStr(unsigned type, unsigned flags)
void throwFeatureNotImplementedException(const char *_pAsciiFeatureName, const css::uno::Reference< XInterface > &_rxContext)
bool equalsIgnoreAsciiCase(std::u16string_view s1, std::u16string_view s2)
sal_Int32 type
Definition: pq_statics.cxx:60
OUString sMessage
signed char sal_Int8