/// <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); }
/// <summary> /// 对Select类SQL语句进行参数处理 /// </summary> /// <param name="sqlStatement">待处理的SQL语句</param> /// <param name="db">数据访问对象</param> /// <returns>处理后的参数数组</returns> private List <IDbDataParameter> ParameterHandlerForSelect(SqlStatement sqlStatement, ITeldDatabase db) { var selectSql = sqlStatement as SelectSqlStatement; var parameters = new List <IDbDataParameter>(selectSql.FilterCondition.ChildCollection.Count); for (int parameterIndex = 0; parameterIndex < selectSql.FilterCondition.ChildCollection.Count; parameterIndex++) { var filterCondition = selectSql.FilterCondition.ChildCollection[parameterIndex]; if (filterCondition is SqlPrimaryKey) { foreach (var keyField in filterCondition.ChildCollection) { SqlPrimaryKeyField pkField = keyField as SqlPrimaryKeyField; if (pkField == null) { throw new Exception("ParameterHandlerForUpdate.updateField, parameterIndex:" + parameterIndex); } parameters.Add(ParameterHandler(db, pkField.Value, pkField)); } } } return(parameters); }
/// <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); }
/// <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()); }
/// <summary> /// 构造查询主体部分 /// </summary> /// <remarks>查询主体是一个子查询,为了支持一个数据对象多个数据库表</remarks> /// <param name="sql">Select语句</param> /// <param name="domainModel">通用中间对象</param> /// <param name="domainObject">当前节点</param> /// <param name="dataObject">当前节点对应的数据对象</param> private void BuildMainFrom(SelectSqlStatement sql, DomainModel.Spi.DomainModel domainModel, DomainObject domainObject, DataObject dataObject, string tableName, SqlBuildingContext sqlContext) { //初始化主查询SQL语句 var context = sqlContext.DataContext; sql.NodeID = domainObject.ID; sql.CommonObjectID = domainModel.ID; sql.NodeVersion = domainModel.Version.ToString(); sql.CommonObjectVersion = domainModel.Version.ToString(); sql.TableName = tableName; sql.TableCode = tableName; sql.SqlBuildingInfo = base.InitSqlBuildingInfo(domainModel, domainObject, dataObject, tableName, sqlContext.DataSource); if (context.Data.ContainsKey(domainObject.ID)) { var dataContextItem = context.GetCurrentDataContextItem(domainObject.ID); foreach (var column in dataObject.PKColumns) { var pkField = new SqlPrimaryKeyField(sql.SqlBuildingInfo.CurrentSqlTable, column.ColumnName); var pkElement = domainObject.Elements.FirstOrDefault(i => i.DataColumnID == column.ID); pkField.Value.Value = dataContextItem.PrimaryKeyData[pkElement.ID]; sql.PrimaryKeys.ChildCollection.Add(pkField); } } //将子查询加入到当前Select语句中 sql.From.ChildCollection.Add(sql.SqlBuildingInfo.CurrentSqlTable); }
/// <summary> /// 克隆 /// </summary> /// <returns>主键类</returns> public override object Clone() { SqlPrimaryKeyField newObj = this.MemberwiseClone() as SqlPrimaryKeyField; if (this.Value != null) { newObj.fieldValue = this.Value.Clone() as FieldValue; } return(newObj); }
/// <summary> /// 根据SQLDOM元素类型初始化SQLDOM元素对象 /// </summary> /// <param name="sqlElementType">SQLDOM元素类型</param> /// <returns>SQLDOM元素对象</returns> public static SqlElement GetSQLElement(int sqlElementType) { SqlElement element = null; SqlElementType type = (SqlElementType)sqlElementType; switch (type) { case SqlElementType.InsertField: element = new InsertField(); break; case SqlElementType.SqlPrimaryKeyField: element = new SqlPrimaryKeyField(); break; case SqlElementType.SelectListField: element = new SelectListField(); break; case SqlElementType.UpdateField: element = new UpdateField(); break; case SqlElementType.From: element = new From(); break; case SqlElementType.FromItem: element = new FromItem(); break; case SqlElementType.JoinCondition: element = new JoinConditionStatement(); break; case SqlElementType.OrderByCondition: element = new ConditionStatement(); break; case SqlElementType.FilterCondition: element = new ConditionStatement(); break; case SqlElementType.UnKnown: break; } return(element); }
/// <summary> /// 对Select类SQL语句进行参数处理 /// </summary> /// <param name="sqlStatement">待处理的SQL语句</param> /// <param name="db">数据访问对象</param> /// <returns>处理后的参数数组</returns> private List <IDbDataParameter> ParameterHandlerForDelete(SqlStatement sqlStatement, ITeldDatabase db) { var deleteSql = sqlStatement as DeleteSqlStatement; var parameters = new List <IDbDataParameter>(); if (deleteSql.SubQuerySql != null) { for (int parameterIndex = 0; parameterIndex < deleteSql.SubQuerySql.Condition.ChildCollection.Count; parameterIndex++) { var filterCondition = deleteSql.SubQuerySql.Condition.ChildCollection[parameterIndex]; if (filterCondition is SqlPrimaryKey) { foreach (var keyField in filterCondition.ChildCollection) { SqlPrimaryKeyField pkField = keyField as SqlPrimaryKeyField; if (pkField == null) { throw new Exception("ParameterHandlerForDelete.PkField, parameterIndex:" + parameterIndex); } parameters.Add(ParameterHandler(db, pkField.Value, pkField)); } } } } else { for (int parameterIndex = 0; parameterIndex < deleteSql.Conditions.ChildCollection.Count; parameterIndex++) { var filterCondition = deleteSql.Conditions.ChildCollection[parameterIndex]; if (filterCondition is SqlPrimaryKey) { foreach (var keyField in filterCondition.ChildCollection) { SqlPrimaryKeyField pkField = keyField as SqlPrimaryKeyField; if (pkField == null) { throw new Exception("ParameterHandlerForDelete.PkField, parameterIndex:" + parameterIndex); } parameters.Add(ParameterHandler(db, pkField.Value, pkField)); } } } } return(parameters); }
/// <summary> /// 处理通用中间对象的节点的GSPAbsDBTable。 /// </summary> /// <param name="sql">Sql语句对象。</param> /// <param name="domainModel">领域模型。</param> /// <param name="domainObject">领域对象。</param> /// <param name="dataObject">领域对象对应的数据对象。</param> /// <param name="tableName">表名称。</param> protected void HandlingSqlStatement(SqlStatement sql, SqlBuildingContext context) { sql.NodeID = context.Node.ID; sql.CommonObjectID = context.CommonObject.ID; sql.NodeVersion = context.CommonObject.Version.ToString(); sql.CommonObjectVersion = context.CommonObject.Version.ToString(); sql.TableName = context.TableName; //构造SqlBuildingInfo sql.SqlBuildingInfo = this.InitSqlBuildingInfo(context.CommonObject, context.Node, context.DataObject, context.TableName, context.DataSource); //复合主键 foreach (var item in context.DataObject.PKColumns) { var keyField = new SqlPrimaryKeyField(); keyField.FieldName = item.ColumnName; sql.PrimaryKeys.ChildCollection.Add(keyField); } }
/// <summary> /// 转换成SQL /// </summary> /// <returns>SQL</returns> public override string ToSQL() { if (this.ChildCollection == null || this.ChildCollection.Count == 0) { return(string.Empty); } var result = new StringBuilder(); SqlPrimaryKeyField keyField = null; for (int i = 0; i < this.ChildCollection.Count; i++) { keyField = this.ChildCollection[i] as SqlPrimaryKeyField; result.Append(keyField.ToSQL()); if (i < this.ChildCollection.Count - 1) { result.Append(" AND "); } } return(result.ToString()); }