Sqlite: Introduce BindParameterCount template parameter

The BindParameterCount is checked at compile time and then again for
the construction of the statement. So we provide an early error instead of
an some error later or even stranger behavior.

Change-Id: I860ca1f78645c222ae1accf5c7a469c77befc3bd
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2021-12-07 17:28:38 +01:00
parent 4e69b0a689
commit d4c6f06a63
22 changed files with 389 additions and 387 deletions

View File

@@ -51,12 +51,8 @@ namespace Sqlite {
BaseStatement::BaseStatement(Utils::SmallStringView sqlStatement, Database &database)
: m_compiledStatement(nullptr, deleteCompiledStatement)
, m_database(database)
, m_bindingParameterCount(0)
, m_columnCount(0)
{
prepare(sqlStatement);
setBindingParameterCount();
setColumnCount();
}
void BaseStatement::deleteCompiledStatement(sqlite3_stmt *compiledStatement)
@@ -141,11 +137,6 @@ void BaseStatement::step() const
next();
}
int BaseStatement::columnCount() const
{
return m_columnCount;
}
void BaseStatement::bind(int index, NullValue)
{
int resultCode = sqlite3_bind_null(m_compiledStatement.get(), index);
@@ -512,34 +503,16 @@ void BaseStatement::checkForBindingError(int resultCode) const
throwUnknowError("SqliteStatement::bind: unknown error has happened");
}
void BaseStatement::checkBindingParameterCount(int bindingParameterCount) const
{
if (bindingParameterCount != sqlite3_bind_parameter_count(m_compiledStatement.get()))
throw WrongBindingParameterCount{"Sqlite: wrong binding parameter count!"};
}
void BaseStatement::checkColumnCount(int columnCount) const
{
if (columnCount != m_columnCount)
throw ColumnCountDoesNotMatch("SqliteStatement::values: column count does not match!");
}
void BaseStatement::checkBindingName(int index) const
{
if (index <= 0 || index > m_bindingParameterCount)
throwWrongBingingName("SqliteStatement::bind: binding name are not exists in this statement!");
}
void BaseStatement::setBindingParameterCount()
{
m_bindingParameterCount = sqlite3_bind_parameter_count(m_compiledStatement.get());
}
Utils::SmallStringView chopFirstLetter(const char *rawBindingName)
{
if (rawBindingName != nullptr)
return Utils::SmallStringView(++rawBindingName);
return Utils::SmallStringView("");
}
void BaseStatement::setColumnCount()
{
m_columnCount = sqlite3_column_count(m_compiledStatement.get());
if (columnCount != sqlite3_column_count(m_compiledStatement.get()))
throw WrongColumnCount{"Sqlite: wrong column count!"};
}
bool BaseStatement::isReadOnlyStatement() const
@@ -582,11 +555,6 @@ void BaseStatement::throwBindingIndexIsOutOfRange(const char *whatHasHappened) c
throw BindingIndexIsOutOfRange(whatHasHappened, sqlite3_errmsg(sqliteDatabaseHandle()));
}
void BaseStatement::throwWrongBingingName(const char *whatHasHappened) const
{
throw WrongBindingName(whatHasHappened);
}
void BaseStatement::throwUnknowError(const char *whatHasHappened) const
{
if (sqliteDatabaseHandle())