1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Iterator;
public class GenerateExportScript {
private String myDB;
private String myHost;
private String myTable;
private Connection myConnexion;
private String driversPost = "org.postgresql.Driver";
private String urlPost = "jdbc:postgresql://";
static private String baseDir;
static private File outputDirect;
StringBuffer outputWithData = new StringBuffer();
StringBuffer outputWithoutData = new StringBuffer();
StringBuffer outputWithDataImport = new StringBuffer();
StringBuffer outputWithoutDataImport = new StringBuffer();
public static void main(String[] args){
String myHost = "127.0.0.1";
baseDir = "C:\\Dump"+myHost+"\\";
outputDirect = new File(baseDir);
//String[] listTableDB = { "ma_DB","ma_Table" };
String[] listTableDB = { "Ma_DB","#all" };
new GenerateExportScript(myHost, listTableDB[0], listTableDB[1]);
}
public GenerateExportScript(String myHost, String myDB, String myTable) {
this.myDB = myDB;
this.myHost = myHost;
this.myTable = myTable;
try {
Class.forName(driversPost);
myConnexion = DriverManager.getConnection(urlPost + myHost + ":5432/" + myDB, "User", "Pass");
System.out.println("Batch file body");
String host;
if ("127.0.0.1".equals(myHost)){
host = "";
}else{
host = "-h "+myHost;
}
File directory = null;
File directoryData = null;
directory = new File(outputDirect, myDB);
directory.mkdirs();
directoryData = new File(directory, "Data");
directoryData.mkdirs();
if(myTable == "#all"){
HashMap tablesFirst = getTables(myConnexion);
Iterator listTBF = tablesFirst.keySet().iterator();
while (listTBF.hasNext()) {
String table = (String) listTBF.next();
dumpCommand(table);
}
}else{
dumpCommand(myTable);
}
File batFileData = new File(directoryData, "Dump.bat");
FileOutputStream batFileDataOS = new FileOutputStream(batFileData);
System.out.println(outputWithData.toString());
batFileDataOS.write(outputWithData.toString().getBytes());
batFileDataOS.flush();
batFileDataOS.close();
//------
File batFileDataImport = new File(directoryData, "Import.bat");
FileOutputStream batFileDataOSImport = new FileOutputStream(batFileDataImport);
System.out.println(outputWithDataImport.toString());
batFileDataOSImport.write(outputWithDataImport.toString().getBytes());
batFileDataOSImport.flush();
batFileDataOSImport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void dumpCommand(String myTable){
this.myTable = myTable;
outputWithData.append("pg_dump -D -U arthur "+myHost+" -c -t"+myTable+" "+myDB+" > "+myTable+".sql");
outputWithData.append('\r');
outputWithData.append('\n');
}
private HashMap getTables(Connection conn) throws SQLException{
HashMap tables = new HashMap();
DatabaseMetaData dbMD = conn.getMetaData();
String[] types = { "TABLE", "" };
ResultSet res = dbMD.getTables("public", "", null, types);
while (res.next()) {
String tableName = res.getString("table_name");
HashMap fields = new HashMap();
ResultSet res2 = dbMD.getColumns("public", "", tableName, null);
while (res2.next()) {
String column = res2.getString("COLUMN_NAME");
int type = res2.getInt("DATA_TYPE");
int size = res2.getInt("COLUMN_SIZE");
int decimal = res2.getInt("DECIMAL_DIGITS");
String deff = res2.getString("COLUMN_DEF");
String comment = res2.getString("REMARKS");
String typeName = res2.getString("TYPE_NAME");
fields.put(column, new Field(type, size, decimal, typeName, deff, comment));
}
tables.put(tableName, fields);
}
return tables;
}
private class Field {
private int type;
private int size;
private int decimal;
private String typeName;
private String deff;
private String remark;
public int getSize(){
return size;
}
public int getType(){
return type;
}
public Field(int type, int size, int decimal, String typeName, String deff, String remark) {
this.size = size;
this.decimal = decimal;
this.type = type;
this.typeName = typeName;
this.deff = deff;
}
public String toString(){
return "type=" + type + ", size=" + size + ", decimal=" + decimal + ", Type Name=" + typeName + ", Default Value=" + deff;
}
public boolean equals(Object obj){
if (obj instanceof Field) {
Field cp = (Field) obj;
return (cp.getType() == type && cp.getSize() == size);
} else {
return false;
}
}
public String getDeff(){
return deff;
}
public String getTypeName(){
return typeName;
}
public int getDecimal(){
return decimal;
}
public String getRemark(){
return remark;
}
}
} |
Partager