Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,5 @@ dmypy.json
# Pyre type checker
.pyre/
main.py
test.py
test.py
pyproject_backup.toml
51 changes: 0 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ $ pip install postgres-dynamic
</div>

## Parameter Format:
- connection_string: dict
```Python
connection_string = {
'PG_HOST': 'YOUR_CONNETION_HOST_ADDRESS',
'PG_DATABASE': 'YOUR_CONNECTION_DATABASE_NAME',
'PG_USER': 'YOUR_CONNECTION_USERNAME',
'PG_PASSWORD': 'YOUR_CONNECTION_PASSWORD',
}
```
- connection_object: Callable
```Python
connection_object = psycopg2.connect(host,port,database,user,password) #object created from psycopg2.connect()
Expand Down Expand Up @@ -118,7 +109,6 @@ Example DB

Parameters:
```
connection_string #required
main_table #required
where #required
join_table #optional (if omitted it won't join to any table)
Expand All @@ -133,12 +123,6 @@ Example DB
import asyncio

query_result = PGDGet.get_one(
connection_string={
'PG_HOST': 'localhost', #using default port 5432
'PG_DATABASE': 'postgres',
'PG_USER': 'postgres',
'PG_PASSWORD': 'password'
},
main_table={'table': 'employees'},
where=[
{'column_name': 'id', 'value': '1'},
Expand All @@ -156,12 +140,6 @@ Example DB
# with join table salaries

query_result = PGDGet.get_one(
connection_string={
'PG_HOST': 'localhost', #using default port 5432
'PG_DATABASE': 'postgres',
'PG_USER': 'postgres',
'PG_PASSWORD': 'password'
},
main_table={'table': 'employees', 'alias': 'emp'},
join_table=[
{'table': 'salaries', 'alias': 'sal', 'join_method': 'INNER', 'on': 'emp.id = sal.employee_id'}
Expand All @@ -185,7 +163,6 @@ Example DB
<summary>Show more...</summary>

```
connection_string #required
main_table #required
where #optional (if omitted no condition will be passed)
join_table #optional (if omitted it won't join to any table)
Expand All @@ -206,12 +183,6 @@ Example DB
import asyncio

query_result = PGDGet.get_all(
connection_string={
'PG_HOST': 'localhost', #using default port 5432
'PG_DATABASE': 'postgres',
'PG_USER': 'postgres',
'PG_PASSWORD': 'password'
},
main_table={'table': 'employees'},
limit=3,
offset=2
Expand All @@ -231,7 +202,6 @@ Example DB
<summary>Show more...</summary>

```
connection_string #required
main_table #required
where #optional (if omitted no condition will be passed)
join_table #optional (if omitted it won't join to any table)
Expand All @@ -243,12 +213,6 @@ Example DB
import asyncio

query_result = PGDGet.get_count(
connection_string={
'PG_HOST': 'localhost', #using default port 5432
'PG_DATABASE': 'postgres',
'PG_USER': 'postgres',
'PG_PASSWORD': 'password'
},
main_table={'table': 'employees'},
where=[{'column_name': 'first_name', 'value': 'Alex'}]
)
Expand All @@ -270,7 +234,6 @@ Example DB

Parameters:
```
connection_object #required
main_table #required
column_and_value #required
commit #optional (if omitted, default value will be False which will not saving any changes to database)
Expand All @@ -283,9 +246,7 @@ Example DB
from postgres_dynamic import PGDTransaction
import asyncio

connection_object = psycopg2.connect(database='postgres', host='localhost', port=5432, user='postgres', password='password')
query_result = PGDTransaction.insert(
connection_object=connection_object,
main_table='employees',
column_and_value={'id': 6, 'first_name': 'Harrison', 'last_name': 'Ford'},
commit=True
Expand All @@ -301,9 +262,7 @@ Example DB
```Python
# without auto commit

connection_object = psycopg2.connect(database='postgres', host='localhost', port=5432, user='postgres', password='password')
query_result = PGDTransaction.insert(
connection_object=connection_object,
main_table='salaries',
column_and_value={'employee_id': 6, 'salary': 250000},
)
Expand All @@ -330,7 +289,6 @@ Example DB

Parameters:
```
connection_object #required
main_table #required
column_and_value #required
where #required
Expand All @@ -344,9 +302,7 @@ Example DB
from postgres_dynamic import PGDTransaction
import asyncio

connection_object = psycopg2.connect(database='postgres', host='localhost', port=5432, user='postgres', password='password')
query_result = PGDTransaction.update(
connection_object=connection_object,
main_table='employees',
column_and_value={'first_name': 'Tyler', 'last_name': 'Oakley'},
where=[
Expand All @@ -365,9 +321,7 @@ Example DB
```Python
# without auto commit

connection_object = psycopg2.connect(database='postgres', host='localhost', port=5432, user='postgres', password='password')
query_result = PGDTransaction.update(
connection_object=connection_object,
main_table='salaries',
column_and_value={'salary': 450000},
where=[
Expand Down Expand Up @@ -398,7 +352,6 @@ Example DB

Parameters:
```
connection_object #required
main_table #required
where #required
commit #optional (if omitted, default value will be False which will not saving any changes to database)
Expand All @@ -411,9 +364,7 @@ Example DB
from postgres_dynamic import PGDTransaction
import asyncio

connection_object = psycopg2.connect(database='postgres', host='localhost', port=5432, user='postgres', password='password')
query_result = PGDTransaction.delete(
connection_object=connection_object,
main_table='salaries',
where=[
{'column_name': 'employee_id', 'value': '6'},
Expand All @@ -431,9 +382,7 @@ Example DB
```Python
# without auto commit

connection_object = psycopg2.connect(database='postgres', host='localhost', port=5432, user='postgres', password='password')
query_result = PGDTransaction.delete(
connection_object=connection_object,
main_table='employees',
where=[
{'column_name': 'id', 'value': '6'},
Expand Down
7 changes: 2 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ build-backend = "setuptools.build_meta"

[project]
name = "postgres-dynamic"
version = "0.0.6"
version = "0.0.11"
authors = [
{ name="Bayu Satria Gemilang", email="bayu.code.lab@gmail.com" },
]
description = "Fast API Google Cloud Storage"
description = "Postgres Dynamic Query"
readme = "README.md"
dependencies = [
"psycopg2>=2.9.3"
]
requires-python = ">=3.7"
classifiers = [
"Programming Language :: Python :: 3",
Expand Down
21 changes: 0 additions & 21 deletions src/postgres_dynamic/pgd_connection.py

This file was deleted.

72 changes: 15 additions & 57 deletions src/postgres_dynamic/pgd_get.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
from typing import Awaitable, Optional
from datetime import datetime, timedelta
from postgres_dynamic.pgd_connection import PGDConnection

class PGDGet:
@classmethod
async def get_one(cls, connection_string: dict, main_table: dict, where: list, join_table:list = [], column_name: list = None) -> Awaitable[dict]:
async def get_one(cls, main_table: dict, where: list, join_table:list = [], column_name: list = None) -> Awaitable[dict]:
"""Get one always return a single value from the query, based on fetchone in psycopg2 and returning a dictionary with {column_name: value} of the tables.
Please refer to documentation for thorough guide.

Example parameters:

connection_string={
'PG_HOST': 'localhost', #using default port 5432
'PG_DATABASE': 'postgres',
'PG_USER': 'postgres',
'PG_PASSWORD': 'password'
}

main_table={'table': 'employees', 'alias': 'emp'} #alias can be omitted

join_table=[
Expand All @@ -32,7 +23,6 @@ async def get_one(cls, connection_string: dict, main_table: dict, where: list, j
column_name=['first_name'] #can be omitted, when omitted it equals as `select * from blabla`
"""
try:
result = {}
column_name = ','.join(column_name) if column_name else '*'
query = """
SELECT {column_name} FROM {main_table} {main_table_alias} {join_table} WHERE {where_query}
Expand All @@ -45,36 +35,23 @@ async def get_one(cls, connection_string: dict, main_table: dict, where: list, j

join_query = ''
for i in join_table:
join_query += ' INNER JOIN' if i['join_method'] == 'INNER' else ' LEFT JOIN' if i['join_method'] == 'LEFT' else ' RIGHT JOIN'
join_query += ' INNER JOIN' if i['join_method'] == 'INNER' else ' LEFT JOIN' if i['join_method'] == 'LEFT' else ' RIGHT JOIN' if i['join_method'] == 'RIGHT' else ' FULL JOIN' if i['join_method'] == 'FULL' else ' JOIN'
join_query += f' {i["table"]} {i["alias"]} ON {i["on"]}'

query = query.format(column_name=column_name, main_table=main_table['table'], main_table_alias=main_table['alias'] if main_table.get('alias') else '', join_table=join_query, where_query=where_query)
where_values = tuple(wv['value'] for wv in where)
values = where_values
with PGDConnection(connection_string, query, values) as cursor:
column_name = [i[0] for i in cursor.description]
data = cursor.fetchone()
if data:
for a, b in zip(column_name, data):
result[a] = str(b + timedelta(hours=7)) if type(b) is datetime else b
return result
where_value = tuple(wv['value'] for wv in where)
value = where_value
return query, value
except Exception as e:
raise e

@classmethod
async def get_all(cls, connection_string: dict, main_table: dict, where: list = [], join_table:list = [], order: dict = {}, column_name: list = None, limit: Optional[int] = None, offset: Optional[int] = None) -> Awaitable[dict]:
async def get_all(cls, main_table: dict, where: list = [], join_table:list = [], order: dict = {}, column_name: list = None, limit: Optional[int] = None, offset: Optional[int] = None) -> Awaitable[dict]:
"""Get all always return a dict with key data, based on fetchall in psycopg2 and returning a list of dictionary with {column_name: value} of the tables.
Please refer to documentation for thorough guide.

Example parameters:

connection_string={
'PG_HOST': 'localhost', #using default port 5432
'PG_DATABASE': 'postgres',
'PG_USER': 'postgres',
'PG_PASSWORD': 'password'
}

main_table={'table': 'employees', 'alias': 'emp'} #alias can be omitted

join_table=[
Expand All @@ -96,7 +73,6 @@ async def get_all(cls, connection_string: dict, main_table: dict, where: list =
offset=1 #optional
"""
try:
result = []
column_name = ','.join(column_name) if column_name else '*'
query = """
SELECT {column_name} FROM {main_table} {main_table_alias} {join_table} {where_query} {order_query} LIMIT %s OFFSET %s
Expand All @@ -115,38 +91,23 @@ async def get_all(cls, connection_string: dict, main_table: dict, where: list =

join_query = ''
for i in join_table:
join_query += ' INNER JOIN' if i['join_method'] == 'INNER' else ' LEFT JOIN' if i['join_method'] == 'LEFT' else ' RIGHT JOIN'
join_query += ' INNER JOIN' if i['join_method'] == 'INNER' else ' LEFT JOIN' if i['join_method'] == 'LEFT' else ' RIGHT JOIN' if i['join_method'] == 'RIGHT' else ' FULL JOIN' if i['join_method'] == 'FULL' else ' JOIN'
join_query += f' {i["table"]} {i["alias"]} ON {i["on"]}'

query = query.format(column_name=column_name, main_table=main_table['table'], main_table_alias=main_table['alias'] if main_table.get('alias') else '', join_table=join_query, where_query=where_query, order_query=order_query)
where_values = tuple(wv['value'] for wv in where)
where_value = tuple(wv['value'] for wv in where)
offset_value = (limit*(offset-1)) if offset else None
values = where_values + (limit, offset_value)
with PGDConnection(connection_string, query, values) as cursor:
column_name = [i[0] for i in cursor.description]
for i in cursor.fetchall():
data = {}
for a, b in zip(column_name, i):
data[a] = str(b + timedelta(hours=7)) if type(b) is datetime else b
result.append(data)
return {'data': result}
value = where_value + (limit, offset_value)
return query, value
except Exception as e:
raise e

@classmethod
async def get_count(cls, connection_string: dict, main_table: dict, where: list = [], join_table:list = []) -> Awaitable[dict]:
async def get_count(cls, main_table: dict, where: list = [], join_table:list = []) -> Awaitable[dict]:
"""Get count always return a dict with key total_data, based on select count(*) in SQL and returning a dictionary with {total_data: value} of the query.
Please refer to documentation for thorough guide.

Example parameters:

connection_string={
'PG_HOST': 'localhost', #using default port 5432
'PG_DATABASE': 'postgres',
'PG_USER': 'postgres',
'PG_PASSWORD': 'password'
}

main_table={'table': 'employees', 'alias': 'emp'} #alias can be omitted

join_table=[
Expand All @@ -160,7 +121,6 @@ async def get_count(cls, connection_string: dict, main_table: dict, where: list
#conjunction only required when providing more than one dictionary
"""
try:
result = []
query = """
SELECT COUNT(*) FROM {main_table} {main_table_alias} {join_table} {where_query}
"""
Expand All @@ -174,14 +134,12 @@ async def get_count(cls, connection_string: dict, main_table: dict, where: list

join_query = ''
for i in join_table:
join_query += ' INNER JOIN' if i['join_method'] == 'INNER' else ' LEFT JOIN' if i['join_method'] == 'LEFT' else ' RIGHT JOIN'
join_query += ' INNER JOIN' if i['join_method'] == 'INNER' else ' LEFT JOIN' if i['join_method'] == 'LEFT' else ' RIGHT JOIN' if i['join_method'] == 'RIGHT' else ' FULL JOIN' if i['join_method'] == 'FULL' else ' JOIN'
join_query += f' {i["table"]} {i["alias"]} ON {i["on"]}'

query = query.format(main_table=main_table['table'], main_table_alias=main_table['alias'] if main_table.get('alias') else '', join_table=join_query, where_query=where_query)
where_values = tuple(wv['value'] for wv in where)
values = where_values
with PGDConnection(connection_string, query, values) as cursor:
result = cursor.fetchone()[0]
return {'total_data': result}
where_value = tuple(wv['value'] for wv in where)
value = where_value
return query, value
except Exception as e:
raise e
Loading