createtablesqlstatementbuilder.cpp 4.59 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5 6 7 8 9 10 11
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17 18 19 20 21 22
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 24 25 26 27
**
****************************************************************************/

#include "createtablesqlstatementbuilder.h"

Marco Bubke's avatar
Marco Bubke committed
28
namespace Sqlite {
29 30

CreateTableSqlStatementBuilder::CreateTableSqlStatementBuilder()
Marco Bubke's avatar
Marco Bubke committed
31
    : m_sqlStatementBuilder("CREATE $temporaryTABLE $ifNotExits$table($columnDefinitions)$withoutRowId")
32 33 34
{
}

Marco Bubke's avatar
Marco Bubke committed
35
void CreateTableSqlStatementBuilder::setTableName(Utils::SmallString &&tableName)
36
{
Marco Bubke's avatar
Marco Bubke committed
37
    m_sqlStatementBuilder.clear();
38

39
    this->m_tableName = std::move(tableName);
40 41
}

42 43
void CreateTableSqlStatementBuilder::addColumn(Utils::SmallString &&columnName,
                                               ColumnType columnType,
Marco Bubke's avatar
Marco Bubke committed
44
                                               Contraint constraint)
45
{
Marco Bubke's avatar
Marco Bubke committed
46
    m_sqlStatementBuilder.clear();
47

Marco Bubke's avatar
Marco Bubke committed
48
    m_columns.emplace_back(std::move(columnName), columnType, constraint);
49 50
}

51
void CreateTableSqlStatementBuilder::setColumns(const SqliteColumns &columns)
52
{
Marco Bubke's avatar
Marco Bubke committed
53
    m_sqlStatementBuilder.clear();
54

55
    m_columns = std::move(columns);
56 57 58 59
}

void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
{
60
    m_useWithoutRowId = useWithoutRowId;
61 62
}

Marco Bubke's avatar
Marco Bubke committed
63 64 65 66 67 68 69 70 71 72
void CreateTableSqlStatementBuilder::setUseIfNotExists(bool useIfNotExists)
{
    m_useIfNotExits = useIfNotExists;
}

void CreateTableSqlStatementBuilder::setUseTemporaryTable(bool useTemporaryTable)
{
    m_useTemporaryTable = useTemporaryTable;
}

73 74
void CreateTableSqlStatementBuilder::clear()
{
Marco Bubke's avatar
Marco Bubke committed
75
    m_sqlStatementBuilder.clear();
76
    m_columns.clear();
Marco Bubke's avatar
Marco Bubke committed
77 78
    m_tableName.clear();
    m_useWithoutRowId = false;
79 80 81 82
}

void CreateTableSqlStatementBuilder::clearColumns()
{
Marco Bubke's avatar
Marco Bubke committed
83
    m_sqlStatementBuilder.clear();
84
    m_columns.clear();
85 86
}

87
Utils::SmallStringView CreateTableSqlStatementBuilder::sqlStatement() const
88
{
Marco Bubke's avatar
Marco Bubke committed
89
    if (!m_sqlStatementBuilder.isBuild())
90 91
        bindAll();

Marco Bubke's avatar
Marco Bubke committed
92
    return m_sqlStatementBuilder.sqlStatement();
93 94 95 96
}

bool CreateTableSqlStatementBuilder::isValid() const
{
97
    return m_tableName.hasContent() && !m_columns.empty();
98 99 100 101
}

void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
{
102
    Utils::SmallStringVector columnDefinitionStrings;
103

104 105
    for (const SqliteColumn &columns : m_columns) {
        Utils::SmallString columnDefinitionString = {columns.name(), " ", columns.typeString()};
106

Marco Bubke's avatar
Marco Bubke committed
107 108 109 110 111
        switch (columns.constraint()) {
            case Contraint::PrimaryKey: columnDefinitionString.append(" PRIMARY KEY"); break;
            case Contraint::Unique: columnDefinitionString.append(" UNIQUE"); break;
            case Contraint::NoConstraint: break;
        }
112

113
        columnDefinitionStrings.push_back(columnDefinitionString);
114 115
    }

116
    m_sqlStatementBuilder.bind("$columnDefinitions", columnDefinitionStrings);
117 118 119 120
}

void CreateTableSqlStatementBuilder::bindAll() const
{
121
    m_sqlStatementBuilder.bind("$table", m_tableName.clone());
122

Marco Bubke's avatar
Marco Bubke committed
123 124
    bindTemporary();
    bindIfNotExists();
125
    bindColumnDefinitions();
Marco Bubke's avatar
Marco Bubke committed
126 127
    bindWithoutRowId();
}
128

Marco Bubke's avatar
Marco Bubke committed
129 130
void CreateTableSqlStatementBuilder::bindWithoutRowId() const
{
Marco Bubke's avatar
Marco Bubke committed
131
    if (m_useWithoutRowId)
132
        m_sqlStatementBuilder.bind("$withoutRowId", " WITHOUT ROWID");
133
    else
134
        m_sqlStatementBuilder.bindEmptyText("$withoutRowId");
135 136
}

Marco Bubke's avatar
Marco Bubke committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
void CreateTableSqlStatementBuilder::bindIfNotExists() const
{
    if (m_useIfNotExits)
        m_sqlStatementBuilder.bind("$ifNotExits", "IF NOT EXISTS ");
    else
        m_sqlStatementBuilder.bindEmptyText("$ifNotExits");
}

void CreateTableSqlStatementBuilder::bindTemporary() const
{
    if (m_useTemporaryTable)
        m_sqlStatementBuilder.bind("$temporary", "TEMPORARY ");
    else
        m_sqlStatementBuilder.bindEmptyText("$temporary");
}

Marco Bubke's avatar
Marco Bubke committed
153
} // namespace Sqlite