Пример #1
0
        object getValue( ConfigDbKey key, String valueName, out ConfigDbDataType dataType )
        {
            dataType = ConfigDbDataType.String;

            if( valueName != null )
                valueName = valueName.Trim();

            // cannot set CURRENT_USER key directly - must append the Username
            if( key == ConfigDbKey.CurrentUser )
                key = OpenKey( key, String.Empty, true );

            // make sure the key is there and get the default values in case we need them
            FilterExpression srchExp = new FilterExpression( StrID, (Int32) key, EqualityEnum.Equal, MatchTypeEnum.UseCase );
            Table table = _db.SelectRecords( srchExp, new String[] { StrDataType, StrValue, StrValues } );

            if( table.Count == 0 )
            {
                throw new Exception( ErrKeyNotFound );
            }

            object value = null;

            // if valueName is null or empty, use the Key's value, which we already have from above

            if( !string.IsNullOrEmpty( valueName ) )
            {
                String filter = String.Format( "ParentID = {0} AND Type = {1} AND ~Name = '{2}'", (int) key, (int) KeyValueType.Value, valueName );
                table = getMatchingRecords( filter, new String[] { StrDataType, StrValue, StrValues }, false, null );

                if( table.Count == 0 )
                {
                    // Note: its easier to program against if we don't throw an error if there is no Value
                    // throw new Exception( ErrValueNotFound );
                    return null;
                }

                Debug.Assert( table.Count == 1 );
            }

            dataType = (ConfigDbDataType) table[0][StrDataType];

            if( dataType == ConfigDbDataType.String )
                value = table[0][StrValue];
            else
                value = table[0][StrValues];
            
            return value;
        }
Пример #2
0
        public void SetValue( ConfigDbKey key, String valueName, ConfigDbDataType dataType, object value )
        {
            checkDbOpen();

            if( key == ConfigDbKey.CurrentUser || key == ConfigDbKey.LocalMachine )
                throw new Exception( ErrCantHaveValues );

            Table table;

            if( valueName != null )
                valueName = valueName.Trim();

    		// cannot set CURRENT_USER key directly - must append the Username
            if( key == ConfigDbKey.CurrentUser )
                key = OpenKey( key, String.Empty, true );
            else
            {
                // make sure the key is there
                FilterExpression srchExp = new FilterExpression( StrID, (Int32) key, EqualityEnum.Equal, MatchTypeEnum.UseCase );
                table = _db.SelectRecords( srchExp, new String[] { StrID } );

                if( table.Count == 0 )
                {
                    throw new Exception( ErrKeyNotFound );
                }
            }

            int id = -1;
            FieldValues fieldValues = new FieldValues( 5 );
            
            // these fields are always set regardless of adding or updating
            fieldValues.Add( StrDataType, (Int32) dataType );
            if( dataType == ConfigDbDataType.StringArray )
                fieldValues.Add( StrValues, value );
            else
                fieldValues.Add( StrValue, value );

            if( string.IsNullOrEmpty( valueName ) )
            {
                // we use the Key's value instead of creating a new 
                id = (Int32) key;
            }
            else
            {
                String filter = String.Format( "ParentID = {0} AND Type = {1} AND ~Name = '{2}'", (int) key, (int) KeyValueType.Value, valueName );
                table = getMatchingRecords( filter, new String[] { StrID }, false, null );

                if( table.Count > 0 )
                {
                    // found an existing value - update it
                    id = (int) table[0][0];
                }
            }

            if( id > -1 )
            {
                // update
                _db.UpdateRecordByKey( id, fieldValues );
            }
            else // add new value
            {
                fieldValues.Add( StrParentID, (Int32) key );
                fieldValues.Add( StrType, (Int32) KeyValueType.Value );
                fieldValues.Add( StrName, valueName );
                int index = _db.AddRecord( fieldValues );
            }
        }