Mercurial > pub > bltoolkit
comparison Source/Data/Linq/DataContext.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f990fcb411a9 |
---|---|
1 using System; | |
2 using System.Data; | |
3 using System.Text; | |
4 | |
5 namespace BLToolkit.Data.Linq | |
6 { | |
7 using Data.Sql.SqlProvider; | |
8 using DataProvider; | |
9 using Mapping; | |
10 | |
11 public class DataContext : IDataContext | |
12 { | |
13 public DataContext() : this(DbManager.DefaultConfiguration) | |
14 { | |
15 } | |
16 | |
17 public DataContext(string configurationString) | |
18 { | |
19 ConfigurationString = configurationString; | |
20 DataProvider = DbManager.GetDataProvider(configurationString); | |
21 ContextID = DataProvider.Name; | |
22 | |
23 MappingSchema = DataProvider.MappingSchema ?? Map.DefaultSchema; | |
24 } | |
25 | |
26 public string ConfigurationString { get; private set; } | |
27 public DataProviderBase DataProvider { get; private set; } | |
28 public string ContextID { get; set; } | |
29 public MappingSchema MappingSchema { get; set; } | |
30 public string LastQuery { get; set; } | |
31 | |
32 private bool _keepConnectionAlive; | |
33 public bool KeepConnectionAlive | |
34 { | |
35 get { return _keepConnectionAlive; } | |
36 set | |
37 { | |
38 _keepConnectionAlive = value; | |
39 | |
40 if (value == false) | |
41 ReleaseQuery(); | |
42 } | |
43 } | |
44 | |
45 private bool? _isMarsEnabled; | |
46 public bool IsMarsEnabled | |
47 { | |
48 get | |
49 { | |
50 if (_isMarsEnabled == null) | |
51 { | |
52 if (_dbManager == null) | |
53 return false; | |
54 _isMarsEnabled = _dbManager.IsMarsEnabled; | |
55 } | |
56 | |
57 return _isMarsEnabled.Value; | |
58 } | |
59 set { _isMarsEnabled = value; } | |
60 } | |
61 | |
62 internal int LockDbManagerCounter; | |
63 | |
64 string _connectionString; | |
65 DbManager _dbManager; | |
66 | |
67 internal DbManager GetDBManager() | |
68 { | |
69 if (_dbManager == null) | |
70 { | |
71 if (_connectionString == null) | |
72 _connectionString = DbManager.GetConnectionString(ConfigurationString); | |
73 | |
74 _dbManager = new DbManager(DataProvider, _connectionString) { MappingSchema = MappingSchema }; | |
75 } | |
76 | |
77 return _dbManager; | |
78 } | |
79 | |
80 internal void ReleaseQuery() | |
81 { | |
82 LastQuery = _dbManager.LastQuery; | |
83 | |
84 if (_dbManager != null && LockDbManagerCounter == 0 && KeepConnectionAlive == false) | |
85 { | |
86 _dbManager.Dispose(); | |
87 _dbManager = null; | |
88 } | |
89 } | |
90 | |
91 Func<ISqlProvider> IDataContext.CreateSqlProvider | |
92 { | |
93 get { return DataProvider.CreateSqlProvider; } | |
94 } | |
95 | |
96 object IDataContext.SetQuery(IQueryContext queryContext) | |
97 { | |
98 var ctx = GetDBManager() as IDataContext; | |
99 return ctx.SetQuery(queryContext); | |
100 } | |
101 | |
102 int IDataContext.ExecuteNonQuery(object query) | |
103 { | |
104 var ctx = GetDBManager() as IDataContext; | |
105 return ctx.ExecuteNonQuery(query); | |
106 } | |
107 | |
108 object IDataContext.ExecuteScalar(object query) | |
109 { | |
110 var ctx = GetDBManager() as IDataContext; | |
111 return ctx.ExecuteScalar(query); | |
112 } | |
113 | |
114 IDataReader IDataContext.ExecuteReader(object query) | |
115 { | |
116 var ctx = GetDBManager() as IDataContext; | |
117 return ctx.ExecuteReader(query); | |
118 } | |
119 | |
120 void IDataContext.ReleaseQuery(object query) | |
121 { | |
122 ReleaseQuery(); | |
123 } | |
124 | |
125 string IDataContext.GetSqlText(object query) | |
126 { | |
127 if (_dbManager != null) | |
128 return ((IDataContext)_dbManager).GetSqlText(query); | |
129 | |
130 var ctx = GetDBManager() as IDataContext; | |
131 var str = ctx.GetSqlText(query); | |
132 | |
133 ReleaseQuery(); | |
134 | |
135 return str; | |
136 } | |
137 | |
138 DataContext(int n) {} | |
139 | |
140 IDataContext IDataContext.Clone(bool forNestedQuery) | |
141 { | |
142 var dc = new DataContext(0) | |
143 { | |
144 ConfigurationString = ConfigurationString, | |
145 KeepConnectionAlive = KeepConnectionAlive, | |
146 DataProvider = DataProvider, | |
147 ContextID = ContextID, | |
148 MappingSchema = MappingSchema, | |
149 }; | |
150 | |
151 if (forNestedQuery && _dbManager != null && _dbManager.IsMarsEnabled) | |
152 dc._dbManager = _dbManager.Transaction != null ? | |
153 new DbManager(DataProvider, _dbManager.Transaction) { MappingSchema = MappingSchema } : | |
154 new DbManager(DataProvider, _dbManager.Connection) { MappingSchema = MappingSchema }; | |
155 | |
156 return dc; | |
157 } | |
158 | |
159 public event EventHandler OnClosing; | |
160 | |
161 void IDisposable.Dispose() | |
162 { | |
163 if (_dbManager != null) | |
164 { | |
165 if (OnClosing != null) | |
166 OnClosing(this, EventArgs.Empty); | |
167 | |
168 _dbManager.Dispose(); | |
169 _dbManager = null; | |
170 } | |
171 } | |
172 } | |
173 } |