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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
import java.sql.*;
import java.util.*;
/*
* This class is the central point for meta data extraction. It is a default implementation for all JDBC providers
* and can be sub-classed for database specific adaptations
*/
class GdaJMeta {
private static native void initIDs();
public Connection cnc;
public DatabaseMetaData md;
private HashMap<String, Boolean> current_schemas = new HashMap<String, Boolean> ();
public GdaJMeta (Connection cnc) throws Exception {
this.cnc = cnc;
this.md = cnc.getMetaData ();
}
public void schemaAddCurrent (String schema) {
current_schemas.put (GdaJValue.toLower (schema), true);
}
public boolean schemaIsCurrent (String schema) throws Exception {
return current_schemas.containsKey (GdaJValue.toLower (schema));
}
// The returned String must _never_ be NULL, but set to "" if it was NULL
public String getCatalog () throws Exception {
String s = cnc.getCatalog ();
if (s == null)
return "";
else
return s;
}
public GdaJResultSet getSchemas (String catalog, String schema) throws Exception {
return new GdaJMetaSchemas (this, catalog, schema);
}
public GdaJResultSet getTables (String catalog, String schema, String name) throws Exception {
return new GdaJMetaTables (this, catalog, schema, name);
}
public GdaJResultSet getViews (String catalog, String schema, String name) throws Exception {
return new GdaJMetaViews (this, catalog, schema, name);
}
public GdaJResultSet getColumns (String catalog, String schema, String tab_name) throws Exception {
return new GdaJMetaColumns (this, catalog, schema, tab_name);
}
// class initializer
static {
initIDs ();
}
}
/*
* Meta data retrieval is returned as instances of the class which allows a degree of
* customization depending on the type of database being accessed.
*/
abstract class GdaJMetaResultSet extends GdaJResultSet {
public Vector<GdaJColumnInfos> meta_col_infos;
protected ResultSet rs;
protected DatabaseMetaData md;
protected GdaJMeta jm;
protected GdaJMetaResultSet (int ncols, GdaJMeta jm) {
super (ncols);
this.jm = jm;
md = jm.md;
rs = null;
meta_col_infos = new Vector<GdaJColumnInfos> (0);
}
// get result set's meta data
public GdaJResultSetInfos getInfos () throws Exception {
return new GdaJMetaInfos (this);
}
abstract public boolean fillNextRow (long c_pointer) throws Exception;
}
/*
* Extends GdaJResultSetInfos for GdaJMetaResultSet
*/
class GdaJMetaInfos extends GdaJResultSetInfos {
private GdaJMetaResultSet meta;
// Constructor
public GdaJMetaInfos (GdaJMetaResultSet meta) {
super (meta.meta_col_infos);
this.meta = meta;
}
// describe a column, index starting at 0
public GdaJColumnInfos describeColumn (int col) throws Exception {
return meta.meta_col_infos.elementAt (col);
}
}
/*
* Meta data for schemas
*/
class GdaJMetaSchemas extends GdaJMetaResultSet {
String catalog = null;
String schema = null;
public GdaJMetaSchemas (GdaJMeta jm, String catalog, String schema) throws Exception {
super (4, jm);
meta_col_infos.add (new GdaJColumnInfos ("catalog_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("schema_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("schema_owner", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("schema_internal", null, java.sql.Types.BOOLEAN));
rs = jm.md.getSchemas ();
this.catalog = catalog;
this.schema = schema;
}
protected void columnTypesDeclared () {
// the catalog part cannot be NULL, but "" instead
GdaJValue cv = col_values.elementAt (0);
cv.no_null = true;
cv.convert_lc = true;
(col_values.elementAt (1)).convert_lc = true;
}
public boolean fillNextRow (long c_pointer) throws Exception {
if (! rs.next ())
return false;
GdaJValue cv;
if (catalog != null) {
String s = rs.getString (2);
if (s != catalog)
return fillNextRow (c_pointer);
}
if (schema != null) {
String s = rs.getString (1);
if (s != schema)
return fillNextRow (c_pointer);
}
cv = col_values.elementAt (0);
cv.setCValue (rs, 1, c_pointer);
cv = col_values.elementAt (1);
cv.setCValue (rs, 0, c_pointer);
cv = col_values.elementAt (3);
cv.setCBoolean (c_pointer, 3, false);
return true;
}
}
/*
* Meta data for tables
*/
class GdaJMetaTables extends GdaJMetaResultSet {
protected GdaJMetaTables (GdaJMeta jm) {
super (9, jm);
meta_col_infos.add (new GdaJColumnInfos ("table_catalog", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_schema", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_type", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("is_insertable_into", null, java.sql.Types.BOOLEAN));
meta_col_infos.add (new GdaJColumnInfos ("table_comments", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_short_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_full_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_owner", null, java.sql.Types.VARCHAR));
md = jm.md;
}
public GdaJMetaTables (GdaJMeta jm, String catalog, String schema, String name) throws Exception {
this (jm);
rs = md.getTables (catalog, schema, name, null);
}
protected void columnTypesDeclared () {
// the catalog part cannot be NULL, but "" instead
GdaJValue cv = col_values.elementAt (0);
cv.no_null = true;
cv.convert_lc = true;
(col_values.elementAt (1)).convert_lc = true;
(col_values.elementAt (2)).convert_lc = true;
(col_values.elementAt (6)).convert_lc = true;
(col_values.elementAt (7)).convert_lc = true;
}
public boolean fillNextRow (long c_pointer) throws Exception {
if (! rs.next ())
return false;
GdaJValue cv;
cv = col_values.elementAt (0);
cv.setCValue (rs, 0, c_pointer);
cv = col_values.elementAt (1);
cv.setCValue (rs, 1, c_pointer);
cv = col_values.elementAt (2);
cv.setCValue (rs, 2, c_pointer);
cv = col_values.elementAt (3);
cv.setCValue (rs, 3, c_pointer);
cv = col_values.elementAt (5);
cv.setCValue (rs, 4, c_pointer);
String ln = GdaJValue.toLower (rs.getString (2) + "." + rs.getString (3));
if (jm.schemaIsCurrent (rs.getString (2)))
cv.setCString (c_pointer, 6, GdaJValue.toLower (rs.getString (3)));
else
cv.setCString (c_pointer, 6, ln);
cv = col_values.elementAt (7);
cv.setCString (c_pointer, 7, ln);
return true;
}
}
/*
* Meta data for views
*/
class GdaJMetaViews extends GdaJMetaResultSet {
protected GdaJMetaViews (GdaJMeta jm) {
super (6, jm);
meta_col_infos.add (new GdaJColumnInfos ("table_catalog", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_schema", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("view_definition", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("check_option", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("is_updatable", null, java.sql.Types.BOOLEAN));
md = jm.md;
}
public GdaJMetaViews (GdaJMeta jm, String catalog, String schema, String name) throws Exception {
this (jm);
rs = jm.md.getTables (catalog, schema, name, new String[]{"VIEW"});
}
protected void columnTypesDeclared () {
// the catalog part cannot be NULL, but "" instead
GdaJValue cv = col_values.elementAt (0);
cv.no_null = true;
cv.convert_lc = true;
(col_values.elementAt (1)).convert_lc = true;
(col_values.elementAt (2)).convert_lc = true;
}
public boolean fillNextRow (long c_pointer) throws Exception {
if (! rs.next ())
return false;
GdaJValue cv;
cv = col_values.elementAt (0);
cv.setCValue (rs, 0, c_pointer);
cv = col_values.elementAt (1);
cv.setCValue (rs, 1, c_pointer);
cv = col_values.elementAt (2);
cv.setCValue (rs, 2, c_pointer);
return true;
}
}
/*
* Meta data for table's columns
*/
class GdaJMetaColumns extends GdaJMetaResultSet {
protected GdaJMetaColumns (GdaJMeta jm) {
super (24, jm);
meta_col_infos.add (new GdaJColumnInfos ("table_catalog", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_schema", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("table_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("column_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("ordinal_position", null, java.sql.Types.INTEGER));
meta_col_infos.add (new GdaJColumnInfos ("column_default", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("is_nullable", null, java.sql.Types.BOOLEAN));
meta_col_infos.add (new GdaJColumnInfos ("data_type", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("array_spec", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("gtype", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("character_maximum_length", null, java.sql.Types.INTEGER));
meta_col_infos.add (new GdaJColumnInfos ("character_octet_length", null, java.sql.Types.INTEGER));
meta_col_infos.add (new GdaJColumnInfos ("numeric_precision", null, java.sql.Types.INTEGER));
meta_col_infos.add (new GdaJColumnInfos ("numeric_scale", null, java.sql.Types.INTEGER));
meta_col_infos.add (new GdaJColumnInfos ("datetime_precision", null, java.sql.Types.INTEGER));
meta_col_infos.add (new GdaJColumnInfos ("character_set_catalog", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("character_set_schema", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("character_set_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("collation_catalog", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("collation_schema", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("collation_name", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("extra", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("is_updatable", null, java.sql.Types.VARCHAR));
meta_col_infos.add (new GdaJColumnInfos ("column_comments", null, java.sql.Types.VARCHAR));
md = jm.md;
}
public GdaJMetaColumns (GdaJMeta jm, String catalog, String schema, String tab_name) throws Exception {
this (jm);
rs = jm.md.getColumns (catalog, schema, tab_name, null);
}
protected void columnTypesDeclared () {
// the catalog part cannot be NULL, but "" instead
GdaJValue cv = col_values.elementAt (0);
cv.no_null = true;
cv.convert_lc = true;
(col_values.elementAt (1)).convert_lc = true;
(col_values.elementAt (2)).convert_lc = true;
(col_values.elementAt (3)).convert_lc = true;
(col_values.elementAt (7)).convert_lc = true;
(col_values.elementAt (15)).convert_lc = true;
(col_values.elementAt (16)).convert_lc = true;
(col_values.elementAt (17)).convert_lc = true;
(col_values.elementAt (18)).convert_lc = true;
(col_values.elementAt (19)).convert_lc = true;
(col_values.elementAt (20)).convert_lc = true;
}
public boolean fillNextRow (long c_pointer) throws Exception {
if (! rs.next ())
return false;
GdaJValue cv;
int i;
(col_values.elementAt (0)).setCValue (rs, 0, c_pointer);
(col_values.elementAt (1)).setCValue (rs, 1, c_pointer);
(col_values.elementAt (2)).setCValue (rs, 2, c_pointer);
(col_values.elementAt (3)).setCValue (rs, 3, c_pointer);
(col_values.elementAt (4)).setCValue (rs, 16, c_pointer);
(col_values.elementAt (5)).setCValue (rs, 12, c_pointer);
cv = col_values.elementAt (6);
i = rs.getInt (10);
if (i == DatabaseMetaData.columnNoNulls)
cv.setCBoolean (c_pointer, 6, false);
else
cv.setCBoolean (c_pointer, 6, true);
(col_values.elementAt (7)).setCValue (rs, 5, c_pointer);
(col_values.elementAt (9)).setCString (c_pointer, 9,
GdaJValue.jdbc_type_to_g_type (rs.getInt (5))); // gtype
(col_values.elementAt (11)).setCValue (rs, 15, c_pointer);
(col_values.elementAt (12)).setCValue (rs, 8, c_pointer); // numeric_precision
(col_values.elementAt (23)).setCValue (rs, 11, c_pointer); // comments
return true;
}
}
|