· 9 years ago · Oct 13, 2016, 06:42 PM
1public SQLCallback_Connect(Handle:owner, Handle:hndl, const String:error[], any:data)
2{
3 if(hndl==INVALID_HANDLE)
4 {
5 SetFailState("Failed to connect to SQL database. Error: %s", error);
6 }
7 else
8 {
9 // If it's already connected we are good to go
10 if(g_hDatabase != INVALID_HANDLE)
11 return;
12
13 g_hDatabase = hndl;
14 decl String:m_szDriver[2];
15 SQL_ReadDriver(g_hDatabase, STRING(m_szDriver));
16 if(m_szDriver[0] == 'm')
17 {
18 g_bMySQL = true;
19 SQL_TVoid(g_hDatabase, "CREATE TABLE IF NOT EXISTS `store_players` (\
20 `id` int(11) NOT NULL AUTO_INCREMENT,\
21 `authid` varchar(32) NOT NULL,\
22 `name` varchar(64) NOT NULL,\
23 `credits` int(11) NOT NULL,\
24 `date_of_join` int(11) NOT NULL,\
25 `date_of_last_join` int(11) NOT NULL,\
26 PRIMARY KEY (`id`),\
27 UNIQUE KEY `id` (`id`),\
28 UNIQUE KEY `authid` (`authid`)\
29 )");
30 SQL_TVoid(g_hDatabase, "CREATE TABLE IF NOT EXISTS `store_items` (\
31 `id` int(11) NOT NULL AUTO_INCREMENT,\
32 `player_id` int(11) NOT NULL,\
33 `type` varchar(16) NOT NULL,\
34 `unique_id` varchar(256) NOT NULL,\
35 `date_of_purchase` int(11) NOT NULL,\
36 `date_of_expiration` int(11) NOT NULL,\
37 PRIMARY KEY (`id`)\
38 )");
39 SQL_TVoid(g_hDatabase, "CREATE TABLE IF NOT EXISTS `store_equipment` (\
40 `player_id` int(11) NOT NULL,\
41 `type` varchar(16) NOT NULL,\
42 `unique_id` varchar(256) NOT NULL,\
43 `slot` int(11) NOT NULL\
44 )");
45 SQL_TVoid(g_hDatabase, "CREATE TABLE IF NOT EXISTS `store_logs` (\
46 `id` int(11) NOT NULL AUTO_INCREMENT,\
47 `player_id` int(11) NOT NULL,\
48 `credits` int(11) NOT NULL,\
49 `reason` varchar(256) NOT NULL,\
50 `date` int(11) NOT NULL,\
51 PRIMARY KEY (`id`)\
52 )");
53 SQL_TQuery(g_hDatabase, SQLCallback_NoError, "ALTER TABLE store_items ADD COLUMN price_of_purchase int(11)");
54 decl String:m_szQuery[512];
55 Format(STRING(m_szQuery), "CREATE TABLE IF NOT EXISTS `%s` (\
56 `id` int(11) NOT NULL AUTO_INCREMENT,\
57 `parent_id` int(11) NOT NULL DEFAULT '-1',\
58 `item_price` int(32) NOT NULL,\
59 `item_type` varchar(64) NOT NULL,\
60 `item_flag` varchar(64) NOT NULL,\
61 `item_name` varchar(64) NOT NULL,\
62 `additional_info` text NOT NULL,\
63 `item_status` tinyint(1) NOT NULL,\
64 `supported_game` varchar(64) NOT NULL,\
65 PRIMARY KEY (`id`)\
66 )", g_eCvars[g_cvarItemsTable][sCache]);
67 SQL_TVoid(g_hDatabase, m_szQuery);
68 }
69 else
70 {
71 SQL_TVoid(g_hDatabase, "CREATE TABLE IF NOT EXISTS `store_players` (\
72 `id` INTEGER PRIMARY KEY AUTOINCREMENT,\
73 `authid` varchar(32) NOT NULL,\
74 `name` varchar(64) NOT NULL,\
75 `credits` int(11) NOT NULL,\
76 `date_of_join` int(11) NOT NULL,\
77 `date_of_last_join` int(11) NOT NULL\
78 )");
79 SQL_TVoid(g_hDatabase, "CREATE TABLE IF NOT EXISTS `store_items` (\
80 `id` INTEGER PRIMARY KEY AUTOINCREMENT,\
81 `player_id` int(11) NOT NULL,\
82 `type` varchar(16) NOT NULL,\
83 `unique_id` varchar(256) NOT NULL,\
84 `date_of_purchase` int(11) NOT NULL,\
85 `date_of_expiration` int(11) NOT NULL\
86 )");
87 SQL_TVoid(g_hDatabase, "CREATE TABLE IF NOT EXISTS `store_equipment` (\
88 `player_id` int(11) NOT NULL,\
89 `type` varchar(16) NOT NULL,\
90 `unique_id` varchar(256) NOT NULL,\
91 `slot` int(11) NOT NULL\
92 )");
93 SQL_TQuery(g_hDatabase, SQLCallback_NoError, "ALTER TABLE store_items ADD COLUMN price_of_purchase int(11)");
94 if(strcmp(g_eCvars[g_cvarItemSource][sCache], "database")==0)
95 {
96
97 SetFailState("Database item source can only be used with MySQL databases");
98 }
99 }
100
101 // Do some housekeeping
102 decl String:m_szQuery[256];
103 Format(STRING(m_szQuery), "DELETE FROM store_items WHERE `date_of_expiration` <> 0 AND `date_of_expiration` < %d", GetTime());
104 SQL_TVoid(g_hDatabase, m_szQuery);
105 }
106}