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 "sqlstatementbuilder.h"
|
|
|
|
|
|
|
|
|
|
#include "sqlstatementbuilderexception.h"
|
|
|
|
|
#include "utf8stringvector.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
namespace Sqlite {
|
|
|
|
|
|
2015-06-01 18:51:55 +02:00
|
|
|
SqlStatementBuilder::SqlStatementBuilder(const Utf8String &sqlTemplate)
|
2017-07-26 16:02:24 +02:00
|
|
|
: m_sqlTemplate(sqlTemplate)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::bindEmptyText(const Utf8String &name)
|
|
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
|
|
|
|
changeBinding(name, Utf8String());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::bind(const Utf8String &name, const Utf8String &text)
|
|
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextIsNotEmpty(text);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
|
|
|
|
changeBinding(name, text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::bind(const Utf8String &name, const Utf8StringVector &textVector)
|
|
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextVectorIsNotEmpty(textVector);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
|
|
|
|
changeBinding(name, textVector.join(Utf8StringLiteral(", ")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::bind(const Utf8String &name, int value)
|
|
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
|
|
|
|
changeBinding(name, Utf8String::number(value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::bind(const Utf8String &name, const QVector<int> &integerVector)
|
|
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingIntegerVectorIsNotEmpty(integerVector);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
|
|
|
|
changeBinding(name, Utf8StringVector::fromIntegerVector(integerVector).join(Utf8StringLiteral(", ")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::bindWithInsertTemplateParameters(const Utf8String &name, const Utf8StringVector &columns)
|
|
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextVectorIsNotEmpty(columns);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
|
|
|
|
changeBinding(name, insertTemplateParameters(columns));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::bindWithUpdateTemplateParameters(const Utf8String &name, const Utf8StringVector &columns)
|
|
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextVectorIsNotEmpty(columns);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
|
|
|
|
changeBinding(name, updateTemplateParameters(columns));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::bindWithUpdateTemplateNames(const Utf8String &name, const Utf8StringVector &columns)
|
|
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextVectorIsNotEmpty(columns);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
|
|
|
|
changeBinding(name, updateTemplateNames(columns));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::clear()
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_bindings.clear();
|
|
|
|
|
m_sqlStatement_.clear();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Utf8String SqlStatementBuilder::insertTemplateParameters(const Utf8StringVector &columns)
|
|
|
|
|
{
|
|
|
|
|
const Utf8StringVector templateParamters(columns.count(), Utf8StringLiteral("?"));
|
|
|
|
|
|
|
|
|
|
return templateParamters.join(Utf8StringLiteral(", "));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Utf8String SqlStatementBuilder::updateTemplateParameters(const Utf8StringVector &columns)
|
|
|
|
|
{
|
|
|
|
|
Utf8String templateParamters = columns.join(Utf8StringLiteral("=?, "));
|
|
|
|
|
templateParamters.append(Utf8StringLiteral("=?"));
|
|
|
|
|
|
|
|
|
|
return templateParamters;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Utf8String SqlStatementBuilder::updateTemplateNames(const Utf8StringVector &columns)
|
|
|
|
|
{
|
|
|
|
|
Utf8StringVector templateNames;
|
|
|
|
|
|
|
|
|
|
foreach (const Utf8String &columnName, columns)
|
|
|
|
|
templateNames.append(columnName+Utf8StringLiteral("=@")+columnName);
|
|
|
|
|
|
|
|
|
|
return templateNames.join(Utf8StringLiteral(", "));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::sortBindings() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
std::sort(m_bindings.begin(), m_bindings.end(), [] (const BindingPair &lhs,const BindingPair &rhs)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
return lhs.first.byteSize() == rhs.first.byteSize() ? lhs.first.toByteArray() < rhs.first.toByteArray() : lhs.first.byteSize() > rhs.first.byteSize();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utf8String SqlStatementBuilder::sqlStatement() const
|
|
|
|
|
{
|
|
|
|
|
if (!isBuild())
|
|
|
|
|
generateSqlStatement();
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
return m_sqlStatement_;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SqlStatementBuilder::isBuild() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
return m_sqlStatement_.hasContent();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Utf8String SqlStatementBuilder::columnTypeToString(ColumnType columnType)
|
|
|
|
|
{
|
|
|
|
|
switch (columnType) {
|
|
|
|
|
case ColumnType::Numeric: return Utf8StringLiteral("NUMERIC");
|
|
|
|
|
case ColumnType::Integer: return Utf8StringLiteral("INTEGER");
|
|
|
|
|
case ColumnType::Real: return Utf8StringLiteral("REAL");
|
|
|
|
|
case ColumnType::Text: return Utf8StringLiteral("TEXT");
|
|
|
|
|
case ColumnType::None: return Utf8String();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::generateSqlStatement() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatement_ = m_sqlTemplate;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
sortBindings();
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
auto bindingIterator = m_bindings.cbegin();
|
|
|
|
|
while (bindingIterator != m_bindings.cend()) {
|
2015-06-01 18:51:55 +02:00
|
|
|
const Utf8String &placeHolderToken = bindingIterator->first;
|
|
|
|
|
const Utf8String &replacementToken = bindingIterator->second;
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatement_.replace(placeHolderToken, replacementToken);
|
2015-06-01 18:51:55 +02:00
|
|
|
++bindingIterator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkIfNoPlaceHoldersAynmoreExists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::changeBinding(const Utf8String &name, const Utf8String &text)
|
|
|
|
|
{
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
auto findBindingIterator = std::find_if(m_bindings.begin(), m_bindings.end(), [name] (const BindingPair &binding) {
|
2015-06-01 18:51:55 +02:00
|
|
|
return binding.first == name;
|
|
|
|
|
});
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
if (findBindingIterator == m_bindings.end())
|
|
|
|
|
m_bindings.push_back(std::make_pair(name, text));
|
2015-06-01 18:51:55 +02:00
|
|
|
else
|
|
|
|
|
findBindingIterator->second = text;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::clearSqlStatement()
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_sqlStatement_.clear();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::checkIfPlaceHolderExists(const Utf8String &name) const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
if (name.byteSize() < 2 || !name.startsWith('$') || !m_sqlTemplate.contains(name))
|
2015-06-01 18:51:55 +02:00
|
|
|
throwException("SqlStatementBuilder::bind: placeholder name does not exists!", name.constData());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::checkIfNoPlaceHoldersAynmoreExists() const
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
if (m_sqlStatement_.contains('$'))
|
|
|
|
|
throwException("SqlStatementBuilder::bind: there are still placeholder in the sql statement!", m_sqlTemplate.constData());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::checkBindingTextIsNotEmpty(const Utf8String &text) const
|
|
|
|
|
{
|
|
|
|
|
if (text.isEmpty())
|
2017-07-26 16:02:24 +02:00
|
|
|
throwException("SqlStatementBuilder::bind: binding text it empty!", m_sqlTemplate.constData());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::checkBindingTextVectorIsNotEmpty(const Utf8StringVector &textVector) const
|
|
|
|
|
{
|
|
|
|
|
if (textVector.isEmpty())
|
2017-07-26 16:02:24 +02:00
|
|
|
throwException("SqlStatementBuilder::bind: binding text vector it empty!", m_sqlTemplate.constData());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::checkBindingIntegerVectorIsNotEmpty(const QVector<int> &integerVector) const
|
|
|
|
|
{
|
|
|
|
|
if (integerVector.isEmpty())
|
2017-07-26 16:02:24 +02:00
|
|
|
throwException("SqlStatementBuilder::bind: binding integer vector it empty!", m_sqlTemplate.constData());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::throwException(const char *whatHasHappened, const char *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
throw SqlStatementBuilderException(whatHasHappened, errorMessage);
|
|
|
|
|
}
|
2017-07-26 16:02:24 +02:00
|
|
|
|
|
|
|
|
} // namespace Sqlite
|