1 package migratool.datasources;
2
3 import java.sql.Connection;
4 import java.sql.ResultSet;
5 import java.sql.ResultSetMetaData;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8 import java.util.ArrayList;
9
10 import migratool.access.Connector;
11 import migratool.model.BDdescription;
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 public abstract class AbstractDestinationDataSource {
39
40 protected Connector dbAccess;
41 protected Connection dbConnection;
42 protected BDdescription destinationDescrition;
43 public static int INITIAL_COLUMN = 1;
44
45
46
47
48 public void closeConnetion() throws MigDestinationDataSourceException {
49 try {
50 dbConnection.close();
51 } catch(SQLException sqlexp) {
52 throw new MigDestinationDataSourceException(sqlexp);
53 }
54
55 }
56
57 public abstract void addData(ArrayList data) throws MigDestinationDataSourceException;
58
59
60
61
62
63
64 protected String generateVariables(int quantVariable) {
65 String toReturn = "(?";
66 for (int variables = 1; variables < quantVariable; variables++) {
67 toReturn = toReturn + ",?";
68 }
69 toReturn = toReturn + ")";
70 return toReturn;
71 }
72
73 public boolean testDataBase() {
74 boolean success = false;
75 ResultSet rs = null;
76 Statement stmt = null;
77 ResultSetMetaData rsmd = null;
78 if (dbConnection != null)
79 success = true;
80
81 try {
82 stmt = dbConnection.createStatement();
83 rs =
84 stmt.executeQuery(
85 "select * from " + destinationDescrition.getTableName());
86 rsmd = rs.getMetaData();
87 } catch (SQLException e) {
88 e.printStackTrace();
89 success = false;
90 }
91
92 if (rsmd == null)
93 success = false;
94
95 try {
96 rs.close();
97 stmt.close();
98 } catch (SQLException e1) {
99 e1.printStackTrace();
100 success = false;
101 }
102
103 return success;
104 }
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 protected String generateColumnNames(ResultSetMetaData rsmd)
122 throws SQLException {
123 String toReturn = " (";
124 String tempTableName = null;
125 String tempTableType = null;
126 int quantColumn = rsmd.getColumnCount();
127
128 tempTableName = rsmd.getColumnName(INITIAL_COLUMN);
129 tempTableType = rsmd.getColumnTypeName(INITIAL_COLUMN);
130 toReturn += tempTableName + " " + tempTableType;
131
132 for (int columCount = 2; columCount <= quantColumn; columCount++) {
133 tempTableName = rsmd.getColumnName(columCount);
134 tempTableType = rsmd.getColumnTypeName(columCount);
135 toReturn += ", " + tempTableName + " " + tempTableType;
136 }
137 toReturn += ");";
138 return toReturn;
139 }
140
141 }