-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataBaseIOS.cs
More file actions
267 lines (236 loc) · 8.94 KB
/
DataBaseIOS.cs
File metadata and controls
267 lines (236 loc) · 8.94 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ExpressBase.Mobile.Data;
using ExpressBase.Mobile.Helpers;
using ExpressBase.Mobile.iOS.Data;
using Mono.Data.Sqlite;
using Xamarin.Forms;
[assembly: Dependency(typeof(DataBaseIOS))]
namespace ExpressBase.Mobile.iOS.Data
{
public class DataBaseIOS : IDataBase
{
public string DbPath { get; set; }
public int CreateDB(string sid)
{
try
{
if (string.IsNullOrEmpty(sid))
return 0;
string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), string.Format("{0}.db3", sid));
if (!File.Exists(dbPath))
{
Mono.Data.Sqlite.SqliteConnection.CreateFile(dbPath);
this.DbPath = dbPath;
return 1;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return 0;
}
public void SetDbPath(string sid)
{
try
{
if (string.IsNullOrEmpty(sid))
return;
string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), string.Format("{0}.db3", sid));
if (!File.Exists(dbPath))
Mono.Data.Sqlite.SqliteConnection.CreateFile(dbPath);
this.DbPath = dbPath;
}
catch (Exception ex)
{
EbLog.Error(ex.Message);
}
}
public EbDataTable DoQuery(string query, params DbParameter[] parameters)
{
EbDataTable dt = new EbDataTable();
try
{
using (SqliteConnection con = new SqliteConnection("Data Source=" + this.DbPath))
{
con.Open();
using (SqliteCommand cmd = con.CreateCommand())
{
cmd.CommandText = query;
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(this.DbParamToSqlParam(parameters));
using (var reader = cmd.ExecuteReader())
{
PrepareDataTable(reader, dt);
}
}
con.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return dt;
}
public EbDataSet DoQueries(string query, params DbParameter[] parameters)
{
EbDataSet ds = new EbDataSet();
try
{
using (SqliteConnection con = new SqliteConnection("Data Source=" + this.DbPath))
{
con.Open();
using (SqliteCommand cmd = con.CreateCommand())
{
cmd.CommandText = query;
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(this.DbParamToSqlParam(parameters));
using (var reader = cmd.ExecuteReader())
{
do
{
EbDataTable dt = new EbDataTable();
PrepareDataTable(reader, dt);
ds.Tables.Add(dt);
ds.RowNumbers += dt.Rows.Count.ToString() + ",";
}
while (reader.NextResult());
}
}
con.Close();
}
}
catch (Exception ex)
{
EbLog.Error("DataBaseAndroid.DoQueries---" + ex.Message);
}
return ds;
}
public int DoNonQuery(string query, params DbParameter[] parameters)
{
try
{
using (SqliteConnection con = new SqliteConnection("Data Source=" + this.DbPath))
{
con.Open();
using (SqliteCommand cmd = con.CreateCommand())
{
cmd.CommandText = query;
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(this.DbParamToSqlParam(parameters));
return cmd.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return 0;
}
public void DoNonQueryBatch(EbDataTable Table)
{
try
{
using (SqliteConnection con = new SqliteConnection("Data Source=" + this.DbPath))
{
con.Open();
string query = "INSERT INTO {0} ({1}) VALUES ({2});";
List<string> _cols = new List<string>();
List<string> _vals = new List<string>();
using (SqliteCommand cmd = con.CreateCommand())
{
using (SqliteTransaction transaction = con.BeginTransaction())
{
for (int k = 0; k < Table.Rows.Count; k++)
{
cmd.Parameters.Clear();
if (k >= 1000)
break;
for (int i = 0; i < Table.Rows[k].Count; i++)
{
EbDataColumn column = Table.Columns.Find(item => item.ColumnIndex == i);
if (k == 0)
{
_cols.Add(column.ColumnName);
_vals.Add("@" + column.ColumnName);
}
cmd.Parameters.Add(new SqliteParameter { ParameterName = "@" + column.ColumnName, Value = Table.Rows[k][i] });
}
cmd.CommandText = string.Format(query, Table.TableName, string.Join(",", _cols.ToArray()), string.Join(",", _vals.ToArray()));
int rowAffected = cmd.ExecuteNonQuery();
}
transaction.Commit();
}
}
con.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public object DoScalar(string query, params DbParameter[] parameters)
{
try
{
using (SqliteConnection con = new SqliteConnection("Data Source=" + this.DbPath))
{
con.Open();
using (SqliteCommand cmd = con.CreateCommand())
{
cmd.CommandText = query;
if (parameters != null && parameters.Length > 0)
cmd.Parameters.AddRange(this.DbParamToSqlParam(parameters));
return cmd.ExecuteScalar();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return null;
}
protected void PrepareDataTable(SqliteDataReader reader, EbDataTable dt)
{
int _fieldCount = reader.FieldCount;
for (int i = 0; i < _fieldCount; i++)
{
dt.Columns.Add(new EbDataColumn
{
ColumnName = reader.GetName(i),
ColumnIndex = i,
Type = DbTypeConverter.ConvertToDbType(reader.GetFieldType(i))
});
}
while (reader.Read())
{
EbDataRow dr = dt.NewDataRow();
object[] oArray = new object[_fieldCount];
reader.GetValues(oArray);
dr.AddRange(oArray);
dt.Rows.Add(dr);
}
}
private SqliteParameter[] DbParamToSqlParam(params DbParameter[] parameters)
{
List<SqliteParameter> SqlP = new List<SqliteParameter>();
foreach (DbParameter param in parameters)
{
SqlP.Add(new SqliteParameter
{
ParameterName = param.ParameterName,
DbType = (DbType)param.DbType,
Value = param.Value
});
}
return SqlP.ToArray();
}
}
}