nss: upgrade to release 3.73
[LibreOffice.git] / dbaccess / source / filter / hsqldb / utils.cxx
blob3dc8f2e55b1625f4b3907f9acc61668b96f8bca2
2 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
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>
25 #include "utils.hxx"
27 using namespace dbahsql;
29 namespace
31 int getHexValue(sal_Unicode c)
33 if (c >= '0' && c <= '9')
35 return c - '0';
37 else if (c >= 'A' && c <= 'F')
39 return c - 'A' + 10;
41 else if (c >= 'a' && c <= 'f')
43 return c - 'a' + 10;
45 else
47 return -1;
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);
60 if (i == -1)
62 break;
64 i += 2;
65 if (res.getLength() - i >= 4)
67 bool escape = true;
68 sal_Unicode c = 0;
69 for (sal_Int32 j = 0; j != 4; ++j)
71 auto const n = getHexValue(res[i + j]);
72 if (n == -1)
74 escape = false;
75 break;
77 c = (c << 4) | n;
79 if (escape)
81 i -= 2;
82 res = res.replaceAt(i, 6, OUString(c));
83 ++i;
87 return res;
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")
97 ++wordIter;
98 if (*wordIter == "CACHED")
99 ++wordIter;
100 if (*wordIter == "TABLE")
101 ++wordIter;
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);
116 return result;
119 // next word is the table's name
120 // it might stuck together with the column definitions.
121 sal_Int32 nParenPos = wordIter->indexOf("(");
122 if (nParenPos > 0)
123 return wordIter->copy(0, nParenPos);
124 else
125 return *wordIter;
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: */