1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:38:12 +00:00

LibSQL+SQLServer: Implement first cut of SELECT ... ORDER BY foo

Ordering is done by replacing the straight Vector holding the query
result in the SQLResult object with a dedicated Vector subclass that
inserts result rows according to their sort key using a binary search.
This is done in the ResultSet class.

There are limitations:
- "SELECT ... ORDER BY 1" (or 2 or 3 etc) is supposed to sort by the
n-th result column. This doesn't work yet
- "SELECT ... column-expression alias ... ORDER BY alias" is supposed to
sort by the column with the given alias. This doesn't work yet

What does work however is something like
```SELECT foo FROM bar SORT BY quux```
i.e. sorted by a column not in the result set. Once functions are
supported it should be possible to sort by random functions.
This commit is contained in:
Jan de Visser 2022-01-12 09:49:43 -05:00 committed by Andreas Kling
parent 53cd87cc1d
commit 7fc901d1b3
7 changed files with 205 additions and 17 deletions

View file

@ -10,6 +10,7 @@
#include <AK/NonnullOwnPtrVector.h>
#include <AK/Vector.h>
#include <LibCore/Object.h>
#include <LibSQL/ResultSet.h>
#include <LibSQL/Tuple.h>
#include <LibSQL/Type.h>
@ -110,10 +111,10 @@ class SQLResult : public Core::Object {
C_OBJECT(SQLResult)
public:
void append(Tuple const& tuple)
void insert(Tuple const& row, Tuple const& sort_key)
{
m_has_results = true;
m_result_set.append(tuple);
m_result_set.insert_row(row, sort_key);
}
SQLCommand command() const { return m_command; }
@ -129,7 +130,7 @@ public:
bool has_error() const { return m_error.code != SQLErrorCode::NoError; }
SQLError const& error() const { return m_error; }
bool has_results() const { return m_has_results; }
Vector<Tuple> const& results() const { return m_result_set; }
ResultSet const& results() const { return m_result_set; }
private:
SQLResult() = default;
@ -161,7 +162,7 @@ private:
int m_insert_count { 0 };
int m_delete_count { 0 };
bool m_has_results { false };
Vector<Tuple> m_result_set;
ResultSet m_result_set;
};
}