|
Added
Link Here
|
| 1 |
package org.eclipse.datatools.enablement.msft.internal.sqlserver.loaders; |
| 2 |
|
| 3 |
import java.sql.ResultSet; |
| 4 |
import java.sql.SQLException; |
| 5 |
import java.sql.Statement; |
| 6 |
import java.util.Collection; |
| 7 |
import java.util.List; |
| 8 |
|
| 9 |
import org.eclipse.datatools.connectivity.sqm.core.definition.DataModelElementFactory; |
| 10 |
import org.eclipse.datatools.connectivity.sqm.core.definition.DatabaseDefinition; |
| 11 |
import org.eclipse.datatools.connectivity.sqm.core.rte.ICatalogObject; |
| 12 |
import org.eclipse.datatools.connectivity.sqm.core.rte.jdbc.JDBCUserDefinedFunction; |
| 13 |
import org.eclipse.datatools.connectivity.sqm.internal.core.RDBCorePlugin; |
| 14 |
import org.eclipse.datatools.connectivity.sqm.loader.IConnectionFilterProvider; |
| 15 |
import org.eclipse.datatools.connectivity.sqm.loader.JDBCRoutineLoader; |
| 16 |
import org.eclipse.datatools.modelbase.sql.routines.Routine; |
| 17 |
import org.eclipse.datatools.modelbase.sql.routines.SQLRoutinesPackage; |
| 18 |
import org.eclipse.datatools.modelbase.sql.routines.Source; |
| 19 |
import org.eclipse.datatools.modelbase.sql.schema.Database; |
| 20 |
import org.eclipse.emf.ecore.EClass; |
| 21 |
|
| 22 |
public class SQL2005RoutineLoader extends JDBCRoutineLoader { |
| 23 |
|
| 24 |
public static final String COLUMN_ROUTINE_NAME = "ROUTINE_NAME"; |
| 25 |
public static final String COLUMN_ROUTINE_CATALOG = "SPECIFIC_CATALOG"; |
| 26 |
public static final String COLUMN_ROUTINE_SCHEMA = "SPECIFIC_SCHEMA"; |
| 27 |
public static final String COLUMN_ROUTINE_TYPE = "ROUTINE_TYPE"; |
| 28 |
public static final String COLUMN_ROUTINE_DEFINITION = "ROUTINE_DEFINITION"; |
| 29 |
|
| 30 |
private IRoutineFactory mUserDefinedFunctionFactory; |
| 31 |
private IRoutineFactory mProcedureFactory; |
| 32 |
|
| 33 |
public static class SQL2005ProcedureFactory extends ProcedureFactory { |
| 34 |
|
| 35 |
private Database database = null; |
| 36 |
|
| 37 |
public void setDatabase (Database db) { |
| 38 |
database = db; |
| 39 |
} |
| 40 |
|
| 41 |
public void initialize(Routine routine, ResultSet rs) |
| 42 |
throws SQLException { |
| 43 |
String name = rs.getString(COLUMN_ROUTINE_NAME); |
| 44 |
|
| 45 |
// strip out the version indicator |
| 46 |
if (name.indexOf(";") > -1) |
| 47 |
name = name.substring(0, name.indexOf(";") -1 ); |
| 48 |
routine.setName(name); |
| 49 |
|
| 50 |
String source = rs.getString(COLUMN_ROUTINE_DEFINITION); |
| 51 |
loadSource(routine, source, database); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public static class SQL2005UserDefinedFunctionFactory extends SQL2005ProcedureFactory { |
| 56 |
|
| 57 |
public EClass getRoutineEClass() { |
| 58 |
return SQLRoutinesPackage.eINSTANCE.getUserDefinedFunction(); |
| 59 |
} |
| 60 |
|
| 61 |
protected Routine newRoutine() { |
| 62 |
return new JDBCUserDefinedFunction(); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public SQL2005RoutineLoader() { |
| 67 |
super(null); |
| 68 |
} |
| 69 |
|
| 70 |
public SQL2005RoutineLoader(ICatalogObject catalogObject) { |
| 71 |
super(catalogObject); |
| 72 |
} |
| 73 |
|
| 74 |
public SQL2005RoutineLoader(ICatalogObject catalogObject, |
| 75 |
IConnectionFilterProvider connectionFilterProvider) { |
| 76 |
super(catalogObject, connectionFilterProvider); |
| 77 |
mUserDefinedFunctionFactory = new SQL2005UserDefinedFunctionFactory(); |
| 78 |
mProcedureFactory = new SQL2005ProcedureFactory(); |
| 79 |
} |
| 80 |
|
| 81 |
public SQL2005RoutineLoader(ICatalogObject catalogObject, |
| 82 |
IConnectionFilterProvider connectionFilterProvider, |
| 83 |
IRoutineFactory udfFactory, IRoutineFactory spFactory) { |
| 84 |
super(catalogObject, connectionFilterProvider, udfFactory, spFactory); |
| 85 |
} |
| 86 |
|
| 87 |
protected ResultSet createResultSet() throws SQLException { |
| 88 |
String schemaName = getSchema().getName(); |
| 89 |
String catalogName = getSchema().getCatalog().getName(); |
| 90 |
String query = "select INFORMATION_SCHEMA.ROUTINES.SPECIFIC_CATALOG as \'" + COLUMN_ROUTINE_CATALOG + "\', " + |
| 91 |
"INFORMATION_SCHEMA.ROUTINES.SPECIFIC_SCHEMA as \'" + COLUMN_ROUTINE_SCHEMA + "\', " + |
| 92 |
"INFORMATION_SCHEMA.ROUTINES.ROUTINE_NAME as \'" + COLUMN_ROUTINE_NAME + "\', " + |
| 93 |
"INFORMATION_SCHEMA.ROUTINES.ROUTINE_DEFINITION as \'" + COLUMN_ROUTINE_DEFINITION + "\', " + |
| 94 |
"INFORMATION_SCHEMA.ROUTINES.ROUTINE_TYPE as \'" + COLUMN_ROUTINE_TYPE + "\' " + |
| 95 |
"from INFORMATION_SCHEMA.ROUTINES where " + |
| 96 |
"INFORMATION_SCHEMA.ROUTINES.SPECIFIC_CATALOG = \'" + catalogName + "\' and " + |
| 97 |
"INFORMATION_SCHEMA.ROUTINES.SPECIFIC_SCHEMA = \'" + schemaName + "\'"; |
| 98 |
if (getJDBCFilterPattern() != null |
| 99 |
&& getJDBCFilterPattern().length() > 0) { |
| 100 |
String filter = " AND ALIAS LIKE " + getJDBCFilterPattern();//$NON-NLS-1$ |
| 101 |
query = query + filter; |
| 102 |
} |
| 103 |
query = query + " ORDER BY " + COLUMN_ROUTINE_NAME; |
| 104 |
|
| 105 |
Statement s = getCatalogObject().getConnection().createStatement(); |
| 106 |
ResultSet r = s.executeQuery(query); |
| 107 |
return r; |
| 108 |
} |
| 109 |
|
| 110 |
protected boolean isProcedure(ResultSet rs) throws SQLException { |
| 111 |
return rs.getString(COLUMN_ROUTINE_TYPE).startsWith("P"); |
| 112 |
} |
| 113 |
|
| 114 |
public IRoutineFactory getProcedureFactory() { |
| 115 |
return this.mProcedureFactory; |
| 116 |
} |
| 117 |
|
| 118 |
public IRoutineFactory getUserDefinedFunctionFactory() { |
| 119 |
return this.mUserDefinedFunctionFactory; |
| 120 |
} |
| 121 |
|
| 122 |
protected Routine processRow(ResultSet rs) throws SQLException { |
| 123 |
ICatalogObject object = getCatalogObject(); |
| 124 |
Database database = object.getCatalogDatabase(); |
| 125 |
if (mProcedureFactory == null) { |
| 126 |
mProcedureFactory = new SQL2005ProcedureFactory(); |
| 127 |
((SQL2005ProcedureFactory) mProcedureFactory).setDatabase(database); |
| 128 |
} |
| 129 |
if (mUserDefinedFunctionFactory == null) { |
| 130 |
mUserDefinedFunctionFactory = new SQL2005UserDefinedFunctionFactory(); |
| 131 |
((SQL2005UserDefinedFunctionFactory) mUserDefinedFunctionFactory).setDatabase(database); |
| 132 |
} |
| 133 |
IRoutineFactory routineFactory = isProcedure(rs) ? mProcedureFactory |
| 134 |
: mUserDefinedFunctionFactory; |
| 135 |
return routineFactory.createRoutine(rs); |
| 136 |
} |
| 137 |
|
| 138 |
public void loadRoutines(List containmentList, Collection existingRoutines) |
| 139 |
throws SQLException { |
| 140 |
ResultSet rs = null; |
| 141 |
try { |
| 142 |
initActiveFilter(); |
| 143 |
ICatalogObject object = getCatalogObject(); |
| 144 |
Database database = object.getCatalogDatabase(); |
| 145 |
for (rs = createResultSet(); rs.next();) { |
| 146 |
String routineName = rs.getString(COLUMN_ROUTINE_NAME); |
| 147 |
if (routineName == null || isFiltered(routineName)) { |
| 148 |
continue; |
| 149 |
} |
| 150 |
Routine routine = (Routine) getAndRemoveSQLObject( |
| 151 |
existingRoutines, routineName); |
| 152 |
if (routine == null) { |
| 153 |
routine = processRow(rs); |
| 154 |
if (routine != null) { |
| 155 |
containmentList.add(routine); |
| 156 |
} |
| 157 |
} |
| 158 |
else { |
| 159 |
if (isProcedure(rs)) { |
| 160 |
((SQL2005ProcedureFactory) mProcedureFactory).setDatabase(database); |
| 161 |
mProcedureFactory.initialize(routine, rs); |
| 162 |
} |
| 163 |
else { |
| 164 |
((SQL2005UserDefinedFunctionFactory) mUserDefinedFunctionFactory).setDatabase(database); |
| 165 |
mUserDefinedFunctionFactory.initialize(routine, rs); |
| 166 |
} |
| 167 |
containmentList.add(routine); |
| 168 |
if (routine instanceof ICatalogObject) { |
| 169 |
((ICatalogObject) routine).refresh(); |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
finally { |
| 175 |
if (rs != null) { |
| 176 |
closeResultSet(rs); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
private static void loadSource(Routine routine, String aliasInfo, |
| 182 |
Database database) { |
| 183 |
if (aliasInfo == null || aliasInfo.trim().length() == 0) |
| 184 |
return; |
| 185 |
int index = aliasInfo.indexOf("begin"); //$NON-NLS-1$ |
| 186 |
|
| 187 |
// if it doesn't have any source, then don't bother |
| 188 |
if (index == -1) return; |
| 189 |
|
| 190 |
// otherwise process the source |
| 191 |
String body = aliasInfo.substring(index); |
| 192 |
final DatabaseDefinition definition = RDBCorePlugin.getDefault() |
| 193 |
.getDatabaseDefinitionRegistry().getDefinition(database); |
| 194 |
final DataModelElementFactory factory = definition |
| 195 |
.getDataModelElementFactory(); |
| 196 |
|
| 197 |
Source s = (Source) factory.create(SQLRoutinesPackage.eINSTANCE |
| 198 |
.getSource()); |
| 199 |
s.setBody(body); |
| 200 |
routine.setSource(s); |
| 201 |
} |
| 202 |
|
| 203 |
} |