summaryrefslogtreecommitdiffstats
path: root/connectivity/source/drivers/mysqlc/mysqlc_general.cxx
blob: bc57770413f5c6c8b8d210e042dec21d5eda5e70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */
#include "mysqlc_general.hxx"

#include <sal/log.hxx>
#include <rtl/ustring.hxx>
#include <o3tl/string_view.hxx>

#include <com/sun/star/sdbc/DataType.hpp>

using com::sun::star::sdbc::SQLException;

using com::sun::star::uno::Any;
using com::sun::star::uno::Reference;
using com::sun::star::uno::XInterface;

using namespace rtl;

namespace mysqlc_sdbc_driver
{
void allocateSqlVar(void** mem, enum_field_types eType, unsigned nSize)
{
    assert(mem);
    switch (eType)
    {
        case MYSQL_TYPE_LONG:
        case MYSQL_TYPE_INT24:
            *mem = malloc(sizeof(sal_Int32));
            break;
        case MYSQL_TYPE_SHORT:
        case MYSQL_TYPE_YEAR:
            *mem = malloc(sizeof(sal_Int16));
            break;
        case MYSQL_TYPE_BIT:
        case MYSQL_TYPE_TINY:
            *mem = malloc(sizeof(sal_Int8));
            break;
        case MYSQL_TYPE_LONGLONG:
            *mem = malloc(sizeof(sal_Int64));
            break;
        case MYSQL_TYPE_FLOAT:
            *mem = malloc(sizeof(float));
            break;
        case MYSQL_TYPE_DOUBLE:
            *mem = malloc(sizeof(double));
            break;
        case MYSQL_TYPE_DATE:
        case MYSQL_TYPE_TIME:
        case MYSQL_TYPE_DATETIME:
        case MYSQL_TYPE_TIMESTAMP:
        case MYSQL_TYPE_NEWDATE:
        case MYSQL_TYPE_ENUM:
        case MYSQL_TYPE_SET:
        case MYSQL_TYPE_GEOMETRY:
            *mem = malloc(sizeof(MYSQL_TIME));
            break;
        case MYSQL_TYPE_STRING:
        case MYSQL_TYPE_VAR_STRING:
        case MYSQL_TYPE_VARCHAR:
        case MYSQL_TYPE_DECIMAL:
        case MYSQL_TYPE_NEWDECIMAL:
            *mem = malloc(sizeof(char) * nSize);
            break;
        case MYSQL_TYPE_NULL:
        case MYSQL_TYPE_BLOB:
        case MYSQL_TYPE_TINY_BLOB:
        case MYSQL_TYPE_MEDIUM_BLOB:
        case MYSQL_TYPE_LONG_BLOB:
            *mem = nullptr;
            break;
        default:
            SAL_WARN("connectivity.mysqlc", "unknown enum_field_type");
    }
}

void throwFeatureNotImplementedException(const char* _pAsciiFeatureName,
                                         const css::uno::Reference<XInterface>& _rxContext)
{
    const OUString sMessage
        = OUString::createFromAscii(_pAsciiFeatureName) + ": feature not implemented.";
    throw SQLException(sMessage, _rxContext, "HYC00", 0, Any());
}

void throwInvalidArgumentException(const char* _pAsciiFeatureName,
                                   const css::uno::Reference<XInterface>& _rxContext)
{
    const OUString sMessage
        = OUString::createFromAscii(_pAsciiFeatureName) + ": invalid arguments.";
    throw SQLException(sMessage, _rxContext, "HYC00", 0, Any());
}

void throwSQLExceptionWithMsg(const char* msg, const char* SQLSTATE, unsigned int errorNum,
                              const css::uno::Reference<css::uno::XInterface>& _context,
                              const rtl_TextEncoding encoding)
{
    OString errorMsg{ msg };
    throwSQLExceptionWithMsg(OStringToOUString(errorMsg, encoding), SQLSTATE, errorNum, _context);
}

void throwSQLExceptionWithMsg(const OUString& msg, const char* SQLSTATE, unsigned int errorNum,
                              const css::uno::Reference<css::uno::XInterface>& _context)
{
    throw SQLException(msg, _context, OStringToOUString(SQLSTATE, RTL_TEXTENCODING_ASCII_US),
                       errorNum, Any());
}

sal_Int32 mysqlToOOOType(int eType, int charsetnr) noexcept
{
    // charset number 63 indicates binary
    switch (eType)
    {
        case MYSQL_TYPE_BIT:
            return css::sdbc::DataType::BIT;

        case MYSQL_TYPE_TINY:
            return css::sdbc::DataType::TINYINT;

        case MYSQL_TYPE_SHORT:
        case MYSQL_TYPE_YEAR:
            return css::sdbc::DataType::SMALLINT;

        case MYSQL_TYPE_INT24:
        case MYSQL_TYPE_LONG:
            return css::sdbc::DataType::INTEGER;

        case MYSQL_TYPE_LONGLONG:
            return css::sdbc::DataType::BIGINT;

        case MYSQL_TYPE_FLOAT:
            return css::sdbc::DataType::REAL;

        case MYSQL_TYPE_DOUBLE:
            return css::sdbc::DataType::DOUBLE;

        case MYSQL_TYPE_DECIMAL:
        case MYSQL_TYPE_NEWDECIMAL:
            return css::sdbc::DataType::DECIMAL;

        case MYSQL_TYPE_STRING:
            if (charsetnr == 63)
                return css::sdbc::DataType::BINARY;
            return css::sdbc::DataType::CHAR;

        case MYSQL_TYPE_ENUM:
        case MYSQL_TYPE_SET:
        case MYSQL_TYPE_VAR_STRING:
            if (charsetnr == 63)
                return css::sdbc::DataType::VARBINARY;
            return css::sdbc::DataType::VARCHAR;

        case MYSQL_TYPE_BLOB:
        case MYSQL_TYPE_TINY_BLOB:
        case MYSQL_TYPE_MEDIUM_BLOB:
        case MYSQL_TYPE_LONG_BLOB:
            if (charsetnr == 63)
                return css::sdbc::DataType::LONGVARBINARY;
            return css::sdbc::DataType::LONGVARCHAR;

        case MYSQL_TYPE_TIMESTAMP:
        case MYSQL_TYPE_DATETIME:
            return css::sdbc::DataType::TIMESTAMP;

        case MYSQL_TYPE_DATE:
            return css::sdbc::DataType::DATE;

        case MYSQL_TYPE_TIME:
            return css::sdbc::DataType::TIME;

        case MYSQL_TYPE_GEOMETRY:
            return css::sdbc::DataType::VARCHAR;

        case MYSQL_TYPE_NULL:
            return css::sdbc::DataType::SQLNULL;
    }

    OSL_FAIL("mysqlToOOOType: unhandled case, falling back to VARCHAR");
    return css::sdbc::DataType::VARCHAR;
}

sal_Int32 mysqlStrToOOOType(std::u16string_view sType)
{
    // TODO other types.
    if (o3tl::equalsIgnoreAsciiCase(sType, u"tiny")
        || o3tl::equalsIgnoreAsciiCase(sType, u"tinyint"))
        return css::sdbc::DataType::TINYINT;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"smallint")
        || o3tl::equalsIgnoreAsciiCase(sType, u"year"))
        return css::sdbc::DataType::SMALLINT;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"int")
        || o3tl::equalsIgnoreAsciiCase(sType, u"mediumint"))
        return css::sdbc::DataType::INTEGER;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"varchar") || o3tl::equalsIgnoreAsciiCase(sType, u"set")
        || o3tl::equalsIgnoreAsciiCase(sType, u"enum"))
        return css::sdbc::DataType::VARCHAR;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"bigint"))
        return css::sdbc::DataType::BIGINT;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"blob")
        || o3tl::equalsIgnoreAsciiCase(sType, u"longblob")
        || o3tl::equalsIgnoreAsciiCase(sType, u"tinyblob")
        || o3tl::equalsIgnoreAsciiCase(sType, u"mediumblob"))
        return css::sdbc::DataType::BLOB;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"varbinary"))
        return css::sdbc::DataType::VARBINARY;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"char"))
        return css::sdbc::DataType::CHAR;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"tinytext"))
        return css::sdbc::DataType::VARCHAR;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"text"))
        return css::sdbc::DataType::LONGVARCHAR;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"mediumtext")
        || o3tl::equalsIgnoreAsciiCase(sType, u"longtext"))
        return css::sdbc::DataType::CLOB;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"binary"))
        return css::sdbc::DataType::BINARY;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"time"))
        return css::sdbc::DataType::TIME;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"date"))
        return css::sdbc::DataType::DATE;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"datetime")
        || o3tl::equalsIgnoreAsciiCase(sType, u"timestamp"))
        return css::sdbc::DataType::TIMESTAMP;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"decimal"))
        return css::sdbc::DataType::DECIMAL;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"real") || o3tl::equalsIgnoreAsciiCase(sType, u"float"))
        return css::sdbc::DataType::REAL;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"double"))
        return css::sdbc::DataType::DOUBLE;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"bit"))
        return css::sdbc::DataType::BIT;
    if (o3tl::equalsIgnoreAsciiCase(sType, u"bool")
        || o3tl::equalsIgnoreAsciiCase(sType, u"boolean"))
        return css::sdbc::DataType::BOOLEAN;
    OSL_FAIL("Unknown type name from string, failing back to varchar.");
    return css::sdbc::DataType::VARCHAR;
}

OUString mysqlTypeToStr(unsigned type, unsigned flags)
{
    bool isUnsigned = (flags & UNSIGNED_FLAG) != 0;
    bool isZerofill = (flags & ZEROFILL_FLAG) != 0;
    switch (type)
    {
        case MYSQL_TYPE_BIT:
            return OUString{ "BIT" };
        case MYSQL_TYPE_DECIMAL:
        case MYSQL_TYPE_NEWDECIMAL:
            return isUnsigned ? (isZerofill ? OUString{ "DECIMAL UNSIGNED ZEROFILL" }
                                            : OUString{ "DECIMAL UNSIGNED" })
                              : OUString{ "DECIMAL" };
        case MYSQL_TYPE_TINY:
            return isUnsigned ? (isZerofill ? OUString{ "TINYINT UNSIGNED ZEROFILL" }
                                            : OUString{ "TINYINT UNSIGNED" })
                              : OUString{ "TINYINT" };
        case MYSQL_TYPE_SHORT:
            return isUnsigned ? (isZerofill ? OUString{ "SMALLINT UNSIGNED ZEROFILL" }
                                            : OUString{ "SMALLINT UNSIGNED" })
                              : OUString{ "SMALLINT" };
        case MYSQL_TYPE_LONG:
            return isUnsigned ? (isZerofill ? OUString{ "INT UNSIGNED ZEROFILL" }
                                            : OUString{ "INT UNSIGNED" })
                              : OUString{ "INT" };
        case MYSQL_TYPE_FLOAT:
            return isUnsigned ? (isZerofill ? OUString{ "FLOAT UNSIGNED ZEROFILL" }
                                            : OUString{ "FLOAT UNSIGNED" })
                              : OUString{ "FLOAT" };
        case MYSQL_TYPE_DOUBLE:
            return isUnsigned ? (isZerofill ? OUString{ "DOUBLE UNSIGNED ZEROFILL" }
                                            : OUString{ "DOUBLE UNSIGNED" })
                              : OUString{ "DOUBLE" };
        case MYSQL_TYPE_NULL:
            return OUString{ "NULL" };
        case MYSQL_TYPE_TIMESTAMP:
            return OUString{ "TIMESTAMP" };
        case MYSQL_TYPE_LONGLONG:
            return isUnsigned ? (isZerofill ? OUString{ "BIGINT UNSIGNED ZEROFILL" }
                                            : OUString{ "BIGINT UNSIGNED" })
                              : OUString{ "BIGINT" };
        case MYSQL_TYPE_INT24:
            return isUnsigned ? (isZerofill ? OUString{ "MEDIUMINT UNSIGNED ZEROFILL" }
                                            : OUString{ "MEDIUMINT UNSIGNED" })
                              : OUString{ "MEDIUMINT" };
        case MYSQL_TYPE_DATE:
            return OUString{ "DATE" };
        case MYSQL_TYPE_TIME:
            return OUString{ "TIME" };
        case MYSQL_TYPE_DATETIME:
            return OUString{ "DATETIME" };
        case MYSQL_TYPE_TINY_BLOB:
        {
            return OUString{ "TINYBLOB" };
        }
        case MYSQL_TYPE_MEDIUM_BLOB:
        {
            return OUString{ "MEDIUMBLOB" };
        }
        case MYSQL_TYPE_LONG_BLOB:
        {
            return OUString{ "LONGBLOB" };
        }
        case MYSQL_TYPE_BLOB:
        {
            return OUString{ "BLOB" };
        }
        case MYSQL_TYPE_VARCHAR:
        case MYSQL_TYPE_VAR_STRING:
            if (flags & ENUM_FLAG)
            {
                return OUString{ "ENUM" };
            }
            if (flags & SET_FLAG)
            {
                return OUString{ "SET" };
            }
            return OUString{ "VARCHAR" };
        case MYSQL_TYPE_STRING:
            if (flags & ENUM_FLAG)
            {
                return OUString{ "ENUM" };
            }
            if (flags & SET_FLAG)
            {
                return OUString{ "SET" };
            }
            return OUString{ "CHAR" };
        case MYSQL_TYPE_YEAR:
            return OUString{ "YEAR" };
        case MYSQL_TYPE_GEOMETRY:
            return OUString{ "GEOMETRY" };
        default:
            return OUString{ "UNKNOWN" };
    }
}

OUString convert(const ::std::string& _string, const rtl_TextEncoding encoding)
{
    return OUString(_string.c_str(), _string.size(), encoding);
}

} /* namespace */

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */