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>
27 using namespace dbahsql
;
31 int getHexValue(sal_Unicode c
)
33 if (c
>= '0' && c
<= '9')
37 else if (c
>= 'A' && c
<= 'F')
41 else if (c
>= 'a' && c
<= 'f')
51 } // unnamed namespace
53 //Convert ascii escaped unicode to utf-8
54 OUString
utils::convertToUTF8(const OString
& original
)
56 OUString res
= OStringToOUString(original
, RTL_TEXTENCODING_UTF8
);
57 for (sal_Int32 i
= 0;;)
59 i
= res
.indexOf("\\u", i
);
65 if (res
.getLength() - i
>= 4)
69 for (sal_Int32 j
= 0; j
!= 4; ++j
)
71 auto const n
= getHexValue(res
[i
+ j
]);
82 res
= res
.replaceAt(i
, 6, OUString(c
));
90 OUString
utils::getTableNameFromStmt(const OUString
& sSql
)
92 auto stmtComponents
= comphelper::string::split(sSql
, sal_Unicode(u
' '));
93 assert(stmtComponents
.size() > 2);
94 auto wordIter
= stmtComponents
.begin();
96 if (*wordIter
== "CREATE" || *wordIter
== "ALTER")
98 if (*wordIter
== "CACHED")
100 if (*wordIter
== "TABLE")
103 // it may contain spaces if it's put into apostrophes.
104 if (wordIter
->indexOf("\"") >= 0)
106 sal_Int32 nAposBegin
= sSql
.indexOf("\"");
107 sal_Int32 nAposEnd
= nAposBegin
;
108 bool bProperEndAposFound
= false;
109 while (!bProperEndAposFound
)
111 nAposEnd
= sSql
.indexOf("\"", nAposEnd
+ 1);
112 if (sSql
[nAposEnd
- 1] != u
'\\')
113 bProperEndAposFound
= true;
115 OUString result
= sSql
.copy(nAposBegin
, nAposEnd
- nAposBegin
+ 1);
119 // next word is the table's name
120 // it might stuck together with the column definitions.
121 sal_Int32 nParenPos
= wordIter
->indexOf("(");
123 return wordIter
->copy(0, nParenPos
);
128 void utils::ensureFirebirdTableLength(const OUString
& sName
)
130 if (sName
.getLength() > 30) // Firebird limitation
132 constexpr char NAME_TOO_LONG
[] = "Firebird 3 doesn't support object (table, field) names "
133 "of more than 30 characters; please shorten your object "
134 "names in the original file and try again.";
135 dbtools::throwGenericSQLException(NAME_TOO_LONG
,
136 ::comphelper::getProcessComponentContext());
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */