Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Backend #54

Merged
merged 58 commits into from
Sep 27, 2016
Merged

SQL Backend #54

merged 58 commits into from
Sep 27, 2016

Conversation

Antar1011
Copy link
Owner

@Antar1011 Antar1011 commented Sep 26, 2016

Resolves #18

SQLite implementation of a backend for Onix.

Before merging I might want to add some "pipeline" tests that verify expected operation from start to finish

For now I think it's valid to treat this as SQL-specific. Use-cases where that degree of normalization isn't required won't need it.
Since I'm using SQLAlchemy, the backend is generically SQL rather than specifically SQLite. SQLAlchemy doesn't play nice with everything, but it works on a wide enough variety of DBs that it seems silly to designate this backend SQLite-specific.
This is a bit silly, since the sinks are just stumps for these tests, but it's good form.
# Conflicts:
#	onix-env2.yaml
#	onix-env3.yaml
If ability isn't sanitized, get_all_formes won't work
@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.1%) to 99.721% when pulling 068fd3d on backend into c921abe on master.

I'm not sure that SQL isn't smart enough to not repeat the calculation, but this works regardless.
@Antar1011
Copy link
Owner Author

In case anyone's interested, this is the compiled SQL query for get_usage_by_species:

SELECT anon_1.species, sum(anon_1.weight) AS sum 
FROM (SELECT anon_2.bid AS bid, anon_2.side AS side, anon_2.weight AS weight, count(anon_2.slot) AS count, anon_2.species AS species 
FROM (SELECT anon_3.bid AS bid, anon_3.side AS side, anon_3.weight AS weight, teams.idx AS slot, teams.sid AS sid, CASE group_concat(formes.species) WHEN ? THEN ? WHEN ? THEN ? WHEN ? THEN ? WHEN ? THEN ? ELSE ? || group_concat(formes.species) END AS species 
FROM (SELECT anon_4.bid AS bid, anon_4.side AS side, anon_4.pid AS pid, anon_4.tid AS tid, CASE WHEN (ifnull(anon_4.rprd, ?) > ?) THEN ? ELSE weight(ifnull(anon_4.rpr, ?), ifnull(anon_4.rprd, ?), ?) END AS weight 
FROM (SELECT anon_5.id AS bid, battle_players.side AS side, battle_players.pid AS pid, battle_players.tid AS tid, battle_players.w AS w, battle_players.l AS l, battle_players.t AS t, battle_players.elo AS elo, battle_players.r AS r, battle_players.rd AS rd, battle_players.rpr AS rpr, battle_players.rprd AS rprd 
FROM (SELECT battle_infos.id AS id 
FROM battle_infos 
WHERE battle_infos.format = ? AND battle_infos.date BETWEEN ? AND ? AND battle_infos.turns >= ?) AS anon_5 JOIN battle_players ON anon_5.id = battle_players.bid) AS anon_4) AS anon_3 JOIN teams ON anon_3.tid = teams.tid JOIN moveset_forme ON teams.sid = moveset_forme.sid JOIN formes ON moveset_forme.fid = formes.id GROUP BY anon_3.bid, anon_3.side, teams.idx) AS anon_2 GROUP BY anon_2.bid, anon_2.side, anon_2.species) AS anon_1 GROUP BY anon_1.species ORDER BY sum DESC

@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.1%) to 99.721% when pulling b301d15 on backend into c921abe on master.

@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.1%) to 99.722% when pulling 40628a1 on backend into c921abe on master.

@Antar1011
Copy link
Owner Author

Crud. This structure doesn't support ordering ('camerupt,cameruptmegax' is just a matter of alphabetical sorting, but how can we do 'charizardmegay,charizardmegax'?)

@Antar1011
Copy link
Owner Author

Antar1011 commented Sep 26, 2016

I can handle this the same way it's handled for team members (in fact, it looks like there's a way to handle this more cleanly), but I honestly think this might be the impetus I need to tackle #48.

@Antar1011
Copy link
Owner Author

LOL. I've been doing this kind of ordering all over the place (see: battle_players), but w/e--I feel like the core approach is cleaner

@coveralls
Copy link

coveralls commented Sep 26, 2016

Coverage Status

Coverage increased (+0.09%) to 99.71% when pulling f6e9ef3 on backend into c921abe on master.

@coveralls
Copy link

coveralls commented Sep 27, 2016

Coverage Status

Coverage increased (+0.09%) to 99.71% when pulling dd578f5 on backend into c921abe on master.

@Antar1011
Copy link
Owner Author

The new compiled SQLite query:

SELECT anon_1.species, sum(anon_1.weight) AS sum 
FROM (SELECT anon_2.bid AS bid, anon_2.side AS side, anon_2.weight AS weight, count(anon_2.slot) AS count, anon_2.species AS species 
FROM (SELECT anon_3.bid AS bid, anon_3.side AS side, anon_3.weight AS weight, anon_3.slot AS slot, anon_3.sid AS sid, CASE group_concat(anon_3.species) ELSE ? || group_concat(anon_3.species) END AS species 
FROM (SELECT anon_4.bid AS bid, anon_4.side AS side, anon_4.weight AS weight, teams.idx AS slot, teams.sid AS sid, formes.species AS species, moveset_forme.prime AS prime 
FROM (SELECT anon_5.bid AS bid, anon_5.side AS side, anon_5.pid AS pid, anon_5.tid AS tid, CASE WHEN (ifnull(anon_5.rprd, ?) > ?) THEN ? ELSE weight(ifnull(anon_5.rpr, ?), ifnull(anon_5.rprd, ?), ?) END AS weight 
FROM (SELECT anon_6.id AS bid, battle_players.side AS side, battle_players.pid AS pid, battle_players.tid AS tid, battle_players.w AS w, battle_players.l AS l, battle_players.t AS t, battle_players.elo AS elo, battle_players.r AS r, battle_players.rd AS rd, battle_players.rpr AS rpr, battle_players.rprd AS rprd 
FROM (SELECT battle_infos.id AS id 
FROM battle_infos 
WHERE battle_infos.format = ? AND battle_infos.date BETWEEN ? AND ? AND battle_infos.turns >= ?) AS anon_6 JOIN battle_players ON anon_6.id = battle_players.bid) AS anon_5) AS anon_4 JOIN teams ON anon_4.tid = teams.tid JOIN moveset_forme ON teams.sid = moveset_forme.sid JOIN formes ON moveset_forme.fid = formes.id ORDER BY formes.species, moveset_forme.prime DESC) AS anon_3 GROUP BY anon_3.bid, anon_3.side, anon_3.slot) AS anon_2 GROUP BY anon_2.bid, anon_2.side, anon_2.species) AS anon_1 GROUP BY anon_1.species ORDER BY sum DESC

@coveralls
Copy link

coveralls commented Sep 27, 2016

Coverage Status

Coverage increased (+0.09%) to 99.71% when pulling 81a0ee9 on backend into c921abe on master.

@Antar1011 Antar1011 merged commit 14827a5 into master Sep 27, 2016
@Antar1011 Antar1011 deleted the backend branch September 27, 2016 03:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants