mirror of
https://github.com/skoelle/calender_sync.git
synced 2026-09-17 18:20:24 +00:00
38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
# Copyright (c) 2026 Stefan Koelle (https://stefankoelle.de)
|
|
# Licensed under the MIT License. See LICENSE file in project root for details.
|
|
|
|
import os
|
|
import sqlite3
|
|
|
|
DB_BACKEND = os.environ.get("DB_BACKEND", "mysql")
|
|
|
|
if DB_BACKEND == "sqlite":
|
|
DB_PATH = os.environ.get("DB_PATH", "demo.db")
|
|
|
|
def get_connection(autocommit: bool = True):
|
|
conn = sqlite3.connect(DB_PATH)
|
|
conn.execute("PRAGMA journal_mode=WAL")
|
|
conn.execute("PRAGMA foreign_keys=ON")
|
|
conn.row_factory = sqlite3.Row
|
|
if autocommit:
|
|
conn.isolation_level = None
|
|
return conn
|
|
else:
|
|
import mysql.connector
|
|
|
|
DB_HOST = os.environ.get("DB_HOST", "mariadb.fritz.box")
|
|
DB_PORT = int(os.environ.get("DB_PORT", "3306"))
|
|
DB_NAME = os.environ.get("DB_NAME", "calendar_sync")
|
|
DB_USER = os.environ["DB_USER"]
|
|
DB_PASSWORD = os.environ["DB_PASSWORD"]
|
|
|
|
def get_connection(autocommit: bool = True):
|
|
return mysql.connector.connect(
|
|
host=DB_HOST,
|
|
port=DB_PORT,
|
|
user=DB_USER,
|
|
password=DB_PASSWORD,
|
|
database=DB_NAME,
|
|
autocommit=autocommit,
|
|
)
|