-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Suggestion: adding multiple parameters
PostPosted: Fri Nov 14, 2003 1:25 pm 
Newbie

Joined: Fri Nov 07, 2003 10:58 am
Posts: 14
Location: Albuquerque, NM
If this is already there and I am missing it, sorry...

I am going to make a few different suggestions related to how queries are specified... just something to think about.

I freqently build dynamic queries where the number of parameters change each time. Currently I am saving all of the parameters up in a HashMap where the key is the parameter name and the value is the parameter. Then after I create the query object (from session.createQuery(sHql)) I add the parameters. It would be cool if I could just hand a query.setParametersList(HashMap) and it reads the keys and values to build up the parameter list.

Alternatively, have a seperate object which is a parameter list which behaves like query with respect to .setString(key,value), and the others types then hand the parameter list to query. Make sense?

The final suggestion would be to be more like a sql interface class that I wrote which makes passing parameters very handy. Syntax is like:

Code:
   SqlRunner sql = null;
   try {
      sql = new SqlRunner();
      sql.setSql("INSERT INTO aces_equipment (");
      sql.addSql("site_undo,");
      sql.addSql("status_undo,");
      sql.addSql("uniqueNum,");
      sql.addSql("id,", v.getNewId());
      sql.addSql("type,", v.getType());
      sql.addSql("home,", v.getHome());
      sql.addSql("site,", v.getSite());
      sql.addSql("status,", v.getStatus());
      sql.addSql("retired", v.getRetired());
      sql.addSql(") VALUES (null,null,aces_seq_equipment_id.nextval,?,?,?,?,?,?)");
      sql.executeUpdate();

      sql.setSql("DELETE FROM aces_comments");
      sql.addSql("WHERE equipmentid=?", v.getNewId());
      sql.executeUpdate();
   } finally {
      if (sql != null)
         sql.close();
   }

///-------

   SqlRunner sql = null;
   try {
      sql.setSql("SELECT ");
      sql.addSql("e.uniqueNum,e.id,e.type,e.home,e.site");
      sql.addSql("FROM aces_equipment e");
      sql.addSql("WHERE e.id=? AND e.home=?",sLookup);
      sql.addParameter(sHome);
      ResultSet rs = sql.executeQuery();
      while(rs.next()) {
         //Do something
      }
   } finally {
      if (sql != null)
         sql.close();
   }



The keys here is that the sql can be easily built on multiple lines, the first part of the sql is added with setSql(), subsequent with addSql().

You can add parameters with the optional second parameter of setSql or addSql. The second param can take all of the primitive types and other specific object types (Long, Integer, Timestamp, etc.) and it detects the exact parameter type. Nice for all statements, you can easily see which parameter goes where, but especially nice with regards to INSERT because the parameters line up nicely with where they are attached to.

This method also makes building the statement up dynamically (some parts are added sometimes, some are omitted) and having the parameters line up easy to do.

Just some thoughts.
Kevin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.