customloader.cs

来自「NHibernate NET开发者所需的」· CS 代码 · 共 579 行 · 第 1/2 页

CS
579
字号
		// Not ported: scroll

		protected override object GetResultColumnOrRow(object[] row, IResultTransformer resultTransformer, IDataReader rs,
		                                               ISessionImplementor session)
		{
			return rowProcessor.BuildResultRow(row, rs, resultTransformer != null, session);
		}

		protected override IList GetResultList(IList results, IResultTransformer resultTransformer)
		{
			// meant to handle dynamic instantiation queries...(Copy from QueryLoader)
			HolderInstantiator holderInstantiator = HolderInstantiator.GetHolderInstantiator(
				null,
				resultTransformer,
				ReturnAliasesForTransformer
				);
			if (holderInstantiator.IsRequired)
			{
				for (int i = 0; i < results.Count; i++)
				{
					object[] row = (object[]) results[i];
					object result = holderInstantiator.Instantiate(row);
					results[i] = result;
				}

				return resultTransformer.TransformList(results);
			}
			else
			{
				return results;
			}
		}

		// Not ported: getReturnAliasesForTransformer()

		protected override IEntityAliases[] EntityAliases
		{
			get { return entityAliases; }
		}

		protected override ICollectionAliases[] CollectionAliases
		{
			get { return collectionAliases; }
		}

		public override int[] GetNamedParameterLocs(string name)
		{
			object loc = namedParameterBindPoints[name];
			if (loc == null)
			{
				throw new QueryException(
					"Named parameter does not appear in Query: " +
					name,
					sql.ToString());
			}

			if (loc is int)
			{
				return new int[] {(int) loc};
			}
			else
			{
				return ArrayHelper.ToIntArray((IList) loc);
			}
		}

		private string[] ReturnAliasesForTransformer
		{
			get { return transformerAliases; }
		}

		protected internal override SqlString SqlString
		{
			get { return sql; }
			set { throw new NotSupportedException("CustomLoader.set_SqlString"); }
		}

		protected override void AutoDiscoverTypes(IDataReader rs)
		{
			MetaData metadata = new MetaData(rs);
			IList aliases = new ArrayList();
			IList types = new ArrayList();

			rowProcessor.PrepareForAutoDiscovery(metadata);

			for (int i = 0; i < rowProcessor.ColumnProcessors.Length; i++)
			{
				rowProcessor.ColumnProcessors[i].PerformDiscovery(metadata, types, aliases);
			}

			resultTypes = ArrayHelper.ToTypeArray(types);
			transformerAliases = ArrayHelper.ToStringArray(aliases);
		}

		public class ResultRowProcessor
		{
			private readonly bool hasScalars;
			private ResultColumnProcessor[] columnProcessors;

			public ResultColumnProcessor[] ColumnProcessors
			{
				get { return columnProcessors; }
			}

			public ResultRowProcessor(bool hasScalars, ResultColumnProcessor[] columnProcessors)
			{
				this.hasScalars = hasScalars || (columnProcessors == null || columnProcessors.Length == 0);
				this.columnProcessors = columnProcessors;
			}

			public object BuildResultRow(
				object[] data,
				IDataReader resultSet,
				bool hasTransformer,
				ISessionImplementor session)
			{
				object[] resultRow;
				if (!hasScalars)
				{
					resultRow = data;
				}
				else
				{
					// build an array with indices equal to the total number
					// of actual returns in the result Hibernate will return
					// for this query (scalars + non-scalars)
					resultRow = new object[columnProcessors.Length];
					for (int i = 0; i < columnProcessors.Length; i++)
					{
						resultRow[i] = columnProcessors[i].Extract(data, resultSet, session);
					}
				}

				return (hasTransformer)
				       	? resultRow
				       	: (resultRow.Length == 1)
				       	  	? resultRow[0]
				       	  	: resultRow;
			}

			public void PrepareForAutoDiscovery(MetaData metadata)
			{
				if (columnProcessors == null || columnProcessors.Length == 0)
				{
					int columns = metadata.GetColumnCount();
					columnProcessors = new ResultColumnProcessor[columns];
					for (int i = 0; i < columns; i++)
					{
						columnProcessors[i] = new ScalarResultColumnProcessor(i);
					}
				}
			}
		}

		public interface ResultColumnProcessor
		{
			object Extract(object[] data, IDataReader resultSet, ISessionImplementor session);
			void PerformDiscovery(MetaData metadata, IList types, IList aliases);
		}

		public class NonScalarResultColumnProcessor : ResultColumnProcessor
		{
			private readonly int position;

			public NonScalarResultColumnProcessor(int position)
			{
				this.position = position;
			}

			public object Extract(
				Object[] data,
				IDataReader resultSet,
				ISessionImplementor session)
			{
				return data[position];
			}

			public void PerformDiscovery(MetaData metadata, IList types, IList aliases)
			{
			}
		}

		public class ScalarResultColumnProcessor : ResultColumnProcessor
		{
			private int position = -1;
			private string alias;
			private IType type;

			public ScalarResultColumnProcessor(int position)
			{
				this.position = position;
			}

			public ScalarResultColumnProcessor(String alias, IType type)
			{
				this.alias = alias;
				this.type = type;
			}

			public object Extract(
				object[] data,
				IDataReader resultSet,
				ISessionImplementor session)
			{
				return type.NullSafeGet(resultSet, alias, session, null);
			}

			public void PerformDiscovery(MetaData metadata, IList types, IList aliases)
			{
				if (alias == null)
				{
					alias = metadata.GetColumnName(position);
				}
				else if (position < 0)
				{
					position = metadata.GetColumnPosition(alias);
				}
				if (type == null)
				{
					type = metadata.GetHibernateType(position);
				}
				types.Add(type);
				aliases.Add(alias);
			}
		}

		public override string QueryIdentifier
		{
			get { return sql.ToString(); }
		}

		/// <summary>
		/// Encapsulates the metadata available from the database result set.
		/// </summary>
		public class MetaData
		{
			private readonly IDataReader resultSet;

			/// <summary>
			/// Initializes a new instance of the <see cref="MetaData"/> class.
			/// </summary>
			/// <param name="resultSet">The result set.</param>
			public MetaData(IDataReader resultSet)
			{
				this.resultSet = resultSet;
			}

			/// <summary>
			/// Gets the column count in the result set.
			/// </summary>
			/// <returns>The column count.</returns>
			public int GetColumnCount()
			{
				return resultSet.FieldCount;
			}

			/// <summary>
			/// Gets the name of the column at the specified position.
			/// </summary>
			/// <param name="position">The (zero-based) position.</param>
			/// <returns>The column name.</returns>
			public string GetColumnName(int position)
			{
				return resultSet.GetName(position);
			}

			/// <summary>
			/// Gets the (zero-based) position of the column with the specified name.
			/// </summary>
			/// <param name="columnName">Name of the column.</param>
			/// <returns>The column position.</returns>
			public int GetColumnPosition(string columnName)
			{
				return resultSet.GetOrdinal(columnName);
			}

			/// <summary>
			/// Gets the Hibernate type of the specified column.
			/// </summary>
			/// <param name="columnPos">The column position.</param>
			/// <returns>The Hibernate type.</returns>
			public IType GetHibernateType(int columnPos)
			{
				return TypeFactory.Basic(resultSet.GetFieldType(columnPos).Name);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?