# fastsql
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->
## Install
``` sh
pip install fastsql
```
## Creating a table
``` python
from fastsql import *
import sqlalchemy as sa
```
First we instantiate our database using FastSQL’s Database class:
``` python
db = Database("sqlite:///:memory:")
```
The main table object in FastSQL is
[`DBTable`](https://answerdotai.github.io/fastsql/core.html#dbtable),
which you can create by calling `DBTable(table)` with a SQLAlchemy
`Table` object, or by calling `Database.create(cls)` with a class
(optionally a dataclass). We’ll demonstrate the latter with these
classes:
``` python
class User: name:str; pwd:str
class Todo: title:str; name:str; id:int=None; done:bool=False; details:str=''
```
Equipped with our schemas, let’s turn them into database tables.
``` python
users = db.create(User, pk='name')
todos = db.create(Todo, pk='id')
```
Let’s confirm the table design:
``` python
print(db.schema())
```
Table: todo
- title: VARCHAR
- name: VARCHAR
* id: INTEGER
- done: BOOLEAN
- details: VARCHAR
Table: user
* name: VARCHAR
- pwd: VARCHAR
We can check if a table exists:
``` python
users.exists()
```
True
## Using FastSQL
Let’s create some dataclass objects representing users and todos.
``` python
u0 = User('jph','foo')
u1 = User('rlt','bar')
t0 = Todo('do it', 'jph')
t1 = Todo('build it', 'jph')
t2 = Todo('write book', 'rlt')
```
Let’s convert these dataclass objects into database records. To do that
we insert them into their tables using the aply named `insert` method:
``` python
users.insert(u0)
users.insert(u1)
todos.insert(t0)
todos.insert(t1)
todos.insert(t2)
```
Todo(title='write book', name='rlt', id=3, done=False, details='')
To query a single table, call the table like a function (which is
implemented in Python using the special `__call__` method.
------------------------------------------------------------------------
<a
href="https://github.com/answerdotai/fastsql/blob/main/fastsql/core.py#LNone"
target="_blank" style="float:right; font-size:smaller">source</a>
### DBTable.\_\_call\_\_
> DBTable.__call__ (where:str|None=None,
> where_args:Union[Iterable,dict,NoneType]=None,
> order_by:str|None=None, limit:int|None=None,
> offset:int|None=None, select:str='*', **kw)
*Result of `select` query on the table*
| | **Type** | **Default** | **Details** |
|-------------|-------------|-------------|---------------------------------------------------------------------------|
| where | str \| None | None | SQL where fragment to use, for example `id > ?` |
| where_args | Union | None | Parameters to use with `where`; iterable for `id>?`, or dict for `id>:id` |
| order_by | str \| None | None | Column or fragment of SQL to order by |
| limit | int \| None | None | Number of rows to limit to |
| offset | int \| None | None | SQL offset |
| select | str | \* | Comma-separated list of columns to select |
| kw | | | |
| **Returns** | **list** | | **List of returned objects** |
``` python
users()
```
[User(name='jph', pwd='foo'), User(name='rlt', pwd='bar')]
Use where statement to filter records, in this case only jph’s todos.
``` python
todos(where="name = :name", name="jph")
```
[Todo(title='do it', name='jph', id=1, done=False, details=''),
Todo(title='build it', name='jph', id=2, done=False, details='')]
Look only for those records with the word `it` in it.
``` python
todos(where="title LIKE :title", title="%% it%%")
```
[Todo(title='do it', name='jph', id=1, done=False, details=''),
Todo(title='build it', name='jph', id=2, done=False, details='')]
You can also fetch a record just by the primary key by using `[]` with
the table:
``` python
user = users['rlt']
user
```
User(name='rlt', pwd='bar')
Change a value in a record by updating an object, and passing the
updated object to `update()`:
``` python
user.pwd = 'baz'
users.update(user)
users['rlt']
```
User(name='rlt', pwd='baz')
## Using SQLAlchemy
``` python
ut,uc = users.t
tt,tc = todos.t
```
``` python
query = sa.select(uc.name, uc.pwd, tc.title).select_from(
tt.join(ut, tc.name == uc.name))
list(db.execute(query))
```
[('jph', 'foo', 'do it'),
('jph', 'foo', 'build it'),
('rlt', 'baz', 'write book')]
``` python
dbm = db.meta
```
Raw data
{
"_id": null,
"home_page": "https://github.com/fewsats/hub-sdk",
"name": "fewsats-hub",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "nbdev jupyter notebook python",
"author": "Pol Alvarez & Jordi Montes",
"author_email": "pol@fewsats.com",
"download_url": null,
"platform": null,
"description": "# fastsql\n\n\n<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->\n\n## Install\n\n``` sh\npip install fastsql\n```\n\n## Creating a table\n\n``` python\nfrom fastsql import *\nimport sqlalchemy as sa\n```\n\nFirst we instantiate our database using FastSQL\u2019s Database class:\n\n``` python\ndb = Database(\"sqlite:///:memory:\")\n```\n\nThe main table object in FastSQL is\n[`DBTable`](https://answerdotai.github.io/fastsql/core.html#dbtable),\nwhich you can create by calling `DBTable(table)` with a SQLAlchemy\n`Table` object, or by calling `Database.create(cls)` with a class\n(optionally a dataclass). We\u2019ll demonstrate the latter with these\nclasses:\n\n``` python\nclass User: name:str; pwd:str\nclass Todo: title:str; name:str; id:int=None; done:bool=False; details:str=''\n```\n\nEquipped with our schemas, let\u2019s turn them into database tables.\n\n``` python\nusers = db.create(User, pk='name')\ntodos = db.create(Todo, pk='id')\n```\n\nLet\u2019s confirm the table design:\n\n``` python\nprint(db.schema())\n```\n\n Table: todo\n - title: VARCHAR\n - name: VARCHAR\n * id: INTEGER\n - done: BOOLEAN\n - details: VARCHAR\n Table: user\n * name: VARCHAR\n - pwd: VARCHAR\n\nWe can check if a table exists:\n\n``` python\nusers.exists()\n```\n\n True\n\n## Using FastSQL\n\nLet\u2019s create some dataclass objects representing users and todos.\n\n``` python\nu0 = User('jph','foo')\nu1 = User('rlt','bar')\nt0 = Todo('do it', 'jph')\nt1 = Todo('build it', 'jph')\nt2 = Todo('write book', 'rlt')\n```\n\nLet\u2019s convert these dataclass objects into database records. To do that\nwe insert them into their tables using the aply named `insert` method:\n\n``` python\nusers.insert(u0)\nusers.insert(u1)\ntodos.insert(t0)\ntodos.insert(t1)\ntodos.insert(t2)\n```\n\n Todo(title='write book', name='rlt', id=3, done=False, details='')\n\nTo query a single table, call the table like a function (which is\nimplemented in Python using the special `__call__` method.\n\n------------------------------------------------------------------------\n\n<a\nhref=\"https://github.com/answerdotai/fastsql/blob/main/fastsql/core.py#LNone\"\ntarget=\"_blank\" style=\"float:right; font-size:smaller\">source</a>\n\n### DBTable.\\_\\_call\\_\\_\n\n> DBTable.__call__ (where:str|None=None,\n> where_args:Union[Iterable,dict,NoneType]=None,\n> order_by:str|None=None, limit:int|None=None,\n> offset:int|None=None, select:str='*', **kw)\n\n*Result of `select` query on the table*\n\n| | **Type** | **Default** | **Details** |\n|-------------|-------------|-------------|---------------------------------------------------------------------------|\n| where | str \\| None | None | SQL where fragment to use, for example `id > ?` |\n| where_args | Union | None | Parameters to use with `where`; iterable for `id>?`, or dict for `id>:id` |\n| order_by | str \\| None | None | Column or fragment of SQL to order by |\n| limit | int \\| None | None | Number of rows to limit to |\n| offset | int \\| None | None | SQL offset |\n| select | str | \\* | Comma-separated list of columns to select |\n| kw | | | |\n| **Returns** | **list** | | **List of returned objects** |\n\n``` python\nusers()\n```\n\n [User(name='jph', pwd='foo'), User(name='rlt', pwd='bar')]\n\nUse where statement to filter records, in this case only jph\u2019s todos.\n\n``` python\ntodos(where=\"name = :name\", name=\"jph\")\n```\n\n [Todo(title='do it', name='jph', id=1, done=False, details=''),\n Todo(title='build it', name='jph', id=2, done=False, details='')]\n\nLook only for those records with the word `it` in it.\n\n``` python\ntodos(where=\"title LIKE :title\", title=\"%% it%%\")\n```\n\n [Todo(title='do it', name='jph', id=1, done=False, details=''),\n Todo(title='build it', name='jph', id=2, done=False, details='')]\n\nYou can also fetch a record just by the primary key by using `[]` with\nthe table:\n\n``` python\nuser = users['rlt']\nuser\n```\n\n User(name='rlt', pwd='bar')\n\nChange a value in a record by updating an object, and passing the\nupdated object to `update()`:\n\n``` python\nuser.pwd = 'baz'\nusers.update(user)\nusers['rlt']\n```\n\n User(name='rlt', pwd='baz')\n\n## Using SQLAlchemy\n\n``` python\nut,uc = users.t\ntt,tc = todos.t\n```\n\n``` python\nquery = sa.select(uc.name, uc.pwd, tc.title).select_from(\n tt.join(ut, tc.name == uc.name))\nlist(db.execute(query))\n```\n\n [('jph', 'foo', 'do it'),\n ('jph', 'foo', 'build it'),\n ('rlt', 'baz', 'write book')]\n\n``` python\ndbm = db.meta\n```\n",
"bugtrack_url": null,
"license": "Apache Software License 2.0",
"summary": "Python SDK for Fewsats Hub",
"version": "0.0.1",
"project_urls": {
"Homepage": "https://github.com/fewsats/hub-sdk"
},
"split_keywords": [
"nbdev",
"jupyter",
"notebook",
"python"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "66ae1502085adcf0c5389b5e9a17fd30fcffb86f8c05df9d6d1ddd081fdee351",
"md5": "00c482d0e3905c615bccebcc52a8ed77",
"sha256": "36f2d40a62c3c897398c251a3d23656ef7ee144bd0db76abbc392775ab9b6649"
},
"downloads": -1,
"filename": "fewsats_hub-0.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "00c482d0e3905c615bccebcc52a8ed77",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5747,
"upload_time": "2024-08-28T15:31:05",
"upload_time_iso_8601": "2024-08-28T15:31:05.674941Z",
"url": "https://files.pythonhosted.org/packages/66/ae/1502085adcf0c5389b5e9a17fd30fcffb86f8c05df9d6d1ddd081fdee351/fewsats_hub-0.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-28 15:31:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "fewsats",
"github_project": "hub-sdk",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fewsats-hub"
}