db: Move SQLite Connection stuff into a new file
I find this cleaner than importing from the toplevel module Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
96a6bb1687
commit
9cf1df7c33
|
@ -1,16 +1,7 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import lib
|
||||
import pathlib
|
||||
import sqlite3
|
||||
|
||||
File = lib.data.emmental_data / "emmental.sqlite3"
|
||||
|
||||
Connection = sqlite3.connect(File, detect_types=sqlite3.PARSE_DECLTYPES)
|
||||
Connection.row_factory = sqlite3.Row
|
||||
|
||||
commit = Connection.commit
|
||||
execute = Connection.execute
|
||||
executemany = Connection.executemany
|
||||
|
||||
from . import artist
|
||||
from . import album
|
||||
|
|
|
@ -10,9 +10,9 @@
|
|||
# | (artistid, name) -> albumid |
|
||||
# +-----------------------------+
|
||||
from gi.repository import GObject
|
||||
from .sql import execute
|
||||
from . import artist
|
||||
from . import disc
|
||||
from . import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
# | name -> artistid |
|
||||
# +------------------+
|
||||
from gi.repository import GObject
|
||||
from .sql import execute
|
||||
from . import album
|
||||
from . import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
# | decadeid | plstateid | decade |
|
||||
# +----------+-----------+--------+
|
||||
from gi.repository import GObject
|
||||
from . import execute
|
||||
from .sql import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
from . import year
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
# | discid | albumid | number | subtitle |
|
||||
# +--------+---------+--------+----------+
|
||||
from gi.repository import GObject
|
||||
from .sql import execute
|
||||
from . import album
|
||||
from . import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
# | genreid | trackid |
|
||||
# +---------+---------+
|
||||
from gi.repository import GObject
|
||||
from . import execute
|
||||
from .sql import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
from . import track
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
# +-----------+-----------+---------+------+
|
||||
import pathlib
|
||||
from gi.repository import GObject
|
||||
from . import commit
|
||||
from . import execute
|
||||
from .sql import commit
|
||||
from .sql import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
from . import track
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import sqlite3
|
||||
from gi.repository import GObject
|
||||
from . import execute
|
||||
from .sql import execute
|
||||
|
||||
class Row(GObject.GObject):
|
||||
def __init__(self, rowid):
|
||||
|
|
|
@ -10,8 +10,7 @@
|
|||
# | name -> playlistid |
|
||||
# +--------------------+
|
||||
from gi.repository import GObject
|
||||
from . import execute
|
||||
from . import executemany
|
||||
from .sql import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
from . import track
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import lib
|
||||
import sqlite3
|
||||
|
||||
File = lib.data.emmental_data / "emmental.sqlite"
|
||||
|
||||
Connection = sqlite3.connect(File, detect_types=sqlite3.PARSE_DECLTYPES)
|
||||
Connection.row_factory = sqlite3.Row
|
||||
|
||||
commit = Connection.commit
|
||||
execute = Connection.execute
|
|
@ -5,7 +5,7 @@
|
|||
# | plstateid | random | loop | current | sort |
|
||||
# +-----------+--------+------+---------+------+
|
||||
from gi.repository import GObject
|
||||
from . import execute
|
||||
from .sql import execute
|
||||
from . import objects
|
||||
|
||||
class PlaylistState(objects.Row):
|
||||
|
|
|
@ -14,7 +14,7 @@ class TestAlbumTable(unittest.TestCase):
|
|||
|
||||
def test_album_table_init(self):
|
||||
self.assertIsInstance(db.album.Table, db.album.AlbumTable)
|
||||
db.execute("SELECT albumid,artistid,plstateid,name,sort FROM albums")
|
||||
db.sql.execute("SELECT albumid,artistid,plstateid,name,sort FROM albums")
|
||||
|
||||
def test_album_table_insert(self):
|
||||
db.album.Table.connect("row-inserted", self.on_row_inserted)
|
||||
|
|
|
@ -17,7 +17,7 @@ class TestArtistTable(unittest.TestCase):
|
|||
|
||||
def test_artist_table_init(self):
|
||||
self.assertIsInstance(db.artist.Table, db.artist.ArtistTable)
|
||||
db.execute("SELECT artistid,plstateid,name,sort FROM artists")
|
||||
db.sql.execute("SELECT artistid,plstateid,name,sort FROM artists")
|
||||
|
||||
def test_artist_table_insert(self):
|
||||
db.artist.Table.connect("row-inserted", self.on_row_inserted)
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import db
|
||||
import lib
|
||||
import sqlite3
|
||||
import unittest
|
||||
|
||||
class TestDB(unittest.TestCase):
|
||||
def test_db_init(self):
|
||||
self.assertEqual(db.File, lib.data.emmental_data / "emmental.sqlite3")
|
||||
self.assertIsInstance(db.Connection, sqlite3.Connection)
|
||||
self.assertEqual(db.Connection.row_factory, sqlite3.Row)
|
||||
|
||||
self.assertEqual(db.commit, db.Connection.commit)
|
||||
self.assertEqual(db.execute, db.Connection.execute)
|
||||
self.assertEqual(db.executemany, db.Connection.executemany)
|
|
@ -11,7 +11,7 @@ class TestDecadeTable(unittest.TestCase):
|
|||
|
||||
def test_decade_table_init(self):
|
||||
self.assertIsInstance(db.decade.Table, db.decade.DecadeTable)
|
||||
db.execute("SELECT decadeid,plstateid,decade FROM decades")
|
||||
db.sql.execute("SELECT decadeid,plstateid,decade FROM decades")
|
||||
|
||||
def test_decade_table_insert(self):
|
||||
decade = db.decade.Table.insert(2020)
|
||||
|
|
|
@ -11,7 +11,7 @@ class TestDiscTable(unittest.TestCase):
|
|||
|
||||
def test_disc_table_init(self):
|
||||
self.assertIsInstance(db.disc.Table, db.disc.DiscTable)
|
||||
db.execute("SELECT discid,albumid,plstateid,number,subtitle FROM discs")
|
||||
db.sql.execute("SELECT discid,albumid,plstateid,number,subtitle FROM discs")
|
||||
|
||||
def test_disc_table_insert(self):
|
||||
artist = db.artist.Table.insert("Test Artist", "Test Sort")
|
||||
|
|
|
@ -14,7 +14,7 @@ class TestGenreTable(unittest.TestCase):
|
|||
|
||||
def test_genre_table_init(self):
|
||||
self.assertIsInstance(db.genre.Table, db.genre.GenreTable)
|
||||
db.execute("SELECT genreid,plstateid,name,sort FROM genres")
|
||||
db.sql.execute("SELECT genreid,plstateid,name,sort FROM genres")
|
||||
|
||||
def test_genre_table_insert(self):
|
||||
db.genre.Table.connect("row-inserted", self.on_row_inserted)
|
||||
|
@ -57,7 +57,7 @@ class TestGenreMap(unittest.TestCase):
|
|||
self.assertIsInstance(db.genre.Map, db.genre.GenreMap)
|
||||
self.assertEqual(db.genre.Map.map_lhs, db.genre.Genre)
|
||||
self.assertEqual(db.genre.Map.map_rhs, db.track.Track)
|
||||
db.execute("SELECT genreid,trackid FROM genre_map")
|
||||
db.sql.execute("SELECT genreid,trackid FROM genre_map")
|
||||
|
||||
def test_genre_map_insert(self):
|
||||
track = db.make_fake_track(1, 1.234, "Test Title", "/a/b.cde")
|
||||
|
|
|
@ -12,7 +12,7 @@ class TestLibraryTable(unittest.TestCase):
|
|||
|
||||
def test_library_table_init(self):
|
||||
self.assertIsInstance(db.library.Table, db.library.LibraryTable)
|
||||
db.execute("SELECT libraryid,plstateid,enabled,path FROM libraries")
|
||||
db.sql.execute("SELECT libraryid,plstateid,enabled,path FROM libraries")
|
||||
|
||||
def test_library_table_insert(self):
|
||||
library = db.library.Table.insert(pathlib.Path("/a/b/c"))
|
||||
|
|
|
@ -20,14 +20,14 @@ class TestPlaylistTable(unittest.TestCase):
|
|||
self.assertEqual(db.playlist.Default, [ "Collection", "Favorites", "New Tracks",
|
||||
"Previous", "Queued Tracks" ])
|
||||
|
||||
cur = db.execute("SELECT playlistid, "
|
||||
"playlists.plstateid, "
|
||||
"name, "
|
||||
"playlists.sort, "
|
||||
"random,loop "
|
||||
"FROM playlists "
|
||||
"JOIN playlist_states "
|
||||
"ON playlists.plstateid = playlist_states.plstateid")
|
||||
cur = db.sql.execute("SELECT playlistid, "
|
||||
"playlists.plstateid, "
|
||||
"name, "
|
||||
"playlists.sort, "
|
||||
"random,loop "
|
||||
"FROM playlists "
|
||||
"JOIN playlist_states "
|
||||
"ON playlists.plstateid = playlist_states.plstateid")
|
||||
rows = cur.fetchall()
|
||||
self.assertEqual(tuple(rows[0]), expected[0])
|
||||
self.assertEqual(tuple(rows[1]), expected[1])
|
||||
|
@ -87,8 +87,8 @@ class TestPlaylistMap(unittest.TestCase):
|
|||
self.assertFalse(db.playlist.Map.temporary)
|
||||
self.assertTrue( db.playlist.TempMap.temporary)
|
||||
|
||||
db.execute("SELECT playlistid,trackid FROM playlist_map")
|
||||
db.execute("SELECT playlistid,trackid FROM temp_playlist_map")
|
||||
db.sql.execute("SELECT playlistid,trackid FROM playlist_map")
|
||||
db.sql.execute("SELECT playlistid,trackid FROM temp_playlist_map")
|
||||
|
||||
def test_playlist_map_insert(self):
|
||||
track = db.make_fake_track(1, 1.234, "Test Title", "/a/b.cde")
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
# Copyright 2021 (c) Anna Schumaker.
|
||||
import lib
|
||||
import sqlite3
|
||||
import unittest
|
||||
from . import sql
|
||||
|
||||
class TestSQL(unittest.TestCase):
|
||||
def test_init(self):
|
||||
self.assertEqual(sql.File, lib.data.emmental_data / "emmental.sqlite")
|
||||
self.assertIsInstance(sql.Connection, sqlite3.Connection)
|
||||
self.assertEqual(sql.Connection.row_factory, sqlite3.Row)
|
||||
|
||||
self.assertEqual(sql.commit, sql.Connection.commit)
|
||||
self.assertEqual(sql.execute, sql.Connection.execute)
|
|
@ -11,8 +11,8 @@ class TestPlaylistStateTable(unittest.TestCase):
|
|||
|
||||
def test_playlist_state_table_init(self):
|
||||
self.assertIsInstance(db.state.Table, db.state.PlaylistStateTable)
|
||||
db.execute("SELECT plstateid,random,loop,current,sort "
|
||||
"FROM playlist_states")
|
||||
db.sql.execute("SELECT plstateid,random,loop,current,sort "
|
||||
"FROM playlist_states")
|
||||
|
||||
def test_playlist_state_table_insert(self):
|
||||
state = db.state.Table.insert(False, False)
|
||||
|
|
|
@ -13,8 +13,8 @@ class TestTrackTable(unittest.TestCase):
|
|||
|
||||
def test_track_table_init(self):
|
||||
self.assertIsInstance(db.track.Table, db.track.TrackTable)
|
||||
db.execute("SELECT trackid,libraryid,artistid,albumid,discid,decadeid,yearid FROM tracks")
|
||||
db.execute("SELECT number,playcount,lastplayed,length,title,path FROM tracks")
|
||||
db.sql.execute("SELECT trackid,libraryid,artistid,albumid,discid,decadeid,yearid FROM tracks")
|
||||
db.sql.execute("SELECT number,playcount,lastplayed,length,title,path FROM tracks")
|
||||
|
||||
def test_track_table_insert(self):
|
||||
library = db.library.Table.find(pathlib.Path("/a/b/c"))
|
||||
|
|
|
@ -11,7 +11,7 @@ class TestYearTable(unittest.TestCase):
|
|||
|
||||
def test_year_table_init(self):
|
||||
self.assertIsInstance(db.year.Table, db.year.YearTable)
|
||||
db.execute("SELECT yearid,decadeid,plstateid,year FROM years")
|
||||
db.sql.execute("SELECT yearid,decadeid,plstateid,year FROM years")
|
||||
|
||||
def test_year_table_insert(self):
|
||||
decade = db.decade.Table.insert(2020)
|
||||
|
|
|
@ -14,12 +14,12 @@
|
|||
import datetime
|
||||
import pathlib
|
||||
from gi.repository import GObject
|
||||
from .sql import commit
|
||||
from .sql import execute
|
||||
from . import artist
|
||||
from . import album
|
||||
from . import commit
|
||||
from . import decade
|
||||
from . import disc
|
||||
from . import execute
|
||||
from . import library
|
||||
from . import objects
|
||||
from . import year
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
# | yearid | decadeid | plstateid | year |
|
||||
# +--------+----------+-----------+------+
|
||||
from gi.repository import GObject
|
||||
from .sql import execute
|
||||
from . import decade
|
||||
from . import execute
|
||||
from . import objects
|
||||
from . import state
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ class Task(GObject.GObject):
|
|||
|
||||
|
||||
class CommitTask(Task):
|
||||
def run_task(self): db.commit()
|
||||
def run_task(self): db.sql.commit()
|
||||
|
||||
|
||||
class FileTask(Task):
|
||||
|
@ -75,7 +75,7 @@ class CheckTask(Task):
|
|||
db.track.Table.delete(track)
|
||||
need_commit = True
|
||||
if need_commit:
|
||||
db.commit()
|
||||
db.sql.commit()
|
||||
|
||||
|
||||
class CheckSchedulerTask(Task):
|
||||
|
@ -102,7 +102,7 @@ class RemoveTask(Task):
|
|||
def run_task(self):
|
||||
for track in self.tracks:
|
||||
db.track.Table.delete(track)
|
||||
db.commit()
|
||||
db.sql.commit()
|
||||
|
||||
|
||||
class RemoveLibraryTask(Task):
|
||||
|
@ -112,7 +112,7 @@ class RemoveLibraryTask(Task):
|
|||
|
||||
def run_task(self):
|
||||
db.library.Table.delete(self.library)
|
||||
db.commit()
|
||||
db.sql.commit()
|
||||
|
||||
|
||||
class RemoveLibrarySchedulerTask(Task):
|
||||
|
|
Loading…
Reference in New Issue