diff --git a/docs/sql_queries.rst b/docs/sql_queries.rst index 407e4ba2..4958b56a 100644 --- a/docs/sql_queries.rst +++ b/docs/sql_queries.rst @@ -3,34 +3,22 @@ Running SQL queries =================== -Datasette treats SQLite database files as read-only and immutable. This means it -is not possible to execute INSERT or UPDATE statements using Datasette, which -allows us to expose SELECT statements to the outside world without needing to -worry about SQL injection attacks. +Datasette treats SQLite database files as read-only and immutable. This means it is not possible to execute INSERT or UPDATE statements using Datasette, which allows us to expose SELECT statements to the outside world without needing to worry about SQL injection attacks. -The easiest way to execute custom SQL against Datasette is through the web UI. -The database index page includes a SQL editor that lets you run any SELECT query -you like. You can also construct queries using the filter interface on the -tables page, then click "View and edit SQL" to open that query in the custom -SQL editor. +The easiest way to execute custom SQL against Datasette is through the web UI. The database index page includes a SQL editor that lets you run any SELECT query you like. You can also construct queries using the filter interface on the tables page, then click "View and edit SQL" to open that query in the custom SQL editor. -Note that this interface is only available if the :ref:`permissions_execute_sql` -permission is allowed. +Note that this interface is only available if the :ref:`permissions_execute_sql` permission is allowed. -Any Datasette SQL query is reflected in the URL of the page, allowing you to -bookmark them, share them with others and navigate through previous queries -using your browser back button. +Any Datasette SQL query is reflected in the URL of the page, allowing you to bookmark them, share them with others and navigate through previous queries using your browser back button. -You can also retrieve the results of any query as JSON by adding ``.json`` to -the base URL. +You can also retrieve the results of any query as JSON by adding ``.json`` to the base URL. .. _sql_parameters: Named parameters ---------------- -Datasette has special support for SQLite named parameters. Consider a SQL query -like this: +Datasette has special support for SQLite named parameters. Consider a SQL query like this: .. code-block:: sql @@ -38,17 +26,13 @@ like this: where "PermitNotes" like :notes and "qSpecies" = :species -If you execute this query using the custom query editor, Datasette will extract -the two named parameters and use them to construct form fields for you to -provide values. +If you execute this query using the custom query editor, Datasette will extract the two named parameters and use them to construct form fields for you to provide values. You can also provide values for these fields by constructing a URL:: /mydatabase?sql=select...&species=44 -SQLite string escaping rules will be applied to values passed using named -parameters - they will be wrapped in quotes and their content will be correctly -escaped. +SQLite string escaping rules will be applied to values passed using named parameters - they will be wrapped in quotes and their content will be correctly escaped. Datasette disallows custom SQL queries containing the string PRAGMA (with a small number `of exceptions `__) as SQLite pragma statements can be used to change database settings at runtime. If you need to include the string "pragma" in a query you can do so safely using a named parameter. @@ -57,9 +41,7 @@ Datasette disallows custom SQL queries containing the string PRAGMA (with a smal Views ----- -If you want to bundle some pre-written SQL queries with your Datasette-hosted -database you can do so in two ways. The first is to include SQL views in your -database - Datasette will then list those views on your database index page. +If you want to bundle some pre-written SQL queries with your Datasette-hosted database you can do so in two ways. The first is to include SQL views in your database - Datasette will then list those views on your database index page. The quickest way to create views is with the SQLite command-line interface:: @@ -74,8 +56,7 @@ The quickest way to create views is with the SQLite command-line interface:: Canned queries -------------- -As an alternative to adding views to your database, you can define canned -queries inside your ``metadata.json`` file. Here's an example: +As an alternative to adding views to your database, you can define canned queries inside your ``metadata.json`` file. Here's an example: .. code-block:: json @@ -95,8 +76,7 @@ Then run Datasette like this:: datasette sf-trees.db -m metadata.json -Each canned query will be listed on the database index page, and will also get -its own URL at:: +Each canned query will be listed on the database index page, and will also get its own URL at:: /database-name/canned-query-name @@ -104,20 +84,14 @@ For the above example, that URL would be:: /sf-trees/just_species -You can optionally include ``"title"`` and ``"description"`` keys to show a -title and description on the canned query page. As with regular table metadata -you can alternatively specify ``"description_html"`` to have your description -rendered as HTML (rather than having HTML special characters escaped). +You can optionally include ``"title"`` and ``"description"`` keys to show a title and description on the canned query page. As with regular table metadata you can alternatively specify ``"description_html"`` to have your description rendered as HTML (rather than having HTML special characters escaped). .. _canned_queries_named_parameters: Canned query parameters ~~~~~~~~~~~~~~~~~~~~~~~ -Canned queries support named parameters, so if you include those in the SQL you -will then be able to enter them using the form fields on the canned query page -or by adding them to the URL. This means canned queries can be used to create -custom JSON APIs based on a carefully designed SQL statement. +Canned queries support named parameters, so if you include those in the SQL you will then be able to enter them using the form fields on the canned query page or by adding them to the URL. This means canned queries can be used to create custom JSON APIs based on a carefully designed SQL statement. Here's an example of a canned query with a named parameter: @@ -380,14 +354,9 @@ The ``"message"`` and ``"redirect"`` values here will take into account ``on_suc Pagination ---------- -Datasette's default table pagination is designed to be extremely efficient. SQL -OFFSET/LIMIT pagination can have a significant performance penalty once you get -into multiple thousands of rows, as each page still requires the database to -scan through every preceding row to find the correct offset. +Datasette's default table pagination is designed to be extremely efficient. SQL OFFSET/LIMIT pagination can have a significant performance penalty once you get into multiple thousands of rows, as each page still requires the database to scan through every preceding row to find the correct offset. -When paginating through tables, Datasette instead orders the rows in the table -by their primary key and performs a WHERE clause against the last seen primary -key for the previous page. For example: +When paginating through tables, Datasette instead orders the rows in the table by their primary key and performs a WHERE clause against the last seen primary key for the previous page. For example: .. code-block:: sql @@ -395,15 +364,9 @@ key for the previous page. For example: This represents page three for this particular table, with a page size of 100. -Note that we request 101 items in the limit clause rather than 100. This allows -us to detect if we are on the last page of the results: if the query returns -less than 101 rows we know we have reached the end of the pagination set. -Datasette will only return the first 100 rows - the 101st is used purely to -detect if there should be another page. +Note that we request 101 items in the limit clause rather than 100. This allows us to detect if we are on the last page of the results: if the query returns less than 101 rows we know we have reached the end of the pagination set. Datasette will only return the first 100 rows - the 101st is used purely to detect if there should be another page. -Since the where clause acts against the index on the primary key, the query is -extremely fast even for records that are a long way into the overall pagination -set. +Since the where clause acts against the index on the primary key, the query is extremely fast even for records that are a long way into the overall pagination set. .. _cross_database_queries: