missing unprepared() and updated docs

Added missing unprepared() and affectingStatement() proxies.

 - also updated docs
This commit is contained in:
silverqx
2021-07-17 17:06:36 +02:00
parent 4ba7c804c9
commit b211a4db46
6 changed files with 54 additions and 6 deletions
+14
View File
@@ -139,6 +139,20 @@ Some database statements do not return any value. For these types of operations,
DB::statement("drop table users");
:::tip
`DB::statement()` should be used for [DDL](https://en.wikipedia.org/wiki/Data_definition_language) queries, don't use it for "select" queries because it internally calls `recordsHaveBeenModified()`.
:::
#### Running An Unprepared Statement
Sometimes you may want to execute an SQL statement without binding any values. You may use the `DB` facade's `unprepared` method to accomplish this:
DB::unprepared("update users set votes = 100 where name = 'Dries'");
:::caution
Since unprepared statements do not bind parameters, they may be vulnerable to SQL injection. You should never allow user controlled values within an unprepared statement.
:::
#### Implicit Commits
When using the `DB` facade's `statement` methods within transactions, you must be careful to avoid statements that cause [implicit commits](https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html). These statements will cause the database engine to indirectly commit the entire transaction, leaving TinyORM unaware of the database's transaction level. An example of such a statement is creating a database table:
+1 -2
View File
@@ -111,8 +111,7 @@ namespace Orm
const QVector<QVariant> &bindings = {}) override;
/*! Run a raw, unprepared query against the database. */
QSqlQuery
unprepared(const QString &queryString) override;
QSqlQuery unprepared(const QString &queryString) override;
/*! Get underlying database connection (QSqlDatabase). */
QSqlDatabase getQtConnection();
+8 -2
View File
@@ -71,10 +71,16 @@ namespace Query
/*! Run a delete statement against the database. */
std::tuple<int, QSqlQuery>
remove(const QString &query, const QVector<QVariant> &bindings = {});
/*! Execute an SQL statement and return the boolean result and QSqlQuery. */
QSqlQuery
statement(const QString &query,
const QVector<QVariant> &bindings = {});
statement(const QString &query, const QVector<QVariant> &bindings = {});
/*! Run an SQL statement and get the number of rows affected. */
std::tuple<int, QSqlQuery>
affectingStatement(const QString &query, const QVector<QVariant> &bindings = {});
/*! Run a raw, unprepared query against the database. */
QSqlQuery unprepared(const QString &query);
/*! Start a new database transaction. */
bool beginTransaction();
+8 -2
View File
@@ -105,10 +105,16 @@ namespace Orm
/*! Run a delete statement against the database. */
static std::tuple<int, QSqlQuery>
remove(const QString &query, const QVector<QVariant> &bindings = {});
/*! Execute an SQL statement and return the boolean result and QSqlQuery. */
static QSqlQuery
statement(const QString &query,
const QVector<QVariant> &bindings = {});
statement(const QString &query, const QVector<QVariant> &bindings = {});
/*! Run an SQL statement and get the number of rows affected. */
static std::tuple<int, QSqlQuery>
affectingStatement(const QString &query, const QVector<QVariant> &bindings = {});
/*! Run a raw, unprepared query against the database. */
static QSqlQuery unprepared(const QString &query);
/*! Start a new database transaction. */
static bool beginTransaction(const QString &connection = "");
+12
View File
@@ -128,6 +128,18 @@ DatabaseManager::statement(const QString &query, const QVector<QVariant> &bindin
return connection().statement(query, bindings);
}
std::tuple<int, QSqlQuery>
DatabaseManager::affectingStatement(const QString &query,
const QVector<QVariant> &bindings)
{
return connection().affectingStatement(query, bindings);
}
QSqlQuery DatabaseManager::unprepared(const QString &query)
{
return connection().unprepared(query);
}
bool DatabaseManager::beginTransaction()
{
return connection().beginTransaction();
+11
View File
@@ -141,6 +141,17 @@ DB::statement(const QString &query, const QVector<QVariant> &bindings)
return manager().connection().statement(query, bindings);
}
std::tuple<int, QSqlQuery>
DB::affectingStatement(const QString &query, const QVector<QVariant> &bindings)
{
return manager().connection().affectingStatement(query, bindings);
}
QSqlQuery DB::unprepared(const QString &query)
{
return manager().connection().unprepared(query);
}
// NOTE api different silverqx
bool DB::beginTransaction(const QString &connection)
{