⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 platformimplbase.java

📁 OBPM是一个开源
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	/**
	 * {@inheritDoc}
	 */
	public void insert(Database model, DynaBean dynaBean)
			throws DatabaseOperationException {
		Connection connection = borrowConnection();

		try {
			insert(connection, model, dynaBean);
		} finally {
			returnConnection(connection);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void insert(Connection connection, Database model,
			Collection dynaBeans) throws DatabaseOperationException {
		SqlDynaClass dynaClass = null;
		SqlDynaProperty[] properties = null;
		PreparedStatement statement = null;
		int addedStmts = 0;
		boolean identityWarningPrinted = false;

		for (Iterator it = dynaBeans.iterator(); it.hasNext();) {
			DynaBean dynaBean = (DynaBean) it.next();
			SqlDynaClass curDynaClass = model.getDynaClassFor(dynaBean);
			if (curDynaClass != dynaClass) {
				if (dynaClass != null) {
					executeBatch(statement, addedStmts, dynaClass.getTable());
					addedStmts = 0;
				}

				dynaClass = curDynaClass;
				properties = getPropertiesForInsertion(model, curDynaClass,
						dynaBean);

				if (properties.length == 0) {
					_log.warn("Cannot insert application. of type " + dynaClass
							+ " because it has no usable properties");
					continue;
				}
				if (!identityWarningPrinted
						&& (getRelevantIdentityColumns(model, curDynaClass,
								dynaBean).length > 0)) {
					_log
							.warn("Updating the bean properties corresponding to auto-increment columns is not supported in batch mode");
					identityWarningPrinted = true;
				}

				String insertSql = createInsertSql(model, dynaClass,
						properties, null);

				if (_log.isDebugEnabled()) {
					_log.debug("Starting new batch with SQL: " + insertSql);
				}
				try {
					statement = connection.prepareStatement(insertSql);

				} catch (SQLException ex) {
					System.out.println(ex.getMessage());
					throw new DatabaseOperationException(
							"Error while preparing insert statement", ex);
				}
			}
			try {
				for (int idx = 0; idx < properties.length; idx++) {
					setObject(statement, idx + 1, dynaBean, properties[idx]);
				}
				// statement.execute();
				statement.addBatch();
				addedStmts++;
			} catch (SQLException ex) {
				System.out.println(ex.getMessage());
				throw new DatabaseOperationException(
						"Error while adding batch insert", ex);
			}
		}
		if (dynaClass != null) {
			executeBatch(statement, addedStmts, dynaClass.getTable());
		}
	}

	/**
	 * Performs the batch for the given statement, and checks that the specified
	 * amount of rows have been changed.
	 * 
	 * @param statement
	 *            The prepared statement
	 * @param numRows
	 *            The number of rows that should change
	 * @param table
	 *            The changed table
	 */
	private void executeBatch(PreparedStatement statement, int numRows,
			Table table) throws DatabaseOperationException {
		if (statement != null) {
			try {
				Connection connection = statement.getConnection();

				beforeInsert(connection, table);

				int[] results = statement.executeBatch();

				closeStatement(statement);
				afterInsert(connection, table);

				int sum = 0;

				for (int idx = 0; (results != null) && (idx < results.length); idx++) {
					sum += results[idx];
				}
				if (sum != numRows) {
					_log.warn("Attempted to insert " + numRows
							+ " rows into table " + table.getName()
							+ " but changed " + sum + " rows");
				}
			} catch (SQLException ex) {
				System.out.println(ex.getMessage());
				throw new DatabaseOperationException(
						"Error while inserting into the database", ex);
			}
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void insert(Database model, Collection dynaBeans)
			throws DatabaseOperationException {
		Connection connection = borrowConnection();

		try {
			insert(connection, model, dynaBeans);
		} finally {
			returnConnection(connection);
		}
	}

	/**
	 * Allows platforms to issue statements directly before rows are inserted
	 * into the specified table.
	 * 
	 * @param connection
	 *            The connection used for the insertion
	 * @param table
	 *            The table that the rows are inserted into
	 */
	protected void beforeInsert(Connection connection, Table table)
			throws SQLException {
	}

	/**
	 * Allows platforms to issue statements directly after rows have been
	 * inserted into the specified table.
	 * 
	 * @param connection
	 *            The connection used for the insertion
	 * @param table
	 *            The table that the rows have been inserted into
	 */
	protected void afterInsert(Connection connection, Table table)
			throws SQLException {
	}

	/**
	 * Creates the SQL for updating an object of the given type. If a concrete
	 * bean is given, then a concrete update statement is created, otherwise an
	 * update statement usable in a prepared statement is build.
	 * 
	 * @param model
	 *            The database model
	 * @param dynaClass
	 *            The type
	 * @param primaryKeys
	 *            The primary keys
	 * @param properties
	 *            The properties to write
	 * @param bean
	 *            Optionally the concrete bean to update
	 * @return The SQL required to update the instance
	 */
	protected String createUpdateSql(Database model, SqlDynaClass dynaClass,
			SqlDynaProperty[] primaryKeys, SqlDynaProperty[] properties,
			DynaBean bean) {
		Table table = model.findTable(dynaClass.getTableName());
		HashMap columnValues = toColumnValues(properties, bean);

		columnValues.putAll(toColumnValues(primaryKeys, bean));

		return _builder.getUpdateSql(table, columnValues, bean == null);
	}

	/**
	 * {@inheritDoc}
	 */
	public String getUpdateSql(Database model, DynaBean dynaBean) {
		SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
		SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();

		if (primaryKeys.length == 0) {
			_log.info("Cannot update application. of type " + dynaClass
					+ " because it has no primary keys");
			return null;
		}

		return createUpdateSql(model, dynaClass, primaryKeys, dynaClass
				.getNonPrimaryKeyProperties(), dynaBean);
	}

	/**
	 * {@inheritDoc}
	 */
	public void update(Connection connection, Database model, DynaBean dynaBean)
			throws DatabaseOperationException {
		SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
		SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();

		if (primaryKeys.length == 0) {
			_log.info("Cannot update application. of type " + dynaClass
					+ " because it has no primary keys");
			return;
		}

		SqlDynaProperty[] properties = dynaClass.getNonPrimaryKeyProperties();
		String sql = createUpdateSql(model, dynaClass, primaryKeys, properties,
				null);
		PreparedStatement statement = null;

		if (_log.isDebugEnabled()) {
			_log.debug("About to execute SQL: " + sql);
		}
		try {
			beforeUpdate(connection, dynaClass.getTable());

			statement = connection.prepareStatement(sql);

			int sqlIndex = 1;

			for (int idx = 0; idx < properties.length; idx++) {
				setObject(statement, sqlIndex++, dynaBean, properties[idx]);
			}
			for (int idx = 0; idx < primaryKeys.length; idx++) {
				setObject(statement, sqlIndex++, dynaBean, primaryKeys[idx]);
			}

			int count = statement.executeUpdate();

			afterUpdate(connection, dynaClass.getTable());

			if (count != 1) {
				_log.warn("Attempted to insert a single row " + dynaBean
						+ " into table " + dynaClass.getTableName()
						+ " but changed " + count + " row(s)");
			}
		} catch (SQLException ex) {
			throw new DatabaseOperationException(
					"Error while updating in the database", ex);
		} finally {
			closeStatement(statement);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void update(Database model, DynaBean dynaBean)
			throws DatabaseOperationException {
		Connection connection = borrowConnection();

		try {
			update(connection, model, dynaBean);
		} finally {
			returnConnection(connection);
		}
	}

	/**
	 * Allows platforms to issue statements directly before rows are updated in
	 * the specified table.
	 * 
	 * @param connection
	 *            The connection used for the update
	 * @param table
	 *            The table that the rows are updateed into
	 */
	protected void beforeUpdate(Connection connection, Table table)
			throws SQLException {
	}

	/**
	 * Allows platforms to issue statements directly after rows have been
	 * updated in the specified table.
	 * 
	 * @param connection
	 *            The connection used for the update
	 * @param table
	 *            The table that the rows have been updateed into
	 */
	protected void afterUpdate(Connection connection, Table table)
			throws SQLException {
	}

	/**
	 * Determines whether the given dyna bean is stored in the database.
	 * 
	 * @param dynaBean
	 *            The bean
	 * @param connection
	 *            The connection
	 * @return <code>true</code> if this dyna bean has a primary key
	 */
	protected boolean exists(Connection connection, DynaBean dynaBean) {
		// TODO: check for the pk value, and if present, query against database
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	public void store(Database model, DynaBean dynaBean)
			throws DatabaseOperationException {
		Connection connection = borrowConnection();

		try {
			if (exists(connection, dynaBean)) {
				update(connection, model, dynaBean);
			} else {
				insert(connection, model, dynaBean);
			}
		} finally {
			returnConnection(connection);
		}
	}

	/**
	 * Creates the SQL for deleting an object of the given type. If a concrete
	 * bean is given, then a concrete delete statement is created, otherwise a
	 * delete statement usable in a prepared statement is build.
	 * 
	 * @param model
	 *            The database model
	 * @param dynaClass
	 *            The type
	 * @param primaryKeys
	 *            The primary keys
	 * @param bean
	 *            Optionally the concrete bean to update
	 * @return The SQL required to delete the instance
	 */
	protected String createDeleteSql(Database model, SqlDynaClass dynaClass,
			SqlDynaProperty[] primaryKeys, DynaBean bean) {
		Table table = model.findTable(dynaClass.getTableName());
		HashMap pkValues = toColumnValues(primaryKeys, bean);

		return _builder.getDeleteSql(table, pkValues, bean == null);
	}

	/**
	 * {@inheritDoc}
	 */
	public String getDeleteSql(Database model, DynaBean dynaBean) {
		SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
		SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();

		if (primaryKeys.length == 0) {
			_log.warn("Cannot delete application. of type " + dynaClass
					+ " because it has no primary keys");
			return null;
		} else {
			return createDeleteSql(model, dynaClass, primaryKeys, dynaBean);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void delete(Database model, DynaBean dynaBean)
			throws DatabaseOperationException {
		Connection connection = borrowConnection();

		try {
			delete(connection, model, dynaBean);
		} finally {
			returnConnection(connection);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public void delete(Connection connection, Database model, DynaBean dynaBean)
			throws DatabaseOperationException {
		PreparedStatement statement = null;

		try {
			SqlDynaClass dynaClass = model.getDynaClassFor(dynaBean);
			SqlDynaProperty[] primaryKeys = dynaClass.getPrimaryKeyProperties();

			if (primaryKeys.length == 0) {
				_log.warn("Cannot delete application. of type " + dynaClass
						+ " because it has no primary keys");
				return;
			}

			String sql = createDeleteSql(model, dynaClass, primaryKeys, null);

			if (_log.isDebugEnabled()) {
				_log.debug("About to execute SQL " + sql);
			}

			statement = connection.prepareStatement(sql);

			for (int idx = 0; idx < primaryKeys.length; idx++) {
				setObject(statement, idx + 1, dynaBean, primaryKeys[idx]);
			}

			int count = statement.executeUpdate();

			if (count != 1) {
				_log.warn("Attempted to delete a single row " + dynaBean
						+ " in table " + dynaClass.getTableName()
						+ " but changed " + count + " row(s).");
			}
		} catch (SQLException ex) {
			throw new DatabaseOperationException(
					"Error while deleting from the database", ex);
		} finally {
			closeStatement(statement);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	public Database readModelFromDatabase(String name)
			throws DatabaseOperationException {
		Connection connection = borrowConnection();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -