Skip to content
Open
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
35 changes: 35 additions & 0 deletions database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from psycopg2.pool import ThreadedConnectionPool
from functools import lru_cache


class Database:
def __init__(self, database: str = 'postgres', host: str = 'localhost', password: str = 'secret', user: str = 'root', port: str = '5432'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iki haram mas nek neng toped. Nek password dll di commit / dimasukke github

self.__database = database
self.__host = host
self.__password = password
self.__user = user
self.__port = port
self.__pool = self.__create_connection()

def __create_connection(self):
try:
pool_connection = ThreadedConnectionPool(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kenapaa milih ThreadedConnectionPool daripada SimpleConnectionPool mass? Cmiiw

minconn=1,
maxconn=10,
dsn=f'postgresql://{self.__user}:{self.__password}@{self.__host}:{self.__port}/{self.__database}?application_name=python-postgresql-conn'
)
return pool_connection
except Exception as e:
raise ValueError(e)

def get_pool_conn(self):
return self.__pool.getconn()

def put_pool_conn(self, conn):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kalaau di init tadi kayanya pakai fitur generic di fungsi atas mas. Tapi kalau disini ndak pakai generic jadinya kurang konsisten mass
contoh generic itu yang ada tambahan kaya gini mas (self, text str)

return self.__pool.putconn(conn)

database_obj = Database()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ikii kayanee nek codingane koyo ngene setiap manggil file iki bakal deklarasi ulang mas.
Tapi jujur iki aku yo rareti bener opo ora mas. Soale nek bahasa lain biasane do nggo pointer

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soalee biyen aku yo coba coba tapi durung nemu sg paling bener kepiye mas


@lru_cache
def get_connection() -> Database:
return database_obj
44 changes: 44 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from database import get_connection


class RepositoryPostgresql:
def __init__(self):
self.__conn = get_connection()

def get_all_data(self):
pool = self.__conn.get_pool_conn()
try:
cur = pool.cursor()
sql = """
select * from tbl_job_desc limit 10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ikii nek neng tutorial tutorial youtube / neng tempatku. Ojoo select * mass. Lebih baik select semua column di definisi satu" kaya
select column1, column2 dan seterusnyaa

"""
cur.execute(sql)
return cur.fetchall()
except Exception as error:
raise error
finally:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cursornyaa kalau error belum ditutup mass (cur)

self.__conn.put_pool_conn(pool)

def get_one_data(self, id):
pool = self.__conn.get_pool_conn()
try:
cur = pool.cursor()
sql = """
select * from tbl_job_desc where id = %s limit 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inii sql injection kayanya mas. Kalau gasalah kan biar ga kena sql injection paramnya dimasukin ke execute itu mas, atau udahh update baru yak modulnya mas? Cmiiw

"""%(id)
cur.execute(sql)
return cur.fetchone()
except Exception as error:
raise error
finally:
self.__conn.put_pool_conn(pool)


if __name__ == '__main__':
databaseRepo = RepositoryPostgresql()

data = databaseRepo.get_all_data()
print(data)

data = databaseRepo.get_one_data(1)
print(data)