Esempio n. 1
0
        public DataItem getRow(Command cmd, DbTran transaction)
        {
            DataItem row = new DataItem();

            try {
                reader = query(cmd, transaction);
                if (reader.Read())
                {
                    int len = reader.FieldCount;

                    for (int i = 0; i < len; i++)
                    {
                        row.set(reader.GetName(i), reader[i]);
                    }
                }

                if (row.count() > 0)
                {
                    return(row);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex) {
                WeedConfig.logException(cmd, ex);
                throw ex;
            }
            finally {
                tryClose();
            }
        }
Esempio n. 2
0
        public T getItem <T>(T model, Command cmd, DbTran transaction) where T : class, IBinder
        {
            //T model = BinderMapping.getBinder<T>();
            try {
                reader = query(cmd, transaction);
                if (reader.Read())
                {
                    model.bind((key) => {
                        try {
                            return(new Variate(key, reader[key]));
                        } catch (Exception ex) {
                            WeedConfig.logException(cmd, ex);
                            return(new Variate(key, null));
                        }
                    });

                    return(model);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex) {
                WeedConfig.logException(cmd, ex);
                throw ex;
            }
            finally {
                tryClose();
            }
        }
Esempio n. 3
0
        public List <T> getList <T>(T model, Command cmd, DbTran transaction) where T : IBinder
        {
            List <T> list = new List <T>();

            try {
                reader = query(cmd, transaction);
                while (reader.Read())
                {
                    model.bind((key) => {
                        return(new Variate(key, reader[key]));
                    });

                    list.Add(model);
                    model = (T)model.clone();
                }

                if (list.Count > 0)
                {
                    return(list);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex) {
                WeedConfig.logException(cmd, ex);
                throw ex;
            }
            finally {
                tryClose();
            }
        }
Esempio n. 4
0
        private static void tast_db3_tran(DbTranQueue queue)
        {
            //使用了 .await(true) 将不提交事务(交由上一层控制)
            //
            DbTran tran = new DbTran(DbConfig.pc_live);

            tran.join(queue).execute((t) => {
                t.db().sql("insert into $.test(txt) values(?)", "xx").tran(t).execute();

                throw new Exception("xxxx");
            });
        }
Esempio n. 5
0
        //------------------
        private static void tast_db1_tran(DbTranQueue queue)
        {
            //使用了 .await(true) 将不提交事务(交由上一层控制)
            //
            DbTran tran = new DbTran(DbConfig.pc_user);

            tran.join(queue).execute((t) => {
                t.db().sql("insert into $.test(txt) values(?)", "cc").tran(t).execute();
                t.db().sql("insert into $.test(txt) values(?)", "dd").tran(t).execute();
                t.db().sql("insert into $.test(txt) values(?)", "ee").tran(t).execute();

                t.result = t.db().sql("select name from $.user_info where user_id=3").tran(t).getValue("");
            });
        }
Esempio n. 6
0
        private void doRollback() //从尾向头回滚
        {
            int len = queue.Count;

            for (int i = len - 1; i > -1; i--)
            {
                DbTran tran = get(i);

                try {
                    tran.rollback(true);
                } catch (Exception ex) {
                    WeedConfig.logException(null, ex);
                }
            }
        }
Esempio n. 7
0
        //查询
        private DbReader query(Command cmd, DbTran transaction)
        {
            if (false == buildCMD(cmd, (transaction == null ? null : transaction.connection), false))
            {
                return(null);
            }

            //3.执行
            DbReader rst = new DbReader(stmt.ExecuteReader()); //stmt.executeQuery();

            //*.监听
            WeedConfig.logExecuteAft(cmd);

            return(rst);
        }
Esempio n. 8
0
        //查询
        private DbReader query(Command cmd, DbTran transaction)
        {
            DbCommand dbcmd = null;

            if (transaction == null)
            {
                dbcmd = buildCMD(cmd, null, false);
            }
            else
            {
                dbcmd = buildCMD(cmd, transaction.connection, false);
            }

            //3.执行
            return(new DbReader(dbcmd.ExecuteReader()));//stmt.executeQuery();
        }
Esempio n. 9
0
        public List <T> getList <T>(T model, Command cmd, DbTran transaction) where T : IBinder
        {
            List <T> list = new List <T>();

            try {
                reader = query(cmd, transaction);
                while (reader.Read())
                {
                    T item = (T)model.clone();

                    if (WeedConfig.isDebug)
                    {
                        if (item is T)
                        {
                            throw new WeedException(model.GetType() + " clone error(" + item.GetType() + ")");
                        }
                    }

                    item.bind((key) => {
                        try {
                            return(new Variate(key, reader[key]));
                        } catch (Exception ex) {
                            WeedConfig.logException(cmd, ex);
                            return(new Variate(key, null));
                        }
                    });

                    list.Add(item);
                }

                if (list.Count > 0)
                {
                    return(list);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ex) {
                WeedConfig.logException(cmd, ex);
                throw ex;
            }
            finally {
                tryClose();
            }
        }
Esempio n. 10
0
        public long insert(Command cmd, DbTran transaction)
        {
            try {
                if (false == buildCMD(cmd, (transaction == null ? null : transaction.connection), true))
                {
                    return(-1);
                }

                long rst = 0;
                if (stmt.CommandText.IndexOf("@@IDENTITY") > 0)
                {
                    var obj = stmt.ExecuteScalar();

                    if (obj is ulong)
                    {
                        rst = (long)((ulong)obj);
                    }
                    else if (obj is long)
                    {
                        rst = (long)obj;
                    }
                    else
                    {
                        rst = 0;
                    }
                }
                else
                {
                    rst = stmt.ExecuteNonQuery();
                }

                //*.监听
                WeedConfig.logExecuteAft(cmd);

                return(rst);
            }
            catch (Exception ex) {
                WeedConfig.logException(cmd, ex);
                throw ex;
            }
            finally {
                tryClose();
            }
        }
Esempio n. 11
0
        public long insert(Command cmd, DbTran transaction)
        {
            try {
                DbCommand dbcmd = null;
                if (transaction == null)
                {
                    dbcmd = buildCMD(cmd, null, true);
                }
                else
                {
                    dbcmd = buildCMD(cmd, transaction.connection, true);
                }

                if (dbcmd.CommandText.IndexOf("@@IDENTITY") > 0)
                {
                    var obj = dbcmd.ExecuteScalar();

                    if (obj is ulong)
                    {
                        return((long)((ulong)obj));
                    }
                    else if (obj is long)
                    {
                        return((long)obj);
                    }
                    else
                    {
                        return(0);
                    }
                }
                else
                {
                    return(dbcmd.ExecuteNonQuery());
                }
            }
            catch (Exception ex) {
                WeedConfig.logException(cmd, ex);
                throw ex;
            }
            finally {
                tryClose();
            }
        }
Esempio n. 12
0
 public Variate getVariate(Command cmd, DbTran transaction)
 {
     try {
         reader = query(cmd, transaction);
         if (reader.Read())
         {
             return(new Variate(null, reader[0])); //也可能从1开始
         }
         else
         {
             return(null);//new Variate(null, null);
         }
     }
     catch (Exception ex) {
         WeedConfig.logException(cmd, ex);
         throw ex;
     }
     finally {
         tryClose();
     }
 }
Esempio n. 13
0
        //执行
        public int execute(Command cmd, DbTran transaction)
        {
            try {
                if (false == buildCMD(cmd, (transaction == null ? null : transaction.connection), false))
                {
                    return(-1);
                }

                int rst = stmt.ExecuteNonQuery();

                //*.监听
                WeedConfig.logExecuteAft(cmd);

                return(rst);
            }
            catch (Exception ex) {
                WeedConfig.logException(cmd, ex);
                throw ex;
            }
            finally {
                tryClose();
            }
        }
Esempio n. 14
0
        //执行
        public int execute(Command cmd, DbTran transaction)
        {
            try {
                DbCommand dbcmd = null;
                if (transaction == null)
                {
                    dbcmd = buildCMD(cmd, null, false);
                }
                else
                {
                    dbcmd = buildCMD(cmd, transaction.connection, false);
                }

                return(dbcmd.ExecuteNonQuery());
            }
            catch (Exception ex) {
                WeedConfig.logException(cmd, ex);
                throw ex;
            }
            finally {
                tryClose();
            }
        }
Esempio n. 15
0
 internal void add(DbTran tran)
 {
     queue.Add(tran);
 }
Esempio n. 16
0
 public DbTableQueryBase <T> tran()
 {
     _tran = _context.tran();
     return((T)this);
 }
Esempio n. 17
0
 public DbTableQueryBase <T> tran(DbTran transaction)
 {
     _tran = transaction;
     return((T)this);
 }
Esempio n. 18
0
 internal void add(DbTran tran)
 {
     queue.Add(tran);
 }
Esempio n. 19
0
 public X tran(DbTran transaction)
 {
     _tran = transaction;
     return((X)this);
 }