Skip to content

Commit 51f4575

Browse files
author
Shati Patel
committed
Include spec and "About QL" in reference sphinx project
1 parent ba1a0da commit 51f4575

4 files changed

Lines changed: 2144 additions & 27 deletions

File tree

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
About the QL language
2+
######################
3+
4+
QL is the powerful query language that underlies CodeQL, which is used to analyze code.
5+
6+
For more information about the important concepts and syntactic constructs of QL, see the individual reference topics such as :doc:`Predicates <predicates>` and :doc:`Types <types>`.
7+
The explanations and examples help you understand how the language works, and how to write more advanced QL code.
8+
9+
For formal specifications of the QL language and QLDoc comments, see the :doc:`QL language specification <language>` and :doc:`QLDoc comment specification <qldoc>`.
10+
11+
About query languages and databases
12+
-----------------------------------
13+
14+
This section is aimed at users with a background in general purpose programming as well as in databases. For a basic introduction and information on how to get started, see `Learning CodeQL <https://help.semmle.com/QL/learn-ql/index.html>`__.
15+
16+
QL is a declarative, object-oriented query language that is optimized to enable efficient analysis of hierarchical data structures, in particular, databases representing software artifacts.
17+
18+
A database is an organized collection of data. The most commonly used database model is a relational model which stores data in tables and SQL (Structured Query Language) is the most commonly used query language for relational databases.
19+
20+
The purpose of a query language is to provide a programming platform where you can ask questions about information stored in a database. A database management system manages the storage and administration of data and provides the querying mechanism. A query typically refers to the relevant database entities and specifies various conditions (called predicates) that must be satisfied by the results. Query evaluation involves checking these predicates and generating the results. Some of the desirable properties of a good query language and its implementation include:
21+
22+
- Declarative specifications - a declarative specification describes properties that the result must satisfy, rather than providing the procedure to compute the result. In the context of database query languages, declarative specifications abstract away the details of the underlying database management system and query processing techniques. This greatly simplifies query writing.
23+
- Expressiveness - a powerful query language allows you to write complex queries. This makes the language widely applicable.
24+
- Efficient execution - queries can be complex and databases can be very large, so it is crucial for a query language implementation to process and execute queries efficiently.
25+
26+
Properties of QL
27+
----------------
28+
29+
The syntax of QL is similar to SQL, but the semantics of QL are based on Datalog, a declarative logic programming language often used as a query language. This makes QL primarily a logic language, and all operations in QL are logical operations. Furthermore, QL inherits recursive predicates from Datalog, and adds support for aggregates, making even complex queries concise and simple. For example, consider a database containing parent-child relationships for people. If we want to find the number of descendants of a person, typically we would:
30+
31+
#. Find a descendant of the given person, that is, a child or a descendant of a child.
32+
#. Count the number of descendants found using the previous step.
33+
34+
When you write this process in QL, it closely resembles the above structure. Notice that we used recursion to find all descendants of the given person, and an aggregate to count the number of descendants. Translating these steps into the final query without adding any procedural details is possible due to the declarative nature of the language. The QL code would look something like this:
35+
36+
.. code-block:: ql
37+
38+
Person getADescendant(Person p) {
39+
result = p.getAChild() or
40+
result = getADescendant(p.getAChild())
41+
}
42+
43+
int getNumberOfDescendants(Person p) {
44+
result = count(getADescendant(p))
45+
}
46+
47+
QL and object orientation
48+
-------------------------
49+
50+
Object orientation is an important feature of QL. The benefits of object orientation are well known – it increases modularity, enables information hiding, and allows code reuse. QL offers all these benefits without compromising on its logical foundation. This is achieved by defining a simple object model where classes are modeled as predicates and inheritance as implication. The libraries made available for all supported languages make extensive use of classes and inheritance.
51+
52+
QL and general purpose programming languages
53+
--------------------------------------------
54+
55+
Here are a few prominent conceptual and functional differences between general purpose programming languages and QL:
56+
57+
- QL does not have any imperative features such as assignments to variables or file system operations.
58+
- QL operates on sets of tuples and a query can be viewed as a complex sequence of set operations that defines the result of the query.
59+
- QL's set-based semantics makes it very natural to process collections of values without having to worry about efficiently storing, indexing and traversing them.
60+
- In object oriented programming languages, instantiating a class involves creating an object by allocating physical memory to hold the state of that instance of the class. In QL, classes are just logical properties describing sets of already existing values.
61+
62+
Further reading
63+
---------------
64+
65+
Academic references available from the `Semmle website <https://help.semmle.com/publications.html>`__ also provide an overview of QL and its semantics. Other useful references on database query languages and Datalog:
66+
67+
- `Database theory: Query languages <http://www.lsv.ens-cachan.fr/~segoufin/Papers/Mypapers/DB-chapter.pdf>`__
68+
- `Logic Programming and Databases book - Amazon page <http://www.amazon.co.uk/Programming-Databases-Surveys-Computer-Science/dp/3642839541>`__
69+
- `Foundations of Databases <http://webdam.inria.fr/Alice/>`__
70+
- `Datalog <https://en.wikipedia.org/wiki/Datalog>`__
Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
1-
QL language handbook
2-
####################
1+
QL language reference
2+
#####################
33

4-
Welcome to the QL language handbook!
5-
6-
This document describes the main features of the QL language. The explanations and examples
7-
help you understand how the language works, and how to write more advanced QL code.
8-
9-
Each section describes an important concept or syntactic construct of QL. For an overview, see
10-
the table of contents below.
11-
12-
If you are just getting started with QL, see `Learning CodeQL <https://help.semmle.com/QL/learn-ql/>`_
13-
for a list of the available resources.
14-
15-
.. index:: specification
16-
17-
For a formal specification of the QL language, see the `QL language specification
18-
<https://help.semmle.com/QL/ql-spec/language.html>`_.
19-
20-
Table of contents
21-
*****************
4+
Learn all about QL, the powerful query language that underlies the code scanning tool CodeQL.
225

236
.. toctree::
24-
:maxdepth: 3
7+
:maxdepth: 1
258

9+
about-the-ql-language
2610
predicates
2711
queries
2812
types
@@ -36,9 +20,5 @@ Table of contents
3620
lexical-syntax
3721
name-resolution
3822
evaluation
39-
40-
Index and search
41-
****************
42-
43-
* :ref:`genindex`
44-
* :ref:`search`
23+
language
24+
qldoc

0 commit comments

Comments
 (0)