Archive
/
/
/
/
/
Echo.Database.DatabaseService.vala
2008-12-02 05:29:52 UTC
previous
next
/* vim: set noexpandtab tabstop=4 shiftwidth=4 nowrap textwidth=100
*
* Echo Media Player
* Copyright (C) 2008 Shane O'Connell
*
* [ The original file includes a copyright header in this location describing
* the file as being released under the terms of the GNU General Public
* License. It has been removed in order to display the file as part of the
* archive. ]
*/
using Echo.Database.SQL;
public class Echo.Database.DatabaseService : GLib.Object
{
private static DatabaseConnection _db;
private static Gee.ArrayList<Library> _libraries;
public DatabaseService() throws SQLError, GLib.Error
{
message("Loading database");
assert(Sqlite.threadsafe() == 1);
File config_dir = File.new_for_path(Environment.get_user_config_dir());
assert(config_dir.is_native());
assert(config_dir.query_exists(null)); // TODO throw errors instead of using assert()
File echo_dir = config_dir.get_child("echo-dev");
if (!echo_dir.query_exists(null)) {
echo_dir.make_directory(null);
if (FileUtils.chmod(echo_dir.get_path(), 0700) == -1) {
assert_not_reached(); // TODO throw errors instead of assert()
}
}
File db_file = echo_dir.get_child("echo.db");
bool new_db = !db_file.query_exists(null);
if (new_db)
message("No database detected, creating a new default one");
string db_filename = db_file.get_path();
assert(db_filename != null);
_db = new DatabaseConnection(db_filename);
if (new_db)
if (FileUtils.chmod(db_filename, 0600) == -1) {
assert_not_reached(); // TODO throw errors
}
init_tables();
try {
load_libraries();
}
catch (SQLError e) {
error("Unable to load libraries from database, error was: %s", e.message);
}
}
private static void init_tables() throws SQLError
{
/*_db.run("CREATE TEMPORARY TABLE MemoryPlaylists (" +
" PlaylistID INTEGER PRIMARY KEY," +
" PlaylistName TEXT" +
")");
_db.run("CREATE TEMPORARY TABLE MemoryPlaylistTracks (" +
" PlaylistID INTEGER," +
" PlaylistIndex INTEGER," +
" ArtistName TEXT," +
" AlbumName TEXT," +
" AlbumReleaseDate INTEGER," +
" TrackName TEXT," +
" TrackNumber INTEGER," +
" TrackURI TEXT UNIQUE" +
")");*/
_db.run("CREATE TABLE IF NOT EXISTS Config (" +
" Name TEXT," +
" Value TEXT" +
")");
_db.run("CREATE TABLE IF NOT EXISTS Artists (" +
" ArtistID INTEGER PRIMARY KEY," +
" ArtistName TEXT UNIQUE" +
")");
_db.run("CREATE TABLE IF NOT EXISTS Albums (" +
" AlbumID INTEGER PRIMARY KEY," +
" ArtistID INTEGER," +
" AlbumName TEXT," +
" AlbumReleaseYear INTEGER," +
" AlbumReleaseMonth INTEGER," +
" AlbumReleaseDay INTEGER" +
")");
_db.run("CREATE TABLE IF NOT EXISTS Tracks (" +
" TrackID INTEGER PRIMARY KEY," +
" LibraryID INTEGER," +
" AlbumID INTEGER," +
" TrackName TEXT," +
" TrackNumber INTEGER," +
" TrackRating INTEGER," +
" TrackURI TEXT UNIQUE ON CONFLICT REPLACE" +
")");
_db.run("CREATE TABLE IF NOT EXISTS Libraries (" +
" LibraryID INTEGER PRIMARY KEY," +
" LibraryName TEXT," +
" LibraryIcon TEXT," +
" LibraryURI TEXT" +
")");
if (_db.get_row_count("Libraries") == 0) {
_db.run("INSERT INTO Libraries (LibraryName, LibraryIcon, LibraryURI) " +
"VALUES('Music Library', 'audio-x-generic', 'file:///home/shane/library/music/albums')");
_db.run("INSERT INTO Libraries (LibraryName, LibraryIcon, LibraryURI) " +
"VALUES('Video Library', 'video-x-generic', 'file:///home/shane/library/television')");
}
/*_db.run("CREATE TABLE IF NOT EXISTS LibraryTracks (" +
" LibraryID INTEGER," +
" TrackID INTEGER," +
" UNIQUE(LibraryID, TrackID) ON CONFLICT REPLACE" +
")");*/
_db.run("CREATE TABLE IF NOT EXISTS Playlists (" +
" PlaylistID INTEGER PRIMARY KEY," +
" PlaylistName TEXT" +
")");
_db.run("CREATE TABLE IF NOT EXISTS PlaylistTracks (" +
" PlaylistID INTEGER," +
" TrackID INTEGER," +
" PlaylistOrder INTEGER" +
")");
}
private static void load_libraries() throws SQLError
{
var stmt = _db.prepare("SELECT LibraryID, LibraryName, LibraryIcon, LibraryURI FROM Libraries");
_libraries = new Gee.ArrayList<Library>();
while (stmt.step()) {
_libraries.add(new Library(
_db,
stmt.get_int64_column(0),
stmt.get_string_column(1),
stmt.get_string_column(2),
stmt.get_string_column(3)));
}
}
public static Gee.ReadOnlyList<Library> get_libraries()
{
return new Gee.ReadOnlyList<Library>(_libraries);
}
}