79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
import socket,fdb,pymysql
|
|
from pymysql.cursors import DictCursor
|
|
|
|
import pymysql
|
|
from pymysql.cursors import DictCursor
|
|
import socket
|
|
|
|
|
|
def get_mysql_connection(cursor_mode=None):
|
|
"""
|
|
Return a PyMySQL connection.
|
|
If cursor_mode == "DICT", return connection with DictCursor.
|
|
Otherwise, return default tuple cursor connection.
|
|
"""
|
|
hostname = socket.gethostname().strip()
|
|
|
|
# decide cursor class
|
|
cursor_cls = DictCursor if cursor_mode == "DICT" else None
|
|
|
|
if hostname in ("NTBVBHP470G10", "Z230"):
|
|
MYSQL_CFG = dict(
|
|
host="192.168.1.76",
|
|
port=3307,
|
|
user="root",
|
|
password="Vlado9674+",
|
|
database="medevio",
|
|
autocommit=True,
|
|
)
|
|
elif hostname == "SESTRA":
|
|
MYSQL_CFG = dict(
|
|
host="127.0.0.1",
|
|
port=3307,
|
|
user="root",
|
|
password="Vlado9674+",
|
|
database="medevio",
|
|
autocommit=True,
|
|
)
|
|
else:
|
|
print(f"Unknown host: {hostname}")
|
|
return None
|
|
|
|
# include cursorclass only if we want a dict cursor
|
|
if cursor_cls is not None:
|
|
MYSQL_CFG["cursorclass"] = cursor_cls
|
|
|
|
try:
|
|
return pymysql.connect(**MYSQL_CFG)
|
|
except pymysql.MySQLError as e:
|
|
print(f"MySQL connection failed: {e}")
|
|
return None
|
|
|
|
|
|
def get_medicus_connection():
|
|
"""
|
|
Attempt to create a Firebird connection to the Medicus database.
|
|
Returns:
|
|
fdb.Connection object on success
|
|
None on failure
|
|
"""
|
|
if socket.gethostname().strip() in ("NTBVBHP470G10","Z230"):
|
|
MEDICUS_CFG = dict(
|
|
dsn=r"192.168.1.4:z:\medicus 3\data\medicus.fdb",
|
|
user="SYSDBA",
|
|
password="masterkey",
|
|
charset="win1250",
|
|
)
|
|
elif socket.gethostname().strip()=="SESTRA":
|
|
MEDICUS_CFG = dict(
|
|
dsn=r"192.168.1.10:m:\medicus\data\medicus.fdb",
|
|
user="SYSDBA",
|
|
password="masterkey",
|
|
charset="win1250",
|
|
)
|
|
|
|
try:
|
|
return fdb.connect(**MEDICUS_CFG)
|
|
except fdb.fbcore.DatabaseError as e:
|
|
print(f"Medicus DB connection failed: {e}")
|
|
return None |