Esempio n. 1
0
        public int Insert(Type itemType, object item, string tableName)
        {
            InsertCommandBuilder inserter = InsertCommandBuilder.Create(itemType);
            DbCommand            command  = inserter.Build(item, tableName, _database, Connection, Transaction);

            command.ExecuteNonQuery();
            return((int)inserter.GetKey(command));
        }
Esempio n. 2
0
        public void  Test()
        {
            DBConnection         connection = TestSettings.CreateConnection();
            InsertCommandBuilder builder    = new InsertCommandBuilder(connection);

            builder.TableName = "Table1";
            builder.AddParameter("value", "1");
            IDbCommand command = builder.Build();

            Assert.IsNotNull(command);
            Assert.AreEqual(1, connection.ExecuteNonQuery(command));
        }
Esempio n. 3
0
        /// <summary>
        /// Creates the insert command.
        /// </summary>
        /// <returns></returns>
        private void CreateInsertCommand()
        {
            var builder = new InsertCommandBuilder(_connection.Factory);

            builder.TableName = TableName;

            foreach (PropertyInfo field in _fields)
            {
                builder.AddParameter(field.Name, field.PropertyType);
            }


            _insertCommand = builder.Build();
            //insertCommand.Prepare();
        }
Esempio n. 4
0
    public void Build_Generates_Command_With_Output()
    {
        // Arrange
        var input = new InsertCommandBuilder().Into("MyTable")
                    .AddFieldNames(new[] { "Field1", "Field2", "Field3" }.AsEnumerable())
                    .AddFieldValues(new[] { "\"Value1\"", "\"Value2\"", "\"Value3\"" }.AsEnumerable())
                    .AddOutputFields(new[] { "INSERTED.Field1", "INSERTED.Field2", "INSERTED.Field3" }.AsEnumerable());

        // Act
        var actual = input.Build();

        // Assert
        actual.Operation.Should().Be(DatabaseOperation.Insert);
        actual.CommandText.Should().Be("INSERT INTO MyTable(Field1, Field2, Field3) OUTPUT INSERTED.Field1, INSERTED.Field2, INSERTED.Field3 VALUES(\"Value1\", \"Value2\", \"Value3\")");
    }
        public void AddParameter()
        {
            InsertCommandBuilder builder = new InsertCommandBuilder(TestSettings.CreateConnection());

            builder.TableName = "Table1";
            builder.AddParameter("p1", DbType.Date);
            builder.AddParameter("p2", typeof(DateTime));
            builder.AddParameter("p3", 1);
            builder.AddParameter("p4", "par4", true);
            IDbCommand command = builder.Build();

            Assert.AreEqual(command.CommandType, CommandType.Text);
            Assert.IsNotNull(command.Parameters["@p1"]);
            Assert.IsNotNull(command.Parameters["@p2"]);
            Assert.IsNotNull(command.Parameters["@p3"]);
            Assert.IsNotNull(command.Parameters["@par4"]);
        }
Esempio n. 6
0
        private SendParameter PreparePrimitive(Type parameterType)
        {
            var builder = new CreateTableCommandBuilder(_connection.Factory)
            {
                TableName = TableName
            };

            builder.AddColumn("value", parameterType);
            _connection.ExecuteNonQuery(builder.Build());

            var builderInsert = new InsertCommandBuilder(_connection.Factory)
            {
                TableName = TableName
            };

            builderInsert.AddParameter("value", parameterType);
            _insertCommand = builderInsert.Build();
            //insertCommand.Prepare();

            return(SendPrimitive);
        }
Esempio n. 7
0
    public void Build_Generates_Command_With_Output_And_Into()
    {
        // Arrange
        var input = new InsertCommandBuilder().Into("MyTable")
                    .AddFieldNames("Field1", "Field2", "Field3")
                    .AddFieldValues("\"Value1\"", "\"Value2\"", "\"Value3\"")
                    .AddOutputFields("INSERTED.Field1", "INSERTED.Field2", "INSERTED.Field3")
                    .Into("MyTable")
                    .WithTemporaryTable("@NewValues");

        // Act
        var actual = input.Build();

        // Assert
        actual.Operation.Should().Be(DatabaseOperation.Insert);
        actual.CommandText.Should().Be("INSERT INTO MyTable(Field1, Field2, Field3) OUTPUT INSERTED.Field1, INSERTED.Field2, INSERTED.Field3 INTO @NewValues VALUES(\"Value1\", \"Value2\", \"Value3\")");
        actual.CommandParameters.Should().BeAssignableTo <IDictionary <string, object> >();
        var parameters = actual.CommandParameters as IDictionary <string, object>;

        parameters.Should().BeEmpty();
    }
Esempio n. 8
0
    public void Build_Generates_Command_With_Parameters()
    {
        // Arrange
        var input = new InsertCommandBuilder().Into("MyTable")
                    .AddFieldNames("Field1", "Field2", "Field3")
                    .AddFieldValues("@Field1", "@Field2", "@Field3")
                    .AppendParameters(new { Field1 = "Value1", Field2 = "Value2", Field3 = "Value3" });

        // Act
        var actual = input.Build();

        // Assert
        actual.Operation.Should().Be(DatabaseOperation.Insert);
        actual.CommandText.Should().Be("INSERT INTO MyTable(Field1, Field2, Field3) VALUES(@Field1, @Field2, @Field3)");
        actual.CommandParameters.Should().BeAssignableTo <IDictionary <string, object> >();
        var parameters = actual.CommandParameters as IDictionary <string, object>;

        if (parameters != null)
        {
            parameters.Should().HaveCount(3);
            parameters.Keys.Should().BeEquivalentTo(new[] { "Field1", "Field2", "Field3" });
            parameters.Values.Should().BeEquivalentTo(new[] { "Value1", "Value2", "Value3" });
        }
    }