From ab9f8b5ab87d9301311745bef9ffc639e7956d84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20G=C3=B3mez=20Maitret?= Date: Wed, 27 Apr 2022 21:03:35 -0500 Subject: [PATCH] New version. --- loaf/__init__.py | 25 +++++++++++++++++++++---- setup.py | 4 ++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/loaf/__init__.py b/loaf/__init__.py index 044b8c8..5d5894a 100644 --- a/loaf/__init__.py +++ b/loaf/__init__.py @@ -1,4 +1,4 @@ -import pymysql, datetime, socket +import pymysql, psycopg2, datetime, socket host_ = socket.gethostbyname(socket.gethostname()) port_ = 80 # Default XAMPP Apache server port. @@ -7,10 +7,11 @@ db_ = None creds_ = "" cursor_ = "DEFAULT" +mode_ = "MySQL" # Make this differently, for the love of god! -def bake(host=host_, port=port_, user=user_, pasw=pasw_, db=db_, creds=creds_, cursor=cursor_): - global host_, port_, user_, pasw_, db_, creds_, cursor_ +def bake(host=host_, port=port_, user=user_, pasw=pasw_, db=db_, creds=creds_, cursor=cursor_, mode=mode_): + global host_, port_, user_, pasw_, db_, creds_, cursor_, mode_ if host != "": host_=host if port != "": port_=port if user != "": user_=user @@ -18,10 +19,14 @@ def bake(host=host_, port=port_, user=user_, pasw=pasw_, db=db_, creds=creds_, c if db != "": db_=db if creds != "": creds_=creds if cursor != "": cursor_=cursor + if mode != "": mode_=mode # A query. def query(query): - conn = pymysql.connect(host=host_, port=port_, user=user_, passwd=pasw_, db=db_) + if (mode_ == "MySQL"): + conn = pymysql.connect(host=host_, port=port_, user=user_, passwd=pasw_, db=db_) + elif (mode_ == "PostgreSQL"): + conn = psycopg2.connect(host=host_, port=port_, user=user_, password=pasw_, database=db_) conn_object = conn.cursor(pymysql.cursors.DictCursor) if cursor_=="DICTIONARY" else conn.cursor() conn_object.execute(query) response = conn_object.fetchall() @@ -30,6 +35,18 @@ def query(query): conn.close() return response +# Test your connection with your database. +def test(): + try: + if (mode_ == "MySQL"): + conn = pymysql.connect(host=host_, port=port_, user=user_, passwd=pasw_, db=db_) + elif (mode_ == "PostgreSQL"): + conn = psycopg2.connect(host=host_, port=port_, user=user_, password=pasw_, database=db_) + print(f"Successful connection at: {host_}") + except Exception as ex: + print(f"Connection error at: {host_}") + print(ex) + # Call a stored procedure. def call(func, *args): call = "CALL " + func + "(" diff --git a/setup.py b/setup.py index 0a2e924..6a6f655 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages -VERSION = '0.1.11' -DESCRIPTION = 'Effortlessly access your MySQL server and procedures, plus some other utilities!' +VERSION = '0.1.12' +DESCRIPTION = 'Effortlessly access your SQL servers and procedures, plus some other utilities!' with open('README.md', encoding="utf8") as f: LONG_DESCRIPTION = f.read()