Mercurial > hg > ucis.core
annotate Database.cs @ 9:9533a87363f3
Fixed deadlock in QueuedPacketStream
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Fri, 18 Jan 2013 16:29:50 +0100 |
parents | 4b78cc5f116b |
children | 40715b34d0d3 |
rev | line source |
---|---|
0 | 1 ???using System; |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
2 using System.Collections.Generic; |
0 | 3 using System.Data; |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
4 using System.Reflection; |
0 | 5 |
6 namespace UCIS { | |
7 public class Database { | |
8 private delegate object ConstructorDelegate(); | |
9 private ConstructorInfo _ConnectionConstructor; | |
10 | |
11 public Database(Type DBConnectionType, string connectionString) { | |
12 this.ConnectionString = connectionString; | |
13 _ConnectionConstructor = DBConnectionType.GetConstructor(new Type[] { }); | |
14 } | |
15 | |
16 public string ConnectionString { get; set; } | |
17 | |
18 public virtual IDbConnection GetConnection() { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
19 IDbConnection conn = (IDbConnection)_ConnectionConstructor.Invoke(null); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
20 conn.ConnectionString = ConnectionString; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
21 conn.Open(); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
22 return conn; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
23 } |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
24 |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
25 private IDbCommand PrepareQuery(IDbConnection Connection, string Query, params object[] Parameters) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
26 IDbCommand Command = Connection.CreateCommand(); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
27 Command.CommandType = CommandType.Text; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
28 Command.CommandText = Query; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
29 Command.Parameters.Clear(); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
30 int ParameterI = 0; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
31 foreach (object Parameter in Parameters) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
32 IDbDataParameter DBParameter = Command.CreateParameter(); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
33 DBParameter.Direction = ParameterDirection.Input; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
34 DBParameter.ParameterName = "?" + ParameterI.ToString(); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
35 DBParameter.Value = Parameter; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
36 Command.Parameters.Add(DBParameter); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
37 ParameterI++; |
0 | 38 } |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
39 if (ParameterI > 0) Command.Prepare(); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
40 return Command; |
0 | 41 } |
42 | |
43 public IDbCommand PrepareQuery(string Query, params object[] Parameters) { | |
44 IDbConnection Connection = GetConnection(); | |
45 try { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
46 return PrepareQuery(Connection, Query, Parameters); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
47 } catch (Exception) { |
0 | 48 Connection.Close(); |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
49 throw; |
0 | 50 } |
51 } | |
52 | |
53 public int NonQuery(string QueryString, params object[] Parameters) { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
54 using (IDbConnection connection = GetConnection()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
55 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
56 return command.ExecuteNonQuery(); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
57 } |
0 | 58 } |
59 } | |
60 | |
61 public object FetchField(string QueryString, params object[] Parameters) { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
62 using (IDbConnection connection = GetConnection()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
63 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
64 return command.ExecuteScalar(); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
65 } |
0 | 66 } |
67 } | |
68 public object[] FetchRow(string QueryString, params object[] Parameters) { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
69 using (IDbConnection connection = GetConnection()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
70 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
71 using (IDataReader Reader = command.ExecuteReader()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
72 if (!Reader.Read()) return null; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
73 object[] Result = new object[Reader.FieldCount]; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
74 Reader.GetValues(Result); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
75 return Result; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
76 } |
0 | 77 } |
78 } | |
79 } | |
80 public object[][] FetchRows(string QueryString, params object[] Parameters) { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
81 using (IDbConnection connection = GetConnection()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
82 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
83 using (IDataReader Reader = command.ExecuteReader()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
84 List<object[]> Result = new List<object[]>(); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
85 while (Reader.Read()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
86 object[] ResultArray = new object[Reader.FieldCount]; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
87 Reader.GetValues(ResultArray); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
88 Result.Add(ResultArray); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
89 } |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
90 return Result.ToArray(); |
0 | 91 } |
92 } | |
93 } | |
94 } | |
95 public void ForEachRow(Action<Object[]> f, string QueryString, params object[] Parameters) { | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
96 using (IDbConnection connection = GetConnection()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
97 using (IDbCommand command = PrepareQuery(connection, QueryString, Parameters)) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
98 using (IDataReader Reader = command.ExecuteReader()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
99 while (Reader.Read()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
100 object[] ResultArray = new object[Reader.FieldCount]; |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
101 Reader.GetValues(ResultArray); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
102 f(ResultArray); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
103 } |
0 | 104 } |
105 } | |
106 } | |
7
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
107 } |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
108 public void ForEachRow(Action<IDataRecord> callback, string query, params object[] parameters) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
109 using (IDbConnection connection = GetConnection()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
110 using (IDbCommand command = PrepareQuery(connection, query, parameters)) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
111 using (IDataReader Reader = command.ExecuteReader()) { |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
112 while (Reader.Read()) callback(Reader); |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
113 } |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
114 } |
4b78cc5f116b
Fixes and improvements (some untested)
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
115 } |
0 | 116 } |
117 | |
118 /*public DBReader GetReader(string QueryString, params object[] Parameters) { | |
119 return new DBReader(PrepareQuery(QueryString, Parameters)); | |
120 }*/ | |
121 } | |
122 } |