]> git.jsancho.org Git - datasette-pytables.git/blob - DEVELOPERS.md
Post 2.0.1 release actions done
[datasette-pytables.git] / DEVELOPERS.md
1 # How is datasette-pytables implemented?
2
3 Datasette-PyTables is an external connector for [Datasette](https://github.com/simonw/datasette). Datasette publish data in SQLite files to the Internet with a JSON API, and this connector provides a way to do the same thing with PyTables files.  By using [Datasette-Connectors](https://github.com/PyTables/datasette-connectors), we can load external connectors that allow us to access to any data container. For this, the connectors need the interface that is described here.  By following these interface, you will can make connectors for other data sources too.
4
5 ## Starting from scratch
6
7 For making a Datasette connector for your favorite database files, you need to inherit from `datasette_connectors.Connector`. Then, you can specify your connector type in the class property `connector_type` and, very important, you should set `connection_class` property with a class that inherits from `datasette_connectors.Connection` and implements a method for opening your database files.
8
9 For example, for Pytables the next class definition is used:
10
11     import tables
12     import datasette_connectors as dc
13
14     class PyTablesConnection(dc.Connection):
15         def __init__(self, path, connector):
16             super().__init__(path, connector)
17             self.h5file = tables.open_file(path)
18
19     class PyTablesConnector(dc.Connector):
20         connector_type = 'pytables'
21         connection_class = PyTablesConnection
22
23 ## Tables inspection
24
25 Datasette needs some data about your database so you have to provide it overwriting some methods in your custom connector. For that, the connector stores and instance of the class set in `connection_class` in the property `conn`, so you can use `self.conn` to access to your database in order to retrieve that data.
26
27 The methods that must be overwritten are:
28
29 * **table_names(self)**: a list of table names
30 * **hidden_table_names(self)**: a list of hidden table names
31 * **detect_spatialite(self)**: a boolean indicating if geometry_columns exists
32 * **view_names(self)**: a list of view names
33 * **table_count(self, table_name)**: an integer with the rows count of the table
34 * **table_info(self, table_name)**: a list of dictionaries with columns description
35 * **foreign_keys(self, table_name)**: a list of dictionaries with foreign keys description
36 * **table_exists(self, table_name)**: a boolean indicating if table exists in the database
37 * **table_definition(self, table_type, table_name)**: a string with a 'CREATE TABLE' sql definition
38 * **indices_definition(self, table_name)**: a list of strings with 'CREATE INDEX' sql definitions
39
40 ## Returning results
41
42 Datasette uses SQL for specifying the queries, so your connector has to accept SQL and execute it. Overwriting `execute` method you can receive the query in SQL format and return some results.
43
44 The `Connector.execute()` method receives:
45
46 * **sql**: the query
47 * **params**: a dictionary with the params used in the query
48 * **truncate**: a boolean saying if the returned data can be separated in pages or not
49 * **custom_time_limit**: an integer with a time limit for the execution of the query in seconds
50 * **page_size**: the number of rows a page can contain
51 * **log_sql_errors**: a boolean saying if errors has to be logged
52
53 In our case, we need to parse the SQL query because PyTables has its own style for queries, but other databases could work with the SQL queries without requiring any parsing.
54
55 Note: Sometimes, Datasette make queries to `sqlite_master`; you need to keep it in mind.
56
57 The `Connector.execute()` method has to return a tuple with:
58
59 * a list of rows; each row is a dictionary with the field name as key and the field value as value
60 * a boolean saying if the data is truncated, i.e., if we return all the rows or there are more rows than the maximum indicated in max_returned_rows
61 * a tuple with the description of the columns in the form `(('c1',), ('c2',), ...)`