CodeSmith Community
Your Code. Your Way. Faster!

Problems with ParameterBuilder and .BeginGroup

Latest post 05-28-2008 9:33 AM by bslatner. 11 replies.
  • 04-18-2008 1:11 AM

    • xazos
    • Top 500 Contributor
    • Joined on 01-04-2007
    • Posts 9
    • Points 165

    Problems with ParameterBuilder and .BeginGroup

    Hi all,

    For the life of me i can't get the .BeginGroup method working correctly. Where ever it is placed while i build my parameter collection, it simply inserts empty brackets and causes sql exceptions. Here is an example of my code where i'm having the issue.

    ---------- 

    Dim
    pb As ConsigneeParameterBuilder = New ConsigneeParameterBuilder()

    pb.AppendIn(String.Empty, ConsigneeColumn.ExporterId, CStr(ExporterId.Value))

    'Check by name only if the original consignee name is different from the edited name.

    If (consigneeName <> hdnConsigneeOriginalName.Value) Then

    pb.AppendEquals("AND", ConsigneeColumn.ConsName, consigneeName)

    End If

    pb.AppendEquals("OR", ConsigneeColumn.Address1, address1)

    pb.AppendEquals("AND", ConsigneeColumn.MarketCode, marketCode)

    'Add telephone and email parameters only if they aren't empty/null

    If (Not String.IsNullOrEmpty(email) Or Not String.IsNullOrEmpty(telephone)) Then

    pb.BeginGroup("OR")

    If (Not String.IsNullOrEmpty(telephone)) Then

    pb.AppendEquals(String.Empty, ConsigneeColumn.Telephone, telephone)

    End If

    If (Not String.IsNullOrEmpty(email)) Then

    pb.AppendEquals("OR", ConsigneeColumn.Email, email)

    End If

    pb.EndGroup()

    pb.AppendIn(
    "AND", ConsigneeColumn.ExporterId, CStr(ExporterId.Value))

    End If

    'Search for any consignees with matching criteria

    Dim consignees As TList(Of Consignee) = DataRepository.ConsigneeProvider.Find(pb)

    -------------------

    The above code yields the following sql:

    (ExporterId IN (@Param0)) AND (ConsName = @Param1) OR (Address1 = @Param2) AND (MarketCode = @Param3)OR ()  (Telephone = @Param4) OR (Email = @Param5) AND (ExporterId IN (@Param6))

    You can see that the OR has been inserted with empty brackets. The right bracket should appear at the end of @param5. I've been able to fudge it using IFs etc, but it would be much cleaner if i could build this parameter list using the begin and endgroup methods.

     thanks for any help you may provide.....

    • Post Points: 5
  • 04-20-2008 7:16 PM In reply to

    • xazos
    • Top 500 Contributor
    • Joined on 01-04-2007
    • Posts 9
    • Points 165

    Re: Problems with ParameterBuilder and .BeginGroup

    Just wondering if anyone can help, this problem is really infuriating. I've also tried to build a where clause with string builder (of which the content works fine if copied into query analyzer) but when used in the .Find() method, gives me an error saying:

    "Unable to use this part of the where clause in this version of Find: (address1 = '34 john st'"

     I can't for the life of me figure out why the above statement causes an error using the find method. It works fine when copied into query analyzer....

    • Post Points: 35
  • 04-21-2008 7:23 AM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 734
    • Points 17,040

    Re: Problems with ParameterBuilder and .BeginGroup

     xazos,

    with find method you have to use parameterbuilder class, which seem you're doing in your first post, and also invoke GetParameters() method to dynamically generate a paramaterized SQL statement

    Dim consignees As TList(Of Consignee) = DataRepository.ConsigneeProvider.Find(pb.GetParameters())

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 35
  • 04-22-2008 6:31 PM In reply to

    • xazos
    • Top 500 Contributor
    • Joined on 01-04-2007
    • Posts 9
    • Points 165

    Re: Problems with ParameterBuilder and .BeginGroup

    Hi Mike,

    I did try the getparameters method and received the same results so i just left it as passing the entire builder object.

     X

    • Post Points: 35
  • 05-16-2008 6:52 AM In reply to

    Re: Problems with ParameterBuilder and .BeginGroup

     I got the same project.

    every kind of using .BeginGroup() produces  SqlSyntaxError

     

    • Post Points: 35
  • 05-16-2008 10:36 AM In reply to

    • bgjohnso
    • Top 10 Contributor
    • Joined on 09-15-2005
    • Spokane, WA
    • Posts 764
    • Points 22,530

    Re: Problems with ParameterBuilder and .BeginGroup

    There is a fairly simple work around for this issue.  After issuing a BeginGroup call, you just need to set the Junction to an empty string.  Something like the following:

             WorkOrderService service = new WorkOrderService();

     

             WorkOrderParameterBuilder pb = new WorkOrderParameterBuilder();

     

             pb.AppendGreaterThan(WorkOrderColumn.OrderDate, "1/1/01");

     

             pb.AppendEquals(WorkOrderColumn.Source, "JDE");

     

             pb.BeginGroup("OR");

     

             pb.Junction = "";

     

             pb.AppendEquals(WorkOrderColumn.Source, "IVARA");

     

             pb.AppendGreaterThan("AND",WorkOrderColumn.OrderDate, "12/31/01");

     

             pb.EndGroup();

     

             pb.Junction = "AND"; //Needed if continuing to add filters

     

             TList<WorkOrder> list = service.Find(pb);

    Ben Johnson
    ------------------------------
     Member of the .NetTiers team
     Visit http://www.nettiers.com
    ------------------------------

    • Post Points: 65
  • 05-19-2008 4:53 AM In reply to

    Re: Problems with ParameterBuilder and .BeginGroup

     Hello,

     very thanks for than hint with that .Junction = "".

    But just using it didn't run at first.

     

    So i got to make an annotation.      --->> SETTING the .Junction after first .Append back to   .Junction = "AND" 

     

    I create my query to runtime. 

    The parameter are filled in GUI to a DataTable. 

    That Table I transform to a suitable format, a List<QueryRow> ( each DataTable means such a QueryRow) 

     A QueryRow is a class containing two properties : the logical operator as string  and a list<SingleQuery>.

    At runtime I organize every queryRow in a single Group. 

    And SingleQuery (two properties: string columnName,string  cellValue) has the content for  a .Append*(...) clause

     

    now creating the query:

    foreach ( QueryRow in  queryRowList)

    {

        //greating the group

      .BeginGroup("") oder ähnlich --> depends on how to organize the runtime code.

      .Junction = "";

     

        foreach (SingleRow in queryList.SingleRowList)

        {

    .          Append... ; 

               .Junction = "AND" ;    -->> that is the point that wasn't in your hint  --> setting the junction after the first append clause

        } 

        .EndGroup(); 

     }

     

     VERY THANKS 

     

    • Post Points: 5
  • 05-28-2008 1:17 AM In reply to

    • bslatner
    • Top 500 Contributor
    • Joined on 10-26-2007
    • Charlotte, NC
    • Posts 19
    • Points 485

    Re: Problems with ParameterBuilder and .BeginGroup

    Actually, this workaround only works if your criteria are static. With dynamic criteria, the SqlParameterBuilder is driving me insane. For example:

     

    MyParameterBuilder pb = new MyParameterBuilder();

    if (somethingValueSomewhere == 1) 

      pb.AppendEquals(MyColumn.Something, "ABC");

    if (someFlag)

      pb.AppendEquals(MyColumn.OtherThing, "DEF");

    i f (someOtherFlag)

       pb.AppendEquals(MyColumn.YetAnotherThing, "GHI");

     

    In the above, all the conditions might be true, none of them might be true, or any combination might be true.

    It's almost impossible to get the junctions working right without painstakingly keeping track of whether or not you've previously added any criteria. Otherwise, the builder winds up putting the junction text before the first parameter and you wind up with stuff like WHERE AND Something='ABC'. I really shouldn't have to do that. The SqlParameterBuilder should know not to put a junction before the first item in a group.

    -Bryan

    P.S. How are you pasting code with syntax highlighting like you did above?? 

    • Post Points: 35
  • 05-28-2008 6:14 AM In reply to

    Re: Problems with ParameterBuilder and .BeginGroup

    of course you have to check. (like you have to chekc for possible exceptions)

     

    that's normal

    watch my last post --> very dynamical creating of query

     

    what you can do.

    check the last char in query:

    query.ToString().TrimEnd.EndsWith(...)

    if end with '(' --> .Append("",...,...)

    id end with ')' --> Append("AND",...,...) 

    • Post Points: 35
  • 05-28-2008 9:02 AM In reply to

    • bslatner
    • Top 500 Contributor
    • Joined on 10-26-2007
    • Charlotte, NC
    • Posts 19
    • Points 485

    Re: Problems with ParameterBuilder and .BeginGroup

    I completely disagree that it's "normal" in any way. It seems very simple to me for the SqlParameterBuilder to do this for me. Whether or not the criterion I am adding is at the beginning of a group is a matter of object state. Objects are supposed to keep track of their OWN state. I shouldn't have to do it for them.
    • Post Points: 35
  • 05-28-2008 9:18 AM In reply to

    Re: Problems with ParameterBuilder and .BeginGroup

    you're right.Yes

     

    but don't forget: .netTiers is open source 

    feel free to help or to pay for someone who patch your need..Cool

    • Post Points: 35
  • 05-28-2008 9:33 AM In reply to

    • bslatner
    • Top 500 Contributor
    • Joined on 10-26-2007
    • Charlotte, NC
    • Posts 19
    • Points 485

    Re: Problems with ParameterBuilder and .BeginGroup

     I intend to fix it myself, I just haven't gotten around to it Stick out tongue But I want to do it in such a way that it doesn't break people's existing code.

    • Post Points: 5
Page 1 of 1 (12 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems