|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2011 Zenika |
| 3 |
* |
| 4 |
* All rights reserved. This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* http://www.eclipse.org/legal/epl-v10.html |
| 8 |
* |
| 9 |
* Contributors: queinnec - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
package org.eclipse.datatools.enablement.postgresql.catalog.loaders; |
| 12 |
|
| 13 |
import java.sql.PreparedStatement; |
| 14 |
import java.sql.ResultSet; |
| 15 |
import java.sql.SQLException; |
| 16 |
import java.util.Collection; |
| 17 |
import java.util.List; |
| 18 |
|
| 19 |
import org.eclipse.datatools.connectivity.sqm.core.connection.ConnectionFilter; |
| 20 |
import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject; |
| 21 |
import org.eclipse.datatools.connectivity.sqm.loader.IConnectionFilterProvider; |
| 22 |
import org.eclipse.datatools.connectivity.sqm.loader.JDBCBaseLoader; |
| 23 |
import org.eclipse.datatools.connectivity.sqm.loader.SchemaObjectFilterProvider; |
| 24 |
import org.eclipse.datatools.modelbase.sql.schema.SQLSchemaFactory; |
| 25 |
import org.eclipse.datatools.modelbase.sql.schema.Sequence; |
| 26 |
|
| 27 |
/** |
| 28 |
* This class adds the ability to retrieve a list of sequences from a PostgreSQL |
| 29 |
* database. |
| 30 |
* |
| 31 |
* @author pierre.queinnec@zenika.com |
| 32 |
*/ |
| 33 |
public class PostgresSequenceLoader extends JDBCBaseLoader { |
| 34 |
|
| 35 |
private static final String SEQUENCE_QUERY = "SELECT c.relname FROM pg_class c WHERE c.relkind = 'S' ORDER BY c.relname"; //$NON-NLS-1$ |
| 36 |
private static final String SEQUENCE_NAME = "relname"; |
| 37 |
|
| 38 |
public PostgresSequenceLoader() { |
| 39 |
this(null); |
| 40 |
} |
| 41 |
|
| 42 |
public PostgresSequenceLoader(ICatalogObject catalogObject) { |
| 43 |
this(catalogObject, new SchemaObjectFilterProvider( |
| 44 |
ConnectionFilter.SEQUENCE_FILTER)); |
| 45 |
} |
| 46 |
|
| 47 |
public PostgresSequenceLoader(ICatalogObject catalogObject, |
| 48 |
IConnectionFilterProvider connectionFilterProvider) { |
| 49 |
|
| 50 |
super(catalogObject, connectionFilterProvider); |
| 51 |
} |
| 52 |
|
| 53 |
public void clearSequences(List sequences) { |
| 54 |
sequences.clear(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* This method loads and fills the containmentList with the sequences. In |
| 59 |
* addition every reference (to an found sequence) is removed from |
| 60 |
* existingSequences. Considered are only those sequences, that are owned by |
| 61 |
* the schema denoted by CatalogObject (should be an Schema). |
| 62 |
* |
| 63 |
* @param containmentList |
| 64 |
* List of new Sequences |
| 65 |
* @param existingSequences |
| 66 |
* List of old Sequences |
| 67 |
* @throws SQLException |
| 68 |
* In case of an database error |
| 69 |
*/ |
| 70 |
public void loadSequences(List containmentList, Collection existingSequences) |
| 71 |
throws SQLException { |
| 72 |
|
| 73 |
ResultSet rs = null; |
| 74 |
PreparedStatement stmt = null; |
| 75 |
try { |
| 76 |
initActiveFilter(); |
| 77 |
|
| 78 |
stmt = getCatalogObject().getConnection().prepareStatement( |
| 79 |
SEQUENCE_QUERY); |
| 80 |
rs = createResultSet(stmt); |
| 81 |
|
| 82 |
while (rs.next()) { |
| 83 |
String sequenceName = rs.getString(SEQUENCE_NAME); |
| 84 |
|
| 85 |
if (sequenceName == null || isFiltered(sequenceName)) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
Sequence sequence = (Sequence) getAndRemoveSQLObject( |
| 90 |
existingSequences, sequenceName); |
| 91 |
|
| 92 |
if (sequence == null) { |
| 93 |
sequence = processRow(rs); |
| 94 |
if (sequence != null) { |
| 95 |
containmentList.add(sequence); |
| 96 |
} |
| 97 |
} else { |
| 98 |
containmentList.add(sequence); |
| 99 |
if (sequence instanceof ICatalogObject) { |
| 100 |
((ICatalogObject) sequence).refresh(); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} finally { |
| 105 |
try { |
| 106 |
if (rs != null) { |
| 107 |
rs.close(); |
| 108 |
} |
| 109 |
|
| 110 |
} catch (SQLException e) { |
| 111 |
// ignored |
| 112 |
|
| 113 |
} finally { |
| 114 |
try { |
| 115 |
if (stmt != null) { |
| 116 |
stmt.close(); |
| 117 |
} |
| 118 |
} catch (SQLException e) { |
| 119 |
// ignored |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
protected Sequence processRow(ResultSet rs) throws SQLException { |
| 126 |
Sequence sequence = SQLSchemaFactory.eINSTANCE.createSequence(); |
| 127 |
sequence.setName(rs.getString(SEQUENCE_NAME).trim()); |
| 128 |
return sequence; |
| 129 |
} |
| 130 |
|
| 131 |
protected ResultSet createResultSet(PreparedStatement stmt) |
| 132 |
throws SQLException { |
| 133 |
try { |
| 134 |
// TODO decide wether to filter on the schema (temp sequences are |
| 135 |
// generated on a special schema, so decide wether to list them or |
| 136 |
// not) |
| 137 |
|
| 138 |
// it's expected that catalog object is an Schema |
| 139 |
// String schema = ((Schema) getCatalogObject()).getName(); |
| 140 |
// stmt.setString(1, schema); |
| 141 |
return stmt.executeQuery(); |
| 142 |
|
| 143 |
} catch (RuntimeException e) { |
| 144 |
SQLException error = new SQLException( |
| 145 |
"Error while retrieving catalog information (sequences)"); |
| 146 |
error.initCause(e); |
| 147 |
throw error; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
} |