-
Notifications
You must be signed in to change notification settings - Fork 2
Examples
Rico Mariani edited this page Nov 19, 2023
·
33 revisions
This is an archive of interesting examples that can be helpful.
Information for contributors is at the end of the article.
- This is the Hello World sample in the docs
- It goes over the bootstrap code you need as well
- You can see a similar thing in the
sources/repl
directory, look atgo.sql
,go.sh
, andgo_query_plan.sh
- See Parent Child Result Set for the source code
- The lua output for this is very interesting and fairly readable, this gives you a lot of insight as to how this works under the hood. The C code is isomorphic.
- In this example we create an overall result set (which we could return) and then iterate it
- This presumes that you want the overall result set materialized
- See Parent Child With No Result Set for the source code
- Result sets are useful if you want to return the results of entire query as a unit and use them durably, maybe even after the database contents have changed
- If you don't need durable results, you can simply iterate results from a cursor, this uses much less memory
- See Using nested result sets to return records in a column for the source code
- Generates a single row result with either a
SELECT
or anOUT
statement to give you a flexible record - You can emit this record and then access it as usual
- Note that the record might be empty (no row is legal!)
- Appendex 10 contains a complete working example
- This shows interop between C and CQL as well as assorted SQL operations and rowset access
- A simple demo for Lua codegen along with various tests is in the
sources/lua_demo
directory - There are various samples in that directory the demo script runs them all
- It also executes the
run_test.sql
test cases - Naturally, you need to install Lua and the Lua SQLite library on your system for this to work
-
demo.sh
will run it
- A simple demo for Java codegen and JNI bindings to result sets can be found in the
sources/java_demo
directory - Note that an open opportunity here is to generate the java and JNI to call the procedure to get results sets automatically; presently the java generation only creates wrappers to help you read data out of results sets, you have to do the JNI to get those result sets yourself
- The example code shows you how to do this in one case, but it isn't generalized (it could be)
-
demo.sh
will run it
- A simple demo that uses
cqlrt_cf
the CoreFoundation version of the runtime is in thesources/cqlrt_cf
directory - This code really only works on MacOS as it requires CoreFoundation
- I have made attempts to use CFLite open source as an alternative to get the code to run on Linux but that went badly
- If you want to see how to do ObjectiveC wrappers with
--rt objc
and a little glue, this is the source code for you -
make.sh
will build and run it
- The code that verifies the AST and code-gen output of the compiler is itself written in CQL and is interesting
- Source can be found in
sources/tester
- Build the tester using the
regen.sh
command - This code runs on every execution of
test.sh
andcov.sh
as the primary verification tool - It includes a main and database helpers in
dbhelp.sql
- Note that it is not auto-updated so that we do not try to use a potentially broken CQL compiler to create its own tester
- Many rowset access concepts are demonstrated by this bit of code
- This rather more complicated demo uses a variety of techniques to build a complex result set
- It uses conditional fragments in two different forms, normal fragments, as well as parent/child result sets
- It tries to share some of the core helpers as a good pattern example for query assembly from fragments
- These are the tool that
test.sh
uses to test the compiler, they are written in CQL! - General Output Verifier
- Line Directive Checker
- Both of these tools demonstrate some interesting database techniques as well as control flow and interfacing with new native interfaces
It's helpful to create a complete example that you could drop into the playground examples directory and run with play.sh
. A command like play.sh run c examples/your_example.sql
should
do the trick.
When developing samples, more error tracing might be helpful. Use the following at the start of your sample:
@echo c, '
#undef cql_error_trace
#define cql_error_trace() \
fprintf(stderr, "SQL Failure %d %s: %s %d\n", _rc_, sqlite3_errmsg(_db_), __FILE__, __LINE__);
';
If you're targeting Lua (e.g., ./play.sh run lua examples/your_example.sql
) then tracing is present by default.