2014-03-14 17:11:21 +11:00
|
|
|
|
using System;
|
2016-07-08 14:51:02 +02:00
|
|
|
|
using System.Diagnostics;
|
2020-09-17 13:36:26 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-04-12 19:55:50 +02:00
|
|
|
|
using Moq;
|
2016-04-12 15:11:07 +02:00
|
|
|
|
using NPoco;
|
2014-03-14 17:11:21 +11:00
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
using Umbraco.Core;
|
2017-12-18 18:26:32 +01:00
|
|
|
|
using Umbraco.Core.Migrations;
|
2017-12-19 15:06:18 +01:00
|
|
|
|
using Umbraco.Core.Migrations.Expressions.Common.Expressions;
|
|
|
|
|
|
using Umbraco.Core.Migrations.Expressions.Create.Index;
|
2014-03-14 17:11:21 +11:00
|
|
|
|
using Umbraco.Core.Persistence;
|
|
|
|
|
|
using Umbraco.Core.Persistence.DatabaseAnnotations;
|
|
|
|
|
|
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
|
2017-12-28 09:06:33 +01:00
|
|
|
|
using Umbraco.Core.Persistence.Dtos;
|
2014-03-14 17:11:21 +11:00
|
|
|
|
using Umbraco.Core.Persistence.SqlSyntax;
|
2021-01-25 19:22:58 +01:00
|
|
|
|
using Umbraco.Tests.Integration.Testing;
|
2017-12-20 11:18:17 +01:00
|
|
|
|
using Umbraco.Tests.Testing;
|
2021-02-09 10:22:42 +01:00
|
|
|
|
using Constants = Umbraco.Cms.Core.Constants;
|
2014-03-14 17:11:21 +11:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Tests.Persistence.SyntaxProvider
|
|
|
|
|
|
{
|
|
|
|
|
|
[TestFixture]
|
2021-01-25 19:22:58 +01:00
|
|
|
|
public class SqlServerSyntaxProviderTests : UmbracoIntegrationTest
|
2014-03-14 17:11:21 +11:00
|
|
|
|
{
|
2021-01-25 19:22:58 +01:00
|
|
|
|
private ISqlContext SqlContext => GetRequiredService<IUmbracoDatabaseFactory>().SqlContext;
|
|
|
|
|
|
|
|
|
|
|
|
[UmbracoTest(Database = UmbracoTestOptions.Database.NewEmptyPerTest)]
|
2014-03-14 17:11:21 +11:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Generate_Delete_SubQuery_Statement()
|
|
|
|
|
|
{
|
2017-09-19 18:19:05 +02:00
|
|
|
|
var mediaObjectType = Constants.ObjectTypes.Media;
|
2021-01-25 19:22:58 +01:00
|
|
|
|
var subQuery = SqlContext.Sql()
|
|
|
|
|
|
.Select("DISTINCT cmsContentNu.nodeId")
|
|
|
|
|
|
.From<ContentNuDto>()
|
2016-04-12 15:11:07 +02:00
|
|
|
|
.InnerJoin<NodeDto>()
|
2021-01-25 19:22:58 +01:00
|
|
|
|
.On<ContentNuDto, NodeDto>(left => left.NodeId, right => right.NodeId)
|
2016-04-12 15:11:07 +02:00
|
|
|
|
.Where<NodeDto>(dto => dto.NodeObjectType == mediaObjectType);
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2021-01-25 19:22:58 +01:00
|
|
|
|
var sqlOutput = SqlContext.SqlSyntax.GetDeleteSubquery("cmsContentNu", "nodeId", subQuery);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
|
2021-01-25 19:22:58 +01:00
|
|
|
|
Assert.AreEqual(@"DELETE FROM [cmsContentNu] WHERE [nodeId] IN (SELECT [nodeId] FROM (SELECT DISTINCT cmsContentNu.nodeId
|
|
|
|
|
|
FROM [cmsContentNu]
|
2014-03-14 17:11:21 +11:00
|
|
|
|
INNER JOIN [umbracoNode]
|
2021-01-25 19:22:58 +01:00
|
|
|
|
ON [cmsContentNu].[nodeId] = [umbracoNode].[id]
|
2016-03-16 07:56:41 +01:00
|
|
|
|
WHERE (([umbracoNode].[nodeObjectType] = @0))) x)".Replace(Environment.NewLine, " ").Replace("\n", " ").Replace("\r", " "),
|
2015-09-14 12:01:48 +02:00
|
|
|
|
sqlOutput.SQL.Replace(Environment.NewLine, " ").Replace("\n", " ").Replace("\r", " "));
|
2014-09-24 20:20:27 +10:00
|
|
|
|
|
2015-09-14 12:01:48 +02:00
|
|
|
|
Assert.AreEqual(1, sqlOutput.Arguments.Length);
|
|
|
|
|
|
Assert.AreEqual(mediaObjectType, sqlOutput.Arguments[0]);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[NUnit.Framework.Ignore("This doesn't actually test anything")]
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Can_Generate_Create_Table_Statement()
|
|
|
|
|
|
{
|
|
|
|
|
|
var type = typeof (NodeDto);
|
2016-04-12 15:11:07 +02:00
|
|
|
|
var definition = DefinitionFactory.GetTableDefinition(type, SqlContext.SqlSyntax);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
|
2016-04-12 15:11:07 +02:00
|
|
|
|
string create = SqlContext.SqlSyntax.Format(definition);
|
|
|
|
|
|
string primaryKey = SqlContext.SqlSyntax.FormatPrimaryKey(definition);
|
|
|
|
|
|
var indexes = SqlContext.SqlSyntax.Format(definition.Indexes);
|
|
|
|
|
|
var keys = SqlContext.SqlSyntax.Format(definition.ForeignKeys);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
|
2016-07-08 14:51:02 +02:00
|
|
|
|
Debug.Print(create);
|
|
|
|
|
|
Debug.Print(primaryKey);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
foreach (var sql in keys)
|
|
|
|
|
|
{
|
2016-07-08 14:51:02 +02:00
|
|
|
|
Debug.Print(sql);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var sql in indexes)
|
|
|
|
|
|
{
|
2016-07-08 14:51:02 +02:00
|
|
|
|
Debug.Print(sql);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Format_SqlServer_NonClusteredIndexDefinition_AddsNonClusteredDirective()
|
|
|
|
|
|
{
|
2018-11-26 16:54:32 +01:00
|
|
|
|
var sqlSyntax = new SqlServerSyntaxProvider();
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2014-03-14 17:11:21 +11:00
|
|
|
|
var indexDefinition = CreateIndexDefinition();
|
|
|
|
|
|
indexDefinition.IndexType = IndexTypes.NonClustered;
|
|
|
|
|
|
|
2015-02-22 21:36:02 +01:00
|
|
|
|
var actual = sqlSyntax.Format(indexDefinition);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
Assert.AreEqual("CREATE NONCLUSTERED INDEX [IX_A] ON [TheTable] ([A])", actual);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void Format_SqlServer_NonClusteredIndexDefinition_UsingIsClusteredFalse_AddsClusteredDirective()
|
|
|
|
|
|
{
|
2018-11-26 16:54:32 +01:00
|
|
|
|
var sqlSyntax = new SqlServerSyntaxProvider();
|
2014-03-14 17:11:21 +11:00
|
|
|
|
|
|
|
|
|
|
var indexDefinition = CreateIndexDefinition();
|
2018-10-24 11:51:07 +02:00
|
|
|
|
indexDefinition.IndexType = IndexTypes.Clustered;
|
2014-03-14 17:11:21 +11:00
|
|
|
|
|
2015-02-22 21:36:02 +01:00
|
|
|
|
var actual = sqlSyntax.Format(indexDefinition);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
Assert.AreEqual("CREATE CLUSTERED INDEX [IX_A] ON [TheTable] ([A])", actual);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void CreateIndexBuilder_SqlServer_NonClustered_CreatesNonClusteredIndex()
|
|
|
|
|
|
{
|
2020-09-17 13:36:26 +02:00
|
|
|
|
var logger = Mock.Of<ILogger<MigrationContext>>();
|
2018-11-26 16:54:32 +01:00
|
|
|
|
var sqlSyntax = new SqlServerSyntaxProvider();
|
2017-12-20 11:18:17 +01:00
|
|
|
|
var db = new TestDatabase(DatabaseType.SqlServer2005, sqlSyntax);
|
2016-04-12 19:55:50 +02:00
|
|
|
|
var context = new MigrationContext(db, logger);
|
2017-12-20 11:18:17 +01:00
|
|
|
|
|
2017-12-26 12:54:27 +01:00
|
|
|
|
var createExpression = new CreateIndexExpression(context)
|
2015-09-14 12:01:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
Index = { Name = "IX_A" }
|
|
|
|
|
|
};
|
2017-12-20 11:18:17 +01:00
|
|
|
|
|
|
|
|
|
|
new CreateIndexBuilder(createExpression)
|
|
|
|
|
|
.OnTable("TheTable").OnColumn("A").Ascending().WithOptions().NonClustered()
|
|
|
|
|
|
.Do();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, db.Operations.Count);
|
|
|
|
|
|
Assert.AreEqual("CREATE NONCLUSTERED INDEX [IX_A] ON [TheTable] ([A])", db.Operations[0].Sql);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
|
public void CreateIndexBuilder_SqlServer_Unique_CreatesUniqueNonClusteredIndex()
|
|
|
|
|
|
{
|
2020-09-17 13:36:26 +02:00
|
|
|
|
var logger = Mock.Of<ILogger<MigrationContext>>();
|
2018-11-26 16:54:32 +01:00
|
|
|
|
var sqlSyntax = new SqlServerSyntaxProvider();
|
2017-12-20 11:18:17 +01:00
|
|
|
|
var db = new TestDatabase(DatabaseType.SqlServer2005, sqlSyntax);
|
2016-04-12 19:55:50 +02:00
|
|
|
|
var context = new MigrationContext(db, logger);
|
2017-12-20 11:18:17 +01:00
|
|
|
|
|
2017-12-26 12:54:27 +01:00
|
|
|
|
var createExpression = new CreateIndexExpression(context)
|
2015-09-14 12:01:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
Index = { Name = "IX_A" }
|
|
|
|
|
|
};
|
2017-12-20 11:18:17 +01:00
|
|
|
|
|
|
|
|
|
|
new CreateIndexBuilder(createExpression)
|
|
|
|
|
|
.OnTable("TheTable").OnColumn("A").Ascending().WithOptions().Unique()
|
|
|
|
|
|
.Do();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, db.Operations.Count);
|
|
|
|
|
|
Assert.AreEqual("CREATE UNIQUE NONCLUSTERED INDEX [IX_A] ON [TheTable] ([A])", db.Operations[0].Sql);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void CreateIndexBuilder_SqlServer_Unique_CreatesUniqueNonClusteredIndex_Multi_Columnn()
|
|
|
|
|
|
{
|
2020-09-17 13:36:26 +02:00
|
|
|
|
var logger = Mock.Of<ILogger<MigrationContext>>();
|
2018-11-26 16:54:32 +01:00
|
|
|
|
var sqlSyntax = new SqlServerSyntaxProvider();
|
2017-12-20 11:18:17 +01:00
|
|
|
|
var db = new TestDatabase(DatabaseType.SqlServer2005, sqlSyntax);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
var context = new MigrationContext(db, logger);
|
2017-12-20 11:18:17 +01:00
|
|
|
|
|
2017-12-26 12:54:27 +01:00
|
|
|
|
var createExpression = new CreateIndexExpression(context)
|
2017-05-12 14:49:44 +02:00
|
|
|
|
{
|
|
|
|
|
|
Index = { Name = "IX_AB" }
|
|
|
|
|
|
};
|
2017-12-20 11:18:17 +01:00
|
|
|
|
|
|
|
|
|
|
new CreateIndexBuilder(createExpression)
|
|
|
|
|
|
.OnTable("TheTable").OnColumn("A").Ascending().OnColumn("B").Ascending().WithOptions().Unique()
|
|
|
|
|
|
.Do();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, db.Operations.Count);
|
|
|
|
|
|
Assert.AreEqual("CREATE UNIQUE NONCLUSTERED INDEX [IX_AB] ON [TheTable] ([A],[B])", db.Operations[0].Sql);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-03-14 17:11:21 +11:00
|
|
|
|
[Test]
|
|
|
|
|
|
public void CreateIndexBuilder_SqlServer_Clustered_CreatesClusteredIndex()
|
|
|
|
|
|
{
|
2020-09-17 13:36:26 +02:00
|
|
|
|
var logger = Mock.Of<ILogger<MigrationContext>>();
|
2018-11-26 16:54:32 +01:00
|
|
|
|
var sqlSyntax = new SqlServerSyntaxProvider();
|
2017-12-20 11:18:17 +01:00
|
|
|
|
var db = new TestDatabase(DatabaseType.SqlServer2005, sqlSyntax);
|
2016-04-12 19:55:50 +02:00
|
|
|
|
var context = new MigrationContext(db, logger);
|
2017-12-20 11:18:17 +01:00
|
|
|
|
|
2017-12-26 12:54:27 +01:00
|
|
|
|
var createExpression = new CreateIndexExpression(context)
|
2015-09-14 12:01:48 +02:00
|
|
|
|
{
|
|
|
|
|
|
Index = { Name = "IX_A" }
|
|
|
|
|
|
};
|
2017-12-20 11:18:17 +01:00
|
|
|
|
|
|
|
|
|
|
new CreateIndexBuilder(createExpression)
|
|
|
|
|
|
.OnTable("TheTable").OnColumn("A").Ascending().WithOptions().Clustered()
|
|
|
|
|
|
.Do();
|
|
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, db.Operations.Count);
|
|
|
|
|
|
Assert.AreEqual("CREATE CLUSTERED INDEX [IX_A] ON [TheTable] ([A])", db.Operations[0].Sql);
|
2014-03-14 17:11:21 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static IndexDefinition CreateIndexDefinition()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new IndexDefinition
|
|
|
|
|
|
{
|
|
|
|
|
|
ColumnName = "A",
|
|
|
|
|
|
Name = "IX_A",
|
|
|
|
|
|
TableName = "TheTable",
|
|
|
|
|
|
SchemaName = "dbo"
|
|
|
|
|
|
};
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|
2014-03-14 17:11:21 +11:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|