@@ -72,28 +72,37 @@ private void Cleanup()
7272 public void Handle ( )
7373 {
7474 List < Dictionary < string , object > > list = null ;
75- var nonQueryResult = 0 ;
76- var lastInsertRowId = 0L ;
75+ int nonQueryResult = 0 ;
76+ long lastInsertRowId = 0L ;
7777 try
7878 {
79- if ( Connection == null ) throw new Exception ( "Connection is null" ) ;
79+ if ( Connection == null )
80+ {
81+ throw new Exception ( "Connection is null" ) ;
82+ }
83+
8084 _connection = ( SQLiteConnection ) Connection . Con ;
8185 if ( _connection . State == ConnectionState . Closed )
86+ {
8287 _connection . Open ( ) ;
88+ }
89+
8390 _cmd = _connection . CreateCommand ( ) ;
8491 _cmd . CommandText = Sql . SQL ;
8592 Sql . AddParams ( _cmd , Sql . Arguments , "@" ) ;
8693 if ( NonQuery )
94+ {
8795 nonQueryResult = _cmd . ExecuteNonQuery ( ) ;
96+ }
8897 else
8998 {
90- using ( var reader = _cmd . ExecuteReader ( ) )
99+ using ( SQLiteDataReader reader = _cmd . ExecuteReader ( ) )
91100 {
92101 list = new List < Dictionary < string , object > > ( ) ;
93102 while ( reader . Read ( ) )
94103 {
95- var dict = new Dictionary < string , object > ( ) ;
96- for ( var i = 0 ; i < reader . FieldCount ; i ++ )
104+ Dictionary < string , object > dict = new Dictionary < string , object > ( ) ;
105+ for ( int i = 0 ; i < reader . FieldCount ; i ++ )
97106 {
98107 dict . Add ( reader . GetName ( i ) , reader . GetValue ( i ) ) ;
99108 }
@@ -106,8 +115,12 @@ public void Handle()
106115 }
107116 catch ( Exception ex )
108117 {
109- var message = "Sqlite handle raised an exception" ;
110- if ( Connection ? . Plugin != null ) message += $ " in '{ Connection . Plugin . Name } v{ Connection . Plugin . Version } ' plugin";
118+ string message = "Sqlite handle raised an exception" ;
119+ if ( Connection ? . Plugin != null )
120+ {
121+ message += $ " in '{ Connection . Plugin . Name } v{ Connection . Plugin . Version } ' plugin";
122+ }
123+
111124 Interface . Oxide . LogException ( message , ex ) ;
112125 Cleanup ( ) ;
113126 }
@@ -116,16 +129,28 @@ public void Handle()
116129 Connection ? . Plugin ? . TrackStart ( ) ;
117130 try
118131 {
119- if ( Connection != null ) Connection . LastInsertRowId = lastInsertRowId ;
132+ if ( Connection != null )
133+ {
134+ Connection . LastInsertRowId = lastInsertRowId ;
135+ }
136+
120137 if ( ! NonQuery )
138+ {
121139 Callback ( list ) ;
140+ }
122141 else
142+ {
123143 CallbackNonQuery ? . Invoke ( nonQueryResult ) ;
144+ }
124145 }
125146 catch ( Exception ex )
126147 {
127- var message = "Sqlite command callback raised an exception" ;
128- if ( Connection ? . Plugin != null ) message += $ " in '{ Connection . Plugin . Name } v{ Connection . Plugin . Version } ' plugin";
148+ string message = "Sqlite command callback raised an exception" ;
149+ if ( Connection ? . Plugin != null )
150+ {
151+ message += $ " in '{ Connection . Plugin . Name } v{ Connection . Plugin . Version } ' plugin";
152+ }
153+
129154 Interface . Oxide . LogException ( message , ex ) ;
130155 }
131156 Connection ? . Plugin ? . TrackEnd ( ) ;
@@ -144,21 +169,34 @@ private void Worker()
144169 lock ( _syncroot )
145170 {
146171 if ( _queue . Count > 0 )
172+ {
147173 query = _queue . Dequeue ( ) ;
174+ }
148175 else
149176 {
150- foreach ( var connection in _runningConnections )
151- if ( connection != null && ! connection . ConnectionPersistent ) CloseDb ( connection ) ;
177+ foreach ( Connection connection in _runningConnections )
178+ {
179+ if ( connection != null && ! connection . ConnectionPersistent )
180+ {
181+ CloseDb ( connection ) ;
182+ }
183+ }
184+
152185 _runningConnections . Clear ( ) ;
153186 }
154187 }
155188 if ( query != null )
156189 {
157190 query . Handle ( ) ;
158- if ( query . Connection != null ) _runningConnections . Add ( query . Connection ) ;
191+ if ( query . Connection != null )
192+ {
193+ _runningConnections . Add ( query . Connection ) ;
194+ }
159195 }
160196 else if ( _running )
197+ {
161198 _workevent . WaitOne ( ) ;
199+ }
162200 }
163201 }
164202
@@ -173,11 +211,18 @@ public SQLite()
173211 [ LibraryFunction ( "OpenDb" ) ]
174212 public Connection OpenDb ( string file , Plugin plugin , bool persistent = false )
175213 {
176- if ( string . IsNullOrEmpty ( file ) ) return null ;
177- var filename = Path . Combine ( _dataDirectory , file ) ;
214+ if ( string . IsNullOrEmpty ( file ) )
215+ {
216+ return null ;
217+ }
218+
219+ string filename = Path . Combine ( _dataDirectory , file ) ;
178220 if ( ! filename . StartsWith ( _dataDirectory , StringComparison . Ordinal ) )
221+ {
179222 throw new Exception ( "Only access to oxide directory!" ) ;
180- var conStr = $ "Data Source={ filename } ;Version=3;";
223+ }
224+
225+ string conStr = $ "Data Source={ filename } ;Version=3;";
181226 Connection connection ;
182227 if ( _connections . TryGetValue ( conStr , out connection ) )
183228 {
@@ -186,6 +231,7 @@ public Connection OpenDb(string file, Plugin plugin, bool persistent = false)
186231 Interface . Oxide . LogWarning ( "Already open connection ({0}), by plugin '{1}'..." , conStr , connection . Plugin ) ;
187232 return null ;
188233 }
234+
189235 Interface . Oxide . LogWarning ( "Already open connection ({0}), using existing instead..." , conStr ) ;
190236 }
191237 else
@@ -198,24 +244,37 @@ public Connection OpenDb(string file, Plugin plugin, bool persistent = false)
198244 _connections [ conStr ] = connection ;
199245 }
200246 if ( plugin != null && ! _pluginRemovedFromManager . ContainsKey ( plugin ) )
247+ {
201248 _pluginRemovedFromManager [ plugin ] = plugin . OnRemovedFromManager . Add ( OnRemovedFromManager ) ;
249+ }
250+
202251 return connection ;
203252 }
204253
205254 private void OnRemovedFromManager ( Plugin sender , PluginManager manager )
206255 {
207- var toRemove = new List < string > ( ) ;
208- foreach ( var connection in _connections )
256+ List < string > toRemove = new List < string > ( ) ;
257+ foreach ( KeyValuePair < string , Connection > connection in _connections )
209258 {
210- if ( connection . Value . Plugin != sender ) continue ;
259+ if ( connection . Value . Plugin != sender )
260+ {
261+ continue ;
262+ }
263+
211264 if ( connection . Value . Con ? . State != ConnectionState . Closed )
265+ {
212266 Interface . Oxide . LogWarning ( "Unclosed sqlite connection ({0}), by plugin '{1}', closing..." , connection . Value . ConnectionString , connection . Value . Plugin ? . Name ?? "null" ) ;
267+ }
268+
213269 connection . Value . Con ? . Close ( ) ;
214270 connection . Value . Plugin = null ;
215271 toRemove . Add ( connection . Key ) ;
216272 }
217- foreach ( var conStr in toRemove )
273+ foreach ( string conStr in toRemove )
274+ {
218275 _connections . Remove ( conStr ) ;
276+ }
277+
219278 Event . Callback < Plugin , PluginManager > event_callback ;
220279 if ( _pluginRemovedFromManager . TryGetValue ( sender , out event_callback ) )
221280 {
@@ -227,7 +286,11 @@ private void OnRemovedFromManager(Plugin sender, PluginManager manager)
227286 [ LibraryFunction ( "CloseDb" ) ]
228287 public void CloseDb ( Connection db )
229288 {
230- if ( db == null ) return ;
289+ if ( db == null )
290+ {
291+ return ;
292+ }
293+
231294 _connections . Remove ( db . ConnectionString ) ;
232295 if ( db . Plugin != null && _connections . Values . All ( c => c . Plugin != db . Plugin ) )
233296 {
@@ -251,27 +314,35 @@ public Sql NewSql()
251314 [ LibraryFunction ( "Query" ) ]
252315 public void Query ( Sql sql , Connection db , Action < List < Dictionary < string , object > > > callback )
253316 {
254- var query = new SQLiteQuery
317+ SQLiteQuery query = new SQLiteQuery
255318 {
256319 Sql = sql ,
257320 Connection = db ,
258321 Callback = callback
259322 } ;
260- lock ( _syncroot ) _queue . Enqueue ( query ) ;
323+ lock ( _syncroot )
324+ {
325+ _queue . Enqueue ( query ) ;
326+ }
327+
261328 _workevent . Set ( ) ;
262329 }
263330
264331 [ LibraryFunction ( "ExecuteNonQuery" ) ]
265332 public void ExecuteNonQuery ( Sql sql , Connection db , Action < int > callback = null )
266333 {
267- var query = new SQLiteQuery
334+ SQLiteQuery query = new SQLiteQuery
268335 {
269336 Sql = sql ,
270337 Connection = db ,
271338 CallbackNonQuery = callback ,
272339 NonQuery = true
273340 } ;
274- lock ( _syncroot ) _queue . Enqueue ( query ) ;
341+ lock ( _syncroot )
342+ {
343+ _queue . Enqueue ( query ) ;
344+ }
345+
275346 _workevent . Set ( ) ;
276347 }
277348
0 commit comments