use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class RDBMSSchemaHandler method getRDBMSTableFKInfoForTable.
/**
* Convenience method to get the ForeignKey info for the specified table from the datastore.
* @param conn Connection to use
* @param catalogName Catalog
* @param schemaName Schema
* @param tableName Name of the table
* @return The foreign key info
*/
protected RDBMSTableFKInfo getRDBMSTableFKInfoForTable(Connection conn, String catalogName, String schemaName, String tableName) {
// We don't cache FK info, so retrieve it directly
RDBMSTableFKInfo info = new RDBMSTableFKInfo(catalogName, schemaName, tableName);
DatastoreAdapter dba = getDatastoreAdapter();
try {
ResultSet rs = conn.getMetaData().getImportedKeys(catalogName, schemaName, tableName);
try {
while (rs.next()) {
ForeignKeyInfo fki = dba.newFKInfo(rs);
if (!info.getChildren().contains(fki)) {
// Ignore any duplicate FKs
info.addChild(fki);
}
}
} finally {
rs.close();
}
} catch (SQLException sqle) {
throw new NucleusDataStoreException("Exception thrown while querying foreign keys for table=" + tableName, sqle);
}
return info;
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class RDBMSSchemaHandler method getRDBMSSchemaInfoForCatalogSchema.
/**
* Convenience method to retrieve schema information for all tables in the specified catalog/schema.
* @param conn Connection
* @param catalog Catalog
* @param schema Schema
* @return Schema information
*/
protected RDBMSSchemaInfo getRDBMSSchemaInfoForCatalogSchema(Connection conn, String catalog, String schema) {
if (storeMgr.getBooleanProperty(RDBMSPropertyNames.PROPERTY_RDBMS_OMIT_DATABASEMETADATA_GETCOLUMNS)) {
// User has requested to omit calls to DatabaseMetaData.getColumns due to JDBC ineptness
return null;
}
RDBMSSchemaInfo schemaInfo = new RDBMSSchemaInfo(catalog, schema);
ResultSet rs = null;
try {
String catalogName = getIdentifierForUseWithDatabaseMetaData(catalog);
String schemaName = getIdentifierForUseWithDatabaseMetaData(schema);
rs = getDatastoreAdapter().getColumns(conn, catalogName, schemaName, null, null);
while (rs.next()) {
// Construct a fully-qualified name for the table in this row of the ResultSet
String colCatalogName = rs.getString(1);
String colSchemaName = rs.getString(2);
String colTableName = rs.getString(3);
if (StringUtils.isWhitespace(colTableName)) {
// JDBC driver should return the table name as a minimum TODO Localise
throw new NucleusDataStoreException("Invalid 'null' table name identifier returned by database. " + "Check with your JDBC driver vendor (ref:DatabaseMetaData.getColumns).");
}
if (rs.wasNull() || (colCatalogName != null && colCatalogName.length() < 1)) {
colCatalogName = null;
}
if (rs.wasNull() || (colSchemaName != null && colSchemaName.length() < 1)) {
colSchemaName = null;
}
String tableKey = getTableKeyInRDBMSSchemaInfo(catalog, schema, colTableName);
RDBMSTableInfo table = (RDBMSTableInfo) schemaInfo.getChild(tableKey);
if (table == null) {
// No current info for table so add it
table = new RDBMSTableInfo(colCatalogName, colSchemaName, colTableName);
table.addProperty("table_key", tableKey);
schemaInfo.addChild(table);
}
RDBMSColumnInfo col = getDatastoreAdapter().newRDBMSColumnInfo(rs);
table.addChild(col);
}
} catch (SQLException sqle) {
// TODO Localise
NucleusLogger.DATASTORE_SCHEMA.warn("Exception thrown obtaining schema column information from datastore", sqle);
throw new NucleusDataStoreException("Exception thrown obtaining schema column information from datastore", sqle);
} finally {
try {
if (rs != null) {
Statement stmt = rs.getStatement();
rs.close();
if (stmt != null) {
stmt.close();
}
}
} catch (SQLException sqle) {
// TODO Localise
throw new NucleusDataStoreException("Exception thrown closing results of DatabaseMetaData.getColumns()", sqle);
}
}
return schemaInfo;
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class RDBMSSchemaHandler method refreshTableData.
/**
* Convenience method for refreshing the table-column information for the tables specified
* within the defined catalog/schema.
* @param connection Connection to the datastore
* @param catalog Catalog to refresh
* @param schema Schema to refresh
* @param tableNames Collection of table names (String) to refresh
*/
private void refreshTableData(Object connection, String catalog, String schema, Collection tableNames) {
if (storeMgr.getBooleanProperty(RDBMSPropertyNames.PROPERTY_RDBMS_OMIT_DATABASEMETADATA_GETCOLUMNS)) {
// User has requested to omit calls to DatabaseMetaData.getColumns due to JDBC ineptness
return;
}
if (tableNames == null || tableNames.size() == 0) {
return;
}
RDBMSSchemaInfo info = (RDBMSSchemaInfo) getSchemaData(connection, TYPE_TABLES, null);
if (info == null) {
info = new RDBMSSchemaInfo(rdbmsStoreMgr.getCatalogName(), rdbmsStoreMgr.getSchemaName());
schemaDataByName.put(TYPE_TABLES, info);
}
// Get timestamp to mark the tables that are refreshed
Long now = Long.valueOf(System.currentTimeMillis());
// Retrieve all column info for the required catalog/schema
ResultSet rs = null;
Set<String> tablesProcessed = new HashSet();
try {
Connection conn = (Connection) connection;
String catalogName = getIdentifierForUseWithDatabaseMetaData(catalog);
String schemaName = getIdentifierForUseWithDatabaseMetaData(schema);
if (tableNames.size() == 1) {
// Single table to retrieve so restrict the query
String tableName = getIdentifierForUseWithDatabaseMetaData((String) tableNames.iterator().next());
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("050028", tableName, catalogName, schemaName));
}
rs = getDatastoreAdapter().getColumns(conn, catalogName, schemaName, tableName, null);
} else {
// Multiple tables so just retrieve all for this catalog/schema
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("050028", StringUtils.collectionToString(tableNames), catalogName, schemaName));
}
rs = getDatastoreAdapter().getColumns(conn, catalogName, schemaName, null, null);
}
boolean insensitiveIdentifiers = identifiersCaseInsensitive();
while (rs.next()) {
// Construct a fully-qualified name for the table in this row of the ResultSet
String colCatalogName = rs.getString(1);
String colSchemaName = rs.getString(2);
String colTableName = rs.getString(3);
if (StringUtils.isWhitespace(colTableName)) {
// TODO Localise
throw new NucleusDataStoreException("Invalid 'null' table name identifier returned by database. " + "Check with your JDBC driver vendor (ref:DatabaseMetaData.getColumns).");
}
if (rs.wasNull() || (colCatalogName != null && colCatalogName.length() < 1)) {
colCatalogName = null;
}
if (rs.wasNull() || (colSchemaName != null && colSchemaName.length() < 1)) {
colSchemaName = null;
}
String colTableNameToCheck = colTableName;
if (insensitiveIdentifiers) {
// Cater for case-insensitive identifiers
colTableNameToCheck = colTableName.toLowerCase();
}
if (tableNames.contains(colTableNameToCheck)) {
// Required table, so refresh/add it
String tableKey = getTableKeyInRDBMSSchemaInfo(catalog, schema, colTableName);
RDBMSTableInfo table = (RDBMSTableInfo) info.getChild(tableKey);
if (tablesProcessed.add(tableKey)) {
// Table met for first time in this refresh
if (table == null) {
// No current info for table
table = new RDBMSTableInfo(colCatalogName, colSchemaName, colTableName);
table.addProperty("table_key", tableKey);
info.addChild(table);
} else {
// Current info, so clean out columns
table.clearChildren();
}
table.addProperty("time", now);
}
if (table != null) {
table.addChild(getDatastoreAdapter().newRDBMSColumnInfo(rs));
}
}
}
} catch (NullPointerException npe) {
// SQLite throws NPEs, nice.
NucleusLogger.DATASTORE_SCHEMA.warn("Exception thrown obtaining schema column information from datastore", npe);
throw new NucleusDataStoreException("Exception thrown obtaining schema column information from datastore", npe);
} catch (SQLException sqle) {
// TODO Localise
NucleusLogger.DATASTORE_SCHEMA.warn("Exception thrown obtaining schema column information from datastore", sqle);
throw new NucleusDataStoreException("Exception thrown obtaining schema column information from datastore", sqle);
} finally {
try {
if (rs != null) {
Statement stmt = rs.getStatement();
rs.close();
if (stmt != null) {
stmt.close();
}
}
} catch (SQLException sqle) {
// TODO Localise
throw new NucleusDataStoreException("Exception thrown closing results of DatabaseMetaData.getColumns()", sqle);
}
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("050029", catalog, schema, "" + tablesProcessed.size(), "" + (System.currentTimeMillis() - now.longValue())));
}
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class RDBMSSchemaHandler method getTableType.
/**
* Returns the type of a database table/view in the datastore.
* Uses DatabaseMetaData.getTables() to extract this information.
* @param conn Connection to the database.
* @param table The table/view
* @return The table type (consistent with the return from DatabaseMetaData.getTables())
* @throws SQLException if an error occurs obtaining the information
*/
public String getTableType(Connection conn, Table table) throws SQLException {
String tableType = null;
// Calculate the catalog/schema names since we need to search fully qualified
DatastoreAdapter dba = getDatastoreAdapter();
String[] c = splitTableIdentifierName(dba.getCatalogSeparator(), table.getIdentifier().getName());
String catalogName = table.getCatalogName();
String schemaName = table.getSchemaName();
String tableName = table.getIdentifier().getName();
if (c[0] != null) {
catalogName = c[0];
}
if (c[1] != null) {
schemaName = c[1];
}
if (c[2] != null) {
tableName = c[2];
}
catalogName = getIdentifierForUseWithDatabaseMetaData(catalogName);
schemaName = getIdentifierForUseWithDatabaseMetaData(schemaName);
tableName = getIdentifierForUseWithDatabaseMetaData(tableName);
try {
ResultSet rs = conn.getMetaData().getTables(catalogName, schemaName, tableName, null);
try {
boolean insensitive = identifiersCaseInsensitive();
while (rs.next()) {
if ((insensitive && tableName.equalsIgnoreCase(rs.getString(3))) || (!insensitive && tableName.equals(rs.getString(3)))) {
tableType = rs.getString(4).toUpperCase();
break;
}
}
} finally {
rs.close();
}
} catch (SQLException sqle) {
throw new NucleusDataStoreException("Exception thrown finding table type using DatabaseMetaData.getTables()", sqle);
}
return tableType;
}
use of org.datanucleus.exceptions.NucleusDataStoreException in project datanucleus-rdbms by datanucleus.
the class AbstractArrayStore method set.
/**
* Method to set the array for the specified owner to the passed value.
* @param op ObjectProvider for the owner
* @param array the array
* @return Whether the array was updated successfully
*/
public boolean set(ObjectProvider op, Object array) {
if (array == null || Array.getLength(array) == 0) {
return true;
}
// Validate all elements for writing
ExecutionContext ec = op.getExecutionContext();
int length = Array.getLength(array);
for (int i = 0; i < length; i++) {
Object obj = Array.get(array, i);
validateElementForWriting(ec, obj, null);
}
boolean modified = false;
List exceptions = new ArrayList();
boolean batched = allowsBatching() && length > 1;
try {
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
try {
processBatchedWrites(mconn);
// Loop through all elements to be added
E element = null;
for (int i = 0; i < length; i++) {
element = (E) Array.get(array, i);
try {
// Add the row to the join table
int[] rc = internalAdd(op, element, mconn, batched, i, (i == length - 1));
if (rc != null) {
for (int j = 0; j < rc.length; j++) {
if (rc[j] > 0) {
// At least one record was inserted
modified = true;
}
}
}
} catch (MappedDatastoreException mde) {
exceptions.add(mde);
NucleusLogger.DATASTORE.error("Exception thrown in set of element", mde);
}
}
} finally {
mconn.release();
}
} catch (MappedDatastoreException e) {
exceptions.add(e);
NucleusLogger.DATASTORE.error("Exception thrown in set of element", e);
}
if (!exceptions.isEmpty()) {
// Throw all exceptions received as the cause of a NucleusDataStoreException so the user can see which
// record(s) didn't persist
String msg = Localiser.msg("056009", ((Exception) exceptions.get(0)).getMessage());
NucleusLogger.DATASTORE.error(msg);
throw new NucleusDataStoreException(msg, (Throwable[]) exceptions.toArray(new Throwable[exceptions.size()]), op.getObject());
}
return modified;
}
Aggregations