-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclasses.cs
290 lines (233 loc) · 8.32 KB
/
classes.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
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
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Data.SqlServerCe;
namespace ms
{
class DBHandler {
private static string _fileName;
private static string _password;
private string _conStr;
private SqlCeConnection _cn = null;
public string fileName{
get {
return _fileName;
}
set {
if(!hasExtension(value, ".sdf")){
value += ".sdf";
}
_fileName = value;
}
}
public string password {
set {
_password = value;
}
}
public DBHandler(string file_name, string pass) {
fileName = file_name;
password = pass;
_conStr = string.Format("DataSource=\"{0}\"; Password='{1}';", fileName, _password);
if (DBexists()) {
connect();
}
}
~DBHandler() {
disconnect();
}
public bool DBexists() {
if (File.Exists(fileName)) {
return true;
}
return false;
}
public void create() {
SqlCeEngine en = new SqlCeEngine(_conStr);
en.CreateDatabase();
connect();
}
public bool isEmpty(string table_name) {
string q = string.Format("SELECT * FROM {0}", table_name);
SqlCeCommand cmd = new SqlCeCommand(q, _cn);
SqlCeDataReader r;
r = cmd.ExecuteReader();
if (!r.Read()) {
return true;
}
return false;
}
public bool existsTables(params string[] table_names) {
string q = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE <> 'VIEW'";
ArrayList existing_tables = new ArrayList();
SqlCeCommand cmd = new SqlCeCommand(q, _cn);
SqlCeDataReader r;
r = cmd.ExecuteReader();
while(r.Read()) {
existing_tables.Add(r.GetString(0));
}
foreach (string tablename in table_names) {
if (!existing_tables.Contains(tablename)) {
return false;
}
}
return true;
}
public void createTable(string table_query) {
string q = "CREATE TABLE {0}";
SqlCeCommand cmd = new SqlCeCommand(string.Format(q, table_query), _cn);
cmd.ExecuteNonQuery();
}
public bool insert(string table_name, Dictionary<string, string> cols_vals, bool silent = false){
string q = string.Format("INSERT INTO {0}({1}) VALUES('{2}')", table_name, string.Join(",", cols_vals.Keys.ToArray()),
string.Join("','", cols_vals.Values.ToArray()));
int rows = 0;
SqlCeCommand cmd = new SqlCeCommand(q, _cn);
try {
rows = cmd.ExecuteNonQuery();
}
catch (SqlCeException e) {
if (!silent) {
throw e;
}
}
if (rows > 0) {
return true;
}
return false;
}
public SqlCeDataReader getData(string q) {
SqlCeCommand cmd = new SqlCeCommand(q, _cn);
return cmd.ExecuteReader();
}
public void delete(string query) {
string q = string.Format("DELETE FROM {0}", query);
SqlCeCommand cmd = new SqlCeCommand(q, _cn);
cmd.ExecuteNonQuery();
}
private bool hasExtension(string str, string extension) {
if (str.IndexOf(extension) > 0) {
return true;
}
return false;
}
private void connect() {
if (null == _cn) {
_cn = new SqlCeConnection(_conStr);
if (_cn.State == System.Data.ConnectionState.Closed) {
_cn.Open();
}
}
}
private void disconnect() {
if (null != _cn) {
_cn.Close();
}
}
}
class XmlParser : XmlTextReader {
private string _element_name;
private string _element_attr;
private string _subelement_name;
private string _subelement_attr;
public XmlParser(string URL, string element_name, string element_attr, string subelement_name, string subelement_attr)
: base(URL) {
this._element_name = element_name;
this._element_attr = element_attr;
this._subelement_name = subelement_name;
this._subelement_attr = subelement_attr;
}
public DateTime getDate() {
DateTime dt = new DateTime();
bool set = false;
while (this.Read() && !set) {
switch (this.NodeType) {
case XmlNodeType.Element:
if (this.Name == _element_name) {
while (this.MoveToNextAttribute()) {
if (this.Name == _element_attr) {
dt = DateTime.Parse(this.Value);
set = true;
break;
}
}
}
break;
}
}
return dt;
}
public Dictionary<string, Dictionary<string, decimal>> parse() {
Dictionary<string, Dictionary<string, decimal>> currencies = new Dictionary<string, Dictionary<string, decimal>>();
string current_key = null;
string current_currency = null;
while (this.Read()) {
switch (this.NodeType) {
case XmlNodeType.Element:
if (this.Name == _element_name) {
while (this.MoveToNextAttribute()) {
if (this.Name == _element_attr) {
currencies.Add(this.Value, new Dictionary<string, decimal>());
current_key = this.Value;
}
}
}
else if (this.Name == _subelement_name && null != current_key) {
while (this.MoveToNextAttribute()) {
if (this.Name == _subelement_attr) {
current_currency = this.Value;
}
}
}
break;
case XmlNodeType.Text:
if (null != current_currency) {
currencies[current_key][current_currency] = decimal.Parse(this.Value);
}
break;
}
}
return currencies;
}
}
class Converter {
private Dictionary<string, decimal> _conversionRates;
public readonly decimal ERR_NOFROM = -1;
public readonly decimal ERR_NOTO = -2;
public readonly decimal ERR_OF = -3;
public Dictionary<string, decimal> conversionRates{
get {
return _conversionRates;
}
set {
_conversionRates = value;
}
}
public Converter() {
}
public Converter(Dictionary<string, decimal> conversionRates) {
_conversionRates = conversionRates;
}
public decimal convert(decimal amount, string from, string to) {
from = from.ToUpper();
to = to.ToUpper();
if(!conversionRates.ContainsKey(from)){
return ERR_NOFROM;
}
if(!conversionRates.ContainsKey(to)){
return ERR_NOTO;
}
try {
return (decimal)amount * conversionRates[from] / conversionRates[to];
}
catch (OverflowException e) {
return ERR_OF;
}
}
}
}