LibreOffice Module dbaccess (master) 1
utils.cxx
Go to the documentation of this file.
1
2/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3/*
4 * This file is part of the LibreOffice project.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 *
10 * This file incorporates work covered by the following license notice:
11 *
12 * Licensed to the Apache Software Foundation (ASF) under one or more
13 * contributor license agreements. See the NOTICE file distributed
14 * with this work for additional information regarding copyright
15 * ownership. The ASF licenses this file to you under the Apache
16 * License, Version 2.0 (the "License"); you may not use this file
17 * except in compliance with the License. You may obtain a copy of
18 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 */
20
21#include <comphelper/string.hxx>
24#include <sal/log.hxx>
25
26#include "utils.hxx"
27
28using namespace dbahsql;
29
30namespace
31{
32int getHexValue(sal_Unicode c)
33{
34 if (c >= '0' && c <= '9')
35 {
36 return c - '0';
37 }
38 else if (c >= 'A' && c <= 'F')
39 {
40 return c - 'A' + 10;
41 }
42 else if (c >= 'a' && c <= 'f')
43 {
44 return c - 'a' + 10;
45 }
46 else
47 {
48 return -1;
49 }
50}
51
52} // unnamed namespace
53
54//Convert ascii escaped unicode to utf-8
55OUString utils::convertToUTF8(std::string_view original)
56{
57 OUString res = OStringToOUString(original, RTL_TEXTENCODING_UTF8);
58 for (sal_Int32 i = 0;;)
59 {
60 i = res.indexOf("\\u", i);
61 if (i == -1)
62 {
63 break;
64 }
65 i += 2;
66 if (res.getLength() - i >= 4)
67 {
68 bool escape = true;
69 sal_Unicode c = 0;
70 for (sal_Int32 j = 0; j != 4; ++j)
71 {
72 auto const n = getHexValue(res[i + j]);
73 if (n == -1)
74 {
75 escape = false;
76 break;
77 }
78 c = (c << 4) | n;
79 }
80 if (escape)
81 {
82 i -= 2;
83 res = res.replaceAt(i, 6, rtl::OUStringChar(c));
84 ++i;
85 }
86 }
87 }
88 return res;
89}
90
91OUString utils::getTableNameFromStmt(std::u16string_view sSql)
92{
93 auto stmtComponents = comphelper::string::split(sSql, sal_Unicode(u' '));
94 assert(stmtComponents.size() > 2);
95 auto wordIter = stmtComponents.begin();
96
97 if (*wordIter == "CREATE" || *wordIter == "ALTER")
98 ++wordIter;
99 if (*wordIter == "CACHED")
100 ++wordIter;
101 if (*wordIter == "TABLE")
102 ++wordIter;
103
104 // it may contain spaces if it's put into apostrophes.
105 if (wordIter->indexOf("\"") >= 0)
106 {
107 size_t nAposBegin = sSql.find('"');
108 size_t nAposEnd = nAposBegin;
109 bool bProperEndAposFound = false;
110 while (!bProperEndAposFound)
111 {
112 nAposEnd = sSql.find('"', nAposEnd + 1);
113 if (nAposEnd == std::u16string_view::npos)
114 {
115 SAL_WARN("dbaccess", "no matching \"");
116 return OUString();
117 }
118 if (sSql[nAposEnd - 1] != u'\\')
119 bProperEndAposFound = true;
120 }
121 std::u16string_view result = sSql.substr(nAposBegin, nAposEnd - nAposBegin + 1);
122 return OUString(result);
123 }
124
125 // next word is the table's name
126 // it might stuck together with the column definitions.
127 sal_Int32 nParenPos = wordIter->indexOf("(");
128 if (nParenPos > 0)
129 return wordIter->copy(0, nParenPos);
130 else
131 return *wordIter;
132}
133
135{
136 if (sName.size() > 30) // Firebird limitation
137 {
138 static constexpr OUStringLiteral NAME_TOO_LONG
139 = u"Firebird 3 doesn't support object (table, field) names "
140 "of more than 30 characters; please shorten your object "
141 "names in the original file and try again.";
143 ::comphelper::getProcessComponentContext());
144 }
145}
146
147/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OptionalString sName
float u
sal_Int64 n
#define SAL_WARN(area, stream)
std::vector< OUString > split(std::u16string_view rStr, sal_Unicode cSeparator)
OUString getTableNameFromStmt(std::u16string_view sSql)
Definition: utils.cxx:91
OUString convertToUTF8(std::string_view original)
Definition: utils.cxx:55
void ensureFirebirdTableLength(std::u16string_view sName)
Definition: utils.cxx:134
void throwGenericSQLException(const OUString &_rMsg, const css::uno::Reference< css::uno::XInterface > &_rxSource)
int i
sal_uInt16 sal_Unicode
Any result