示例#1
0
        /// <summary>
        /// Update a specific named parameter with the given value.
        /// </summary>
        /// <param name="paramName">The parameter whose value to set</param>
        /// <param name="paramValue">The value being assigned to the parameter</param>
        public void SetParameter(string paramName, object paramValue)
        {
            FieldMap fm = map != null?map.GetFieldMapFromColumn(paramName) : null;

            // support setting parameters without using the prefix character
            if (!cmd.Parameters.Contains(paramName))
            {
                GentleSqlFactory sf = broker.GetSqlFactory();
                paramName = sf.GetParameterPrefix() + paramName + sf.GetParameterSuffix();
            }
            Check.Verify(cmd.Parameters.Contains(paramName), Error.NoSuchParameter, paramName);
            IDataParameter param = (IDataParameter)cmd.Parameters[paramName];

            SetParameter(param, paramValue, fm);
        }
示例#2
0
        /// <summary>
        /// Obtain a key for the specified object instance and property names. The returned
        /// key will contain the corresponding column names for the type, and thus foreign
        /// key columns must use identical naming for this to work.
        /// </summary>
        /// <param name="broker">The optional PersistenceBroker instance to use for obtaining the
        /// ObjectMap for the supplied object instance. If this parameter is null, Gentle will try
        /// to infer the broker from the object instance. If that fails, it will use the default
        /// provider.</param>
        /// <param name="key">An optional existing key to add the values to</param>
        /// <param name="isPropertyKeys">False is key indexers are column names, true for property names</param>
        /// <param name="instance">The object instance whose property values will be used</param>
        /// <param name="members">The names of the properties to include in the key</param>
        /// <returns>The key</returns>
        public static Key GetKey(PersistenceBroker broker, Key key, bool isPropertyKeys, object instance, params string[] members)
        {
            Check.VerifyNotNull(instance, Error.NullParameter, "instance");
            // try to infer broker from instance
            if (broker == null && instance is IBrokerLock)
            {
                broker = (instance as IBrokerLock).SessionBroker;
            }
            // WARNING/TODO if broker is null here and no ObjectMap yet exists for the type,
            // the DefaultProvider will be used to create the ObjectMap
            ObjectMap map = ObjectFactory.GetMap(broker, instance);

            // only set source type reference if this is a new key
            if (key == null)
            {
                key = new Key(map.GetTableName(instance), instance.GetType(), isPropertyKeys);
            }
            //else
            //	Check.Verify( ! key.isPropertyKeys, Error.DeveloperError,
            //		"Unable to combine keys containing property names due to possible name clashes." );
            Check.VerifyEquals(key.isPropertyKeys, isPropertyKeys, "Cannot combine property and " +
                               "column names in a single key - use one or the other.");
            Check.VerifyNotNull(members, Error.NullParameter, "members");
            // process the list of specified properties
            foreach (string memberName in members)
            {
                FieldMap fm = isPropertyKeys ? map.GetFieldMap(memberName) : map.GetFieldMapFromColumn(memberName);
                Check.VerifyNotNull(fm, Error.NoProperty, map.Type, memberName);                   // FIXME outdated error message
                object memberValue = fm.GetValue(instance);
                // translate foreign references to local names
                if (key.SourceType != map.Type)
                {
                    // WARNING/TODO if broker is null here and no ObjectMap yet exists for the type,
                    // the DefaultProvider will be used to create the ObjectMap
                    ObjectMap keyMap = ObjectFactory.GetMap(broker, key.SourceType);
                    fm = keyMap.GetForeignKeyFieldMap(map.TableName, fm.ColumnName);
                }
                key[isPropertyKeys ? fm.MemberName : fm.ColumnName] = memberValue;
            }
            // add concurrency value if enabled and instance has revision column
            if (GentleSettings.ConcurrencyControl && map.ConcurrencyMap != null)
            {
                long version = Convert.ToInt64(map.ConcurrencyMap.GetValue(instance));
                key[isPropertyKeys ? map.ConcurrencyMap.MemberName : map.ConcurrencyMap.ColumnName] = version;
            }
            return(key);
        }
示例#3
0
        private static Type GetColumnType(string columnName, ObjectMap map, SqlResult sr)
        {
            Type result = null;

            if (map != null)
            {
                FieldMap fm = map.GetFieldMapFromColumn(columnName);
                if (fm != null)
                {
                    result = fm.Type;
                }
            }
            if (result == null && sr.RowsContained > 0)
            {
                object obj = sr.GetObject(0, columnName);
                if (obj != null)
                {
                    result = obj.GetType();
                }
            }
            return(result);
        }
示例#4
0
		private static Type GetColumnType( string columnName, ObjectMap map, SqlResult sr )
		{
			Type result = null;
			if( map != null )
			{
				FieldMap fm = map.GetFieldMapFromColumn( columnName );
				if( fm != null )
				{
					result = fm.Type;
				}
			}
			if( result == null && sr.RowsContained > 0 )
			{
				object obj = sr.GetObject( 0, columnName );
				if( obj != null )
				{
					result = obj.GetType();
				}
			}
			return result;
		}
        /// <summary>
        /// Constructor that reads and encapsulates results from the data reader. The maximum
        /// number of rows to retrieve is obtained from the SqlStatement parameter.
        /// </summary>
        /// <param name="broker">The PersistenceBroker instance to use for database access.</param>
        /// <param name="dr">The DataReader instance to read from</param>
        /// <param name="stmt">The SqlStatement leading to this SqlResult</param>
        public SqlResult(PersistenceBroker broker, IDataReader dr, SqlStatement stmt) : base(broker)
        {
            this.stmt = stmt;
            try
            {
                rows = new ArrayList();
                // store column names/order
                columnNames = new string[dr.FieldCount];
                // also cache fieldmaps for object retrievals (used later for NullValue handling)
                ObjectMap map = stmt != null && stmt.Type != null?ObjectFactory.GetMap(broker, stmt.Type) : null;

                fieldMaps = map != null ? new FieldMap[dr.FieldCount] : null;
                // process all columns
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    columnNames[i] = dr.GetName(i);
                    if (map != null)
                    {
                        fieldMaps[i] = map.GetFieldMapFromColumn(columnNames[i]);
                    }
                }
                // skip past offset number of rows if SQL Server and paged statement
                int skipCount = 0;
                if (stmt.RowOffset > 0 && broker.ProviderName == "SQLServer")
                {
                    while (dr != null && skipCount++ < stmt.RowOffset && dr.Read())
                    {
                        ;
                    }
                }
                // read and store rows in result set
                while (dr != null && dr.Read() && (stmt.RowLimit <= 0 || rows.Count < stmt.RowLimit))
                {
                    // store values
                    object[] row = new object[dr.FieldCount];
                    dr.GetValues(row);
                    // convert DBNulls to system nulls (or the appropriate null translation value)
                    for (int i = 0; i < row.Length; i++)
                    {
                        if (row[i] == DBNull.Value)
                        {
                            row[i] = (map != null && fieldMaps[i] != null) ? fieldMaps[i].NullValue : null;
                        }
                    }
                    rows.Add(row);
                }
                rowsAffected = rows.Count;
            }
            //catch( Exception e )
            //{
            //    string msg = e.Message;
            //    throw;
            //}
            finally
            {
                // close the data reader
                if (dr != null && !dr.IsClosed)
                {
                    dr.Close();
                }
            }
        }