Ejemplo n.º 1
0
        /// <summary>
        /// 初始化SQL构造中间变量
        /// </summary>
        /// <param name="commonObject">通用中间对象</param>
        /// <param name="coNode">节点</param>
        /// <param name="dataObject">数据对象</param>
        /// <param name="currentDbTable">当前操作对应的数据库表</param>
        /// <returns>SQL构造中间变量</returns>
        protected SqlBuildingInfo InitSqlBuildingInfo(DomainModel.Spi.DomainModel commonObject, DomainObject coNode, DataObject dataObject, string tableName, string dataSourceName)
        {
            //构造SqlBuildingInfo
            var sqlInfo = new SqlBuildingInfo();

            sqlInfo.CommonObject      = commonObject;
            sqlInfo.RootNode          = commonObject.RootDomainObject;
            sqlInfo.CurrentNode       = coNode;
            sqlInfo.CurrentDataObject = dataObject;
            sqlInfo.TableName         = tableName;
            sqlInfo.DataSource        = dataSourceName;
            sqlInfo.CurrentSqlTable   = new SqlTable(sqlInfo.TableName, sqlInfo.TableName, sqlInfo.TableName);

            //SqlTable注册处理
            RegistSqlTable(sqlInfo.CurrentSqlTable.TableName, sqlInfo.CurrentSqlTable, sqlInfo);

            //如果当前节点是根节点:
            if (sqlInfo.RootNode.ID == sqlInfo.CurrentNode.ID)
            {
                sqlInfo.RootDataObject = dataObject;
                sqlInfo.RootSqlTable   = sqlInfo.CurrentSqlTable;
            }
            else
            {
                sqlInfo.RootDataObject = commonObject.RootDomainObject.DataObject;

                //sqlInfo.RootSqlTable = GetSubQuerySql(commonObject, commonObject.RootNode);
            }

            //SqlTable再次注册处理
            RegistSqlTable(sqlInfo.CurrentSqlTable.TableName, sqlInfo.CurrentSqlTable, sqlInfo);

            return(sqlInfo);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取CO上某GSPNode的GSPAbsDBTable主键的过滤条件。
        /// </summary>
        /// <param name="context">SQL构造上下文信息。</param>
        /// <param name="sqlInfo">SQL拼装的中间变量。</param>
        /// <param name="coNode">主键过滤条件对应的GSPNode。</param>
        /// <param name="dbTable">主键过滤条件对应的GSPAbsDBTable。</param>
        /// <param name="IsUseFieldPrefix">是否使用表名</param>
        /// <returns>主键的过滤条件。</returns>
        protected string GetPrimaryKeyCondition(SqlBuildingContext context, SqlBuildingInfo sqlInfo,
                                                DomainObject coNode, bool IsUseFieldPrefix = false)
        {
            var tableName     = context.TableName;
            var tableKey      = tableName;
            var sqlPrimaryKey = new SqlPrimaryKey();
            var sqlTable      = this.TryFindAndRegistSqlTable(tableKey, tableName, tableName, tableName, sqlInfo);
            var pkColumnData  = context.DataContext.GetCurrentDataContextItem(context.Node.ID).PrimaryKeyData;

            foreach (DataColumn item in context.DataObject.PKColumns)
            {
                var pkElement = context.Node.Elements.FirstOrDefault(i => i.DataColumnID == item.ID);
                if (pkElement == null)
                {
                    throw new Exception("Cannot find PkElement, Column ID: " + item.ID);
                }

                if (pkColumnData.ContainsKey(pkElement.ID))
                {
                    var keyField = new SqlPrimaryKeyField();
                    keyField.Table            = sqlTable;
                    keyField.FieldName        = item.ColumnName;
                    keyField.Value.Value      = pkColumnData[pkElement.ID];
                    keyField.IsUseFieldPrefix = IsUseFieldPrefix;
                    sqlPrimaryKey.ChildCollection.Add(keyField);
                }
            }

            return(sqlPrimaryKey.ToSQL());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 构造主键的过滤条件。
        /// </summary>
        /// <param name="context">SQL构造上下文信息。</param>
        /// <param name="sqlInfo">SQL拼装的中间变量。</param>
        /// <param name="currentObject">主键过滤条件对应的领域对象</param>
        /// <param name="pkColumnData">主键值Dictionary,Key是ElemetID,Value是列的值。</param>
        /// <param name="IsUseFieldPrefix">是否使用列全名。</param>
        /// <returns>主键过滤条件</returns>
        protected SqlPrimaryKey GetPrimaryKeyConditions(SqlBuildingContext context, SqlBuildingInfo sqlInfo,
                                                        DomainObject currentObject, IDictionary <string, object> data, bool IsUseFieldPrefix = false)
        {
            var      curDataObject = currentObject.DataObject;
            var      tableName     = context.DataObjectTableMapping[curDataObject.ID];
            SqlTable curTable      = this.FindSqlTable(tableName, sqlInfo);

            if (curTable == null)
            {
                curTable = new SqlTable(tableName, tableName, tableName);
                this.RegistSqlTable(tableName, curTable, sqlInfo);
            }

            //var data = context.DataContext.GetCurrentDataContextItem(currentObject.ID);
            var primaryKey = new SqlPrimaryKey();

            foreach (var column in curDataObject.PKColumns)
            {
                var keyField  = new SqlPrimaryKeyField(curTable, column.ColumnName);
                var pkElement = currentObject.Elements.FirstOrDefault(i => i.DataColumnID == column.ID);
                keyField.Value.Value      = data[pkElement.ID];
                keyField.IsUseFieldPrefix = true;
                primaryKey.ChildCollection.Add(keyField);
            }

            return(primaryKey);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 初始化SQL构造中间变量
        /// </summary>
        /// <param name="commonObject">通用中间对象</param>
        /// <param name="coNode">节点</param>
        /// <param name="dataObject">数据对象</param>
        /// <param name="currentDbTable">当前操作对应的数据库表</param>
        /// <returns>SQL构造中间变量</returns>
        protected SqlBuildingInfo InitSqlBuildingInfoForSelect(DomainModel.Spi.DomainModel commonObject, DomainObject coNode, DataObject dataObject, string tableName)
        {
            var sqlInfo = new SqlBuildingInfo()
            {
                CommonObject      = commonObject,
                RootNode          = commonObject.RootDomainObject,
                CurrentNode       = coNode,
                CurrentDataObject = dataObject,
                TableName         = tableName
            };

            sqlInfo.CurrentSqlTable = new SqlTable(sqlInfo.TableName, sqlInfo.TableName, sqlInfo.TableName);

            //如果当前节点是根节点:
            if (sqlInfo.CurrentNode.IsRootObject || sqlInfo.RootNode.ID == sqlInfo.CurrentNode.ID)
            {
                sqlInfo.RootDataObject = dataObject;
                sqlInfo.RootSqlTable   = sqlInfo.CurrentSqlTable;
            }
            else
            {
                sqlInfo.RootDataObject = commonObject.RootDomainObject.DataObject;
                //sqlInfo.RootSqlTable = CoHelper.GetRootSQLDomTable(commonObject);
            }

            RegistSqlTable(sqlInfo.CurrentDataObject.ID, sqlInfo.CurrentSqlTable, sqlInfo);

            return(sqlInfo);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 获取SQL中的表
 /// </summary>
 /// <param name="tableKey">表的标识 -- 主从关系ID、或者关联关系ID</param>
 /// <param name="sqlInfo">SQL构造信息</param>
 /// <returns>Sql中的表</returns>
 protected SqlTable FindSqlTable(string tableKey, SqlBuildingInfo sqlInfo)
 {
     if (String.IsNullOrWhiteSpace(tableKey))
     {
         throw new ArgumentNullException("FindSqlTable.tableKey");
     }
     if (sqlInfo.SqlTableCollection.Contains(tableKey))
     {
         return((SqlTable)sqlInfo.SqlTableCollection[tableKey]);
     }
     return(null);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// 克隆
        /// </summary>
        /// <returns>SQL语句类副本</returns>
        public override object Clone()
        {
            SqlStatement sqlStatement = base.Clone() as SqlStatement;

            if (PrimaryKeys != null)
            {
                sqlStatement.PrimaryKeys = PrimaryKeys.Clone() as SqlPrimaryKey;
            }

            sqlStatement.SqlBuildingInfo = SqlBuildingInfo.Clone() as SqlBuildingInfo;

            return(sqlStatement);
        }
Ejemplo n.º 7
0
        public object Clone()
        {
            SqlBuildingInfo newInfo = base.MemberwiseClone() as SqlBuildingInfo;

            newInfo.SqlTableCollection = new Hashtable();
            foreach (DictionaryEntry item in SqlTableCollection)
            {
                newInfo.SqlTableCollection.Add(item.Key, item.Value);
            }

            newInfo.SqlTableAliasCollection = new Hashtable();
            foreach (DictionaryEntry item in SqlTableAliasCollection)
            {
                newInfo.SqlTableAliasCollection.Add(item.Key, item.Value);
            }

            return(newInfo);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 获取表别名
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="sqlInfo"></param>
        /// <returns>考虑到在表关联的时候同一张表,被关联多次,所以需要给表生成别名</returns>
        protected string GetSqlTableAlias(string tableName, SqlBuildingInfo sqlInfo)
        {
            if (string.IsNullOrWhiteSpace(tableName))
            {
                throw new ArgumentNullException("GetSqlTableAlias.tableName");
            }

            Hashtable htAlias = sqlInfo.SqlTableAliasCollection;

            if (!htAlias.Contains(tableName))
            {
                htAlias.Add(tableName, 0);
                return(tableName);
            }
            int index = Convert.ToInt32(htAlias[tableName]) + 1;

            htAlias[tableName] = index;
            return(this.GetSqlTableAlias(tableName + index.ToString(), sqlInfo));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 注册SQL中的表,并根据情况更改表别名
        /// </summary>
        /// <param name="tableKey">表唯一标识</param>
        /// <param name="table">描述SQL语句的表</param>
        /// <param name="sqlInfo"></param>
        protected void RegistSqlTable(string tableKey, SqlTable table, SqlBuildingInfo sqlInfo)
        {
            if (string.IsNullOrWhiteSpace(tableKey))
            {
                throw new ArgumentNullException("RegistSqlTable.tableKey");
            }
            if (table == null)
            {
                throw new ArgumentNullException("RegistSqlTable.SqlTable");
            }

            Hashtable htTable = sqlInfo.SqlTableCollection;

            if (!sqlInfo.SqlTableCollection.Contains(tableKey))
            {
                table.TableAlias = this.GetSqlTableAlias(table.TableAlias, sqlInfo);
                htTable.Add(tableKey, table);
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 构造跟主表的关联。
        /// </summary>
        /// <param name="currentObject">当前的数据模型对象。</param>
        /// <param name="sqlBuildInfo">SQL构造的中间变量。</param>
        /// <param name="querySql">删除过滤子查询SQL。</param>
        /// <remarks>
        /// 当前对象若是从对象,则需要建立跟主对象的关联,
        /// 然后根据主对象的唯一标识,形成删除的Sql。
        /// </remarks>
        private void BuildParentInnerJoin(DomainObject currentObject, SqlBuildingInfo sqlBuildInfo, SelectSqlForSubQuery querySql, SqlBuildingContext context)
        {
            //如果是根节点,退出
            if (currentObject.IsRootObject || currentObject.ID == sqlBuildInfo.CommonObject.RootDomainObjectID)
            {
                return;
            }

            #region 子节点(当前节点)

            var      currentDataObject = currentObject.DataObject;
            var      tableName         = context.GetTableName(currentDataObject.ID);
            SqlTable currentTable      = base.FindSqlTable(tableName, sqlBuildInfo);
            if (currentTable == null)
            {
                currentTable = new SqlTable(tableName, tableName, tableName);
                base.RegistSqlTable(tableName, currentTable, sqlBuildInfo);
            }

            #endregion

            #region 父节点

            var      parentObjecct    = currentObject.ParentObject;
            var      parentDataObject = parentObjecct.DataObject;
            var      parentTableName  = context.GetTableName(parentDataObject.ID);
            SqlTable parentTable      = base.FindSqlTable(parentTableName, sqlBuildInfo);
            if (parentTable == null)
            {
                parentTable = new SqlTable(parentTableName, parentTableName, parentTableName);
                base.RegistSqlTable(parentTableName, parentTable, sqlBuildInfo);
            }

            FromItem parentFromItem = new FromItem(parentTable);
            querySql.From.ChildCollection.Add(parentFromItem);

            #endregion

            #region 关联关系

            //目前只是支持单个主对象结构,因此应该只有一个主从关联
            var association = currentObject.Associations.FirstOrDefault(i => i.AssociateType == AssociateType.InnerJoin);
            foreach (var item in association.Items)
            {
                //主从关联中,源节点在子节点中,目标节点在父节点上
                var sourceElement = currentObject.Elements.FirstOrDefault(i => i.ID == item.SourceElementID);
                var targetElement = parentObjecct.Elements.FirstOrDefault(i => i.ID == item.TargetElementID);
                var childCol      = currentDataObject.Columns.FirstOrDefault(i => i.ID == sourceElement.ID);
                var parentCol     = parentDataObject.Columns.FirstOrDefault(i => i.ID == targetElement.ID);

                JoinConditionItem joinItem = new JoinConditionItem();
                joinItem.LeftField  = new ConditionField();
                joinItem.RightField = new ConditionField();


                joinItem.LeftField.Table            = currentTable;
                joinItem.LeftField.FieldName        = childCol.ColumnName;
                joinItem.LeftField.IsUseFieldPrefix = true;


                joinItem.RightField.Table            = parentTable;
                joinItem.RightField.FieldName        = parentCol.ColumnName;
                joinItem.RightField.IsUseFieldPrefix = true;

                querySql.JoinCondition.ChildCollection.Add(joinItem);
            }

            #endregion

            BuildParentInnerJoin(parentObjecct, sqlBuildInfo, querySql, context);
        }
Ejemplo n.º 11
0
 internal string ParseOrderByCondition(string orderByCondition, SqlBuildingInfo sqlBuildingInfo)
 {
     return(orderByCondition);
 }
Ejemplo n.º 12
0
        /// <summary>
        /// 构造主键的过滤条件。
        /// </summary>
        /// <param name="context">SQL构造上下文信息。</param>
        /// <param name="sqlInfo">SQL拼装的中间变量。</param>
        /// <param name="currentObject">主键过滤条件对应的领域对象</param>
        /// <param name="pkColumnData">主键值Dictionary,Key是ElemetID,Value是列的值。</param>
        /// <param name="IsUseFieldPrefix">是否使用列全名。</param>
        /// <returns>主键过滤条件</returns>
        protected FilterConditionStatement GetPrimaryKeyConditionsEx(SqlBuildingContext context, SqlBuildingInfo sqlInfo,
                                                                     DomainObject currentObject, IDictionary <string, object> pkColumnData, bool IsUseFieldPrefix = false)
        {
            var conditionItem = new FilterConditionStatement();

            foreach (KeyValuePair <string, object> pkdata in pkColumnData)
            {
                var pkElement = currentObject.Elements.FirstOrDefault(i => i.ID == pkdata.Key);
                var pkColumn  = currentObject.DataObject.PKColumns.FirstOrDefault(c => c.ID == pkElement.DataColumnID);

                var  tableName  = context.GetTableName(currentObject.DataObject.ID);
                bool isTextType = DataTypeUtils.IsTextType(pkColumn.DataObjectID);
                if (isTextType)
                {
                    var keyCondition = new KeyValueConditionStatement <string>();
                    keyCondition.Field.FieldName = pkColumn.ColumnName;
                    if (IsUseFieldPrefix)
                    {
                        var sqlTable = this.TryFindAndRegistSqlTable(tableName, tableName, tableName, tableName, sqlInfo);
                        keyCondition.Field.IsUseFieldPrefix = IsUseFieldPrefix;
                        keyCondition.Field.Table            = sqlTable;
                    }
                    keyCondition.Value           = Convert.ToString(pkdata.Value);
                    keyCondition.LogicalOperator = OperatorType.And;
                    conditionItem.ChildCollection.Add(keyCondition);
                }
                else
                {
                    var keyCondition = new KeyValueConditionStatement <long>();
                    keyCondition.Field.FieldName = pkColumn.ColumnName;
                    if (IsUseFieldPrefix)
                    {
                        var sqlTable = this.TryFindAndRegistSqlTable(tableName, tableName, tableName, tableName, sqlInfo);
                        keyCondition.Field.IsUseFieldPrefix = IsUseFieldPrefix;
                        keyCondition.Field.Table            = sqlTable;
                    }
                    keyCondition.Value           = Convert.ToInt64(pkdata.Value);
                    keyCondition.LogicalOperator = OperatorType.And;
                    conditionItem.ChildCollection.Add(keyCondition);
                }
            }

            return(conditionItem);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 获取主键过滤条件。
        /// </summary>
        /// <param name="context">SQL构造上下文信息。</param>
        /// <param name="domainObject">领域对象</param>
        /// <param name="curDataObject">数据对象</param>
        /// <param name="sqlInfo">SQL拼装的中间变量。</param>
        /// <returns>主键的过滤条件。</returns>
        private SqlPrimaryKey GetPrimaryKeyCondition(SqlBuildingContext context, DomainObject domainObject, DataObject curDataObject, SqlBuildingInfo sqlInfo)
        {
            var      tableName = context.DataObjectTableMapping[curDataObject.ID];
            SqlTable curTable  = this.FindSqlTable(tableName, sqlInfo);

            if (curTable == null)
            {
                curTable = new SqlTable(tableName, tableName, tableName);
                this.RegistSqlTable(tableName, curTable, sqlInfo);
            }

            var data = context.DataContext.GetCurrentDataContextItem(domainObject.ID);

            if (data.PrimaryKeyData.Count == 0)
            {
                return(null);
            }

            var primaryKey = new SqlPrimaryKey();

            foreach (var column in curDataObject.PKColumns)
            {
                var keyField  = new SqlPrimaryKeyField(curTable, column.ColumnName);
                var pkElement = domainObject.Elements.FirstOrDefault(i => i.DataColumnID == column.ID);
                keyField.Value.Value      = data.PrimaryKeyData[pkElement.ID];
                keyField.IsUseFieldPrefix = true;
                primaryKey.ChildCollection.Add(keyField);
            }

            return(primaryKey);
        }
Ejemplo n.º 14
0
 //非主键的SQL过滤条件。
 private FilterConditionStatement GetOrdinaryCondition(SqlBuildingContext context, SqlBuildingInfo sqlBuildInfo)
 {
     return(null);
 }
Ejemplo n.º 15
0
        /// <summary>
        /// 先从缓存中查找,未找到时创建SqlTable。
        /// </summary>
        /// <param name="tableKey">唯一标识。</param>
        /// <param name="tableName">表名。</param>
        /// <param name="tableAlias">表别名。</param>
        /// <param name="realTableName">实表名。</param>
        /// <param name="sqlInfo">Sql构造中间变量。</param>
        /// <returns></returns>
        protected SqlTable TryFindAndRegistSqlTable(string tableKey, string tableName, string tableAlias, string realTableName, SqlBuildingInfo sqlInfo)
        {
            SqlTable table = this.FindSqlTable(tableKey, sqlInfo);

            if (table == null)
            {
                table = new SqlTable(tableName, tableAlias, realTableName);
            }
            this.RegistSqlTable(tableKey, table, sqlInfo);
            return(table);
        }
Ejemplo n.º 16
0
 /// <summary>
 /// 构造函数
 /// </summary>
 public SqlStatement()
     : base()
 {
     PrimaryKeys     = new SqlPrimaryKey();
     SqlBuildingInfo = new SqlBuildingInfo();
 }