public static void assertNext(ResultSet resultSet, String query) throws SQLException { if (!resultSet.next()) throw new ObjectNotFoundException("Database query did not return a result: " + query); }
public IdentityModel getIdentity(String tableName, boolean required) { IdentityModel result = identities.get(tableName); if (required && (result == null || result instanceof NoIdentity)) throw new ObjectNotFoundException("No identity defined for table '" + tableName + "'"); return result; }
public String columnReferencedBy(String fkColumnName, boolean required) { int index = ArrayUtil.indexOf(fkColumnName, fkColumnNames); if (index < 0) { if (required) throw new ObjectNotFoundException("foreign key '" + name + "' does not have a column '" + fkColumnName + "'"); else return null; } return refereeColumnNames[index]; }
public Object cellValueForHeader(String header, Object[] cells) { String trimmedHeader = StringUtil.trim(header); for (int i = 0; i < headers.length; i++) if (headers[i].equals(trimmedHeader)) return cells[i]; throw new ObjectNotFoundException("Undefined header: '" + trimmedHeader + "'"); }
public static File getFileIgnoreCase(File file, boolean required) { try { // if the file exists with the given capitalization use it as it is if (file.exists()) return file; // otherwise scan the folder for a file with the same name but different capitalization file = file.getCanonicalFile(); File parentFile = file.getParentFile(); if (parentFile == null) return file; File[] files = parentFile.listFiles(); if (files == null) return file; // parent directory is empty for (File sibling : files) if (sibling.getName().equalsIgnoreCase(file.getName())) return sibling; // if no file of equal name has been found... if (required) throw new ObjectNotFoundException("File not found: " + file); // ... then complain if one is required else return file; // otherwise use the predefined name } catch (IOException e) { throw new RuntimeException("Error checking file " + file, e); } }
public DBSequence getSequence(String sequenceName, Connection connection) throws SQLException { DBSequence[] sequences = querySequences(connection); for (DBSequence seq : sequences) if (seq.getName().equalsIgnoreCase(sequenceName)) return seq; throw new ObjectNotFoundException("No sequence found with name '" + sequenceName + "'"); }
public DBSchema getSchema(String schemaName) { for (DBCatalog catalog : getCatalogs()) { DBSchema schema = catalog.getSchema(schemaName); if (schema != null) return schema; } throw new ObjectNotFoundException("Table '" + name + "'"); }
private static DBForeignKeyConstraint parseFK(String spec, Database database) { spec = spec.trim(); int iBracket = spec.indexOf('('); String tableName = spec.substring(0, iBracket); String columnList = spec.substring(iBracket + 1, spec.length() - 1); String[] columns = StringUtil.splitAndTrim(columnList, ','); DBTable refererTable = database.getTable(tableName, true); DBForeignKeyConstraint fk = refererTable.getForeignKeyConstraint(columns); if (fk == null) throw new ObjectNotFoundException("Foreign ke constraint not found: " + tableName + '(' + ArrayFormat.format(columns) + ')'); return fk; }
@Override public Object evaluate(Context context) { try { return resolveNamePart(qnParts, qnParts.length, context); } catch (ObjectNotFoundException e) { throw new ObjectNotFoundException("Unable to resolve " + ArrayFormat.format(".", qnParts)); } }
public static List<Class<?>> getClasses(String packageName) { try { ClassLoader classLoader = getContextClassLoader(); String packagePath = packageName.replace('.', '/'); Enumeration<URL> resourceUris = classLoader.getResources(packagePath); List<Class<?>> classes = new ArrayList<Class<?>>(); while (resourceUris.hasMoreElements()) { URL resource = resourceUris.nextElement(); String protocol = resource.getProtocol(); if ("jar".equals(protocol)) findClassesInJar(resource.getFile(), packagePath, classes); else if ("file".equals(protocol)) findClassesInDirectory(new File(resource.toURI()), packageName, classes); else throw new UnsupportedOperationException("Not a supported protocol: " + protocol); } return classes; } catch (IOException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new ObjectNotFoundException(e); } catch (URISyntaxException e) { throw new RuntimeException(e); } }
public DBTable getTable(String name, boolean required) { for (DBSchema schema : getSchemas()) for (DBTable table : schema.getTables()) if (table.getName().equals(name)) return table; if (required) throw new ObjectNotFoundException("Table '" + name + "'"); else return null; }
key.pkTable = schema.getTable(key.pktable_name); } catch (ObjectNotFoundException e) { throw new ObjectNotFoundException("Table " + key.pktable_name + " is referenced by table " + key.fktable_name + " but not found in the database. Possibly it was filtered out?");
public DBRow queryByPK(Object pk, Connection connection, DatabaseDialect dialect) throws SQLException { String[] pkColumnNames = getPrimaryKeyConstraint().getColumnNames(); if (pkColumnNames.length == 0) throw new ObjectNotFoundException("Table " + name + " has no primary key"); Object[] pkComponents = (pk.getClass().isArray() ? (Object[]) pk : new Object[] { pk }); String whereClause = SQLUtil.renderWhereClause(pkColumnNames, pkComponents, dialect); DBRowIterator iterator = new DBRowIterator(this, connection, whereClause); if (!iterator.hasNext()) throw new ObjectNotFoundException("No " + name + " row with id (" + pkComponents + ")"); DBRow result = iterator.next(); iterator.close(); return result; } /*
public DBColumn getColumn(String columnName) { haveColumnsImported(); DBColumn column = columns.get(columnName); if (column == null) throw new ObjectNotFoundException("Column '" + columnName + "' not found in table '" + this.getName() + "'"); return column; }
public DBTable getTable(String name, boolean required) { for (DBCatalog catalog : getCatalogs()) for (DBTable table : catalog.getTables()) if (StringUtil.equalsIgnoreCase(table.getName(), name)) return table; if (required) throw new ObjectNotFoundException("Table '" + name + "'"); else return null; }
public DBForeignKeyConstraint getForeignKeyConstraint(String... columnNames) { haveFKsImported(); for (DBForeignKeyConstraint fk : foreignKeyConstraints) if (StringUtil.equalsIgnoreCase(fk.getColumnNames(), columnNames)) return fk; throw new ObjectNotFoundException("Table '" + name + "' has no foreign key " + "with the columns (" + ArrayFormat.format(columnNames) + ")"); }
DBCatalog catalogOfSchema = database.getCatalog(catalogNameOfSchema); if (catalogOfSchema == null) throw new ObjectNotFoundException("Catalog not found: " + catalogOfSchema); new DBSchema(schemaName, catalogOfSchema); schemaCount++;
public static Object resolveNamePart(String[] qnParts, int qnLength, Context context) { String objectOrClassName = ArrayFormat.formatPart(".", 0, qnLength, qnParts); if (context.contains(objectOrClassName)) { return context.get(objectOrClassName); } else { try { return DefaultClassProvider.resolveByObjectOrDefaultInstance(objectOrClassName, context); } catch (ConfigurationError e) { // ignore errors signaling that a class was not found } LOGGER.debug("Class not found: {}", objectOrClassName); if (qnLength > 1) { return readField(qnParts, qnLength - 1, qnParts[qnLength - 1], context); } else throw new ObjectNotFoundException("'" + objectOrClassName + "' is not defined"); } }