python-module-pgsql/manage_psql.py

111 lines
3.1 KiB
Python
Raw Permalink Normal View History

2019-12-03 11:11:52 +01:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Module psql connection."""
# /*
# * ----------------------------------------------------------------------------
# * "THE BEER-WARE LICENSE" (Revision 42):
# * <boris.tassou@securmail.fr> wrote this file. As long as you retain this notice you
# * can do whatever you want with this stuff. If we meet some day, and you think
# * this stuff is worth it, you can buy me a beer in return Boris Tassou
# * ----------------------------------------------------------------------------
# */
2019-12-03 12:12:25 +01:00
import socket
2020-05-15 14:08:17 +02:00
from configparser import ConfigParser
2019-12-03 11:11:52 +01:00
import psycopg2
2019-12-03 12:12:25 +01:00
# Read conf.ini
2020-05-15 14:08:17 +02:00
INI_CONF = "./config/psql_conf.ini"
CONFIG = ConfigParser()
2019-12-03 12:12:25 +01:00
CONFIG.read(INI_CONF)
# DB parameters
PSQL_HOST = CONFIG.get('psql', 'host')
PSQL_DB = CONFIG.get('psql', 'database')
PSQL_USER = CONFIG.get('psql', 'user')
PSQL_PWD = CONFIG.get('psql', 'passwd')
2019-12-03 11:11:52 +01:00
class HandlePsql:
"""Class to handle psql connection."""
conn = None
cur = None
@staticmethod
2019-12-03 12:12:25 +01:00
def psql_checkport():
"""Check pgsql port."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
check_port = sock.connect_ex((PSQL_HOST, 5432))
if check_port:
return True
return False
2019-12-03 11:11:52 +01:00
2019-12-03 12:12:25 +01:00
@staticmethod
def psql_checkcon():
"""Check pgsql connexion to database."""
try:
conn = psycopg2.connect(host=PSQL_HOST, database=PSQL_DB,
user=PSQL_USER, password=PSQL_PWD,
connect_timeout=5)
conn.close()
return True
except conn.error:
return False
2019-12-03 11:11:52 +01:00
2019-12-03 12:12:25 +01:00
@staticmethod
def psql_conn():
"""Init psql connection."""
2019-12-03 11:11:52 +01:00
# connexion DB
# http://initd.org/psycopg/docs/usage.html
# http://www.postgresqltutorial.com/postgresql-python/
try:
global conn
2019-12-03 12:12:25 +01:00
conn = psycopg2.connect(host=PSQL_HOST, database=PSQL_DB,
user=PSQL_USER, password=PSQL_PWD)
2019-12-03 11:11:52 +01:00
conn.cursor()
2019-12-03 12:12:25 +01:00
except psycopg2.DatabaseError as error:
2019-12-03 11:11:52 +01:00
print(error)
@staticmethod
def psql_select(sql_select):
"""Psql select request."""
cur = conn.cursor()
cur.execute(sql_select)
sql_return = cur.fetchall()
return sql_return
@staticmethod
def psql_delete(sql_delete):
"""Psql delte request."""
cur = conn.cursor()
cur.execute(sql_delete)
conn.commit()
@staticmethod
def psql_insert(sql_insert):
"""Psql insert request."""
cur = conn.cursor()
cur.execute(sql_insert)
conn.commit()
@staticmethod
def psql_update(sql_update):
"""Psql insert request."""
cur = conn.cursor()
cur.execute(sql_update)
conn.commit()
@staticmethod
def psql_unconn():
"""End psql connection."""
2019-12-03 12:12:25 +01:00
global conn
2019-12-03 11:11:52 +01:00
cur = conn.cursor()
cur.close()
conn.close()
2019-12-03 12:12:25 +01:00
2019-12-03 11:11:52 +01:00
if __name__ == "__main__":
print("Module psql to handle the connection")