createtablesqlstatementbuilder.cpp 3.91 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
void CreateTableSqlStatementBuilder::addColumnDefinition(Utils::SmallString &&columnName,
44 45 46
                                                         ColumnType columnType,
                                                         bool isPrimaryKey)
{
Marco Bubke's avatar
Marco Bubke committed
47
    m_sqlStatementBuilder.clear();
48 49

    ColumnDefinition columnDefinition;
50
    columnDefinition.setName(std::move(columnName));
51 52 53
    columnDefinition.setType(columnType);
    columnDefinition.setIsPrimaryKey(isPrimaryKey);

54
    m_columnDefinitions.push_back(columnDefinition);
55 56
}

57
void CreateTableSqlStatementBuilder::setColumnDefinitions(ColumnDefinitions &&columnDefinitions)
58
{
Marco Bubke's avatar
Marco Bubke committed
59
    m_sqlStatementBuilder.clear();
60

61
    m_columnDefinitions = std::move(columnDefinitions);
62 63 64 65
}

void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
{
66
    m_useWithoutRowId = useWithoutRowId;
67 68 69 70
}

void CreateTableSqlStatementBuilder::clear()
{
Marco Bubke's avatar
Marco Bubke committed
71 72 73 74
    m_sqlStatementBuilder.clear();
    m_columnDefinitions.clear();
    m_tableName.clear();
    m_useWithoutRowId = false;
75 76 77 78
}

void CreateTableSqlStatementBuilder::clearColumns()
{
Marco Bubke's avatar
Marco Bubke committed
79 80
    m_sqlStatementBuilder.clear();
    m_columnDefinitions.clear();
81 82
}

83
Utils::SmallStringView CreateTableSqlStatementBuilder::sqlStatement() const
84
{
Marco Bubke's avatar
Marco Bubke committed
85
    if (!m_sqlStatementBuilder.isBuild())
86 87
        bindAll();

Marco Bubke's avatar
Marco Bubke committed
88
    return m_sqlStatementBuilder.sqlStatement();
89 90 91 92
}

bool CreateTableSqlStatementBuilder::isValid() const
{
93
    return m_tableName.hasContent() && !m_columnDefinitions.empty();
94 95 96 97
}

void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
{
98
    Utils::SmallStringVector columnDefinitionStrings;
99

100 101
    for (const ColumnDefinition &columnDefinition : m_columnDefinitions) {
        Utils::SmallString columnDefinitionString = {columnDefinition.name(), " ", columnDefinition.typeString()};
102 103

        if (columnDefinition.isPrimaryKey())
104
            columnDefinitionString.append(" PRIMARY KEY");
105

106
        columnDefinitionStrings.push_back(columnDefinitionString);
107 108
    }

109
    m_sqlStatementBuilder.bind("$columnDefinitions", columnDefinitionStrings);
110 111 112 113
}

void CreateTableSqlStatementBuilder::bindAll() const
{
114
    m_sqlStatementBuilder.bind("$table", m_tableName.clone());
115 116 117

    bindColumnDefinitions();

Marco Bubke's avatar
Marco Bubke committed
118
    if (m_useWithoutRowId)
119
        m_sqlStatementBuilder.bind("$withoutRowId", " WITHOUT ROWID");
120
    else
121
        m_sqlStatementBuilder.bindEmptyText("$withoutRowId");
122 123
}

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