# datasette-ersatz-table-valued-functions
[![PyPI](https://img.shields.io/pypi/v/datasette-ersatz-table-valued-functions.svg)](https://pypi.org/project/datasette-ersatz-table-valued-functions/)
[![Changelog](https://img.shields.io/github/v/release/cldellow/datasette-ersatz-table-valued-functions?include_prereleases&label=changelog)](https://github.com/cldellow/datasette-ersatz-table-valued-functions/releases)
[![Tests](https://github.com/cldellow/datasette-ersatz-table-valued-functions/workflows/Test/badge.svg)](https://github.com/cldellow/datasette-ersatz-table-valued-functions/actions?query=workflow%3ATest)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/cldellow/datasette-ersatz-table-valued-functions/blob/main/LICENSE)
**ersatz** *(adj.)*: made or used as a substitute, typically an inferior one, for something else.
Enable a limited form of table-valued functions.
See also [ersatz-table-valued-functions](https://github.com/cldellow/ersatz-table-valued-functions/).
## Installation
Install this plugin in the same environment as Datasette.
datasette install datasette-ersatz-table-valued-functions
## Usage
Write a plugin that registers a table-valued function in the `startup` hook:
```python
from datasette import hookimpl
from datasette_ersatz_table_valued_functions import create_table_function
def tbl_squares(n):
return [(i, i*i) for i in range(n)]
@hookimpl
def startup():
create_table_function('tbl_squares', 1, tbl_squares, ['root', 'square'])
```
You can then query this in Datasette:
```sql
SELECT root FROM tbl_squares(10) WHERE square % 2 = 0 AND square < 50
```
will emit 0, 2, 4, 6.
The parameters to the function can come from a subselect, which could target
any table and be arbitrarily complex:
```sql
SELECT root FROM tbl_squares((SELECT 10))
```
You can do whatever with the output of this function -- join it, aggregate it, etc.
What you can't do is use a join as the source of _input_ to the function:
```sql
WITH xs AS (SELECT 10 AS x) SELECT root FROM tbl_squares(x), xs
```
Queries that aren't supported are passed as-is to SQLite, which will itself
then reject them since no such table function is registered.
## Development
To set up this plugin locally, first checkout the code. Then create a new virtual environment:
cd datasette-ersatz-table-valued-functions
python3 -m venv venv
source venv/bin/activate
Now install the dependencies and test dependencies:
pip install -e '.[test]'
To run the tests:
pytest
Raw data
{
"_id": null,
"home_page": "https://github.com/cldellow/datasette-ersatz-table-valued-functions",
"name": "datasette-ersatz-table-valued-functions",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "",
"author": "Colin Dellow",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/a7/a1/9a71dade6322cb7910604ee4695848134fb196542b6669a5ecbe61278a7f/datasette-ersatz-table-valued-functions-0.2.tar.gz",
"platform": null,
"description": "# datasette-ersatz-table-valued-functions\n\n[![PyPI](https://img.shields.io/pypi/v/datasette-ersatz-table-valued-functions.svg)](https://pypi.org/project/datasette-ersatz-table-valued-functions/)\n[![Changelog](https://img.shields.io/github/v/release/cldellow/datasette-ersatz-table-valued-functions?include_prereleases&label=changelog)](https://github.com/cldellow/datasette-ersatz-table-valued-functions/releases)\n[![Tests](https://github.com/cldellow/datasette-ersatz-table-valued-functions/workflows/Test/badge.svg)](https://github.com/cldellow/datasette-ersatz-table-valued-functions/actions?query=workflow%3ATest)\n[![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/cldellow/datasette-ersatz-table-valued-functions/blob/main/LICENSE)\n\n**ersatz** *(adj.)*: made or used as a substitute, typically an inferior one, for something else.\n\nEnable a limited form of table-valued functions.\n\nSee also [ersatz-table-valued-functions](https://github.com/cldellow/ersatz-table-valued-functions/).\n\n## Installation\n\nInstall this plugin in the same environment as Datasette.\n\n datasette install datasette-ersatz-table-valued-functions\n\n## Usage\n\nWrite a plugin that registers a table-valued function in the `startup` hook:\n\n```python\nfrom datasette import hookimpl\nfrom datasette_ersatz_table_valued_functions import create_table_function\n\ndef tbl_squares(n):\n return [(i, i*i) for i in range(n)]\n\n@hookimpl\ndef startup():\n create_table_function('tbl_squares', 1, tbl_squares, ['root', 'square'])\n```\n\nYou can then query this in Datasette:\n\n```sql\nSELECT root FROM tbl_squares(10) WHERE square % 2 = 0 AND square < 50\n```\n\nwill emit 0, 2, 4, 6.\n\nThe parameters to the function can come from a subselect, which could target\nany table and be arbitrarily complex:\n\n```sql\nSELECT root FROM tbl_squares((SELECT 10))\n```\n\nYou can do whatever with the output of this function -- join it, aggregate it, etc.\n\nWhat you can't do is use a join as the source of _input_ to the function:\n\n```sql\nWITH xs AS (SELECT 10 AS x) SELECT root FROM tbl_squares(x), xs\n```\n\nQueries that aren't supported are passed as-is to SQLite, which will itself\nthen reject them since no such table function is registered.\n\n## Development\n\nTo set up this plugin locally, first checkout the code. Then create a new virtual environment:\n\n cd datasette-ersatz-table-valued-functions\n python3 -m venv venv\n source venv/bin/activate\n\nNow install the dependencies and test dependencies:\n\n pip install -e '.[test]'\n\nTo run the tests:\n\n pytest\n",
"bugtrack_url": null,
"license": "Apache License, Version 2.0",
"summary": "Enable a limited form of table-valued functions.",
"version": "0.2",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "15f3425aed306db7efbb5532814e751d5ebb2198aa009d788c8c81efc1faa827",
"md5": "b382ff5889da7bd57d232c08ea00341c",
"sha256": "cb821827c6cfb546aa8c32fda507d582319f5365d3e7a52c58499aaff4bc1d08"
},
"downloads": -1,
"filename": "datasette_ersatz_table_valued_functions-0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b382ff5889da7bd57d232c08ea00341c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 7635,
"upload_time": "2023-01-23T05:46:57",
"upload_time_iso_8601": "2023-01-23T05:46:57.970005Z",
"url": "https://files.pythonhosted.org/packages/15/f3/425aed306db7efbb5532814e751d5ebb2198aa009d788c8c81efc1faa827/datasette_ersatz_table_valued_functions-0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a7a19a71dade6322cb7910604ee4695848134fb196542b6669a5ecbe61278a7f",
"md5": "f609c168a5cb6fc073906c17930fd6c8",
"sha256": "dc89271db3a7b15d9bee060a48b6a6defa84e4f5e6f6d0aaed20eca46c67ce54"
},
"downloads": -1,
"filename": "datasette-ersatz-table-valued-functions-0.2.tar.gz",
"has_sig": false,
"md5_digest": "f609c168a5cb6fc073906c17930fd6c8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 6921,
"upload_time": "2023-01-23T05:46:59",
"upload_time_iso_8601": "2023-01-23T05:46:59.514498Z",
"url": "https://files.pythonhosted.org/packages/a7/a1/9a71dade6322cb7910604ee4695848134fb196542b6669a5ecbe61278a7f/datasette-ersatz-table-valued-functions-0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-23 05:46:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "cldellow",
"github_project": "datasette-ersatz-table-valued-functions",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "datasette-ersatz-table-valued-functions"
}