bump product version to 7.2.5.1
[LibreOffice.git] / connectivity / source / drivers / mysqlc / mysqlc_general.cxx
blob878efdc3be244e5b957f4ef5defd14b816076979
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include "mysqlc_general.hxx"
21 #include <sal/log.hxx>
22 #include <rtl/ustring.hxx>
24 #include <com/sun/star/sdbc/DataType.hpp>
26 using com::sun::star::sdbc::SQLException;
28 using com::sun::star::uno::Any;
29 using com::sun::star::uno::Reference;
30 using com::sun::star::uno::XInterface;
32 using namespace rtl;
34 namespace mysqlc_sdbc_driver
36 void allocateSqlVar(void** mem, enum_field_types eType, unsigned nSize)
38 assert(mem);
39 switch (eType)
41 case MYSQL_TYPE_LONG:
42 case MYSQL_TYPE_INT24:
43 *mem = malloc(sizeof(sal_Int32));
44 break;
45 case MYSQL_TYPE_SHORT:
46 *mem = malloc(sizeof(sal_Int16));
47 break;
48 case MYSQL_TYPE_BIT:
49 case MYSQL_TYPE_TINY:
50 *mem = malloc(sizeof(sal_Int8));
51 break;
52 case MYSQL_TYPE_LONGLONG:
53 *mem = malloc(sizeof(sal_Int64));
54 break;
55 case MYSQL_TYPE_FLOAT:
56 *mem = malloc(sizeof(float));
57 break;
58 case MYSQL_TYPE_DOUBLE:
59 *mem = malloc(sizeof(double));
60 break;
61 case MYSQL_TYPE_DATE:
62 case MYSQL_TYPE_TIME:
63 case MYSQL_TYPE_DATETIME:
64 case MYSQL_TYPE_TIMESTAMP:
65 case MYSQL_TYPE_YEAR: // FIXME below
66 case MYSQL_TYPE_NEWDATE:
67 case MYSQL_TYPE_ENUM:
68 case MYSQL_TYPE_SET:
69 case MYSQL_TYPE_GEOMETRY:
70 *mem = malloc(sizeof(MYSQL_TIME));
71 break;
72 case MYSQL_TYPE_STRING:
73 case MYSQL_TYPE_VAR_STRING:
74 case MYSQL_TYPE_VARCHAR:
75 case MYSQL_TYPE_DECIMAL:
76 case MYSQL_TYPE_NEWDECIMAL:
77 *mem = malloc(sizeof(char) * nSize);
78 break;
79 case MYSQL_TYPE_NULL:
80 case MYSQL_TYPE_BLOB:
81 case MYSQL_TYPE_TINY_BLOB:
82 case MYSQL_TYPE_MEDIUM_BLOB:
83 case MYSQL_TYPE_LONG_BLOB:
84 *mem = nullptr;
85 break;
86 default:
87 SAL_WARN("connectivity.mysqlc", "unknown enum_field_type");
91 void throwFeatureNotImplementedException(const char* _pAsciiFeatureName,
92 const css::uno::Reference<XInterface>& _rxContext)
94 const OUString sMessage
95 = OUString::createFromAscii(_pAsciiFeatureName) + ": feature not implemented.";
96 throw SQLException(sMessage, _rxContext, "HYC00", 0, Any());
99 void throwInvalidArgumentException(const char* _pAsciiFeatureName,
100 const css::uno::Reference<XInterface>& _rxContext)
102 const OUString sMessage
103 = OUString::createFromAscii(_pAsciiFeatureName) + ": invalid arguments.";
104 throw SQLException(sMessage, _rxContext, "HYC00", 0, Any());
107 void throwSQLExceptionWithMsg(const char* msg, const char* SQLSTATE, unsigned int errorNum,
108 const css::uno::Reference<css::uno::XInterface>& _context,
109 const rtl_TextEncoding encoding)
111 OString errorMsg{ msg };
112 throwSQLExceptionWithMsg(OStringToOUString(errorMsg, encoding), SQLSTATE, errorNum, _context);
115 void throwSQLExceptionWithMsg(const OUString& msg, const char* SQLSTATE, unsigned int errorNum,
116 const css::uno::Reference<css::uno::XInterface>& _context)
118 throw SQLException(msg, _context, OStringToOUString(SQLSTATE, RTL_TEXTENCODING_ASCII_US),
119 errorNum, Any());
122 sal_Int32 mysqlToOOOType(int eType, int charsetnr) noexcept
124 // charset number 63 indicates binary
125 switch (eType)
127 case MYSQL_TYPE_BIT:
128 return css::sdbc::DataType::VARCHAR;
130 case MYSQL_TYPE_TINY:
131 return css::sdbc::DataType::TINYINT;
133 case MYSQL_TYPE_SHORT:
134 return css::sdbc::DataType::SMALLINT;
136 case MYSQL_TYPE_INT24:
137 case MYSQL_TYPE_LONG:
138 return css::sdbc::DataType::INTEGER;
140 case MYSQL_TYPE_LONGLONG:
141 return css::sdbc::DataType::BIGINT;
143 case MYSQL_TYPE_FLOAT:
144 return css::sdbc::DataType::REAL;
146 case MYSQL_TYPE_DOUBLE:
147 return css::sdbc::DataType::DOUBLE;
149 case MYSQL_TYPE_DECIMAL:
150 case MYSQL_TYPE_NEWDECIMAL:
151 return css::sdbc::DataType::DECIMAL;
153 case MYSQL_TYPE_STRING:
154 if (charsetnr == 63)
155 return css::sdbc::DataType::BINARY;
156 return css::sdbc::DataType::CHAR;
158 case MYSQL_TYPE_ENUM:
159 case MYSQL_TYPE_SET:
160 case MYSQL_TYPE_VAR_STRING:
161 if (charsetnr == 63)
162 return css::sdbc::DataType::VARBINARY;
163 return css::sdbc::DataType::VARCHAR;
165 case MYSQL_TYPE_BLOB:
166 if (charsetnr == 63)
167 return css::sdbc::DataType::LONGVARBINARY;
168 return css::sdbc::DataType::LONGVARCHAR;
170 case MYSQL_TYPE_TIMESTAMP:
171 case MYSQL_TYPE_DATETIME:
172 return css::sdbc::DataType::TIMESTAMP;
174 case MYSQL_TYPE_DATE:
175 return css::sdbc::DataType::DATE;
177 case MYSQL_TYPE_TIME:
178 return css::sdbc::DataType::TIME;
180 case MYSQL_TYPE_GEOMETRY:
181 return css::sdbc::DataType::VARCHAR;
183 case MYSQL_TYPE_NULL:
184 return css::sdbc::DataType::SQLNULL;
187 OSL_FAIL("mysqlToOOOType: unhandled case, falling back to VARCHAR");
188 return css::sdbc::DataType::VARCHAR;
191 sal_Int32 mysqlStrToOOOType(const OUString& sType)
193 // TODO other types.
194 if (sType.equalsIgnoreAsciiCase("tiny") || sType.equalsIgnoreAsciiCase("tinyint"))
195 return css::sdbc::DataType::TINYINT;
196 if (sType.equalsIgnoreAsciiCase("smallint"))
197 return css::sdbc::DataType::SMALLINT;
198 if (sType.equalsIgnoreAsciiCase("longtext"))
199 return css::sdbc::DataType::LONGVARCHAR;
200 if (sType.equalsIgnoreAsciiCase("int") || sType.equalsIgnoreAsciiCase("mediumint"))
201 return css::sdbc::DataType::INTEGER;
202 if (sType.equalsIgnoreAsciiCase("varchar") || sType.equalsIgnoreAsciiCase("set")
203 || sType.equalsIgnoreAsciiCase("enum"))
204 return css::sdbc::DataType::VARCHAR;
205 if (sType.equalsIgnoreAsciiCase("bigint"))
206 return css::sdbc::DataType::BIGINT;
207 if (sType.equalsIgnoreAsciiCase("blob") || sType.equalsIgnoreAsciiCase("longblob"))
208 return css::sdbc::DataType::BLOB;
209 if (sType.equalsIgnoreAsciiCase("varbinary"))
210 return css::sdbc::DataType::VARBINARY;
211 if (sType.equalsIgnoreAsciiCase("char"))
212 return css::sdbc::DataType::CHAR;
213 if (sType.equalsIgnoreAsciiCase("text"))
214 return css::sdbc::DataType::CLOB;
215 if (sType.equalsIgnoreAsciiCase("binary"))
216 return css::sdbc::DataType::BINARY;
217 if (sType.equalsIgnoreAsciiCase("time"))
218 return css::sdbc::DataType::TIME;
219 if (sType.equalsIgnoreAsciiCase("date"))
220 return css::sdbc::DataType::DATE;
221 if (sType.equalsIgnoreAsciiCase("datetime") || sType.equalsIgnoreAsciiCase("timestamp"))
222 return css::sdbc::DataType::TIMESTAMP;
223 if (sType.equalsIgnoreAsciiCase("decimal"))
224 return css::sdbc::DataType::DECIMAL;
225 if (sType.equalsIgnoreAsciiCase("real") || sType.equalsIgnoreAsciiCase("float"))
226 return css::sdbc::DataType::REAL;
227 if (sType.equalsIgnoreAsciiCase("double"))
228 return css::sdbc::DataType::DOUBLE;
229 if (sType.equalsIgnoreAsciiCase("bit") || sType.equalsIgnoreAsciiCase("bool")
230 || sType.equalsIgnoreAsciiCase("boolean"))
231 return css::sdbc::DataType::BOOLEAN;
232 OSL_FAIL("Unknown type name from string, failing back to varchar.");
233 return css::sdbc::DataType::VARCHAR;
236 OUString mysqlTypeToStr(unsigned type, unsigned flags)
238 bool isUnsigned = (flags & UNSIGNED_FLAG) != 0;
239 bool isZerofill = (flags & ZEROFILL_FLAG) != 0;
240 switch (type)
242 case MYSQL_TYPE_BIT:
243 return OUString{ "BIT" };
244 case MYSQL_TYPE_DECIMAL:
245 case MYSQL_TYPE_NEWDECIMAL:
246 return isUnsigned ? (isZerofill ? OUString{ "DECIMAL UNSIGNED ZEROFILL" }
247 : OUString{ "DECIMAL UNSIGNED" })
248 : OUString{ "DECIMAL" };
249 case MYSQL_TYPE_TINY:
250 return isUnsigned ? (isZerofill ? OUString{ "TINYINT UNSIGNED ZEROFILL" }
251 : OUString{ "TINYINT UNSIGNED" })
252 : OUString{ "TINYINT" };
253 case MYSQL_TYPE_SHORT:
254 return isUnsigned ? (isZerofill ? OUString{ "SMALLINT UNSIGNED ZEROFILL" }
255 : OUString{ "SMALLINT UNSIGNED" })
256 : OUString{ "SMALLINT" };
257 case MYSQL_TYPE_LONG:
258 return isUnsigned ? (isZerofill ? OUString{ "INT UNSIGNED ZEROFILL" }
259 : OUString{ "INT UNSIGNED" })
260 : OUString{ "INT" };
261 case MYSQL_TYPE_FLOAT:
262 return isUnsigned ? (isZerofill ? OUString{ "FLOAT UNSIGNED ZEROFILL" }
263 : OUString{ "FLOAT UNSIGNED" })
264 : OUString{ "FLOAT" };
265 case MYSQL_TYPE_DOUBLE:
266 return isUnsigned ? (isZerofill ? OUString{ "DOUBLE UNSIGNED ZEROFILL" }
267 : OUString{ "DOUBLE UNSIGNED" })
268 : OUString{ "DOUBLE" };
269 case MYSQL_TYPE_NULL:
270 return OUString{ "NULL" };
271 case MYSQL_TYPE_TIMESTAMP:
272 return OUString{ "TIMESTAMP" };
273 case MYSQL_TYPE_LONGLONG:
274 return isUnsigned ? (isZerofill ? OUString{ "BIGINT UNSIGNED ZEROFILL" }
275 : OUString{ "BIGINT UNSIGNED" })
276 : OUString{ "BIGINT" };
277 case MYSQL_TYPE_INT24:
278 return isUnsigned ? (isZerofill ? OUString{ "MEDIUMINT UNSIGNED ZEROFILL" }
279 : OUString{ "MEDIUMINT UNSIGNED" })
280 : OUString{ "MEDIUMINT" };
281 case MYSQL_TYPE_DATE:
282 return OUString{ "DATE" };
283 case MYSQL_TYPE_TIME:
284 return OUString{ "TIME" };
285 case MYSQL_TYPE_DATETIME:
286 return OUString{ "DATETIME" };
287 case MYSQL_TYPE_TINY_BLOB:
289 return OUString{ "TINYBLOB" };
291 case MYSQL_TYPE_MEDIUM_BLOB:
293 return OUString{ "MEDIUMBLOB" };
295 case MYSQL_TYPE_LONG_BLOB:
297 return OUString{ "LONGBLOB" };
299 case MYSQL_TYPE_BLOB:
301 return OUString{ "BLOB" };
303 case MYSQL_TYPE_VARCHAR:
304 case MYSQL_TYPE_VAR_STRING:
305 if (flags & ENUM_FLAG)
307 return OUString{ "ENUM" };
309 if (flags & SET_FLAG)
311 return OUString{ "SET" };
313 return OUString{ "VARCHAR" };
314 case MYSQL_TYPE_STRING:
315 if (flags & ENUM_FLAG)
317 return OUString{ "ENUM" };
319 if (flags & SET_FLAG)
321 return OUString{ "SET" };
323 return OUString{ "CHAR" };
324 case MYSQL_TYPE_YEAR:
325 return OUString{ "YEAR" };
326 case MYSQL_TYPE_GEOMETRY:
327 return OUString{ "GEOMETRY" };
328 default:
329 return OUString{ "UNKNOWN" };
333 OUString convert(const ::std::string& _string, const rtl_TextEncoding encoding)
335 return OUString(_string.c_str(), _string.size(), encoding);
338 } /* namespace */
340 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */