diff options
author | onefang | 2019-08-07 07:44:25 +1000 |
---|---|---|
committer | onefang | 2019-08-07 07:44:25 +1000 |
commit | 1613ca58be0cc3a4c53e72b63899b7fa4492ed7b (patch) | |
tree | 7f31bb248a6ce88409a3b742bc022017b3b027f0 /OpenSim/Data/MySQL/MySQLRaw.cs | |
parent | The beginnings of an account management web page. (diff) | |
download | opensim-SC-1613ca58be0cc3a4c53e72b63899b7fa4492ed7b.zip opensim-SC-1613ca58be0cc3a4c53e72b63899b7fa4492ed7b.tar.gz opensim-SC-1613ca58be0cc3a4c53e72b63899b7fa4492ed7b.tar.bz2 opensim-SC-1613ca58be0cc3a4c53e72b63899b7fa4492ed7b.tar.xz |
Swap out MySQLGenericHandler and swap in MySQLRaw.
Smaller, simpler, works better.
Diffstat (limited to 'OpenSim/Data/MySQL/MySQLRaw.cs')
-rw-r--r-- | OpenSim/Data/MySQL/MySQLRaw.cs | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/OpenSim/Data/MySQL/MySQLRaw.cs b/OpenSim/Data/MySQL/MySQLRaw.cs new file mode 100644 index 0000000..4be6d90 --- /dev/null +++ b/OpenSim/Data/MySQL/MySQLRaw.cs | |||
@@ -0,0 +1,142 @@ | |||
1 | // https://dev.mysql.com/doc/connector-net/en/ | ||
2 | |||
3 | |||
4 | using System; | ||
5 | using System.Collections; | ||
6 | using System.Collections.Generic; | ||
7 | using System.Data; | ||
8 | using System.Reflection; | ||
9 | using System.Text; | ||
10 | using log4net; | ||
11 | using MySql.Data.MySqlClient; | ||
12 | |||
13 | namespace OpenSim.Data.MySQL | ||
14 | { | ||
15 | public class MySQLRaw | ||
16 | { | ||
17 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
18 | private string m_connectString; | ||
19 | |||
20 | public MySQLRaw(string connect) | ||
21 | { | ||
22 | m_connectString = connect; | ||
23 | } | ||
24 | |||
25 | public int Count(string table) | ||
26 | { | ||
27 | return Count(table, ""); | ||
28 | } | ||
29 | public int Count(string table, string wher) | ||
30 | { | ||
31 | string query = "SELECT Count(*) FROM " + table; | ||
32 | if ("" != wher) | ||
33 | query = query + " WHERE " + wher; | ||
34 | int result = -1; | ||
35 | |||
36 | object r = doScalarQuery(query); | ||
37 | if (r != null) | ||
38 | result = Convert.ToInt32(r); | ||
39 | |||
40 | return result; | ||
41 | } | ||
42 | |||
43 | public List< Hashtable > Select(string table, string wher) | ||
44 | { | ||
45 | string query = "SELECT * FROM " + table; | ||
46 | if ("" != wher) | ||
47 | query = query + " WHERE " + wher; | ||
48 | |||
49 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
50 | { | ||
51 | dbcon.Open(); | ||
52 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
53 | MySqlDataReader rdr = cmd.ExecuteReader(); | ||
54 | List<string> names = new List<string>(); | ||
55 | DataTable schema = rdr.GetSchemaTable(); | ||
56 | List< Hashtable > list = new List< Hashtable >(); | ||
57 | |||
58 | foreach (DataRow row in schema.Rows) | ||
59 | { | ||
60 | string tbl = ""; | ||
61 | string nm = ""; | ||
62 | string tp = ""; | ||
63 | foreach (DataColumn col in schema.Columns) | ||
64 | { | ||
65 | if ("BaseTableName" == col.ColumnName) tbl = row[col].ToString(); | ||
66 | if ("ColumnName" == col.ColumnName) nm = row[col].ToString(); | ||
67 | if ("DataType" == col.ColumnName) tp = row[col].ToString(); | ||
68 | } | ||
69 | names.Add(nm); | ||
70 | } | ||
71 | |||
72 | while (rdr.Read()) | ||
73 | { | ||
74 | Hashtable r = new Hashtable(); | ||
75 | foreach (string name in names) | ||
76 | { | ||
77 | r[name] = rdr[name]; | ||
78 | } | ||
79 | list.Add(r); | ||
80 | } | ||
81 | |||
82 | rdr.Close(); | ||
83 | dbcon.Close(); | ||
84 | return list; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | private object doScalarQuery(string query) | ||
89 | { | ||
90 | try | ||
91 | { | ||
92 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
93 | { | ||
94 | dbcon.Open(); | ||
95 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
96 | Object ret = cmd.ExecuteScalar(); | ||
97 | dbcon.Close(); | ||
98 | return ret; | ||
99 | } | ||
100 | } | ||
101 | catch (MySqlException e) | ||
102 | { | ||
103 | m_log.ErrorFormat("[MYSQL RAW]: Problem connecting to the database {0}", e.Message); | ||
104 | return null; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | private void doNonQuery(string query) | ||
109 | { | ||
110 | using (MySqlConnection dbcon = new MySqlConnection(m_connectString)) | ||
111 | { | ||
112 | dbcon.Open(); | ||
113 | MySqlCommand cmd = new MySqlCommand(query, dbcon); | ||
114 | cmd.ExecuteNonQuery(); | ||
115 | dbcon.Close(); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | public void Insert(string table) | ||
120 | { | ||
121 | string query = "INSERT INTO " + table + " (name, age) VALUES('John Smith', '33')"; | ||
122 | doNonQuery(query); | ||
123 | } | ||
124 | |||
125 | public void Update(string table, string wher) | ||
126 | { | ||
127 | string query = "UPDATE " + table + " SET name='Joe', age='22'"; | ||
128 | if ("" != wher) | ||
129 | query = query + " WHERE " + wher; | ||
130 | doNonQuery(query); | ||
131 | } | ||
132 | |||
133 | public void Delete(string table, string wher) | ||
134 | { | ||
135 | string query = "DELETE FROM " + table; | ||
136 | if ("" != wher) | ||
137 | query = query + " WHERE " + wher; | ||
138 | doNonQuery(query); | ||
139 | } | ||
140 | |||
141 | } | ||
142 | } | ||