2014年7月24日 星期四

iDempiere groovy metadata arrary

import groovy.sql.Sql
import java.sql.Timestamp
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.Types
import org.compiere.util.Env
import org.compiere.util.DB

int client=Env.getContextAsInt(A_Ctx, "AD_Client_ID");
int org=Env.getContextAsInt(A_Ctx, "AD_Org_ID");
int uid=Env.getContextAsInt(A_Ctx, "#AD_User_ID");

def t_start= System.currentTimeMillis()

// def oracle = Sql.newInstance('jdbc:oracle:thin:@127.0.0.1:1521:orcl','c##ad',,'c##ad','oracle.jdbc.driver.OracleDriver')

def postsql = Sql.newInstance('jdbc:postgresql://127.0.0.1/daily','adempiere','adempiere','org.postgresql.Driver' )

// def ptable= postsql.dataSet("h_searchline")   //上傳

// def informix = Sql.newInstance( 'jdbc:informix-sqli://192.168.170.211:1534/hisopddb:informixserver=hisopdsrv;CLIENT_LOCALE=zh_tw.big5;','informix','db@w94u6', 'com.informix.jdbc.IfxDriver' );
// def metadata = informix.connection.metaData

def metadata = postsql.connection.metaData

String catalog = null;
String schemaPattern = "adempiere";
String tableNamePattern = "ad_table%"
String[] tableTypes = ["TABLE"]
String columnNamePattern = null;
ResultSet rs = metadata.getTables(catalog , schemaPattern, tableNamePattern, tableTypes );
// "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
String tblName = null;
println   "============================== \t";
rs = metadata.getTables(catalog , schemaPattern, tableNamePattern, tableTypes );
while(rs.next()){
   tblName = rs.getString('TABLE_NAME');
   println ">>>>>> "+tblName
   def colList = [][]
   ResultSet tblMetadata = metadata.getColumns(catalog, schemaPattern, tblName, columnNamePattern );
   while(tblMetadata.next()){
      String  TABLE_CAT      = tblMetadata.getString("TABLE_CAT");
      String TABLE_SCHEM  = tblMetadata.getString("TABLE_SCHEM");
      String TABLE_NAME    = tblMetadata.getString("TABLE_NAME");
      String COLUMN_NAME= tblMetadata.getString("COLUMN_NAME");
      int       DATA_TYPE      = tblMetadata.getInt("DATA_TYPE");
      String TYPE_NAME      = tblMetadata.getString ("TYPE_NAME");
      int       COLUMN_SIZE  = tblMetadata.getInt("COLUMN_SIZE");
      int       DECIMAL_DIGITS  = tblMetadata.getInt("DECIMAL_DIGITS");
      int       NULLABLE             = tblMetadata.getInt("NULLABLE");
      String str = "";
 if         (TYPE_NAME=='decimal')    str = COLUMN_NAME  + " DECIMAL   (" + COLUMN_SIZE+ ", " + DECIMAL_DIGITS +")";
 else if (TYPE_NAME=='char')         str =  COLUMN_NAME + " CHAR        (" + COLUMN_SIZE+ ")"; // informix
 else if (TYPE_NAME=='varchar')    str =  COLUMN_NAME + " VARCHAR  (" + COLUMN_SIZE+ ")"; // postgres
 else if (TYPE_NAME=='varying character')    str =  COLUMN_NAME + " VARCHAR  (" + COLUMN_SIZE+ ")"; // postgres
 else if (TYPE_NAME=='bpchar')     str =  COLUMN_NAME + " CHAR        (" + COLUMN_SIZE+ ")"; // postgres
 else if (TYPE_NAME=='numeric')    str =  COLUMN_NAME + " NUMERIC  (" + COLUMN_SIZE+ ")"; // postgres
 else if (TYPE_NAME=='timestamp')  str = COLUMN_NAME + " DATE";  // postgres
 else if (TYPE_NAME=='datetime')    str = COLUMN_NAME + "  DATE";
 else if (TYPE_NAME=='date')          str = COLUMN_NAME  + " DATE";
 else                                                 str = COLUMN_NAME  + "   " + TYPE_NAME + " ";
 // colList .add( [str] )
    colList .add( [ COLUMN_NAME, TYPE_NAME,  " "+COLUMN_SIZE, " "+DECIMAL_DIGITS ] )
   } // while(tblMetadata.next())

   (0.. (colList.size() -1) ).each { i ->
     //   println  colList.get(i)
        println  colList [i][0]+" "+colList [i][1]+" "+colList [i][2]+" "+colList [i][3]
   }

} // while(rs.next())
def t_end = System.currentTimeMillis()
result="執行完成: " + (t_end - t_start)

沒有留言:

張貼留言