@@ -163,16 +163,15 @@ public partial class SQLiteConnection : IDisposable
163
163
{
164
164
private bool _open ;
165
165
private TimeSpan _busyTimeout ;
166
- private Dictionary < string , TableMapping > _mappings = null ;
167
- private Dictionary < string , TableMapping > _tables = null ;
166
+ readonly static Dictionary < string , TableMapping > _mappings = new Dictionary < string , TableMapping > ( ) ;
168
167
private System . Diagnostics . Stopwatch _sw ;
169
168
private long _elapsedMilliseconds = 0 ;
170
169
171
170
private int _transactionDepth = 0 ;
172
171
private Random _rand = new Random ( ) ;
173
172
174
173
public Sqlite3DatabaseHandle Handle { get ; private set ; }
175
- internal static readonly Sqlite3DatabaseHandle NullHandle = default ( Sqlite3DatabaseHandle ) ;
174
+ static readonly Sqlite3DatabaseHandle NullHandle = default ( Sqlite3DatabaseHandle ) ;
176
175
177
176
/// <summary>
178
177
/// Gets the database path used by this connection.
@@ -345,7 +344,9 @@ public TimeSpan BusyTimeout {
345
344
/// </summary>
346
345
public IEnumerable < TableMapping > TableMappings {
347
346
get {
348
- return _tables != null ? _tables . Values : Enumerable . Empty < TableMapping > ( ) ;
347
+ lock ( _mappings ) {
348
+ return new List < TableMapping > ( _mappings . Values ) ;
349
+ }
349
350
}
350
351
}
351
352
@@ -364,13 +365,19 @@ public IEnumerable<TableMapping> TableMappings {
364
365
/// </returns>
365
366
public TableMapping GetMapping ( Type type , CreateFlags createFlags = CreateFlags . None )
366
367
{
367
- if ( _mappings == null ) {
368
- _mappings = new Dictionary < string , TableMapping > ( ) ;
369
- }
370
368
TableMapping map ;
371
- if ( ! _mappings . TryGetValue ( type . FullName , out map ) ) {
372
- map = new TableMapping ( type , createFlags ) ;
373
- _mappings [ type . FullName ] = map ;
369
+ var key = type . FullName ;
370
+ lock ( _mappings ) {
371
+ if ( _mappings . TryGetValue ( key , out map ) ) {
372
+ if ( createFlags != CreateFlags . None && createFlags != map . CreateFlags ) {
373
+ map = new TableMapping ( type , createFlags ) ;
374
+ _mappings [ key ] = map ;
375
+ }
376
+ }
377
+ else {
378
+ map = new TableMapping ( type , createFlags ) ;
379
+ _mappings . Add ( key , map ) ;
380
+ }
374
381
}
375
382
return map ;
376
383
}
@@ -451,18 +458,11 @@ public CreateTableResult CreateTable<T> (CreateFlags createFlags = CreateFlags.N
451
458
/// </returns>
452
459
public CreateTableResult CreateTable ( Type ty , CreateFlags createFlags = CreateFlags . None )
453
460
{
454
- if ( _tables == null ) {
455
- _tables = new Dictionary < string , TableMapping > ( ) ;
456
- }
457
- TableMapping map ;
458
- if ( ! _tables . TryGetValue ( ty . FullName , out map ) ) {
459
- map = GetMapping ( ty , createFlags ) ;
460
- _tables . Add ( ty . FullName , map ) ;
461
- }
461
+ var map = GetMapping ( ty , createFlags ) ;
462
462
463
463
// Present a nice error if no columns specified
464
464
if ( map . Columns . Length == 0 ) {
465
- throw new Exception ( string . Format ( "Cannot create a table with zero columns (does '{0}' have public properties?)" , ty . FullName ) ) ;
465
+ throw new Exception ( string . Format ( "Cannot create a table without columns (does '{0}' have public properties?)" , ty . FullName ) ) ;
466
466
}
467
467
468
468
// Check if the table exists
@@ -2100,13 +2100,16 @@ public class TableMapping
2100
2100
2101
2101
public string GetByPrimaryKeySql { get ; private set ; }
2102
2102
2103
- Column _autoPk ;
2104
- Column [ ] _insertColumns ;
2105
- Column [ ] _insertOrReplaceColumns ;
2103
+ public CreateFlags CreateFlags { get ; private set ; }
2104
+
2105
+ readonly Column _autoPk ;
2106
+ readonly Column [ ] _insertColumns ;
2107
+ readonly Column [ ] _insertOrReplaceColumns ;
2106
2108
2107
2109
public TableMapping ( Type type , CreateFlags createFlags = CreateFlags . None )
2108
2110
{
2109
2111
MappedType = type ;
2112
+ CreateFlags = createFlags ;
2110
2113
2111
2114
var typeInfo = type . GetTypeInfo ( ) ;
2112
2115
var tableAttr =
@@ -2164,6 +2167,9 @@ from p in ti.DeclaredProperties
2164
2167
// People should not be calling Get/Find without a PK
2165
2168
GetByPrimaryKeySql = string . Format ( "select * from \" {0}\" limit 1" , TableName ) ;
2166
2169
}
2170
+
2171
+ _insertColumns = Columns . Where ( c => ! c . IsAutoInc ) . ToArray ( ) ;
2172
+ _insertOrReplaceColumns = Columns . ToArray ( ) ;
2167
2173
}
2168
2174
2169
2175
public bool HasAutoIncPK { get ; private set ; }
@@ -2177,18 +2183,12 @@ public void SetAutoIncPK (object obj, long id)
2177
2183
2178
2184
public Column [ ] InsertColumns {
2179
2185
get {
2180
- if ( _insertColumns == null ) {
2181
- _insertColumns = Columns . Where ( c => ! c . IsAutoInc ) . ToArray ( ) ;
2182
- }
2183
2186
return _insertColumns ;
2184
2187
}
2185
2188
}
2186
2189
2187
2190
public Column [ ] InsertOrReplaceColumns {
2188
2191
get {
2189
- if ( _insertOrReplaceColumns == null ) {
2190
- _insertOrReplaceColumns = Columns . ToArray ( ) ;
2191
- }
2192
2192
return _insertOrReplaceColumns ;
2193
2193
}
2194
2194
}
0 commit comments