You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2013/07/19 11:33:29 UTC
[46/61] [partial] Hard rename of all 'org/eobjects' folders to
'org/apache'.
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptableColumnCreationBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptableColumnCreationBuilder.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptableColumnCreationBuilder.java
new file mode 100644
index 0000000..1ce3092
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptableColumnCreationBuilder.java
@@ -0,0 +1,102 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.create.ColumnCreationBuilder;
+import org.eobjects.metamodel.create.TableCreationBuilder;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.ColumnType;
+import org.eobjects.metamodel.schema.Table;
+
+final class InterceptableColumnCreationBuilder implements ColumnCreationBuilder {
+
+ private final ColumnCreationBuilder _columnCreationBuilder;
+ private final InterceptableTableCreationBuilder _tableCreationBuilder;
+
+ public InterceptableColumnCreationBuilder(
+ ColumnCreationBuilder columnCreationBuilder,
+ InterceptableTableCreationBuilder tableCreationBuilder) {
+ _columnCreationBuilder = columnCreationBuilder;
+ _tableCreationBuilder = tableCreationBuilder;
+ }
+
+ @Override
+ public String toSql() {
+ return _tableCreationBuilder.toSql();
+ }
+
+ @Override
+ public TableCreationBuilder like(Table table) {
+ return _tableCreationBuilder.like(table);
+ }
+
+ @Override
+ public ColumnCreationBuilder withColumn(String name) {
+ _columnCreationBuilder.withColumn(name);
+ return this;
+ }
+
+ @Override
+ public Table toTable() {
+ return _tableCreationBuilder.toTable();
+ }
+
+ @Override
+ public Table execute() throws MetaModelException {
+ return _tableCreationBuilder.execute();
+ }
+
+ @Override
+ public ColumnCreationBuilder like(Column column) {
+ _columnCreationBuilder.like(column);
+ return this;
+ }
+
+ @Override
+ public ColumnCreationBuilder asPrimaryKey() {
+ _columnCreationBuilder.asPrimaryKey();
+ return this;
+ }
+
+ @Override
+ public ColumnCreationBuilder ofType(ColumnType type) {
+ _columnCreationBuilder.ofType(type);
+ return this;
+ }
+
+ @Override
+ public ColumnCreationBuilder ofNativeType(String nativeType) {
+ _columnCreationBuilder.ofNativeType(nativeType);
+ return this;
+ }
+
+ @Override
+ public ColumnCreationBuilder ofSize(int size) {
+ _columnCreationBuilder.ofSize(size);
+ return this;
+ }
+
+ @Override
+ public ColumnCreationBuilder nullable(boolean nullable) {
+ _columnCreationBuilder.nullable(nullable);
+ return this;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptableDataContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptableDataContext.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptableDataContext.java
new file mode 100644
index 0000000..63a6dc2
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptableDataContext.java
@@ -0,0 +1,284 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.UpdateScript;
+import org.eobjects.metamodel.UpdateableDataContext;
+import org.eobjects.metamodel.create.TableCreationBuilder;
+import org.eobjects.metamodel.data.DataSet;
+import org.eobjects.metamodel.delete.RowDeletionBuilder;
+import org.eobjects.metamodel.drop.TableDropBuilder;
+import org.eobjects.metamodel.insert.RowInsertionBuilder;
+import org.eobjects.metamodel.query.CompiledQuery;
+import org.eobjects.metamodel.query.Query;
+import org.eobjects.metamodel.query.builder.InitFromBuilder;
+import org.eobjects.metamodel.query.builder.InitFromBuilderImpl;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+import org.eobjects.metamodel.update.RowUpdationBuilder;
+import org.eobjects.metamodel.util.HasNameMapper;
+
+public class InterceptableDataContext implements UpdateableDataContext {
+
+ private final DataContext _delegate;
+ private final InterceptorList<DataSet> _dataSetInterceptors;
+ private final InterceptorList<Query> _queryInterceptors;
+ private final InterceptorList<Schema> _schemaInterceptors;
+ private final InterceptorList<RowInsertionBuilder> _rowInsertionInterceptors;
+ private final InterceptorList<RowUpdationBuilder> _rowUpdationInterceptors;
+ private final InterceptorList<RowDeletionBuilder> _rowDeletionInterceptors;
+ private final InterceptorList<TableCreationBuilder> _tableCreationInterceptors;
+ private final InterceptorList<TableDropBuilder> _tableDropInterceptors;
+
+ protected InterceptableDataContext(DataContext delegate) {
+ _delegate = delegate;
+ _dataSetInterceptors = new InterceptorList<DataSet>();
+ _queryInterceptors = new InterceptorList<Query>();
+ _schemaInterceptors = new InterceptorList<Schema>();
+ _rowInsertionInterceptors = new InterceptorList<RowInsertionBuilder>();
+ _rowUpdationInterceptors = new InterceptorList<RowUpdationBuilder>();
+ _rowDeletionInterceptors = new InterceptorList<RowDeletionBuilder>();
+ _tableCreationInterceptors = new InterceptorList<TableCreationBuilder>();
+ _tableDropInterceptors = new InterceptorList<TableDropBuilder>();
+ }
+
+ public InterceptableDataContext addTableCreationInterceptor(TableCreationInterceptor interceptor) {
+ _tableCreationInterceptors.add(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext removeTableCreationInterceptor(TableCreationInterceptor interceptor) {
+ _tableCreationInterceptors.remove(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext addTableDropInterceptor(TableDropInterceptor interceptor) {
+ _tableDropInterceptors.add(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext removeTableDropInterceptor(TableDropInterceptor interceptor) {
+ _tableDropInterceptors.remove(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext addRowInsertionInterceptor(RowInsertionInterceptor interceptor) {
+ _rowInsertionInterceptors.add(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext removeRowInsertionInterceptor(RowInsertionInterceptor interceptor) {
+ _rowInsertionInterceptors.remove(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext addRowUpdationInterceptor(RowUpdationInterceptor interceptor) {
+ _rowUpdationInterceptors.add(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext removeRowUpdationInterceptor(RowUpdationInterceptor interceptor) {
+ _rowUpdationInterceptors.remove(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext addRowDeletionInterceptor(RowDeletionInterceptor interceptor) {
+ _rowDeletionInterceptors.add(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext removeRowDeletionInterceptor(RowDeletionInterceptor interceptor) {
+ _rowDeletionInterceptors.remove(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext addQueryInterceptor(QueryInterceptor interceptor) {
+ _queryInterceptors.add(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext removeQueryInterceptor(QueryInterceptor interceptor) {
+ _queryInterceptors.remove(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext addSchemaInterceptor(SchemaInterceptor interceptor) {
+ _schemaInterceptors.add(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext removeSchemaInterceptor(SchemaInterceptor interceptor) {
+ _schemaInterceptors.remove(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext addDataSetInterceptor(DataSetInterceptor interceptor) {
+ _dataSetInterceptors.add(interceptor);
+ return this;
+ }
+
+ public InterceptableDataContext removeDataSetInterceptor(DataSetInterceptor interceptor) {
+ _dataSetInterceptors.remove(interceptor);
+ return this;
+ }
+
+ public InterceptorList<DataSet> getDataSetInterceptors() {
+ return _dataSetInterceptors;
+ }
+
+ public InterceptorList<Query> getQueryInterceptors() {
+ return _queryInterceptors;
+ }
+
+ public InterceptorList<RowInsertionBuilder> getRowInsertionInterceptors() {
+ return _rowInsertionInterceptors;
+ }
+
+ public InterceptorList<RowUpdationBuilder> getRowUpdationInterceptors() {
+ return _rowUpdationInterceptors;
+ }
+
+ public InterceptorList<RowDeletionBuilder> getRowDeletionInterceptors() {
+ return _rowDeletionInterceptors;
+ }
+
+ public InterceptorList<Schema> getSchemaInterceptors() {
+ return _schemaInterceptors;
+ }
+
+ public InterceptorList<TableCreationBuilder> getTableCreationInterceptors() {
+ return _tableCreationInterceptors;
+ }
+
+ public DataContext getDelegate() {
+ return _delegate;
+ }
+
+ @Override
+ public DataSet executeQuery(Query query) throws MetaModelException {
+ query = _queryInterceptors.interceptAll(query);
+ DataSet dataSet = _delegate.executeQuery(query);
+ dataSet = _dataSetInterceptors.interceptAll(dataSet);
+ return dataSet;
+ }
+
+ @Override
+ public UpdateableDataContext refreshSchemas() {
+ _delegate.refreshSchemas();
+ return this;
+ }
+
+ @Override
+ public Schema[] getSchemas() throws MetaModelException {
+ Schema[] schemas = _delegate.getSchemas();
+ if (!_schemaInterceptors.isEmpty()) {
+ for (int i = 0; i < schemas.length; i++) {
+ schemas[i] = _schemaInterceptors.interceptAll(schemas[i]);
+ }
+ }
+ return schemas;
+ }
+
+ @Override
+ public String[] getSchemaNames() throws MetaModelException {
+ if (_schemaInterceptors.isEmpty()) {
+ return _delegate.getSchemaNames();
+ }
+ Schema[] schemas = getSchemas();
+ String[] schemaNames = new String[schemas.length];
+ for (int i = 0; i < schemaNames.length; i++) {
+ schemaNames[i] = new HasNameMapper().eval(schemas[i]);
+ }
+ return schemaNames;
+ }
+
+ @Override
+ public Schema getDefaultSchema() throws MetaModelException {
+ Schema schema = _delegate.getDefaultSchema();
+ schema = _schemaInterceptors.interceptAll(schema);
+ return schema;
+ }
+
+ @Override
+ public Schema getSchemaByName(String name) throws MetaModelException {
+ Schema schema = _delegate.getSchemaByName(name);
+ schema = _schemaInterceptors.interceptAll(schema);
+ return schema;
+ }
+
+ @Override
+ public InitFromBuilder query() {
+ return new InitFromBuilderImpl(this);
+ }
+
+ @Override
+ public Column getColumnByQualifiedLabel(String columnName) {
+ return _delegate.getColumnByQualifiedLabel(columnName);
+ }
+
+ @Override
+ public Table getTableByQualifiedLabel(String tableName) {
+ return _delegate.getTableByQualifiedLabel(tableName);
+ }
+
+ @Override
+ public void executeUpdate(UpdateScript update) {
+ if (!(_delegate instanceof UpdateableDataContext)) {
+ throw new UnsupportedOperationException("Delegate is not an UpdateableDataContext");
+ }
+ final UpdateableDataContext delegate = (UpdateableDataContext) _delegate;
+
+ if (_tableCreationInterceptors.isEmpty() && _tableDropInterceptors.isEmpty()
+ && _rowInsertionInterceptors.isEmpty() && _rowUpdationInterceptors.isEmpty()
+ && _rowDeletionInterceptors.isEmpty()) {
+ delegate.executeUpdate(update);
+ return;
+ }
+
+ UpdateScript interceptableUpdateScript = new InterceptableUpdateScript(this, update,
+ _tableCreationInterceptors, _tableDropInterceptors, _rowInsertionInterceptors,
+ _rowUpdationInterceptors, _rowDeletionInterceptors);
+ delegate.executeUpdate(interceptableUpdateScript);
+ }
+
+ @Override
+ public Query parseQuery(String queryString) throws MetaModelException {
+ return _delegate.parseQuery(queryString);
+ }
+
+ @Override
+ public DataSet executeQuery(String queryString) throws MetaModelException {
+ final Query query = parseQuery(queryString);
+ final DataSet dataSet = executeQuery(query);
+ return dataSet;
+ }
+
+ @Override
+ public CompiledQuery compileQuery(Query query) {
+ return _delegate.compileQuery(query);
+ }
+
+ @Override
+ public DataSet executeQuery(CompiledQuery compiledQuery, Object... values) {
+ return _delegate.executeQuery(compiledQuery, values);
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowDeletionBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowDeletionBuilder.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowDeletionBuilder.java
new file mode 100644
index 0000000..8a1d516
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowDeletionBuilder.java
@@ -0,0 +1,87 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.delete.RowDeletionBuilder;
+import org.eobjects.metamodel.query.FilterItem;
+import org.eobjects.metamodel.query.SelectItem;
+import org.eobjects.metamodel.query.builder.AbstractFilterBuilder;
+import org.eobjects.metamodel.query.builder.FilterBuilder;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Table;
+
+final class InterceptableRowDeletionBuilder implements RowDeletionBuilder {
+
+ private final RowDeletionBuilder _rowDeletionBuilder;
+ private final InterceptorList<RowDeletionBuilder> _rowDeletionInterceptors;
+
+ public InterceptableRowDeletionBuilder(RowDeletionBuilder rowDeletionBuilder,
+ InterceptorList<RowDeletionBuilder> rowDeletionInterceptors) {
+ _rowDeletionBuilder = rowDeletionBuilder;
+ _rowDeletionInterceptors = rowDeletionInterceptors;
+ }
+
+ @Override
+ public FilterBuilder<RowDeletionBuilder> where(Column column) {
+ final SelectItem selectItem = new SelectItem(column);
+ return new AbstractFilterBuilder<RowDeletionBuilder>(selectItem) {
+ @Override
+ protected RowDeletionBuilder applyFilter(FilterItem filter) {
+ return where(filter);
+ }
+ };
+ }
+
+ @Override
+ public FilterBuilder<RowDeletionBuilder> where(String columnName) {
+ Column column = getTable().getColumnByName(columnName);
+ return where(column);
+ }
+
+ @Override
+ public RowDeletionBuilder where(FilterItem... filterItems) {
+ _rowDeletionBuilder.where(filterItems);
+ return this;
+ }
+
+ @Override
+ public RowDeletionBuilder where(Iterable<FilterItem> filterItems) {
+ _rowDeletionBuilder.where(filterItems);
+ return this;
+ }
+
+ @Override
+ public Table getTable() {
+ return _rowDeletionBuilder.getTable();
+ }
+
+ @Override
+ public String toSql() {
+ return _rowDeletionBuilder.toSql();
+ }
+
+ @Override
+ public void execute() throws MetaModelException {
+ RowDeletionBuilder rowDeletionBuilder = _rowDeletionBuilder;
+ rowDeletionBuilder = _rowDeletionInterceptors.interceptAll(rowDeletionBuilder);
+ rowDeletionBuilder.execute();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowInsertionBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowInsertionBuilder.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowInsertionBuilder.java
new file mode 100644
index 0000000..3a0228b
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowInsertionBuilder.java
@@ -0,0 +1,111 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.data.Row;
+import org.eobjects.metamodel.data.Style;
+import org.eobjects.metamodel.insert.RowInsertionBuilder;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Table;
+
+final class InterceptableRowInsertionBuilder implements RowInsertionBuilder {
+
+ private final RowInsertionBuilder _rowInsertionBuilder;
+ private final InterceptorList<RowInsertionBuilder> _rowInsertionInterceptors;
+
+ public InterceptableRowInsertionBuilder(
+ RowInsertionBuilder rowInsertionBuilder,
+ InterceptorList<RowInsertionBuilder> rowInsertionInterceptors) {
+ _rowInsertionBuilder = rowInsertionBuilder;
+ _rowInsertionInterceptors = rowInsertionInterceptors;
+ }
+
+ @Override
+ public String toSql() {
+ return _rowInsertionBuilder.toSql();
+ }
+
+ @Override
+ public RowInsertionBuilder value(int columnIndex, Object value) {
+ _rowInsertionBuilder.value(columnIndex, value);
+ return this;
+ }
+
+ @Override
+ public RowInsertionBuilder value(int columnIndex, Object value, Style style) {
+ _rowInsertionBuilder.value(columnIndex, value, style);
+ return this;
+ }
+
+ @Override
+ public RowInsertionBuilder value(Column column, Object value) {
+ _rowInsertionBuilder.value(column, value);
+ return this;
+ }
+
+ @Override
+ public RowInsertionBuilder value(Column column, Object value, Style style) {
+ _rowInsertionBuilder.value(column, value, style);
+ return this;
+ }
+
+ @Override
+ public RowInsertionBuilder value(String columnName, Object value) {
+ _rowInsertionBuilder.value(columnName, value);
+ return this;
+ }
+
+ @Override
+ public RowInsertionBuilder like(Row row) {
+ _rowInsertionBuilder.like(row);
+ return this;
+ }
+
+ @Override
+ public RowInsertionBuilder value(String columnName, Object value,
+ Style style) {
+ _rowInsertionBuilder.value(columnName, value, style);
+ return this;
+ }
+
+ @Override
+ public void execute() throws MetaModelException {
+ RowInsertionBuilder rowInsertionBuilder = _rowInsertionBuilder;
+ rowInsertionBuilder = _rowInsertionInterceptors
+ .interceptAll(rowInsertionBuilder);
+ rowInsertionBuilder.execute();
+ }
+
+ @Override
+ public Row toRow() {
+ return _rowInsertionBuilder.toRow();
+ }
+
+ @Override
+ public Table getTable() {
+ return _rowInsertionBuilder.getTable();
+ }
+
+ @Override
+ public boolean isSet(Column column) {
+ return _rowInsertionBuilder.isSet(column);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowUpdationBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowUpdationBuilder.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowUpdationBuilder.java
new file mode 100644
index 0000000..d5dbc16
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptableRowUpdationBuilder.java
@@ -0,0 +1,136 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.data.Row;
+import org.eobjects.metamodel.data.Style;
+import org.eobjects.metamodel.query.FilterItem;
+import org.eobjects.metamodel.query.SelectItem;
+import org.eobjects.metamodel.query.builder.AbstractFilterBuilder;
+import org.eobjects.metamodel.query.builder.FilterBuilder;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Table;
+import org.eobjects.metamodel.update.RowUpdationBuilder;
+
+final class InterceptableRowUpdationBuilder implements RowUpdationBuilder {
+
+ private final RowUpdationBuilder _rowUpdationBuilder;
+ private final InterceptorList<RowUpdationBuilder> _rowUpdationInterceptors;
+
+ public InterceptableRowUpdationBuilder(RowUpdationBuilder rowUpdationBuilder,
+ InterceptorList<RowUpdationBuilder> rowUpdationInterceptors) {
+ _rowUpdationBuilder = rowUpdationBuilder;
+ _rowUpdationInterceptors = rowUpdationInterceptors;
+ }
+
+ @Override
+ public RowUpdationBuilder value(int columnIndex, Object value) {
+ _rowUpdationBuilder.value(columnIndex, value);
+ return this;
+ }
+
+ @Override
+ public RowUpdationBuilder value(int columnIndex, Object value, Style style) {
+ _rowUpdationBuilder.value(columnIndex, value, style);
+ return this;
+ }
+
+ @Override
+ public RowUpdationBuilder value(Column column, Object value) {
+ _rowUpdationBuilder.value(column, value);
+ return this;
+ }
+
+ @Override
+ public RowUpdationBuilder value(Column column, Object value, Style style) {
+ _rowUpdationBuilder.value(column, value, style);
+ return this;
+ }
+
+ @Override
+ public RowUpdationBuilder value(String columnName, Object value) {
+ _rowUpdationBuilder.value(columnName, value);
+ return this;
+ }
+
+ @Override
+ public RowUpdationBuilder value(String columnName, Object value, Style style) {
+ _rowUpdationBuilder.value(columnName, value, style);
+ return this;
+ }
+
+ @Override
+ public Row toRow() {
+ return _rowUpdationBuilder.toRow();
+ }
+
+ @Override
+ public boolean isSet(Column column) {
+ return _rowUpdationBuilder.isSet(column);
+ }
+
+ @Override
+ public FilterBuilder<RowUpdationBuilder> where(Column column) {
+ final SelectItem selectItem = new SelectItem(column);
+ return new AbstractFilterBuilder<RowUpdationBuilder>(selectItem) {
+ @Override
+ protected RowUpdationBuilder applyFilter(FilterItem filter) {
+ where(filter);
+ return InterceptableRowUpdationBuilder.this;
+ }
+ };
+ }
+
+ @Override
+ public FilterBuilder<RowUpdationBuilder> where(String columnName) {
+ Column column = getTable().getColumnByName(columnName);
+ return where(column);
+ }
+
+ @Override
+ public RowUpdationBuilder where(FilterItem... filterItems) {
+ _rowUpdationBuilder.where(filterItems);
+ return this;
+ }
+
+ @Override
+ public RowUpdationBuilder where(Iterable<FilterItem> filterItems) {
+ _rowUpdationBuilder.where(filterItems);
+ return this;
+ }
+
+ @Override
+ public String toSql() {
+ return _rowUpdationBuilder.toSql();
+ }
+
+ @Override
+ public Table getTable() {
+ return _rowUpdationBuilder.getTable();
+ }
+
+ @Override
+ public void execute() throws MetaModelException {
+ RowUpdationBuilder rowUpdationBuilder = _rowUpdationBuilder;
+ rowUpdationBuilder = _rowUpdationInterceptors.interceptAll(rowUpdationBuilder);
+ rowUpdationBuilder.execute();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptableTableCreationBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptableTableCreationBuilder.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptableTableCreationBuilder.java
new file mode 100644
index 0000000..0976113
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptableTableCreationBuilder.java
@@ -0,0 +1,69 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.create.ColumnCreationBuilder;
+import org.eobjects.metamodel.create.TableCreationBuilder;
+import org.eobjects.metamodel.schema.Table;
+
+final class InterceptableTableCreationBuilder implements TableCreationBuilder {
+
+ private final TableCreationBuilder _tabelCreationBuilder;
+ private final InterceptorList<TableCreationBuilder> _tableCreationInterceptors;
+
+ public InterceptableTableCreationBuilder(
+ TableCreationBuilder tabelCreationBuilder,
+ InterceptorList<TableCreationBuilder> tableCreationInterceptors) {
+ _tabelCreationBuilder = tabelCreationBuilder;
+ _tableCreationInterceptors = tableCreationInterceptors;
+ }
+
+ @Override
+ public String toSql() {
+ return _tabelCreationBuilder.toSql();
+ }
+
+ @Override
+ public TableCreationBuilder like(Table table) {
+ _tabelCreationBuilder.like(table);
+ return this;
+ }
+
+ @Override
+ public ColumnCreationBuilder withColumn(String name) {
+ ColumnCreationBuilder columnCreationBuilder = _tabelCreationBuilder
+ .withColumn(name);
+ return new InterceptableColumnCreationBuilder(columnCreationBuilder,
+ this);
+ }
+
+ @Override
+ public Table toTable() {
+ return _tabelCreationBuilder.toTable();
+ }
+
+ @Override
+ public Table execute() throws MetaModelException {
+ TableCreationBuilder tableCreationBuilder = _tabelCreationBuilder;
+ tableCreationBuilder = _tableCreationInterceptors
+ .interceptAll(tableCreationBuilder);
+ return tableCreationBuilder.execute();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptableTableDropBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptableTableDropBuilder.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptableTableDropBuilder.java
new file mode 100644
index 0000000..cb275a2
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptableTableDropBuilder.java
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.drop.TableDropBuilder;
+import org.eobjects.metamodel.schema.Table;
+
+final class InterceptableTableDropBuilder implements TableDropBuilder {
+
+ private final TableDropBuilder _tableDropBuilder;
+ private final InterceptorList<TableDropBuilder> _tableDropInterceptors;
+
+ public InterceptableTableDropBuilder(TableDropBuilder tableDropBuilder,
+ InterceptorList<TableDropBuilder> tableDropInterceptors) {
+ _tableDropBuilder = tableDropBuilder;
+ _tableDropInterceptors = tableDropInterceptors;
+ }
+
+ @Override
+ public Table getTable() {
+ return _tableDropBuilder.getTable();
+ }
+
+ @Override
+ public String toSql() {
+ return _tableDropBuilder.toSql();
+ }
+
+ @Override
+ public void execute() throws MetaModelException {
+ TableDropBuilder tableDropBuilder = _tableDropBuilder;
+ tableDropBuilder = _tableDropInterceptors.interceptAll(_tableDropBuilder);
+ tableDropBuilder.execute();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptableUpdateCallback.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptableUpdateCallback.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptableUpdateCallback.java
new file mode 100644
index 0000000..1538374
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptableUpdateCallback.java
@@ -0,0 +1,128 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.AbstractUpdateCallback;
+import org.eobjects.metamodel.UpdateCallback;
+import org.eobjects.metamodel.create.TableCreationBuilder;
+import org.eobjects.metamodel.delete.RowDeletionBuilder;
+import org.eobjects.metamodel.drop.TableDropBuilder;
+import org.eobjects.metamodel.insert.RowInsertionBuilder;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+import org.eobjects.metamodel.update.RowUpdationBuilder;
+
+/**
+ * {@link UpdateCallback} wrapper that allows adding interceptors for certain operations.
+ */
+final class InterceptableUpdateCallback extends AbstractUpdateCallback implements UpdateCallback {
+
+ private final UpdateCallback _updateCallback;
+ private final InterceptorList<TableCreationBuilder> _tableCreationInterceptors;
+ private final InterceptorList<TableDropBuilder> _tableDropInterceptors;
+ private final InterceptorList<RowInsertionBuilder> _rowInsertionInterceptors;
+ private final InterceptorList<RowUpdationBuilder> _rowUpdationInterceptors;
+ private final InterceptorList<RowDeletionBuilder> _rowDeletionInterceptors;
+
+ public InterceptableUpdateCallback(InterceptableDataContext dataContext, UpdateCallback updateCallback,
+ InterceptorList<TableCreationBuilder> tableCreationInterceptors,
+ InterceptorList<TableDropBuilder> tableDropInterceptors,
+ InterceptorList<RowInsertionBuilder> rowInsertionInterceptors,
+ InterceptorList<RowUpdationBuilder> rowUpdationInterceptors,
+ InterceptorList<RowDeletionBuilder> rowDeletionInterceptors) {
+ super(dataContext);
+ _updateCallback = updateCallback;
+ _tableCreationInterceptors = tableCreationInterceptors;
+ _tableDropInterceptors = tableDropInterceptors;
+ _rowInsertionInterceptors = rowInsertionInterceptors;
+ _rowUpdationInterceptors = rowUpdationInterceptors;
+ _rowDeletionInterceptors = rowDeletionInterceptors;
+ }
+
+ @Override
+ public TableCreationBuilder createTable(Schema schema, String name) throws IllegalArgumentException,
+ IllegalStateException {
+ TableCreationBuilder tabelCreationBuilder = _updateCallback.createTable(schema, name);
+ if (_tableCreationInterceptors.isEmpty()) {
+ return tabelCreationBuilder;
+ }
+ return new InterceptableTableCreationBuilder(tabelCreationBuilder, _tableCreationInterceptors);
+ }
+
+ @Override
+ public RowInsertionBuilder insertInto(Table table) throws IllegalArgumentException, IllegalStateException {
+ RowInsertionBuilder rowInsertionBuilder = _updateCallback.insertInto(table);
+ if (_rowInsertionInterceptors.isEmpty()) {
+ return rowInsertionBuilder;
+ }
+ return new InterceptableRowInsertionBuilder(rowInsertionBuilder, _rowInsertionInterceptors);
+ }
+
+ @Override
+ public boolean isCreateTableSupported() {
+ return _updateCallback.isCreateTableSupported();
+ }
+
+ @Override
+ public boolean isDropTableSupported() {
+ return _updateCallback.isDropTableSupported();
+ }
+
+ @Override
+ public TableDropBuilder dropTable(Table table) {
+ TableDropBuilder tableDropBuilder = _updateCallback.dropTable(table);
+ if (_tableDropInterceptors.isEmpty()) {
+ return tableDropBuilder;
+ }
+ return new InterceptableTableDropBuilder(tableDropBuilder, _tableDropInterceptors);
+ }
+
+ @Override
+ public boolean isInsertSupported() {
+ return _updateCallback.isInsertSupported();
+ }
+
+ @Override
+ public boolean isUpdateSupported() {
+ return _updateCallback.isUpdateSupported();
+ }
+
+ @Override
+ public RowUpdationBuilder update(Table table) {
+ RowUpdationBuilder rowUpdationBuilder = _updateCallback.update(table);
+ if (_rowUpdationInterceptors.isEmpty()) {
+ return rowUpdationBuilder;
+ }
+ return new InterceptableRowUpdationBuilder(rowUpdationBuilder, _rowUpdationInterceptors);
+ }
+
+ @Override
+ public boolean isDeleteSupported() {
+ return _updateCallback.isDeleteSupported();
+ }
+
+ @Override
+ public RowDeletionBuilder deleteFrom(Table table) {
+ RowDeletionBuilder rowDeletionBuilder = _updateCallback.deleteFrom(table);
+ if (_rowDeletionInterceptors.isEmpty()) {
+ return rowDeletionBuilder;
+ }
+ return new InterceptableRowDeletionBuilder(rowDeletionBuilder, _rowDeletionInterceptors);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptableUpdateScript.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptableUpdateScript.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptableUpdateScript.java
new file mode 100644
index 0000000..c5796c3
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptableUpdateScript.java
@@ -0,0 +1,62 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.UpdateCallback;
+import org.eobjects.metamodel.UpdateScript;
+import org.eobjects.metamodel.create.TableCreationBuilder;
+import org.eobjects.metamodel.delete.RowDeletionBuilder;
+import org.eobjects.metamodel.drop.TableDropBuilder;
+import org.eobjects.metamodel.insert.RowInsertionBuilder;
+import org.eobjects.metamodel.update.RowUpdationBuilder;
+
+final class InterceptableUpdateScript implements UpdateScript {
+
+ private final InterceptableDataContext _interceptableDataContext;
+ private final UpdateScript _updateScript;
+ private final InterceptorList<TableCreationBuilder> _tableCreationInterceptors;
+ private final InterceptorList<TableDropBuilder> _tableDropInterceptors;
+ private final InterceptorList<RowInsertionBuilder> _rowInsertionInterceptors;
+ private final InterceptorList<RowUpdationBuilder> _rowUpdationInterceptors;
+ private final InterceptorList<RowDeletionBuilder> _rowDeletionInterceptors;
+
+ public InterceptableUpdateScript(InterceptableDataContext interceptableDataContext, UpdateScript updateScript,
+ InterceptorList<TableCreationBuilder> tableCreationInterceptors,
+ InterceptorList<TableDropBuilder> tableDropInterceptors,
+ InterceptorList<RowInsertionBuilder> rowInsertionInterceptors,
+ InterceptorList<RowUpdationBuilder> rowUpdationInterceptors,
+ InterceptorList<RowDeletionBuilder> rowDeletionInterceptors) {
+ _interceptableDataContext = interceptableDataContext;
+ _updateScript = updateScript;
+ _tableCreationInterceptors = tableCreationInterceptors;
+ _tableDropInterceptors = tableDropInterceptors;
+ _rowInsertionInterceptors = rowInsertionInterceptors;
+ _rowUpdationInterceptors = rowUpdationInterceptors;
+ _rowDeletionInterceptors = rowDeletionInterceptors;
+ }
+
+ @Override
+ public void run(UpdateCallback callback) {
+ UpdateCallback interceptableUpdateCallback = new InterceptableUpdateCallback(_interceptableDataContext,
+ callback, _tableCreationInterceptors, _tableDropInterceptors, _rowInsertionInterceptors,
+ _rowUpdationInterceptors, _rowDeletionInterceptors);
+ _updateScript.run(interceptableUpdateCallback);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/Interceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/Interceptor.java b/core/src/main/java/org/apache/metamodel/intercept/Interceptor.java
new file mode 100644
index 0000000..2eb2c4a
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/Interceptor.java
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+/**
+ * Defines a high-level interface for interceptors in MetaModel.
+ *
+ * An intereptor can touch, modify, enhance or do other operations on certain
+ * object types as they are passed around for execution in MetaModel. There are
+ * 5 types of concrete interceptors:
+ *
+ * @see QueryInterceptor
+ * @see DataSetInterceptor
+ * @see RowInsertionInterceptor
+ * @see TableCreationInterceptor
+ * @see SchemaInterceptor
+ *
+ * @author Kasper Sørensen
+ *
+ * @param <E>
+ * the type of object to intercept
+ */
+public interface Interceptor<E> {
+
+ /**
+ * Interception method invoked by MetaModel when the intercepted object is
+ * being activated.
+ *
+ * @param input
+ * the intercepted object
+ * @return the intercepted object, or a modification of this if the object
+ * is to be replaced by the interceptor. The returned object must
+ * not be null.
+ */
+ public E intercept(E input);
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/InterceptorList.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/InterceptorList.java b/core/src/main/java/org/apache/metamodel/intercept/InterceptorList.java
new file mode 100644
index 0000000..32c1e0e
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/InterceptorList.java
@@ -0,0 +1,74 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Represents a list of interceptors
+ *
+ * @author Kasper Sørensen
+ *
+ * @param <E>
+ * the thing to intercept
+ *
+ * @see Interceptor
+ */
+public final class InterceptorList<E> {
+
+ private final List<Interceptor<E>> _interceptors = new ArrayList<Interceptor<E>>();
+
+ public void add(Interceptor<E> interceptor) {
+ _interceptors.add(interceptor);
+ }
+
+ public void remove(Interceptor<E> interceptor) {
+ _interceptors.remove(interceptor);
+ }
+
+ /**
+ * Gets the first (if any) interceptor of a specific type.
+ *
+ * @param interceptorClazz
+ * @return
+ */
+ public <I extends Interceptor<E>> I getInterceptorOfType(
+ Class<I> interceptorClazz) {
+ for (Interceptor<?> interceptor : _interceptors) {
+ if (interceptorClazz.isAssignableFrom(interceptor.getClass())) {
+ @SuppressWarnings("unchecked")
+ I result = (I) interceptor;
+ return result;
+ }
+ }
+ return null;
+ }
+
+ public boolean isEmpty() {
+ return _interceptors.isEmpty();
+ }
+
+ protected E interceptAll(E input) {
+ for (Interceptor<E> interceptor : _interceptors) {
+ input = interceptor.intercept(input);
+ }
+ return input;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/Interceptors.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/Interceptors.java b/core/src/main/java/org/apache/metamodel/intercept/Interceptors.java
new file mode 100644
index 0000000..2111fa2
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/Interceptors.java
@@ -0,0 +1,35 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.DataContext;
+
+public final class Interceptors {
+
+ private Interceptors() {
+ // prevent instantiation
+ }
+
+ public static InterceptableDataContext intercept(DataContext dc) {
+ if (dc instanceof InterceptableDataContext) {
+ return (InterceptableDataContext) dc;
+ }
+ return new InterceptableDataContext(dc);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/QueryInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/QueryInterceptor.java b/core/src/main/java/org/apache/metamodel/intercept/QueryInterceptor.java
new file mode 100644
index 0000000..b1299cf
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/QueryInterceptor.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.query.Query;
+
+/**
+ * An {@link Interceptor} for Queries, allowing to touch or modify a query
+ * before execution.
+ *
+ * @author Kasper Sørensen
+ */
+public interface QueryInterceptor extends Interceptor<Query> {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/RowDeletionInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/RowDeletionInterceptor.java b/core/src/main/java/org/apache/metamodel/intercept/RowDeletionInterceptor.java
new file mode 100644
index 0000000..4891185
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/RowDeletionInterceptor.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.delete.RowDeletionBuilder;
+
+/**
+ * An {@link Interceptor} for {@link RowDeletionBuilder}, allowing for
+ * interception of "delete from table" operations before they are executed.
+ *
+ * @author Kasper Sørensen
+ */
+public interface RowDeletionInterceptor extends Interceptor<RowDeletionBuilder> {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/RowInsertionInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/RowInsertionInterceptor.java b/core/src/main/java/org/apache/metamodel/intercept/RowInsertionInterceptor.java
new file mode 100644
index 0000000..c652e47
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/RowInsertionInterceptor.java
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.insert.RowInsertionBuilder;
+
+/**
+ * An {@link Interceptor} for {@link RowInsertionBuilder}, allowing for
+ * interception of "insert into table" operations before they are executed.
+ *
+ * @author Kasper Sørensen
+ */
+public interface RowInsertionInterceptor extends
+ Interceptor<RowInsertionBuilder> {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/RowUpdationInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/RowUpdationInterceptor.java b/core/src/main/java/org/apache/metamodel/intercept/RowUpdationInterceptor.java
new file mode 100644
index 0000000..5a9cded
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/RowUpdationInterceptor.java
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.update.RowUpdationBuilder;
+
+/**
+ * An {@link Interceptor} for {@link RowUpdationBuilder}, allowing for
+ * interception of "update table" operations before they are executed.
+ *
+ * @author Kasper Sørensen
+ */
+public interface RowUpdationInterceptor extends
+ Interceptor<RowUpdationBuilder> {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/SchemaInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/SchemaInterceptor.java b/core/src/main/java/org/apache/metamodel/intercept/SchemaInterceptor.java
new file mode 100644
index 0000000..3ff3489
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/SchemaInterceptor.java
@@ -0,0 +1,31 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.schema.Schema;
+
+/**
+ * An {@link Interceptor} for {@link Schema}s, which allows for intercepting
+ * schema objects before they are returned to the user.
+ *
+ * @author Kasper Sørensen
+ */
+public interface SchemaInterceptor extends Interceptor<Schema> {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/TableCreationInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/TableCreationInterceptor.java b/core/src/main/java/org/apache/metamodel/intercept/TableCreationInterceptor.java
new file mode 100644
index 0000000..af55dde
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/TableCreationInterceptor.java
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.create.TableCreationBuilder;
+
+/**
+ * An {@link Interceptor} for {@link TableCreationBuilder}s, which allows for
+ * intercepting "create table" operations before they are executed.
+ *
+ * @author Kasper Sørensen
+ */
+public interface TableCreationInterceptor extends
+ Interceptor<TableCreationBuilder> {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/TableDropInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/TableDropInterceptor.java b/core/src/main/java/org/apache/metamodel/intercept/TableDropInterceptor.java
new file mode 100644
index 0000000..d148ec1
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/TableDropInterceptor.java
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.intercept;
+
+import org.eobjects.metamodel.drop.TableDropBuilder;
+
+/**
+ * An {@link Interceptor} for {@link TableDropBuilder}s, which allows for
+ * intercepting "drop table" operations before they are executed.
+ *
+ * @author Kasper Sørensen
+ */
+public interface TableDropInterceptor extends
+ Interceptor<TableDropBuilder> {
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/package-info.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/package-info.java b/core/src/main/java/org/apache/metamodel/package-info.java
new file mode 100644
index 0000000..74b215c
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/package-info.java
@@ -0,0 +1,23 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+/**
+ * Root package for MetaModel
+ */
+package org.eobjects.metamodel;
+
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/query/AbstractQueryClause.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/query/AbstractQueryClause.java b/core/src/main/java/org/apache/metamodel/query/AbstractQueryClause.java
new file mode 100644
index 0000000..c06cf27
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/query/AbstractQueryClause.java
@@ -0,0 +1,163 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.query;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eobjects.metamodel.util.BaseObject;
+
+/**
+ * Represents an abstract clause in a query. Clauses contains IQueryItems and
+ * provide basic ways of adding, modifying and removing these.
+ *
+ * @param <E>
+ * the type of query item this QueryClause handles
+ *
+ * @see Query
+ */
+public abstract class AbstractQueryClause<E extends QueryItem> extends BaseObject implements QueryClause<E> {
+
+ private static final long serialVersionUID = 3987346267433022231L;
+
+ public static final String PREFIX_SELECT = "SELECT ";
+ public static final String PREFIX_FROM = " FROM ";
+ public static final String PREFIX_WHERE = " WHERE ";
+ public static final String PREFIX_GROUP_BY = " GROUP BY ";
+ public static final String PREFIX_HAVING = " HAVING ";
+ public static final String PREFIX_ORDER_BY = " ORDER BY ";
+ public static final String DELIM_COMMA = ", ";
+ public static final String DELIM_AND = " AND ";
+
+ private final Query _query;
+ private final List<E> _items = new ArrayList<E>();
+ private final String _prefix;
+ private final String _delim;
+
+ public AbstractQueryClause(Query query, String prefix, String delim) {
+ _query = query;
+ _prefix = prefix;
+ _delim = delim;
+ }
+
+ @Override
+ public QueryClause<E> setItems(E... items) {
+ _items.clear();
+ return addItems(items);
+ }
+
+ @Override
+ public QueryClause<E> addItems(E... items) {
+ for (E item : items) {
+ addItem(item);
+ }
+ return this;
+ }
+
+ @Override
+ public QueryClause<E> addItems(Iterable<E> items) {
+ for (E item : items) {
+ addItem(item);
+ }
+ return this;
+ }
+
+ public QueryClause<E> addItem(int index, E item) {
+ if (item.getQuery() == null) {
+ item.setQuery(_query);
+ }
+ _items.add(index, item);
+ return this;
+ };
+
+ @Override
+ public QueryClause<E> addItem(E item) {
+ return addItem(getItemCount(), item);
+ }
+
+ @Override
+ public int getItemCount() {
+ return _items.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return getItemCount() == 0;
+ }
+
+ @Override
+ public E getItem(int index) {
+ return _items.get(index);
+ }
+
+ @Override
+ public List<E> getItems() {
+ return _items;
+ }
+
+ @Override
+ public QueryClause<E> removeItem(int index) {
+ _items.remove(index);
+ return this;
+ }
+
+ @Override
+ public QueryClause<E> removeItem(E item) {
+ _items.remove(item);
+ return this;
+ }
+
+ @Override
+ public QueryClause<E> removeItems() {
+ _items.clear();
+ return this;
+ }
+
+ @Override
+ public String toSql() {
+ return toSql(false);
+ }
+
+ @Override
+ public String toSql(boolean includeSchemaInColumnPaths) {
+ if (_items.size() == 0) {
+ return "";
+ }
+ final StringBuilder sb = new StringBuilder(_prefix);
+ for (int i = 0; i < _items.size(); i++) {
+ final E item = _items.get(i);
+ if (i != 0) {
+ sb.append(_delim);
+ }
+ final String sql = item.toSql(includeSchemaInColumnPaths);
+ sb.append(sql);
+ }
+ return sb.toString();
+ }
+
+ @Override
+ public String toString() {
+ return toSql();
+ }
+
+ @Override
+ protected void decorateIdentity(List<Object> identifiers) {
+ identifiers.add(_items);
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/query/AverageAggregateBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/query/AverageAggregateBuilder.java b/core/src/main/java/org/apache/metamodel/query/AverageAggregateBuilder.java
new file mode 100644
index 0000000..3b2d949
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/query/AverageAggregateBuilder.java
@@ -0,0 +1,49 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.query;
+
+import org.eobjects.metamodel.util.AggregateBuilder;
+import org.eobjects.metamodel.util.NumberComparator;
+
+final class AverageAggregateBuilder implements AggregateBuilder<Double> {
+
+ public double _average;
+ public int _numValues;
+
+ @Override
+ public void add(Object o) {
+ if (o == null) {
+ return;
+ }
+ Number number = NumberComparator.toNumber(o);
+ if (number == null) {
+ throw new IllegalArgumentException("Could not convert to number: "
+ + o);
+ }
+ double total = _average * _numValues + number.doubleValue();
+ _numValues++;
+ _average = total / _numValues;
+ }
+
+ @Override
+ public Double getAggregate() {
+ return _average;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/query/CompiledQuery.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/query/CompiledQuery.java b/core/src/main/java/org/apache/metamodel/query/CompiledQuery.java
new file mode 100644
index 0000000..cd242b9
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/query/CompiledQuery.java
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.query;
+
+import java.io.Closeable;
+import java.util.List;
+
+import org.eobjects.metamodel.DataContext;
+
+/**
+ * A {@link CompiledQuery} is a {@link Query} which has been compiled, typically
+ * by the data source itself, to provide optimized execution speed. Compiled
+ * queries are produced using the {@link DataContext#compileQuery(Query)} method.
+ *
+ * Typically the compilation itself takes a bit of time, but firing the compiled
+ * query is faster than regular queries. This means that for repeated executions
+ * of the same query, it is usually faster to use compiled queries.
+ *
+ * To make {@link CompiledQuery} useful for more than just one specific query,
+ * variations of the query can be fired, as long as the variations can be
+ * expressed as a {@link QueryParameter} for instance in the WHERE clause of the
+ * query.
+ *
+ * @see DataContext#compileQuery(Query)
+ * @see QueryParameter
+ */
+public interface CompiledQuery extends Closeable {
+
+ /**
+ * Gets the {@link QueryParameter}s associated with the compiled query.
+ * Values for these parameters are expected when the query is executed.
+ *
+ * @return a list of query parameters
+ */
+ public List<QueryParameter> getParameters();
+
+ /**
+ * A representation of the query as SQL.
+ *
+ * @return a SQL string.
+ */
+ public String toSql();
+
+ /**
+ * Closes any resources related to the compiled query.
+ */
+ @Override
+ public void close();
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/query/CountAggregateBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/query/CountAggregateBuilder.java b/core/src/main/java/org/apache/metamodel/query/CountAggregateBuilder.java
new file mode 100644
index 0000000..c5bca84
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/query/CountAggregateBuilder.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.query;
+
+import org.eobjects.metamodel.util.AggregateBuilder;
+
+final class CountAggregateBuilder implements AggregateBuilder<Long> {
+
+ private long counter = 0;
+
+ @Override
+ public void add(Object o) {
+ if (o != null) {
+ counter++;
+ }
+ }
+
+ @Override
+ public Long getAggregate() {
+ return counter;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/query/DefaultCompiledQuery.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/query/DefaultCompiledQuery.java b/core/src/main/java/org/apache/metamodel/query/DefaultCompiledQuery.java
new file mode 100644
index 0000000..c1f3dab
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/query/DefaultCompiledQuery.java
@@ -0,0 +1,176 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.query;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Represents a default implementation of the {@link CompiledQuery} interface.
+ * This implementation does not actually do anything to prepare the query, but
+ * allows creating a clone of the originating query with the parameters replaced
+ * by values.
+ */
+public class DefaultCompiledQuery implements CompiledQuery {
+
+ private final Query _query;
+ private final List<QueryParameter> _parameters;
+
+ public DefaultCompiledQuery(Query query) {
+ _query = query;
+ _parameters = createParameterList();
+ }
+
+ /**
+ * Clones the query while replacing query parameters with corresponding
+ * values.
+ *
+ * @param values
+ * @return
+ */
+ public Query cloneWithParameterValues(Object[] values) {
+ final AtomicInteger parameterIndex = new AtomicInteger(0);
+ final Query clonedQuery = _query.clone();
+ replaceParametersInQuery(values, parameterIndex, _query, clonedQuery);
+ return clonedQuery;
+ }
+
+ private void replaceParametersInQuery(Object[] values, AtomicInteger parameterIndex, Query originalQuery,
+ Query newQuery) {
+ replaceParametersInFromClause(values, parameterIndex, originalQuery, newQuery);
+ replaceParametersInWhereClause(values, parameterIndex, originalQuery, newQuery);
+ }
+
+ private void replaceParametersInWhereClause(Object[] values, final AtomicInteger parameterIndex,
+ Query originalQuery, Query newQuery) {
+ // creates a clone of the original query, but rebuilds a completely new
+ // where clause based on parameter values
+
+ final List<FilterItem> items = originalQuery.getWhereClause().getItems();
+ int i = 0;
+ for (FilterItem filterItem : items) {
+ final FilterItem newFilter = copyFilterItem(filterItem, values, parameterIndex);
+ if (filterItem != newFilter) {
+ newQuery.getWhereClause().removeItem(i);
+ newQuery.getWhereClause().addItem(i, newFilter);
+ }
+ i++;
+ }
+ }
+
+ private void replaceParametersInFromClause(Object[] values, AtomicInteger parameterIndex, Query originalQuery,
+ Query newQuery) {
+ final List<FromItem> fromItems = originalQuery.getFromClause().getItems();
+ int i = 0;
+ for (FromItem fromItem : fromItems) {
+ final Query subQuery = fromItem.getSubQuery();
+ if (subQuery != null) {
+ final Query newSubQuery = newQuery.getFromClause().getItem(i).getSubQuery();
+ replaceParametersInQuery(values, parameterIndex, subQuery, newSubQuery);
+
+ newQuery.getFromClause().removeItem(i);
+ newQuery.getFromClause().addItem(i, new FromItem(newSubQuery).setAlias(fromItem.getAlias()));
+ }
+ i++;
+ }
+ }
+
+ private FilterItem copyFilterItem(FilterItem item, Object[] values, AtomicInteger parameterIndex) {
+ if (item.isCompoundFilter()) {
+ final FilterItem[] childItems = item.getChildItems();
+ final FilterItem[] newChildItems = new FilterItem[childItems.length];
+ for (int i = 0; i < childItems.length; i++) {
+ final FilterItem childItem = childItems[i];
+ final FilterItem newChildItem = copyFilterItem(childItem, values, parameterIndex);
+ newChildItems[i] = newChildItem;
+ }
+ final FilterItem newFilter = new FilterItem(newChildItems);
+ return newFilter;
+ } else {
+ if (item.getOperand() instanceof QueryParameter) {
+ final Object newOperand = values[parameterIndex.getAndIncrement()];
+ final FilterItem newFilter = new FilterItem(item.getSelectItem(), item.getOperator(), newOperand);
+ return newFilter;
+ } else {
+ return item;
+ }
+ }
+ }
+
+ private List<QueryParameter> createParameterList() {
+ final List<QueryParameter> parameters = new ArrayList<QueryParameter>();
+
+ buildParameterListInFromClause(parameters, _query);
+ buildParameterListInWhereClause(parameters, _query);
+ return parameters;
+ }
+
+ private void buildParameterListInWhereClause(List<QueryParameter> parameters, Query query) {
+ List<FilterItem> items = query.getWhereClause().getItems();
+ for (FilterItem item : items) {
+ buildParameterFromFilterItem(parameters, item);
+ }
+ }
+
+ private void buildParameterListInFromClause(List<QueryParameter> parameters, Query query) {
+ List<FromItem> fromItems = query.getFromClause().getItems();
+ for (FromItem fromItem : fromItems) {
+ Query subQuery = fromItem.getSubQuery();
+ if (subQuery != null) {
+ buildParameterListInFromClause(parameters, subQuery);
+ buildParameterListInWhereClause(parameters, subQuery);
+ }
+ }
+ }
+
+ @Override
+ public List<QueryParameter> getParameters() {
+ return _parameters;
+ }
+
+ @Override
+ public String toSql() {
+ return _query.toSql();
+ }
+
+ @Override
+ public String toString() {
+ return getClass().getSimpleName() + "[" + toSql() + "]";
+ }
+
+ @Override
+ public void close() {
+ // do nothing
+ }
+
+ private void buildParameterFromFilterItem(List<QueryParameter> parameters, FilterItem item) {
+ if (item.isCompoundFilter()) {
+ FilterItem[] childItems = item.getChildItems();
+ for (FilterItem childItem : childItems) {
+ buildParameterFromFilterItem(parameters, childItem);
+ }
+ } else {
+ if (item.getOperand() instanceof QueryParameter) {
+ parameters.add((QueryParameter) item.getOperand());
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/query/FilterClause.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/query/FilterClause.java b/core/src/main/java/org/apache/metamodel/query/FilterClause.java
new file mode 100644
index 0000000..591fb01
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/query/FilterClause.java
@@ -0,0 +1,88 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eobjects.metamodel.query;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eobjects.metamodel.schema.Column;
+
+/**
+ * Represents a clause of filters in the query. This type of clause is used for
+ * the WHERE and HAVING parts of an SQL query.
+ *
+ * Each provided FilterItem will be evaluated with the logical AND operator,
+ * which requires that all filters are applied. Alternatively, if you wan't to
+ * use an OR operator, then use the appropriate constructor of FilterItem to
+ * create a composite filter.
+ *
+ * @see FilterItem
+ */
+public class FilterClause extends AbstractQueryClause<FilterItem> {
+
+ private static final long serialVersionUID = -9077342278766808934L;
+
+ public FilterClause(Query query, String prefix) {
+ super(query, prefix, AbstractQueryClause.DELIM_AND);
+ }
+
+ public List<SelectItem> getEvaluatedSelectItems() {
+ List<SelectItem> result = new ArrayList<SelectItem>();
+ List<FilterItem> items = getItems();
+ for (FilterItem item : items) {
+ addEvaluatedSelectItems(result, item);
+ }
+ return result;
+ }
+
+ private void addEvaluatedSelectItems(List<SelectItem> result,
+ FilterItem item) {
+ FilterItem[] orItems = item.getChildItems();
+ if (orItems != null) {
+ for (FilterItem filterItem : orItems) {
+ addEvaluatedSelectItems(result, filterItem);
+ }
+ }
+ SelectItem selectItem = item.getSelectItem();
+ if (selectItem != null && !result.contains(selectItem)) {
+ result.add(selectItem);
+ }
+ Object operand = item.getOperand();
+ if (operand != null && operand instanceof SelectItem
+ && !result.contains(operand)) {
+ result.add((SelectItem) operand);
+ }
+ }
+
+ /**
+ * Traverses the items and evaluates whether or not the given column is
+ * referenced in either of them.
+ *
+ * @param column
+ * @return true if the column is referenced in the clause or false if not
+ */
+ public boolean isColumnReferenced(Column column) {
+ for (FilterItem item : getItems()) {
+ if (item.isReferenced(column)) {
+ return true;
+ }
+ }
+ return false;
+ }
+}
\ No newline at end of file