bump product version to 6.3.0.0.beta1
[LibreOffice.git] / connectivity / source / drivers / mysqlc / mysqlc_general.cxx
blob75b97b4028de272605c251c5ee72293ebc4bdebc
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"
20 #include "mysqlc_resultsetmetadata.hxx"
22 #include <sal/log.hxx>
23 #include <rtl/ustring.hxx>
25 using com::sun::star::sdbc::SQLException;
27 using com::sun::star::uno::Any;
28 using com::sun::star::uno::Reference;
29 using com::sun::star::uno::XInterface;
31 using namespace rtl;
33 namespace mysqlc_sdbc_driver
35 void allocateSqlVar(void** mem, enum_field_types eType, unsigned nSize)
37 assert(mem);
38 switch (eType)
40 case MYSQL_TYPE_LONG:
41 case MYSQL_TYPE_INT24:
42 *mem = malloc(sizeof(sal_Int32));
43 break;
44 case MYSQL_TYPE_SHORT:
45 *mem = malloc(sizeof(sal_Int16));
46 break;
47 case MYSQL_TYPE_BIT:
48 case MYSQL_TYPE_TINY:
49 *mem = malloc(sizeof(sal_Int8));
50 break;
51 case MYSQL_TYPE_LONGLONG:
52 *mem = malloc(sizeof(sal_Int64));
53 break;
54 case MYSQL_TYPE_FLOAT:
55 *mem = malloc(sizeof(float));
56 break;
57 case MYSQL_TYPE_DOUBLE:
58 *mem = malloc(sizeof(double));
59 break;
60 case MYSQL_TYPE_DATE:
61 case MYSQL_TYPE_TIME:
62 case MYSQL_TYPE_DATETIME:
63 case MYSQL_TYPE_TIMESTAMP:
64 case MYSQL_TYPE_YEAR: // FIXME below
65 case MYSQL_TYPE_NEWDATE:
66 case MYSQL_TYPE_ENUM:
67 case MYSQL_TYPE_SET:
68 case MYSQL_TYPE_GEOMETRY:
69 *mem = malloc(sizeof(MYSQL_TIME));
70 break;
71 case MYSQL_TYPE_STRING:
72 case MYSQL_TYPE_VAR_STRING:
73 case MYSQL_TYPE_VARCHAR:
74 case MYSQL_TYPE_DECIMAL:
75 case MYSQL_TYPE_NEWDECIMAL:
76 *mem = malloc(sizeof(char) * nSize);
77 break;
78 case MYSQL_TYPE_NULL:
79 case MYSQL_TYPE_BLOB:
80 case MYSQL_TYPE_TINY_BLOB:
81 case MYSQL_TYPE_MEDIUM_BLOB:
82 case MYSQL_TYPE_LONG_BLOB:
83 *mem = nullptr;
84 break;
85 default:
86 SAL_WARN("connectivity.mysqlc", "unknown enum_field_type");
90 void throwFeatureNotImplementedException(const sal_Char* _pAsciiFeatureName,
91 const css::uno::Reference<XInterface>& _rxContext)
93 const OUString sMessage
94 = OUString::createFromAscii(_pAsciiFeatureName) + ": feature not implemented.";
95 throw SQLException(sMessage, _rxContext, "HYC00", 0, Any());
98 void throwInvalidArgumentException(const sal_Char* _pAsciiFeatureName,
99 const css::uno::Reference<XInterface>& _rxContext)
101 const OUString sMessage
102 = OUString::createFromAscii(_pAsciiFeatureName) + ": invalid arguments.";
103 throw SQLException(sMessage, _rxContext, "HYC00", 0, Any());
106 void throwSQLExceptionWithMsg(const char* msg, unsigned int errorNum,
107 const css::uno::Reference<css::uno::XInterface>& _context,
108 const rtl_TextEncoding encoding)
110 OString errorMsg{ msg };
111 // TODO error code?
112 throw SQLException(OStringToOUString(errorMsg, encoding), _context, OUString(), errorNum,
113 Any());
116 sal_Int32 mysqlToOOOType(int eType, int charsetnr) noexcept
118 // charset number 63 indicates binary
119 switch (eType)
121 case MYSQL_TYPE_BIT:
122 return css::sdbc::DataType::VARCHAR;
124 case MYSQL_TYPE_TINY:
125 return css::sdbc::DataType::TINYINT;
127 case MYSQL_TYPE_SHORT:
128 return css::sdbc::DataType::SMALLINT;
130 case MYSQL_TYPE_INT24:
131 case MYSQL_TYPE_LONG:
132 return css::sdbc::DataType::INTEGER;
134 case MYSQL_TYPE_LONGLONG:
135 return css::sdbc::DataType::BIGINT;
137 case MYSQL_TYPE_FLOAT:
138 return css::sdbc::DataType::REAL;
140 case MYSQL_TYPE_DOUBLE:
141 return css::sdbc::DataType::DOUBLE;
143 case MYSQL_TYPE_DECIMAL:
144 case MYSQL_TYPE_NEWDECIMAL:
145 return css::sdbc::DataType::DECIMAL;
147 case MYSQL_TYPE_STRING:
148 if (charsetnr == 63)
149 return css::sdbc::DataType::BINARY;
150 return css::sdbc::DataType::CHAR;
152 case MYSQL_TYPE_ENUM:
153 case MYSQL_TYPE_SET:
154 case MYSQL_TYPE_VAR_STRING:
155 if (charsetnr == 63)
156 return css::sdbc::DataType::VARBINARY;
157 return css::sdbc::DataType::VARCHAR;
159 case MYSQL_TYPE_BLOB:
160 if (charsetnr == 63)
161 return css::sdbc::DataType::LONGVARBINARY;
162 return css::sdbc::DataType::LONGVARCHAR;
164 case MYSQL_TYPE_TIMESTAMP:
165 case MYSQL_TYPE_DATETIME:
166 return css::sdbc::DataType::TIMESTAMP;
168 case MYSQL_TYPE_DATE:
169 return css::sdbc::DataType::DATE;
171 case MYSQL_TYPE_TIME:
172 return css::sdbc::DataType::TIME;
174 case MYSQL_TYPE_GEOMETRY:
175 return css::sdbc::DataType::VARCHAR;
177 case MYSQL_TYPE_NULL:
178 return css::sdbc::DataType::SQLNULL;
181 OSL_FAIL("mysqlToOOOType: unhandled case, falling back to VARCHAR");
182 return css::sdbc::DataType::VARCHAR;
185 sal_Int32 mysqlStrToOOOType(const OUString& sType)
187 // TODO other types.
188 if (sType.equalsIgnoreAsciiCase("tiny") || sType.equalsIgnoreAsciiCase("tinyint"))
189 return css::sdbc::DataType::TINYINT;
190 if (sType.equalsIgnoreAsciiCase("smallint") || sType.equalsIgnoreAsciiCase("mediumint"))
191 return css::sdbc::DataType::SMALLINT;
192 if (sType.equalsIgnoreAsciiCase("longtext"))
193 return css::sdbc::DataType::LONGVARCHAR;
194 if (sType.equalsIgnoreAsciiCase("int"))
195 return css::sdbc::DataType::INTEGER;
196 if (sType.equalsIgnoreAsciiCase("varchar") || sType.equalsIgnoreAsciiCase("set")
197 || sType.equalsIgnoreAsciiCase("enum"))
198 return css::sdbc::DataType::VARCHAR;
199 if (sType.equalsIgnoreAsciiCase("bigint"))
200 return css::sdbc::DataType::BIGINT;
201 if (sType.equalsIgnoreAsciiCase("blob") || sType.equalsIgnoreAsciiCase("longblob"))
202 return css::sdbc::DataType::BLOB;
203 if (sType.equalsIgnoreAsciiCase("varbinary"))
204 return css::sdbc::DataType::VARBINARY;
205 if (sType.equalsIgnoreAsciiCase("text") || sType.equalsIgnoreAsciiCase("char"))
206 return css::sdbc::DataType::CHAR;
207 if (sType.equalsIgnoreAsciiCase("binary"))
208 return css::sdbc::DataType::BINARY;
209 if (sType.equalsIgnoreAsciiCase("time"))
210 return css::sdbc::DataType::TIME;
211 if (sType.equalsIgnoreAsciiCase("date"))
212 return css::sdbc::DataType::DATE;
213 if (sType.equalsIgnoreAsciiCase("datetime") || sType.equalsIgnoreAsciiCase("timestamp"))
214 return css::sdbc::DataType::TIMESTAMP;
215 if (sType.equalsIgnoreAsciiCase("decimal"))
216 return css::sdbc::DataType::DECIMAL;
217 if (sType.equalsIgnoreAsciiCase("real") || sType.equalsIgnoreAsciiCase("float"))
218 return css::sdbc::DataType::REAL;
219 if (sType.equalsIgnoreAsciiCase("double"))
220 return css::sdbc::DataType::DOUBLE;
221 if (sType.equalsIgnoreAsciiCase("bit") || sType.equalsIgnoreAsciiCase("bool")
222 || sType.equalsIgnoreAsciiCase("boolean"))
223 return css::sdbc::DataType::BOOLEAN;
224 OSL_FAIL("Unknown type name from string, failing back to varchar.");
225 return css::sdbc::DataType::VARCHAR;
228 OUString mysqlTypeToStr(unsigned type, unsigned flags)
230 bool isUnsigned = (flags & UNSIGNED_FLAG) != 0;
231 bool isZerofill = (flags & ZEROFILL_FLAG) != 0;
232 switch (type)
234 case MYSQL_TYPE_BIT:
235 return OUString{ "BIT" };
236 case MYSQL_TYPE_DECIMAL:
237 case MYSQL_TYPE_NEWDECIMAL:
238 return isUnsigned ? (isZerofill ? OUString{ "DECIMAL UNSIGNED ZEROFILL" }
239 : OUString{ "DECIMAL UNSIGNED" })
240 : OUString{ "DECIMAL" };
241 case MYSQL_TYPE_TINY:
242 return isUnsigned ? (isZerofill ? OUString{ "TINYINT UNSIGNED ZEROFILL" }
243 : OUString{ "TINYINT UNSIGNED" })
244 : OUString{ "TINYINT" };
245 case MYSQL_TYPE_SHORT:
246 return isUnsigned ? (isZerofill ? OUString{ "SMALLINT UNSIGNED ZEROFILL" }
247 : OUString{ "SMALLINT UNSIGNED" })
248 : OUString{ "SMALLINT" };
249 case MYSQL_TYPE_LONG:
250 return isUnsigned ? (isZerofill ? OUString{ "INT UNSIGNED ZEROFILL" }
251 : OUString{ "INT UNSIGNED" })
252 : OUString{ "INT" };
253 case MYSQL_TYPE_FLOAT:
254 return isUnsigned ? (isZerofill ? OUString{ "FLOAT UNSIGNED ZEROFILL" }
255 : OUString{ "FLOAT UNSIGNED" })
256 : OUString{ "FLOAT" };
257 case MYSQL_TYPE_DOUBLE:
258 return isUnsigned ? (isZerofill ? OUString{ "DOUBLE UNSIGNED ZEROFILL" }
259 : OUString{ "DOUBLE UNSIGNED" })
260 : OUString{ "DOUBLE" };
261 case MYSQL_TYPE_NULL:
262 return OUString{ "NULL" };
263 case MYSQL_TYPE_TIMESTAMP:
264 return OUString{ "TIMESTAMP" };
265 case MYSQL_TYPE_LONGLONG:
266 return isUnsigned ? (isZerofill ? OUString{ "BIGINT UNSIGNED ZEROFILL" }
267 : OUString{ "BIGINT UNSIGNED" })
268 : OUString{ "BIGINT" };
269 case MYSQL_TYPE_INT24:
270 return isUnsigned ? (isZerofill ? OUString{ "MEDIUMINT UNSIGNED ZEROFILL" }
271 : OUString{ "MEDIUMINT UNSIGNED" })
272 : OUString{ "MEDIUMINT" };
273 case MYSQL_TYPE_DATE:
274 return OUString{ "DATE" };
275 case MYSQL_TYPE_TIME:
276 return OUString{ "TIME" };
277 case MYSQL_TYPE_DATETIME:
278 return OUString{ "DATETIME" };
279 case MYSQL_TYPE_TINY_BLOB:
281 return OUString{ "TINYBLOB" };
283 case MYSQL_TYPE_MEDIUM_BLOB:
285 return OUString{ "MEDIUMBLOB" };
287 case MYSQL_TYPE_LONG_BLOB:
289 return OUString{ "LONGBLOB" };
291 case MYSQL_TYPE_BLOB:
293 return OUString{ "BLOB" };
295 case MYSQL_TYPE_VARCHAR:
296 case MYSQL_TYPE_VAR_STRING:
297 if (flags & ENUM_FLAG)
299 return OUString{ "ENUM" };
301 if (flags & SET_FLAG)
303 return OUString{ "SET" };
305 return OUString{ "VARCHAR" };
306 case MYSQL_TYPE_STRING:
307 if (flags & ENUM_FLAG)
309 return OUString{ "ENUM" };
311 if (flags & SET_FLAG)
313 return OUString{ "SET" };
315 return OUString{ "CHAR" };
316 case MYSQL_TYPE_YEAR:
317 return OUString{ "YEAR" };
318 case MYSQL_TYPE_GEOMETRY:
319 return OUString{ "GEOMETRY" };
320 default:
321 return OUString{ "UNKNOWN" };
325 OUString convert(const ::std::string& _string, const rtl_TextEncoding encoding)
327 return OUString(_string.c_str(), _string.size(), encoding);
330 } /* namespace */
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */