Skip to content

FrancoLiberali/cql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CQL: Compiled Query Language

Build Status Go Report Card Quality Gate Status Coverage

Go.Dev reference Documentation Status

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:

  • Is compile-time safe: queries are validated at compile time to avoid errors such as comparing attributes that are of different types, trying to use attributes or navigate relationships that do not exist, using information from tables that are not included in the query, etc.; ensuring that a runtime error will not be raised.
  • Is easy to use: the use of its query system does not require knowledge of databases, SQL languages or complex concepts. Writing queries only requires programming in Go and the result is easy to read.
  • Is designed for real applications: the query system is designed to work well in real-world cases where queries are complex, require navigating multiple relationships, performing multiple comparisons, etc.
  • Is designed so that developers can focus on the business model: its queries allow easy retrieval of model relationships to apply business logic to the model and it provides mechanisms to avoid errors in the business logic due to mistakes in loading information from the database.
  • It is designed for high performance: the query system avoids as much as possible the use of reflection and aims that all the necessary model data can be retrieved in a single query to the database.
Language Query
SQL SELECT cities.* FROM cities
INNER JOIN countries ON
   countries.id = cities.country_id AND
   countries.name = "France"
WHERE cities.name = "Paris"
GORM db.Where(
 "cities.name = ?",
 "Paris",
).Joins(
 "Country",
 db.Where(
   "Country.name = ?",
   "France",
  ),
).Find(&cities)
CQL cql.Query[models.City](
  db,
  conditions.City.Name.Is().Eq("Paris"),
  conditions.City.Country(
   conditions.Country.Name.Is().Eq("France"),
  ),
).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.