createtablesqlstatementbuilder.cpp 4.04 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 28 29
**
****************************************************************************/

#include "createtablesqlstatementbuilder.h"

#include "utf8stringvector.h"

Marco Bubke's avatar
Marco Bubke committed
30
namespace Sqlite {
31 32

CreateTableSqlStatementBuilder::CreateTableSqlStatementBuilder()
Marco Bubke's avatar
Marco Bubke committed
33 34
    : m_sqlStatementBuilder(Utf8StringLiteral("CREATE TABLE IF NOT EXISTS $table($columnDefinitions)$withoutRowId")),
      m_useWithoutRowId(false)
35 36 37 38 39
{
}

void CreateTableSqlStatementBuilder::setTable(const Utf8String &tableName)
{
Marco Bubke's avatar
Marco Bubke committed
40
    m_sqlStatementBuilder.clear();
41

Marco Bubke's avatar
Marco Bubke committed
42
    this->m_tableName = tableName;
43 44 45 46 47 48
}

void CreateTableSqlStatementBuilder::addColumnDefinition(const Utf8String &columnName,
                                                         ColumnType columnType,
                                                         bool isPrimaryKey)
{
Marco Bubke's avatar
Marco Bubke committed
49
    m_sqlStatementBuilder.clear();
50 51 52 53 54 55

    ColumnDefinition columnDefinition;
    columnDefinition.setName(columnName);
    columnDefinition.setType(columnType);
    columnDefinition.setIsPrimaryKey(isPrimaryKey);

Marco Bubke's avatar
Marco Bubke committed
56
    m_columnDefinitions.append(columnDefinition);
57 58 59 60
}

void CreateTableSqlStatementBuilder::setColumnDefinitions(const QVector<ColumnDefinition> &columnDefinitions)
{
Marco Bubke's avatar
Marco Bubke committed
61
    m_sqlStatementBuilder.clear();
62

Marco Bubke's avatar
Marco Bubke committed
63
    this->m_columnDefinitions = columnDefinitions;
64 65 66 67
}

void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
{
Marco Bubke's avatar
Marco Bubke committed
68
    this->m_useWithoutRowId = useWithoutRowId;
69 70 71 72
}

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

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

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

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

bool CreateTableSqlStatementBuilder::isValid() const
{
Marco Bubke's avatar
Marco Bubke committed
95
    return m_tableName.hasContent() && !m_columnDefinitions.isEmpty();
96 97 98 99 100 101
}

void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
{
    Utf8StringVector columnDefinitionStrings;

Marco Bubke's avatar
Marco Bubke committed
102
    foreach (const ColumnDefinition &columnDefinition, m_columnDefinitions) {
103 104 105 106 107 108 109 110
        Utf8String columnDefinitionString = columnDefinition.name() + Utf8StringLiteral(" ") + columnDefinition.typeString();

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

        columnDefinitionStrings.append(columnDefinitionString);
    }

Marco Bubke's avatar
Marco Bubke committed
111
    m_sqlStatementBuilder.bind(Utf8StringLiteral("$columnDefinitions"), columnDefinitionStrings);
112 113 114 115
}

void CreateTableSqlStatementBuilder::bindAll() const
{
Marco Bubke's avatar
Marco Bubke committed
116
    m_sqlStatementBuilder.bind(Utf8StringLiteral("$table"), m_tableName);
117 118 119

    bindColumnDefinitions();

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

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