Dynamic Searches

Posts   
 
    
Posts: 20
Joined: 27-Jun-2011
# Posted on: 27-Jun-2011 14:58:34   

Hi I am trying to use LLBLGEN pro to write a new sql interface to my software. I want to be able to do dymanic queries.

That is create an entity and collection based on the table name (I have done this ok).

I then want to be able to do searches which the user may want to perform. I can see that Predicates seem to work at compile time, not runtime,

Any pointers where to start.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 28-Jun-2011 07:37:12   

That depends on how the user would input the search query. Here are some ideas:

Dynamic Linq. This is not directly supported by LLBLGen, but apparently some users have this working. http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=17046

Using switches. http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=1966

Using Activator class. http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=6304&StartAtMessage=0&#34915

David Elizondo | LLBLGen Support Team
Posts: 20
Joined: 27-Jun-2011
# Posted on: 29-Jun-2011 02:23:53   

daelmo wrote:

That depends on how the user would input the search query. Here are some ideas:

Dynamic Linq. This is not directly supported by LLBLGen, but apparently some users have this working. http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=17046

I'm no expert on Linq so might give this one a miss confused

Using switches. http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=1966

There are thousands of possible fields, that would be a mighty case statement disappointed

Using Activator class. http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=6304&StartAtMessage=0&#34915

Sounds like a plan. I use the Activator class already to create the Entity class as all I know is the table name.

Posts: 20
Joined: 27-Jun-2011
# Posted on: 29-Jun-2011 08:56:40   

Hi Thanks for the help. That all worked fine. My code now looks like the following

    // Create an Entity Class based on the Table Name
    public CommonEntityBase CreateEntity(String Table)
    {
        thisTable = char.ToUpper(Table[0]) + Table.Substring(1); //Make the first character uppercase
        String Name = String.Format("MicronetDAL.EntityClasses.{0}Entity", thisTable);

        Assembly a = Assembly.LoadFrom(".\\MicronetDAL.dll");
        thisEntityType = a.GetType(Name);

        TableEntity = (CommonEntityBase)Activator.CreateInstance(thisEntityType);
        return(TableEntity);
    }

    // Create a Entity Collection based on the Table name
    public EntityCollection GetEntityCollection()
    {
        object e;

        String Name = String.Format("MicronetDAL.FactoryClasses.{0}EntityFactory", thisTable);
        Assembly a = Assembly.LoadFrom(".\\MicronetDAL.dll");
        Type t = a.GetType(Name);

        e = (object)Activator.CreateInstance(t);
        return (new EntityCollection((IEntityFactory2)e));
    }

    String ConvertFieldName(String f)
    {
        String ret = char.ToUpper(f[0]) + f.Substring(1);
        return(ret);
    }

   bool FetchEntityCollection(DataAccessAdapter da, EntityCollection thisCollection, IRelationPredicateBucket filter, int max)
    {
        try
        {
            da.FetchEntityCollection(thisCollection, filter, max);
            if (thisCollection.Count == 0)
            {
                Diagnostic.ErrorCode = Error_Code.INOT_ERR;
                Diagnostic.LastError = "";
                return (false);
            }
            return (true);
        }
        catch (Exception ex)
        {
            Diagnostic.CatchError(ex, null,null);
            return (false);
        }

    }

    public bool SelectRecord(String table,  sqlSelectParam[] SelectArray)
    {
        bool ret = true;

        DataAccessAdapter da = new DataAccessAdapter();
        CommonEntityBase = CreateEntity(table);
        EntityCollection thisCollection = GetEntityCollection();

        try
        {
            IRelationPredicateBucket filter = new RelationPredicateBucket();
            for (int i = 0; i < SelectArray.Length; i++)
            {
                FieldCompareValuePredicate predicateToAdd = null;

                IEntityField2 f = TableEntity.Fields[ConvertFieldName(SelectArray[i].FieldName)];
                predicateToAdd = new FieldCompareValuePredicate(f, null, ComparisonOperator.Equal, SelectArray[i].value);

                if (predicateToAdd != null)
                {
                    if (i == 0)
                        filter.PredicateExpression.Add(predicateToAdd);
                    else
                        filter.PredicateExpression.AddWithAnd(predicateToAdd);
                }
            }

            ret = this.FetchEntityCollection(da,thisCollection, filter, 0);
        }
        catch (Exception ex)
        {
            ret = false;
            Diagnostic.CatchError(ex, null, thisTable);
        }
        return (ret);
    }
Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 30-Jun-2011 11:11:33   

Thanks for sharing.