예제 #1
0
        /// --------------------------------------------------------------------------------
        /// <summary>
        /// Returns whether the key exists within the collection. If the collection's
        /// IDatabaseObjects.Subset has been set then only the subset is searched not the
        /// entire table.
        /// </summary>
        ///
        /// <param name="objCollection">
        /// The collection to search within.
        /// </param>
        ///
        /// <param name="objKey">
        /// The key value to search by.
        /// </param>
        ///
        /// <returns><see cref="Boolean" />	(System.Boolean)</returns>
        ///
        /// <example>
        /// <code>
        /// Public Function Exists(ByVal strProductCode As String) As Boolean
        ///
        ///     Return objDatabase.ObjectExists(Me, strProductCode)
        ///
        /// End Function
        /// </code>
        /// </example>
        /// --------------------------------------------------------------------------------
        ///
        public bool ObjectExists(IDatabaseObjects objCollection, object objKey)
        {
            var objSelect = new SQL.SQLSelect();
            string keyFieldName = objCollection.KeyFieldName();

            EnsureKeyFieldNameIsSet(keyFieldName, objCollection);

            objSelect.Tables.Add(objCollection.TableName());
            //.Fields.Add objCollection.DistinctFieldName
            objSelect.Where.Add(keyFieldName, SQL.ComparisonOperator.EqualTo, objKey);
            var objSubset = objCollection.Subset();
            if (objSubset != null && !objSubset.IsEmpty)
            {
                objSelect.Where.Add(objSubset);
            }

            using (ConnectionScope objConnection = new ConnectionScope(this))
                using (IDataReader objReader = objConnection.Execute(objSelect))
                    return objReader.Read();
        }
예제 #2
0
        /// --------------------------------------------------------------------------------
        /// <summary>
        /// Returns an object from the collection using a unique key value.
        /// The key must be unique within the collection. If the collection's
        /// IDatabaseObjects.Subset has been implemented then the key need only be unique
        /// within the subset specified, not the entire database table.
        /// Returns Nothing/null if the object does exist with the specified key.
        /// This feature is what differentiates Database.ObjectByKey() from Database.ObjectByKeyExists().
        /// </summary>
        ///
        /// <param name="objCollection">
        /// The collection which contains the object.
        /// </param>
        ///
        /// <param name="objKey">
        /// The key that identifies the object with the collection. The key is the value of
        /// the field defined by the collection's IDatabaseObjects.KeyFieldName.
        /// </param>
        ///
        /// <returns><see cref="IDatabaseObject" />	(DatabaseObjects.IDatabaseObject)</returns>
        ///
        /// <example>
        /// <code>
        /// Default Public ReadOnly Property Item(ByVal strProductCode As String) As Product
        ///     Get
        ///
        ///         Return objDatabase.ObjectByKey(Me, strProductCode)
        ///
        ///     End Get
        /// End Property
        /// </code>
        /// </example>
        /// --------------------------------------------------------------------------------
        ///
        public IDatabaseObject ObjectByKeyIfExists(IDatabaseObjects objCollection, object objKey)
        {
            var objSelect = new SQL.SQLSelect();
            string keyFieldName = objCollection.KeyFieldName();

            EnsureKeyFieldNameIsSet(keyFieldName, objCollection);

            SQL.SQLSelectTable objPrimaryTable = objSelect.Tables.Add(objCollection.TableName());
            objSelect.Tables.Joins = objCollection.TableJoins(objPrimaryTable, objSelect.Tables);
            objSelect.Where.Add(keyFieldName, SQL.ComparisonOperator.EqualTo, objKey);
            var objSubset = objCollection.Subset();
            if (objSubset != null && !objSubset.IsEmpty)
            {
                objSelect.Where.Add(objSubset);
            }

            using (ConnectionScope objConnection = new ConnectionScope(this))
            {
                using (IDataReader objReader = objConnection.Execute(objSelect))
                {
                    if (objReader.Read())
                        return ObjectFromDataReader(objCollection, objReader);
                    else
                        return null;
                }
            }
        }
예제 #3
0
        private object ItemKeyFieldValue(IDatabaseObjects objCollection, IDatabaseObject objItem, SQL.SQLFieldValues objFieldValues)
        {
            //On the rare occurance that the KeyField is the same as the DistinctField
            //then the key value may not have been set in the Save and therefore be
            //available in the objFieldValues collection. In which case the
            //key has to be extracted from the objItem.DistinctField.
            object objKeyFieldValue;

            if (string.Compare(objCollection.DistinctFieldName(), objCollection.KeyFieldName(), true) == 0)
                objKeyFieldValue = objItem.DistinctValue;
            else
                objKeyFieldValue = objFieldValues[objCollection.KeyFieldName()].Value;

            return objKeyFieldValue;
        }
예제 #4
0
        private IDictionary ObjectsDictionaryBase(IDatabaseObjects objCollection, bool bKeyIsDistinctField = false)
        {
            //Returns an IDictionary with the key being either the DistinctField or KeyField

            IDictionary objDictionary = new Hashtable();
            SQL.SQLSelect objSelect = new SQL.SQLSelect();
            string strKeyField;

            SQL.SQLSelectTable objPrimaryTable = objSelect.Tables.Add(objCollection.TableName());
            objSelect.Tables.Joins = objCollection.TableJoins(objPrimaryTable, objSelect.Tables);
            objSelect.Where = objCollection.Subset();
            objSelect.OrderBy = objCollection.OrderBy();

            using (ConnectionScope objConnection = new ConnectionScope(this))
            {
                using (IDataReader objReader = objConnection.Execute(objSelect))
                {
                    if (bKeyIsDistinctField)
                        strKeyField = objCollection.DistinctFieldName();
                    else
                        strKeyField = objCollection.KeyFieldName();

                    while (objReader.Read())
                        objDictionary.Add(objReader[strKeyField], ObjectFromDataReader(objCollection, objReader));

                    return objDictionary;
                }
            }
        }
예제 #5
0
        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);
            }
        }