/** * ** Returns true if the parent records in their respective parent tables exist. ** @return True * if the parent records exist. */ public boolean parentsExist() throws DBException { DBFactory<gDBR> dbFact = this.getFactory(); DBFieldValues myFldVals = this.getFieldValues(); java.util.List<String> parentList = dbFact.getParentTables(); for (String parentTable : parentList) { /* get parent table DBFactory */ Print.logInfo("[%s] Parent table: %s", this.getTableName(), parentTable); DBFactory parentFact = DBFactory.getFactoryByName(parentTable); if (parentFact == null) { Print.logError("Unexpected error finding parent table: " + parentTable); return false; } /* create parent record key with fields from this key */ DBRecordKey parentKey = parentFact.createKey(); // an empty key DBField parentKeyFlds[] = parentFact.getKeyFields(); for (DBField pkf : parentKeyFlds) { String pfn = pkf.getName(); /* get this DBField */ DBField myKeyFld = this.getField(pfn); if (myKeyFld == null) { Print.logError("Unexpected error finding field: [" + this.getTableName() + "] " + pfn); return false; } /* get parent key field value */ Object pkv = myFldVals.getFieldValue(pfn); if (pkv == null) { Print.logError("Unexpected error finding parent field: [" + parentTable + "] " + pfn); return false; } if (myKeyFld.isDefaultValue(pkv)) { Print.logInfo("This key contains a global value, skipping parent check: " + parentTable); parentKey = null; break; } parentKey.setFieldValue(pfn, pkv); } /* check parent existence */ if ((parentKey != null) && !parentKey.exists()) { Print.logError("Parent record does not exist: [" + parentTable + "] " + parentKey); return false; } } return true; }
/** * ** Returns true if the specified key attribute exists in the table ** @param altIndexName The * alternate index name, or null to use the primary index ** @param whereKeyType The partial key * match type ** @return True if the specified key attribute exists in the table, false otherwise */ protected boolean _exists(String altIndexName, int whereKeyType) throws SQLException, DBException { /* key fields */ boolean usePrimaryKey = StringTools.isBlank(altIndexName); DBField kfld[] = usePrimaryKey ? this.getKeyFields() : this.getAltKeyFields(altIndexName); if (ListTools.isEmpty(kfld)) { throw new DBException("No keys found!"); } /* check last key for "auto_increment" */ if (whereKeyType == DBWhere.KEY_FULL) { DBField lastField = kfld[kfld.length - 1]; if (lastField.isAutoIncrement() && !this.getFieldValues().hasFieldValue(lastField.getName())) { // full key requested and last key is auto_increment, which is missing return false; } } // DBSelect: SELECT <Keys> FROM <TableName> <KeyWhere> String firstKey = kfld[0].getName(); DBSelect<gDBR> dsel = new DBSelect<gDBR>(this.getFactory()); dsel.setSelectedFields(firstKey); dsel.setWhere(this._getWhereClause(altIndexName, whereKeyType)); /* get keyed record */ DBConnection dbc = null; Statement stmt = null; ResultSet rs = null; boolean exists = false; try { dbc = DBConnection.getDefaultConnection(); stmt = dbc.execute(dsel.toString()); // may throw DBException rs = stmt.getResultSet(); exists = rs.next(); } catch (SQLException sqe) { if (sqe.getErrorCode() == DBFactory.SQLERR_TABLE_NOTLOCKED) { // MySQL: This case has been seen on rare occasions. Not sure what causes it. Print.logError("SQL Lock Error: " + sqe); Print.logError("Hackery! Forcing lock on table: " + this.getTableName()); if (DBProvider.lockTableForRead(this.getTableName(), true)) { // may throw DBException stmt = dbc.execute(dsel.toString()); // may throw SQLException, DBException rs = stmt.getResultSet(); // SQLException exists = rs.next(); // SQLException DBProvider.unlockTables(); // DBException } } else { throw sqe; } } finally { if (rs != null) { try { rs.close(); } catch (Throwable t) { } } if (stmt != null) { try { stmt.close(); } catch (Throwable t) { } } DBConnection.release(dbc); } return exists; }