mirror of
https://github.com/Xevion/postgres-test.git
synced 2025-12-06 07:15:47 -06:00
Add dataclass types, setup SQL seed scripts
This commit is contained in:
2
main.py
2
main.py
@@ -1,5 +1,5 @@
|
|||||||
from psycopg2 import connect
|
from psycopg2 import connect
|
||||||
from seed import main
|
from src.seed import main
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
conn = connect(dbname='railway', user='postgres', password='8h9hxh5YeBwfqTCrxQQb',
|
conn = connect(dbname='railway', user='postgres', password='8h9hxh5YeBwfqTCrxQQb',
|
||||||
|
|||||||
35
src/sql.py
Normal file
35
src/sql.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
from psycopg2._psycopg import cursor
|
||||||
|
|
||||||
|
from types import Account, Posting, PostingFilter
|
||||||
|
|
||||||
|
|
||||||
|
def add_account(cursor: cursor, data: Account) -> int:
|
||||||
|
"""Adds an account to the database."""
|
||||||
|
cursor.execute('INSERT INTO Account'
|
||||||
|
'(first_name, last_name, email)'
|
||||||
|
'VALUES (%s, %s, %s)'
|
||||||
|
'RETURNING id',
|
||||||
|
(data.first_name, data.last_name, data.email))
|
||||||
|
return cursor.fetchone()[0]
|
||||||
|
|
||||||
|
|
||||||
|
def add_posting(cursor: cursor, data: Posting) -> int:
|
||||||
|
"""Adds a posting to the database."""
|
||||||
|
cursor.execute('INSERT INTO Posting'
|
||||||
|
'(name, date, description, a, b, c)'
|
||||||
|
'VALUES (%s, %s, %s, %s, %s, %s)'
|
||||||
|
'RETURNING id',
|
||||||
|
(data.name, data.date, data.description, data.a, data.b, data.c))
|
||||||
|
return cursor.fetchone()[0]
|
||||||
|
|
||||||
|
|
||||||
|
def add_posting_filter(cursor: cursor, data: PostingFilter) -> int:
|
||||||
|
"""Adds a posting filter to the database."""
|
||||||
|
b_lower, b_upper = data.b if data.b else (None, None)
|
||||||
|
c_lower, c_upper = data.c if data.c else (None, None)
|
||||||
|
cursor.execute('INSERT INTO PostingFilter'
|
||||||
|
'(creator, name, expires, a, b_lower, b_upper, c_lower, c_upper)'
|
||||||
|
'VALUES (%s, %s, %s, %s, %s, %s)'
|
||||||
|
'RETURNING id',
|
||||||
|
(data.creator, data.name, data.expires, data.a, b_lower, b_upper, c_lower, c_upper))
|
||||||
|
return cursor.fetchone()[0]
|
||||||
32
src/types.py
Normal file
32
src/types.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Posting:
|
||||||
|
name: str
|
||||||
|
date: datetime
|
||||||
|
description: str
|
||||||
|
|
||||||
|
a: bool
|
||||||
|
b: int
|
||||||
|
c: float
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class PostingFilter:
|
||||||
|
creator: int
|
||||||
|
name: str
|
||||||
|
expires: datetime
|
||||||
|
|
||||||
|
a: Optional[bool]
|
||||||
|
b: Optional[Tuple[int, int]]
|
||||||
|
c: Optional[Tuple[float, float]]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Account:
|
||||||
|
first_name: str
|
||||||
|
last_name: str
|
||||||
|
email: str
|
||||||
Reference in New Issue
Block a user