summaryrefslogtreecommitdiff
path: root/dbaccess/source/filter/hsqldb/fbcreateparser.cxx
blob: 89ffe5e69833709c9e2b69becd01ce2410f4a021 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* -*- 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 "fbcreateparser.hxx"
#include "columndef.hxx"

#include <com/sun/star/sdbc/DataType.hpp>
#include <rtl/ustrbuf.hxx>

using namespace css::sdbc;

namespace
{
void lcl_appendWithSpace(OUStringBuffer& sBuff, const OUString& sStr)
{
    sBuff.append(" ");
    sBuff.append(sStr);
}

OUString lcl_DataTypetoFbTypeName(sal_Int32 eType)
{
    switch (eType)
    {
        case DataType::CHAR:
        case DataType::BINARY:
            return OUString("CHAR");
        case DataType::VARCHAR:
        case DataType::VARBINARY:
            return OUString("VARCHAR");
        case DataType::TINYINT: // no such type in Firebird
        case DataType::SMALLINT:
            return OUString("SMALLINT");
        case DataType::INTEGER:
            return OUString("INTEGER");
        case DataType::BIGINT:
            return OUString("BIGINT");
        case DataType::NUMERIC:
            return OUString("NUMERIC");
        case DataType::DECIMAL:
            return OUString("DECIMAL");
        case DataType::BOOLEAN:
            return OUString("BOOLEAN");
        case DataType::LONGVARCHAR:
        case DataType::LONGVARBINARY:
        case DataType::CLOB:
        case DataType::BLOB:
        case DataType::OTHER:
            return OUString("BLOB");
        case DataType::DATE:
            return OUString("DATE");
        case DataType::TIME:
            return OUString("TIME");
        case DataType::TIMESTAMP:
            return OUString("TIMESTAMP");
        case DataType::DOUBLE:
        case DataType::REAL:
            return OUString("DOUBLE PRECISION");
        default:
            assert(false);
            return OUString();
    }
}

OUString lcl_getTypeModifier(sal_Int32 eType)
{
    // TODO bind -9546 magic number to a common definition. It also appears
    // in the connectivity modul.
    switch (eType)
    {
        case DataType::CLOB:
        case DataType::LONGVARCHAR:
            return OUString("SUB_TYPE 1");
        case DataType::LONGVARBINARY:
            return OUString("SUB_TYPE -9546");
        case DataType::BINARY:
        case DataType::VARBINARY:
            return OUString("CHARACTER SET OCTETS");
        default:
            return OUString();
    }
}

} // unnamed namespace

namespace dbahsql
{
OUString FbCreateStmtParser::compose() const
{
    OUStringBuffer sSql("CREATE TABLE ");
    sSql.append(getTableName());

    lcl_appendWithSpace(sSql, "(");
    auto& rColumns = getColumnDef();
    auto columnIter = rColumns.cbegin();
    while (columnIter != rColumns.end())
    {
        lcl_appendWithSpace(sSql, columnIter->getName());
        lcl_appendWithSpace(sSql, lcl_DataTypetoFbTypeName(columnIter->getDataType()));

        std::vector<sal_Int32> params{ columnIter->getParams() };

        if (columnIter->getDataType() == DataType::NUMERIC
            || columnIter->getDataType() == DataType::DECIMAL)
        {
            // max precision is 18 here
            if (params.at(0) > 18)
                params[0] = 18;
        }

        // Firebird SQL dialect does not like parameters for TIMESTAMP
        if (params.size() > 0 && columnIter->getDataType() != DataType::TIMESTAMP)
        {
            sSql.append("(");
            auto it = params.cbegin();
            while (it != params.end())
            {
                sSql.append(OUString::number(*it));
                ++it;
                if (it != params.end())
                    sSql.append(",");
            }
            sSql.append(")"); // end of param declaration
        }

        // special modifiers here, based on type (e.g. charset, subtype)
        OUString sModifier = lcl_getTypeModifier(columnIter->getDataType());
        if (!sModifier.isEmpty())
            lcl_appendWithSpace(sSql, sModifier);

        if (columnIter->isAutoIncremental())
        {
            lcl_appendWithSpace(sSql, "GENERATED BY DEFAULT AS IDENTITY (START WITH ");

            // start with 0:
            // HSQLDB: first value will be 0.
            // Firebird: first value will be 1.
            sSql.append(columnIter->getStartValue() - 1);
            sSql.append(")");
        }
        else if (!columnIter->isNullable())
            lcl_appendWithSpace(sSql, "NOT NULL");

        if (columnIter->isPrimaryKey())
            lcl_appendWithSpace(sSql, "PRIMARY KEY");

        if (columnIter->isCaseInsensitive())
            lcl_appendWithSpace(sSql, "COLLATE UNICODE_CI");

        ++columnIter;
        if (columnIter != rColumns.end())
            sSql.append(",");
    }

    sSql.append(")"); // end of column declaration
    return sSql.makeStringAndClear();
}

} // dbahsql

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