-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathStarSystemSqLiteRepository.cs
More file actions
813 lines (743 loc) · 36.3 KB
/
StarSystemSqLiteRepository.cs
File metadata and controls
813 lines (743 loc) · 36.3 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
using EddiDataDefinitions;
using JetBrains.Annotations;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Data.Common;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Utilities;
namespace EddiDataProviderService
{
public class StarSystemSqLiteRepository : SqLiteBaseRepository
{
private const string TABLE_GET_SCHEMA_VERSION_SQL = @"PRAGMA user_version;";
private const string TABLE_SET_SCHEMA_VERSION_SQL = @"PRAGMA user_version = ";
private long SCHEMA_VERSION { get; set; }
// Append new table columns to the end of the list to maximize compatibility with schema version 0.
private const string CREATE_TABLE_SQL = @"
CREATE TABLE IF NOT EXISTS starsystems
(
systemaddress INT UNIQUE,
name TEXT NOT NULL COLLATE NOCASE,
x DECIMAL(12,5),
y DECIMAL(12,5),
z DECIMAL(12,5),
starsystem TEXT NOT NULL,
starsystemlastupdated DATETIME NOT NULL,
population INT,
totalvisits INT NOT NULL,
lastvisit DATETIME,
comment TEXT,
CONSTRAINT combined_uniques UNIQUE (name, systemaddress)
);";
private const string CREATE_INDEX_SQL = @"
CREATE INDEX IF NOT EXISTS
starsystems_idx_1 ON starsystems(name COLLATE NOCASE);
CREATE UNIQUE INDEX IF NOT EXISTS
starsystems_idx_2 ON starsystems(systemaddress) WHERE systemaddress IS NOT NULL;
";
private const string TABLE_INFO_SQL = @"PRAGMA table_info(starsystems)";
private const string REPLACE_TABLE_SQL = @"
PRAGMA foreign_keys=off;
BEGIN TRANSACTION;
DROP TABLE IF EXISTS old_starsystems;
ALTER TABLE starsystems RENAME TO old_starsystems;"
+ CREATE_TABLE_SQL + INSERT_SQL + @"
SELECT DISTINCT
systemaddress,
name,
x,
y,
z,
starsystem,
starsystemlastupdated,
population,
totalvisits,
lastvisit,
comment
FROM old_starsystems;
DROP TABLE old_starsystems;
COMMIT;
PRAGMA foreign_keys=on;
VACUUM;
PRAGMA optimize;";
private const string INSERT_SQL = @"
INSERT INTO starsystems
(
systemaddress,
name,
x,
y,
z,
starsystem,
starsystemlastupdated,
population,
totalvisits,
lastvisit,
comment
)";
private const string UPDATE_SQL = @"
UPDATE starsystems
SET
systemaddress = @systemaddress,
name = @name,
x = @x,
y = @y,
z = @z,
starsystem = @starsystem,
starsystemlastupdated = @starsystemlastupdated,
population = @population,
totalvisits = @totalvisits,
lastvisit = @lastvisit,
comment = @comment
";
private const string DELETE_SQL = @"DELETE FROM starsystems ";
private const string SELECT_SQL = @"SELECT * FROM starsystems ";
private const string SELECT_NAME_SQL = @"SELECT name FROM starsystems ";
private const string VALUES_SQL = @"
VALUES
(
@systemaddress,
@name,
@x,
@y,
@z,
@starsystem,
@starsystemlastupdated,
@population,
@totalvisits,
@lastvisit,
@comment
)";
private const string WHERE_SYSTEMADDRESS = @"WHERE systemaddress = @systemaddress;";
private const string WHERE_NAME = @"WHERE name = @name;";
private const string WHERE_NAME_STARTSWITH = @"WHERE name LIKE @name;";
private StarSystemSqLiteRepository ( bool unitTesting = false )
{
SqLiteBaseRepository.unitTesting = unitTesting;
}
public static StarSystemSqLiteRepository Create ( bool isUnitTesting = false )
{
try
{
var repository = new StarSystemSqLiteRepository(isUnitTesting);
repository.CreateOrUpdateDatabase();
return repository;
}
catch (DllNotFoundException dllEx)
{
Logging.Error( "SQLite native library not found. The application will continue without database access for star systems.", dllEx );
// Return a repository instance that will handle database operations gracefully
// This allows the application to continue functioning with degraded functionality
var repository = new StarSystemSqLiteRepository(isUnitTesting);
return repository;
}
}
public async Task<DatabaseStarSystem> GetSqlStarSystemAsync ( ulong systemAddress, CancellationToken cancellationToken )
{
if ( systemAddress <= 0 ) { return null; }
return ( await GetSqlStarSystemsAsync( [ systemAddress ], cancellationToken ).ConfigureAwait(false) )?.FirstOrDefault();
}
public async Task<List<DatabaseStarSystem>> GetSqlStarSystemsAsync ( ulong[] systemAddresses, CancellationToken cancellationToken )
{
var results = new List<DatabaseStarSystem>();
if ( !File.Exists( DbFile ) || systemAddresses.Length == 0 ) { return results; }
results = await ReadStarSystemsAsync( systemAddresses, cancellationToken ).ConfigureAwait(false);
FixLegacyDbStarSystemData(results);
return results;
}
public async Task<List<DatabaseStarSystem>> GetSqlStarSystemsAsync ( string[] systemNames, CancellationToken cancellationToken )
{
var results = new List<DatabaseStarSystem>();
if ( !File.Exists( DbFile ) || systemNames.Length == 0 ) { return results; }
results = await ReadStarSystemsAsync( systemNames, cancellationToken ).ConfigureAwait(false);
FixLegacyDbStarSystemData( results );
return results;
}
private static void FixLegacyDbStarSystemData ( List<DatabaseStarSystem> results )
{
foreach ( var dbStarSystem in results )
{
if ( !string.IsNullOrEmpty( dbStarSystem.systemJson ) )
{
// Old versions of the data could have a string "No volcanism" for volcanism. If so we remove it
dbStarSystem.systemJson = dbStarSystem.systemJson?.Replace( @"""No volcanism""", "null" );
// Old versions of the data could have a string "InterstellarFactorsContact" for the facilitator station service. If so we update it
dbStarSystem.systemJson =
dbStarSystem.systemJson?.Replace( @"""InterstellarFactorsContact""", @"""Facilitator""" );
}
}
}
[ NotNull, ItemNotNull ]
public async Task<List<string>> GetStarSystemNamesAsync ( string startingWith, CancellationToken cancellationToken )
{
if ( string.IsNullOrWhiteSpace( startingWith ) )
{
throw new ArgumentException( @"Input string cannot be null or empty.", nameof(startingWith) );
}
var results = new HashSet<string>();
try
{
await using ( var con = SimpleDbConnection() )
{
await con.OpenAsync( cancellationToken ).ConfigureAwait( false );
await using ( var transaction = con.BeginTransaction() )
{
await using ( var cmd = con.CreateCommand() )
{
cmd.Transaction = transaction;
cmd.CommandText = SELECT_NAME_SQL + WHERE_NAME_STARTSWITH;
cmd.Parameters.AddWithValue( "@name", $"{startingWith}%" );
await using ( var rdr = await cmd.ExecuteReaderAsync( cancellationToken ).ConfigureAwait( false ) )
{
while ( await rdr.ReadAsync( cancellationToken ).ConfigureAwait(false) )
{
var name = rdr["name"] as string;
if ( !string.IsNullOrEmpty( name ) )
{
results.Add( name );
}
}
}
}
transaction.Commit();
}
}
}
catch ( SQLiteException sqle )
{
Logging.Warn( $"An error occurred while fetching star system names starting with '{startingWith}': {sqle.Message}", sqle );
}
catch ( DllNotFoundException dllEx )
{
Logging.Warn( $"SQLite database unavailable: {dllEx.Message}", dllEx );
}
return results.ToList();
}
[ NotNull, ItemNotNull ]
private static async Task<List<DatabaseStarSystem>> ReadStarSystemsAsync ( ulong[] systemAddresses, CancellationToken cancellationToken )
{
var results = new List<DatabaseStarSystem>();
if ( systemAddresses.Length == 0 ) { return results; }
await using ( var con = SimpleDbConnection() )
{
try
{
await con.OpenAsync( cancellationToken ).ConfigureAwait( false );
await using ( var cmd = new SQLiteCommand( con ) )
{
await using ( var transaction = con.BeginTransaction() )
{
foreach ( var systemAddress in systemAddresses.Where( systemAddress => systemAddress > 0 ) )
{
try
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue( "@systemaddress", systemAddress );
cmd.CommandText = SELECT_SQL + WHERE_SYSTEMADDRESS;
var result = await ReadStarSystemEntryAsync( cmd, cancellationToken )
.ConfigureAwait( false );
if ( result != null )
{
results.Add( result );
}
}
catch ( InvalidOperationException ioe )
{
Logging.Warn(
$"Problem reading data for star system '{systemAddress}' from database.", ioe );
}
catch ( SQLiteException sqle )
{
Logging.Warn(
$"Problem reading data for star system '{systemAddress}' from database.",
sqle );
}
}
transaction.Commit();
}
}
}
catch ( TaskCanceledException )
{
// Task cancelled, nothing to do here.
}
catch ( DllNotFoundException dllEx )
{
Logging.Warn( $"SQLite database unavailable when reading star systems: {dllEx.Message}" );
}
}
return results.RemoveNulls().ToList();
}
[NotNull, ItemNotNull]
private static async Task<List<DatabaseStarSystem>> ReadStarSystemsAsync ( string[] systemNames, CancellationToken cancellationToken )
{
var results = new List<DatabaseStarSystem>();
if ( systemNames.Length == 0 ) { return results; }
try
{
await using ( var con = SimpleDbConnection() )
{
await con.OpenAsync( cancellationToken ).ConfigureAwait(false);
await using ( var cmd = new SQLiteCommand( con ) )
{
await using ( var transaction = con.BeginTransaction() )
{
foreach ( var systemName in systemNames.Where( sName => !string.IsNullOrEmpty(sName) ) )
{
try
{
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue( "@name", systemName );
cmd.CommandText = SELECT_SQL + WHERE_NAME;
var result = await ReadStarSystemEntryAsync( cmd, cancellationToken ).ConfigureAwait(false);
if ( result != null )
{
results.Add( result );
}
}
catch (InvalidOperationException ioe )
{
Logging.Warn( $"Problem reading data for star system '{systemName}' from database.", ioe );
}
catch ( SQLiteException sqle )
{
Logging.Warn( $"Problem reading data for star system '{systemName}' from database.", sqle );
}
}
transaction.Commit();
}
}
}
}
catch ( DllNotFoundException dllEx )
{
Logging.Warn( $"SQLite database unavailable when reading star systems by name: {dllEx.Message}" );
}
return results.RemoveNulls().ToList();
}
private static async Task<List<DatabaseStarSystem>> ReadStarSystemsAsync( IList<StarSystem> starSystems, CancellationToken cancellationToken )
{
var results = new List<DatabaseStarSystem>();
if ( starSystems is null || !starSystems.Any()) { return results; }
var systemAddresses = starSystems.Select( s => s.systemAddress ).Distinct().ToArray();
return await ReadStarSystemsAsync( systemAddresses, cancellationToken ).ConfigureAwait(false);
}
private static async Task<DatabaseStarSystem> ReadStarSystemEntryAsync ( SQLiteCommand cmd, CancellationToken cancellationToken )
{
ulong? systemAddress = null;
var systemName = string.Empty;
var starSystemJson = string.Empty;
var comment = string.Empty;
var lastUpdated = DateTime.MinValue;
DateTime? lastVisit = null;
var totalVisits = 0;
decimal? x = null;
decimal? y = null;
decimal? z = null;
long? population = 0;
var fieldMappings = new Dictionary<string, Action<DbDataReader, int>>
{
{ "systemaddress", (rdr, i) => systemAddress = rdr.IsDBNull(i)
? null
: (ulong?)rdr.GetInt64(i) },
{ "name", (rdr, i) => systemName = rdr.IsDBNull(i)
? string.Empty
: rdr.GetString(i) },
{ "x", (rdr, i) => x = rdr.IsDBNull(i)
? null
: (decimal?)rdr.GetDecimal(i) },
{ "y", (rdr, i) => y = rdr.IsDBNull(i)
? null
: (decimal?)rdr.GetDecimal(i) },
{ "z", (rdr, i) => z = rdr.IsDBNull(i)
? null
: (decimal?)rdr.GetDecimal(i) },
{ "starsystem", (rdr, i) => starSystemJson = rdr.IsDBNull(i)
? string.Empty
: rdr.GetString(i) },
{ "comment", (rdr, i) => comment = rdr.IsDBNull(i)
? string.Empty
: rdr.GetString(i) },
{ "starsystemlastupdated", (rdr, i) => lastUpdated = rdr.IsDBNull(i)
? DateTime.MinValue
: rdr.GetDateTime(i).ToUniversalTime() },
{ "lastvisit", (rdr, i) => lastVisit = rdr.IsDBNull(i)
? null
: (DateTime?)rdr.GetDateTime(i).ToUniversalTime() },
{ "totalvisits", (rdr, i) => totalVisits = rdr.IsDBNull(i)
? 0
: rdr.GetInt32(i) },
{ "population", (rdr, i) => population = rdr.IsDBNull(i)
? null
: (long?)rdr.GetInt64(i) }
};
await using ( var rdr = await cmd.ExecuteReaderAsync( cancellationToken ).ConfigureAwait( false ) )
{
if ( await rdr.ReadAsync( cancellationToken ).ConfigureAwait( false ) )
{
if ( rdr.HasRows )
{
for ( var i = 0; i < rdr.FieldCount; i++ )
{
if ( fieldMappings.TryGetValue( rdr.GetName( i ), out var mapAction ) )
{
mapAction( rdr, i );
}
}
return new DatabaseStarSystem( systemName, systemAddress ?? 0, starSystemJson )
{
x = x,
y = y,
z = z,
comment = comment,
lastUpdated = lastUpdated,
lastVisit = lastVisit,
totalVisits = totalVisits,
population = population
};
}
}
}
return null;
}
public async Task SaveStarSystemAsync( StarSystem starSystem, CancellationToken cancellationToken )
{
if (starSystem == null) { return; }
await SaveStarSystemsAsync( new List<StarSystem> { starSystem }, cancellationToken ).ConfigureAwait(false);
}
public async Task SaveStarSystemsAsync( IList<StarSystem> starSystems, CancellationToken cancellationToken )
{
// Determine whether we need to delete, insert, or update each system
var delete = new List<StarSystem>();
var update = new List<StarSystem>();
var insert = new List<StarSystem>();
var dbSystems = await ReadStarSystemsAsync( starSystems, cancellationToken ).ConfigureAwait(false);
// Determine whether to insert + delete or update the SQL record.
// Skip records with a zero value for the systemAddress
foreach (var system in starSystems)
{
if ( system.systemAddress == 0 )
{
Logging.Warn($"{system.systemname} has an invalid system address ({system.systemAddress}) and can't be recorded in EDDI's star system database.");
continue;
}
var dbSystem = dbSystems.FirstOrDefault(s =>
s.systemAddress == system.systemAddress ||
s.systemName == system.systemname);
if (dbSystem?.systemJson is null)
{
// Use our delete method to purge all obsolete copies of the star system from the database,
// then re-add the star system.
delete.Add(system);
insert.Add(system);
}
else
{
update.Add(system);
}
}
// Delete applicable systems
await deleteStarSystemsAsync(delete.ToImmutableList()).ConfigureAwait(false);
// Insert applicable systems
await insertStarSystemsAsync(insert.ToImmutableList() ).ConfigureAwait(false);
// Update applicable systems
await updateStarSystemsAsync(update.ToImmutableList() ).ConfigureAwait(false);
}
private static async Task insertStarSystemsAsync(ImmutableList<StarSystem> systems)
{
if ( systems.Count == 0)
{
return;
}
await using ( var con = SimpleDbConnection() )
{
await con.OpenAsync().ConfigureAwait( false );
await using ( var cmd = new SQLiteCommand( con ) )
{
await using ( var transaction = con.BeginTransaction() )
{
try
{
foreach ( var system in systems )
{
cmd.Parameters.Clear();
cmd.CommandText = INSERT_SQL + VALUES_SQL;
cmd.Parameters.AddWithValue( "@name", system.systemname );
cmd.Parameters.AddWithValue( "@systemaddress", system.systemAddress );
cmd.Parameters.AddWithValue( "@x", system.x );
cmd.Parameters.AddWithValue( "@y", system.y );
cmd.Parameters.AddWithValue( "@z", system.z );
cmd.Parameters.AddWithValue( "@totalvisits", system.visits );
cmd.Parameters.AddWithValue( "@lastvisit", system.lastvisit ?? DateTime.UtcNow );
cmd.Parameters.AddWithValue( "@starsystem", JsonConvert.SerializeObject( system ) );
cmd.Parameters.AddWithValue( "@starsystemlastupdated", system.lastupdated );
cmd.Parameters.AddWithValue( "@population", system.population );
cmd.Parameters.AddWithValue( "@comment", system.comment );
Logging.Debug( "Inserting new starsystem " + system.systemAddress, system );
await cmd.ExecuteNonQueryAsync().ConfigureAwait( false );
}
transaction.Commit();
}
catch ( SQLiteException e )
{
LogAndRollbackSqlLiteException( transaction, e );
}
catch ( DllNotFoundException dllEx )
{
Logging.Warn( $"SQLite database unavailable. Unable to insert star systems: {dllEx.Message}" );
}
}
}
}
}
private static async Task updateStarSystemsAsync ( ImmutableList<StarSystem> systems )
{
if ( systems.Count == 0 )
{
return;
}
await using ( var con = SimpleDbConnection() )
{
await con.OpenAsync().ConfigureAwait( false );
await using ( var cmd = new SQLiteCommand( con ) )
{
await using ( var transaction = con.BeginTransaction() )
{
try
{
foreach ( var system in systems )
{
var serializedSystem = JsonConvert.SerializeObject( system );
if ( system.systemAddress != 0 )
{
cmd.CommandText = UPDATE_SQL + WHERE_SYSTEMADDRESS;
}
else
{
cmd.CommandText = UPDATE_SQL + WHERE_NAME;
}
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue( "@systemaddress", system.systemAddress );
cmd.Parameters.AddWithValue( "@name", system.systemname );
cmd.Parameters.AddWithValue( "@x", system.x );
cmd.Parameters.AddWithValue( "@y", system.y );
cmd.Parameters.AddWithValue( "@z", system.z );
cmd.Parameters.AddWithValue( "@starsystem", serializedSystem );
cmd.Parameters.AddWithValue( "@starsystemlastupdated", system.lastupdated );
cmd.Parameters.AddWithValue( "@population", system.population );
cmd.Parameters.AddWithValue( "@totalvisits", system.visits );
cmd.Parameters.AddWithValue( "@lastvisit", system.lastvisit ?? DateTime.UtcNow );
cmd.Parameters.AddWithValue( "@comment", system.comment );
Logging.Debug( "Updating starsystem " + system.systemAddress, system );
await cmd.ExecuteNonQueryAsync().ConfigureAwait( false );
}
transaction.Commit();
}
catch ( SQLiteException ex )
{
LogAndRollbackSqlLiteException( transaction, ex );
}
catch ( DllNotFoundException dllEx )
{
Logging.Warn( $"SQLite database unavailable. Unable to update star systems: {dllEx.Message}" );
}
}
}
}
}
private static async Task deleteStarSystemsAsync ( ImmutableList<StarSystem> systems )
{
if ( systems.Count == 0 )
{
return;
}
await using ( var con = SimpleDbConnection() )
{
await con.OpenAsync().ConfigureAwait( false );
await using ( var cmd = new SQLiteCommand( con ) )
{
await using ( var transaction = con.BeginTransaction() )
{
try
{
foreach ( var system in systems )
{
// Delete all possible variations of this data from the database.
if ( system.systemAddress != 0 )
{
cmd.CommandText = DELETE_SQL + WHERE_SYSTEMADDRESS;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue( "@systemaddress", system.systemAddress );
Logging.Debug( "Deleting starsystem " + system.systemAddress );
await cmd.ExecuteNonQueryAsync().ConfigureAwait( false );
}
else if ( !string.IsNullOrEmpty( system.systemname ) )
{
cmd.CommandText = DELETE_SQL + WHERE_NAME;
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue( "@name", system.systemname );
Logging.Debug( "Deleting starsystem " + system.systemname );
await cmd.ExecuteNonQueryAsync().ConfigureAwait( false );
}
}
transaction.Commit();
}
catch ( SQLiteException ex )
{
LogAndRollbackSqlLiteException( transaction, ex );
}
catch ( DllNotFoundException dllEx )
{
Logging.Warn( $"SQLite database unavailable. Unable to delete star systems: {dllEx.Message}" );
}
}
}
}
}
private void CreateOrUpdateDatabase()
{
using ( var con = SimpleDbConnection() )
{
try
{
con.Open();
using ( var cmd = new SQLiteCommand( CREATE_TABLE_SQL, con ) )
{
Logging.Debug( "Preparing starsystem repository" );
cmd.ExecuteNonQuery();
}
// Get schema version
using ( var cmd = new SQLiteCommand( TABLE_GET_SCHEMA_VERSION_SQL, con ) )
{
SCHEMA_VERSION = (long)( cmd.ExecuteScalar() ?? 0 );
Logging.Debug( "Starsystem repository is schema version " + SCHEMA_VERSION );
}
// Apply any necessary updates
if ( SCHEMA_VERSION < 1 )
{
Logging.Debug( "Updating starsystem repository to schema version 1" );
AddColumnIfMissing( con, "comment", @"ALTER TABLE starsystems ADD COLUMN comment TEXT;" );
SCHEMA_VERSION = 1;
}
if ( SCHEMA_VERSION < 2 )
{
Logging.Debug( "Updating starsystem repository to schema version 2" );
// Allocate our new columns
AddColumnIfMissing( con, "systemaddress", @"ALTER TABLE starsystems ADD COLUMN systemaddress INT" );
// We have to replace our table with a new copy to assign our new columns as unique
using ( var cmd = new SQLiteCommand( REPLACE_TABLE_SQL, con ) )
{
cmd.ExecuteNonQuery();
}
SCHEMA_VERSION = 2;
}
if ( SCHEMA_VERSION < 3 )
{
Logging.Debug( "Updating starsystem repository to schema version 3" );
// We will recreate our table without the "edsmid" column as we won't be indexing based on this value nor using it to evaluate uniqueness
// We have to replace our table with a new copy to reassign unique columns
using ( var cmd = new SQLiteCommand( REPLACE_TABLE_SQL, con ) )
{
cmd.ExecuteNonQuery();
}
SCHEMA_VERSION = 3;
}
if ( SCHEMA_VERSION < 4 )
{
Logging.Debug( "Updating starsystem repository to schema version 4" );
AddColumnIfMissing( con, "x", @"ALTER TABLE starsystems ADD COLUMN x DECIMAL(12,5)" );
AddColumnIfMissing( con, "y", @"ALTER TABLE starsystems ADD COLUMN y DECIMAL(12,5)" );
AddColumnIfMissing( con, "z", @"ALTER TABLE starsystems ADD COLUMN z DECIMAL(12,5)" );
AddColumnIfMissing( con, "population", @"ALTER TABLE starsystems ADD COLUMN population INT" );
// We have to replace our table with a new copy to reorder columns
using ( var cmd = new SQLiteCommand( REPLACE_TABLE_SQL, con ) )
{
cmd.ExecuteNonQuery();
}
SCHEMA_VERSION = 4;
}
// Add our indices (if they don't already exist)
using ( var cmd = new SQLiteCommand( CREATE_INDEX_SQL, con ) )
{
Logging.Debug( "Creating starsystem index" );
cmd.ExecuteNonQuery();
}
// Optimize the database
using ( var cmd = new SQLiteCommand( "PRAGMA optimize;", con ) )
{
Logging.Debug( "Creating starsystem index" );
cmd.ExecuteNonQuery();
}
// Set schema version
using ( var cmd = new SQLiteCommand( TABLE_SET_SCHEMA_VERSION_SQL + SCHEMA_VERSION + ";", con ) )
{
Logging.Info( "Starsystem repository schema is version " + SCHEMA_VERSION );
cmd.ExecuteNonQuery();
}
}
catch ( SQLiteException ex )
{
Logging.Warn( "SQLite error: ", ex.ToString() );
}
}
Logging.Debug("Starsystem repository ready.");
}
/// <summary> Parameters like `DISTINCT` cannot be set on columns by this method, the table would need to be recreated instead. </summary>
private static void AddColumnIfMissing(SQLiteConnection con, string columnName, string alterTableCmd )
{
//
if ( !string.IsNullOrEmpty( alterTableCmd ) )
{
var columnExists = false;
using ( var cmd = new SQLiteCommand( TABLE_INFO_SQL, con ) )
{
using ( var rdr = cmd.ExecuteReader() )
{
while ( rdr.Read() )
{
if ( columnName == rdr.GetString( 1 ) )
{
columnExists = true;
break;
}
}
}
}
if ( !columnExists )
{
Logging.Debug( "Updating starsystem repository with new column " + columnName );
try
{
using ( var cmd = new SQLiteCommand( alterTableCmd, con ) )
{
cmd.ExecuteNonQuery();
}
}
catch ( SQLiteException ex )
{
Logging.Warn( "SQLite error: ", ex.ToString() );
}
}
}
}
private static void LogAndRollbackSqlLiteException ( SQLiteTransaction transaction, SQLiteException ex )
{
Logging.Warn( "SQLite error: ", ex.ToString() );
try
{
transaction?.Rollback();
}
catch ( SQLiteException ex2 )
{
Logging.Warn( "SQLite transaction rollback failed." );
Logging.Warn( "SQLite error: ", ex2.ToString() );
}
}
}
}