Skip to content

Commit b921384

Browse files
committed
Move source to src directory and clean up a bit
1 parent 6507fc6 commit b921384

12 files changed

Lines changed: 162 additions & 65 deletions

File tree

Oxide.SQLite.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
33
VisualStudioVersion = 15.0
44
MinimumVisualStudioVersion = 15.0
5-
Project("{A368D19F-85B0-4AB4-9782-3FDEA33D33D1}") = "Oxide.SQLite", "Oxide.SQLite\Oxide.SQLite.csproj", "{C54EA613-C5FD-4ACD-8AC4-79063B08CF5A}"
5+
Project("{A368D19F-85B0-4AB4-9782-3FDEA33D33D1}") = "Oxide.SQLite", "src\Oxide.SQLite.csproj", "{C54EA613-C5FD-4ACD-8AC4-79063B08CF5A}"
66
EndProject
77
Global
88
GlobalSection(SolutionConfigurationPlatforms) = preSolution

NuGet.config renamed to nuget.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
</activePackageSource>
1313
<packageSources>
1414
<add key="Oxide" value="https://www.myget.org/f/oxide" />
15+
<add key="Local" value="%USERPROFILE%\.nuget\packages" />
1516
</packageSources>
1617
</configuration>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
using System.Data.SQLite;
1+
using Oxide.Core.Plugins;
2+
using System.Data.SQLite;
23
#if NET35
34
using System.Security.Permissions;
45
#endif
5-
using Oxide.Core.Plugins;
66

77
namespace Oxide.Ext.SQLite
88
{
99
#if NET35
1010
[ReflectionPermission(SecurityAction.Deny, Flags = ReflectionPermissionFlag.AllFlags)]
1111
#endif
12+
1213
public sealed class Connection
1314
{
1415
internal string ConnectionString { get; set; }
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 97 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
44
<PropertyGroup>
5-
<Version>2.0.0.0</Version>
5+
<Version>2.0.0</Version>
66
<AssemblyName>Oxide.SQLite</AssemblyName>
77
<Authors>Oxide and Contributors</Authors>
88
<Description>SQLite database support for the Oxide modding framework</Description>
@@ -20,8 +20,10 @@
2020
-->
2121
<FrameworkPathOverride Condition="'$(TargetFramework)' == 'net35' And '$(OS)' == 'Windows_NT'">C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client</FrameworkPathOverride>
2222
<FrameworkPathOverride Condition="'$(TargetFramework)' == 'net35' And '$(OS)' == 'OSX'">/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/2.0-api</FrameworkPathOverride>
23+
<ThisAssemblyNamespace>Oxide.Core.SQLite</ThisAssemblyNamespace>
2324
</PropertyGroup>
2425
<ItemGroup>
26+
<PackageReference Include="GitInfo" Version="2.0.*" />
2527
<PackageReference Include="Oxide.Core" Version="2.0.*" />
2628
<Reference Include="System.Data.SQLite">
2729
<HintPath>Dependencies\System.Data.SQLite.dll</HintPath>
@@ -34,7 +36,7 @@
3436
<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
3537
<ItemGroup>
3638
<ReferencePath Condition="'%(FileName)' == 'Oxide.References'">
37-
<Aliases>Oxide</Aliases>
39+
<Aliases>References</Aliases>
3840
</ReferencePath>
3941
</ItemGroup>
4042
</Target>

0 commit comments

Comments
 (0)