@@ -57,55 +57,23 @@ public async Task<string> GetCreateTablesScriptAsync(IEnumerable<string> tables,
5757 foreach ( var table in splittedTableNames )
5858 {
5959 var query = $ """
60- SELECT TABLE_SCHEMA AS [SCHEMA], TABLE_NAME AS [TABLE], COLUMN_NAME AS [COLUMN],
61- UPPER(DATA_TYPE) + ISNULL('(' + IIF(CHARACTER_MAXIMUM_LENGTH = -1, 'MAX', CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10))) + ')','') + ' ' +
62- CASE WHEN IS_NULLABLE = 'YES' THEN 'NULL' ELSE 'NOT NULL' END AS DESCRIPTION
63- FROM
64- INFORMATION_SCHEMA.COLUMNS
65- WHERE
66- TABLE_SCHEMA = @schema AND TABLE_NAME = @table
60+ SELECT '[' + COLUMN_NAME + '] ' +
61+ UPPER(DATA_TYPE) + COALESCE('(' +
62+ CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN 'MAX' ELSE CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) END + ')','') + ' ' +
63+ CASE WHEN IS_NULLABLE = 'YES' THEN 'NULL' ELSE 'NOT NULL' END
64+ FROM INFORMATION_SCHEMA.COLUMNS
65+ WHERE TABLE_SCHEMA = @schema
66+ AND TABLE_NAME = @table
67+ AND COLUMN_NAME NOT IN @{ nameof ( excludedColumns ) }
68+ AND TABLE_SCHEMA + '.' + TABLE_NAME + '.' + COLUMN_NAME NOT IN @{ nameof ( excludedColumns ) } ;
6769 """ ;
6870
69- var allColumns = await connection . QueryAsync < ColumnEntity > ( query , new { schema = table . Schema , table = table . Name } ) ;
70-
71- var columns = allColumns . Where ( c => IsIncluded ( c , excludedColumns ) )
72- . Select ( c => $ "[{ c . Column } ] { c . Description } ") . ToList ( ) ;
71+ var columns = await connection . QueryAsync < ColumnEntity > ( query , new { schema = table . Schema , table = table . Name , excludedColumns } ) ;
7372
7473 result . AppendLine ( $ "CREATE TABLE [{ table . Schema } ].[{ table . Name } ] ({ string . Join ( ',' , columns ) } );") ;
7574 }
7675
7776 return result . ToString ( ) ;
78-
79- static bool IsIncluded ( ColumnEntity column , IEnumerable < string > excludedColumns )
80- {
81- // Checks if the column should be included or not, verifying if it is present in the list of excluded columns.
82- var isIncluded = excludedColumns . All ( e =>
83- {
84- var parts = e . Split ( '.' ) ;
85- var schemaOrColumnName = parts . ElementAtOrDefault ( 0 ) ? . Trim ( ) ;
86- var tableName = parts . ElementAtOrDefault ( 1 ) ? . Trim ( ) ;
87- var columnName = parts . ElementAtOrDefault ( 2 ) ? . Trim ( ) ;
88-
89- if ( string . IsNullOrWhiteSpace ( columnName ) )
90- {
91- // The columnName variable is null: it means that the excluded column has been specified without the full qualified name.
92- // In this case, the schemaOrColumnName variable contains the column name and it is a column that must be excluded from all tables.
93- var isExcluded = column . Column . Equals ( schemaOrColumnName , StringComparison . OrdinalIgnoreCase ) ;
94- return ! isExcluded ;
95- }
96- else
97- {
98- // The excluded column has been specified using the full qualified name.
99- var isExcluded = column . Schema . Equals ( schemaOrColumnName , StringComparison . OrdinalIgnoreCase )
100- && column . Table . Equals ( tableName , StringComparison . OrdinalIgnoreCase )
101- && column . Column . Equals ( columnName , StringComparison . OrdinalIgnoreCase ) ;
102-
103- return ! isExcluded ;
104- }
105- } ) ;
106-
107- return isIncluded ;
108- }
10977 }
11078
11179 public Task < string ? > GetQueryHintsAsync ( CancellationToken cancellationToken = default )
0 commit comments