OSDN Git Service

create database accessor and its support class
[amulettoolsmh4/main.git] / model / db_supports.py
1 # -*- coding: utf-8 -*-
2
3 # constant numbers of database
4 # データベース関連の定数値など
5 # 2013/12/04 written by kei9 
6
7 # import modules
8 import os.path
9 import codecs
10
11 # --- filenames ---
12 ZIP_FILE = "data.zip"
13 DATA_DIRECTORY = "data"
14 OUTPUT_DB_FILE_NAME = "OmamoriMH4.sqlite3"
15 SKILL_FILE_NAME = "skill.csv"
16 AMULET_FILE_NAME = "amulet.csv"
17 MIN_MAX_FILE_NAME = "minmax.csv"
18 SUFFICIENT_FILE_NAME = "sufficient.csv"
19 SECOND_SKILL_FILE_NAME = "2ndskill.csv"
20 SECOND_SLOT_FILE_NAME = "2ndskill_slot.csv"
21
22 # --- sql sentences of database ---
23 # for skill table
24 SKILL_TABLE_NAME = u"skill_table"
25 SKILL_TABLE_COL_SKILL_NAME = u"skill_name"
26 SKILL_TABLE_CREATE_SQL = u"""create table if not exists {table} 
27     (id integer primary key, {skill_name} varchar unique);""".format(
28         table=SKILL_TABLE_NAME, 
29         skill_name=SKILL_TABLE_COL_SKILL_NAME)
30 SKILL_TABLE_INSERT_SQL = u"""insert into {table} 
31     ({skill_col}) values(?);""".format(
32         table=SKILL_TABLE_NAME,
33         skill_col=SKILL_TABLE_COL_SKILL_NAME)
34 SKILL_TABLE_ID2SKILL_SQL = u"""select {skill_col} from {table} where id='{{id}}'""".format(
35         skill_col=SKILL_TABLE_COL_SKILL_NAME,
36         table=SKILL_TABLE_NAME)
37 SKILL_TABLE_SKILL2ID_SQL = u"""select id from {table} where {skill_col}='{{skill_name}}'""".format(
38         skill_col=SKILL_TABLE_COL_SKILL_NAME,
39         table=SKILL_TABLE_NAME)
40 SKILL_TABLE_SELECT_ALL_SQL = u"""select id, {skill_col} from {table}""".format(
41         skill_col=SKILL_TABLE_COL_SKILL_NAME,
42         table=SKILL_TABLE_NAME)
43
44 # for amulet table
45 AMULET_TABLE_NAME = u"amulet_table"
46 AMULET_TABLE_COL_AMULET_NAME = u"amulet_name"
47 AMULET_TABLE_CREATE_SQL = u"""create table if not exists {table} 
48     (id integer primary key, {amulet_name} varchar unique);""".format(
49         table=AMULET_TABLE_NAME, 
50         amulet_name=AMULET_TABLE_COL_AMULET_NAME)
51 AMULET_TABLE_INSERT_SQL = u"""insert into {table}
52     ({amulet_col}) values(?);""".format(
53         table=AMULET_TABLE_NAME,
54         amulet_col=AMULET_TABLE_COL_AMULET_NAME)
55 AMULET_TABLE_ID2AMULET_SQL = u"""select {amulet_col} from {table} where id='{{id}}'""".format(
56         amulet_col=AMULET_TABLE_COL_AMULET_NAME,
57         table=AMULET_TABLE_NAME)
58 AMULET_TABLE_AMULET2ID_SQL = u"""select id from {table} where {amulet_col}='{{amulet_name}}'""".format(
59         amulet_col=AMULET_TABLE_COL_AMULET_NAME,
60         table=AMULET_TABLE_NAME)
61 AMULET_TABLE_SELECT_ALL_SQL = u"""select id, {amulet_col} from {table}""".format(
62         amulet_col=AMULET_TABLE_COL_AMULET_NAME,
63         table=AMULET_TABLE_NAME)
64
65 # for minmax of skill
66 MIN_MAX_MASTER_TABLE_NAME = u"skill_minmax_master"
67 MIN_MAX_TABLE_NAME = u"skill_minmax_{id}"
68 MIN_MAX_COL_AMULET_ID = u"amulet_id"
69 MIN_MAX_COL_SKILL_TABLE_NAME = u"skill_table_name"
70 MIN_MAX_COL_MIN1 = u"min1"
71 MIN_MAX_COL_MIN2 = u"min2"
72 MIN_MAX_COL_MAX1 = u"max1"
73 MIN_MAX_COL_MAX2 = u"max2"
74 MIN_MAX_COL_SKILL_ID = u"skill_id"
75 MIN_MAX_MASTER_TABLE_CREATE_SQL = u"""create table if not exists {master}
76     (id integer primary key, {amulet_id} integer, {table_name} varchar,
77     foreign key(amulet_id) references {amulet_table}(id));""".format(
78         master=MIN_MAX_MASTER_TABLE_NAME,
79         amulet_id=MIN_MAX_COL_AMULET_ID,
80         amulet_table=AMULET_TABLE_NAME,
81         table_name=MIN_MAX_COL_SKILL_TABLE_NAME)
82 MIN_MAX_TABLE_CREATE_SQL = u"""create table if not exists {{table_name}} 
83     (id integer primary key, {skill_id} integer,
84     {min1} integer, {max1} integer, {min2} integer, {max2} integer, 
85     foreign key(skill_id) references {skill_table}(id));""".format(
86         skill_id=MIN_MAX_COL_SKILL_ID,
87         min1=MIN_MAX_COL_MIN1,
88         min2=MIN_MAX_COL_MIN2,
89         max1=MIN_MAX_COL_MAX1,
90         max2=MIN_MAX_COL_MAX2,
91         skill_table=SKILL_TABLE_NAME)
92 MIN_MAX_MASTER_TABLE_INSERT_SQL = u"""insert into {master}
93     ({amulet_id}, {table_col}) values(?,?);""".format(
94         master=MIN_MAX_MASTER_TABLE_NAME,
95         table_col=MIN_MAX_COL_SKILL_TABLE_NAME,
96         amulet_id=MIN_MAX_COL_AMULET_ID)
97 MIN_MAX_TABLE_INSERT_SQL = u"""insert into {{table_name}}
98     ({skill_id}, {min1}, {max1}, {min2}, {max2}) values(?,?,?,?,?)""".format(
99         skill_id=MIN_MAX_COL_SKILL_ID,
100         min1=MIN_MAX_COL_MIN1,
101         min2=MIN_MAX_COL_MIN2,
102         max1=MIN_MAX_COL_MAX1,
103         max2=MIN_MAX_COL_MAX2)
104 MIN_MAX_MASTER_TABLE_SELECT_ALL_SQL = u"""select {amu_id}, {table_col} from {table}""".format(
105         amu_id=MIN_MAX_COL_AMULET_ID,
106         table_col=MIN_MAX_COL_SKILL_TABLE_NAME,
107         table=MIN_MAX_MASTER_TABLE_NAME)
108 MIN_MAX_TABLE_SELECT_ALL_SQL = u"""select {skill_id}, {min1}, {max1}, {min2}, {max2} from {{table_name}} """.format(
109         skill_id=MIN_MAX_COL_SKILL_ID,
110         min1=MIN_MAX_COL_MIN1,
111         max1=MIN_MAX_COL_MAX1,
112         min2=MIN_MAX_COL_MIN2,
113         max2=MIN_MAX_COL_MAX2)
114
115
116 # for Second skill
117 SECOND_MASTER_TABLE_NAME = u"skill_second_master"
118 SECOND_TABLE_NAME = u"skill_second_{id}"
119 SECOND_SLOT_TABLE_NAME = u"skill_second_slot"
120 SECOND_COL_AMULET_ID = u"amulet_id"
121 SECOND_COL_SECOND_TABLE_NAME = u"second_table_name"
122 SECOND_COL_RANDOM_SEED = u"random_seed"
123 SECOND_COL_SKILL_ID1 = u"skill_id1"
124 SECOND_COL_SKILL_ID2 = u"skill_id2"
125 SECOND_COL_SKILL_ID3 = u"skill_id3"
126 SECOND_COL_SKILL_ID4 = u"skill_id4"
127 SECOND_COL_SKILL_ID5 = u"skill_id5"
128 SECOND_COL_SKILL_ID6 = u"skill_id6"
129 SECOND_COL_SKILL_ID7 = u"skill_id7"
130 SECOND_COL_SKILL_LIST = [SECOND_COL_SKILL_ID1, SECOND_COL_SKILL_ID2, SECOND_COL_SKILL_ID3, SECOND_COL_SKILL_ID4, SECOND_COL_SKILL_ID5, SECOND_COL_SKILL_ID6, SECOND_COL_SKILL_ID7]
131 SECOND_COL_SLOT1 = u"slot1"
132 SECOND_COL_SLOT2 = u"slot2"
133 SECOND_COL_SLOT3 = u"slot3"
134 SECOND_COL_SLOT4 = u"slot4"
135 SECOND_COL_SLOT5 = u"slot5"
136 SECOND_COL_SLOT6 = u"slot6"
137 SECOND_COL_SLOT7 = u"slot7"
138 SECOND_COL_SLOT_LIST = [SECOND_COL_SLOT1, SECOND_COL_SLOT2, SECOND_COL_SLOT3, SECOND_COL_SLOT4, SECOND_COL_SLOT5, SECOND_COL_SLOT6, SECOND_COL_SLOT7]
139 SECOND_MASTER_TABLE_CREATE_SQL = u"""create table if not exists {master}
140     (id integer primary key, {amulet_id} integer, {table_name} varchar,
141     foreign key(amulet_id) references {amulet_table}(id));""".format(
142         master=SECOND_MASTER_TABLE_NAME,
143         amulet_id=SECOND_COL_AMULET_ID,
144         amulet_table=AMULET_TABLE_NAME,
145         table_name=SECOND_COL_SECOND_TABLE_NAME)
146 SECOND_TABLE_CREATE_SQL = u"""create table if not exists {{table_name}} 
147     (id integer primary key, {seed} integer unique, 
148     {skill_id1} integer, {skill_id2} integer, {skill_id3} integer, 
149     {skill_id4} integer, {skill_id5} integer, {skill_id6} integer, {skill_id7} integer, 
150     foreign key({skill_id1}) references {skill_table}(id),
151     foreign key({skill_id2}) references {skill_table}(id),
152     foreign key({skill_id3}) references {skill_table}(id),
153     foreign key({skill_id4}) references {skill_table}(id),
154     foreign key({skill_id5}) references {skill_table}(id),
155     foreign key({skill_id6}) references {skill_table}(id),
156     foreign key({skill_id7}) references {skill_table}(id));""".format(
157         seed=SECOND_COL_RANDOM_SEED,
158         skill_id1=SECOND_COL_SKILL_ID1,
159         skill_id2=SECOND_COL_SKILL_ID2,
160         skill_id3=SECOND_COL_SKILL_ID3,
161         skill_id4=SECOND_COL_SKILL_ID4,
162         skill_id5=SECOND_COL_SKILL_ID5,
163         skill_id6=SECOND_COL_SKILL_ID6,
164         skill_id7=SECOND_COL_SKILL_ID7,
165         skill_table=SKILL_TABLE_NAME)
166 SECOND_SLOT_TABLE_CREATE_SQL = u"""create table if not exists {table} 
167     (id integer primary key, {seed} integer unique,
168     {slot1} integer, {slot2} integer, {slot3} integer,
169     {slot4} integer, {slot5} integer, {slot6} integer, {slot7} integer);""".format(
170         table=SECOND_SLOT_TABLE_NAME,
171         seed=SECOND_COL_RANDOM_SEED,
172         slot1=SECOND_COL_SLOT1,
173         slot2=SECOND_COL_SLOT2,
174         slot3=SECOND_COL_SLOT3,
175         slot4=SECOND_COL_SLOT4,
176         slot5=SECOND_COL_SLOT5,
177         slot6=SECOND_COL_SLOT6,
178         slot7=SECOND_COL_SLOT7)
179 SECOND_MASTER_TABLE_INSERT_SQL = u"""insert into {table}
180     ({amulet_col}, {table_col}) values(?,?);""".format(
181         table=SECOND_MASTER_TABLE_NAME,
182         table_col=SECOND_COL_SECOND_TABLE_NAME,
183         amulet_col=SECOND_COL_AMULET_ID)
184 SECOND_TABLE_INSERT_SQL = u"""insert into {{table_name}}
185     ({seed}, {skill_id1}, {skill_id2}, {skill_id3}, 
186     {skill_id4}, {skill_id5}, {skill_id6}, {skill_id7}) values(?,?,?,?,?,?,?,?)""".format(
187         seed=SECOND_COL_RANDOM_SEED,
188         skill_id1=SECOND_COL_SKILL_ID1,
189         skill_id2=SECOND_COL_SKILL_ID2,
190         skill_id3=SECOND_COL_SKILL_ID3,
191         skill_id4=SECOND_COL_SKILL_ID4,
192         skill_id5=SECOND_COL_SKILL_ID5,
193         skill_id6=SECOND_COL_SKILL_ID6,
194         skill_id7=SECOND_COL_SKILL_ID7)
195 SECOND_SLOT_TABLE_INSERT_SQL = u"""insert into {table}
196     ({seed}, {slot1}, {slot2}, {slot3}, {slot4}, {slot5}, {slot6}, {slot7})
197     values(?,?,?,?,?,?,?,?);""".format(
198         table=SECOND_SLOT_TABLE_NAME,
199         seed=SECOND_COL_RANDOM_SEED,
200         slot1=SECOND_COL_SLOT1,
201         slot2=SECOND_COL_SLOT2,
202         slot3=SECOND_COL_SLOT3,
203         slot4=SECOND_COL_SLOT4,
204         slot5=SECOND_COL_SLOT5,
205         slot6=SECOND_COL_SLOT6,
206         slot7=SECOND_COL_SLOT7)
207 SECOND_MASTER_TABLE_SELECT_ALL_SQL = u"""select {amu_id}, {table_col} from {table}""".format(
208         amu_id=SECOND_COL_AMULET_ID,
209         table_col=SECOND_COL_SECOND_TABLE_NAME,
210         table=SECOND_MASTER_TABLE_NAME)
211 SECOND_TABLE_SELECT_SEED_SQL = u"""select {seed} from {{table_name}} """.format(
212         seed=SECOND_COL_RANDOM_SEED)
213 SECOND_TABLE_SELECT_ALL_SQL = u"""select {seed}, {skill_id1}, {skill_id2}, 
214     {skill_id3}, {skill_id4}, {skill_id5}, {skill_id6}, {skill_id7} from {{table_name}} """.format(
215         seed=SECOND_COL_RANDOM_SEED,
216         skill_id1=SECOND_COL_SKILL_ID1,
217         skill_id2=SECOND_COL_SKILL_ID2,
218         skill_id3=SECOND_COL_SKILL_ID3,
219         skill_id4=SECOND_COL_SKILL_ID4,
220         skill_id5=SECOND_COL_SKILL_ID5,
221         skill_id6=SECOND_COL_SKILL_ID6,
222         skill_id7=SECOND_COL_SKILL_ID7)
223 SECOND_SLOT_TABLE_SELECT_ALL_SQL = u"""select {seed}, {slot1}, {slot2},
224     {slot3}, {slot4}, {slot5}, {slot6}, {slot7} from {table} """.format(
225         table=SECOND_SLOT_TABLE_NAME,
226         seed=SECOND_COL_RANDOM_SEED,
227         slot1=SECOND_COL_SLOT1,
228         slot2=SECOND_COL_SLOT2,
229         slot3=SECOND_COL_SLOT3,
230         slot4=SECOND_COL_SLOT4,
231         slot5=SECOND_COL_SLOT5,
232         slot6=SECOND_COL_SLOT6,
233         slot7=SECOND_COL_SLOT7)
234
235 # for sufficient value for slots
236 SUFFICIENT_MASTER_TABLE_NAME = u"sufficient_master"
237 SUFFICIENT_TABLE_NAME = u"sufficient_{id}"
238 SUFFICIENT_COL_AMULET_ID = u"amulet_id"
239 SUFFICIENT_COL_SUFFICIENT_TABLE_NAME = u"sufficient_table_name"
240 SUFFICIENT_COL_SUFFICIENT_VALUE = u"sufficient_value"
241 SUFFICIENT_COL_SLOT1_THRESHOLD = u"slot1_threshold"
242 SUFFICIENT_COL_SLOT2_THRESHOLD = u"slot2_threshold"
243 SUFFICIENT_COL_SLOT3_THRESHOLD = u"slot3_threshold"
244 SUFFICIENT_MASTER_TABLE_CREATE_SQL = u"""create table if not exists {master}
245     (id integer primary key, {amulet_id} integer, {table_name} varchar, 
246     foreign key(amulet_id) references {amulet_table}(id));""".format(
247         master=SUFFICIENT_MASTER_TABLE_NAME,
248         amulet_id=SUFFICIENT_COL_AMULET_ID,
249         amulet_table=AMULET_TABLE_NAME,
250         table_name=SUFFICIENT_COL_SUFFICIENT_TABLE_NAME)
251 SUFFICIENT_TABLE_CREATE_SQL = u"""create table if not exists {{table_name}} 
252     (id integer primary key, {sufficient_val} integer unique, 
253     {slot1_val} integer, {slot2_val} integer, {slot3_val} integer) ;""".format(
254         sufficient_val=SUFFICIENT_COL_SUFFICIENT_VALUE,
255         slot1_val=SUFFICIENT_COL_SLOT1_THRESHOLD,
256         slot2_val=SUFFICIENT_COL_SLOT2_THRESHOLD,
257         slot3_val=SUFFICIENT_COL_SLOT3_THRESHOLD)
258 SUFFICIENT_MASTER_TABLE_INSERT_SQL = u"""insert into {table}
259     ({amulet_col}, {table_col}) values(?,?);""".format(
260         table=SUFFICIENT_MASTER_TABLE_NAME,
261         table_col=SUFFICIENT_COL_SUFFICIENT_TABLE_NAME,
262         amulet_col=SUFFICIENT_COL_AMULET_ID)
263 SUFFICIENT_TABLE_INSERT_SQL = u"""insert into {{table_name}}
264     ({sufficient_val}, {slot1_val}, {slot2_val}, {slot3_val})
265     values(?,?,?,?);""".format(
266         sufficient_val=SUFFICIENT_COL_SUFFICIENT_VALUE,
267         slot1_val=SUFFICIENT_COL_SLOT1_THRESHOLD,
268         slot2_val=SUFFICIENT_COL_SLOT2_THRESHOLD,
269         slot3_val=SUFFICIENT_COL_SLOT3_THRESHOLD)
270 SUFFICIENT_MASTER_TABLE_SELECT_ALL_SQL = u"""select {amu_id}, {table_col} from {table}""".format(
271         amu_id=SUFFICIENT_COL_AMULET_ID,
272         table_col=SUFFICIENT_COL_SUFFICIENT_TABLE_NAME,
273         table=SUFFICIENT_MASTER_TABLE_NAME)
274 SUFFICIENT_TABLE_SELECT_ALL_SQL = u"""select {sufficient_val}, {slot1_val}, {slot2_val}, {slot3_val} from {{table_name}}""".format(
275         sufficient_val=SUFFICIENT_COL_SUFFICIENT_VALUE,
276         slot1_val=SUFFICIENT_COL_SLOT1_THRESHOLD,
277         slot2_val=SUFFICIENT_COL_SLOT2_THRESHOLD,
278         slot3_val=SUFFICIENT_COL_SLOT3_THRESHOLD)