开发者

groovy sql framework

Hi I want to design a class using Groovy Sql which I will be able to use in any of my future db related projects. At present I came up with the below class, which has separate functions for individual queries, I want to learn how can I make it generic! So that I just need to pass 1 query and some bind parameters (if not pass null) Please help me get a solution

My code

import java.sql.*;
import java.util.List;
import groovy.sql.Sql

public class ProductInfo {

  ReadProperty prop

  def sql
  public ProductInfo()
  {
    prop=ReadProperty.getInstance("db")
    sql = Sql.newInstance("jdbc:oracle:thin:@"+prop.getProperty("hostname")+":"+prop.getProperty("port")+":"+prop.getProperty("service"), "asimonc", "asimon","oracle.jdbc.OracleDriver")
  }

  public List executeSelection(String query)
  {
    List result=new ArrayList()
    sql.eachRow(query)
    { 
      String[] rows=new String[5]
      rows[0]=(String)it.id
      rows[1]=(String)it.name
      rows[2]=(String)it.description
      rows[3]=(String)it.active
      rows[4]=(String)it.release_date

      result.add(rows)
    }
    return result
  }

  public executeInsert(String query,Object[] paramValues)
  {
    sql.execute(quer开发者_开发技巧y,[paramValues[0], paramValues[1],paramValues[2], paramValues[3], paramValues[4]])
  }

  public executeUpdation(String query,Object[] paramValues)
  {
    sql.executeUpdate(query,[paramValues[1], paramValues[2],paramValues[4],paramValues[5], paramValues[0]])
  }

  public int executeSelectMax(String query)
  {
    int max     
    sql.eachRow(query) 
    {
      max=it.max
    }
    if(max==null)
      return 0
    else
      return max
  }
}

My oracle table

CREATE TABLE PRODUCTINFO
(     "ID" NUMBER NOT NULL ENABLE, 
      "NAME" NVARCHAR2(200), 
      "DESCRIPTION" NVARCHAR2(200), 
      "ACTIVE" NVARCHAR2(2), 
      "RELEASE_DATE" DATE, 
       PRIMARY KEY ("ID")
)

for finding out max id (selection)

ProductInfo pinfo= new ProductInfo();
int max=pinfo.executeSelectMax("select max(id) as max from productinfo");

for updation

Object[] paramValues={iid,name,desc,"A",date,active};
pinfo.executeUpdation("update productinfo set name=?, description=?, release_date=?, active=? where id=?",paramValues);

for insertion

Object[] paramValues={max+1,name,desc,"A",date};
pinfo.executeInsert("insert into productinfo(ID,NAME,DESCRIPTION,ACTIVE,RELEASE_DATE) values (?,?,?,?,?)",paramValues);

for yet another selection

List result=pinfo.executeSelection("select ID,NAME,DESCRIPTION,ACTIVE,RELEASE_DATE from productinfo where ACTIVE='A'");


How about:

import groovy.sql.Sql

public class ProductInfo {

  ReadProperty prop

  def sql
  public ProductInfo() {
    prop=ReadProperty.getInstance("db")
    sql = Sql.newInstance( "jdbc:oracle:thin:@"+prop.getProperty("hostname")+":"+prop.getProperty("port")+":"+prop.getProperty("service"), "asimonc", "asimon","oracle.jdbc.OracleDriver")
  }

  public List executeSelection(String query) {
    List result = []
    sql.eachRow(query) { 
      result << [ it.id, it.name, it.description, it.active, it.release_date ]
    }
    result
  }

  public void executeInsert( GString query ) {
    sql.execute( query )
  }

  public void executeUpdation( GString query ) {
    sql.executeUpdate( query )
  }

  public int executeSelectMax( String query ) {
    sql.firstRow(query)?.max ?: 0
  }
}

Then, your update and insert examples become:

update:

pinfo.executeUpdation( "update productinfo set name=$name, description=$desc, release_date=$date, active=${'A'} where id=$iid" )

insert:

pinfo.executeInsert( "insert into productinfo(ID,NAME,DESCRIPTION,ACTIVE,RELEASE_DATE) values (${max+1}, $name, $desc, ${'A'}, $date )" )

As you can see... a lot of your code is simply wrapping things that already exist in the groovy.sql.Sql class

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜