Esempio n. 1
0
        /// <summary>
        ///     Constructs a full insert statement
        /// </summary>
        protected override string ConstructFullInsertStatementInternal()
        {
            if (RefreshOnInsertProperties.Length == 0)
            {
                return
                    (ResolveWithCultureInvariantFormatter(
                         $"INSERT INTO {GetTableName()} ({ConstructColumnEnumerationForInsert()}) VALUES ({ConstructParamEnumerationForInsert()})"));
            }

            // one database generated field to be inserted, and that alone is a the primary key
            if (InsertKeyDatabaseGeneratedProperties.Length == 1 && RefreshOnInsertProperties.Length == 1)
            {
                var keyProperty     = InsertKeyDatabaseGeneratedProperties[0];
                var keyPropertyType = keyProperty.Descriptor.PropertyType;

                if (keyPropertyType == typeof(int) || keyPropertyType == typeof(long))
                {
                    return
                        (ResolveWithCultureInvariantFormatter(
                             $@"INSERT
                                    INTO {GetTableName()} ({ConstructColumnEnumerationForInsert()})
                                    VALUES ({ConstructParamEnumerationForInsert()});
                           SELECT SCOPE_IDENTITY() AS {GetDelimitedIdentifier(keyProperty.PropertyName)}"));
                }
            }

            var dbInsertedOutputColumns = string.Join(",",
                                                      RefreshOnInsertProperties.Select(propInfo => $"inserted.{GetColumnName(propInfo, null, true)}"));
            var dbGeneratedColumns = ConstructRefreshOnInsertColumnSelection();

            // the union will make the constraints be ignored
            return(ResolveWithCultureInvariantFormatter($@"
                SELECT *
                    INTO #temp
                    FROM (SELECT {dbGeneratedColumns} FROM {GetTableName()} WHERE 1=0
                        UNION SELECT {dbGeneratedColumns} FROM {GetTableName()} WHERE 1=0) as u;

                INSERT INTO {GetTableName()} ({ConstructColumnEnumerationForInsert()})
                    OUTPUT {dbInsertedOutputColumns} INTO #temp
                    VALUES ({ConstructParamEnumerationForInsert()});

                SELECT * FROM #temp"));
        }
Esempio n. 2
0
 /// <summary>
 ///     Constructs a column selection of all columns to be refreshed on insert of the form
 ///     <code>@PropertyName1,@PropertyName2...</code>
 /// </summary>
 /// <returns></returns>
 protected virtual string ConstructRefreshOnInsertColumnSelection()
 {
     return(string.Join(",", RefreshOnInsertProperties.Select(propInfo => GetColumnName(propInfo, null, true))));
 }