This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added in-built functions to query postgres tables. - Added ChangeLog to docs. - Improve documentation using sphinx-toolbox.
- Loading branch information
1 parent
20799a1
commit c81c716
Showing
10 changed files
with
356 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
ChangeLog | ||
========= | ||
|
||
Latest Version - ``v0.2.5`` | ||
|
||
------------------ | ||
|
||
.. confval:: v0.2.5 | ||
|
||
- Added in-built functions to query postgres tables - :ref:`Read More <default-functions>` | ||
- Added ChangeLog to docs (this webpage) | ||
- Improve documentation using sphinx-toolbox | ||
|
||
|
||
.. confval:: v0.2.4 | ||
|
||
- This Documentation was created |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
sphinx-rtd-theme | ||
sphinx-copybutton | ||
sphinx-rtd-dark-mode | ||
# sphinx-toolbox | ||
sphinx-toolbox |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
Querying Postgres Tables | ||
======================== | ||
|
||
In this guide, you will learn how to query using postgres tables while using PyStark | ||
|
||
.. note:: | ||
|
||
This feature is still in beta. There are a lot of things to add like default classes, alembic support, etc and this is just a pre-release. | ||
|
||
.. contents:: Contents | ||
:backlinks: none | ||
:depth: 1 | ||
:local: | ||
|
||
-------- | ||
|
||
.. _default-functions: | ||
|
||
Using the in-built functions | ||
---------------------------- | ||
|
||
PyStark provides some default functions to query postgres tables. These functions allow you to query tables using table name (``__tablename__`` attribute), that is, a string instead of a class. Therefore, you do not need to import classes. | ||
|
||
.. note:: | ||
|
||
All the in-built functions are asynchronous. | ||
|
||
.. list-table:: In-built Functions | ||
:widths: 25 75 | ||
:header-rows: 1 | ||
|
||
* - Name | ||
- Function | ||
* - **all_db** | ||
- :ref:`Get All Rows <all_db>` | ||
* - **get_db** | ||
- :ref:`Get a Particular Row <get_db>` | ||
* - **count_db** | ||
- :ref:`Get Number of Rows <count_db>` | ||
* - **set_db** | ||
- :ref:`Set/Update value of a key in a Row <set_db>` | ||
* - **delete_db** | ||
- :ref:`Delete a Row <delete_db>` | ||
|
||
.. _all_db: | ||
|
||
- **Get All Rows** | ||
|
||
.. code-block:: python | ||
from pystark.database.postgres import all_db | ||
# Get all rows from "users" table as dicts. | ||
async def get_users(): | ||
all_data = await all_db("users") | ||
print(all_data) | ||
.. _get_db: | ||
|
||
- **Get a Particular Row** | ||
|
||
.. code-block:: python | ||
from pystark.database.postgres import get_db | ||
# Get row using primary key from "users" table. | ||
async def get_user(): | ||
user_id = 500123456 # primary key | ||
get_data = await get_db("users", user_id) | ||
print(get_data) | ||
.. _count_db: | ||
|
||
- **Get Number of Rows** | ||
|
||
.. code-block:: python | ||
from pystark.database.postgres import count_db | ||
# Get number of rows in "users" table. | ||
async def user_count(): | ||
count = await count_db("users") | ||
print(count) | ||
.. _set_db: | ||
|
||
- **Set/Update value of a key in a Row** | ||
|
||
.. code-block:: python | ||
from pystark.database.postgres import set_db | ||
# set/update key, value pairs in "users" table. | ||
async def set_data(): | ||
user_id = 500123456 # primary key | ||
key_to_change = "aim" | ||
new_value = "programmer" | ||
set_data = await set_db("users", user_id, key_to_change, new_value) | ||
print("Set") | ||
.. _delete_db: | ||
|
||
- **Delete a Row** | ||
|
||
.. code-block:: python | ||
from pystark.database.postgres import delete_db | ||
# Delete a row using primary key from "users" table. | ||
async def delete_user(): | ||
user_id = 500123456 | ||
delete_data = await delete_db("users", user_id) | ||
print("Deleted") | ||
-------- | ||
|
||
.. _session-object: | ||
|
||
Using the Regular Way (Session object) | ||
-------------------------------------- | ||
|
||
You can query tables using the ``Session`` object which is the regular way in sqlalchemy. | ||
|
||
|
||
.. code-block:: python | ||
# import 'Session' object | ||
from pystark.database.postgres import Session | ||
# import Python class for respective table | ||
# let's say it is in 'users_sql.py' inside 'database' folder. | ||
from database.users_sql import Users | ||
# This function gives total 'rows', that is total user ids in 'users' table. | ||
def num_users(): | ||
users = Session.query(Users).count() | ||
# close session after all queries are made. | ||
Session.close() | ||
return users | ||
# This function returns 'name' and 'aim' for users by using 'user_id' | ||
def get_name_and_aim(user_id): | ||
query = Session.query(Users).get(user_id) | ||
name = query.name # get name | ||
aim = query.aim # get aim | ||
Session.close() | ||
return (name, aim) | ||
# This function sets name and aim for users by using 'user_id' | ||
def set_name_and_aim(user_id, name, aim): | ||
query = Session.query(Users).get(user_id) | ||
query.name = name # set name | ||
query.aim = aim # set aim | ||
Session.commit() # use this after setting anything. | ||
# Now you don't need to 'Session.close()' as you used 'Session.commit()' already. | ||
# Etc |
Oops, something went wrong.