summaryrefslogtreecommitdiff
path: root/src/dalstoragesql.h
blob: 077b7b0a8115f43acb4fd2e99e89fc4b05be25b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 *  The Mana World Server
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World  is free software; you can redistribute  it and/or modify it
 *  under the terms of the GNU General  Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or any later version.
 *
 *  The Mana  World is  distributed in  the hope  that it  will be  useful, but
 *  WITHOUT ANY WARRANTY; without even  the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *  more details.
 *
 *  You should  have received a  copy of the  GNU General Public  License along
 *  with The Mana  World; if not, write to the  Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *  $Id$
 */


#ifndef _TMWSERV_DALSTORAGE_SQL_H_
#define _TMWSERV_DALSTORAGE_SQL_H_


#if !defined (MYSQL_SUPPORT) && !defined (SQLITE_SUPPORT) && \
    !defined (POSTGRESQL_SUPPORT)

#error "(dalstorage.h) no database backend defined"
#endif


#include <string>

// TODO: Fix problem with PostgreSQL null primary key's.

/**
 * MySQL specificities:
 *     - TINYINT is an integer (1 byte) type defined as an extension to
 *       the SQL standard.
 *     - all integer types can have an optional (non-standard) attribute
 *       UNSIGNED (http://dev.mysql.com/doc/mysql/en/numeric-types.html)
 *
 * SQLite3 specificities:
 *     - any column (but only one for each table) with the exact type of
 *       'INTEGER PRIMARY KEY' is taken as auto-increment.
 *     - the supported data types are: NULL, INTEGER, REAL, TEXT and BLOB
 *       (http://www.sqlite.org/datatype3.html)
 *     - the size of TEXT cannot be set, it is just ignored by the engine.
 *     - IMPORTANT: foreign key constraints are not yet supported
 *       (http://www.sqlite.org/omitted.html). Included in case of future
 *       support.
 *
 * Notes:
 *     - the SQL queries will take advantage of the most appropriate data
 *       types supported by a particular database engine in order to
 *       optimize the server database size.
 */


namespace {


/**
 * TABLE: tmw_maps.
 */
const std::string MAPS_TBL_NAME("tmw_maps");
const std::string SQL_MAPS_TABLE(
    "CREATE TABLE tmw_maps ("
#if defined (MYSQL_SUPPORT)
        "id  TINYINT PRIMARY KEY AUTO_INCREMENT,"
        "map TEXT    NOT NULL,"
        "INDEX (id)"
#elif defined (SQLITE_SUPPORT)
        "id  INTEGER PRIMARY KEY,"
        "map TEXT    NOT NULL"
#elif defined (POSTGRESQL_SUPPORT)
        "id  SERIAL  PRIMARY KEY,"
        "map TEXT    NOT NULL"
#endif
    ");"
);


/**
 * TABLE: tmw_accounts.
 *
 * Notes:
 *     - the user levels are:
 *           0: normal user
 *           1: moderator (has medium level rights)
 *           2: administrator (i am god :))
 *     - the 'banned' field contains the UNIX time of unban (default = 0)
 */
const std::string ACCOUNTS_TBL_NAME("tmw_accounts");
const std::string SQL_ACCOUNTS_TABLE(
    "CREATE TABLE tmw_accounts ("
#if defined (MYSQL_SUPPORT)
        "id       INTEGER     PRIMARY KEY AUTO_INCREMENT,"
        "username VARCHAR(32) NOT NULL UNIQUE,"
        "password VARCHAR(32) NOT NULL,"
        "email    VARCHAR(64) NOT NULL,"
        "level    TINYINT     UNSIGNED NOT NULL,"
        "banned   TINYINT     UNSIGNED NOT NULL,"
        "INDEX (id)"
#elif defined (SQLITE_SUPPORT)
        "id       INTEGER     PRIMARY KEY,"
        "username TEXT        NOT NULL UNIQUE,"
        "password TEXT        NOT NULL,"
        "email    TEXT        NOT NULL,"
        "level    INTEGER     NOT NULL,"
        "banned   INTEGER     NOT NULL"
#elif defined (POSTGRESQL_SUPPORT)
        "id       SERIAL      PRIMARY KEY,"
        "username TEXT        NOT NULL UNIQUE,"
        "password TEXT        NOT NULL,"
        "email    TEXT        NOT NULL,"
        "level    INTEGER     NOT NULL,"
        "banned   INTEGER     NOT NULL"
#endif
    ");"
);


/**
 * TABLE: tmw_characters.
 *
 * Notes:
 *     - the stats will need to be thought over, as we'll be implementing a
 *       much more elaborate skill based system; we should probably have a
 *       separate table for storing the skill levels.
 *     - gender is 0 for male, 1 for female.
 */
const std::string CHARACTERS_TBL_NAME("tmw_characters");
const std::string SQL_CHARACTERS_TABLE(
    "CREATE TABLE tmw_characters ("
#if defined (MYSQL_SUPPORT)
        "id      INTEGER     PRIMARY KEY AUTO_INCREMENT,"
        "user_id INTEGER     UNSIGNED NOT NULL,"
        "name    VARCHAR(32) NOT NULL UNIQUE,"
        // general information about the character
        "gender  TINYINT     UNSIGNED NOT NULL,"
        "level   TINYINT     UNSIGNED NOT NULL,"
        "money   INTEGER     UNSIGNED NOT NULL,"
        // location on the map
        "x       SMALLINT    UNSIGNED NOT NULL,"
        "y       SMALLINT    UNSIGNED NOT NULL,"
        "map_id  TINYINT     NOT NULL,"
        // stats
        "str     SMALLINT    UNSIGNED NOT NULL,"
        "agi     SMALLINT    UNSIGNED NOT NULL,"
        "vit     SMALLINT    UNSIGNED NOT NULL,"
        // note: int must be backquoted as it's a MySQL keyword
        "`int`   SMALLINT    UNSIGNED NOT NULL,"
        "dex     SMALLINT    UNSIGNED NOT NULL,"
        "luck    SMALLINT    UNSIGNED NOT NULL,"
        "FOREIGN KEY (user_id) REFERENCES tmw_accounts(id),"
        "FOREIGN KEY (map_id)  REFERENCES tmw_maps(id),"
        "INDEX (id)"
#elif defined (SQLITE_SUPPORT)
        "id      INTEGER     PRIMARY KEY,"
        "user_id INTEGER     NOT NULL,"
        "name    TEXT        NOT NULL UNIQUE,"
        // general information about the character
        "gender  INTEGER     NOT NULL,"
        "level   INTEGER     NOT NULL,"
        "money   INTEGER     NOT NULL,"
        // location on the map
        "x       INTEGER     NOT NULL,"
        "y       INTEGER     NOT NULL,"
        "map_id  INTEGER     NOT NULL,"
        // stats
        "str     INTEGER     NOT NULL,"
        "agi     INTEGER     NOT NULL,"
        "vit     INTEGER     NOT NULL,"
        "int     INTEGER     NOT NULL,"
        "dex     INTEGER     NOT NULL,"
        "luck    INTEGER     NOT NULL,"
        "FOREIGN KEY (user_id) REFERENCES tmw_accounts(id),"
        "FOREIGN KEY (map_id)  REFERENCES tmw_maps(id)"
#elif defined (POSTGRESQL_SUPPORT)
        "id      SERIAL      PRIMARY KEY,"
        "user_id INTEGER     NOT NULL,"
        "name    TEXT        NOT NULL UNIQUE,"
        // general information about the character
        "gender  INTEGER     NOT NULL,"
        "level   INTEGER     NOT NULL,"
        "money   INTEGER     NOT NULL,"
        // location on the map
        "x       INTEGER     NOT NULL,"
        "y       INTEGER     NOT NULL,"
        "map_id  INTEGER     NOT NULL,"
        // stats
        "str     INTEGER     NOT NULL,"
        "agi     INTEGER     NOT NULL,"
        "vit     INTEGER     NOT NULL,"
        "int     INTEGER     NOT NULL,"
        "dex     INTEGER     NOT NULL,"
        "luck    INTEGER     NOT NULL,"
        "FOREIGN KEY (user_id) REFERENCES tmw_accounts(id),"
        "FOREIGN KEY (map_id)  REFERENCES tmw_maps(id)"
#endif
    ");"
);


/**
 * TABLE: tmw_items.
 *
 * Notes:
 *     - amount: indicates how many items of the same kind can stack.
 *     - state: (optional) item state saved by script.
 */
const std::string ITEMS_TBL_NAME("tmw_items");
const std::string SQL_ITEMS_TABLE(
    "CREATE TABLE tmw_items ("
#if defined (MYSQL_SUPPORT)
        "id     SMALLINT PRIMARY KEY AUTO_INCREMENT,"
        "amount TINYINT  UNSIGNED NOT NULL,"
        "type   TINYINT  UNSIGNED NOT NULL,"
        "state  TEXT,"
        "INDEX (id)"
#elif defined (SQLITE_SUPPORT)
        "id     INTEGER  PRIMARY KEY,"
        "amount INTEGER  NOT NULL,"
        "type   INTEGER  NOT NULL,"
        "state  TEXT"
#elif defined (POSTGRESQL_SUPPORT)
        "id     SERIAL   PRIMARY KEY,"
        "amount INTEGER  NOT NULL,"
        "type   INTEGER  NOT NULL,"
        "state  TEXT"
#endif
    ");"
);


/**
 * TABLE: tmw_world_items.
 *
 * Notes:
 *     - store items on the ground in the game world.
 */
const std::string WORLD_ITEMS_TBL_NAME("tmw_world_items");
// NOTE: Problem here with primary key (only one type of item is allowed on the same map at one time).
const std::string SQL_WORLD_ITEMS_TABLE(
    "CREATE TABLE tmw_world_items ("
#if defined (MYSQL_SUPPORT)
        "id        SMALLINT UNSIGNED NOT NULL,"
        // location on the map
        "x         SMALLINT UNSIGNED NOT NULL,"
        "y         SMALLINT UNSIGNED NOT NULL,"
        "map_id    TINYINT  NOT NULL,"
        // time to die (UNIX time)
        "deathtime INTEGER  UNSIGNED NOT NULL,"
        "PRIMARY KEY (id, map_id)," 
        "FOREIGN KEY (id)     REFERENCES tmw_items(id),"
        "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)"
#elif defined (SQLITE_SUPPORT)
        "id        INTEGER  NOT NULL,"
        // location on the map
        "x         INTEGER  NOT NULL,"
        "y         INTEGER  NOT NULL,"
        "map_id    INTEGER  NOT NULL,"
        // time to die (UNIX time)
        "deathtime INTEGER  NOT NULL,"
        "PRIMARY KEY (id, map_id),"
        "FOREIGN KEY (id)     REFERENCES tmw_items(id),"
        "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)"
#elif defined (POSTGRESQL_SUPPORT)
        "id        INTEGER  NOT NULL,"
        // location on the map
        "x         INTEGER  NOT NULL,"
        "y         INTEGER  NOT NULL,"
        "map_id    INTEGER  NOT NULL,"
        // time to die (UNIX time)
        "deathtime INTEGER  NOT NULL,"
        "PRIMARY KEY (id, map_id),"
        "FOREIGN KEY (id)     REFERENCES tmw_items(id),"
        "FOREIGN KEY (map_id) REFERENCES tmw_maps(id)"
#endif
    ");"
);


/**
 * TABLE: tmw_inventories.
 */
const std::string INVENTORIES_TBL_NAME("tmw_inventories");
const std::string SQL_INVENTORIES_TABLE(
    "CREATE TABLE tmw_inventories ("
#if defined (MYSQL_SUPPORT)
        "id       SMALLINT NOT NULL,"
        "owner_id INTEGER  NOT NULL,"
        "FOREIGN KEY (id)       REFERENCES tmw_items(id),"
        "FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)"
#elif defined (SQLITE_SUPPORT)
        "id       INTEGER  NOT NULL,"
        "owner_id INTEGER  NOT NULL,"
        "FOREIGN KEY (id)     REFERENCES tmw_items(id),"
        "FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)"
#elif defined (POSTGRESQL_SUPPORT)
        "id       INTEGER  NOT NULL,"
        "owner_id INTEGER  NOT NULL,"
        "FOREIGN KEY (id)       REFERENCES tmw_items(id),"
        "FOREIGN KEY (owner_id) REFERENCES tmw_characters(id)"
#endif
    ");"
);


} // anonymous namespace


#endif // _TMWSERV_DALSTORAGE_SQL_H_