1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00

Allow extra db connection args. Set utf8mb4 as the default charset for MySQL

This commit is contained in:
spl0k 2018-02-21 22:13:35 +01:00
parent 3005e529a2
commit ba61281ba1
2 changed files with 17 additions and 7 deletions

View File

@ -27,7 +27,12 @@ URI is:
driver://username:password@host:port/database driver://username:password@host:port/database
Supported drivers are `sqlite`, `mysql` and `postgres` (or `postgresql`) If the connection needs some additional parameters, they can be provided as a
query string, such as:
driver://username:password@host:port/database?param1=value1&param2=value2
Supported drivers are `sqlite`, `mysql` and `postgres` (or `postgresql`).
As SQLite connects to local files, the format is slightly different. The "file" As SQLite connects to local files, the format is slightly different. The "file"
portion of the URI is the filename of the database. For a relative path, it portion of the URI is the filename of the database. For a relative path, it
@ -43,8 +48,10 @@ database_uri = sqlite:////home/user/supysonic.db
database_uri = sqlite:///C:\Users\user\supysonic.db database_uri = sqlite:///C:\Users\user\supysonic.db
``` ```
A MySQL-compatible database require either `MySQLdb` or `pymysql` to be A MySQL-compatible database requires either `MySQLdb` or `pymysql` to be
installed. PostgreSQL needs `psycopg2`. installed. PostgreSQL needs `psycopg2`.
Note that for MySQL if no character set is defined on the URI it defaults to
`utf8mb4` regardless of what's set on your MySQL installation.
If `database_uri` isn't provided, it defaults to a SQLite database stored in If `database_uri` isn't provided, it defaults to a SQLite database stored in
`/tmp/supysonic/supysonic.db`. `/tmp/supysonic/supysonic.db`.

View File

@ -31,9 +31,9 @@ from uuid import UUID, uuid4
from .py23 import dict, strtype from .py23 import dict, strtype
try: try:
from urllib.parse import urlparse from urllib.parse import urlparse, parse_qsl
except ImportError: except ImportError:
from urlparse import urlparse from urlparse import urlparse, parse_qsl
def now(): def now():
return datetime.now().replace(microsecond = 0) return datetime.now().replace(microsecond = 0)
@ -443,6 +443,8 @@ def parse_uri(database_uri):
raise TypeError('Expecting a string') raise TypeError('Expecting a string')
uri = urlparse(database_uri) uri = urlparse(database_uri)
args = dict(parse_qsl(uri.query))
if uri.scheme == 'sqlite': if uri.scheme == 'sqlite':
path = uri.path path = uri.path
if not path: if not path:
@ -450,11 +452,12 @@ def parse_uri(database_uri):
elif path[0] == '/': elif path[0] == '/':
path = path[1:] path = path[1:]
return dict(provider = 'sqlite', filename = path) return dict(provider = 'sqlite', filename = path, **args)
elif uri.scheme in ('postgres', 'postgresql'): elif uri.scheme in ('postgres', 'postgresql'):
return dict(provider = 'postgres', user = uri.username, password = uri.password, host = uri.hostname, database = uri.path[1:]) return dict(provider = 'postgres', user = uri.username, password = uri.password, host = uri.hostname, dbname = uri.path[1:], **args)
elif uri.scheme == 'mysql': elif uri.scheme == 'mysql':
return dict(provider = 'mysql', user = uri.username, passwd = uri.password, host = uri.hostname, db = uri.path[1:]) args.setdefault('charset', 'utf8mb4')
return dict(provider = 'mysql', user = uri.username, passwd = uri.password, host = uri.hostname, db = uri.path[1:], **args)
return dict() return dict()
def init_database(database_uri, create_tables = False): def init_database(database_uri, create_tables = False):