8 using GameHub
.Data
.Sources
.Steam
;
9 using GameHub
.Data
.Sources
.GOG
;
10 using GameHub
.Data
.Sources
.Humble
;
12 namespace GameHub
.Data
16 private Database? db
= null;
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());
30 public void create_tables() requires (db
!= null)
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)
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
);
57 return res
== Sqlite
.DONE
;
60 public bool add_unsupported_game(GameSource src
, string id
) requires (db
!= null)
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
);
71 return res
== Sqlite
.DONE
;
74 public ArrayList
<Game
> get_games(GameSource? src
= null)
81 res
= db
.prepare_v2("SELECT * FROM `games` WHERE `source` = ?", -1, out stmt
);
82 res
= stmt
.bind_text(1, src
.name
);
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));
99 games
.add(new SteamGame
.from_db((Steam
) s
, stmt
));
103 games
.add(new GOGGame
.from_db((GOG
) s
, stmt
));
107 games
.add(new HumbleGame
.from_db((Humble
) s
, stmt
));
114 public ArrayList
<string> get_unsupported_games(GameSource src
)
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));
132 public bool is_game_unsupported(GameSource src
, string id
)
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()
149 instance
= new
GamesDB();
154 public static void init()
156 GamesDB
.get_instance().create_tables();