Beispiel #1
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();
            }
        }
Beispiel #2
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();
            }
        }
Beispiel #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();
            }
        }
Beispiel #4
0
 private void close()
 {
     foreach (DbTran tran in queue) //从头到关闭(关闭时,不能影响其它事务)
     {
         try {
             tran.close(true);
         } catch (Exception ex) {
             WeedConfig.logException(null, ex);
         }
     }
 }
Beispiel #5
0
 private void tryClose()
 {
     try { if (reader != null)
           {
               reader.Close(); reader = null;
           }
     } catch (Exception ex) { WeedConfig.logException(null, ex); };
     try { if (conn != null)
           {
               conn.Close(); conn = null;
           }
     } catch (Exception ex) { WeedConfig.logException(null, ex); };
 }
Beispiel #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);
                }
            }
        }
Beispiel #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);
        }
Beispiel #8
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();
            }
        }
Beispiel #9
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();
            }
        }
Beispiel #10
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();
            }
        }
Beispiel #11
0
        private bool buildCMD(Command cmd, DbConnection c, bool isInsert)
        {
            //*.监听
            if (WeedConfig.logExecuteBef(cmd) == false)
            {
                return(false);
            }

            //1.构建连接和命令(外部的c不能给conn)
            if (c == null)
            {
                c = conn = cmd.context.getConnection();
                c.Open();
            }

            stmt             = c.CreateCommand();
            stmt.CommandText = cmd.text;
            if (cmd.text.IndexOf(' ') < 0) //没有空隔的是存储过程
            {
                stmt.CommandType = System.Data.CommandType.StoredProcedure;
            }
            else
            {
                stmt.CommandText = cmd.text;
            }

            if (cmd.paramS != null)
            {
                //2.设置参数值
                foreach (Variate p in cmd.paramS)
                {
                    var pm = stmt.CreateParameter();
                    pm.ParameterName = p.getName();
                    pm.Value         = p.getValue();
                    pm.DbType        = p.getType();
                    stmt.Parameters.Add(pm);
                }
            }

            return(true);
        }
Beispiel #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();
     }
 }
Beispiel #13
0
        private DbCommand buildCMD(Command cmd, DbConnection c, bool isInsert)
        {
            //0.监听
            WeedConfig.logExecute(cmd);

            //1.构建连接和命令(外部的c不能给conn)
            if (c == null)
            {
                c = conn = cmd.context.getConnection();
                c.Open();
            }

            DbCommand dbcmd = c.CreateCommand();

            dbcmd.CommandText = cmd.text;
            if (cmd.text.IndexOf(' ') < 0) //没有空隔的是存储过程
            {
                dbcmd.CommandType = System.Data.CommandType.StoredProcedure;
            }
            else
            {
                dbcmd.CommandText = cmd.text;
            }

            if (cmd.paramS != null)
            {
                //2.设置参数值
                foreach (Variate p in cmd.paramS)
                {
                    var pm = dbcmd.CreateParameter();
                    pm.ParameterName = p.getName();
                    pm.Value         = p.getValue();
                    pm.DbType        = p.getType();
                    dbcmd.Parameters.Add(pm);
                }
            }

            return(dbcmd);
        }
Beispiel #14
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();
            }
        }
Beispiel #15
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();
            }
        }
Beispiel #16
0
        protected override Command getCommand()
        {
            Command cmd = new Command(this.context);

            cmd.key    = getCommandID();
            cmd.paramS = this.paramS;

            StringBuilder sb = new StringBuilder(commandText);

            //1.替换schema
            {
                int idx = 0;
                while (true)
                {
                    idx = sb.IndexOf(DataFlagConfig.dbNameFlag[0], idx);
                    if (idx > 0)
                    {
                        sb.Replace(idx, idx + 1, context.getSchema());
                        idx++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (this.paramS.Count > 0)
            {
                int paIdx = 0; //参数位置
                int atIdx = 0; //? in sb位置//到此时已不再有?...(之前已转换为?)


                while (true)
                {
                    atIdx = sb.IndexOf(DataFlagConfig.paramReplaceFlag[0], atIdx);
                    if (atIdx > 0)
                    {
                        Variate temp = doGet(paIdx);
                        if (temp == null)
                        {
                            WeedConfig.logException(cmd, new WeedException("缺少参数"));
                        }

                        temp.setName("p" + paIdx);
                        sb.Replace(DataFlagConfig.paramReplaceFlag, DataFlagConfig.parameterHeadFlag + temp.getName(), atIdx, 1);

                        //atIdx+= strVal.Length;//增加替换后的长度
                        atIdx += temp.getName().Length + 1;
                        paIdx++;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            cmd.text = sb.ToString();

            logCommandBuilt(cmd);

            return(cmd);
        }