Skip to content

Commit a93aafc

Browse files
authored
Merge pull request github#3194 from shati-patel/50-intro-to-ql
Docs: Include "Introduction to QL" in tutorial topic
2 parents 926d3d0 + b267df0 commit a93aafc

File tree

6 files changed

+37
-44
lines changed

6 files changed

+37
-44
lines changed

docs/language/learn-ql/beginner/catch-the-fire-starter.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ You can now write a query to select the bald southerners who are allowed into th
144144

145145
You have found the two fire starters! They are arrested and the villagers are once again impressed with your work.
146146

147-
What next?
148-
----------
147+
Further reading
148+
---------------
149149

150150
- Find out who will be the new ruler of the village in the :doc:`next tutorial <crown-the-rightful-heir>`.
151151
- Learn more about predicates and classes in the `QL language reference <https://help.semmle.com/QL/ql-handbook/index.html>`__.

docs/language/learn-ql/beginner/crown-the-rightful-heir.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ You could also try writing more of your own QL queries to find interesting facts
158158
- Do all villagers live in the same region of the village as their parents?
159159
- Find out whether there are any time travelers in the village! (Hint: Look for "impossible" family relations.)
160160

161-
What next?
162-
----------
161+
Further reading
162+
---------------
163163

164164
- Learn more about recursion in the `QL language reference <https://help.semmle.com/QL/ql-handbook/index.html>`__.
165165
- Put your QL skills to the test and solve the :doc:`River crossing puzzle <cross-the-river>`.

docs/language/learn-ql/beginner/find-the-thief.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ Have you found the thief?
289289

290290
➤ `See the answer in the query console on LGTM.com <https://lgtm.com/query/1505744186085/>`__
291291

292-
What next?
293-
----------
292+
Further reading
293+
---------------
294294

295295
- Help the villagers track down another criminal in the :doc:`next tutorial <catch-the-fire-starter>`.
296296
- Find out more about the concepts you discovered in this tutorial in the `QL language reference <https://help.semmle.com/QL/ql-handbook/index.html>`__.

docs/language/learn-ql/beginner/ql-tutorials.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ QL tutorials
33

44
Solve puzzles to learn the basics of QL before you analyze code with CodeQL. The tutorials teach you how to write queries and introduce you to key logic concepts along the way.
55

6-
Before starting these tutorials, you can read the :doc:`Introduction to QL <../introduction-to-ql>` for a description of the language and some simple examples.
7-
86
.. toctree::
97
:hidden:
108

9+
../introduction-to-ql
1110
find-the-thief
1211
catch-the-fire-starter
1312
crown-the-rightful-heir
1413
cross-the-river
1514

16-
- :doc:`Find the thief <find-the-thief>`:Take on the role of a detective to find the thief in this fictional village. You will learn how to use logical connectives, quantifiers, and aggregates in QL along the way.
15+
- :doc:`Introduction to QL <../introduction-to-ql>`: Work through some simple exercises and examples to learn about the basics of QL and CodeQL.
16+
- :doc:`Find the thief <find-the-thief>`: Take on the role of a detective to find the thief in this fictional village. You will learn how to use logical connectives, quantifiers, and aggregates in QL along the way.
1717
- :doc:`Catch the fire starter <catch-the-fire-starter>`: Learn about QL predicates and classes to solve your second mystery as a QL detective.
1818
- :doc:`Crown the rightful heir <crown-the-rightful-heir>`: This is a QL detective puzzle that shows you how to use recursion in QL to write more complex queries.
1919
- :doc:`Cross the river <cross-the-river>`: Use common QL features to write a query that finds a solution to the "River crossing" logic puzzle.

docs/language/learn-ql/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ CodeQL is based on a powerful query language called QL. The following topics hel
1818
.. toctree::
1919
:maxdepth: 1
2020

21-
introduction-to-ql
2221
beginner/ql-tutorials
2322
writing-queries/writing-queries
2423
cpp/ql-for-cpp

docs/language/learn-ql/introduction-to-ql.rst

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
Introduction to QL
22
==================
33

4-
QL is the powerful query language that underlies CodeQL, which is used to analyze code.
5-
Queries written with CodeQL can find errors and uncover variants of important security vulnerabilities.
6-
Visit `GitHub Security Lab <https://securitylab.github.com/>`__ to read about examples of vulnerabilities that we have recently found in open source projects.
4+
Work through some simple exercises and examples to learn about the basics of QL and CodeQL.
5+
6+
Basic syntax
7+
------------
78

8-
Before diving into code analysis with CodeQL, it can be helpful to learn about the underlying language more generally.
9+
The basic syntax of QL will look familiar to anyone who has used SQL, but it is used somewhat differently.
910

1011
QL is a logic programming language, so it is built up of logical formulas. QL uses common logical connectives (such as ``and``, ``or``, and ``not``), quantifiers (such as ``forall`` and ``exists``), and other important logical concepts such as predicates.
1112

1213
QL also supports recursion and aggregates. This allows you to write complex recursive queries using simple QL syntax and directly use aggregates such as ``count``, ``sum``, and ``average``.
1314

14-
Basic syntax
15-
------------
16-
17-
The basic syntax of QL will look familiar to anyone who has used SQL, but it is used somewhat differently.
15+
Running a query
16+
---------------
1817

19-
A query is defined by a **select** clause, which specifies what the result of the query should be. You can try out the examples and exercises in this topic directly in LGTM. Open the `query console on LGTM.com <https://lgtm.com/query>`__. Before you can run a query, you need to select a language and project to query (for these logic examples, any language and project will do).
18+
You can try out the following examples and exercises using `CodeQL for VS Code <https://help.semmle.com/codeql/codeql-for-vscode.html>`__, or you can run them in the `query console on LGTM.com <https://lgtm.com/query>`__. Before you can run a query on LGTM.com, you need to select a language and project to query (for these logic examples, any language and project will do).
2019

2120
Once you have selected a language, the query console is populated with the query:
2221

@@ -26,7 +25,7 @@ Once you have selected a language, the query console is populated with the query
2625
2726
select "hello world"
2827
29-
This query simply returns the string ``"hello world"``.
28+
This query returns the string ``"hello world"``.
3029

3130
More complicated queries typically look like this:
3231

@@ -49,14 +48,14 @@ Note that ``int`` specifies that the **type** of ``x`` and ``y`` is 'integer'. T
4948
Simple exercises
5049
----------------
5150

52-
You can try to write simple queries using the some of the basic functions that are available for the ``integer``, ``date``, ``float``, ``boolean`` and ``string`` types. To apply a function, simply append it to the argument. For example, ``1.toString()`` converts the value ``1`` to a string. Notice that as you start typing a function, a pop-up is displayed making it easy to select the function that you want. Also note that you can apply multiple functions in succession. For example, ``100.log().sqrt()`` first takes the natural logarithm of 100 and then computes the square root of the result.
51+
You can write simple queries using the some of the basic functions that are available for the ``int``, ``date``, ``float``, ``boolean`` and ``string`` types. To apply a function, append it to the argument. For example, ``1.toString()`` converts the value ``1`` to a string. Notice that as you start typing a function, a pop-up is displayed making it easy to select the function that you want. Also note that you can apply multiple functions in succession. For example, ``100.log().sqrt()`` first takes the natural logarithm of 100 and then computes the square root of the result.
5352

5453
Exercise 1
5554
~~~~~~~~~~
5655

5756
Write a query which returns the length of the string ``"lgtm"``. (Hint: `here <https://help.semmle.com/QL/ql-spec/language.html#built-ins-for-string>`__ is the list of the functions that can be applied to strings.)
5857

59-
➤ `Answer <https://lgtm.com/query/2103060623/>`__
58+
➤ `See answer in the query console on LGTM.com <https://lgtm.com/query/2103060623/>`__
6059

6160
There is often more than one way to define a query. For example, we can also write the above query in the shorter form:
6261

@@ -69,24 +68,24 @@ Exercise 2
6968

7069
Write a query which returns the sine of the minimum of ``3^5`` (``3`` raised to the power ``5``) and ``245.6``.
7170

72-
➤ `Answer <https://lgtm.com/query/2093780343/>`__
71+
➤ `See answer in the query console on LGTM.com <https://lgtm.com/query/2093780343/>`__
7372

7473
Exercise 3
7574
~~~~~~~~~~
7675

7776
Write a query which returns the opposite of the boolean ``false``.
7877

79-
➤ `Answer <https://lgtm.com/query/2093780344/>`__
78+
➤ `See answer in the query console on LGTM.com <https://lgtm.com/query/2093780344/>`__
8079

8180
Exercise 4
8281
~~~~~~~~~~
8382

8483
Write a query which computes the number of days between June 10 and September 28, 2017.
8584

86-
➤ `Answer <https://lgtm.com/query/2100260596/>`__
85+
➤ `See answer in the query console on LGTM.com <https://lgtm.com/query/2100260596/>`__
8786

88-
Example queries
89-
---------------
87+
Example query with multiple results
88+
-----------------------------------
9089

9190
The exercises above all show queries with exactly one result, but in fact many queries have multiple results. For example, the following query computes all `Pythagorean triples <https://en.wikipedia.org/wiki/Pythagorean_triple>`__ between 1 and 10:
9291

@@ -114,15 +113,16 @@ To simplify the query, we can introduce a class ``SmallInt`` representing the in
114113
115114
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2101340747/>`__
116115

117-
Now that you've seen some general examples, let's use the CodeQL libraries to analyze projects.
118-
In particular, LGTM generates a database representing the code and then CodeQL is used to query this database. For more information, see `Database generation <https://lgtm.com/help/lgtm/generate-database>`__ on LGTM.com.
116+
Example CodeQL queries
117+
----------------------
119118

120-
.. XX: Perhaps a link to the "CodeQL libraries for X"?
119+
The previous examples used the primitive types built in to QL. Although we chose a project to query, we didn't use the information in that project's database.
120+
The following example queries *do* use these databases and give you an idea of how to use CodeQL to analyze projects.
121121

122-
The previous exercises just used the primitive types built in to QL. Although we chose a project to query, they did not use the project-specific database. The following example queries *do* use these databases and give you an idea of what CodeQL can be used for. There are more details about how to use CodeQL `below <#learning-ql>`__, so don't worry if you don't fully understand these examples yet!
122+
Queries using the CodeQL libraries can find errors and uncover variants of important security vulnerabilities in codebases.
123+
Visit `GitHub Security Lab <https://securitylab.github.com/>`__ to read about examples of vulnerabilities that we have recently found in open source projects.
123124

124-
Python
125-
~~~~~~
125+
To import the CodeQL library for a specific programming language, type ``import <language>`` at the start of the query.
126126

127127
.. code-block:: ql
128128
@@ -132,10 +132,7 @@ Python
132132
where count(f.getAnArg()) > 7
133133
select f
134134
135-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2096810474/>`__. The ``from`` clause defines a variable ``f`` representing a function. The ``where`` part limits the functions ``f`` to those with more than 7 arguments. Finally, the ``select`` clause lists these functions.
136-
137-
JavaScript
138-
~~~~~~~~~~
135+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2096810474/>`__. The ``from`` clause defines a variable ``f`` representing a Python function. The ``where`` part limits the functions ``f`` to those with more than 7 arguments. Finally, the ``select`` clause lists these functions.
139136

140137
.. code-block:: ql
141138
@@ -145,10 +142,7 @@ JavaScript
145142
where c.getText().regexpMatch("(?si).*\\bTODO\\b.*")
146143
select c
147144
148-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2101530483/>`__. The ``from`` clause defines a variable ``c`` representing a comment. The ``where`` part limits the comments ``c`` to those containing the word ``"TODO"``. The ``select`` clause lists these comments.
149-
150-
Java
151-
~~~~
145+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2101530483/>`__. The ``from`` clause defines a variable ``c`` representing a JavaScript comment. The ``where`` part limits the comments ``c`` to those containing the word ``"TODO"``. The ``select`` clause lists these comments.
152146

153147
.. code-block:: ql
154148
@@ -158,11 +152,11 @@ Java
158152
where not exists(p.getAnAccess())
159153
select p
160154
161-
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2098670762/>`__. The ``from`` clause defines a variable ``p`` representing a parameter. The ``where`` clause finds unused parameters by limiting the parameters ``p`` to those which are not accessed. Finally, the ``select`` clause lists these parameters.
155+
➤ `See this in the query console on LGTM.com <https://lgtm.com/query/2098670762/>`__. The ``from`` clause defines a variable ``p`` representing a Java parameter. The ``where`` clause finds unused parameters by limiting the parameters ``p`` to those which are not accessed. Finally, the ``select`` clause lists these parameters.
162156

163-
Learning CodeQL
157+
Further reading
164158
---------------
165159

166160
- To find out more about how to write your own queries, try working through the :doc:`QL tutorials <beginner/ql-tutorials>`.
167161
- For an overview of the other available resources, see :doc:`Learning CodeQL <../index>`.
168-
- For a more technical description of the underlying language, see the `QL language reference <https://help.semmle.com/QL/ql-handbook>`__
162+
- For a more technical description of the underlying language, see the `QL language reference <https://help.semmle.com/QL/ql-handbook>`__.

0 commit comments

Comments
 (0)