· 6 years ago · Jan 11, 2020, 07:52 PM
1Database SQL_Initiate()
2{
3/*
4 "hextags"
5 {
6 "driver" "sqlite"
7 "host" "localhost"
8 "database" "hextags-sqlite"
9 "user" "root"
10 "pass" ""
11 //"timeout" "0"
12 //"port" "0"
13 }
14*/
15 Database db = null;
16
17 char error[255];
18 db = SQL_Connect("hextags", false, error, sizeof(error));
19
20 if( !db )
21 {
22 SetFailState("Could not connect to sql database: %s", error);
23 return null;
24 }
25
26 SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS hextags (steamid TEXT, chattag TEXT, chatcolor TEXT, scoretag TEXT);");
27
28 return db;
29}
30
31void SQL_LoadCustomTags(int client)
32{
33 if( !g_SQLDatabase )
34 {
35 return;
36 }
37
38 if( IsFakeClient(client) )
39 {
40 return;
41 }
42
43 char steamId[32];
44 GetClientAuthId(client, AuthId_Steam2, steamId, sizeof(steamId));
45
46 char buffer[256];
47 FormatEx(buffer, sizeof(buffer), "SELECT * FROM hextags WHERE steamid = '%s';", steamId);
48
49 SQL_TQuery(g_SQLDatabase, SQL_OnLoadQueryChecking, buffer, client);
50}
51
52void SQL_SaveCustomTags(int client)
53{
54 if( !g_SQLDatabase )
55 {
56 return;
57 }
58
59 if( IsFakeClient(client) )
60 {
61 return;
62 }
63
64 char steamId[32];
65 GetClientAuthId(client, AuthId_Steam2, steamId, sizeof(steamId));
66
67 char buffer[256];
68 FormatEx(buffer, sizeof(buffer), "INSERT INTO hextags VALUES ('%s', '%s', '%s', '%s')", steamId, sCustomTags[client][ChatTag], sCustomTags[client][ChatColor], sCustomTags[client][ScoreTag]);
69
70 SQL_TQuery(g_SQLDatabase, SQL_OnSaveQueryChecking, buffer);
71}
72
73public void SQL_OnLoadQueryChecking(Handle owner, Handle hndl, const char[] error, any data)
74{
75 if( !owner || !hndl )
76 {
77 LogError("Lost DB handle.");
78 }
79
80 if( strlen(error) > 0 )
81 {
82 LogError("SQL Loading Error: %s", error);
83 return;
84 }
85
86 if( SQL_FetchRow(hndl) )
87 {
88 int client = view_as<int> (data);
89
90 SQL_FetchString(hndl, 1, sCustomTags[client][ChatTag], sizeof(sCustomTags[][]));
91 SQL_FetchString(hndl, 2, sCustomTags[client][ChatColor], sizeof(sCustomTags[][]));
92 SQL_FetchString(hndl, 3, sCustomTags[client][ScoreTag], sizeof(sCustomTags[][]));
93 }
94}
95
96public void SQL_OnSaveQueryChecking(Handle owner, Handle hndl, const char[] error, any data)
97{
98 if( !owner || !hndl )
99 {
100 LogError("Lost DB handle.");
101 }
102
103 if( strlen(error) > 0 )
104 {
105 LogError("SQL Saving Error: %s", error);
106 }
107}