示例#1
0
        public static int ExecuteWithSqliteIdentity(this SqlInsertContext context, IDbConnection dbConnection)
        {
            var sb = BuildSqliteIdentityQuery(context);

            return(dbConnection
                   .Query <int>(sb.ToString(), context.Parameters)
                   .Single());
        }
        private static StringBuilder BuildPostgreSqlIdentityQuery(SqlInsertContext context, string idName)
        {
            var sb = new StringBuilder();

            sb.AppendLine(context.ToString());
            sb.AppendLine($"SELECT currval(pg_get_serial_sequence('{context.Table.ToLower()}', '{idName.ToLower()}'));");
            return(sb);
        }
示例#3
0
        private static StringBuilder BuildSqliteIdentityQuery(SqlInsertContext context)
        {
            var sb = new StringBuilder();

            sb.AppendLine(context.ToString());
            sb.AppendLine("SELECT last_insert_rowid();");

            return(sb);
        }
示例#4
0
        public static async Task <int> ExecuteWithSqliteIdentityAsync(this SqlInsertContext context, IDbConnection dbConnection)
        {
            var sb = BuildSqliteIdentityQuery(context);

            var task = dbConnection
                       .QueryAsync <int>(sb.ToString(), context.Parameters);

            return((await task).Single());
        }
        public static int ExecuteWithSqliteIdentity(this SqlInsertContext context, IDbConnection dbConnection)
        {
            var sb = new StringBuilder();

            sb.AppendLine(context.ToString());
            sb.AppendLine("SELECT last_insert_rowid();");

            return(dbConnection
                   .Query <int>(sb.ToString(), context.Parameters)
                   .Single());
        }
示例#6
0
        private static StringBuilder BuildSqlIdentityQuery <TIdentityType>(SqlInsertContext context)
        {
            var sb = new StringBuilder();

            if (typeof(TIdentityType) == typeof(int))
            {
                sb.AppendLine(context.ToString());
                sb.AppendLine("SELECT CAST(SCOPE_IDENTITY() AS INT)");
            }
            else if (typeof(TIdentityType) == typeof(long))
            {
                sb.AppendLine(context.ToString());
                sb.AppendLine("SELECT CAST(SCOPE_IDENTITY() AS BIGINT)");
            }
            else
            {
                throw new InvalidCastException($"Type {typeof(TIdentityType).Name} in not supported this SQL context.");
            }

            return(sb);
        }
        public static TIdentityType ExecuteWithSqlIdentity <TIdentityType>(this SqlInsertContext context, IDbConnection dbConnection)
        {
            var sb = new StringBuilder();

            if (typeof(TIdentityType) == typeof(int))
            {
                sb.AppendLine(context.ToString());
                sb.AppendLine("SELECT CAST(SCOPE_IDENTITY() AS INT)");
            }
            else if (typeof(TIdentityType) == typeof(Guid))
            {
                sb.AppendLine($"DECLARE @InsertedRows AS TABLE (Id UNIQUEIDENTIFIER);");
                sb.AppendLine(context.ToString());
                sb.AppendLine($"SELECT Id FROM @InsertedRows");
            }
            else
            {
                throw new InvalidCastException($"Type {typeof(TIdentityType).Name} in not supported this SQL context.");
            }

            return(dbConnection
                   .Query <TIdentityType>(sb.ToString(), context.Parameters)
                   .Single());
        }
示例#8
0
 public static async Task <TIdentityType> ExecuteWithSqlIdentityAsync <TEntityType, TIdentityType>(this SqlInsertContext context, IDbConnection dbConnection, Func <TEntityType, TIdentityType> identityTypeSelector)
     where TEntityType : class
 {
     return(await ExecuteWithSqlIdentityAsync <TIdentityType>(context, dbConnection));
 }
示例#9
0
 public static TIdentityType ExecuteWithSqlIdentity <TEntityType, TIdentityType>(this SqlInsertContext <TEntityType> context, IDbConnection dbConnection, Func <TEntityType, TIdentityType> identityTypeSelector)
     where TEntityType : class
 {
     return(ExecuteWithSqlIdentity <TIdentityType>(context, dbConnection));
 }
        public static async Task <TIdentityType> ExecuteWithPostgreSqlIdentityAsync <TEntityType, TIdentityType>(this SqlInsertContext <TEntityType> context, IDbConnection dbConnection, Expression <Func <TEntityType, TIdentityType> > identityNameSelector)
            where TEntityType : class
        {
            if (identityNameSelector.Body.NodeType != ExpressionType.MemberAccess)
            {
                throw new NotSupportedException("Cannot execute a PostgreSQL identity with an expression of type " + identityNameSelector.Body.NodeType);
            }
            var memberExpression = identityNameSelector.Body as MemberExpression;

            var sb = BuildPostgreSqlIdentityQuery(context, memberExpression.Member.Name.ToLower());

            var result = await dbConnection.QueryAsync <TIdentityType>(sb.ToString(), context.Parameters);

            return(result.Single());
        }