summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibSQL
AgeCommit message (Collapse)Author
2021-12-11Everywhere: Fix -Winconsistent-missing-override warnings from ClangDaniel Bertalan
This option is already enabled when building Lagom, so let's enable it for the main build too. We will no longer be surprised by Lagom Clang CI builds failing while everything compiles locally. Furthermore, the stronger `-Wsuggest-override` warning is enabled in this commit, which enforces the use of the `override` keyword in all classes, not just those which already have some methods marked as `override`. This works with both GCC and Clang.
2021-12-08LibSQL: Avoid implicitly copying ByteBufferBen Wiederhake
2021-12-04LibSQL: Gracefully react to unimplemented valid SQLJan de Visser
Fixes a crash that was caused by a syntax error which is difficult to catch by the parser: usually identifiers are accepted in column lists, but they are not in a list of column values to be inserted in an INSERT. Fixed this by putting in a heuristic check; we probably need a better way to do this. Included tests for this case. Also introduced a new SQL Error code, `NotYetImplemented`, and return that instead of crashing when encountering unimplemented SQL.
2021-12-04LibSQL: Improve error handlingJan de Visser
The handling of filesystem level errors was basically non-existing or consisting of `VERIFY_NOT_REACHED` assertions. Addressed this by * Adding `open` methods to `Heap` and `Database` which return errors. * Changing the interface of methods of these classes and clients downstream to propagate these errors. The constructors of `Heap` and `Database` don't open the underlying filesystem file anymore. The SQL statement handlers return an `SQLErrorCode::InternalError` error code if an error comes back from the lower levels. Note that some of these errors are things like duplicate index entry errors that should be caught before the SQL layer attempts to actually update the database. Added tests to catch attempts to open weird or non-existent files as databases. Finally, in between me writing this patch and submitting the PR the AK::Result<Foo, Bar> template got deprecated in favour of ErrorOr<Foo>. This resulted in more busywork.
2021-11-10AK: Make ByteBuffer::try_* functions return ErrorOr<void>Andreas Kling
Same as Vector, ByteBuffer now also signals allocation failure by returning an ENOMEM Error instead of a bool, allowing us to use the TRY() and MUST() patterns.
2021-11-10LibSQL: Implement table joinsJan de Visser
This patch introduces table joins. It uses a pretty dumb algorithm- starting with a singleton '__unity__' row consisting of a single boolean value, a cartesian product of all tables in the 'FROM' clause is built. This cartesian product is then filtered through the 'WHERE' clause, again without any smarts just using brute force. This patch required a bunch of busy work to allow for example the ColumnNameExpression having to deal with multiple tables potentially having columns with the same name.
2021-11-10LibSQL: Relax assignment rules for Null ValuesJan de Visser
It should be possible to assign a Value of any type to a Value which currently is Null.
2021-11-10LibSQL: Add current statement to the ExecutionContextJan de Visser
Because SQL is the craptastic language that it is, sometimes expressions need to know details about the calling statement. For example the tables in the 'FROM' clause may be needed to determine which columns are referenced in 'WHERE' expressions. So the current statement is added to the ExecutionContext and a new 'execute' overload on Statement is created which takes the Database and the Statement and builds an ExecutionContaxt from those.
2021-11-10LibSQL: Add 'schema' and 'table' to TupleElementDescriptorJan de Visser
These are needed to distinguish columns from different tables with the same column name in one and the same (joined) Tuple. Not quite happy yet with this API; I think some sort of hierarchical structure would be better but we'll burn that bridge when we get there :^)
2021-11-10LibSQL: Add the 'extend' operation to the Tuple classJan de Visser
Tuple::extend is similar to the Vector method of the same name; it concatenates a second Tuple to the current one.
2021-11-08LibSQL: Replace Result<T, E> use with ErrorOr<T>Andreas Kling
2021-11-08LibCore: Use ErrorOr<T> for Core::File::open()Andreas Kling
2021-11-02Libraries: Fix visibility of Object-derivative constructorsBen Wiederhake
Derivatives of Core::Object should be constructed through ClassName::construct(), to avoid handling ref-counted objects with refcount zero. Fixing the visibility means that misuses like this are more difficult.
2021-10-25LibSQL: Add better error handling to `evaluate` and `execute` methodsJan de Visser
There was a lot of `VERIFY_NOT_REACHED` error handling going on. Fixed most of those. A bit of a caveat is that after every `evaluate` call for expressions that are part of a statement the error status of the `SQLResult` return value must be called.
2021-10-25LibSQL: First cut of SQL `WHERE` clauseJan de Visser
Filters matching rows by doing a table scan and evaluating the `WHERE` expression for every row. Does not use indexes, for one because they do not exist yet.
2021-10-25LibSQL: Implement evaluate() method of BinaryOperatorExpressionJan de Visser
Mostly just calls the appropriate methods on the Value objects. Exception are the `Concatenate` (string concat), and the logical `and` and `or` operators which are implemented directly in `BinaryOperatorExpression::evaluate`
2021-10-25LibSQL: Implement binary operators for Value objectsJan de Visser
The behaviour of the various operators is supposed to mimic that of the same operators in PostgreSQL; the '+' operator for example will successfully add '98' (string) and 2 (integer), but not 'foo' and 2. Also removed some redundant const& parameter declarations for intrinsic types (ints and doubles etc). Passing those by const& doesn't make a lot of sense.
2021-10-08Libraries: Fix -Wunreachable-code warnings from clangNico Weber
2021-10-06LibSQL: Resolve cyclic dependencyBen Wiederhake
Previously, class SQL::Key depends on def class SQL::IndexDef (because inline def index()) depends on def class SQL::KeyPartDef (inline def key_definition()) depends on def class SQL::ColumnDef (because base class) depends on def class SQL::Relation (because base class) depends on def class SQL::Key (because inline def hash()). This hasn't caused any problems so far because Meta.h happened to be always included after Key.h (in part due to alphabetical ordering). However, a compilation that for example only contains #include <Userland/Libraries/LibSQL/Key.h> would fail to compile. This patch resolves this issue by pushing the inline definition of SQL::Relation::hash() into a different file. Yes, this might reduce performance marginally, but this gets it to compile again.
2021-10-05LibSQL: Allow expressions and column names in SELECT ... FROMJan de Visser
Up to now the only ``SELECT`` statement that worked was ``SELECT * FROM <table>``. This commit allows a column list consisting of column names and expressions in addition to ``*``. ``WHERE`` still doesn't work though.
2021-10-05SQLServer+SQL+LibSQL: Allow sql client to specify the database nameJan de Visser
The database the sql client connected to was 'hardcoded' to the login name of the calling user. - Extended the IPC API to be more expressive when connecting, by returning the name of the database the client connected to in the 'connected' callback. - Gave the sql client a command line argument (-d/--database) allowing an alternative database name to be specified A subsequent commit will have a dot command allowing the user to connect to different databases from the same sql session.
2021-10-04LibSQL: Check data types in INSERT statement parsingMahmoud Mandour
Data types are now checked against the table data types. When multiple rows are inserted at once, we check all rows to be matching W.R.T data types. Only then we insert the rows.
2021-10-04LibSQL: Parse INSERT statement without column namesMahmoud Mandour
This adds the ability to parse SQL INSERT statements in the following form: INSERT INTO schema.tablename VALUES (column1, column2, ...), (column1, column2, ...), ...
2021-09-06Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safeAli Mohammad Pur
2021-09-06Everywhere: Use OOM-safe ByteBuffer APIs where possibleAli Mohammad Pur
If we can easily communicate failure, let's avoid asserting and report failure instead.
2021-09-03Everywhere: Prevent risky implicit casts of (Nonnull)RefPtrDaniel Bertalan
Our existing implementation did not check the element type of the other pointer in the constructors and move assignment operators. This meant that some operations that would require explicit casting on raw pointers were done implicitly, such as: - downcasting a base class to a derived class (e.g. `Kernel::Inode` => `Kernel::ProcFSDirectoryInode` in Kernel/ProcFS.cpp), - casting to an unrelated type (e.g. `Promise<bool>` => `Promise<Empty>` in LibIMAP/Client.cpp) This, of course, allows gross violations of the type system, and makes the need to type-check less obvious before downcasting. Luckily, while adding the `static_ptr_cast`s, only two truly incorrect usages were found; in the other instances, our casts just needed to be made explicit.
2021-09-03LibSQL: Replace Optional<NonnullRefPtr<T>> with RefPtr<T>Andreas Kling
We generally don't use Optional with nullable types, since they already have an empty state, and having multiple empty states is confusing.
2021-08-21LibSQL: Introduce Serializer as a mediator between Heap and client codeJan de Visser
Classes reading and writing to the data heap would communicate directly with the Heap object, and transfer ByteBuffers back and forth with it. This makes things like caching and locking hard. Therefore all data persistence activity will be funneled through a Serializer object which in turn submits it to the Heap. Introducing this unfortunately resulted in a huge amount of churn, in which a number of smaller refactorings got caught up as well.
2021-08-21LibSQL+SQLServer: Bare bones INSERT and SELECT statementsJan de Visser
This patch provides very basic, bare bones implementations of the INSERT and SELECT statements. They are *very* limited: - The only variant of the INSERT statement that currently works is SELECT INTO schema.table (column1, column2, ....) VALUES (value11, value21, ...), (value12, value22, ...), ... where the values are literals. - The SELECT statement is even more limited, and is only provided to allow verification of the INSERT statement. The only form implemented is: SELECT * FROM schema.table These statements required a bit of change in the Statement::execute API. Originally execute only received a Database object as parameter. This is not enough; we now pass an ExecutionContext object which contains the Database, the current result set, and the last Tuple read from the database. This object will undoubtedly evolve over time. This API change dragged SQLServer::SQLStatement into the patch. Another API addition is Expression::evaluate. This method is, unsurprisingly, used to evaluate expressions, like the values in the INSERT statement. Finally, a new test file is added: TestSqlStatementExecution, which tests the currently implemented statements. As the number and flavour of implemented statements grows, this test file will probably have to be restructured.
2021-08-21LibSQL: Added 'nullable' and 'default value' option to ColumnDefJan de Visser
These are standard SQL concepts which columns should be aware of.
2021-08-21LibSQL: Redesign Value implementation and add new typesJan de Visser
The implemtation of the Value class was based on lambda member variables implementing type-dependent behaviour. This was done to ensure that Values can be used as stack-only objects; the simplest alternative, virtual methods, forces them onto the heap. The problem with the the lambda approach is that it bloats the Values (which are supposed to be lightweight objects) quite considerably, because every object contains more than a dozen function pointers. The solution to address both problems (we want Values to be able to live on the stack and be as lightweight as possible) chosen here is to encapsulate type-dependent behaviour and state in an implementation class, and let the Value be an AK::Variant of those implementation classes. All methods of Value are now basically straight delegates to the implementation object using the Variant::visit method. One issue complicating matters is the addition of two aggregate types, Tuple and Array, which each contain a Vector of Values. At this point Tuples and Arrays (and potential future aggregate types) can't contain these aggregate types. This is limiting and needs to be addressed. Another area that needs attention is the nomenclature of things; it's a bit of a tangle of 'ValueBlahBlah' and 'ImplBlahBlah'. It makes sense right now I think but admit we probably can do better. Other things included here: - Added the Boolean and Null types (and Tuple and Array, see above). - to_string now always succeeds and returns a String instead of an Optional. This had some impact on other sources. - Added a lot of tests. - Started moving the serialization mechanism more towards where I want it to be, i.e. a 'DataSerializer' object which just takes serialization and deserialization requests and knows for example how to store long strings out-of-line. One last remark: There is obviously a naming clash between the Tuple class and the Tuple Value type. This is intentional; I plan to make the Tuple class a subclass of Value (and hence Key and Row as well).
2021-08-21LibSQL: Make TupleDescriptor a shared pointer instead of a stack objectJan de Visser
Tuple descriptors are basically the same for for example all rows in a table. Makes sense to share them instead of copying them for every single row.
2021-08-13AK+Everywhere: Delete Variant's default constructorAli Mohammad Pur
This was exposed to the user by mistake, and even accumulated a bunch of users that didn't blow up out of sheer luck.
2021-08-06LibSQL: Use compiler generated default functionsLenny Maiorani
Problem: - Clang ToT generates warnings due to user-declared functions causing the implicitly generated assignment operator to not be generated. Solution: - Declare the default constructor `= default`. - Remove the default copy constructor declaration.
2021-07-08LibSQL+SQLServer: Build SQLServer system serviceJan de Visser
This patch introduces the SQLServer system server. This service is supposed to be the only process/application talking to database storage. This makes things like locking and caching more reliable, easier to implement, and more efficient. In LibSQL we added a client component that does the ugly IPC nitty- gritty for you. All that's needed is setting a number of event handler lambdas and you can connect to databases and execute statements on them. Applications that wish to use this SQLClient class obviously need to link LibSQL and LibIPC.
2021-07-08LibSQL: Invent statement execution machinery and CREATE SCHEMA statementJan de Visser
This patch introduces the ability execute parsed SQL statements. The abstract AST Statement node now has a virtual 'execute' method. This method takes a Database object as parameter and returns a SQLResult object. Also introduced here is the CREATE SCHEMA statement. Tables live in a schema, and if no schema is present in a table reference the 'default' schema is implied. This schema is created if it doesn't yet exist when a Database object is created. Finally, as a proof of concept, the CREATE SCHEMA and CREATE TABLE statements received an 'execute' implementation. The CREATE TABLE method is not able to create tables created from SQL queries yet.
2021-07-08LibSQL: Move Order and Nulls enums from SQL::AST to SQL namespaceJan de Visser
The Order enum is used in the Meta component of LibSQL. Using this enum meant having to include the monster AST/AST.h include file. Furthermore, they are sort of basic and therefore can live in the general SQL namespace. Moved to LibSQL/Type.h. Also introduced a new class, SQLResult, which is needed in future patches.
2021-06-24LibSQL: Make lexer and parser more standard SQL compliantJan de Visser
SQL was standardized before there was consensus on sane language syntax constructs had evolved. The language is mostly case-insensitive, with unquoted text converted to upper case. Identifiers can include lower case characters and other 'special' characters by enclosing the identifier with double quotes. A double quote is escaped by doubling it. Likewise, a single quote in a literal string is escaped by doubling it. All this means that the strategy used in the lexer, where a token's value is a StringView 'window' on the source string, does not work, because the value needs to be massaged before being handed to the parser. Therefore a token now has a String containing its value. Given the limited lifetime of a token, this is acceptable overhead. Not doing this means that for example quote removal and double quote escaping would need to be done in the parser or in AST node construction, which would spread lexing basically all over the place. Which would be suboptimal. There was some impact on the sql utility and SyntaxHighlighter component which was addressed by storing the token's end position together with the start position in order to properly highlight it. Finally, reviewing the tests for parsing numeric literals revealed an inconsistency in which tokens we accept or reject: `1a` is accepted but `1e` is rejected. Related to this is the fate of `0x`. Added a FIXME reminding us to address this.
2021-06-24LibSQL: Move Lexer and Parser machinery to AST directoryJan de Visser
The SQL engine is expected to be a fairly sizeable piece of software. Therefore we're starting to restructure the codebase for growth.
2021-06-19LibSQL: Database layerJan de Visser
This patch implements the beginnings of a database API allowing for the creation of tables, inserting rows in those tables, and retrieving those rows.
2021-06-19LibSQL: Hash index implementation for the SQL storage layerJan de Visser
This patch implements a basic hash index. It uses the extendible hashing algorith. Also includes a test file.
2021-06-19LibSQL: BTree index, Heap, and Meta objects for SQL Storage layerJan de Visser
Unfortunately this patch is quite large. The main functionality included are a BTree index implementation and the Heap class which manages persistent storage. Also included are a Key subclass of the Tuple class, which is a specialization for index key tuples. This "dragged in" the Meta layer, which has classes defining SQL objects like tables and indexes.
2021-06-19LibSQL: Basic dynamic value classes for SQL Storage layerJan de Visser
This patch adds the basic dynamic value classes used by the SQL Storage layer. The most elementary class is Value, which holds a typed Value which can be converted to standard C++ types. A Tuple is a collection of Values described by a TupleDescriptor, which specifies the names, types, and ordering of the elements in the Tuple. Tuples and Values can be serialized and deserialized to and from ByteBuffers. This is mechanism which is used to save them to disk. Tuples are used as keys in SQL indexes and rows in SQL tables. Also included is a test file.
2021-06-13Userland: Allow building SerenityOS with -funsigned-charGunnar Beutner
Some of the code assumed that chars were always signed while that is not the case on ARM hosts. Also, some of the code tried to use EOF (-1) in a way similar to what fgetc() does, however instead of storing the characters in an int variable a char was used. While this seemed to work it also meant that character 0xFF would be incorrectly seen as an end-of-file. Careful reading of fgetc() reveals that fgetc() stores character data in an int where valid characters are in the range of 0-255 and the EOF value is explicitly outside of that range (usually -1).
2021-06-08LibSQL: Limit the number of nested subqueriesTimothy Flynn
SQLite hasn't documented a limit on https://www.sqlite.org/limits.html for the maximum number of nested subqueries. However, its parser is generated with Yacc and has an internal limit of 100 for general nested statements. Fixes https://crbug.com/oss-fuzz/35022.
2021-06-07LibWeb+LibSyntax: Implement nested syntax highlightersAli Mohammad Pur
And use them to highlight javascript in HTML source. This commit also changes how TextDocumentSpan::data is interpreted, as it used to be an opaque pointer, but everyone stuffed an enum value inside it, which made the values not unique to each highlighter; that field is now a u64 serial id. The syntax highlighters don't need to change their ways of stuffing token types into that field, but a highlighter that calls another nested highlighter needs to register the nested types for use with token pairs.
2021-06-05LibSQL: Limit the allowed depth of an expression treeTimothy Flynn
According to the definition at https://sqlite.org/lang_expr.html, SQL expressions could be infinitely deep. For practicality, SQLite enforces a maxiumum expression tree depth of 1000. Apply the same limit in LibSQL to avoid stack overflow in the expression parser. Fixes https://crbug.com/oss-fuzz/34859.
2021-06-05LibSQL: Fix off-by-one error in SyntaxHighlighterMax Wipfli
This changes the SQL SyntaxHighlighter to conform to the now-fixed rendering of syntax highlighting spans in GUI::TextEditor.
2021-06-05LibSQL: Clean up SyntaxHighlighter codeMax Wipfli
This changes SyntaxHighlighter.{cpp,h} to use east const style. It also removes two unused headers and simplifies a loop.
2021-06-03LibSQL: Report a syntax error for unsupported LIMIT clause syntaxTimothy Flynn
Rather than aborting when a LIMIT clause of the form 'LIMIT expr, expr' is encountered, fail the parser with a syntax error. This will be nicer for the user and fixes the following fuzzer bug: https://crbug.com/oss-fuzz/34837