Home

Awesome

CQL: Compiled Query Language <!-- omit in toc -->

Build Status Go Report Card Quality Gate Status Coverage

Go.Dev reference Documentation Status

<!-- keep updated with docs/cql/introduction.md -->

What is cql?

Originally part of BaDaaS, CQL allows easy and safe persistence and querying of objects.

It's built on top of gorm, a library that actually provides the functionality of an ORM: mapping objects to tables in the SQL database. While gorm does this job well with its automatic migration then performing queries on these objects is somewhat limited, forcing us to write SQL queries directly when they are complex. CQL seeks to address these limitations with a query system that:

LanguageQuery
SQLSELECT cities.* FROM cities <br> INNER JOIN countries ON <br>   countries.id = cities.country_id AND <br>   countries.name = "France" <br> WHERE cities.name = "Paris"
GORMdb.Where(<br> "cities.name = ?",<br> "Paris",<br>).Joins(<br> "Country",<br> db.Where( <br>   "Country.name = ?", <br>   "France", <br>  ), <br> ).Find(&cities)
CQLcql.Query[models.City]( <br>  db, <br>  conditions.City.Name.Is().Eq("Paris"), <br>  conditions.City.Country( <br>   conditions.Country.Name.Is().Eq("France"), <br>  ), <br> ).FindOne()

Is cql a copy of gorm-gen?

It is true that its aim seems to be the same:

100% Type-safe DAO API without interface{}

Although gorm-gen provides a more structured API than gorm for performing queries, providing methods like:

Where(conds ...gen.Condition) IUserDo

we can see from this signatures that, for example, the Where method receives parameters of type gen.Condition. In this way, conditions from different models could be mixed without generating a compilation error:

u := query.User
c := query.Company
user, err := u.Where(c.Name.Eq("franco")).First()

which would generate a runtime error during the execution of the generated SQL:

SELECT * FROM `users` WHERE `companies`.`name` = "franco"
no such column: companies.name

Because of this, cql decides to go further in type safety and check that the conditions are of the correct model, that the compared values are of the same type, that the models are included in the query and more, ensuring that a runtime error will not be raised.

Documentation

https://compiledquerylenguage.readthedocs.io/en/latest/

Contributing

See this section to view the cql contribution guidelines.

License

CQL is Licensed under the Mozilla Public License Version 2.0.