public void DefaultValueTest()
		{
			int expectedValue = 0;
			NameOrIndexParameter nip = new NameOrIndexParameter();
			Assert.IsFalse(nip.ByName);
			Assert.AreEqual(nip.Index, expectedValue);
		}
Esempio n. 2
0
        public override bool Equals(object obj)
        {
            if (obj is NameOrIndexParameter)
            {
                NameOrIndexParameter nip = (NameOrIndexParameter)obj;

                if (null != _name && null != nip._name && _name == nip._name)
                {
                    return(true);                    // Same name
                }
                if (null == _name && null == nip._name && _index == nip._index)
                {
                    return(true);                    // Same index
                }
                return(false);
            }

            if (obj is string)
            {
                string name = (string)obj;
                return(null != _name && _name == name);
            }

            if (obj is int)
            {
                int index = (int)obj;
                return(null == _name && _index == index);
            }

            return(false);
        }
		public ScalarDataReaderMapper(
			MappingSchema         mappingSchema,
			IDataReader           dataReader,
			NameOrIndexParameter  nameOrIndex)
			: base(mappingSchema, dataReader)
		{
			_index = nameOrIndex.ByName? dataReader.GetOrdinal(nameOrIndex.Name): nameOrIndex.Index;
		}
		public DictionaryListMapper(
			IDictionary          dic,
			NameOrIndexParameter keyField,
			ObjectMapper         objectMapper)
		{
			_dic        = dic;
			_mapper     = objectMapper;
			_fromSource = keyField.ByName && keyField.Name[0] == '@';
			_keyField   = _fromSource? keyField.Name.Substring(1): keyField;
		}
		public DictionaryIndexListMapper(
			IDictionary  dic,
			MapIndex     index,
			ObjectMapper objectMapper)
		{
			_dic    = dic;
			_mapper = objectMapper;

			_fields = new NameOrIndexParameter[index.Fields.Length];
			_fromSource = new bool[index.Fields.Length];

			for (int i = 0; i < _fields.Length; i++)
			{
				bool fromSource = index.Fields[i].ByName && index.Fields[i].Name[0] == '@';

				_fields[i]     = fromSource? index.Fields[i].Name.Substring(1): index.Fields[i];
				_fromSource[i] = fromSource;
				_isFromSource  = _isFromSource ||  fromSource;
				_isFromDest    = _isFromDest   || !fromSource;
			}
		}
		public void ArrayTest()
		{
			NameOrIndexParameter[] nips = new NameOrIndexParameter[]{ 12345, "54321" };
			Assert.AreEqual(nips[0].Index, 12345);
			Assert.AreEqual(nips[1].Name, "54321");
		}
		public static object SomeFunc(NameOrIndexParameter nip)
		{
			if (nip.ByName)
			{
				return nip.Name;
			}
			else
			{
				return nip.Index;
			}
		}
Esempio n. 8
0
		public virtual IDbDataParameter GetParameter(
			IDbCommand           command,
			NameOrIndexParameter nameOrIndex)
		{
			return (IDbDataParameter)(nameOrIndex.ByName?
				command.Parameters[nameOrIndex.Name]: command.Parameters[nameOrIndex.Index]);
		}
Esempio n. 9
0
		/// <summary>
		/// Calls ILGenerator.Emit(<see cref="OpCodes.Ldstr"/>, string) that
		/// pushes a new object reference to a string literal stored in the metadata.
		/// </summary>
		/// <param name="nameOrIndex">The <see cref="NameOrIndexParameter"/> to be emitted.</param>
		/// <seealso cref="System.Reflection.Emit.ILGenerator.Emit(OpCode,FieldInfo)">ILGenerator.Emit</seealso>
		public EmitHelper ldNameOrIndex(NameOrIndexParameter nameOrIndex)
		{
			return nameOrIndex.ByName?
				ldstr  (nameOrIndex.Name) .call(typeof(NameOrIndexParameter), "op_Implicit", typeof(string)):
				ldc_i4_(nameOrIndex.Index).call(typeof(NameOrIndexParameter), "op_Implicit", typeof(int));
		}
Esempio n. 10
0
		public override IDbDataParameter GetParameter(IDbCommand command, NameOrIndexParameter nameOrIndex)
		{
			IDbDataParameter parameter = base.GetParameter(command, nameOrIndex);

			if (parameter is OracleParameter)
				parameter = OracleParameterWrap.CreateInstance(parameter as OracleParameter);

			return parameter;
		}
Esempio n. 11
0
		/// <summary>
		/// Maps a complex index to an object of the specified type.
		/// </summary>
		void ExecuteDictionaryWithMapIndex(
			NameOrIndexParameter[] index,
			Type elementType)
		{
			_objectType = elementType;

			CreateReturnTypeInstance();
			InitObjectType();
			GetSprocNameOrSqlQueryTest();
			CallSetCommand();
			LoadDestinationOrReturnValue();

			Context.MethodBuilder.Emitter
				.ldsfld(GetIndexField(index))
				.ldloc(_locObjType)
				.ldnull
				.callvirt(typeof(DbManager), "ExecuteDictionary",
							typeof(IDictionary), typeof(MapIndex), typeof(Type), typeof(object[]))
				.pop
				.end()
				;
		}
Esempio n. 12
0
		/// <summary>
		/// Maps any single field to any (other) single field.
		/// </summary>
		void ExecuteScalarDictionaryWithScalarKey(
			NameOrIndexParameter keyField, Type keyType,
			NameOrIndexParameter scalarField, Type elementType)
		{
			_objectType = elementType;

			CreateReturnTypeInstance();
			InitObjectType();
			GetSprocNameOrSqlQueryTest();
			CallSetCommand();
			LoadDestinationOrReturnValue();

			Context.MethodBuilder.Emitter
				.ldNameOrIndex(keyField)
				.LoadType(keyType)
				.ldNameOrIndex(scalarField)
				.ldloc(_locObjType)
				.callvirt(typeof(DbManager), "ExecuteScalarDictionary",
								typeof(IDictionary), typeof(NameOrIndexParameter), typeof(Type),
								typeof(NameOrIndexParameter), typeof(Type))
				.pop
				.end()
				;
		}
Esempio n. 13
0
		/// <summary>
		/// Maps primary keys(s) to a scalar field.
		/// </summary>
		void ExecuteScalarDictionaryWithPK(
			Type keyType,
			NameOrIndexParameter scalarField,
			Type elementType)
		{
			CreateReturnTypeInstance();
			InitObjectType();

			Context.MethodBuilder.Emitter
				.ldarg_0
				.end()
				;

			GetSprocNameOrSqlQueryTest();
			CallSetCommand();
			LoadDestinationOrReturnValue();

			Context.MethodBuilder.Emitter
				.ldloc(_locObjType)
				.LoadType(keyType)
				.ldstr(Context.CurrentMethod.Name)
				.ldNameOrIndex(scalarField)
				.LoadType(elementType)
				.callvirt(_baseType, "ExecuteScalarDictionary", _bindingFlags,
								typeof(DbManager), typeof(IDictionary), typeof(Type),
								typeof(Type), typeof(string), typeof(NameOrIndexParameter), typeof(Type))
				;
		}
Esempio n. 14
0
		public FieldBuilder GetIndexField(NameOrIndexParameter[] namesOrIndexes)
		{
			var id = "index$" + string.Join("%",
				Array.ConvertAll<NameOrIndexParameter, string>(namesOrIndexes,
					delegate(NameOrIndexParameter nameOrIndex)
					{
						return nameOrIndex.ToString();
					}));

			var fieldBuilder = Context.GetField(id);

			if (fieldBuilder == null)
			{
				fieldBuilder = Context.CreatePrivateStaticField(id, typeof(MapIndex));

				EmitHelper emit = Context.TypeBuilder.TypeInitializer.Emitter;

				emit
					.ldc_i4_(namesOrIndexes.Length)
					.newarr(typeof(NameOrIndexParameter))
					;

				for (int i = 0; i < namesOrIndexes.Length; i++)
				{
					emit
						.dup
						.ldc_i4_(i)
						.ldelema(typeof(NameOrIndexParameter));

					if (namesOrIndexes[i].ByName)
					{
						emit
							.ldstr(namesOrIndexes[i].Name)
							.call(typeof(NameOrIndexParameter), "op_Implicit", typeof(string));
					}
					else
					{
						emit
							.ldc_i4_(namesOrIndexes[i].Index)
							.call(typeof(NameOrIndexParameter), "op_Implicit", typeof(int));
					}

					emit
						.stobj(typeof(NameOrIndexParameter))
						.end()
						;
				}

				emit
					.newobj(typeof(MapIndex), typeof(NameOrIndexParameter[]))
					.stsfld(fieldBuilder)
					;
			}

			return fieldBuilder;
		}
Esempio n. 15
0
		protected override void BuildAbstractMethod()
		{
			// Any class variable must be initialized before use
			// as the same instance of the class is utilized to build abstract methods.
			//
			_paramList           = new ArrayList();
			_refParamList        = new ArrayList();
			_formatParamList     = new ArrayList();
			_mapOutputParameters = new ArrayList();
			_destination         = null;
			_createManager       = true;
			_objectType          = null;
			_explicitObjectType  = false;
			_parameters          = Context.CurrentMethod.GetParameters();
			_locManager          = Context.MethodBuilder.Emitter.DeclareLocal(typeof(DbManager));
			_locObjType          = Context.MethodBuilder.Emitter.DeclareLocal(typeof(Type));
			_outputParameters    = null;
			_sqlQueryAttribute   = null;

			GetSqlQueryAttribute();
			ProcessParameters();

			Type returnType = MethodReturnType;
			ReturnType rt = GetReturnType(returnType);

			CreateDbManager(rt != ReturnType.Enumerable);
			SetObjectType();

			// Define execution method type.
			//
			switch (rt)
			{
				case ReturnType.DataReader : ExecuteReader();            break;
				case ReturnType.DataSet    : ExecuteDataSet(returnType); break;
				case ReturnType.DataTable  : ExecuteDataTable();         break;
				case ReturnType.Void       : ExecuteNonQuery();          break;
				case ReturnType.Scalar     : ExecuteScalar();            break;
				case ReturnType.Enumerable : ExecuteEnumerable();        break;

				case ReturnType.List:

					if (!_explicitObjectType)
					{
						Type elementType = TypeHelper.GetListItemType(returnType);

						if (elementType == typeof(object) && _destination != null)
							elementType = TypeHelper.GetListItemType(Context.CurrentMethod.ReturnType);

						if (elementType != typeof(object))
							_objectType = elementType;

						if (ActualTypes.ContainsKey(_objectType))
							_objectType = ActualTypes[_objectType];
					}

					if (_objectType == null || _objectType == typeof(object))
						ThrowTypeBuilderException(Resources.DataAccessorBuilder_BadListItemType);

					if (TypeHelper.IsScalar(_objectType))
						ExecuteScalarList();
					else
						ExecuteList();

					break;

				case ReturnType.Dictionary:
					{
						Type elementType = null;
						Type keyType = typeof(object);
						Type[] gTypes = TypeHelper.GetGenericArguments(returnType, typeof(IDictionary));

						if ((gTypes == null || gTypes.Length != 2) && _destination != null)
							gTypes = TypeHelper.GetGenericArguments(_destination.ParameterType, typeof(IDictionary));

						if (gTypes != null && gTypes.Length == 2)
						{
							keyType = gTypes[0];
							elementType = gTypes[1];
						}

						if (elementType == null || _explicitObjectType)
							elementType = _objectType;

						if (elementType == null || elementType == typeof(object))
							ThrowTypeBuilderException(Resources.DataAccessorBuilder_BadListItemType);

						bool isIndex = TypeHelper.IsSameOrParent(typeof(CompoundValue), keyType);

						if (keyType != typeof(object) && !isIndex && !TypeHelper.IsScalar(keyType))
							ThrowTypeBuilderException(
								Resources.DataAccessorBuilder_BadKeyType);

						MethodInfo mi = Context.CurrentMethod;

						object[] attrs = mi.GetCustomAttributes(typeof(IndexAttribute), true);
						NameOrIndexParameter[] fields = new NameOrIndexParameter[0];

						if (attrs.Length != 0)
							fields = ((IndexAttribute)attrs[0]).Fields;

						if (fields.Length > 1 && keyType != typeof(object) && !isIndex)
							ThrowTypeBuilderException(
								Resources.DataAccessor_InvalidKeyType);

						if (TypeHelper.IsScalar(elementType))
						{
							attrs = mi.GetCustomAttributes(typeof(ScalarFieldNameAttribute), true);

							if (attrs.Length == 0)
								ThrowTypeBuilderException(Resources.DataAccessorBuilder_ScalarFieldNameMissing);

							NameOrIndexParameter scalarField = ((ScalarFieldNameAttribute)attrs[0]).NameOrIndex;

							if (fields.Length == 0)
								ExecuteScalarDictionaryWithPK(keyType, scalarField, elementType);
							else if (isIndex || fields.Length > 1)
								ExecuteScalarDictionaryWithMapIndex(fields, scalarField, elementType);
							else
								ExecuteScalarDictionaryWithScalarKey(fields[0], keyType, scalarField, elementType);
						}
						else
						{
							if (!_explicitObjectType && ActualTypes.ContainsKey(elementType))
								elementType = ActualTypes[elementType];

							if (fields.Length == 0)
								ExecuteDictionaryWithPK(keyType, elementType);
							else if (isIndex || fields.Length > 1)
								ExecuteDictionaryWithMapIndex(fields, elementType);
							else
								ExecuteDictionaryWithScalarKey(fields[0], elementType);
						}
					}

					break;

				default:

					if (_objectType == null)
						_objectType = returnType;

					if (!_explicitObjectType && ActualTypes.ContainsKey(_objectType))
						_objectType = ActualTypes[_objectType];

					ExecuteObject();

					break;
			}

			GetOutRefParameters();

			if (rt != ReturnType.Enumerable)
				Finally();
		}
Esempio n. 16
0
        public override IDbDataParameter GetParameter(
			IDbCommand command,
			NameOrIndexParameter nameOrIndex)
        {
            if (nameOrIndex.ByName)
            {
                // if we have a stored procedure, then maybe command paramaters were formatted
                // (SprocParameterPrefix added). In this case we need to format given parameter name first
                // and only then try to take parameter by formatted parameter name
                string parameterName = command.CommandType == CommandType.StoredProcedure
                    ? Convert(nameOrIndex.Name, ConvertType.NameToSprocParameter).ToString()
                    : nameOrIndex.Name;

                return (IDbDataParameter)(command.Parameters[parameterName]);
            }
            return (IDbDataParameter)(command.Parameters[nameOrIndex.Index]);
        }
Esempio n. 17
0
			public OracleScalarDataReaderMapper(
				MappingSchema        mappingSchema,
				IDataReader          dataReader,
				NameOrIndexParameter nameOrIndex)
				: base(mappingSchema, dataReader, nameOrIndex)
			{
				_dataReader = (OracleDataReader)dataReader;
				_fieldType  = _dataReader.GetProviderSpecificFieldType(Index);

				if (_fieldType != typeof(OracleXmlType) && _fieldType != typeof(OracleBlob))
					_fieldType = _dataReader.GetFieldType(Index);
			}
Esempio n. 18
0
		/// <summary>
		/// Maps any single field to object type.
		/// </summary>
		void ExecuteDictionaryWithScalarKey(
			NameOrIndexParameter keyField,
			Type elementType)
		{
			EmitHelper emit = Context.MethodBuilder.Emitter;

			_objectType = elementType;

			CreateReturnTypeInstance();
			InitObjectType();
			GetSprocNameOrSqlQueryTest();
			CallSetCommand();
			LoadDestinationOrReturnValue();

			if (IsGenericDestinationOrReturnValue())
			{
				Type[] genericArgs = Context.ReturnValue.LocalType.GetGenericArguments();
				Type[] types = new Type[]
					{
						typeof(IDictionary<,>).MakeGenericType(genericArgs),
						typeof(NameOrIndexParameter),
						typeof(Type),
						typeof(object[]),
					};
				MethodInfo method = typeof(DbManager).GetMethod("ExecuteDictionary", _bindingFlags, GenericBinder.Generic, types, null)
					.MakeGenericMethod(genericArgs);

				emit
					.ldNameOrIndex(keyField)
					.ldloc(_locObjType)
					.ldnull
					.callvirt(method)
					.pop
					.end()
					;
			}
			else
			{
				emit
					.ldNameOrIndex(keyField)
					.ldloc(_locObjType)
					.ldnull
					.callvirt(typeof(DbManager), "ExecuteDictionary", typeof(IDictionary), typeof(NameOrIndexParameter), typeof(Type), typeof(object[]))
					.pop
					.end()
					;
			}
		}
Esempio n. 19
0
			public override DataReaderMapper CreateDataReaderMapper(
				IDataReader          dataReader,
				NameOrIndexParameter nip)
			{
				return new OracleScalarDataReaderMapper(this, dataReader, nip);
			}
Esempio n. 20
0
		protected override void BuildAbstractMethod()
		{
			// Any class variable must be initialized before use
			// as the same instance of the class is utilized to build abstract methods.
			//
			_paramList           = new ArrayList();
			_refParamList        = new ArrayList();
			_formatParamList     = new ArrayList();
			_mapOutputParameters = new ArrayList();
			_destination         = null;
			_createManager       = true;
			_objectType          = null;
			_explicitObjectType  = false;
			_parameters          = Context.CurrentMethod.GetParameters();
			_locManager          = Context.MethodBuilder.Emitter.DeclareLocal(typeof(DbManager));
			_locObjType          = Context.MethodBuilder.Emitter.DeclareLocal(typeof(Type));
			_outputParameters    = null;
			_sqlQueryAttribute   = null;

			GetSqlQueryAttribute();
			ProcessParameters();
			CreateDbManager();
			SetObjectType();

			// Define execute method type.
			//
			Type returnType = _destination != null? _destination.ParameterType: Context.CurrentMethod.ReturnType;

			if (returnType == typeof(IDataReader))
			{
				ExecuteReader();
			}
			else if (returnType == typeof(DataSet) || returnType.IsSubclassOf(typeof(DataSet)))
			{
				ExecuteDataSet(returnType);
			}
			else if (returnType == typeof(DataTable) || returnType.IsSubclassOf(typeof(DataTable)))
			{
				ExecuteDataTable();
			}
			else if (!returnType.IsArray && (IsInterfaceOf(returnType, typeof(IList))
				|| returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(IList<>)))
			{
				if (!_explicitObjectType)
				{
					Type elementType = TypeHelper.GetListItemType(returnType);

					if (elementType == typeof(object) && _destination != null)
						elementType = TypeHelper.GetListItemType(Context.CurrentMethod.ReturnType);

					if (elementType != typeof(object))
						_objectType = elementType;

					if (ActualTypes.ContainsKey(_objectType))
						_objectType = ActualTypes[_objectType];
				}

				if (_objectType == null || _objectType == typeof(object))
					throw new TypeBuilderException(string.Format(
						"Can not determine object type for method '{0}.{1}'",
						Context.CurrentMethod.DeclaringType.Name,
						Context.CurrentMethod.Name));

				if (TypeHelper.IsScalar(_objectType))
					ExecuteScalarList();
				else
					ExecuteList();
			}
			else if (IsInterfaceOf(returnType, typeof(IDictionary))
				|| returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(IDictionary<,>))
			{
				Type   elementType = null;
				Type   keyType     = typeof(object);
				Type[] gTypes      = TypeHelper.GetGenericArguments(returnType, typeof(IDictionary));

				if ((gTypes == null || gTypes.Length != 2) && _destination != null)
					gTypes = TypeHelper.GetGenericArguments(_destination.ParameterType, typeof(IDictionary));

				if (gTypes != null && gTypes.Length == 2)
				{
					keyType     = gTypes[0];
					elementType = gTypes[1];
				}

				if (elementType == null || _explicitObjectType)
					elementType = _objectType;

				if (elementType == null || elementType == typeof(object))
					throw new TypeBuilderException(string.Format(
						"Can not determine object type for the method '{0}.{1}'",
						Context.CurrentMethod.DeclaringType.Name,
						Context.CurrentMethod.Name));

				bool isIndex = TypeHelper.IsSameOrParent(typeof(CompoundValue), keyType);

				if (keyType != typeof(object) && !isIndex && !TypeHelper.IsScalar(keyType))
					throw new TypeBuilderException(string.Format(
						"Key type for the method '{0}.{1}' can be of type object, CompoundValue, or a scalar type.",
						Context.CurrentMethod.DeclaringType.Name,
						Context.CurrentMethod.Name));

				MethodInfo mi = Context.CurrentMethod;

				object[] attrs = mi.GetCustomAttributes(typeof(IndexAttribute), true);
				NameOrIndexParameter[] fields = new NameOrIndexParameter[0];

				if (attrs.Length != 0)
					fields = ((IndexAttribute)attrs[0]).Fields;

				if (fields.Length > 1 && keyType != typeof(object) && !isIndex)
					throw new TypeBuilderException(string.Format(
						"Key type for the method '{0}.{1}' can be of type object or CompoundValue.",
						Context.CurrentMethod.DeclaringType.Name,
						Context.CurrentMethod.Name));

				if (TypeHelper.IsScalar(elementType))
				{
					attrs = mi.GetCustomAttributes(typeof(ScalarFieldNameAttribute), true);

					if (attrs.Length == 0)
						throw new TypeBuilderException(string.Format(
							"Scalar field name is not defined for the method '{0}.{1}'.",
							Context.CurrentMethod.DeclaringType.Name,
							Context.CurrentMethod.Name));

					NameOrIndexParameter scalarField = ((ScalarFieldNameAttribute)attrs[0]).NameOrIndex;

					if (fields.Length == 0)
						ExecuteScalarDictionaryWithPK(keyType, scalarField, elementType);
					else if (isIndex || fields.Length > 1)
						ExecuteScalarDictionaryWithMapIndex(fields, scalarField, elementType);
					else
						ExecuteScalarDictionaryWithScalarKey(fields[0], keyType, scalarField, elementType);
				}
				else
				{
					if (!_explicitObjectType && ActualTypes.ContainsKey(elementType))
						elementType = ActualTypes[elementType];

					if (fields.Length == 0)
						ExecuteDictionaryWithPK(keyType, elementType);
					else if (isIndex || fields.Length > 1)
						ExecuteDictionaryWithMapIndex(fields, elementType);
					else
						ExecuteDictionaryWithScalarKey(fields[0], elementType);
				}
			}
			else if (returnType == typeof(void))
			{
				ExecuteNonQuery();
			}
			else if (TypeHelper.IsScalar(returnType.IsByRef? returnType.GetElementType(): returnType))
			{
				ExecuteScalar();
			}
			else
			{
				if (_objectType == null)
					_objectType = returnType;

				if (!_explicitObjectType && ActualTypes.ContainsKey(_objectType))
					_objectType = ActualTypes[_objectType];

				ExecuteObject();
			}

			GetOutRefParameters();
			Finally();
		}