Skip to content

Commit

Permalink
Default to double quote for generic SQL instead of empty string, add …
Browse files Browse the repository at this point in the history
…Column(..) attribute when required, include verification and simple test case for field names starting in digits.
  • Loading branch information
tenbaset committed Dec 31, 2016
1 parent 09c255e commit e128ed4
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 31 deletions.
Binary file modified Dapper.FastCrud.Tests/App_Data/EntityGenDatabase.mdf
Binary file not shown.
Binary file modified Dapper.FastCrud.Tests/App_Data/EntityGenDatabase_log.ldf
Binary file not shown.
12 changes: 6 additions & 6 deletions Dapper.FastCrud.Tests/CrudSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public void WhenIInsertBuildingEntities(int entitiesCount, bool makeAsyncCalls)
[When(@"I insert (\d*) workstation entities using (.*) methods")]
public void WhenIInsertWorkstationEntities(int entitiesCount, bool makeAsyncCalls)
{
this.InsertEntity<Workstation>(makeAsyncCalls, index => new Workstation()
{
this.InsertEntity<Workstation>(makeAsyncCalls, index => new Workstation() {
InventoryIndex = index,
Name = $"Workstation {Guid.NewGuid().ToString("N")}"
Name = $"Workstation {Guid.NewGuid().ToString("N")}",
_10PinSlots = index
},
entitiesCount);
}
Expand Down Expand Up @@ -209,7 +209,7 @@ public void WhenIQueryForTheCountOfAllTheWorkstationEntitiesUsingAsynchronous(bo
public void WhenIQueryForTheCountOfAllTheInsertedBuildingEntitiesUsingAsynchronous(bool useAsyncMethods)
{
FormattableString whereClause = $"charindex(',' + cast({nameof(Building.BuildingId):C} as varchar(10)) + ',', ',' + @BuildingIds + ',') > 0";
var buildingIds = string.Join(",", _testContext.LocalInsertedEntities.OfType<Building>().Select(building => building.BuildingId));
var buildingIds = string.Join(",", _testContext.LocalInsertedEntities.OfType<Building>().Select(building => building.BuildingId));

_testContext.QueriedEntitiesDbCount = useAsyncMethods
? _testContext
Expand All @@ -229,7 +229,7 @@ public void WhenIQueryForTheCountOfAllTheInsertedBuildingEntitiesUsingAsynchrono
[When(@"I query for the count of all the workstation entities combined with the employee entities using (.*) methods")]
public void WhenIQueryForTheCountOfAllTheWorkstationEntitiesCombinedWithTheEmployeeEntitiesUsingMethods(bool useAsyncMethods)
{
_testContext.QueriedEntitiesDbCount = useAsyncMethods
_testContext.QueriedEntitiesDbCount = useAsyncMethods
? _testContext.DatabaseConnection.CountAsync<Workstation>(statement => statement.Include<Employee>()).GetAwaiter().GetResult()
: _testContext.DatabaseConnection.Count<Workstation>(statement => statement.Include<Employee>());
}
Expand Down Expand Up @@ -274,7 +274,7 @@ public void WhenIUpdateAllTheInsertedEmployeeEntities(bool useAsyncMethods)
WorkstationId = originalEmployeeEntity.WorkstationId,
FirstName = "Updated " + originalEmployeeEntity.FirstName,
LastName = "Updated " + originalEmployeeEntity.LastName,
KeyPass = originalEmployeeEntity.KeyPass,
KeyPass = originalEmployeeEntity.KeyPass,
});
}

Expand Down
51 changes: 28 additions & 23 deletions Dapper.FastCrud.Tests/DatabaseSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ private void SetupSqLiteDatabase(string connectionString)
InventoryIndex int NOT NULL,
Name nvarchar(100) NULL,
AccessLevel int NOT NULL DEFAULT(1),
BuildingId int NULL
BuildingId int NULL,
""10PinSlots"" int NULL
)";

command.ExecuteNonQuery();
Expand Down Expand Up @@ -375,6 +376,7 @@ FOR EACH ROW
""InventoryIndex"" int NOT NULL,
""AccessLevel"" int NOT NULL DEFAULT 1,
""BuildingId"" int NULL,
""10PinSlots"" int NULL,
PRIMARY KEY (""WorkstationId"")
);
Expand Down Expand Up @@ -451,6 +453,7 @@ Name nvarchar(100) NOT NULL,
InventoryIndex int NOT NULL,
AccessLevel int NOT NULL DEFAULT 1,
BuildingId int NULL,
10PinSlots int NULL,
PRIMARY KEY (WorkstationId)
);
Expand Down Expand Up @@ -499,12 +502,12 @@ private void SetupMsSqlDatabase(string connectionString)
}
catch (FailedOperationException ex)
{
// sometimes db test files are left behind
// sometimes db test files are left behind

var fileInUseErrorMatch =
new Regex("Cannot create file '(.*?)' because it already exists.").Match(
((ex.InnerException as ExecutionFailureException)?.InnerException as SqlException)?.Message ?? string.Empty);
if (fileInUseErrorMatch.Success)
var fileInUseErrorMatch =
new Regex("Cannot create file '(.*?)' because it already exists.").Match(
((ex.InnerException as ExecutionFailureException)?.InnerException as SqlException)?.Message ?? string.Empty);
if (fileInUseErrorMatch.Success)
{
var dbFilePath = fileInUseErrorMatch.Groups[1].Value;
var dbLogFilePath = Path.Combine(Path.GetDirectoryName(dbFilePath), $"{Path.GetFileNameWithoutExtension(dbFilePath)}_log.ldf");
Expand All @@ -523,12 +526,12 @@ private void SetupMsSqlDatabase(string connectionString)
else
{
throw;
}
}
}

database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET SINGLE_USER"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET COMPATIBILITY_LEVEL = 110"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET RECOVERY BULK_LOGGED"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET SINGLE_USER"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET COMPATIBILITY_LEVEL = 110"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET RECOVERY BULK_LOGGED"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET AUTO_CREATE_STATISTICS OFF"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET AUTO_UPDATE_STATISTICS OFF"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} MODIFY FILE(NAME=[{DatabaseName}], SIZE=100MB, FILEGROWTH=10%)"); // for benchmarking purposes 5MB approx 20k records
Expand All @@ -538,7 +541,7 @@ [Id] [int] IDENTITY(2,1) NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NOT NULL,
[DateOfBirth] [datetime] NULL,
CONSTRAINT [PK_SimpleBenchmarkEntities] PRIMARY KEY CLUSTERED
CONSTRAINT [PK_SimpleBenchmarkEntities] PRIMARY KEY CLUSTERED
(
[Id] ASC
))");
Expand All @@ -549,7 +552,9 @@ [Name] [nvarchar](100) NULL,
[InventoryIndex] [int] NOT NULL,
[AccessLevel] [int] NOT NULL DEFAULT(1),
[BuildingId] [int] NULL,
CONSTRAINT [PK_Workstations] PRIMARY KEY CLUSTERED
-- Verify that column names that begin with numbers will work
[10PinSlots] [int] NULL,
CONSTRAINT [PK_Workstations] PRIMARY KEY CLUSTERED
(
[WorkstationId] ASC
))");
Expand All @@ -563,7 +568,7 @@ [FirstName] [nvarchar](100) NULL,
[BirthDate] [datetime] NOT NULL,
[WorkstationId] [bigint] NULL,
[FullName] AS ([FirstName] + [LastName]),
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[Id] ASC,
[EmployeeId] ASC
Expand All @@ -574,15 +579,15 @@ [EmployeeId] ASC
[Id] [int] IDENTITY(2,1) NOT NULL,
[BuildingName] [nvarchar](100) NULL,
[Description] [nvarchar](100) NULL,
CONSTRAINT [PK_Buildings] PRIMARY KEY CLUSTERED
CONSTRAINT [PK_Buildings] PRIMARY KEY CLUSTERED
(
[Id] ASC
))");

database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET MULTI_USER"); // for benchmarking purposes
database.ExecuteNonQuery($@"ALTER DATABASE {DatabaseName} SET MULTI_USER"); // for benchmarking purposes

// no longer required. local db instances are destroyed and re-created
//database.ExecuteNonQuery(@"CHECKPOINT;");
//database.ExecuteNonQuery(@"CHECKPOINT;");
//database.ExecuteNonQuery(@"DBCC DROPCLEANBUFFERS;");
//database.ExecuteNonQuery(@"DBCC FREESYSTEMCACHE('ALL') WITH NO_INFOMSGS;");
//database.ExecuteNonQuery(@"DBCC FREESESSIONCACHE WITH NO_INFOMSGS;");
Expand All @@ -608,9 +613,9 @@ private void CleanupSqlCeDatabase(string connectionString)
private void CleanupLocalDbDatabase()
{
SqlLocalDbApi.AutomaticallyDeleteInstanceFiles = true;
SqlLocalDbApi.StopOptions=StopInstanceOptions.KillProcess;
SqlLocalDbApi.StopOptions = StopInstanceOptions.KillProcess;

var localDbProvider = new SqlLocalDbProvider();
var localDbProvider = new SqlLocalDbProvider();
var localDbInstanceInfo = localDbProvider.GetInstances().FirstOrDefault(instance => instance.Name==DatabaseName);
if (localDbInstanceInfo != null)
{
Expand All @@ -619,11 +624,11 @@ private void CleanupLocalDbDatabase()
{
localDbInstance.Start();
}
this.CleanupMsSqlDatabase(localDbInstance.CreateConnectionStringBuilder().ConnectionString);
SqlLocalDbApi.StopInstance(DatabaseName,TimeSpan.FromSeconds(20.0));
SqlLocalDbApi.DeleteInstance(DatabaseName);
}
}
this.CleanupMsSqlDatabase(localDbInstance.CreateConnectionStringBuilder().ConnectionString);
SqlLocalDbApi.StopInstance(DatabaseName, TimeSpan.FromSeconds(20.0));
SqlLocalDbApi.DeleteInstance(DatabaseName);
}
}

private void CleanupMsSqlDatabase(string connectionString)
{
Expand Down
7 changes: 7 additions & 0 deletions Dapper.FastCrud.Tests/Models/ModelGeneratorConfig1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public partial class CrazyColumn
/// <summary>
/// Test starting with a digit
/// </summary>
[Column("123mb")]
public virtual int _123mb { get; set; }
/// <summary>
/// Test starting with a C# keyword
/// </summary>
[Column("switch")]
public virtual int @switch { get; set; }
}

Expand Down Expand Up @@ -110,6 +112,11 @@ public partial class Workstation
/// InventoryIndex Column
/// </summary>
public virtual int InventoryIndex { get; set; }
/// <summary>
/// 10PinSlots Column
/// </summary>
[Column("10PinSlots")]
public virtual int? _10PinSlots { get; set; }
public virtual IEnumerable<Employee> Employees { get; set; }
}

Expand Down
3 changes: 2 additions & 1 deletion Dapper.FastCrud/Configuration/SqlDatabaseOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class SqlDatabaseOptions
/// </summary>
public SqlDatabaseOptions()
{
this.StartDelimiter = this.EndDelimiter = string.Empty;
// The ANSI standard delimeter is a double quote mark
this.StartDelimiter = this.EndDelimiter = "\"";
this.ParameterPrefix = "@";
}

Expand Down
5 changes: 4 additions & 1 deletion NuGetSpecs/GenericModelGenerator.tt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ foreach(Table tbl in tables.Where(t=> !t.Ignore)){
if (col.IsPK) { #>
[Key]
<#}
if (col.Name != col.PropertyName) { #>
[Column("<#=col.Name#>")]
<#}
if(col.IsComputed) { #>
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
<#}
Expand Down Expand Up @@ -194,7 +197,7 @@ static Func<string, string> CleanUp = (str) =>

if (cs_keywords.Contains(str))
str = "@" + str;

return str;
};

Expand Down

0 comments on commit e128ed4

Please sign in to comment.