-
Notifications
You must be signed in to change notification settings - Fork 1
Review dit #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Review dit #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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'): | ||
| 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( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| return self.__pool.putconn(conn) | ||
|
|
||
| database_obj = Database() | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| """ | ||
| cur.execute(sql) | ||
| return cur.fetchall() | ||
| except Exception as error: | ||
| raise error | ||
| finally: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment.
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