Humble Bundle support
[GameHub.git] / src / data / GamesDB.vala
blobf9e303b9f3406dca7453122ad073ea22fe1a26e4
1 using Gtk;
2 using Gdk;
3 using Gee;
4 using Sqlite;
6 using GameHub.Utils;
8 using GameHub.Data.Sources.Steam;
9 using GameHub.Data.Sources.GOG;
10 using GameHub.Data.Sources.Humble;
12 namespace GameHub.Data
14 public class GamesDB
16 private Database? db = null;
18 public GamesDB()
20 var path = FSUtils.expand(FSUtils.Paths.Cache.GamesDB);
22 if(Database.open(path, out db) != Sqlite.OK)
24 warning("Can't open games database: " + db.errmsg());
25 db = null;
26 return;
30 public void create_tables() requires (db != null)
32 Statement stmt;
33 if(db.prepare_v2("SELECT `playtime` FROM `games`", -1, out stmt) == Sqlite.OK) // migrate from v1
35 db.exec("DROP TABLE `games`");
38 db.exec("CREATE TABLE IF NOT EXISTS `games`(`source` string not null, `id` string not null, `name` string not null, `icon` string, `image` string, `custom_info` string, PRIMARY KEY(`source`, `id`))");
39 db.exec("CREATE TABLE IF NOT EXISTS `unsupported_games`(`source` string not null, `id` string not null, PRIMARY KEY(`source`, `id`))");
42 public bool add_game(Game game) requires (db != null)
44 Statement stmt;
45 int res = db.prepare_v2("INSERT OR REPLACE INTO `games`(`source`, `id`, `name`, `icon`, `image`, `custom_info`) VALUES (?, ?, ?, ?, ?, ?)", -1, out stmt);
46 assert(res == Sqlite.OK);
48 stmt.bind_text(1, game.source.name);
49 stmt.bind_text(2, game.id);
50 stmt.bind_text(3, game.name);
51 stmt.bind_text(4, game.icon);
52 stmt.bind_text(5, game.image);
53 stmt.bind_text(6, game.custom_info);
55 res = stmt.step();
57 return res == Sqlite.DONE;
60 public bool add_unsupported_game(GameSource src, string id) requires (db != null)
62 Statement stmt;
63 int res = db.prepare_v2("INSERT OR REPLACE INTO `unsupported_games`(`source`, `id`) VALUES (?, ?)", -1, out stmt);
64 assert(res == Sqlite.OK);
66 stmt.bind_text(1, src.name);
67 stmt.bind_text(2, id);
69 res = stmt.step();
71 return res == Sqlite.DONE;
74 public ArrayList<Game> get_games(GameSource? src = null)
76 Statement stmt;
77 int res;
79 if(src != null)
81 res = db.prepare_v2("SELECT * FROM `games` WHERE `source` = ?", -1, out stmt);
82 res = stmt.bind_text(1, src.name);
84 else
86 res = db.prepare_v2("SELECT * FROM `games`", -1, out stmt);
89 assert(res == Sqlite.OK);
91 var games = new ArrayList<Game>(Game.is_equal);
93 while((res = stmt.step()) == Sqlite.ROW)
95 var s = GameSource.by_name(stmt.column_text(0));
97 if(s is Steam)
99 games.add(new SteamGame.from_db((Steam) s, stmt));
101 else if(s is GOG)
103 games.add(new GOGGame.from_db((GOG) s, stmt));
105 else if(s is Humble)
107 games.add(new HumbleGame.from_db((Humble) s, stmt));
111 return games;
114 public ArrayList<string> get_unsupported_games(GameSource src)
116 Statement stmt;
117 int res;
119 res = db.prepare_v2("SELECT * FROM `unsupported_games` WHERE `source` = ?", -1, out stmt);
120 stmt.bind_text(1, src.name);
122 var games = new ArrayList<string>();
124 while((res = stmt.step()) == Sqlite.ROW)
126 games.add(stmt.column_text(1));
129 return games;
132 public bool is_game_unsupported(GameSource src, string id)
134 Statement stmt;
135 int res;
137 res = db.prepare_v2("SELECT * FROM `unsupported_games` WHERE `source` = ? AND `id` = ? LIMIT 1", -1, out stmt);
138 stmt.bind_text(1, src.name);
139 stmt.bind_text(2, id);
141 return stmt.step() == Sqlite.ROW;
144 private static GamesDB? instance;
145 public static unowned GamesDB get_instance()
147 if(instance == null)
149 instance = new GamesDB();
151 return instance;
154 public static void init()
156 GamesDB.get_instance().create_tables();