public void Insert_Into_Test() { var properties = new List <string> { "Name", "Created", "CompanyName", "OptOut" }; var select = new Select { Projection = properties.Select(x => new SelectProjection { Projection = Projection.Create.Field(x, x == "CompanyName" || x == "OptOut") }).ToList(), From = { Type = Data.DataType.Table, Table = new Table { Name = TableName2 } } }; var insert = new Insert { Type = Insert.SetType.Query, Into = new Table { Name = TableName1 }, Query = select }; var statement = InsertWriter <IdentityEntity> .CreateStatement(insert, IdentityMap); statement.Result.ShouldEqual(Statement.ResultType.None); statement.Parameters.Count.ShouldEqual(0); statement.Text.ShouldEqual(string.Format("INSERT INTO [{0}] ([name], [created], [companyname], [optout]) SELECT [name], [created], [companyname], [optout] FROM [{1}] {2}", TableName1, TableName2, select.From.Alias)); }
public void Insert_Guid_Entity_Test() { var assignment = new Dictionary <string, object> { { "id", Guid.NewGuid() }, { "name", "bob" }, { "created", DateTime.MaxValue }, { "companyname", "Dunder Miflin" }, { "optout", true } }; var insert = new Insert { HasIdentityKey = false, Type = Insert.SetType.Values, Values = assignment, Into = new Table { Name = TableName1 } }; var statement = InsertWriter <GuidEntity> .CreateStatement(insert, GuidMap); statement.Result.ShouldEqual(Statement.ResultType.None); statement.Parameters.Count.ShouldEqual(5); statement.Parameters.First().Value.ShouldNotEqual(Guid.Empty); statement.Parameters.Skip(1).First().Value.ShouldEqual("bob"); statement.Parameters.Skip(2).First().Value.ShouldEqual(DateTime.MaxValue); statement.Parameters.Skip(3).First().Value.ShouldEqual("Dunder Miflin"); statement.Parameters.Skip(4).First().Value.ShouldEqual(true); statement.Text.ShouldEqual( string.Format("INSERT INTO [{0}] ([id], [name], [created], [companyname], [optout]) VALUES (@{1}, @{2}, @{3}, @{4}, @{5})", TableName1, statement.Parameters.First().Key, statement.Parameters.Skip(1).First().Key, statement.Parameters.Skip(2).First().Key, statement.Parameters.Skip(3).First().Key, statement.Parameters.Skip(4).First().Key)); }
public void Insert_Identity_Entity_Test() { var assignment = new Dictionary <string, object> { { "name", "bob" }, { "created", DateTime.MaxValue }, { "companyname", "Dunder Miflin" }, { "optout", true } }; var insert = new Insert { HasIdentityKey = true, Type = Insert.SetType.Values, Values = assignment, Into = new Table { Name = TableName1 } }; var statement = InsertWriter <IdentityEntity> .CreateStatement(insert, IdentityMap); statement.Result.ShouldEqual(Statement.ResultType.Scalar); statement.Parameters.Count.ShouldEqual(4); statement.Parameters.First().Value.ShouldEqual("bob"); statement.Parameters.Skip(1).First().Value.ShouldEqual(DateTime.MaxValue); statement.Parameters.Skip(2).First().Value.ShouldEqual("Dunder Miflin"); statement.Parameters.Skip(3).First().Value.ShouldEqual(true); statement.Text.ShouldEqual( string.Format("INSERT INTO [{0}] ([name], [created], [companyname], [optout]) VALUES (@{1}, @{2}, @{3}, @{4}); SELECT CAST(SCOPE_IDENTITY() AS int)", TableName1, statement.Parameters.First().Key, statement.Parameters.Skip(1).First().Key, statement.Parameters.Skip(2).First().Key, statement.Parameters.Skip(3).First().Key)); }
private IQueryable <TEntity> CopyInto(Insert insert) { insert.Query.Projection = GetSharedColumns(insert.Query, insert.Into); var statement = InsertWriter <TEntity> .CreateStatement(insert, _map); Command.Create(statement, _profiler).ExecuteNonQuery(_connectionManager); return(new Table <TEntity>(_connectionManager, insert.Into.Name, _map, _profiler, _noLock)); }
public void Insert(TEntity entity) { var adapter = new EntityAdapter <TEntity>(entity, _map); var hasIdentityKey = _map.Key.KeyType == PrimaryKeyType.Integer && _map.Key.KeyGeneration == PrimaryKeyGeneration.Server; var keyColumnName = _map.Key.GetColumnName(); if (_map.Key.KeyType == PrimaryKeyType.Guid && _map.Key.KeyGeneration == PrimaryKeyGeneration.Client) { adapter.Key = GuidComb.Create(); } var values = adapter.GetValues().Where(x => !hasIdentityKey || (x.Key != keyColumnName)).ToDictionary(x => x.Key, x => x.Value); var insert = new Insert { HasIdentityKey = hasIdentityKey, Type = Model.Insert.SetType.Values, Into = new Table { Name = _table }, Values = values }; var command = Command.Create(InsertWriter <TEntity> .CreateStatement(insert, _map), _profiler); if (command.Statement.Result == Statement.ResultType.None) { command.ExecuteNonQuery(_connectionManager); } else { adapter.Key = command.ExecuteScalar(_connectionManager); } }