2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
4 * This file is part of the LibreOffice project.
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/.
10 * This file incorporates work covered by the following license notice:
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 .
21 #include <comphelper/string.hxx>
22 #include <comphelper/processfactory.hxx>
23 #include <connectivity/dbexception.hxx>
24 #include <sal/log.hxx>
28 using namespace dbahsql
;
32 int getHexValue(sal_Unicode c
)
34 if (c
>= '0' && c
<= '9')
38 else if (c
>= 'A' && c
<= 'F')
42 else if (c
>= 'a' && c
<= 'f')
52 } // unnamed namespace
54 //Convert ascii escaped unicode to utf-8
55 OUString
utils::convertToUTF8(std::string_view original
)
57 OUString res
= OStringToOUString(original
, RTL_TEXTENCODING_UTF8
);
58 for (sal_Int32 i
= 0;;)
60 i
= res
.indexOf("\\u", i
);
66 if (res
.getLength() - i
>= 4)
70 for (sal_Int32 j
= 0; j
!= 4; ++j
)
72 auto const n
= getHexValue(res
[i
+ j
]);
83 res
= res
.replaceAt(i
, 6, rtl::OUStringChar(c
));
91 OUString
utils::getTableNameFromStmt(std::u16string_view sSql
)
93 auto stmtComponents
= comphelper::string::split(sSql
, sal_Unicode(u
' '));
94 assert(stmtComponents
.size() > 2);
95 auto wordIter
= stmtComponents
.begin();
97 if (*wordIter
== "CREATE" || *wordIter
== "ALTER")
99 if (*wordIter
== "CACHED")
101 if (*wordIter
== "TABLE")
104 // it may contain spaces if it's put into apostrophes.
105 if (wordIter
->indexOf("\"") >= 0)
107 size_t nAposBegin
= sSql
.find('"');
108 size_t nAposEnd
= nAposBegin
;
109 bool bProperEndAposFound
= false;
110 while (!bProperEndAposFound
)
112 nAposEnd
= sSql
.find('"', nAposEnd
+ 1);
113 if (nAposEnd
== std::u16string_view::npos
)
115 SAL_WARN("dbaccess", "no matching \"");
118 if (sSql
[nAposEnd
- 1] != u
'\\')
119 bProperEndAposFound
= true;
121 std::u16string_view result
= sSql
.substr(nAposBegin
, nAposEnd
- nAposBegin
+ 1);
122 return OUString(result
);
125 // next word is the table's name
126 // it might stuck together with the column definitions.
127 sal_Int32 nParenPos
= wordIter
->indexOf("(");
129 return wordIter
->copy(0, nParenPos
);
134 void utils::ensureFirebirdTableLength(std::u16string_view sName
)
136 if (sName
.size() > 30) // Firebird limitation
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.";
142 dbtools::throwGenericSQLException(NAME_TOO_LONG
,
143 ::comphelper::getProcessComponentContext());
147 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */