-
Notifications
You must be signed in to change notification settings - Fork 0
/
AccessDatabase.cs
265 lines (233 loc) · 7.75 KB
/
AccessDatabase.cs
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
using UnityEngine;
//using MySql.Data;
using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
// SQL:
// select U_Level, U_Name, U_Password from User;
// select ArtW_ID, Fig_Name from Artwork where ArtW_level = 1; ---> for VIP user only
// select ArtW_ID, Fig_Name from Artwork where ArtW_level = 1 OR ArtW_level = 2; ---> for all of the users
public class AccessDatabase : MonoBehaviour {
#region Variables
// Static Variables
// check the attribute's name
float posx, posy, posz; // painting location
int UID;
// Private Variables
// private bool loading = false;
internal MySqlConnection connect;
// command object
private MySqlCommand cmd = null;
// reader object
private MySqlDataReader datareader = null;
// object collection array
private GameObject[] bodies;
// private Dictionary<string, string> loginPair = new Dictionary<string, string>();
// object definitions
internal struct userData
{
internal int uLevel;
internal string uName;
internal string uPassword;
}
internal List<userData> loginInfo;
internal List<string> figName;
//public struct PosData
//{
//public int U_ID;
//public float pos_x, pos_y, pos_z;
//}
//internal List<PosData> ClonePos;
#endregion
void Awake()
{
print("AccessData Awake() ^_^");
}
// Update is called once per frame
void Update() {
}
internal void buildConnect(string constr)
{
try
{
print("AccessData buildConnect() ^_^");
connect = new MySqlConnection(constr);
if (connect == null)
{
print("Error connect load!");
}
connect.Open();
Debug.Log("Connection State: " + connect.State);
}
catch (Exception ex)
{
Debug.Log(ex.ToString());
}
}
// Read all entries from user table
internal void ReadUserInfo()
{
print("Excuted AccessData ReadUserInfo() ^_^");
string query = string.Empty;
if (loginInfo == null)
loginInfo = new List<userData>();
if (loginInfo.Count > 0)
loginInfo.Clear();
try
{
query = "select `U_Level`, `U_Name`, `U_Password` from `VR`.`User`";
if (connect.State.ToString() != "Open")
connect.Open();
using (connect)
{
using (cmd = new MySqlCommand(query, connect))
{
datareader = cmd.ExecuteReader();
if (datareader.HasRows)
while (datareader.Read())
{
userData uInfo = new userData();
//database always returns string
uInfo.uLevel = int.Parse(datareader["U_Level"].ToString()); //string --> int
uInfo.uName = datareader["U_Name"].ToString();
uInfo.uPassword = datareader["U_Password"].ToString();
loginInfo.Add(uInfo);
}
datareader.Dispose();
}
}
}
catch (Exception ex)
{
Debug.Log(ex.ToString());
}
}
// Update pic location of pictures in the table based on the iddemo_table
internal void UpdateEntries(int UID, float posx, float posy, float posz)
{
//prepPos();
string query = string.Empty;
try
{
query = "UPDATE `VR`.`Position` SET `X_p`=?posx, `Y_p`=?posy, `Z_p`=?posz WHERE `Artwork_ArtW_ID`=?UID";
if (connect.State.ToString() != "Open")
connect.Open();
using (connect)
{
using (cmd = new MySqlCommand(query, connect))
{
MySqlParameter oParamx = cmd.Parameters.Add("?posx", MySqlDbType.Float);
oParamx.Value = posx;
MySqlParameter oParamy = cmd.Parameters.Add("?posy", MySqlDbType.Float);
oParamy.Value = posy;
MySqlParameter oParamz = cmd.Parameters.Add("?posz", MySqlDbType.Float);
oParamz.Value = posz;
MySqlParameter oParamID = cmd.Parameters.Add("?UID", MySqlDbType.Int32);
oParamID.Value = UID;
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
Debug.Log(ex.ToString());
}
finally
{
}
}
// read VIP figure info from database
internal void Read1FigInfo()
{
print("Excuted AccessData Read1FigInfo() ^_^");
string query = string.Empty;
if (figName == null)
figName = new List<string>();
if (figName.Count > 0)
figName.Clear();
try
{
query = "select `ArtW_ID`, `Fig_Name` from `VR`.`Artwork` where `ArtW_level` = 1";
if (connect.State.ToString() != "Open")
connect.Open();
using (connect)
{
using (cmd = new MySqlCommand(query, connect))
{
datareader = cmd.ExecuteReader();
if (datareader.HasRows)
while (datareader.Read())
{
string loc;
//artWInfo.arwID = int.Parse(datareader["ArtW_ID"].ToString());
loc = datareader["Fig_Name"].ToString();
figName.Add(loc);
print(loc);
}
datareader.Dispose();
}
}
if (figName == null){
print("Error: get figName failed");
}
}
catch (Exception ex)
{
Debug.Log(ex.ToString());
}
finally
{
}
}
// read all users' figure info from database
internal void Read2FigInfo()
{
print("Excuted AccessData Read2FigInfo() ^_^");
string query = string.Empty;
if (figName == null)
figName = new List<string>();
if (figName.Count > 0)
figName.Clear();
try
{
query = "select `ArtW_ID`, `Fig_Name` from `VR`.`Artwork` where `ArtW_level` = 1 OR `ArtW_level` = 2";
if (connect.State.ToString() != "Open")
connect.Open();
using (connect)
{
using (cmd = new MySqlCommand(query, connect))
{
datareader = cmd.ExecuteReader();
if (datareader.HasRows)
while (datareader.Read())
{
string loc;
loc = datareader["Fig_Name"].ToString();
figName.Add(loc);
}
datareader.Dispose();
}
}
}
catch (Exception ex)
{
Debug.Log(ex.ToString());
}
finally
{
}
}
// close connection to database
void OnApplicationQuit()
{
Debug.Log("killing con");
if (connect != null)
{
if (connect.State.ToString() != "Closed")
connect.Close();
connect.Dispose();
}
}
}