Ejemplo n.º 1
0
        /// <summary>
        /// Underline it calls Command.Execute and return the number of row effected. No "returning"(in Oracle) or "output"(in SqlServer) parameter involves.
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="conn"></param>
        /// <param name="trans"></param>
        /// <returns></returns>
        public virtual int Execute(ref T obj, DBConnectionWrapper conn, DBTransactionWrapper trans = null)
        {
            var sql = CreateSql(obj);
            int ret = conn.Execute(sql, false, trans);

            return(ret);
        }
Ejemplo n.º 2
0
        public virtual int Delete(T obj, DBConnectionWrapper conn, DBTransactionWrapper trans = null)
        {
            if (DeleteCommand == null)
            {
                throw new NotSupportedException("Delete is not support for this [" + typeof(T).FullName + "], make sure you have primary key defined in Entity Metadata for this type.");
            }

            return(DeleteCommand.Execute(ref obj, conn, trans));
        }
Ejemplo n.º 3
0
        public override int Execute(ref T obj, DBConnectionWrapper conn, DBTransactionWrapper trans = null)
        {
            var sql = CreateSql(obj);
            var ret = conn.Query <T>(sql, trans).FirstOrDefault();

            if (ret != null)
            {
                obj = ret;
                return(1);
            }
            return(0);
        }
Ejemplo n.º 4
0
        public virtual T Load(T obj, DBConnectionWrapper conn, DBTransactionWrapper trans = null)
        {
            if (LoadCommand == null)
            {
                throw new NotSupportedException("Load is not support for this [" + typeof(T).FullName + "], make sure you have primary key defined in Entity Metadata for this type.");
            }
            int cnt = LoadCommand.Execute(ref obj, conn, trans);

            if (cnt == 0)
            {
                return(default(T));
            }
            return(obj);
        }
Ejemplo n.º 5
0
        public static T GetSingleValue <T>(this DBConnectionWrapper conn, Sql sql, DBTransactionWrapper trans = null)
        {
            IDbCommand command = conn.CreateCommand(sql, trans);

            try
            {
                object val = command.ExecuteScalar();
                return(ParseFromDBValue <T>(val));
            }
            catch (DbException ex)
            {
                //throw;
                throw new SqlCmdException("Error occurred when running SQL!", sql, ex);
            }
        }
Ejemplo n.º 6
0
        public virtual int Update(ref T obj, DBConnectionWrapper conn, DBTransactionWrapper trans = null, params Expression <Func <T, object> >[] targetProperties)
        {
            if (UpdateCommand == null)
            {
                throw new NotSupportedException("Update is not support for this [" + typeof(T).FullName + "], make sure you have primary key defined in Entity Metadata for this type.");
            }

            IDBTableAdapterCommand <T> cmd = UpdateCommand;

            if (targetProperties.Length > 0)
            {
                cmd = CreateUpdateCommand(targetProperties);
            }
            return(cmd.Execute(ref obj, conn, trans));
        }
Ejemplo n.º 7
0
        public static List <T> Query <T>(this DBConnectionWrapper conn, Sql sql, DBTransactionWrapper trans = null) where T : new()
        {
            IDbCommand command = conn.CreateCommand(sql, trans);

            try
            {
                using (IDataReader reader = command.ExecuteReader())
                {
                    return(reader.ToList <T>());
                }
            }
            catch (DbException ex)
            {
                //throw;
                throw new SqlCmdException("Error occurred when running SQL!", sql, ex);
            }
        }
Ejemplo n.º 8
0
 public static T Load <T>(this DBTransactionWrapper trans, T obj) where T : new()
 {
     return(trans.Connection.Load <T>(obj, trans));
 }
Ejemplo n.º 9
0
 public DBTableAdapterBase(IResolverProvider resolverProvider, ICommandBuilder <T> commandBuilder, DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
     : base(resolverProvider, currentConnection, currentTrans)
 {
     CommandBuilder = commandBuilder;
     InsertCommand  = commandBuilder.CreateInsertCommand(this);
     UpdateCommand  = commandBuilder.CreateUpdateCommand(this);
     DeleteCommand  = commandBuilder.CreateDeleteCommand(this);
     LoadCommand    = commandBuilder.CreateLoadCommand(this);
 }
Ejemplo n.º 10
0
 public static int Delete <T>(this DBTransactionWrapper trans, T obj) where T : new()
 {
     return(trans.Connection.Delete <T>(obj, trans));
 }
Ejemplo n.º 11
0
 public static List <T> GetFirstColumn <T>(this DBTransactionWrapper trans, Sql sql)
 {
     return(trans.Connection.GetFirstColumn <T>(sql, trans));
 }
Ejemplo n.º 12
0
 public static int Update <T>(this DBTransactionWrapper trans, ref T obj, params Expression <Func <T, object> >[] targetProperties) where T : new()
 {
     return(trans.Connection.Update <T>(ref obj, trans, targetProperties));
 }
Ejemplo n.º 13
0
 public override IDBTableAdapter <T> GetDBTableAdapter <T>(DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
 {
     return(DBTableAdapter <T> .Get(this, _CommandBuilderFactory, currentConnection, currentTrans));
 }
Ejemplo n.º 14
0
 public abstract IDBTableAdapter <T> GetDBTableAdapter <T>(DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans) where T : new();
Ejemplo n.º 15
0
        public static int Insert <T>(this DBConnectionWrapper conn, ref T obj, DBTransactionWrapper trans = null) where T : new()
        {
            var ta = conn.ResolverProvider.GetDBTableAdapter <T>(conn, trans);

            return(ta.Insert(ref obj, conn, trans));
        }
Ejemplo n.º 16
0
        public static T Load <T>(this DBConnectionWrapper conn, T obj, DBTransactionWrapper trans = null) where T : new()
        {
            var ta = conn.ResolverProvider.GetDBTableAdapter <T>(conn, trans);

            return(ta.Load(obj, conn, trans));
        }
Ejemplo n.º 17
0
 public static IDbCommand CreateCommand(this DBConnectionWrapper conn, Sql sql, DBTransactionWrapper trans = null)
 {
     return(conn.ResolverProvider.DBClassResolver.CreateCommand(sql, conn, trans));
 }
Ejemplo n.º 18
0
 public static List <T> Query <T>(this DBTransactionWrapper trans, Sql sql) where T : new()
 {
     return(trans.Connection.Query <T>(sql, trans));
 }
Ejemplo n.º 19
0
 public static int Execute(this DBTransactionWrapper trans, Sql sql, bool storeProcedure = false)
 {
     return(trans.Connection.Execute(sql, storeProcedure, trans));
 }
Ejemplo n.º 20
0
        public static DBTableMapping <T> Get(IResolverProvider resolverProvider, DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
        {
            var ret = _Cache[resolverProvider.Idx];

            if (ret == null)
            {
                return(_Cache[resolverProvider.Idx] = new DBTableMapping <T>(resolverProvider, currentConnection, currentTrans));
            }
            return(ret);
        }
Ejemplo n.º 21
0
 public virtual int Insert(ref T obj, DBConnectionWrapper conn, DBTransactionWrapper trans = null)
 {
     return(InsertCommand.Execute(ref obj, conn, trans));
 }
Ejemplo n.º 22
0
        public static IDBTableAdapter <T> Get(IResolverProvider resolverProvider, CommandBuilderFactoryBase commandBuilderFactory, DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
        {
            var ret = _Cache[resolverProvider.Idx];

            if (ret == null)
            {
                var commandBuilder = commandBuilderFactory.CreateCommandBuilder <T>(resolverProvider.SqlResolver);
                return(_Cache[resolverProvider.Idx] = new DBTableAdapter <T>(resolverProvider, commandBuilder, currentConnection, currentTrans));
            }
            return(ret);
        }
Ejemplo n.º 23
0
        public static int Update <T>(this DBConnectionWrapper conn, ref T obj, DBTransactionWrapper trans = null, params Expression <Func <T, object> >[] targetProperties) where T : new()
        {
            var ta = conn.ResolverProvider.GetDBTableAdapter <T>(conn, trans);

            return(ta.Update(ref obj, conn, trans, targetProperties));
        }
Ejemplo n.º 24
0
 public static int Insert <T>(this DBTransactionWrapper trans, ref T obj) where T : new()
 {
     return(trans.Connection.Insert <T>(ref obj, trans));
 }
Ejemplo n.º 25
0
 public DBTableAdapterContext(IResolverProvider resolverProvider, DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
     : this(DBTableMapping <T> .Get(resolverProvider, currentConnection, currentTrans))
 {
     ResolverProvider = resolverProvider;
 }
Ejemplo n.º 26
0
        public static List <T> GetFirstColumn <T>(this DBConnectionWrapper conn, Sql sql, DBTransactionWrapper trans = null)
        {
            IDbCommand command = conn.CreateCommand(sql, trans);

            var mapping = DBObjectMapping <SingleColumn <T> > .Get().ColumnMappingList.Single();

            List <T> ret = new List <T>();

            try
            {
                using (IDataReader reader = command.ExecuteReader())
                {
                    SingleColumn <T> obj = new SingleColumn <T>();

                    Action <SingleColumn <T>, IDataRecord, int> setter = null;
                    if (reader.Read())
                    {
                        setter = mapping.GetPropSetterForRecord(reader.GetFieldType(0));
                        reader.SetProperty(obj, mapping, setter, 0);
                        ret.Add(obj.Val);
                    }

                    while (reader.Read())
                    {
                        reader.SetProperty(obj, mapping, setter, 0);
                        ret.Add(obj.Val);
                    }
                }
            }
            catch (DbException ex)
            {
                //throw;
                throw new SqlCmdException("Error occurred when running SQL!", sql, ex);
            }
            return(ret);
        }
Ejemplo n.º 27
0
        private DBTableMapping(IResolverProvider resolverProvider, DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
        {
            var mapping = DBObjectMapping <T> .Get();

            Sql sql = resolverProvider.SqlResolver.ColumnMetaDataFromTable(TableName, Schema);
            List <SchemaMetadata> metadatas = currentConnection.Query <SchemaMetadata>(sql, currentTrans);

            ColumnMappingList = mapping.ColumnMappingList
                                .Select(i => new DBColumnMappingInfo <T>(i, metadatas.Single(j => string.Compare(j.COLUMN_NAME, i.ColumnName, true) == 0)));
        }
Ejemplo n.º 28
0
        public static int Execute(this DBConnectionWrapper conn, Sql sql, bool storeProcedure = false, DBTransactionWrapper trans = null)
        {
            IDbCommand command = conn.CreateCommand(sql, trans);

            if (storeProcedure)
            {
                command.CommandType = CommandType.StoredProcedure;
            }

            int ret = 0;

            try
            {
                ret = command.ExecuteNonQuery();
                for (int i = 0; i < sql.Parameters.Count; i++)
                {
                    var p = sql.Parameters[i];
                    if (p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Output || p.Direction == ParameterDirection.ReturnValue)
                    {
                        var val = ((DbParameter)command.Parameters[p.Name]).Value;
                        p.Output = (val == DBNull.Value) ? null : val;
                    }
                }
            }
            catch (DbException ex)
            {
                //throw;
                throw new SqlCmdException("Error occurred when running SQL!", sql, ex);
            }

            return(ret);
        }
Ejemplo n.º 29
0
 private DBTableAdapter(IResolverProvider resolverProvider, ICommandBuilder <T> commandBuilder, DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
     : base(resolverProvider, commandBuilder, currentConnection, currentTrans)
 {
 }
Ejemplo n.º 30
0
 public static T GetSingleValue <T>(this DBTransactionWrapper trans, Sql sql)
 {
     return(trans.Connection.GetSingleValue <T>(sql, trans));
 }