dbquery

Query RDBMS and return S-expression
git clone https://logand.com/git/dbquery.git/
Log | Files | Refs | README

DbQuery.java (5436B)


      1 // dbquery - Query RDBMS, result as S-expression
      2 // Copyright (C) 2008 Tomas Hlavaty
      3 
      4 // This program is free software: you can redistribute it and/or modify
      5 // it under the terms of the GNU General Public License as published by
      6 // the Free Software Foundation, either version 3 of the License, or
      7 // (at your option) any later version.
      8 //
      9 // This program is distributed in the hope that it will be useful,
     10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 // GNU General Public License for more details.
     13 //
     14 // You should have received a copy of the GNU General Public License
     15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 
     17 // Usage:
     18 // $ java -cp "<.>:<jdbc.jar>" DbQuery <driver> <cstr> <username> <pwd> <query>
     19 
     20 // Postgres:
     21 //   postgresql-8.3-603.jdbc4.jar
     22 //   org.postgresql.Driver
     23 //   jdbc:postgresql:<sid>
     24 
     25 // Oracle:
     26 //   classes12.jar
     27 //   oracle.jdbc.driver.OracleDriver
     28 //   jdbc:oracle:thin:@<host>:<port>:<sid>
     29 
     30 // MySQL:
     31 //   com.mysql.jdbc.Driver
     32 //   jdbc:mysql://<host>:<port>/<sid>
     33 
     34 import java.io.*;
     35 import java.sql.*;
     36 
     37 public class DbQuery {
     38 
     39     public static void main(String[] args) throws Exception {
     40         String driver = args[0];
     41         String cstr = args[1];
     42         String username = args[2];
     43         String pwd = args[3];
     44         String query = args[4];
     45 
     46         Class.forName(driver);
     47         Connection conn = DriverManager.getConnection(cstr, username, pwd);
     48         Statement stmt = conn.createStatement();
     49         ResultSet rs = stmt.executeQuery(query);
     50         System.out.println("(");
     51         printHeader(rs);
     52         printBody(rs);
     53         System.out.println(")");
     54         rs.close();
     55         stmt.close();
     56         conn.close();
     57     }
     58 
     59     public static void printHeader(ResultSet rs) throws Exception {
     60         ResultSetMetaData rsm = rs.getMetaData();
     61         int ncols = rsm.getColumnCount();
     62         System.out.print("(");
     63         for(int i = 0; i < ncols; i++) {
     64             if(i == 0) {
     65                 System.out.print("(");
     66             } else {
     67                 System.out.print(" (");
     68             }
     69             print(rsm.getColumnName(i + 1));
     70             System.out.print(" ");
     71             print(rsm.getColumnTypeName(i + 1));
     72             if(i < ncols - 1) {
     73                 System.out.println(")");
     74             } else {
     75                 System.out.print(")");
     76             }
     77         }
     78         System.out.println(")");
     79     }
     80 
     81     public static void printBody(ResultSet rs) throws Exception {
     82         ResultSetMetaData rsm = rs.getMetaData();
     83         int ncols = rsm.getColumnCount();
     84         while(rs.next()) {
     85             System.out.print("(");
     86             for(int i = 0; i < ncols; i++) {
     87                 if(i > 0) {
     88                     System.out.print(" ");
     89                 }
     90                 printCell(rs, i);
     91                 if(i < ncols - 1) {
     92                     System.out.println();
     93                 }
     94             }
     95             System.out.println(")");
     96         }
     97     }
     98 
     99     public static void print(String s) {
    100         if(s != null) {
    101             s.replaceAll("\\\\", "\\\\\\\\");
    102             s.replaceAll("\"", "\\\\\"");
    103             System.out.print('"' + s + '"');
    104         } else {
    105             System.out.print("NIL");
    106         }
    107     }
    108 
    109     public static void printCell(ResultSet rs, int i) throws Exception {
    110         ResultSetMetaData rsm = rs.getMetaData();
    111         int col = i + 1;
    112         if("number".equalsIgnoreCase(rsm.getColumnTypeName(col))) {
    113             String val = rs.getString(i + 1);
    114             if(val != null) {
    115                 System.out.print(rs.getString(i + 1));
    116             } else {
    117                 System.out.print("NIL");
    118             }
    119         } else if("clob".equalsIgnoreCase(rsm.getColumnTypeName(col))) {
    120             String filename = readClob(rs, i);
    121             print(filename);
    122         } else if("blob".equalsIgnoreCase(rsm.getColumnTypeName(col))) {
    123             String filename = readBlob(rs, i);
    124             print(filename);
    125         } else {
    126             print(rs.getString(i + 1));
    127         }
    128     }
    129 
    130     public static String readClob(ResultSet rs, int i) throws Exception {
    131         String tempName = null;
    132         int col = i + 1;
    133         Clob lob = rs.getClob(col);
    134         if(lob != null) {
    135             File temp = File.createTempFile("dbq", ".tmp");
    136             FileOutputStream out = new FileOutputStream(temp);
    137             InputStream in = lob.getAsciiStream();
    138             byte[] buf = new byte[10 * 1024];
    139             int n;
    140             while((n = in.read(buf)) > 0) {
    141                 out.write(buf, 0, n);
    142             }
    143             out.close();
    144             in.close();
    145             tempName = temp.getAbsolutePath();
    146         }
    147         return tempName;
    148     }
    149 
    150     public static String readBlob(ResultSet rs, int i) throws Exception {
    151         String tempName = null;
    152         int col = i + 1;
    153         Blob lob = rs.getBlob(col);
    154         if(lob != null) {
    155             File temp = File.createTempFile("dbq", ".tmp");
    156             FileOutputStream out = new FileOutputStream(temp);
    157             InputStream in = lob.getBinaryStream();
    158             byte[] buf = new byte[10 * 1024];
    159             int n;
    160             while((n = in.read(buf)) > 0) {
    161                 out.write(buf, 0, n);
    162             }
    163             out.close();
    164             in.close();
    165             tempName = temp.getAbsolutePath();
    166         }
    167         return tempName;
    168     }
    169 }