2015-06-01 18:51:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** 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.
|
2015-06-01 18:51:55 +02:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "createtablesqlstatementbuilder.h"
|
|
|
|
|
|
|
|
|
|
#include "utf8stringvector.h"
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
namespace Sqlite {
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
CreateTableSqlStatementBuilder::CreateTableSqlStatementBuilder()
|
2017-07-26 16:02:24 +02:00
|
|
|
: m_sqlStatementBuilder(Utf8StringLiteral("CREATE TABLE IF NOT EXISTS $table($columnDefinitions)$withoutRowId")),
|
|
|
|
|
m_useWithoutRowId(false)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateTableSqlStatementBuilder::setTable(const Utf8String &tableName)
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatementBuilder.clear();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
this->m_tableName = tableName;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateTableSqlStatementBuilder::addColumnDefinition(const Utf8String &columnName,
|
|
|
|
|
ColumnType columnType,
|
|
|
|
|
bool isPrimaryKey)
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatementBuilder.clear();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
ColumnDefinition columnDefinition;
|
|
|
|
|
columnDefinition.setName(columnName);
|
|
|
|
|
columnDefinition.setType(columnType);
|
|
|
|
|
columnDefinition.setIsPrimaryKey(isPrimaryKey);
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
m_columnDefinitions.append(columnDefinition);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateTableSqlStatementBuilder::setColumnDefinitions(const QVector<ColumnDefinition> &columnDefinitions)
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatementBuilder.clear();
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
this->m_columnDefinitions = columnDefinitions;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateTableSqlStatementBuilder::setUseWithoutRowId(bool useWithoutRowId)
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
this->m_useWithoutRowId = useWithoutRowId;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateTableSqlStatementBuilder::clear()
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatementBuilder.clear();
|
|
|
|
|
m_columnDefinitions.clear();
|
|
|
|
|
m_tableName.clear();
|
|
|
|
|
m_useWithoutRowId = false;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateTableSqlStatementBuilder::clearColumns()
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatementBuilder.clear();
|
|
|
|
|
m_columnDefinitions.clear();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utf8String CreateTableSqlStatementBuilder::sqlStatement() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
if (!m_sqlStatementBuilder.isBuild())
|
2015-06-01 18:51:55 +02:00
|
|
|
bindAll();
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
return m_sqlStatementBuilder.sqlStatement();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CreateTableSqlStatementBuilder::isValid() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
return m_tableName.hasContent() && !m_columnDefinitions.isEmpty();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateTableSqlStatementBuilder::bindColumnDefinitions() const
|
|
|
|
|
{
|
|
|
|
|
Utf8StringVector columnDefinitionStrings;
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
foreach (const ColumnDefinition &columnDefinition, m_columnDefinitions) {
|
2015-06-01 18:51:55 +02:00
|
|
|
Utf8String columnDefinitionString = columnDefinition.name() + Utf8StringLiteral(" ") + columnDefinition.typeString();
|
|
|
|
|
|
|
|
|
|
if (columnDefinition.isPrimaryKey())
|
|
|
|
|
columnDefinitionString.append(Utf8StringLiteral(" PRIMARY KEY"));
|
|
|
|
|
|
|
|
|
|
columnDefinitionStrings.append(columnDefinitionString);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatementBuilder.bind(Utf8StringLiteral("$columnDefinitions"), columnDefinitionStrings);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateTableSqlStatementBuilder::bindAll() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatementBuilder.bind(Utf8StringLiteral("$table"), m_tableName);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
bindColumnDefinitions();
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
if (m_useWithoutRowId)
|
|
|
|
|
m_sqlStatementBuilder.bind(Utf8StringLiteral("$withoutRowId"), Utf8StringLiteral(" WITHOUT ROWID"));
|
2015-06-01 18:51:55 +02:00
|
|
|
else
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatementBuilder.bindEmptyText(Utf8StringLiteral("$withoutRowId"));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
} // namespace Sqlite
|