コード例 #1
0
        public void Intercept(IInvocation invocation)
        {
            if (count > 0)
            {
                // 嵌套调用
                invocation.Proceed();
                return;
            }

            count++;
            IDbTransaction transaction = null;
            string         DBName      = "";

            object[] attrs = invocation.TargetType.GetCustomAttributes(typeof(DBSourceAttribute), false);
            if (attrs != null && attrs.Length > 0)
            {
                DBName = (attrs[0] as DBSourceAttribute).name;
            }

            //short dbType = 0;
            short dbType = ConfigCache.GetDBType(DBName);

            // 从类中指定,
            // 从方法中指定
            if (connectString == null)
            {
                //connectString = System.Configuration.ConfigurationManager.ConnectionStrings["CONNECTION_STRING"].ConnectionString;
                connectString = ConfigCache.GetDBConnectStr(DBName);

                //connectString = "Data Source=172.30.1.65:1522//xe;;user id=etl;password=etl;";
                //connectString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=Virgil-Chen)(PORT=1522)))(CONNECT_DATA=(SERVICE_NAME=XE)));User Id=czjd_sd;Password=czjd_sd;";
                //connectString = "Data Source=Virgil-PC:1522//xe;;user id=czjd_sd;password=czjd_sd;";
                //connectString = "server=localhost;User Id=root;database=sakila;Password=root;";
            }

            using (IDbConnection connection = DBUtil.GetConnection(dbType, connectString))
            {
                try
                {
                    connection.Open();

                    string methodName = invocation.MethodInvocationTarget.Name.ToLower();
                    if (!(methodName.StartsWith("get") ||
                          methodName.StartsWith("find") ||
                          methodName.StartsWith("query") ||
                          methodName.StartsWith("list")))
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("BeginTransaction");
                        }
                        transaction = connection.BeginTransaction();
                    }

                    new TransactionContext(connection, transaction, dbType);

                    invocation.Proceed();

                    if (transaction != null)
                    {
                        transaction.Commit();
                    }
                }
                catch (Exception e)
                {
                    if (transaction != null)
                    {
                        transaction.Rollback();
                    }

                    log.Error(e.Message, e);

                    throw e;
                }
                finally
                {
                    if (log.IsDebugEnabled)
                    {
                        log.Debug("connection.Close()");
                    }

                    if (connection != null && ConnectionState.Open == connection.State)
                    {
                        connection.Close();
                    }
                    count--;
                }
            }
        }
コード例 #2
0
ファイル: Object2SQL.cs プロジェクト: zero1036/TGNet
        public void parse(object entity)
        {
            this.SetDBType();

            this.varPrefix = DBUtil.GetDBParamFlag(this.dbType);

            IDictionary <string, object> entityDict = entity as IDictionary <string, object>;

            output = new StringBuilder();

            if (entityDict == null || entityDict.Count == 0)
            {
                return;
            }
            output.Append(" where 1=1");

            this._entity = entity;


            //获得实体的属性集合
            ICollection <string> props = entityDict.Keys;

            this.ParameterNames  = new List <String>(props.Count);
            this.ParameterValues = new List <Object>(props.Count);

            foreach (string prop in props)
            {
                output.Append(" and ");

                string[] keyWithOper = prop.Split('$');
                string   colName     = keyWithOper[0];
                object   colValue    = entityDict[prop];

                if (null == colValue)
                {
                    continue;
                }

                string operatorName            = "=";
                ICollection <object> colValues = this.Conver2ObjectCollection(colValue);
                bool isArray = (colValues != null);// colValue is ICollection<object>;
                if (keyWithOper.Length == 1)
                {
                    operatorName = isArray ? "in" : "=";
                }
                else
                {
                    operatorName = keyWithOper[1];
                }


                if (isArray)
                {
                    if (colValues.Count != 0)
                    {
                        output.Append(colName).Append(" ").Append(operatorName).Append("(");

                        int i = 0;
                        foreach (object colValueI in colValues)
                        {
                            if (i != 0)
                            {
                                output.Append(", ");
                            }
                            string colNameI = colName + "_" + (i++);
                            output.Append(this.varPrefix).Append(colNameI);
                            this.ParameterNames.Add(colNameI);
                            this.ParameterValues.Add(colValueI);
                        }

                        output.Append(")");
                    }
                }
                else
                {
                    output.Append(colName).Append(operatorName);

                    output.Append(this.varPrefix).Append(colName);
                    this.ParameterNames.Add(colName);
                    this.ParameterValues.Add(colValue);
                }
            }
        }