예제 #1
0
        public virtual void TestFetchEmployeesNamedNancyDavolioByNPathQuery()
        {
            using (IContext context = GetContext())
            {
                //Create the query string
                string npath = "Select * From Employee Where FirstName = ? And LastName = ?";

                //Create query parameters
                QueryParameter param1 = new QueryParameter(DbType.String, "Nancy");
                QueryParameter param2 = new QueryParameter(DbType.String, "Davolio");

                //Create an npath query object
                NPathQuery npathQuery = new NPathQuery(npath, typeof(Employee));

                //add the parameters to the query object
                npathQuery.Parameters.Add(param1);
                npathQuery.Parameters.Add(param2);

                //Ask the context to fetch all employees with the name nancy davolio
                IList employees = context.GetObjectsByQuery(npathQuery);

                //Assert that the context didn't return a null value
                Assert.IsNotNull(employees);

                //Assert that the context didn't return an empty list
                Assert.IsTrue(employees.Count > 0);

                //Make sure that each employee in the collection
                //has the name nancy davolio
                foreach (Employee employee in employees)
                {
                    Assert.AreEqual("Nancy", employee.FirstName);
                    Assert.AreEqual("Davolio", employee.LastName);
                }

            }
        }
 public virtual IQuery ToQuery(MarshalQuery marshalQuery)
 {
     IQuery query = null;
     IContext ctx = this.Context;
     IClassMap classMap = ctx.DomainMap.MustGetClassMap(marshalQuery.PrimitiveType);
     Type realType = ctx.AssemblyManager.MustGetTypeFromClassMap(classMap);
     if (marshalQuery.QueryType == "NPathQuery")
         query = new NPathQuery(marshalQuery.QueryString, realType, this.Context);
     if (marshalQuery.QueryType == "SqlQuery")
         query = new SqlQuery(marshalQuery.QueryString, realType, this.Context);
     query.Query = marshalQuery.QueryString;
     foreach (MarshalParameter mp in marshalQuery.Parameters)
     {
         object value = ToParameterValue(mp);
         IQueryParameter param = new QueryParameter(mp.Name, mp.DbType, value);
         query.Parameters.Add(param);
     }
     return query;
 }
예제 #3
0
		protected virtual IQueryParameter TransformParameter(IQueryParameter inParam, string paramName)
		{
			IQueryParameter outParam = null;
			if (inParam.DbType == DbType.Object)
			{
				IClassMap classMap = this.rootClassMap.DomainMap.MustGetClassMap(inParam.Value.GetType() );
				foreach (IPropertyMap propertyMap in classMap.GetIdentityPropertyMaps() )
				{
					object value = this.npathEngine.Context.ObjectManager.GetPropertyValue(inParam.Value, propertyMap.Name );
					DbType dbType =  propertyMap.GetColumnMap().DataType  ;
					outParam = new QueryParameter(paramName, dbType, value );
					break; // TODO: HOW??? To fix for composite identities??????
					// (possibly by traversing the propertypath as many times as the final property has columns ???)
					// (something like WrapColumn in sql engine does..)
				}				
			}
			if (outParam == null)
			{
				outParam = new QueryParameter(paramName, inParam.DbType,  inParam.Value);
			}
			return outParam;								
		}
예제 #4
0
		private SqlExpression EvalGuidValue(NPathGuidValue value)
		{
			string paramName = GetParameterName();
			IQueryParameter param = new QueryParameter(paramName, DbType.Guid, value.Value ) ;
			ResultParameters.Add(param);
			SqlParameter sqlParam = select.AddSqlParameter(paramName, DbType.Guid, value.Value) ;
			return sqlParam ;
		}
예제 #5
0
        //Fetch a list of all the employees in the database where the
        //first or last name contains the string in the filter text box,
        //ordered by first name and last name and add them to the list view
        private void FilterEmployees()
        {
            //set up the list view columns
            SetupListViewColumns();

            //clearing any old list view items
            employeesListView.Items.Clear() ;

            using (IContext context = ContextFactory.GetContext())
            {
                //For this query we will be using an NPathQuery object
                //for comfortable handling of parameters
                NPathQuery npathQuery = new NPathQuery() ;

                //Set the type that we want to fetch objects of
                npathQuery.PrimaryType = typeof(Employee);

                //This is the query string for our query,
                //formulated in the query langauge NPath
                string npathQueryString = "Select * From Employee ";

                string filter = filterTextBox.Text ;

                //Add where clause (unless filter text box is empty)
                if (filter.Length > 0)
                {
                    //add wildcards to the filter
                    filter = "%" + filter + "%";

                    //add the where clause to the query string
                    npathQueryString += "Where FirstName LIKE ? or LastName LIKE ?";

                    //create the parameters
                    QueryParameter firstNameParameter = new QueryParameter(DbType.String, filter);
                    QueryParameter lastNameParameter = new QueryParameter(DbType.String, filter);

                    //add the parameters to our query object
                    npathQuery.Parameters.Add(firstNameParameter);
                    npathQuery.Parameters.Add(lastNameParameter);
                }

                //Add order by clause
                npathQueryString += "Order By FirstName, LastName";

                //Add our query string to our query object
                npathQuery.Query = npathQueryString;

                //Ask the context to execute the query and return the matching employees
                IList employees = context.GetObjectsByQuery(npathQuery);

                //Add the resulting employees to the list view
                foreach (Employee employee in employees)
                {
                    AddEmployeeToListView(employee);
                }
            }
        }
예제 #6
0
		protected virtual SqlParameter AddSqlParameter(SqlStatement sqlStatement, IList parameters, string paramName, object obj, IPropertyMap propertyMap, object value, IColumnMap columnMap, bool noNullStatusCheck)
		{
			DbType dataType = columnMap.DataType;
			IPropertyMap idPropertyMap;
			IObjectManager om = m_SqlEngineManager.Context.ObjectManager;
			IClassMap refClassMap;
			IColumnMap forColMap;
			IClassMap realRefClassMap;
			IQueryParameter param = new QueryParameter(paramName, columnMap.DataType) ;
			parameters.Add(param);
			SqlParameter sqlParameter = sqlStatement.AddSqlParameter(paramName, dataType);
			if (Convert.IsDBNull(value) || value == null)
			{
				param.Value = DBNull.Value;
			}
			if (!(noNullStatusCheck))
			{
				if (om.GetNullValueStatus(obj, propertyMap.Name))
				{
					param.Value = DBNull.Value;
					sqlParameter.Value = DBNull.Value;
					return sqlParameter;
				}				
			}
			if (propertyMap != null)
			{
				if (!(propertyMap.ReferenceType == ReferenceType.None))
				{
					if (value == null)
					{
						param.Value = DBNull.Value;
						sqlParameter.Value = DBNull.Value;
						return sqlParameter;
					}
					else
					{
						if (om.GetObjectStatus(value) == ObjectStatus.UpForCreation)
						{
							param.Value = DBNull.Value;
							sqlParameter.Value = DBNull.Value;
							return sqlParameter;
						}
						else
						{
							refClassMap = propertyMap.MustGetReferencedClassMap();
							forColMap = columnMap.MustGetPrimaryKeyColumnMap();
							dataType = forColMap.DataType;
							if (refClassMap.GetTypeColumnMap() != null && refClassMap.GetTypeColumnMap() == forColMap)
							{
								realRefClassMap = refClassMap.DomainMap.MustGetClassMap(value.GetType());
								value = realRefClassMap.TypeValue;
							}
							else
							{
								idPropertyMap = refClassMap.MustGetPropertyMapForColumnMap(forColMap);
								value = om.GetPropertyValue(value, idPropertyMap.Name);
							}
						}
					}
				}
			}
			param.Value = value;
			sqlParameter.Value = value;
			return sqlParameter;
		}
예제 #7
0
		protected virtual void AddParameter(IList parameters, string paramName, object obj, IPropertyMap propertyMap, object value, IColumnMap columnMap, ref string compareOp, bool noNullStatusCheck)
		{
			DbType dataType = columnMap.DataType;
			IPropertyMap idPropertyMap;
			IObjectManager om = m_SqlEngineManager.Context.ObjectManager;
			IClassMap refClassMap;
			IColumnMap forColMap;
			IClassMap realRefClassMap;
			IQueryParameter param = new QueryParameter(paramName, columnMap.DataType) ;
			parameters.Add(param);
			compareOp = "=";
			if (Convert.IsDBNull(value) || value == null)
			{
				//compareOp = "Is";
				param.Value = DBNull.Value;
				return;
			}
			if (!(noNullStatusCheck))
			{
				if (om.GetNullValueStatus(obj, propertyMap.Name))
				{
					//compareOp = "Is";
					param.Value = DBNull.Value;
					return;
				}				
			}
			if (propertyMap != null)
			{
				if (!(propertyMap.ReferenceType == ReferenceType.None))
				{
					if (value == null)
					{
						//compareOp = "Is";
						param.Value = DBNull.Value;
						return;
					}
					else
					{
						if (om.GetObjectStatus(value) == ObjectStatus.UpForCreation)
						{
							//compareOp = "Is";
							param.Value = DBNull.Value;
							return;
						}
						else
						{
							refClassMap = propertyMap.MustGetReferencedClassMap();
							forColMap = columnMap.MustGetPrimaryKeyColumnMap();
							dataType = forColMap.DataType;
							if (refClassMap.GetTypeColumnMap() != null && refClassMap.GetTypeColumnMap() == forColMap)
							{
								realRefClassMap = refClassMap.DomainMap.MustGetClassMap(value.GetType());
								value = realRefClassMap.TypeValue;
							}
							else
							{
								idPropertyMap = refClassMap.MustGetPropertyMapForColumnMap(forColMap);
								value = om.GetPropertyValue(value, idPropertyMap.Name);
							}
						}
					}
				}
			}
//			if (dataType == DbType.AnsiString || dataType == DbType.AnsiStringFixedLength || dataType == DbType.String || dataType == DbType.StringFixedLength)
//			{
//				if (columnMap.Precision == 0 || columnMap.Precision >= 4000)
//				{
//					compareOp = "LIKE";
//				}
//			}
			param.Value = value;
		}