summaryrefslogtreecommitdiffstats
path: root/dbaccess/source/filter/hsqldb/utils.cxx
blob: d8addd33623e4817fb873691eddd71be19fe5bdb (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

/* -*- 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 <comphelper/string.hxx>
#include <comphelper/processfactory.hxx>
#include <connectivity/dbexception.hxx>
#include <sal/log.hxx>

#include "utils.hxx"

using namespace dbahsql;

namespace
{
int getHexValue(sal_Unicode c)
{
    if (c >= '0' && c <= '9')
    {
        return c - '0';
    }
    else if (c >= 'A' && c <= 'F')
    {
        return c - 'A' + 10;
    }
    else if (c >= 'a' && c <= 'f')
    {
        return c - 'a' + 10;
    }
    else
    {
        return -1;
    }
}

} // unnamed namespace

//Convert ascii escaped unicode to utf-8
OUString utils::convertToUTF8(std::string_view original)
{
    OUString res = OStringToOUString(original, RTL_TEXTENCODING_UTF8);
    for (sal_Int32 i = 0;;)
    {
        i = res.indexOf("\\u", i);
        if (i == -1)
        {
            break;
        }
        i += 2;
        if (res.getLength() - i >= 4)
        {
            bool escape = true;
            sal_Unicode c = 0;
            for (sal_Int32 j = 0; j != 4; ++j)
            {
                auto const n = getHexValue(res[i + j]);
                if (n == -1)
                {
                    escape = false;
                    break;
                }
                c = (c << 4) | n;
            }
            if (escape)
            {
                i -= 2;
                res = res.replaceAt(i, 6, rtl::OUStringChar(c));
                ++i;
            }
        }
    }
    return res;
}

OUString utils::getTableNameFromStmt(std::u16string_view sSql)
{
    auto stmtComponents = comphelper::string::split(sSql, sal_Unicode(u' '));
    assert(stmtComponents.size() > 2);
    auto wordIter = stmtComponents.begin();

    if (*wordIter == "CREATE" || *wordIter == "ALTER")
        ++wordIter;
    if (*wordIter == "CACHED")
        ++wordIter;
    if (*wordIter == "TABLE")
        ++wordIter;

    // it may contain spaces if it's put into apostrophes.
    if (wordIter->indexOf("\"") >= 0)
    {
        size_t nAposBegin = sSql.find('"');
        size_t nAposEnd = nAposBegin;
        bool bProperEndAposFound = false;
        while (!bProperEndAposFound)
        {
            nAposEnd = sSql.find('"', nAposEnd + 1);
            if (nAposEnd == std::u16string_view::npos)
            {
                SAL_WARN("dbaccess", "no matching \"");
                return OUString();
            }
            if (sSql[nAposEnd - 1] != u'\\')
                bProperEndAposFound = true;
        }
        std::u16string_view result = sSql.substr(nAposBegin, nAposEnd - nAposBegin + 1);
        return OUString(result);
    }

    // next word is the table's name
    // it might stuck together with the column definitions.
    sal_Int32 nParenPos = wordIter->indexOf("(");
    if (nParenPos > 0)
        return wordIter->copy(0, nParenPos);
    else
        return *wordIter;
}

void utils::ensureFirebirdTableLength(std::u16string_view sName)
{
    if (sName.size() > 30) // Firebird limitation
    {
        static constexpr OUStringLiteral NAME_TOO_LONG
            = u"Firebird 3 doesn't support object (table, field) names "
              "of  more than 30 characters; please shorten your object "
              "names in the original file and try again.";
        dbtools::throwGenericSQLException(NAME_TOO_LONG,
                                          ::comphelper::getProcessComponentContext());
    }
}

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