mirror of
https://github.com/RGBCube/serenity
synced 2025-07-02 22:42:08 +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:
parent
53cd87cc1d
commit
7fc901d1b3
7 changed files with 205 additions and 17 deletions
38
Userland/Libraries/LibSQL/ResultSet.cpp
Normal file
38
Userland/Libraries/LibSQL/ResultSet.cpp
Normal file
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Jan de Visser <jan@de-visser.net>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibSQL/ResultSet.h>
|
||||
|
||||
namespace SQL {
|
||||
|
||||
size_t ResultSet::binary_search(Tuple const& sort_key, size_t low, size_t high)
|
||||
{
|
||||
if (high <= low) {
|
||||
auto compare = sort_key.compare(at(low).sort_key);
|
||||
return (compare > 0) ? low + 1 : low;
|
||||
}
|
||||
|
||||
auto mid = (low + high) / 2;
|
||||
auto compare = sort_key.compare(at(mid).sort_key);
|
||||
if (compare == 0)
|
||||
return mid + 1;
|
||||
|
||||
if (compare > 0)
|
||||
return binary_search(sort_key, mid + 1, high);
|
||||
return binary_search(sort_key, low, mid);
|
||||
}
|
||||
|
||||
void ResultSet::insert_row(Tuple const& row, Tuple const& sort_key)
|
||||
{
|
||||
if ((sort_key.size() == 0) || is_empty()) {
|
||||
empend(row, sort_key);
|
||||
return;
|
||||
}
|
||||
auto ix = binary_search(sort_key, 0, size() - 1);
|
||||
insert(ix, ResultRow { row, sort_key });
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue