Skip to content

Commit

Permalink
bugfix column aliases quoting
Browse files Browse the repository at this point in the history
 - added unit tests
  • Loading branch information
silverqx committed Oct 31, 2023
1 parent 514d3ab commit 8c0a9a8
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/orm/basegrammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ QString BaseGrammar::wrap(const QString &value, const bool prefixAlias) const
/* If the value being wrapped has a column alias we will need to separate out
the pieces so we can wrap each of the segments of the expression on its
own, and then join these both back together using the "as" connector. */
if (value.contains(QStringLiteral(" as ")))
if (value.contains(QStringLiteral(" as "), Qt::CaseInsensitive))
return wrapAliasedValue(value, prefixAlias);

// FEATURE json columns, this code has to be in the Grammars::Grammar silverqx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ private Q_SLOTS:

void select() const;
void select_ColumnExpression() const;
void select_ColumnAlias() const;

void addSelect() const;
void addSelect_ColumnExpression() const;
void addSelect_ColumnAlias() const;

void selectRaw() const;
void selectRaw_WithBindings_WithWhere() const;
Expand Down Expand Up @@ -790,6 +793,29 @@ void tst_MySql_QueryBuilder::select_ColumnExpression() const
"select count(*) as user_count, status from `torrents`");
}

void tst_MySql_QueryBuilder::select_ColumnAlias() const
{
auto builder = createQuery();

builder->from("torrents");

builder->select({ID, "name as username"});
QCOMPARE(builder->toSql(),
"select `id`, `name` as `username` from `torrents`");

builder->select({ID, "name as username"});
QCOMPARE(builder->toSql(),
"select `id`, `name` as `username` from `torrents`");

builder->select({ID, "name AS username"});
QCOMPARE(builder->toSql(),
"select `id`, `name` as `username` from `torrents`");

builder->select({ID, "name AS username"});
QCOMPARE(builder->toSql(),
"select `id`, `name` as `username` from `torrents`");
}

void tst_MySql_QueryBuilder::addSelect() const
{
auto builder = createQuery();
Expand Down Expand Up @@ -829,6 +855,34 @@ void tst_MySql_QueryBuilder::addSelect_ColumnExpression() const
"from `torrents`");
}

void tst_MySql_QueryBuilder::addSelect_ColumnAlias() const
{
auto builder = createQuery();

builder->from("torrents");

builder->addSelect("name as username");
QCOMPARE(builder->toSql(),
"select `name` as `username` from `torrents`");

builder->addSelect({ID, "name1 as username1"});
QCOMPARE(builder->toSql(),
"select `name` as `username`, `id`, `name1` as `username1` "
"from `torrents`");

builder->addSelect("name2 AS username2");
QCOMPARE(builder->toSql(),
"select `name` as `username`, `id`, `name1` as `username1`, "
"`name2` as `username2` "
"from `torrents`");

builder->addSelect({"note", "name3 AS username3"});
QCOMPARE(builder->toSql(),
"select `name` as `username`, `id`, `name1` as `username1`, "
"`name2` as `username2`, `note`, `name3` as `username3` "
"from `torrents`");
}

void tst_MySql_QueryBuilder::selectRaw() const
{
auto builder = createQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ private Q_SLOTS:

void select() const;
void select_ColumnExpression() const;
void select_ColumnAlias() const;

void addSelect() const;
void addSelect_ColumnExpression() const;
void addSelect_ColumnAlias() const;

void distinct() const;
void distinct_on() const;
Expand Down Expand Up @@ -367,6 +370,29 @@ void tst_PostgreSQL_QueryBuilder::select_ColumnExpression() const
"select count(*) as user_count, status from \"torrents\"");
}

void tst_PostgreSQL_QueryBuilder::select_ColumnAlias() const
{
auto builder = createQuery();

builder->from("torrents");

builder->select({ID, "name as username"});
QCOMPARE(builder->toSql(),
"select \"id\", \"name\" as \"username\" from \"torrents\"");

builder->select({ID, "name as username"});
QCOMPARE(builder->toSql(),
"select \"id\", \"name\" as \"username\" from \"torrents\"");

builder->select({ID, "name AS username"});
QCOMPARE(builder->toSql(),
"select \"id\", \"name\" as \"username\" from \"torrents\"");

builder->select({ID, "name AS username"});
QCOMPARE(builder->toSql(),
"select \"id\", \"name\" as \"username\" from \"torrents\"");
}

void tst_PostgreSQL_QueryBuilder::addSelect() const
{
auto builder = createQuery();
Expand Down Expand Up @@ -406,6 +432,34 @@ void tst_PostgreSQL_QueryBuilder::addSelect_ColumnExpression() const
"from \"torrents\"");
}

void tst_PostgreSQL_QueryBuilder::addSelect_ColumnAlias() const
{
auto builder = createQuery();

builder->from("torrents");

builder->addSelect("name as username");
QCOMPARE(builder->toSql(),
"select \"name\" as \"username\" from \"torrents\"");

builder->addSelect({ID, "name1 as username1"});
QCOMPARE(builder->toSql(),
"select \"name\" as \"username\", \"id\", \"name1\" as \"username1\" "
"from \"torrents\"");

builder->addSelect("name2 AS username2");
QCOMPARE(builder->toSql(),
"select \"name\" as \"username\", \"id\", \"name1\" as \"username1\", "
"\"name2\" as \"username2\" "
"from \"torrents\"");

builder->addSelect({"note", "name3 AS username3"});
QCOMPARE(builder->toSql(),
"select \"name\" as \"username\", \"id\", \"name1\" as \"username1\", "
"\"name2\" as \"username2\", \"note\", \"name3\" as \"username3\" "
"from \"torrents\"");
}

void tst_PostgreSQL_QueryBuilder::distinct() const
{
auto builder = createQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ private Q_SLOTS:

void select() const;
void select_ColumnExpression() const;
void select_ColumnAlias() const;

void addSelect() const;
void addSelect_ColumnExpression() const;
void addSelect_ColumnAlias() const;

void distinct() const;

Expand Down Expand Up @@ -366,6 +369,29 @@ void tst_SQLite_QueryBuilder::select_ColumnExpression() const
"select count(*) as user_count, status from \"torrents\"");
}

void tst_SQLite_QueryBuilder::select_ColumnAlias() const
{
auto builder = createQuery();

builder->from("torrents");

builder->select({ID, "name as username"});
QCOMPARE(builder->toSql(),
"select \"id\", \"name\" as \"username\" from \"torrents\"");

builder->select({ID, "name as username"});
QCOMPARE(builder->toSql(),
"select \"id\", \"name\" as \"username\" from \"torrents\"");

builder->select({ID, "name AS username"});
QCOMPARE(builder->toSql(),
"select \"id\", \"name\" as \"username\" from \"torrents\"");

builder->select({ID, "name AS username"});
QCOMPARE(builder->toSql(),
"select \"id\", \"name\" as \"username\" from \"torrents\"");
}

void tst_SQLite_QueryBuilder::addSelect() const
{
auto builder = createQuery();
Expand Down Expand Up @@ -405,6 +431,34 @@ void tst_SQLite_QueryBuilder::addSelect_ColumnExpression() const
"from \"torrents\"");
}

void tst_SQLite_QueryBuilder::addSelect_ColumnAlias() const
{
auto builder = createQuery();

builder->from("torrents");

builder->addSelect("name as username");
QCOMPARE(builder->toSql(),
"select \"name\" as \"username\" from \"torrents\"");

builder->addSelect({ID, "name1 as username1"});
QCOMPARE(builder->toSql(),
"select \"name\" as \"username\", \"id\", \"name1\" as \"username1\" "
"from \"torrents\"");

builder->addSelect("name2 AS username2");
QCOMPARE(builder->toSql(),
"select \"name\" as \"username\", \"id\", \"name1\" as \"username1\", "
"\"name2\" as \"username2\" "
"from \"torrents\"");

builder->addSelect({"note", "name3 AS username3"});
QCOMPARE(builder->toSql(),
"select \"name\" as \"username\", \"id\", \"name1\" as \"username1\", "
"\"name2\" as \"username2\", \"note\", \"name3\" as \"username3\" "
"from \"torrents\"");
}

void tst_SQLite_QueryBuilder::distinct() const
{
auto builder = createQuery();
Expand Down

0 comments on commit 8c0a9a8

Please sign in to comment.