13 engine
= None # See get_engine
16 def get_sqlalchemy_url():
18 Build an SQLAlchemy for use with create_engine based on the aurweb configuration.
21 aur_db_backend
= aurweb
.config
.get('database', 'backend')
22 if aur_db_backend
== 'mysql':
23 return sqlalchemy
.engine
.url
.URL(
24 'mysql+mysqlconnector',
25 username
=aurweb
.config
.get('database', 'user'),
26 password
=aurweb
.config
.get('database', 'password'),
27 host
=aurweb
.config
.get('database', 'host'),
28 database
=aurweb
.config
.get('database', 'name'),
30 'unix_socket': aurweb
.config
.get('database', 'socket'),
34 elif aur_db_backend
== 'sqlite':
35 return sqlalchemy
.engine
.url
.URL(
37 database
=aurweb
.config
.get('database', 'name'),
40 raise ValueError('unsupported database backend')
45 Return the global SQLAlchemy engine.
47 The engine is created on the first call to get_engine and then stored in the
48 `engine` global variable for the next calls.
50 from sqlalchemy
import create_engine
53 engine
= create_engine(get_sqlalchemy_url(),
54 # check_same_thread is for a SQLite technicality
55 # https://fastapi.tiangolo.com/tutorial/sql-databases/#note
56 connect_args
={"check_same_thread": False})
62 Return an SQLAlchemy connection. Connections are usually pooled. See
63 <https://docs.sqlalchemy.org/en/13/core/connections.html>.
65 Since SQLAlchemy connections are context managers too, you should use it
66 with Python’s `with` operator, or with FastAPI’s dependency injection.
68 return get_engine().connect()
76 aur_db_backend
= aurweb
.config
.get('database', 'backend')
78 if aur_db_backend
== 'mysql':
79 aur_db_host
= aurweb
.config
.get('database', 'host')
80 aur_db_name
= aurweb
.config
.get('database', 'name')
81 aur_db_user
= aurweb
.config
.get('database', 'user')
82 aur_db_pass
= aurweb
.config
.get('database', 'password')
83 aur_db_socket
= aurweb
.config
.get('database', 'socket')
84 self
._conn
= mysql
.connector
.connect(host
=aur_db_host
,
88 unix_socket
=aur_db_socket
,
90 self
._paramstyle
= mysql
.connector
.paramstyle
91 elif aur_db_backend
== 'sqlite':
92 aur_db_name
= aurweb
.config
.get('database', 'name')
93 self
._conn
= sqlite3
.connect(aur_db_name
)
94 self
._paramstyle
= sqlite3
.paramstyle
96 raise ValueError('unsupported database backend')
98 def execute(self
, query
, params
=()):
99 if self
._paramstyle
in ('format', 'pyformat'):
100 query
= query
.replace('%', '%%').replace('?', '%s')
101 elif self
._paramstyle
== 'qmark':
104 raise ValueError('unsupported paramstyle')
106 cur
= self
._conn
.cursor()
107 cur
.execute(query
, params
)