// Builds a map from foreign key instances to commands, with an entry for every command that may need to // precede some other operation. // // Predecessor commands must precede other commands using those values. There are two kinds of // predecessor: // // - Commands (updates or inserts) inserting FK "targets" (referenced by the foreign key) // - Commands (updates or deletes) deleting FK "sources" (referencing the foreign key) // // To avoid violating constraints, FK values must be created before they are referenced, and // cannot be deleted before their references private KeyToListMap <ForeignKeyValue, UpdateCommand> DetermineForeignKeyPredecessors() { var predecessors = new KeyToListMap <ForeignKeyValue, UpdateCommand>( _keyComparer); foreach (var command in Vertices.OfType <DynamicUpdateCommand>()) { if (ModificationOperator.Update == command.Operator || ModificationOperator.Insert == command.Operator) { foreach (var fkConstraint in _targetMap.EnumerateValues(command.Table)) { ForeignKeyValue fk; if (ForeignKeyValue.TryCreateTargetKey(fkConstraint, command.CurrentValues, true, out fk)) { // if this is an update and the target key is unchanged, there is no // need to add a dependency (from the perspective of the target, the update // is a no-op) ForeignKeyValue originalFK; if (ModificationOperator.Update != command.Operator || !ForeignKeyValue.TryCreateTargetKey(fkConstraint, command.OriginalValues, true, out originalFK) || !_keyComparer.Equals(originalFK, fk)) { predecessors.Add(fk, command); } } } } // register all source predecessors if (ModificationOperator.Update == command.Operator || ModificationOperator.Delete == command.Operator) { foreach (var fkConstraint in _sourceMap.EnumerateValues(command.Table)) { ForeignKeyValue fk; if (ForeignKeyValue.TryCreateSourceKey(fkConstraint, command.OriginalValues, false, out fk)) { // if this is an update and the source key is unchanged, there is no // need to add a dependency (from the perspective of the source, the update // is a no-op) ForeignKeyValue currentFK; if (ModificationOperator.Update != command.Operator || !ForeignKeyValue.TryCreateSourceKey(fkConstraint, command.CurrentValues, false, out currentFK) || !_keyComparer.Equals(currentFK, fk)) { predecessors.Add(fk, command); } } } } } return(predecessors); }
/// <summary> /// Initialize foreign key object for the source of a foreign key. /// </summary> /// <param name="metadata">Sets Metadata</param> /// <param name="record">Record containing key value</param> /// <param name="isInsert">Indicates whether the key value is being inserted or deleted</param> /// <param name="key">Outputs key object</param> /// <returns>true if the record contains key values for this constraint; false otherwise</returns> internal static bool TryCreateSourceKey(ReferentialConstraint metadata, PropagatorResult record, bool isInsert, out ForeignKeyValue key) { key = new ForeignKeyValue(metadata, record, false, isInsert); if (null == key.Key) { return false; } return true; }
private IEnumerable <ObjectData> GetList() { var Ioc = AtawIocContext.Current; List <PlugModel> list = new List <PlugModel>(); var sourceList = Ioc.PlugInModelList.ToList(); // var if (KeyValues != null && KeyValues.Count() > 0) { sourceList = sourceList.Where(a => (KeyValues.ToList().Contains(a.Key))).ToList(); } if (!ForeignKeyValue.IsEmpty()) { } string _tableName = this.RegName + "_SEARCH"; if (PostDataSet != null && PostDataSet.Tables[_tableName] != null && PostDataSet.Tables[_tableName].Rows.Count > 0) { DataRow row = PostDataSet.Tables[_tableName].Rows[0]; sourceList = WhereFilter(sourceList, row).ToList(); } List <PlugInModel> regList = new List <PlugInModel>(); if (Pagination != null) { regList = sourceList.Skip((Pagination.PageIndex) * Pagination.PageSize).Take(Pagination.PageSize).ToList(); } else { Pagination = new Pagination().FormDataTable(PostDataSet.Tables["PAGER"]); //Pagination.PageSize = //regList = source.ToList(); } Pagination.TotalCount = sourceList.Count; int i = 0; regList.ForEach((a) => { list.Add( new PlugModel() { RegName = a.Key + i.ToString(), BassClassName = a.BaseType.Name, Desc = a.Description, DllPath = a.InstanceType.Module.ScopeName, InstanceClassName = a.InstanceType.Name, Author = a.Author, CreateDate = a.CreateDate.Value <string>() } ); i++; }); return(list.OrderBy(a => a.DllPath).ThenBy(a => a.BassClassName).ThenBy(a => a.Author)); }
public void CheckUnique(List <ObjectDataView> insertDataViewList, List <ObjectDataView> updateDataViewList, List <string> deleteStringList) { string _columSelect = String.Join(",", UniqueList); // var _updateObjs = updateDataViewList.FindAll(a => HasAllUniqueColumns(a.objectData)); // var _updateIds = updateDataViewList.Select(a => a.KeyId); // deleteStringList.AddRange(_updateIds); if (CheckUniqueFilterSql.IsEmpty()) { CheckUniqueFilterSql = " 1 = 1"; } string _strSql = "SELECT {0},{1} FROM {2} WHERE ({3}) ".SFormat(PrimaryKey, _columSelect, RegName, CheckUniqueFilterSql); var _sqlCmd = SqlUtil.SqlByNotAnd(_strSql, PrimaryKey, deleteStringList); DataSet ds = _sqlCmd.QueryDataSet(DbContext); UniqueRowArrange list = new UniqueRowArrange(); foreach (DataRow row in ds.Tables[0].Rows) { HasOnlyUniqueColumns(row, updateDataViewList); //----------- var _isCheck = list.CheckInsert(new UniqueRow(GetColumnValues(row).ToArray())); if (!_isCheck) { string str = " {0} : {1} ".SFormat(GetColumnDisPlayName(), String.Join(",", GetColumnTexts(row))); ThrowUniError(str); } } //--------------xxxxxxxx--------------------------- // insertDataViewList. insertDataViewList.ForEach(a => { if (!ForeignKeyValue.IsEmpty() && UniqueList.Contains(ForeignKey)) { if (!a.objectData.Row.Table.Columns.Contains(ForeignKey)) { a.objectData.Row.Table.Columns.Add(ForeignKey); } a.objectData.Row[ForeignKey] = ForeignKeyValue; } var _isCheck = list.CheckInsert(new UniqueRow(GetColumnValues(a.objectData.Row).ToArray())); if (!_isCheck) { string str = " {0} : {1} ".SFormat(GetColumnDisPlayName(), String.Join(",", GetColumnTexts(a.objectData.Row))); ThrowUniError(str); //ThrowUniError(GetColumnDisPlayName() + ":" + String.Join(",", GetColumnTexts(a.objectData.Row))); } }); }
// Finds all successors to the given predecessors and registers the resulting dependency edges in this // graph. // // - Commands (updates or inserts) inserting FK "sources" (referencing foreign key) // - Commands (updates or deletes) deleting FK "targets" (referenced by the foreign key) // // To avoid violating constraints, FK references must be created before their referees, and // cannot be deleted before their references. private void AddForeignKeyEdges(KeyToListMap <ForeignKeyValue, UpdateCommand> predecessors) { foreach (var command in Vertices.OfType <DynamicUpdateCommand>()) { // register all source successors if (ModificationOperator.Update == command.Operator || ModificationOperator.Insert == command.Operator) { foreach (var fkConstraint in _sourceMap.EnumerateValues(command.Table)) { ForeignKeyValue fk; if (ForeignKeyValue.TryCreateSourceKey(fkConstraint, command.CurrentValues, true, out fk)) { // if this is an update and the source key is unchanged, there is no // need to add a dependency (from the perspective of the target, the update // is a no-op) ForeignKeyValue originalFK; if (ModificationOperator.Update != command.Operator || !ForeignKeyValue.TryCreateSourceKey(fkConstraint, command.OriginalValues, true, out originalFK) || !_keyComparer.Equals(originalFK, fk)) { foreach (var predecessor in predecessors.EnumerateValues(fk)) { // don't add self-edges for FK dependencies, since a single operation // in the store is atomic if (predecessor != command) { AddEdge(predecessor, command); } } } } } } // register all target successors if (ModificationOperator.Update == command.Operator || ModificationOperator.Delete == command.Operator) { foreach (var fkConstraint in _targetMap.EnumerateValues(command.Table)) { ForeignKeyValue fk; if (ForeignKeyValue.TryCreateTargetKey(fkConstraint, command.OriginalValues, false, out fk)) { // if this is an update and the target key is unchanged, there is no // need to add a dependency (from the perspective of the source, the update // is a no-op) ForeignKeyValue currentFK; if (ModificationOperator.Update != command.Operator || !ForeignKeyValue.TryCreateTargetKey(fkConstraint, command.CurrentValues, false, out currentFK) || !_keyComparer.Equals(currentFK, fk)) { foreach (var predecessor in predecessors.EnumerateValues(fk)) { // don't add self-edges for FK dependencies, since a single operation // in the store is atomic if (predecessor != command) { AddEdge(predecessor, command); } } } } } } } }
private void SetSqlSelect() { string where = ""; DynamicParameters sqlList = new DynamicParameters(); if (KeyValues.Count() == 0) { string searchTable = RegName + "_SEARCH"; if (PostDataSet != null && PostDataSet.Tables[searchTable] != null && PostDataSet.Tables[searchTable].Rows.Count > 0) { DataRow row = PostDataSet.Tables[searchTable].Rows[0]; where = WhereStringBuilder(row, sqlList); } else { if (IsFillEmpty) { where = " AND 1=2 "; } } } else { //当外键也为空的时候 if (ForeignKeyValue.IsEmpty()) { where = SetWhereByKeyValue(sqlList); } else { where = string.Format(" AND {0}=@{0}", ForeignKey); sqlList.Add(string.Format("@{0}", ForeignKey), ChangeForeignKeyValue(ForeignKeyValue));//获取子表过滤条件 } } where = SetWhereFilterSqlByFormConfig(where); where = AdditionalConditionSql(where); string countSql = string.Format(CultureInfo.CurrentCulture, "SELECT COUNT(*) FROM {0} WHERE 1=1 {1}", SetSelectTable(RegName), where); Pagination.TotalCount = DbContext.QueryObject <int>(countSql, sqlList); string _selectStr = SetSelectSql(); string orderName = PrimaryKey + " DESC "; if (Order.IsEmpty()) { if (XmlColumns.Contains("CREATE_TIME")) { orderName = "CREATE_TIME DESC "; } if (XmlColumns.Contains("UPDATE_TIME")) { orderName = "UPDATE_TIME DESC "; } } else { orderName = Order; } if (!Pagination.SortName.IsEmpty()) { if (Pagination.IsASC) { orderName = Pagination.SortName + " ASC"; } else { orderName = Pagination.SortName + " DESC"; } } paraList = new DynamicParameters(); paraList = sqlList; //foreach (var qsql in sqlList)//对象克隆,防止参数重复 //{ // SqlParameter sqlP = new SqlParameter(); // qsql.ObjectClone(sqlP); // paraList.Add(sqlP); //} sql = string.Format(CultureInfo.CurrentCulture, PAGE_SQL, _selectStr, orderName, SetSelectTable(RegName), where); paraList.Add("@skip", Pagination.PageIndex); paraList.Add("@pageSize", Pagination.PageSize); }
private static TestResult CreateInvalidForeignKeyError(ForeignKeyValue foreignKeyValue) { return(new TestResult(ResultType.Error, AddmlLocation.FromFieldIndex(foreignKeyValue.Field), string.Format(Messages.ControlForeignKeyMessage1, foreignKeyValue.Value, foreignKeyValue.ReferencingField))); }
public void Fill() { MySqlCommand command; MySqlDataReader reader; ArrayList newList = new ArrayList(); if ((mTable == null) || (mDisplayColumn == null) || (mValueColumn == null)) { return; } if (mDB == null) { return; } if (mDB.State != ConnectionState.Open) { mDB.Open(); } if (mForeignKey == null) { command = new MySqlCommand("SELECT " + mDisplayColumn + ", " + mValueColumn + " FROM " + mTable + ";", mDB); } else { command = new MySqlCommand("SELECT " + mDisplayColumn + ", " + mValueColumn + " FROM " + mTable + " WHERE " + mForeignKey + " = " + ForeignKeyValue.ToString() + ";", mDB); } reader = command.ExecuteReader(); if (!reader.HasRows) { reader.Close(); mDB.Close(); DataSource = null; Items.Clear(); this.Enabled = false; mList = null; return; } this.Enabled = true; while (reader.Read()) { newList.Add(new ComboData(reader.GetString(mDisplayColumn), reader.GetInt32(mValueColumn), ForeignKeyValue)); } reader.Close(); mDB.Close(); DataSource = newList; DisplayMember = "Name"; ValueMember = "Key"; mList = newList; }