|
| 1 | +using SqlKata.Compilers; |
| 2 | +using SqlKata.Extensions; |
| 3 | +using SqlKata.Tests.Infrastructure; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace SqlKata.Tests |
| 7 | +{ |
| 8 | + public class GeneralTests : TestSupport |
| 9 | + { |
| 10 | + [Fact] |
| 11 | + public void ColumnsEscaping() |
| 12 | + { |
| 13 | + var q = new Query().From("users").Select("mycol[isthis]"); |
| 14 | + var c = Compile(q); |
| 15 | + |
| 16 | + Assert.Equal("SELECT [mycol[isthis]]] FROM [users]", c[EngineCodes.SqlServer]); |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void InnerScopeEngineWithinCTE() |
| 22 | + { |
| 23 | + var series = new Query("table") |
| 24 | + .ForPostgreSql(q => q.WhereRaw("postgres = true")) |
| 25 | + .ForSqlServer(q => q.WhereRaw("sqlsrv = 1")) |
| 26 | + .ForFirebird(q => q.WhereRaw("firebird = 1")); |
| 27 | + var query = new Query("series").With("series", series); |
| 28 | + |
| 29 | + var c = Compile(query); |
| 30 | + |
| 31 | + Assert.Equal("WITH [series] AS (SELECT * FROM [table] WHERE sqlsrv = 1)\nSELECT * FROM [series]", c[EngineCodes.SqlServer]); |
| 32 | + |
| 33 | + Assert.Equal("WITH \"series\" AS (SELECT * FROM \"table\" WHERE postgres = true)\nSELECT * FROM \"series\"", |
| 34 | + c[EngineCodes.PostgreSql]); |
| 35 | + Assert.Equal("WITH \"SERIES\" AS (SELECT * FROM \"TABLE\" WHERE firebird = 1)\nSELECT * FROM \"SERIES\"", |
| 36 | + c[EngineCodes.Firebird]); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void InnerScopeEngineWithinSubQuery() |
| 41 | + { |
| 42 | + var series = new Query("table") |
| 43 | + .ForPostgreSql(q => q.WhereRaw("postgres = true")) |
| 44 | + .ForSqlServer(q => q.WhereRaw("sqlsrv = 1")) |
| 45 | + .ForFirebird(q => q.WhereRaw("firebird = 1")); |
| 46 | + var query = new Query("series").From(series.As("series")); |
| 47 | + |
| 48 | + var c = Compile(query); |
| 49 | + |
| 50 | + Assert.Equal("SELECT * FROM (SELECT * FROM [table] WHERE sqlsrv = 1) AS [series]", c[EngineCodes.SqlServer]); |
| 51 | + |
| 52 | + Assert.Equal("SELECT * FROM (SELECT * FROM \"table\" WHERE postgres = true) AS \"series\"", c[EngineCodes.PostgreSql]); |
| 53 | + Assert.Equal("SELECT * FROM (SELECT * FROM \"TABLE\" WHERE firebird = 1) AS \"SERIES\"", c[EngineCodes.Firebird]); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void ItShouldCacheMethodInfoByType() |
| 58 | + { |
| 59 | + var compiler = new TestSqlServerCompiler(); |
| 60 | + |
| 61 | + var call1 = compiler.Call_FindCompilerMethodInfo( |
| 62 | + typeof(BasicCondition), "CompileBasicCondition" |
| 63 | + ); |
| 64 | + |
| 65 | + var call2 = compiler.Call_FindCompilerMethodInfo( |
| 66 | + typeof(BasicCondition), "CompileBasicCondition" |
| 67 | + ); |
| 68 | + |
| 69 | + Assert.Same(call1, call2); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void Return_Different_MethodInfo_WhenSame_Method_With_Different_GenericTypes() |
| 74 | + { |
| 75 | + var compiler = new TestSqlServerCompiler(); |
| 76 | + |
| 77 | + var call1 = compiler.Call_FindCompilerMethodInfo( |
| 78 | + typeof(NestedCondition<Query>), "CompileNestedCondition" |
| 79 | + ); |
| 80 | + |
| 81 | + var call2 = compiler.Call_FindCompilerMethodInfo( |
| 82 | + typeof(NestedCondition<Join>), "CompileNestedCondition" |
| 83 | + ); |
| 84 | + |
| 85 | + Assert.NotSame(call1, call2); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void Should_Equal_AfterMultipleCompile() |
| 90 | + { |
| 91 | + var query = new Query() |
| 92 | + .Select("Id", "Name") |
| 93 | + .From("Table") |
| 94 | + .OrderBy("Name") |
| 95 | + .Limit(20) |
| 96 | + .Offset(1); |
| 97 | + |
| 98 | + var first = Compile(query); |
| 99 | + Assert.Equal( |
| 100 | + "SELECT * FROM (SELECT [Id], [Name], ROW_NUMBER() OVER (ORDER BY [Name]) AS [row_num] FROM [Table]) AS [results_wrapper] WHERE [row_num] BETWEEN 2 AND 21", |
| 101 | + first[EngineCodes.SqlServer]); |
| 102 | + Assert.Equal("SELECT `Id`, `Name` FROM `Table` ORDER BY `Name` LIMIT 20 OFFSET 1", first[EngineCodes.MySql]); |
| 103 | + Assert.Equal("SELECT \"Id\", \"Name\" FROM \"Table\" ORDER BY \"Name\" LIMIT 20 OFFSET 1", first[EngineCodes.PostgreSql]); |
| 104 | + Assert.Equal("SELECT \"ID\", \"NAME\" FROM \"TABLE\" ORDER BY \"NAME\" ROWS 2 TO 21", first[EngineCodes.Firebird]); |
| 105 | + |
| 106 | + var second = Compile(query); |
| 107 | + |
| 108 | + Assert.Equal(first[EngineCodes.SqlServer], second[EngineCodes.SqlServer]); |
| 109 | + Assert.Equal(first[EngineCodes.MySql], second[EngineCodes.MySql]); |
| 110 | + Assert.Equal(first[EngineCodes.PostgreSql], second[EngineCodes.PostgreSql]); |
| 111 | + Assert.Equal(first[EngineCodes.Firebird], second[EngineCodes.Firebird]); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void Raw_WrapIdentifiers() |
| 116 | + { |
| 117 | + var query = new Query("Users").SelectRaw("[Id], [Name], {Age}"); |
| 118 | + |
| 119 | + var c = Compile(query); |
| 120 | + |
| 121 | + Assert.Equal("SELECT [Id], [Name], [Age] FROM [Users]", c[EngineCodes.SqlServer]); |
| 122 | + Assert.Equal("SELECT `Id`, `Name`, `Age` FROM `Users`", c[EngineCodes.MySql]); |
| 123 | + Assert.Equal("SELECT \"Id\", \"Name\", \"Age\" FROM \"Users\"", c[EngineCodes.PostgreSql]); |
| 124 | + Assert.Equal("SELECT \"Id\", \"Name\", \"Age\" FROM \"USERS\"", c[EngineCodes.Firebird]); |
| 125 | + } |
| 126 | + |
| 127 | + [Fact] |
| 128 | + public void WrapWithSpace() |
| 129 | + { |
| 130 | + var compiler = new SqlServerCompiler(); |
| 131 | + |
| 132 | + |
| 133 | + Assert.Equal("[My Table] AS [Table]", compiler.Wrap("My Table as Table")); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public void WrapWithDotes() |
| 138 | + { |
| 139 | + var compiler = new SqlServerCompiler(); |
| 140 | + |
| 141 | + |
| 142 | + Assert.Equal("[My Schema].[My Table] AS [Table]", compiler.Wrap("My Schema.My Table as Table")); |
| 143 | + } |
| 144 | + |
| 145 | + [Fact] |
| 146 | + public void WrapWithMultipleSpaces() |
| 147 | + { |
| 148 | + var compiler = new SqlServerCompiler(); |
| 149 | + |
| 150 | + |
| 151 | + Assert.Equal("[My Table One] AS [Table One]", compiler.Wrap("My Table One as Table One")); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments