View Javadoc

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   * MIGRATOOL Program to migrate spatial databsets.
16   * Copyright (C) 2007 Fábio Luiz Leite Júnior
17   * Universidade Federal de Campina Grande
18   * contact: fabioleite@gmail.com
19   *
20   * This program is free software; you can redistribute it and/or
21   * modify it under the terms of the GNU General Public License
22   * as published by the Free Software Foundation; either version 2
23   * of the License, or (at your option) any later version.
24   *
25   * This program is distributed in the hope that it will be useful,
26   * but WITHOUT ANY WARRANTY; without even the implied warranty of
27   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28   * GNU General Public License for more details.
29   *
30   * You should have received a copy of the GNU General Public License
31   * along with this program; if not, write to the Free Software
32   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
33   *
34   * @author fabio
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  	 * @param i
62  	 * @return
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 //	 * @param rsmd
108 //	 */
109 //	public void createSchema(ResultSetMetaData rsmd) throws SQLException {
110 //		String createScript = "create table " + rsmd.getTableName(1);
111 //		String columNames = generateColumnNames(rsmd);
112 //		createScript += columNames;
113 //		System.out.println("create consulta - " + createScript);
114 //
115 //	}
116 
117 	/**
118 	 * @param rsmd
119 	 * @return
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 }