Re: Parameters with SQLTemplate

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Tue Nov 23 2004 - 14:38:08 EST

  • Next message: Williams, Beth: "RE: Parameters with SQLTemplate"

    Beth,

    Method "queryWithParameters" clones the original query and returns a new
    instance without changing the original object. So
    "query.queryWithParameters(parameters)" should be replaced with

       query = query.queryWithParameters(parameters);

    Alternativey you can change the original using "void setParameters" like
    this:

       query.setParameters(parameters);

    Andrus

    > I am trying to use a SQLTemplate with some raw sql that incorporates
    > parameters. Below is the code that I'm using, however, it appears that
    > the parameters are not being passed to the query for some reason.
    >
    > **** Code ******
    > DataContext cayenneContext = DataContext.createDataContext();
    > GregorianCalendar gCalendarDate = new GregorianCalendar(2004, 9, 16);
    > Date asOfWeekEndingDate = gCalendarDate.getTime();
    > Map parameters = Collections.singletonMap("date", asOfWeekEndingDate);
    > System.out.println(parameters);
    >
    > String rawSql = "SELECT * FROM tecc WHERE start_dt <= $date " +
    > "AND (stop_dt IS NULL OR stop_dt >= $date " +
    > "AND EXISTS (SELECT terr_id FROM invt_mgt_summary " +
    > "WHERE aggr_lvl = 'TERR' AND we_dt = $date " +
    > "AND terr_id = tecc.terr_id)";
    > SQLTemplate query = new SQLTemplate(TerritoryOwnership.class, rawSql,
    > true); query.queryWithParameters(parameters);
    > List results = cayenneContext.performQuery(query);
    > *****************
    >
    > I get an invalid character SQLException. Here is the query that it is
    > trying to run. As you can see, it has not replace the $date with the
    > appropriate value.
    >
    > ***** SQL ******
    > SELECT * FROM tecc WHERE start_dt <= $date AND (stop_dt IS NULL OR
    > stop_dt >= $date AND EXISTS (SELECT terr_id FROM invt_mgt_summary WHERE
    > aggr_lvl = 'TERR' AND we_dt = $date AND terr_id = tecc.terr_id)
    > ***********
    >
    > When I do a "query.getParameters();" after the queryWithParameters() is
    > call it shows no parameters. What am I doing wrong? Any help would be
    > much appreciated. Thanks.
    >
    > Beth



    This archive was generated by hypermail 2.0.0 : Tue Nov 23 2004 - 14:38:10 EST