Beispiel #1
0
        /// <summary>
        ///  插入单个实体 <para>注意:因为Oracle不支持自增列,所以我们需要使用序列(sequence)来实现自增列 </para>
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="entity">要插入的实体</param>
        /// <param name="identity">是否返回自增主键值</param>
        /// <param name="identitySql">返回自增主键sql
        /// <list type="bullet">
        ///     <item>SqlServer: SELECT SCOPE_IDENTITY()</item>
        ///     <item>MySql: SELECT LAST_INSERT_ID()</item>
        ///     <item>Sqlite: SELECT LAST_INSERT_ROWID()</item>
        ///     <item>PostgreSql: RETURNING $PRIMARYKEY,其中$PRIMARYKEY为主键列名占位符</item>
        ///     <item>Oracle: SELECT $SEQUENCE.CURRVAL FROM DUAL,其中$SEQUENCE为自定义SEQUENCE名占位符</item>
        /// </list>
        /// </param>
        /// <returns>若 <paramref name="identity"/>为 true,则返回自增主键值,否则返回受影响行数</returns>
        public override long Insert <T>(T entity, bool identity, string identitySql = null) where T : class
        {
            if (!identity)
            {
                return(Insert(entity));
            }

            identitySql ??= "SELECT SCOPE_IDENTITY()";

            var builder = Sql.Insert <T>(() => entity, DatabaseType, IsEnableNullValue, isEnableFormat: IsEnableFormat);

            return(FindObject($"{builder.Sql};{identitySql.Trim(';')};", builder.DynamicParameters).To <long>());
        }
        /// <summary>
        ///  插入单个实体 <para>注意:因为Oracle不支持自增列,所以我们需要使用序列(sequence)来实现自增列 </para>
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="entity">要插入的实体</param>
        /// <param name="identity">是否返回自增主键值</param>
        /// <param name="identitySql">返回自增主键sql
        /// <list type="bullet">
        ///     <item>SqlServer: SELECT SCOPE_IDENTITY()</item>
        ///     <item>MySql: SELECT LAST_INSERT_ID()</item>
        ///     <item>Sqlite: SELECT LAST_INSERT_ROWID()</item>
        ///     <item>PostgreSql: RETURNING $PRIMARYKEY,其中$PRIMARYKEY为主键列名占位符</item>
        ///     <item>Oracle: SELECT $SEQUENCE.CURRVAL FROM DUAL,其中$SEQUENCE为自定义SEQUENCE名占位符</item>
        /// </list>
        /// </param>
        /// <returns>若 <paramref name="identity"/>为 true,则返回自增主键值,否则返回受影响行数</returns>
        public override async Task <long> InsertAsync <T>(T entity, bool identity, string identitySql = null) where T : class
        {
            if (!identity)
            {
                return(await InsertAsync(entity));
            }

            identitySql ??= "SELECT LAST_INSERT_ID()";

            var builder = Sql.Insert <T>(() => entity, DatabaseType, IsEnableNullValue, isEnableFormat: IsEnableFormat);

            return((await FindObjectAsync($"{builder.Sql};{identitySql.Trim(';')};", builder.DynamicParameters)).To <long>());
        }
        /// <summary>
        ///  插入单个实体 <para>注意:因为Oracle不支持自增列,所以我们需要使用序列(sequence)来实现自增列 </para>
        /// </summary>
        /// <typeparam name="T">泛型类型</typeparam>
        /// <param name="entity">要插入的实体</param>
        /// <param name="identity">是否返回自增主键值</param>
        /// <param name="identitySql">返回自增主键sql
        /// <list type="bullet">
        ///     <item>SqlServer: SELECT SCOPE_IDENTITY()</item>
        ///     <item>MySql: SELECT LAST_INSERT_ID()</item>
        ///     <item>Sqlite: SELECT LAST_INSERT_ROWID()</item>
        ///     <item>PostgreSql: RETURNING $PRIMARYKEY,其中$PRIMARYKEY为主键列名占位符</item>
        ///     <item>Oracle: SELECT $SEQUENCE.CURRVAL FROM DUAL,其中$SEQUENCE为自定义SEQUENCE名占位符</item>
        /// </list>
        /// </param>
        /// <returns>若 <paramref name="identity"/>为 true,则返回自增主键值,否则返回受影响行数</returns>
        public override async Task <long> InsertAsync <T>(T entity, bool identity, string identitySql = null) where T : class
        {
            if (!identity)
            {
                return(await InsertAsync(entity));
            }

            identitySql ??= "RETURNING $PRIMARYKEY";

            var builder = Sql.Insert <T>(() => entity, DatabaseType, IsEnableNullValue, isEnableFormat: IsEnableFormat);

            return((await FindObjectAsync($"{builder.Sql} {identitySql.Trim(';').Replace("$PRIMARYKEY", Sql.GetPrimaryKey<T>().First().ColumnName)}", builder.DynamicParameters)).To <long>());
        }