To start using SqlCondition you need to drop Eco.Persistence.PersistenceMapperSqlCondition persistence mapper on your EcoSpace. Set EcoSpace's PersistenceMapper property to the PersistenceMapperSqlCondition object and PersistenceMapperSqlCondition's NextPersistenceMapper property to your real persistence mapper (PersistenceMapperSqlServer). Finally set PersistenceMapperSqlCondition.DbConnection property to your database connection object (SqlConnection).

In short set the following:
  • ecoSpace.PersistenceMapper = persistenceMapperSqlCondition;
  • persistenceMapperSqlCondition.NextPersistence = persistenceMapperSqlServer;
  • persistenceMapperSqlCondition.DbConnection = sqlConnection;

In remote persistence scenarios place PersistenceMapperSqlCondition on the server side.

If you have configured persistence mappers properly then you can try to execute some SqlConditions. The following code can help you get started.
// ecoSpase is your EcoSpace instance;
public IObjectList SelectAllBySql()
{
   SqlCondition sqlCondition = new SqlCondition(ecoSpace, typeof(BaseClass));
   sqlCondition.Sql = "select eco_id, eco_type from BaseClass";
   IObjectList sqlResult = ecoSpace.Persistence.GetAllWithCondition(sqlCondition);
   return sqlResult;
}

The following function will return the same list of objects as the previous one.
// ecoSpase is your EcoSpace instance;
public IObjectList SelectAllByOcl()
{
   AbstractCondition oclCondition = ecoSpace.OclPs.CreateRetrieveCondition(null, null, "BaseClass.allInstances");
   IObjectList oclResult = ecoSpace.Persistence.GetAllWithCondition(oclCondition);
   return oclResult;
}

Here is an example of a parametrized SQL query that selects only objects of type DerivedClass. Note that DerivedClass should derive from BaseClass and both should be own mapped.
// ecoSpase is your EcoSpace instance;
public IObjectList SelectAllOfType()
{
   SqlCondition sqlCondition = new SqlCondition(ecoSpace, typeof(BaseClass));
   sqlCondition.Sql = "select eco_id, eco_type from BaseClass where eco_type = :p0";
   sqlCondition.Parameters.Add(new EcoParameter("p0", typeof(DerivedClass)));
   IObjectList sqlResult = ecoSpace.Persistence.GetAllWithCondition(sqlCondition);
   return sqlResult;
}
You can write complicated SQL statements to select only the objects that meet certain criteria. The only requirement is that you always return ECO_ID in the first column and ECO_TYPE in the second.

Last edited Jun 30, 2009 at 8:05 PM by ppavlov, version 6

Comments

No comments yet.