1- // Copyright (C) 2017 Xtensive LLC.
2- // All rights reserved .
3- // For conditions of distribution and use, see license .
1+ // Copyright (C) 2017-2022 Xtensive LLC.
2+ // This code is distributed under MIT license terms .
3+ // See the License.txt file in the project root for more information .
44// Created by: Alexey Kulakov
55// Created: 2017.03.24
66
99using NUnit . Framework ;
1010using Xtensive . Sql ;
1111using Xtensive . Sql . Compiler ;
12- using Xtensive . Sql . Info ;
1312using Xtensive . Sql . Model ;
1413
1514namespace Xtensive . Orm . Tests . Sql
@@ -22,6 +21,8 @@ public class MakeNamesUnreadableTest : SqlTest
2221 private const string DummySchemaName = "DummySchema" ;
2322 private const string DummyDatabaseName = "DummyDatabase" ;
2423
24+ private readonly Dictionary < string , string > emptyMap = new ( 0 ) ;
25+
2526 [ Test ]
2627 public void ReadingNamesTest ( )
2728 {
@@ -48,14 +49,14 @@ public void ReadingNamesTest()
4849
4950 catalog . MakeNamesUnreadable ( ) ;
5051
51- Assert . Throws < InvalidOperationException > ( ( ) => { var catalogName = catalog . Name ; } ) ;
52- Assert . Throws < InvalidOperationException > ( ( ) => { var catalogDbName = catalog . DbName ; } ) ;
52+ _ = Assert . Throws < InvalidOperationException > ( ( ) => { var catalogName = catalog . Name ; } ) ;
53+ _ = Assert . Throws < InvalidOperationException > ( ( ) => { var catalogDbName = catalog . DbName ; } ) ;
5354
5455 Assert . DoesNotThrow ( ( ) => { var catalogName = catalog . GetNameInternal ( ) ; } ) ;
5556 Assert . DoesNotThrow ( ( ) => { var catalogDbName = catalog . GetDbNameInternal ( ) ; } ) ;
5657
57- Assert . Throws < InvalidOperationException > ( ( ) => { var schemaName = defaultSchema . Name ; } ) ;
58- Assert . Throws < InvalidOperationException > ( ( ) => { var schemaName = defaultSchema . DbName ; } ) ;
58+ _ = Assert . Throws < InvalidOperationException > ( ( ) => { var schemaName = defaultSchema . Name ; } ) ;
59+ _ = Assert . Throws < InvalidOperationException > ( ( ) => { var schemaName = defaultSchema . DbName ; } ) ;
5960
6061 Assert . DoesNotThrow ( ( ) => { var schemaName = defaultSchema . GetNameInternal ( ) ; } ) ;
6162 Assert . DoesNotThrow ( ( ) => { var schemaName = defaultSchema . GetDbNameInternal ( ) ; } ) ;
@@ -115,11 +116,11 @@ public void ChangingNamesTest()
115116
116117 catalog . MakeNamesUnreadable ( ) ;
117118
118- Assert . Throws < InvalidOperationException > ( ( ) => catalog . Name = newCatalogName ) ;
119- Assert . Throws < InvalidOperationException > ( ( ) => catalog . DbName = newCatalogDbName ) ;
119+ _ = Assert . Throws < InvalidOperationException > ( ( ) => catalog . Name = newCatalogName ) ;
120+ _ = Assert . Throws < InvalidOperationException > ( ( ) => catalog . DbName = newCatalogDbName ) ;
120121
121- Assert . Throws < InvalidOperationException > ( ( ) => defaultSchema . Name = newSchemaName ) ;
122- Assert . Throws < InvalidOperationException > ( ( ) => defaultSchema . DbName = newSchemaDbName ) ;
122+ _ = Assert . Throws < InvalidOperationException > ( ( ) => defaultSchema . Name = newSchemaName ) ;
123+ _ = Assert . Throws < InvalidOperationException > ( ( ) => defaultSchema . DbName = newSchemaDbName ) ;
123124 }
124125
125126 [ Test ]
@@ -129,8 +130,8 @@ public void TableCreationTest()
129130
130131 var table = defaultSchema . CreateTable ( string . Format ( "Crt1_{0}" , TableName ) ) ;
131132 var column = table . CreateColumn ( "Id" , GetServerTypeFor ( typeof ( int ) ) ) ;
132- table . CreatePrimaryKey ( "PK_Crt_DenyNamesReadingTest" , column ) ;
133- column = table . CreateColumn ( "CreationDate" , GetServerTypeFor ( typeof ( DateTime ) ) ) ;
133+ _ = table . CreatePrimaryKey ( "PK_Crt_DenyNamesReadingTest" , column ) ;
134+ _ = table . CreateColumn ( "CreationDate" , GetServerTypeFor ( typeof ( DateTime ) ) ) ;
134135 var createTableQuery = SqlDdl . Create ( table ) ;
135136
136137 TestQueryNamesReadable ( createTableQuery , defaultSchema ) ;
@@ -144,8 +145,8 @@ public void TableCreationUnreadableNamesTest()
144145
145146 var table = defaultSchema . CreateTable ( string . Format ( "Crt1_{0}" , TableName ) ) ;
146147 var column = table . CreateColumn ( "Id" , GetServerTypeFor ( typeof ( int ) ) ) ;
147- table . CreatePrimaryKey ( "PK_Crt_DenyNamesReadingTest" , column ) ;
148- column = table . CreateColumn ( "CreationDate" , GetServerTypeFor ( typeof ( DateTime ) ) ) ;
148+ _ = table . CreatePrimaryKey ( "PK_Crt_DenyNamesReadingTest" , column ) ;
149+ _ = table . CreateColumn ( "CreationDate" , GetServerTypeFor ( typeof ( DateTime ) ) ) ;
149150 var createTableQuery = SqlDdl . Create ( table ) ;
150151
151152 TestQueryNamesUnreadable ( createTableQuery , defaultSchema ) ;
@@ -307,51 +308,50 @@ public void RowSelectionUnreadableNamesTest()
307308
308309 private void TestQueryNamesReadable ( ISqlCompileUnit query , Schema defaultSchema )
309310 {
310- string queryText = string . Empty ;
311+ var queryText = string . Empty ;
311312
312- if ( MultidatabaseSupported ( ) ) {
313- var compilerConfiguration = new SqlCompilerConfiguration ( new Dictionary < string , string > ( ) , new Dictionary < string , string > ( ) ) { DatabaseQualifiedObjects = true } ;
314- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
313+ if ( IsMultidatabaseSupported ) {
314+ var compilerConfiguration = new SqlCompilerConfiguration { DatabaseQualifiedObjects = true } ;
315+ var postCompilerConfiguration = new SqlPostCompilerConfiguration ( emptyMap , emptyMap ) ;
316+
317+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( postCompilerConfiguration ) ) ;
315318 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . True ) ;
316319 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . True ) ;
317320
318- var schemaMap = new Dictionary < string , string > { { defaultSchema . GetDbNameInternal ( ) , DummySchemaName } } ;
319- var databaseMap = new Dictionary < string , string > { { defaultSchema . Catalog . GetDbNameInternal ( ) , DummyDatabaseName } } ;
320- compilerConfiguration = new SqlCompilerConfiguration ( databaseMap , schemaMap ) { DatabaseQualifiedObjects = true } ;
321+ compilerConfiguration = new SqlCompilerConfiguration { DatabaseQualifiedObjects = true } ;
322+ postCompilerConfiguration = new SqlPostCompilerConfiguration ( GetDatabaseMap ( defaultSchema . Catalog ) , GetSchemaMap ( defaultSchema ) ) ;
321323
322- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
324+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( postCompilerConfiguration ) ) ;
323325 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . True ) ;
324326 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . True ) ;
325327 Assert . That ( queryText . Contains ( DummyDatabaseName ) , Is . False ) ;
326328 Assert . That ( queryText . Contains ( DummySchemaName ) , Is . False ) ;
327329 }
328- if ( MultischemaSupported ( ) ) {
329- var compilerConfiguration = new SqlCompilerConfiguration ( new Dictionary < string , string > ( ) , new Dictionary < string , string > ( ) ) { DatabaseQualifiedObjects = false } ;
330- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
330+ if ( IsMultischemaSupported ) {
331+ var postCompilerConfiguration = new SqlPostCompilerConfiguration ( emptyMap , emptyMap ) ;
332+
333+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query ) . GetCommandText ( postCompilerConfiguration ) ) ;
331334 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . True ) ;
332335 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . False ) ;
333336
334- var schemaMap = new Dictionary < string , string > ( ) { { defaultSchema . GetDbNameInternal ( ) , DummySchemaName } } ;
335- var databaseMap = new Dictionary < string , string > ( ) { { defaultSchema . Catalog . GetDbNameInternal ( ) , DummyDatabaseName } } ;
336- compilerConfiguration = new SqlCompilerConfiguration ( databaseMap , schemaMap ) { DatabaseQualifiedObjects = false } ;
337+ postCompilerConfiguration = new SqlPostCompilerConfiguration ( GetDatabaseMap ( defaultSchema . Catalog ) , GetSchemaMap ( defaultSchema ) ) ;
337338
338- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
339+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query ) . GetCommandText ( postCompilerConfiguration ) ) ;
339340 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . True ) ;
340341 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . False ) ;
341342 Assert . That ( queryText . Contains ( DummyDatabaseName ) , Is . False ) ;
342343 Assert . That ( queryText . Contains ( DummySchemaName ) , Is . False ) ;
343344 }
344345 else {
345- var compilerConfiguration = new SqlCompilerConfiguration ( new Dictionary < string , string > ( ) , new Dictionary < string , string > ( ) ) { DatabaseQualifiedObjects = false } ;
346- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
346+ var postCompilerConfiguration = new SqlPostCompilerConfiguration ( emptyMap , emptyMap ) ;
347+
348+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query ) . GetCommandText ( postCompilerConfiguration ) ) ;
347349 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . False ) ;
348350 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . False ) ;
349351
350- var schemaMap = new Dictionary < string , string > { { defaultSchema . GetDbNameInternal ( ) , DummySchemaName } } ;
351- var databaseMap = new Dictionary < string , string > { { defaultSchema . Catalog . GetDbNameInternal ( ) , DummyDatabaseName } } ;
352- compilerConfiguration = new SqlCompilerConfiguration ( databaseMap , schemaMap ) { DatabaseQualifiedObjects = false } ;
352+ postCompilerConfiguration = new SqlPostCompilerConfiguration ( GetDatabaseMap ( defaultSchema . Catalog ) , GetSchemaMap ( defaultSchema ) ) ;
353353
354- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
354+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query ) . GetCommandText ( postCompilerConfiguration ) ) ;
355355 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . False ) ;
356356 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . False ) ;
357357 Assert . That ( queryText . Contains ( DummyDatabaseName ) , Is . False ) ;
@@ -361,58 +361,56 @@ private void TestQueryNamesReadable(ISqlCompileUnit query, Schema defaultSchema)
361361
362362 private void TestQueryNamesUnreadable ( ISqlCompileUnit query , Schema defaultSchema )
363363 {
364- string queryText = string . Empty ;
364+ var queryText = string . Empty ;
365365
366- if ( MultidatabaseSupported ( ) ) {
367- Assert . Throws < ArgumentNullException > ( ( ) => queryText = Driver . Compile ( query ) . GetCommandText ( ) ) ;
366+ if ( IsMultidatabaseSupported ) {
367+ var compilerConfiguration = new SqlCompilerConfiguration ( ) { DatabaseQualifiedObjects = true , SharedStorageSchema = true } ;
368368
369- var compilerConfiguration =
370- new SqlCompilerConfiguration ( new Dictionary < string , string > ( ) , new Dictionary < string , string > ( ) ) { DatabaseQualifiedObjects = true } ;
371- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
369+ _ = Assert . Throws < ArgumentNullException > ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
370+
371+ var postCompilerConfiguration = new SqlPostCompilerConfiguration ( emptyMap , emptyMap ) ;
372+
373+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( postCompilerConfiguration ) ) ;
372374 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . True ) ;
373375 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . True ) ;
374376
375- var schemaMap = new Dictionary < string , string > { { defaultSchema . GetDbNameInternal ( ) , DummySchemaName } } ;
376- var databaseMap = new Dictionary < string , string > { { defaultSchema . Catalog . GetDbNameInternal ( ) , DummyDatabaseName } } ;
377- compilerConfiguration = new SqlCompilerConfiguration ( databaseMap , schemaMap ) { DatabaseQualifiedObjects = true } ;
377+ postCompilerConfiguration = new SqlPostCompilerConfiguration ( GetDatabaseMap ( defaultSchema . Catalog ) , GetSchemaMap ( defaultSchema ) ) ;
378378
379- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
379+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( postCompilerConfiguration ) ) ;
380380 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . False ) ;
381381 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . False ) ;
382382 Assert . That ( queryText . Contains ( DummyDatabaseName ) , Is . True ) ;
383383 Assert . That ( queryText . Contains ( DummySchemaName ) , Is . True ) ;
384384 }
385- if ( MultischemaSupported ( ) ) {
386- Assert . Throws < ArgumentNullException > ( ( ) => queryText = Driver . Compile ( query ) . GetCommandText ( ) ) ;
385+ if ( IsMultischemaSupported ) {
386+ var compilerConfiguration = new SqlCompilerConfiguration ( ) { SharedStorageSchema = true } ;
387387
388- var compilerConfiguration =
389- new SqlCompilerConfiguration ( new Dictionary < string , string > ( ) , new Dictionary < string , string > ( ) ) { DatabaseQualifiedObjects = false } ;
390- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
388+ _ = Assert . Throws < ArgumentNullException > ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
389+
390+ var postCompilerConfiguration = new SqlPostCompilerConfiguration ( emptyMap , emptyMap ) ;
391+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( postCompilerConfiguration ) ) ;
391392 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . True ) ;
392393 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . False ) ;
393394
394- var schemaMap = new Dictionary < string , string > { { defaultSchema . GetDbNameInternal ( ) , DummySchemaName } } ;
395- var databaseMap = new Dictionary < string , string > { { defaultSchema . Catalog . GetDbNameInternal ( ) , DummyDatabaseName } } ;
396- compilerConfiguration = new SqlCompilerConfiguration ( databaseMap , schemaMap ) { DatabaseQualifiedObjects = false } ;
395+ postCompilerConfiguration = new SqlPostCompilerConfiguration ( GetDatabaseMap ( defaultSchema . Catalog ) , GetSchemaMap ( defaultSchema ) ) ;
397396
398- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
397+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( postCompilerConfiguration ) ) ;
399398 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . False ) ;
400399 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . False ) ;
401400 Assert . That ( queryText . Contains ( DummyDatabaseName ) , Is . False ) ;
402401 Assert . That ( queryText . Contains ( DummySchemaName ) , Is . True ) ;
403402 }
404403 else {
405- Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query ) . GetCommandText ( ) ) ;
406-
407- var compilerConfiguration =
408- new SqlCompilerConfiguration ( new Dictionary < string , string > ( ) , new Dictionary < string , string > ( ) ) { DatabaseQualifiedObjects = false } ;
404+ var compilerConfiguration = new SqlCompilerConfiguration ( ) { SharedStorageSchema = true } ;
409405 Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
406+
407+ var postCompilerConfiguration = new SqlPostCompilerConfiguration ( emptyMap , emptyMap ) ;
408+
409+ Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( postCompilerConfiguration ) ) ;
410410 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . False ) ;
411411 Assert . That ( queryText . Contains ( defaultSchema . Catalog . GetDbNameInternal ( ) ) , Is . False ) ;
412412
413- var schemaMap = new Dictionary < string , string > { { defaultSchema . GetDbNameInternal ( ) , DummySchemaName } } ;
414- var databaseMap = new Dictionary < string , string > { { defaultSchema . Catalog . GetDbNameInternal ( ) , DummyDatabaseName } } ;
415- compilerConfiguration = new SqlCompilerConfiguration ( databaseMap , schemaMap ) { DatabaseQualifiedObjects = false } ;
413+ postCompilerConfiguration = new SqlPostCompilerConfiguration ( GetDatabaseMap ( defaultSchema . Catalog ) , GetSchemaMap ( defaultSchema ) ) ;
416414
417415 Assert . DoesNotThrow ( ( ) => queryText = Driver . Compile ( query , compilerConfiguration ) . GetCommandText ( ) ) ;
418416 Assert . That ( queryText . Contains ( defaultSchema . GetDbNameInternal ( ) ) , Is . False ) ;
@@ -422,6 +420,13 @@ private void TestQueryNamesUnreadable(ISqlCompileUnit query, Schema defaultSchem
422420 }
423421 }
424422
423+ private static Dictionary < string , string > GetSchemaMap ( Schema schema ) =>
424+ new ( ) { { schema . GetDbNameInternal ( ) , DummySchemaName } } ;
425+
426+ private static Dictionary < string , string > GetDatabaseMap ( Catalog catalog ) =>
427+ new ( ) { { catalog . GetDbNameInternal ( ) , DummyDatabaseName } } ;
428+
429+
425430 private Schema GetSchema ( )
426431 {
427432 var catalog = new Catalog ( CatalogName ) ;
@@ -430,34 +435,15 @@ private Schema GetSchema()
430435 var defaultSchema = catalog . DefaultSchema = schema ;
431436 var table = defaultSchema . CreateTable ( TableName ) ;
432437 var column = table . CreateColumn ( "Id" , GetServerTypeFor ( typeof ( int ) ) ) ;
433- table . CreatePrimaryKey ( "PK_DenyNamesReadingTest" , column ) ;
434- column = table . CreateColumn ( "CreationDate" , GetServerTypeFor ( typeof ( DateTime ) ) ) ;
438+ _ = table . CreatePrimaryKey ( "PK_DenyNamesReadingTest" , column ) ;
439+ _ = table . CreateColumn ( "CreationDate" , GetServerTypeFor ( typeof ( DateTime ) ) ) ;
435440 return defaultSchema ;
436441 }
437442
438- private SqlValueType GetServerTypeFor ( Type type )
439- {
440- return Driver . TypeMappings . Mappings [ type ] . MapType ( 255 , null , null ) ;
441- }
442-
443- private SqlValueType GetServerTypeFor ( Type type , int length )
444- {
445- return Driver . TypeMappings . Mappings [ type ] . MapType ( length , null , null ) ;
446- }
447-
448- private SqlValueType GetServerTypeFor ( Type type , int length , int precision , int scale )
449- {
450- return Driver . TypeMappings . Mappings [ type ] . MapType ( length , precision , scale ) ;
451- }
443+ private SqlValueType GetServerTypeFor ( Type type ) =>
444+ Driver . TypeMappings . Mappings [ type ] . MapType ( 255 , null , null ) ;
452445
453- private bool MultidatabaseSupported ( )
454- {
455- return Driver . ServerInfo . Query . Features . HasFlag ( QueryFeatures . MultidatabaseQueries ) ;
456- }
457-
458- private bool MultischemaSupported ( )
459- {
460- return Driver . ServerInfo . Query . Features . HasFlag ( QueryFeatures . MultischemaQueries ) ;
461- }
446+ private SqlValueType GetServerTypeFor ( Type type , int length ) =>
447+ Driver . TypeMappings . Mappings [ type ] . MapType ( length , null , null ) ;
462448 }
463449}
0 commit comments