Example #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);
        }
Example #2
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);
        }
Example #3
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));
        }
Example #4
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);
        }
Example #5
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)));
        }
Example #6
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);
        }
Example #7
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);
        }
Example #8
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);
            }
        }
Example #9
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));
        }
Example #10
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);
            }
        }
Example #11
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);
        }
Example #12
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);
 }
Example #13
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));
        }
Example #14
0
 public DBTableAdapterContext(IResolverProvider resolverProvider, DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
     : this(DBTableMapping <T> .Get(resolverProvider, currentConnection, currentTrans))
 {
     ResolverProvider = resolverProvider;
 }
Example #15
0
 public abstract IDBTableAdapter <T> GetDBTableAdapter <T>(DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans) where T : new();
Example #16
0
 public override IDBTableAdapter <T> GetDBTableAdapter <T>(DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
 {
     return(DBTableAdapter <T> .Get(this, _CommandBuilderFactory, currentConnection, currentTrans));
 }
Example #17
0
 public virtual int Insert(ref T obj, DBConnectionWrapper conn, DBTransactionWrapper trans = null)
 {
     return(InsertCommand.Execute(ref obj, conn, trans));
 }
Example #18
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));
        }
Example #19
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));
        }
Example #20
0
 public static IDbCommand CreateCommand(this DBConnectionWrapper conn, Sql sql, DBTransactionWrapper trans = null)
 {
     return(conn.ResolverProvider.DBClassResolver.CreateCommand(sql, conn, trans));
 }
Example #21
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);
        }
Example #22
0
 private DBTableAdapter(IResolverProvider resolverProvider, ICommandBuilder <T> commandBuilder, DBConnectionWrapper currentConnection, DBTransactionWrapper currentTrans)
     : base(resolverProvider, commandBuilder, currentConnection, currentTrans)
 {
 }
Example #23
0
 public DBTransactionWrapper(IDbTransaction transaction, DBConnectionWrapper connection)
 {
     Connection  = connection;
     Transaction = transaction;
 }