Inheritance: SqlBuilderBase, IInsertIntoTable, IInsertColumn, IInsertValue
        public void InsertIntoSpecifyingTableName()
        {
            var sqlBuilder = new InsertSqlBuilder(SqlCharacters.Empty);

            var sqlQuery = sqlBuilder
                .Into("Table")
                .ToSqlQuery();

            Assert.Equal("INSERT INTO Table", sqlQuery.CommandText);
            Assert.Empty(sqlQuery.Arguments);
        }
        public void InsertIntoSpecifyingType()
        {
            var sqlBuilder = new InsertSqlBuilder(SqlCharacters.Empty);

            var sqlQuery = sqlBuilder
                .Into(typeof(Customer))
                .ToSqlQuery();

            Assert.Equal("INSERT INTO Sales.Customers", sqlQuery.CommandText);
            Assert.Empty(sqlQuery.Arguments);
        }
        public void InsertIntoColumnsValuesWithSqlCharacters()
        {
            var sqlBuilder = new InsertSqlBuilder(MsSqlCharacters.Instance);

            var sqlQuery = sqlBuilder
                .Into("Table")
                .Columns("Column1", "Column2")
                .Values("Foo", 12)
                .ToSqlQuery();

            Assert.Equal("INSERT INTO [Table] ([Column1],[Column2]) VALUES (@p0,@p1)", sqlQuery.CommandText);
            Assert.Equal(2, sqlQuery.Arguments.Count);
            Assert.Equal("Foo", sqlQuery.Arguments[0]);
            Assert.Equal(12, sqlQuery.Arguments[1]);
        }
        public void InsertIntoColumnsValues()
        {
            var sqlBuilder = new InsertSqlBuilder(SqlCharacters.Empty);

            var sqlQuery = sqlBuilder
                .Into("Table")
                .Columns("Column1", "Column2")
                .Values("Foo", 12)
                .ToSqlQuery();

            Assert.Equal("INSERT INTO Table (Column1,Column2) VALUES (?,?)", sqlQuery.CommandText);
            Assert.Equal(2, sqlQuery.Arguments.Count);
            Assert.Equal("Foo", sqlQuery.Arguments[0]);
            Assert.Equal(12, sqlQuery.Arguments[1]);
        }
Exemple #5
0
        /// <summary>
        /// Builds the command text to insert a database record for the specified <see cref="IObjectInfo"/>.
        /// </summary>
        /// <param name="objectInfo">The object information.</param>
        /// <returns>
        /// The created command text.
        /// </returns>
        protected virtual string BuildInsertCommandText(IObjectInfo objectInfo)
        {
            var counter = 0;
            var insertColumns = new string[objectInfo.TableInfo.InsertColumnCount];

            for (int i = 0; i < objectInfo.TableInfo.Columns.Count; i++)
            {
                var columnInfo = objectInfo.TableInfo.Columns[i];

                if (columnInfo.AllowInsert)
                {
                    insertColumns[counter++] = columnInfo.ColumnName;
                }
            }

            var insertSqlQuery = new InsertSqlBuilder(this.SqlCharacters)
                .Into(objectInfo)
                .Columns(insertColumns)
                .Values(new object[objectInfo.TableInfo.InsertColumnCount])
                .ToSqlQuery();

            return insertSqlQuery.CommandText;
        }
        public void InsertIntoSpecifyingTypeWithSqlCharacters()
        {
            var sqlBuilder = new InsertSqlBuilder(MsSqlCharacters.Instance);

            var sqlQuery = sqlBuilder
                .Into(typeof(Customer))
                .ToSqlQuery();

            Assert.Equal("INSERT INTO [Sales].[Customers]", sqlQuery.CommandText);
            Assert.Empty(sqlQuery.Arguments);
        }