First they ignore you
          then they ridicule you
          then they fight you
          then you win
              -- Mahatma Gandhi
          Chinese => English     英文 => 中文             
          隨筆-221  評(píng)論-1047  文章-0  trackbacks-0
          利用Groovy對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作是極其方便的,有時(shí)為了熟悉數(shù)據(jù)庫(kù)中的表,需要將表結(jié)構(gòu)導(dǎo)出,并保存為EXCEL格式。
          下面所展示的源代碼就能夠很好的滿足我們的需求。(這段代碼依賴jxl和Oracle的jdbc驅(qū)動(dòng))


          功能保持不變的條件下,代碼做了一些小調(diào)整,利用Groovy中的強(qiáng)大特性Mixin,使代碼更優(yōu)雅。

          導(dǎo)出效果:
          表名 表注釋 字段名稱 是否主鍵 字段類型 字段長(zhǎng)度 整數(shù)位數(shù) 小數(shù)位數(shù) 允許空值 缺省值 字段注釋
          CUSTOMER 用戶表 USER_ID P VARCHAR2 10

          N
          客戶ID
          USER_BALANCE
          NUMBER 22 18 2 N 0 客戶余額
          USER_GENDER
          VARCHAR2 10

          Y
          客戶性別
          USER_BIRTHDAY
          DATE 7

          N
          客戶生日
          USER_NAME
          VARCHAR2 20

          N
          客戶名






















          LOG 日志表 R
          VARCHAR2 200

          Y
          結(jié)果
          D
          DATE 7

          Y
          時(shí)間


          conf.properties
          filename=table_structures.xls
          tables.to.export=%
          column.width=15
          url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
          user=DANIEL
          password=123456
          driver=oracle.jdbc.driver.OracleDriver


          GroovySql.groovy
          /*
          ?*?Copyright?2008?the?original?author?or?authors.
          ?*?
          ?*?Licensed?under?the?Apache?License,?Version?2.0?(the?"License");
          ?*?you?may?not?use?this?file?except?in?compliance?with?the?License.
          ?*?You?may?obtain?a?copy?of?the?License?at
          ?*?
          ?*???????
          http://www.apache.org/licenses/LICENSE-2.0
          ?*?
          ?*?Unless?required?by?applicable?law?or?agreed?to?in?writing,?software
          ?*?distributed?under?the?License?is?distributed?on?an?"AS?IS"?BASIS,
          ?*?WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY?KIND,?either?express?or?implied.
          ?*?See?the?License?for?the?specific?language?governing?permissions?and
          ?*?limitations?under?the?License.
          ?*
          ?*?Author:?山風(fēng)小子(
          http://www.aygfsteel.com/BlueSUN )
          ?*?Email?:?realbluesun@hotmail.com
          ?
          */ ?


          import ?groovy.sql.Sql
          import ?jxl. *
          import ?jxl.write. *

          Properties?properties?
          = ? new ?Properties();
          properties.load(
          new ?FileInputStream( " conf.properties " ));

          def?filename?
          = ?properties.getProperty( ' filename ' )
          def?tablesToExport?
          = ?properties.getProperty( ' tables.to.export ' )
          def?columnWidth?
          = ?properties.getProperty( ' column.width ' )
          def?url?
          = ?properties.getProperty( ' url ' )
          def?user?
          = ?properties.getProperty( ' user ' )
          def?password?
          = ?properties.getProperty( ' password ' )
          def?driver?
          = ??properties.getProperty( ' driver ' )

          def?sql?
          = ?Sql.newInstance(url,?user,?password,?driver)

          def?sqlStmt?
          = ? """
          ??select?
          ????????a.table_name,?
          ????????a.column_name,?
          ????????(select?
          ????????????d.constraint_type?
          ?????????from
          ????????????all_constraints?d,
          ????????????all_cons_columns?e
          ?????????where?
          ????????????c.owner?
          = ?d.owner?and
          ????????????d.owner?
          = ?e.owner?and
          ????????????c.table_name?
          = ?d.table_name?and
          ????????????d.table_name?
          = ?e.table_name?and
          ????????????b.column_name?
          = ?e.column_name?and
          ????????????d.constraint_name?
          = ?e.constraint_name?and
          ????????????d.constraint_type?
          = ? ' P ' ?and?
          ????????rownum?
          = ? 1
          ????????)?as?constraint_type,
          ????????a.data_type,?
          ????????a.data_length,?
          ????????a.data_precision,?
          ????????a.data_scale,?
          ????????a.nullable,?
          ????????a.data_default,?
          ????????b.comments,
          ????????c.comments?as?tab_comments
          ???from?
          ????????all_tab_columns?a,?
          ????????all_col_comments?b,
          ????????all_tab_comments?c
          ???where?
          ????????a.owner?
          = ?b.owner?and
          ????????b.owner?
          = ?c.owner?and
          ????????a.table_name??
          = ??b.table_name?and
          ????????b.table_name??
          = ??c.table_name?and
          ????????a.column_name??
          = ??b.column_name?and
          ????????a.table_name?like??
          ? ??and?
          ????????a.owner?
          = ? ? ?
          """

          Map?tables?
          = ? new ?HashMap()

          sql.eachRow(sqlStmt,?[tablesToExport,?user]){?row?
          ->

          ????Map?column?
          = ? new ?HashMap()
          ????column.put(
          ' column_name ' ,?row.column_name);
          ????column.put(
          ' constraint_type ' ,?row.constraint_type);
          ????column.put(
          ' data_type ' ,?row.data_type);
          ????column.put(
          ' data_length ' ,?row.data_length);
          ????column.put(
          ' data_precision ' ,?row.data_precision);
          ????column.put(
          ' data_scale ' ,?row.data_scale);
          ????column.put(
          ' nullable ' ,?row.nullable);
          ????column.put(
          ' data_default ' ,?row.data_default);
          ????column.put(
          ' comments ' ,?row.comments);


          ????String?tableName?
          = ?row.table_name
          ????String?tableComments?
          = ?row.tab_comments
          ????Set?columns?
          = ?tables.get(tableName) ? .columns
          ????
          ????
          if ?( null ? == ?columns)?{
          ????????columns?
          = ? new ?HashSet();
          ????????columns?
          << ?column
          ????????
          ????????tables.put(tableName,?[tableComments:tableComments,?columns:columns])
          ????}?
          else ?{
          ????????columns?
          << ?column
          ????}
          }

          println?
          " to?export?table?structures "

          class ?WritableSheetCategory?{
          ????
          static ?insertRow(WritableSheet?writableSheet,?List?row,? int ?x,? int ?y)?{
          ????????row.eachWithIndex?{?col,?i?
          ->
          ????????????Label?cell?
          = ? new ?Label(x? + ?i,?y,?col)
          ????????????writableSheet.addCell(cell)
          ????????}
          ????}????
          }


          WritableWorkbook?writableWorkBook?
          = ?
          ????Workbook.createWorkbook(
          ????????
          new ?File(filename))
          WritableSheet?writableSheet?
          = ?writableWorkBook.createSheet( " 第一頁(yè) " ,? 0 )


          WritableFont?writableFontForTableName?
          =
          ????
          new ?WritableFont(WritableFont.TIMES,? 10 ,?WritableFont.BOLD)
          WritableCellFormat?writableCellFormatForTableName?
          = ?
          ????
          new ?WritableCellFormat(writableFontForTableName)
          // writableCellFormatForTableName.setAlignment(jxl.format.Alignment.CENTRE)
          writableCellFormatForTableName.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE)


          WritableFont?writableFontForTableComments?
          =
          ????
          new ?WritableFont(WritableFont.TIMES,? 10 ,?WritableFont.NO_BOLD)
          WritableCellFormat?writableCellFormatForTableComments?
          = ?
          ????
          new ?WritableCellFormat(writableFontForTableComments)
          // writableCellFormatForTableComments.setAlignment(jxl.format.Alignment.CENTRE)
          writableCellFormatForTableComments.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE)


          int ?line? = ? 0
          List?titleRow?
          = ?[
          ????
          ' 表名 ' ,? ' 表注釋 ' ,? ' 字段名稱 ' ,? ' 是否主鍵 ' ,? ' 字段類型 ' ,? ' 字段長(zhǎng)度 ' ,? ' 整數(shù)位數(shù) ' ,? ' 小數(shù)位數(shù) ' ,? ' 允許空值 ' ,? ' 缺省值 ' ,? ' 字段注釋 '
          ]


          try ?{
          ????columnWidth?
          = ?Integer.parseInt(columnWidth)?
          }?
          catch ?(Exception?e)?{
          ????columnWidth?
          = ? 15
          ????System.err.println(e.getMessage())
          }
          for ?( int ?i? = ? 0 ;?i? < ?titleRow.size();?i ++ )?{
          ????writableSheet.setColumnView(i,?columnWidth)
          }

          use?(WritableSheetCategory)?{
          ????writableSheet.insertRow(titleRow,?line
          ++ ,? 0 )
          }

          tables.each?{?tableName,?tableInfo?
          ->
          ????String?tableComments?
          = ?tableInfo.tableComments
          ????Set?columns?
          = ?tableInfo.columns

          ????Label?tableNameCell?
          = ? new ?Label( 0 ,?line,?tableName,?writableCellFormatForTableName)
          ????writableSheet.addCell(tableNameCell)

          ????Label?tableCommentsCell??
          = ? new ?Label( 1 ,?line,?tableComments? ? ? "" ? + ?tableComments?:? "" ,?writableCellFormatForTableComments)
          ????writableSheet.addCell(tableCommentsCell)

          ????columns.each?{?column?
          ->
          ????????List?row?
          = ?[
          ????????????column.column_name?
          ? ? "" ? + ?column.column_name?:? "" ,
          ????????????column.constraint_type?
          ? ? "" ? + ?column.constraint_type?:? "" ,
          ????????????column.data_type?
          ? ? "" ? + ?column.data_type?:? "" ,
          ????????????column.data_length?
          ? ? "" ? + ?column.data_length?:? "" ,
          ????????????column.data_precision?
          ? ? "" ? + ?column.data_precision?:? "" ,
          ????????????column.data_scale?
          ? ? "" ? + ?column.data_scale?:? "" ,
          ????????????column.nullable?
          ? ? "" ? + ?column.nullable?:? "" ,
          ????????????column.data_default?
          ? ? "" ? + ?column.data_default?:? "" ,
          ????????????column.comments?
          ? ? "" ? + ?column.comments?:? ""
          ????????]
          ????????
          ????????use?(WritableSheetCategory)?{
          ????????????writableSheet.insertRow(row,?
          2 ,?line ++ )
          ????????}
          ????}

          ????writableSheet.mergeCells(
          0 ,?line? - ?columns.size(),? 0 ,?line? - ? 1 )
          ????writableSheet.mergeCells(
          1 ,?line? - ?columns.size(),? 1 ,?line? - ? 1 )

          ????line?
          += ? 2
          }

          writableWorkBook.write();
          writableWorkBook.close();

          println?
          " done! "



          附:朝花夕拾——Groovy & Grails
          posted on 2008-01-26 20:05 山風(fēng)小子 閱讀(4087) 評(píng)論(2)  編輯  收藏 所屬分類: Groovy & Grails
          主站蜘蛛池模板: 揭西县| 宁城县| 南安市| 玛沁县| 汶上县| 福贡县| 易门县| 红安县| 呈贡县| 崇礼县| 肇庆市| 上思县| 攀枝花市| 正镶白旗| 余干县| 福安市| 绥棱县| 阿城市| 淅川县| 南陵县| 庆安县| 合水县| 龙川县| 抚远县| 陕西省| 桃江县| 汾西县| 合水县| 临沭县| 陇南市| 抚州市| 库车县| 白城市| 苍溪县| 宜都市| 礼泉县| 赤城县| 修水县| 祁门县| 济宁市| 剑阁县|