createtablesqlstatementbuilder.cpp 3.62 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()
31
    : m_sqlStatementBuilder("CREATE TABLE IF NOT EXISTS $table($columnDefinitions)$withoutRowId"),
Marco Bubke's avatar
Marco Bubke committed
32
      m_useWithoutRowId(false)
33 34 35
{
}

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

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

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

49
    m_columns.emplace_back(std::move(columnName), columnType, isPrimaryKey);
50 51
}

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

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

void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
{
61
    m_useWithoutRowId = useWithoutRowId;
62 63 64 65
}

void CreateTableSqlStatementBuilder::clear()
{
Marco Bubke's avatar
Marco Bubke committed
66
    m_sqlStatementBuilder.clear();
67
    m_columns.clear();
Marco Bubke's avatar
Marco Bubke committed
68 69
    m_tableName.clear();
    m_useWithoutRowId = false;
70 71 72 73
}

void CreateTableSqlStatementBuilder::clearColumns()
{
Marco Bubke's avatar
Marco Bubke committed
74
    m_sqlStatementBuilder.clear();
75
    m_columns.clear();
76 77
}

78
Utils::SmallStringView CreateTableSqlStatementBuilder::sqlStatement() const
79
{
Marco Bubke's avatar
Marco Bubke committed
80
    if (!m_sqlStatementBuilder.isBuild())
81 82
        bindAll();

Marco Bubke's avatar
Marco Bubke committed
83
    return m_sqlStatementBuilder.sqlStatement();
84 85 86 87
}

bool CreateTableSqlStatementBuilder::isValid() const
{
88
    return m_tableName.hasContent() && !m_columns.empty();
89 90 91 92
}

void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
{
93
    Utils::SmallStringVector columnDefinitionStrings;
94

95 96
    for (const SqliteColumn &columns : m_columns) {
        Utils::SmallString columnDefinitionString = {columns.name(), " ", columns.typeString()};
97

98
        if (columns.isPrimaryKey())
99
            columnDefinitionString.append(" PRIMARY KEY");
100

101
        columnDefinitionStrings.push_back(columnDefinitionString);
102 103
    }

104
    m_sqlStatementBuilder.bind("$columnDefinitions", columnDefinitionStrings);
105 106 107 108
}

void CreateTableSqlStatementBuilder::bindAll() const
{
109
    m_sqlStatementBuilder.bind("$table", m_tableName.clone());
110 111 112

    bindColumnDefinitions();

Marco Bubke's avatar
Marco Bubke committed
113
    if (m_useWithoutRowId)
114
        m_sqlStatementBuilder.bind("$withoutRowId", " WITHOUT ROWID");
115
    else
116
        m_sqlStatementBuilder.bindEmptyText("$withoutRowId");
117 118
}

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