Exemplo n.º 1
0
 public DapperConnection GetConnection()
 {
     var connection = Provider.CreateConnection();
     DapperConnection conn = new DapperConnection();
     conn.Base = connection;
     conn.Base.ConnectionString = _connectionString;
     conn.SqlDbType = SqlType.Npgsql;
     conn.SqlProvider = SqlAdapter.Provider.ProviderFactory.GetProvider(conn.SqlDbType);
     return conn;
 }
Exemplo n.º 2
0
        private static string BuildUpdateSql <T>(DapperConnection connection, T data, List <string> columns = null, List <string> noColumns = null)
        {
            var    t = typeof(T);
            string sql;

            if (columns == null && noColumns == null)
            {
                if (updateSqlDict.TryGetValue(t.FullName, out sql))
                {
                    return(sql);
                }
            }
            var       tb        = TableInfo.FromType(t);
            IProvider _provider = connection.SqlProvider;
            object    primaryValue;
            var       sb = new StringBuilder("UPDATE ");

            sb.Append(_provider.EscapeTableName(tb.TableName)).Append(" SET ");
            if (columns == null)
            {
                for (int i = 0; i < tb.Columns.Count; i++)
                {
                    var column = tb.Columns[i];
                    if (noColumns.Contains(column.Name))
                    {
                        continue;
                    }
                    if (column.Name.Equals(tb.PrimaryColumn.Name))
                    {
                        primaryValue = column.GetValue(data);
                        if (tb.AutoIncrement)
                        {
                            continue;
                        }
                    }
                    if (column.IsResult)
                    {
                        continue;
                    }
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    _provider.AppendColumnNameEqualsValue(sb, column.Name);
                }
            }
            else
            {
                for (int i = 0; i < columns.Count; i++)
                {
                    var column = columns[i];
                    if (i > 0)
                    {
                        sb.Append(", ");
                    }
                    _provider.AppendColumnNameEqualsValue(sb, column);
                }
            }
            sb.Append(" WHERE ");
            _provider.AppendColumnNameEqualsValue(sb, tb.PrimaryColumn.Name);
            sql = sb.ToString();
            if (columns == null && noColumns == null)
            {
                updateSqlDict.TryAdd(t.FullName, sql);
            }
            return(sql);
        }
Exemplo n.º 3
0
 public static async Task <int> UpdateAsync <T>(this DapperConnection connection, T data, List <string> columns = null, List <string> noColumns = null, IDbTransaction transaction = null)
 {
     return(await connection.Base.ExecuteAsync(BuildUpdateSql(connection, data, columns, noColumns), data, transaction));
 }
Exemplo n.º 4
0
 public static int Update <T>(this DapperConnection connection, T data, List <string> columns = null, List <string> noColumns = null, IDbTransaction transaction = null)
 {
     return(connection.Base.Execute(BuildUpdateSql(connection, data, columns, noColumns), data, transaction));
 }
Exemplo n.º 5
0
        public static async Task <Page <T> > GetAllPageAsync <T>(this DapperConnection connection, long page, long pageSize, bool keyAsc = true)
        {
            var sql = BuildAllSql <T>(connection, keyAsc);

            return(await connection.QueryPageAsync <T>(page, pageSize, sql));
        }
Exemplo n.º 6
0
        public static async Task <IEnumerable <T> > GetAllAsync <T>(this DapperConnection connection, bool keyAsc = true)
        {
            var sql = BuildAllSql <T>(connection, keyAsc);

            return(await connection.Base.QueryAsync <T>(sql));
        }
Exemplo n.º 7
0
        public static bool Delete <T>(this DapperConnection connection, object primaryKey, IDbTransaction transaction = null)
        {
            var sqlPara = BuildDeleteSql <T>(connection, primaryKey);

            return(connection.Base.Execute(sqlPara.Item1, sqlPara.Item2, transaction) > 0);
        }
Exemplo n.º 8
0
        public static IEnumerable <T> GetAll <T>(this DapperConnection connection, bool keyAsc = true)
        {
            var sql = BuildAllSql <T>(connection, keyAsc);

            return(connection.Base.Query <T>(sql));
        }
Exemplo n.º 9
0
        public static async Task <T> SingleOrDefaultAsync <T>(this DapperConnection connection, object primaryKey)
        {
            var sqlPara = BuildSingleSql <T>(connection, primaryKey);

            return(await connection.Base.QuerySingleOrDefaultAsync <T>(sqlPara.Item1, sqlPara.Item2));
        }
Exemplo n.º 10
0
        public static T Single <T>(this DapperConnection connection, object primaryKey)
        {
            var sqlPara = BuildSingleSql <T>(connection, primaryKey);

            return(connection.Base.QuerySingle <T>(sqlPara.Item1, sqlPara.Item2));
        }