コード例 #1
0
ファイル: Database.cs プロジェクト: erdincay/DatabaseObjects
        private void ItemKeyEnsureValid(IDatabaseObjects objCollection, IDatabaseObject objItem, SQL.SQLFieldValues objFieldValues)
        {
            SQL.SQLSelect objSelect;
            object objKeyFieldValue;
            SQL.SQLConditions objSubset;

            //If the key field is set and the key field is specified in the object
            if (objCollection.KeyFieldName() != string.Empty && objFieldValues.Exists(objCollection.KeyFieldName()))
            {
                objKeyFieldValue = ItemKeyFieldValue(objCollection, objItem, objFieldValues);

                if (objKeyFieldValue is string)
                {
                    if (String.IsNullOrEmpty((string)objKeyFieldValue))
                        throw new Exceptions.DatabaseObjectsException(objItem.GetType().Name + " " + objCollection.KeyFieldName() + " field is Null");
                }

                objSelect = new SQL.SQLSelect();

                objSelect.Tables.Add(objCollection.TableName());
                objSelect.Fields.Add(objCollection.KeyFieldName());
                objSelect.Where.Add(objCollection.KeyFieldName(), SQL.ComparisonOperator.EqualTo, objKeyFieldValue);
                objSubset = objCollection.Subset();
                if (objSubset != null && !objSubset.IsEmpty)
                    objSelect.Where.Add(objSubset);

                if (objItem.IsSaved)
                    objSelect.Where.Add(objCollection.DistinctFieldName(), SQL.ComparisonOperator.NotEqualTo, objItem.DistinctValue);

                using (ConnectionScope objConnection = new ConnectionScope(this))
                    using (IDataReader objReader = objConnection.Execute(objSelect))
                        if (objReader.Read())
                            throw new Exceptions.ObjectAlreadyExistsException(objItem, objKeyFieldValue);
            }
        }