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"
|
2017-07-27 15:59:54 +02:00
|
|
|
|
|
|
|
|
#include <utils/smallstringvector.h>
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
2017-07-26 16:02:24 +02:00
|
|
|
namespace Sqlite {
|
|
|
|
|
|
2017-08-17 15:33:25 +02:00
|
|
|
SqlStatementBuilder::SqlStatementBuilder(Utils::SmallStringView sqlTemplate)
|
2017-07-27 15:59:54 +02:00
|
|
|
: m_sqlTemplate(std::move(sqlTemplate))
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::bindEmptyText(Utils::SmallString &&name)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
2017-07-27 15:59:54 +02:00
|
|
|
changeBinding(std::move(name), {});
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::bind(Utils::SmallString &&name, Utils::SmallString &&text)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextIsNotEmpty(text);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
2017-07-27 15:59:54 +02:00
|
|
|
changeBinding(std::move(name), std::move(text));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::bind(Utils::SmallString &&name, const Utils::SmallStringVector &textVector)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextVectorIsNotEmpty(textVector);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
2017-07-27 15:59:54 +02:00
|
|
|
changeBinding(std::move(name), textVector.join(", "));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::bind(Utils::SmallString &&name, int value)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
2017-07-27 15:59:54 +02:00
|
|
|
changeBinding(std::move(name), Utils::SmallString::number(value));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
namespace {
|
|
|
|
|
Utils::SmallStringVector integerVectorToStringVector(const std::vector<int> &integerVector)
|
|
|
|
|
{
|
|
|
|
|
Utils::SmallStringVector stringVector;
|
|
|
|
|
stringVector.reserve(integerVector.size());
|
|
|
|
|
|
|
|
|
|
std::transform(integerVector.begin(),
|
|
|
|
|
integerVector.end(),
|
|
|
|
|
std::back_inserter(stringVector),
|
|
|
|
|
[] (int i) { return Utils::SmallString::number(i); });
|
|
|
|
|
|
|
|
|
|
return stringVector;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void SqlStatementBuilder::bind(Utils::SmallString &&name, const std::vector<int> &integerVector)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingIntegerVectorIsNotEmpty(integerVector);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
2017-07-27 15:59:54 +02:00
|
|
|
changeBinding(std::move(name), integerVectorToStringVector(integerVector).join(", "));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::bindWithInsertTemplateParameters(Utils::SmallString &&name,
|
|
|
|
|
const Utils::SmallStringVector &columns)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextVectorIsNotEmpty(columns);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
2017-07-27 15:59:54 +02:00
|
|
|
changeBinding(std::move(name), insertTemplateParameters(columns));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::bindWithUpdateTemplateParameters(Utils::SmallString &&name,
|
|
|
|
|
const Utils::SmallStringVector &columns)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextVectorIsNotEmpty(columns);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
2017-07-27 15:59:54 +02:00
|
|
|
changeBinding(std::move(name), updateTemplateParameters(columns));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::bindWithUpdateTemplateNames(Utils::SmallString &&name,
|
|
|
|
|
const Utils::SmallStringVector &columns)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
clearSqlStatement();
|
|
|
|
|
checkBindingTextVectorIsNotEmpty(columns);
|
|
|
|
|
checkIfPlaceHolderExists(name);
|
2017-07-27 15:59:54 +02:00
|
|
|
changeBinding(std::move(name), updateTemplateNames(columns));
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::clear()
|
|
|
|
|
{
|
2017-07-26 16:02:24 +02:00
|
|
|
m_bindings.clear();
|
2017-07-27 15:59:54 +02:00
|
|
|
m_sqlStatement.clear();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallString SqlStatementBuilder::insertTemplateParameters(const Utils::SmallStringVector &columns)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
const Utils::SmallStringVector templateParamters(columns.size(), "?");
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
return templateParamters.join(", ");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallString SqlStatementBuilder::updateTemplateParameters(const Utils::SmallStringVector &columns)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallString templateParamters = columns.join("=?, ");
|
|
|
|
|
templateParamters.append("=?");
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
return templateParamters;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallString SqlStatementBuilder::updateTemplateNames(const Utils::SmallStringVector &columns)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallStringVector templateNames;
|
|
|
|
|
templateNames.reserve(columns.size());
|
|
|
|
|
|
|
|
|
|
auto convertToTemplateNames = [] (const Utils::SmallString &column) {
|
|
|
|
|
return Utils::SmallString::join({column, "=@", column});
|
|
|
|
|
};
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
std::transform(columns.begin(),
|
|
|
|
|
columns.end(),
|
|
|
|
|
std::back_inserter(templateNames),
|
|
|
|
|
convertToTemplateNames);
|
2015-06-01 18:51:55 +02:00
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
return templateNames.join(", ");
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
return lhs.first.size() == rhs.first.size() ? lhs.first < rhs.first : lhs.first.size() > rhs.first.size();
|
2015-06-01 18:51:55 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallStringView SqlStatementBuilder::sqlStatement() const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
if (!isBuild())
|
|
|
|
|
generateSqlStatement();
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
return m_sqlStatement;
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SqlStatementBuilder::isBuild() const
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
return m_sqlStatement.hasContent();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
Utils::SmallString SqlStatementBuilder::columnTypeToString(ColumnType columnType)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
switch (columnType) {
|
2017-07-27 15:59:54 +02:00
|
|
|
case ColumnType::Numeric: return "NUMERIC";
|
|
|
|
|
case ColumnType::Integer: return "INTEGER";
|
|
|
|
|
case ColumnType::Real: return "REAL";
|
|
|
|
|
case ColumnType::Text: return "TEXT";
|
|
|
|
|
case ColumnType::None: return {};
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::generateSqlStatement() const
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
m_sqlStatement = m_sqlTemplate;
|
2015-06-01 18:51:55 +02:00
|
|
|
|
|
|
|
|
sortBindings();
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
for (const auto &entry : m_bindings) {
|
|
|
|
|
const Utils::SmallStringView placeHolderToken = entry.first;
|
|
|
|
|
const Utils::SmallStringView replacementToken = entry.second;
|
|
|
|
|
m_sqlStatement.replace(placeHolderToken, replacementToken);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
checkIfNoPlaceHoldersAynmoreExists();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::changeBinding(Utils::SmallString &&name, Utils::SmallString &&text)
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
auto findBindingIterator = std::find_if(m_bindings.begin(),
|
|
|
|
|
m_bindings.end(),
|
|
|
|
|
[&] (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())
|
2017-07-27 15:59:54 +02:00
|
|
|
m_bindings.push_back(std::make_pair(std::move(name), std::move(text)));
|
2015-06-01 18:51:55 +02:00
|
|
|
else
|
2017-07-27 15:59:54 +02:00
|
|
|
findBindingIterator->second = std::move(text);
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::clearSqlStatement()
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
m_sqlStatement.clear();
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::checkIfPlaceHolderExists(Utils::SmallStringView name) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
if (name.size() < 2 || !name.startsWith('$') || !m_sqlTemplate.contains(name))
|
2018-09-21 13:52:37 +02:00
|
|
|
throwException("SqlStatementBuilder::bind: placeholder name does not exist!", name.data());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SqlStatementBuilder::checkIfNoPlaceHoldersAynmoreExists() const
|
|
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
if (m_sqlStatement.contains('$'))
|
2017-07-26 16:02:24 +02:00
|
|
|
throwException("SqlStatementBuilder::bind: there are still placeholder in the sql statement!", m_sqlTemplate.constData());
|
2015-06-01 18:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::checkBindingTextIsNotEmpty(Utils::SmallStringView text) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::checkBindingTextVectorIsNotEmpty(const Utils::SmallStringVector &textVector) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
if (textVector.empty())
|
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
|
|
|
}
|
|
|
|
|
|
2017-07-27 15:59:54 +02:00
|
|
|
void SqlStatementBuilder::checkBindingIntegerVectorIsNotEmpty(const std::vector<int> &integerVector) const
|
2015-06-01 18:51:55 +02:00
|
|
|
{
|
2017-07-27 15:59:54 +02:00
|
|
|
if (integerVector.empty())
|
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
|