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:30 UTC
[47/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/data/RowBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/RowBuilder.java b/core/src/main/java/org/apache/metamodel/data/RowBuilder.java
new file mode 100644
index 0000000..0602ec5
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/RowBuilder.java
@@ -0,0 +1,119 @@
+/**
+ * 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.data;
+
+import org.eobjects.metamodel.insert.RowInsertionBuilder;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Table;
+import org.eobjects.metamodel.update.RowUpdationBuilder;
+
+/**
+ * Abstract interface for objects that build rows, either for eg. insert or
+ * update purposes.
+ *
+ * @see RowInsertionBuilder
+ * @see RowUpdationBuilder
+ *
+ * @param <RB>
+ * the RowBuilder subtype, used for cascading return values
+ */
+public interface RowBuilder<RB extends RowBuilder<?>> {
+
+ /**
+ * Gets the table that this row builder pertains to.
+ *
+ * @return the table that this row builder pertains to.
+ */
+ public Table getTable();
+
+ /**
+ * Sets the value of a column, by column index
+ *
+ * @param columnIndex
+ * @param value
+ * @return
+ */
+ public RB value(int columnIndex, Object value);
+
+ /**
+ * Sets the value of a column, by column index
+ *
+ * @param columnIndex
+ * @param value
+ * @param style
+ * @return
+ */
+ public RB value(int columnIndex, Object value, Style style);
+
+ /**
+ * Sets the value of a column
+ *
+ * @param column
+ * @param value
+ * @return
+ */
+ public RB value(Column column, Object value);
+
+ /**
+ * Sets the value of a column
+ *
+ * @param column
+ * @param value
+ * @param style
+ * @return
+ */
+ public RB value(Column column, Object value, Style style);
+
+ /**
+ * Sets the value of a column, by column name
+ *
+ * @param columnName
+ * @param value
+ * @return
+ */
+ public RB value(String columnName, Object value);
+
+ /**
+ * Sets the value and the style of this value of a column, by column name
+ *
+ * @param columnName
+ * @param value
+ * @param style
+ * @return
+ */
+ public RB value(String columnName, Object value, Style style);
+
+ /**
+ * Gets the built record represented as a {@link Row} object.
+ *
+ * @return a {@link Row} object as it will appear if committed and queried.
+ */
+ public Row toRow();
+
+ /**
+ * Determines if a column's value has been explicitly specified or not. This
+ * can be used to tell explicit NULL values apart from just unspecified
+ * values in a statement.
+ *
+ * @param column
+ * the column to check
+ * @return true if the column's value has been set, or false if not
+ */
+ public boolean isSet(Column column);
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/data/RowPublisher.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/RowPublisher.java b/core/src/main/java/org/apache/metamodel/data/RowPublisher.java
new file mode 100644
index 0000000..fe7678b
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/RowPublisher.java
@@ -0,0 +1,70 @@
+/**
+ * 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.data;
+
+/**
+ * An object on which a push-style data reader can publish records to a
+ * {@link RowPublisherDataSet}. The {@link RowPublisher} acts as a buffer
+ * between the publishing and consuming part of a dataset scenario. It will
+ * manage a queue of rows and will block calls if the queue is not being
+ * read/emptied as fast as it is being filled.
+ *
+ * @author Kasper Sørensen
+ */
+public interface RowPublisher {
+
+ /**
+ * Publishes a row
+ *
+ * @param row
+ * the {@link Row} to publish.
+ * @return a boolean indicating whether or not the consumer is still
+ * interested in more rows.
+ */
+ public boolean publish(Row row);
+
+ /**
+ * Publishes a row, represented by an array of values.
+ *
+ * @param values
+ * the objects to convert to a row.
+ * @return a boolean indicating whether or not the consumer is still
+ * interested in more rows.
+ */
+ public boolean publish(Object[] values);
+
+ /**
+ * Publishes a row, represented by an array of values and an array of
+ * styles.
+ *
+ * @param values
+ * the objects to convert to a row.
+ * @param styles
+ * the styles that correspond to the values.
+ * @return a boolean indicating whether or not the consumer is still
+ * interested in more rows.
+ */
+ public boolean publish(Object[] values, Style[] styles);
+
+ /**
+ * Invoked to indicate to the consumer that no more rows will be published.
+ */
+ public void finished();
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/data/RowPublisherDataSet.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/RowPublisherDataSet.java b/core/src/main/java/org/apache/metamodel/data/RowPublisherDataSet.java
new file mode 100644
index 0000000..addbeae
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/RowPublisherDataSet.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.data;
+
+import org.eobjects.metamodel.query.SelectItem;
+import org.eobjects.metamodel.util.Action;
+import org.eobjects.metamodel.util.SharedExecutorService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Abstract {@link DataSet} implementation for use in scenarios where a
+ * pull-oriented style of reading data is not supported. This implementation
+ * instead allows a publshing action to publish rows to the dataset in a
+ * blocking manner, and thereby to adapt without having to load all rows into
+ * memory.
+ *
+ * @author Kasper Sørensen
+ */
+public final class RowPublisherDataSet extends AbstractDataSet {
+
+ private static final Logger logger = LoggerFactory
+ .getLogger(RowPublisherDataSet.class);
+
+ private final int _maxRows;
+ private final Action<RowPublisher> _publishAction;
+ private RowPublisherImpl _rowPublisher;
+ private boolean _closed;
+
+ public RowPublisherDataSet(SelectItem[] selectItems, int maxRows,
+ Action<RowPublisher> publishAction) {
+ super(selectItems);
+ _maxRows = maxRows;
+ _publishAction = publishAction;
+ _closed = false;
+ }
+
+ public int getMaxRows() {
+ return _maxRows;
+ }
+
+ @Override
+ public void close() {
+ super.close();
+ _closed = true;
+ _rowPublisher.finished();
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ super.finalize();
+ if (!_closed) {
+ logger.warn(
+ "finalize() invoked, but DataSet is not closed. Invoking close() on {}",
+ this);
+ close();
+ }
+ }
+
+ @Override
+ public boolean next() {
+ if (_rowPublisher == null) {
+ // first time, create the publisher
+ _rowPublisher = new RowPublisherImpl(this);
+ logger.info("Starting separate thread for publishing action: {}",
+ _publishAction);
+ Runnable runnable = new Runnable() {
+ public void run() {
+ boolean successful = false;
+ try {
+ _publishAction.run(_rowPublisher);
+ logger.debug("Publshing action finished!");
+ successful = true;
+ } catch (Exception e) {
+ _rowPublisher.failed(e);
+ }
+ if (successful) {
+ _rowPublisher.finished();
+ }
+ };
+ };
+ SharedExecutorService.get().submit(runnable);
+ }
+ return _rowPublisher.next();
+ }
+
+ @Override
+ public Row getRow() {
+ if (_rowPublisher == null) {
+ return null;
+ }
+ return _rowPublisher.getRow();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/data/RowPublisherImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/RowPublisherImpl.java b/core/src/main/java/org/apache/metamodel/data/RowPublisherImpl.java
new file mode 100644
index 0000000..6fe2a0d
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/RowPublisherImpl.java
@@ -0,0 +1,126 @@
+/**
+ * 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.data;
+
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.eobjects.metamodel.MetaModelException;
+
+/**
+ * Row publisher implementation used by {@link RowPublisherDataSet}.
+ *
+ * @author Kasper Sørensen
+ */
+class RowPublisherImpl implements RowPublisher {
+
+ public static final int BUFFER_SIZE = 20;
+
+ private final RowPublisherDataSet _dataSet;
+ private final BlockingQueue<Row> _queue;
+ private final AtomicBoolean _finished;
+ private final AtomicInteger _rowCount;
+ private volatile Row _currentRow;
+ private volatile Exception _error;
+
+ public RowPublisherImpl(RowPublisherDataSet dataSet) {
+ _dataSet = dataSet;
+ _queue = new ArrayBlockingQueue<Row>(BUFFER_SIZE);
+ _finished = new AtomicBoolean(false);
+ _rowCount = new AtomicInteger();
+ }
+
+ @Override
+ public boolean publish(Row row) {
+ if (_finished.get()) {
+ return false;
+ }
+ while (!offer(row)) {
+ if (_finished.get()) {
+ return false;
+ }
+ // wait one more cycle
+ }
+ int rowCount = _rowCount.incrementAndGet();
+ if (_dataSet.getMaxRows() > 0 && rowCount >= _dataSet.getMaxRows()) {
+ finished();
+ return false;
+ }
+ return true;
+ }
+
+ private boolean offer(Row row) {
+ try {
+ return _queue.offer(row, 1000, TimeUnit.MICROSECONDS);
+ } catch (InterruptedException e) {
+ // do nothing
+ return false;
+ }
+ }
+
+ @Override
+ public boolean publish(Object[] values) {
+ Row row = new DefaultRow(_dataSet.getHeader(), values);
+ return publish(row);
+ }
+
+ @Override
+ public boolean publish(Object[] values, Style[] styles) {
+ Row row = new DefaultRow(_dataSet.getHeader(), values, styles);
+ return publish(row);
+ }
+
+ @Override
+ public void finished() {
+ _finished.set(true);
+ }
+
+ public boolean next() {
+ if (_queue.isEmpty() && _finished.get()) {
+ return false;
+ }
+ try {
+ _currentRow = _queue.poll(1000, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ // do nothing
+ }
+ if (_currentRow != null) {
+ return true;
+ }
+ if (_error != null) {
+ if (_error instanceof RuntimeException) {
+ throw (RuntimeException) _error;
+ }
+ throw new MetaModelException(_error);
+ }
+ // "busy" (1 second) wait
+ return next();
+ }
+
+ public Row getRow() {
+ return _currentRow;
+ }
+
+ public void failed(Exception error) {
+ _error = error;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/data/SimpleDataSetHeader.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/SimpleDataSetHeader.java b/core/src/main/java/org/apache/metamodel/data/SimpleDataSetHeader.java
new file mode 100644
index 0000000..1e7e461
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/SimpleDataSetHeader.java
@@ -0,0 +1,132 @@
+/**
+ * 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.data;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.eobjects.metamodel.MetaModelHelper;
+import org.eobjects.metamodel.query.SelectItem;
+import org.eobjects.metamodel.schema.Column;
+
+/**
+ * Simple implementation of {@link DataSetHeader} which does no magic to improve
+ * performance.
+ *
+ * Note that except for datasets with very few records, the
+ * {@link CachingDataSetHeader} is preferred.
+ */
+public class SimpleDataSetHeader implements DataSetHeader {
+
+ private static final long serialVersionUID = 1L;
+
+ private final List<SelectItem> _items;
+
+ public SimpleDataSetHeader(List<SelectItem> items) {
+ _items = items;
+ }
+
+ public SimpleDataSetHeader(SelectItem[] selectItems) {
+ this(Arrays.asList(selectItems));
+ }
+
+ public SimpleDataSetHeader(Column[] columns) {
+ this(MetaModelHelper.createSelectItems(columns));
+ }
+
+ @Override
+ public final SelectItem[] getSelectItems() {
+ return _items.toArray(new SelectItem[_items.size()]);
+ }
+
+ @Override
+ public final int size() {
+ return _items.size();
+ }
+
+ @Override
+ public SelectItem getSelectItem(int index) {
+ return _items.get(index);
+ }
+
+ @Override
+ public int indexOf(Column column) {
+ if (column == null) {
+ return -1;
+ }
+ return indexOf(new SelectItem(column));
+ }
+
+ @Override
+ public int indexOf(SelectItem item) {
+ if (item == null) {
+ return -1;
+ }
+ int i = 0;
+ for (SelectItem selectItem : _items) {
+ if (item == selectItem) {
+ return i;
+ }
+ i++;
+ }
+
+ i = 0;
+ for (SelectItem selectItem : _items) {
+ if (item.equalsIgnoreAlias(selectItem, true)) {
+ return i;
+ }
+ i++;
+ }
+
+ i = 0;
+ for (SelectItem selectItem : _items) {
+ if (item.equalsIgnoreAlias(selectItem)) {
+ return i;
+ }
+ i++;
+ }
+
+ return -1;
+ }
+
+ @Override
+ public final int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((_items == null) ? 0 : _items.hashCode());
+ return result;
+ }
+
+ @Override
+ public final boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ SimpleDataSetHeader other = (SimpleDataSetHeader) obj;
+ if (_items == null) {
+ if (other._items != null)
+ return false;
+ } else if (!_items.equals(other._items))
+ return false;
+ return true;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/data/Style.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/Style.java b/core/src/main/java/org/apache/metamodel/data/Style.java
new file mode 100644
index 0000000..f8ee46f
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/Style.java
@@ -0,0 +1,156 @@
+/**
+ * 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.data;
+
+import java.io.Serializable;
+
+/**
+ * A {@link Style} represents the visual presentation ('styling') attributes of
+ * a value in a {@link Row}. Styling can be used to highlight special values and
+ * format the cells of eg. a spreadsheet.
+ *
+ * Most datastores don't support styling, but some do. Those who do not support
+ * it will just omit it.
+ *
+ * Creation of {@link Style} objects is handled by the {@link StyleBuilder}
+ * class.
+ *
+ * @author Kasper Sørensen
+ */
+public interface Style extends Serializable {
+
+ /**
+ * A style object used for values without styling, "unstyled" values or
+ * "neutrally styled" values.
+ */
+ public static final Style NO_STYLE = new StyleImpl();
+
+ /**
+ * Represents the text alignment of a value.
+ *
+ * @author Kasper Sørensen
+ */
+ public static enum TextAlignment {
+ LEFT, RIGHT, CENTER, JUSTIFY
+ }
+
+ /**
+ * Represents a color used for value highlighting.
+ *
+ * Creation of {@link Color} objects is handled by the static
+ * {@link StyleBuilder}.createColor(...) methods.
+ *
+ * @author Kasper Sørensen
+ */
+ public static interface Color extends Serializable {
+
+ public short getRed();
+
+ public short getGreen();
+
+ public short getBlue();
+ }
+
+ /**
+ * Represents a unit of sizing elements (eg. fonts) in a {@link Style}.
+ *
+ * @author Kasper Sørensen
+ */
+ public static enum SizeUnit {
+ /**
+ * Point unit
+ */
+ PT,
+
+ /**
+ * Pixel unit
+ */
+ PX,
+
+ /**
+ * Percent unit
+ */
+ PERCENT
+ }
+
+ /**
+ * Determines whether or not the value is written in bold text.
+ *
+ * @return true if text is bold
+ */
+ public boolean isBold();
+
+ /**
+ * Determines whether or not the value is written in italic text.
+ *
+ * @return true if text is italic
+ */
+ public boolean isItalic();
+
+ /**
+ * Determines whether or not the value is written with an underline
+ *
+ * @return true if text is underlined
+ */
+ public boolean isUnderline();
+
+ /**
+ * Gets the font size, or null if font size is unspecified.
+ *
+ * @see SizeUnit
+ *
+ * @return an Integer, or null
+ */
+ public Integer getFontSize();
+
+ /**
+ * Gets the unit of the font size.
+ *
+ * @return an enum representing the font size unit used.
+ */
+ public SizeUnit getFontSizeUnit();
+
+ /**
+ * Gets the text alignment, or null if text alignment is unspecified.
+ *
+ * @return a TextAlignment value, or null
+ */
+ public TextAlignment getAlignment();
+
+ /**
+ * Gets the foreground (text) color, or null if the color is unspecified.
+ *
+ * @return a Color object representing the foreground color
+ */
+ public Color getForegroundColor();
+
+ /**
+ * Gets the background color, or null if the color is unspecified.
+ *
+ * @return a Color object representing the background color
+ */
+ public Color getBackgroundColor();
+
+ /**
+ * Creates a Cascading Style Sheets (CSS) representation of this style.
+ *
+ * @return a CSS string
+ */
+ public String toCSS();
+}
\ 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/data/StyleBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/StyleBuilder.java b/core/src/main/java/org/apache/metamodel/data/StyleBuilder.java
new file mode 100644
index 0000000..ac4c6c7
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/StyleBuilder.java
@@ -0,0 +1,355 @@
+/**
+ * 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.data;
+
+import java.util.Map;
+import java.util.WeakHashMap;
+
+import org.eobjects.metamodel.data.Style.Color;
+import org.eobjects.metamodel.data.Style.SizeUnit;
+import org.eobjects.metamodel.data.Style.TextAlignment;
+import org.eobjects.metamodel.util.EqualsBuilder;
+
+/**
+ * Builder class for {@link Style} and related objects, like {@link Color}.
+ *
+ * @author Kasper Sørensen
+ */
+public final class StyleBuilder {
+
+ private static final Map<String, Color> _colorCache = new WeakHashMap<String, Color>();
+
+ private boolean _bold;
+ private boolean _italic;
+ private boolean _underline;
+ private Integer _fontSize;
+ private TextAlignment _alignment;
+ private Color _backgroundColor;
+ private Color _foregroundColor;
+ private SizeUnit _fontSizeUnit;
+
+ private final Color _defaultForegroundColor;
+ private final Color _defaultBackgroundColor;
+
+ /**
+ * Constructs a new {@link StyleBuilder} with the default foreground and
+ * background colors.
+ */
+ public StyleBuilder() {
+ this(createColor((short) 0, (short) 0, (short) 0), createColor(
+ (short) 255, (short) 255, (short) 255));
+ }
+
+ /**
+ * Constructs a new {@link StyleBuilder} with a specified default foreground
+ * and background colors. These colors will be disregarded, if posted to the
+ * foreground and background methods.
+ *
+ * @param defaultForegroundColor
+ * @param defaultBackgroundColor
+ */
+ public StyleBuilder(Color defaultForegroundColor,
+ Color defaultBackgroundColor) {
+ _defaultForegroundColor = defaultForegroundColor;
+ _defaultBackgroundColor = defaultBackgroundColor;
+ }
+
+ /**
+ * Resets the state of the built style, which will conceptually match it
+ * with {@link Style#NO_STYLE}.
+ */
+ public void reset() {
+ _bold = false;
+ _italic = false;
+ _underline = false;
+ _fontSize = null;
+ _alignment = null;
+ _backgroundColor = null;
+ _foregroundColor = null;
+ _fontSizeUnit = null;
+ }
+
+ /**
+ * Creates a {@link Style} object based on the build characteristics.
+ *
+ * @return a {@link Style} object based on the build characteristics.
+ */
+ public Style create() {
+ StyleImpl style = new StyleImpl(_bold, _italic, _underline, _fontSize,
+ _fontSizeUnit, _alignment, _backgroundColor, _foregroundColor);
+ if (Style.NO_STYLE.equals(style)) {
+ return Style.NO_STYLE;
+ }
+ return style;
+ }
+
+ /**
+ * Sets the font weight to bold
+ *
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder bold() {
+ _bold = true;
+ return this;
+ }
+
+ /**
+ * Sets the font style to italic
+ *
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder italic() {
+ _italic = true;
+ return this;
+ }
+
+ /**
+ * Sets the text decoration to underlined
+ *
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder underline() {
+ _underline = true;
+ return this;
+ }
+
+ /**
+ * Creates a Color based on a 6-letter RGB hex color code string, eg.
+ * "000000" for black and "FFFFFF" for white.
+ *
+ * @param rgbColorCode
+ * a 6-letter RGB hex color code string
+ * @return a color representing this color code
+ */
+ public static Color createColor(String rgbColorCode) {
+ assert rgbColorCode.length() == 6;
+ String redParth = rgbColorCode.substring(0, 2);
+ String greenParth = rgbColorCode.substring(2, 4);
+ String blueParth = rgbColorCode.substring(4, 6);
+ return createColor(Integer.parseInt(redParth, 16),
+ Integer.parseInt(greenParth, 16),
+ Integer.parseInt(blueParth, 16));
+ }
+
+ /**
+ * Creates a color based on 3 RGB components, represented as ints
+ *
+ * @param r
+ * @param g
+ * @param b
+ * @return a color representing the RGB code
+ */
+ public static Color createColor(int r, int g, int b) {
+ return createColor(toRgbComponent(r), toRgbComponent(g),
+ toRgbComponent(b));
+ }
+
+ /**
+ * Creates a color based on 3 RGB components, represented as shorts
+ *
+ * @param r
+ * @param g
+ * @param b
+ * @return a color representing the RGB code
+ */
+ public static Color createColor(short r, short g, short b) {
+ String cacheId = r + "," + g + "," + b;
+ Color color = _colorCache.get(cacheId);
+ if (color == null) {
+ color = new ColorImpl(r, g, b);
+ _colorCache.put(cacheId, color);
+ }
+ return color;
+ }
+
+ private static short toRgbComponent(int r) {
+ if (r < 0) {
+ // if eg. a byte was passed as a RGB component
+ r = (256 + r);
+ }
+ if (r > 255) {
+ throw new IllegalArgumentException(
+ "RGB component cannot be higher than 255");
+ }
+ return (short) r;
+ }
+
+ /**
+ * Sets the foreground (text) color of the style
+ *
+ * @param rgbColorCode
+ * a 6-letter hex RGB color code, such as FF0000 (red).
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder foreground(String rgbColorCode) {
+ return foreground(createColor(rgbColorCode));
+ }
+
+ /**
+ * Sets the foreground (text) color of the style
+ *
+ * @param rgb
+ * a triplet array of shorts
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder foreground(short[] rgb) {
+ assert rgb.length == 3;
+ return foreground(createColor(rgb[0], rgb[1], rgb[2]));
+ }
+
+ /**
+ * Sets the foreground (text) color of the style
+ *
+ * @param r
+ * red amount (0-255)
+ * @param g
+ * green amount (0-255)
+ * @param b
+ * blue amount (0-255)
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder foreground(int r, int g, int b) {
+ return foreground(createColor(r, g, b));
+ }
+
+ /**
+ * Sets the foreground (text) color of the style
+ *
+ * @param color
+ * the color to use
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder foreground(Color color) {
+ if (EqualsBuilder.equals(_defaultForegroundColor, color)) {
+ _foregroundColor = null;
+ } else {
+ _foregroundColor = color;
+ }
+ return this;
+ }
+
+ /**
+ * Sets the background (fill) color of the style
+ *
+ * @param rgbColorCode
+ * a 6-letter hex RGB color code, such as FF0000 (red).
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder background(String rgbColorCode) {
+ return background(createColor(rgbColorCode));
+ }
+
+ /**
+ * Sets the background (fill) color of the style
+ *
+ * @param rgb
+ * a triplet array of shorts
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder background(short[] rgb) {
+ assert rgb.length == 3;
+ return background(createColor(rgb[0], rgb[1], rgb[2]));
+ }
+
+ /**
+ * Sets the background (fill) color of the style
+ *
+ * @param r
+ * red amount (0-255)
+ * @param g
+ * green amount (0-255)
+ * @param b
+ * blue amount (0-255)
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder background(int r, int g, int b) {
+ return background(createColor(r, g, b));
+ }
+
+ /**
+ * Sets the background (fill) color of the style
+ *
+ * @param color
+ * the color to use
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder background(Color color) {
+ if (EqualsBuilder.equals(_defaultBackgroundColor, color)) {
+ _backgroundColor = null;
+ } else {
+ _backgroundColor = color;
+ }
+ return this;
+ }
+
+ /**
+ * Sets the font size of the style
+ *
+ * @param fontSize
+ * the font size
+ * @param sizeUnit
+ * the font size unit
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder fontSize(int fontSize, SizeUnit sizeUnit) {
+ _fontSize = fontSize;
+ _fontSizeUnit = sizeUnit;
+ return this;
+ }
+
+ /**
+ * Sets the text alignment to center
+ *
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder centerAligned() {
+ _alignment = TextAlignment.CENTER;
+ return this;
+ }
+
+ /**
+ * Sets the text alignment to left
+ *
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder leftAligned() {
+ _alignment = TextAlignment.LEFT;
+ return this;
+ }
+
+ /**
+ * Sets the text alignment to right
+ *
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder rightAligned() {
+ _alignment = TextAlignment.RIGHT;
+ return this;
+ }
+
+ /**
+ * Sets the text alignment to justify
+ *
+ * @return the {@link StyleBuilder} self (for cascading method calls)
+ */
+ public StyleBuilder justifyAligned() {
+ _alignment = TextAlignment.JUSTIFY;
+ return this;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/data/StyleImpl.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/StyleImpl.java b/core/src/main/java/org/apache/metamodel/data/StyleImpl.java
new file mode 100644
index 0000000..e00e1a9
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/StyleImpl.java
@@ -0,0 +1,177 @@
+/**
+ * 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.data;
+
+import java.util.List;
+
+import org.eobjects.metamodel.util.BaseObject;
+
+/**
+ * Default immutable implementation of {@link Style}.
+ *
+ * @author Kasper Sørensen
+ */
+final class StyleImpl extends BaseObject implements Style {
+
+ private static final long serialVersionUID = 1L;
+
+ private final boolean _underline;
+ private final boolean _italic;
+ private final boolean _bold;
+ private final Integer _fontSize;
+ private final TextAlignment _alignment;
+ private final Color _backgroundColor;
+ private final Color _foregroundColor;
+ private final SizeUnit _fontSizeUnit;
+
+ public StyleImpl() {
+ this(false, false, false, null, null, null, null, null);
+ }
+
+ public StyleImpl(boolean bold, boolean italic, boolean underline,
+ Integer fontSize, SizeUnit fontSizeUnit, TextAlignment alignment,
+ Color backgroundColor, Color foregroundColor) {
+ _bold = bold;
+ _italic = italic;
+ _underline = underline;
+ _fontSize = fontSize;
+ _fontSizeUnit = fontSizeUnit;
+ _alignment = alignment;
+ _backgroundColor = backgroundColor;
+ _foregroundColor = foregroundColor;
+ }
+
+ @Override
+ public boolean isBold() {
+ return _bold;
+ }
+
+ @Override
+ public boolean isItalic() {
+ return _italic;
+ }
+
+ @Override
+ public boolean isUnderline() {
+ return _underline;
+ }
+
+ @Override
+ public Integer getFontSize() {
+ return _fontSize;
+ }
+
+ @Override
+ public SizeUnit getFontSizeUnit() {
+ return _fontSizeUnit;
+ }
+
+ @Override
+ public TextAlignment getAlignment() {
+ return _alignment;
+ }
+
+ @Override
+ public Color getForegroundColor() {
+ return _foregroundColor;
+ }
+
+ @Override
+ public Color getBackgroundColor() {
+ return _backgroundColor;
+ }
+
+ @Override
+ public String toCSS() {
+ StringBuilder sb = new StringBuilder();
+ if (_bold) {
+ sb.append("font-weight: bold;");
+ }
+ if (_italic) {
+ sb.append("font-style: italic;");
+ }
+ if (_underline) {
+ sb.append("text-decoration: underline;");
+ }
+ if (_alignment != null) {
+ sb.append("text-align: " + toCSS(_alignment) + ";");
+ }
+ if (_fontSize != null) {
+ sb.append("font-size: " + _fontSize);
+ switch (_fontSizeUnit) {
+ case PT:
+ sb.append("pt");
+ break;
+ case PX:
+ sb.append("px");
+ break;
+ case PERCENT:
+ sb.append("%");
+ break;
+ default:
+ // don't write a size unit
+ }
+ sb.append(';');
+ }
+ if (_foregroundColor != null) {
+ sb.append("color: " + toCSS(_foregroundColor) + ";");
+ }
+ if (_backgroundColor != null) {
+ sb.append("background-color: " + toCSS(_backgroundColor) + ";");
+ }
+ return sb.toString();
+ }
+
+ private String toCSS(Color c) {
+ return "rgb(" + c.getRed() + "," + c.getGreen() + "," + c.getBlue()
+ + ")";
+ }
+
+ @Override
+ public String toString() {
+ return toCSS();
+ }
+
+ private String toCSS(TextAlignment alignment) {
+ switch (alignment) {
+ case LEFT:
+ return "left";
+ case RIGHT:
+ return "right";
+ case CENTER:
+ return "center";
+ case JUSTIFY:
+ return "justify";
+ default:
+ throw new IllegalStateException("Unknown alignment: " + alignment);
+ }
+ }
+
+ @Override
+ protected void decorateIdentity(List<Object> identifiers) {
+ identifiers.add(_underline);
+ identifiers.add(_italic);
+ identifiers.add(_bold);
+ identifiers.add(_fontSize);
+ identifiers.add(_fontSizeUnit);
+ identifiers.add(_alignment);
+ identifiers.add(_backgroundColor);
+ identifiers.add(_foregroundColor);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/data/SubSelectionDataSet.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/SubSelectionDataSet.java b/core/src/main/java/org/apache/metamodel/data/SubSelectionDataSet.java
new file mode 100644
index 0000000..1a1df90
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/SubSelectionDataSet.java
@@ -0,0 +1,57 @@
+/**
+ * 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.data;
+
+import org.eobjects.metamodel.query.SelectItem;
+
+/**
+ * {@link DataSet} wrapper for doing subselection.
+ *
+ * @author Kasper Sørensen
+ */
+public final class SubSelectionDataSet extends AbstractDataSet {
+
+ private final DataSet _dataSet;
+
+ public SubSelectionDataSet(SelectItem[] selectItemsArray, DataSet dataSet) {
+ super(selectItemsArray);
+ _dataSet = dataSet;
+ }
+
+ public DataSet getWrappedDataSet() {
+ return _dataSet;
+ }
+
+ @Override
+ public boolean next() {
+ return _dataSet.next();
+ }
+
+ @Override
+ public Row getRow() {
+ final DataSetHeader header = getHeader();
+ return _dataSet.getRow().getSubSelection(header);
+ }
+
+ @Override
+ public void close() {
+ super.close();
+ _dataSet.close();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/data/WhereClauseBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/WhereClauseBuilder.java b/core/src/main/java/org/apache/metamodel/data/WhereClauseBuilder.java
new file mode 100644
index 0000000..4865bad
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/WhereClauseBuilder.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.data;
+
+import org.eobjects.metamodel.query.FilterItem;
+import org.eobjects.metamodel.query.builder.FilterBuilder;
+import org.eobjects.metamodel.schema.Column;
+
+/**
+ * An interface for builder components that formulate a WHERE clause, either for
+ * querying, updating, deleting or other purposes.
+ *
+ * @param <T>
+ * the return type of the {@link WhereClauseBuilder}s builder methods
+ */
+public interface WhereClauseBuilder<T> {
+
+ /**
+ * Defines a where item to set as a criteria
+ *
+ * @param column
+ * a column to apply a criteria for
+ * @return a builder object for further building the where item
+ */
+ public FilterBuilder<T> where(Column column);
+
+ /**
+ * Defines a where item to set as a criteria
+ *
+ * @param columnName
+ * the name of the colum to which the criteria will be applied
+ * @return a builder object for further building the where item
+ */
+ public FilterBuilder<T> where(String columnName);
+
+ /**
+ * Applies where items to set criteria
+ *
+ * @param filterItems
+ * the where items to set
+ * @return the builder object itself, for further building of the update
+ */
+ public T where(FilterItem... filterItems);
+
+ /**
+ * Applies where items to set criteria
+ *
+ * @param filterItems
+ * the where items to set
+ * @return the builder object, for further building of the update
+ */
+ public T where(Iterable<FilterItem> filterItems);
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/data/package-info.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/data/package-info.java b/core/src/main/java/org/apache/metamodel/data/package-info.java
new file mode 100644
index 0000000..572f5a3
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/data/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.
+ */
+/**
+ * API for data sets
+ */
+package org.eobjects.metamodel.data;
+
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/delete/AbstractRowDeletionBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/delete/AbstractRowDeletionBuilder.java b/core/src/main/java/org/apache/metamodel/delete/AbstractRowDeletionBuilder.java
new file mode 100644
index 0000000..7354f52
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/delete/AbstractRowDeletionBuilder.java
@@ -0,0 +1,135 @@
+/**
+ * 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.delete;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eobjects.metamodel.data.Row;
+import org.eobjects.metamodel.query.FilterClause;
+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;
+
+/**
+ * Abstract {@link RowDeletionBuilder} implementation
+ */
+public abstract class AbstractRowDeletionBuilder implements RowDeletionBuilder {
+
+ private final Table _table;
+ private final List<FilterItem> _whereItems;
+
+ public AbstractRowDeletionBuilder(Table table) {
+ if (table == null) {
+ throw new IllegalArgumentException("Table cannot be null");
+ }
+ _table = table;
+ _whereItems = new ArrayList<FilterItem>();
+ }
+
+ protected List<FilterItem> getWhereItems() {
+ return _whereItems;
+ }
+
+ @Override
+ public FilterBuilder<RowDeletionBuilder> where(Column column) {
+ 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 = _table.getColumnByName(columnName);
+ if (column == null) {
+ throw new IllegalArgumentException("No such column: " + columnName);
+ }
+ return where(column);
+ }
+
+ @Override
+ public RowDeletionBuilder where(FilterItem... filterItems) {
+ for (FilterItem filterItem : filterItems) {
+ _whereItems.add(filterItem);
+ }
+ return this;
+ }
+
+ @Override
+ public RowDeletionBuilder where(Iterable<FilterItem> filterItems) {
+ for (FilterItem filterItem : filterItems) {
+ _whereItems.add(filterItem);
+ }
+ return this;
+ }
+
+ @Override
+ public Table getTable() {
+ return _table;
+ }
+
+ /**
+ * Determines if a row should be deleted or not (can be used by subclasses
+ * as a convenient determinator).
+ *
+ * @param row
+ * @return true if the row should be deleted.
+ */
+ protected boolean deleteRow(Row row) {
+ final List<FilterItem> whereItems = getWhereItems();
+ for (FilterItem filterItem : whereItems) {
+ if (!filterItem.evaluate(row)) {
+ // since filter items are ANDed, if any item does not evaluate
+ // to true, the row is not deleted
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Convenience method to tell subclasses if the delete operation represents
+ * a full table truncate operation. Usually such operations can be optimized
+ * by simply removing the table (and maybe restoring similar headers in a
+ * new table).
+ *
+ * @return
+ */
+ protected boolean isTruncateTableOperation() {
+ final List<FilterItem> whereItems = getWhereItems();
+ return whereItems.isEmpty();
+ }
+
+ @Override
+ public String toString() {
+ return toSql();
+ }
+
+ @Override
+ public String toSql() {
+ return "DELETE FROM " + _table.getQualifiedLabel() + new FilterClause(null, " WHERE ").addItems(_whereItems);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/delete/DeleteFrom.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/delete/DeleteFrom.java b/core/src/main/java/org/apache/metamodel/delete/DeleteFrom.java
new file mode 100644
index 0000000..59d1adf
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/delete/DeleteFrom.java
@@ -0,0 +1,94 @@
+/**
+ * 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.delete;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.UpdateCallback;
+import org.eobjects.metamodel.UpdateScript;
+import org.eobjects.metamodel.UpdateableDataContext;
+import org.eobjects.metamodel.data.WhereClauseBuilder;
+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;
+
+/**
+ * Represents a single DELETE FROM operation to be applied to a
+ * {@link UpdateableDataContext}. Instead of providing a custom implementation
+ * of the {@link UpdateScript} interface, one can use this pre-built delete from
+ * implementation. Some {@link DataContext}s may even optimize specifically
+ * based on the knowledge that there will only be a single delete from statement
+ * executed.
+ */
+public final class DeleteFrom implements UpdateScript, WhereClauseBuilder<DeleteFrom> {
+
+ private final List<FilterItem> _whereItems;
+ private final Table _table;
+
+ public DeleteFrom(Table table) {
+ _table = table;
+ _whereItems = new ArrayList<FilterItem>();
+ }
+
+ @Override
+ public void run(UpdateCallback callback) {
+ callback.deleteFrom(_table).where(_whereItems).execute();
+ }
+
+ @Override
+ public FilterBuilder<DeleteFrom> where(Column column) {
+ SelectItem selectItem = new SelectItem(column);
+ return new AbstractFilterBuilder<DeleteFrom>(selectItem) {
+ @Override
+ protected DeleteFrom applyFilter(FilterItem filter) {
+ return where(filter);
+ }
+ };
+ }
+
+ @Override
+ public FilterBuilder<DeleteFrom> where(String columnName) {
+ Column column = _table.getColumnByName(columnName);
+ if (column == null) {
+ throw new IllegalArgumentException("No such column: " + columnName);
+ }
+ return where(column);
+ }
+
+ @Override
+ public DeleteFrom where(FilterItem... filterItems) {
+ for (FilterItem filterItem : filterItems) {
+ _whereItems.add(filterItem);
+ }
+ return this;
+ }
+
+ @Override
+ public DeleteFrom where(Iterable<FilterItem> filterItems) {
+ for (FilterItem filterItem : filterItems) {
+ _whereItems.add(filterItem);
+ }
+ return this;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/delete/RowDeletable.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/delete/RowDeletable.java b/core/src/main/java/org/apache/metamodel/delete/RowDeletable.java
new file mode 100644
index 0000000..43be6d8
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/delete/RowDeletable.java
@@ -0,0 +1,68 @@
+/**
+ * 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.delete;
+
+import org.eobjects.metamodel.schema.Table;
+
+public interface RowDeletable {
+
+ /**
+ * Determines whether row delete is supported
+ *
+ * @return true if row delete is supported
+ */
+ public boolean isDeleteSupported();
+
+ /**
+ * Initiates a row deletion builder.
+ *
+ * @param table
+ * @return
+ * @throws IllegalArgumentException
+ * @throws IllegalStateException
+ * @throws UnsupportedOperationException
+ */
+ public RowDeletionBuilder deleteFrom(Table table) throws IllegalArgumentException, IllegalStateException,
+ UnsupportedOperationException;
+
+ /**
+ * Initiates a row deletion builder.
+ *
+ * @param tableName
+ * @return
+ * @throws IllegalArgumentException
+ * @throws IllegalStateException
+ * @throws UnsupportedOperationException
+ */
+ public RowDeletionBuilder deleteFrom(String tableName) throws IllegalArgumentException, IllegalStateException,
+ UnsupportedOperationException;
+
+ /**
+ * Initiates a row deletion builder.
+ *
+ * @param schemaName
+ * @param tableName
+ * @return
+ * @throws IllegalArgumentException
+ * @throws IllegalStateException
+ * @throws UnsupportedOperationException
+ */
+ public RowDeletionBuilder deleteFrom(String schemaName, String tableName) throws IllegalArgumentException,
+ IllegalStateException, UnsupportedOperationException;
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/delete/RowDeletionBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/delete/RowDeletionBuilder.java b/core/src/main/java/org/apache/metamodel/delete/RowDeletionBuilder.java
new file mode 100644
index 0000000..90ce319
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/delete/RowDeletionBuilder.java
@@ -0,0 +1,57 @@
+/**
+ * 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.delete;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.data.WhereClauseBuilder;
+import org.eobjects.metamodel.schema.Table;
+
+/**
+ * Builder object for row deletions in a {@link Table}.
+ *
+ * @author Kasper Sørensen
+ */
+public interface RowDeletionBuilder extends WhereClauseBuilder<RowDeletionBuilder> {
+
+ /**
+ * Gets the table that this delete statement pertains to.
+ *
+ * @return the table that this delete statement pertains to.
+ */
+ public Table getTable();
+
+ /**
+ * Gets a SQL representation of this delete operation. Note that the
+ * generated SQL is dialect agnostic, so it is not accurately the same as
+ * what will be passed to a potential backing database.
+ *
+ * @return a SQL representation of this delete operation.
+ */
+ public String toSql();
+
+ /**
+ * Commits the row deletion operation. This operation will delete rows in
+ * the {@link DataContext}.
+ *
+ * @throws MetaModelException
+ * if the operation was rejected
+ */
+ public void execute() throws MetaModelException;
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/delete/package-info.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/delete/package-info.java b/core/src/main/java/org/apache/metamodel/delete/package-info.java
new file mode 100644
index 0000000..37b96f7
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/delete/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.
+ */
+/**
+ * API for deleting rows
+ */
+package org.eobjects.metamodel.delete;
+
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/drop/AbstractTableDropBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/drop/AbstractTableDropBuilder.java b/core/src/main/java/org/apache/metamodel/drop/AbstractTableDropBuilder.java
new file mode 100644
index 0000000..2fa26a5
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/drop/AbstractTableDropBuilder.java
@@ -0,0 +1,51 @@
+/**
+ * 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.drop;
+
+import org.eobjects.metamodel.schema.Table;
+
+/**
+ * Abstract {@link TableDropBuilder} implementation
+ */
+public abstract class AbstractTableDropBuilder implements TableDropBuilder {
+
+ private final Table _table;
+
+ public AbstractTableDropBuilder(Table table) {
+ if (table == null) {
+ throw new IllegalArgumentException("Table cannot be null");
+ }
+ _table = table;
+ }
+
+ @Override
+ public final Table getTable() {
+ return _table;
+ }
+
+ @Override
+ public String toString() {
+ return toSql();
+ }
+
+ @Override
+ public String toSql() {
+ return "DROP TABLE " + _table.getQualifiedLabel();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/drop/DropTable.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/drop/DropTable.java b/core/src/main/java/org/apache/metamodel/drop/DropTable.java
new file mode 100644
index 0000000..f150c50
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/drop/DropTable.java
@@ -0,0 +1,68 @@
+/**
+ * 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.drop;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.UpdateCallback;
+import org.eobjects.metamodel.UpdateScript;
+import org.eobjects.metamodel.UpdateableDataContext;
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+
+/**
+ * Represents a single DROP TABLE operation to be applied to a
+ * {@link UpdateableDataContext}. Instead of providing a custom implementation
+ * of the {@link UpdateScript} interface, one can use this pre-built drop table
+ * implementation. Some {@link DataContext}s may even optimize specifically
+ * based on the knowledge that there will only be a single table dropped.
+ */
+public final class DropTable implements UpdateScript {
+
+ private final String _schemaName;
+ private final String _tableName;
+
+ public DropTable(Table table) {
+ this(table.getSchema().getName(), table.getName());
+ }
+
+ public DropTable(String tableName) {
+ this((String) null, tableName);
+ }
+
+ public DropTable(Schema schema, String tableName) {
+ this(schema.getName(), tableName);
+ }
+
+ public DropTable(String schemaName, String tableName) {
+ _schemaName = schemaName;
+ _tableName = tableName;
+ }
+
+ @Override
+ public void run(UpdateCallback callback) {
+ final TableDropBuilder dropBuilder;
+ if (_schemaName == null) {
+ dropBuilder = callback.dropTable(_tableName);
+ } else {
+ dropBuilder = callback.dropTable(_schemaName, _tableName);
+ }
+ dropBuilder.execute();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/drop/TableDropBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/drop/TableDropBuilder.java b/core/src/main/java/org/apache/metamodel/drop/TableDropBuilder.java
new file mode 100644
index 0000000..34bce59
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/drop/TableDropBuilder.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.drop;
+
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.schema.Table;
+
+public interface TableDropBuilder {
+
+ /**
+ * Gets the table that this drop statement pertains to
+ *
+ * @return the table that this drop statement pertains to
+ */
+ public Table getTable();
+
+ /**
+ * Gets a SQL representation of this drop table operation. Note that the
+ * generated SQL is dialect agnostic, so it is not accurately the same as
+ * what will be passed to a potential backing database.
+ *
+ * @return a SQL representation of this drop table operation.
+ */
+ public String toSql();
+
+ /**
+ * Executes the drop table operation
+ *
+ * @throws MetaModelException
+ * if the operation was rejected
+ */
+ public void execute() throws MetaModelException;
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/drop/TableDroppable.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/drop/TableDroppable.java b/core/src/main/java/org/apache/metamodel/drop/TableDroppable.java
new file mode 100644
index 0000000..d5c8260
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/drop/TableDroppable.java
@@ -0,0 +1,44 @@
+/**
+ * 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.drop;
+
+import org.eobjects.metamodel.schema.Schema;
+import org.eobjects.metamodel.schema.Table;
+
+public interface TableDroppable {
+
+ /**
+ * Determines whether table drop is supported
+ *
+ * @return true if table drop is supported
+ */
+ public boolean isDropTableSupported();
+
+ public TableDropBuilder dropTable(Schema schema, String tableName) throws IllegalArgumentException,
+ IllegalStateException, UnsupportedOperationException;
+
+ public TableDropBuilder dropTable(String schemaName, String tableName) throws IllegalArgumentException,
+ IllegalStateException, UnsupportedOperationException;
+
+ public TableDropBuilder dropTable(String tableName) throws IllegalArgumentException, IllegalStateException,
+ UnsupportedOperationException;
+
+ public TableDropBuilder dropTable(Table table) throws IllegalArgumentException, IllegalStateException,
+ UnsupportedOperationException;
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/drop/package-info.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/drop/package-info.java b/core/src/main/java/org/apache/metamodel/drop/package-info.java
new file mode 100644
index 0000000..7f51816
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/drop/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.
+ */
+/**
+ * API for dropping tables
+ */
+package org.eobjects.metamodel.drop;
+
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/insert/AbstractRowInsertionBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/insert/AbstractRowInsertionBuilder.java b/core/src/main/java/org/apache/metamodel/insert/AbstractRowInsertionBuilder.java
new file mode 100644
index 0000000..c57c26d
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/insert/AbstractRowInsertionBuilder.java
@@ -0,0 +1,108 @@
+/**
+ * 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.insert;
+
+import org.eobjects.metamodel.UpdateCallback;
+import org.eobjects.metamodel.data.AbstractRowBuilder;
+import org.eobjects.metamodel.data.Row;
+import org.eobjects.metamodel.query.SelectItem;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Table;
+
+/**
+ * Abstract implementation of the {@link RowInsertionBuilder} interface,
+ * provided as a convenience to {@link RowInsertable} implementations. Handles
+ * all the building operations, but not the commit operation.
+ *
+ * @author Kasper Sørensen
+ */
+public abstract class AbstractRowInsertionBuilder<U extends UpdateCallback> extends
+ AbstractRowBuilder<RowInsertionBuilder> implements RowInsertionBuilder {
+
+ private final U _updateCallback;
+ private final Table _table;
+
+ public AbstractRowInsertionBuilder(U updateCallback, Table table) {
+ super(table);
+ _updateCallback = updateCallback;
+ _table = table;
+ }
+
+ @Override
+ public Table getTable() {
+ return _table;
+ }
+
+ protected U getUpdateCallback() {
+ return _updateCallback;
+ }
+
+ @Override
+ public RowInsertionBuilder like(Row row) {
+ SelectItem[] selectItems = row.getSelectItems();
+ for (int i = 0; i < selectItems.length; i++) {
+ SelectItem selectItem = selectItems[i];
+ Column column = selectItem.getColumn();
+ if (column != null) {
+ if (_table == column.getTable()) {
+ value(column, row.getValue(i));
+ } else {
+ value(column.getName(), row.getValue(i));
+ }
+ }
+ }
+ return this;
+ }
+
+ @Override
+ public String toSql() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("INSERT INTO ");
+ sb.append(_table.getQualifiedLabel());
+ sb.append("(");
+ Column[] columns = getColumns();
+ for (int i = 0; i < columns.length; i++) {
+ if (i != 0) {
+ sb.append(',');
+ }
+ sb.append(columns[i].getName());
+ }
+ sb.append(") VALUES (");
+ Object[] values = getValues();
+ for (int i = 0; i < values.length; i++) {
+ Object value = values[i];
+ final String stringValue;
+ if (value == null) {
+ stringValue = "NULL";
+ } else if (value instanceof String) {
+ stringValue = "\"" + value + "\"";
+ } else {
+ stringValue = value.toString();
+ }
+ sb.append(stringValue);
+ }
+ sb.append(")");
+ return sb.toString();
+ }
+
+ @Override
+ public String toString() {
+ return toSql();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/insert/InsertInto.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/insert/InsertInto.java b/core/src/main/java/org/apache/metamodel/insert/InsertInto.java
new file mode 100644
index 0000000..df793b9
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/insert/InsertInto.java
@@ -0,0 +1,77 @@
+/**
+ * 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.insert;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.UpdateCallback;
+import org.eobjects.metamodel.UpdateScript;
+import org.eobjects.metamodel.UpdateableDataContext;
+import org.eobjects.metamodel.data.AbstractRowBuilder;
+import org.eobjects.metamodel.data.RowBuilder;
+import org.eobjects.metamodel.data.Style;
+import org.eobjects.metamodel.schema.Column;
+import org.eobjects.metamodel.schema.Table;
+
+/**
+ * Represents a single INSERT INTO operation to be applied to a
+ * {@link UpdateableDataContext}. Instead of providing a custom implementation
+ * of the {@link UpdateScript} interface, one can use this pre-built
+ * single-record insertion implementation. Some {@link DataContext}s may even
+ * optimize specifically based on the knowledge that there will only be a single
+ * record inserted.
+ */
+public final class InsertInto extends AbstractRowBuilder<InsertInto> implements UpdateScript, RowBuilder<InsertInto> {
+
+ private final Table _table;
+
+ public InsertInto(Table table) {
+ super(table);
+ _table = table;
+ }
+
+ @Override
+ public void run(UpdateCallback callback) {
+ RowInsertionBuilder insertBuilder = callback.insertInto(getTable());
+
+ final Column[] columns = getColumns();
+ final Object[] values = getValues();
+ final Style[] styles = getStyles();
+ final boolean[] explicitNulls = getExplicitNulls();
+
+ for (int i = 0; i < columns.length; i++) {
+ Object value = values[i];
+ Column column = columns[i];
+ Style style = styles[i];
+ if (value == null) {
+ if (explicitNulls[i]) {
+ insertBuilder = insertBuilder.value(column, value, style);
+ }
+ } else {
+ insertBuilder = insertBuilder.value(column, value, style);
+ }
+ }
+
+ insertBuilder.execute();
+ }
+
+ @Override
+ public Table getTable() {
+ return _table;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/insert/RowInsertable.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/insert/RowInsertable.java b/core/src/main/java/org/apache/metamodel/insert/RowInsertable.java
new file mode 100644
index 0000000..bcb3aaa
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/insert/RowInsertable.java
@@ -0,0 +1,92 @@
+/**
+ * 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.insert;
+
+import org.eobjects.metamodel.schema.Table;
+
+/**
+ * An interface for objects that support inserting rows into tables.
+ *
+ * @author Kasper Sørensen
+ */
+public interface RowInsertable {
+
+ /**
+ * Determines whether row insertion is supported
+ *
+ * @return true if row insertion is supported
+ */
+ public boolean isInsertSupported();
+
+ /**
+ * Initiates the building of a row insertion operation.
+ *
+ * @param table
+ * the table to insert a row into
+ * @return a builder object on which values can be added and the statement
+ * can be committed.
+ * @throws IllegalArgumentException
+ * if the table argument is null or invalid.
+ * @throws IllegalStateException
+ * if the connection to the DataContext is read-only or another
+ * access restriction is preventing the operation.
+ * @throws UnsupportedOperationException
+ * in case {@link #isInsertSupported()} is false
+ */
+ public RowInsertionBuilder insertInto(Table table) throws IllegalArgumentException, IllegalStateException,
+ UnsupportedOperationException;
+
+ /**
+ * Initiates the building of a row insertion operation.
+ *
+ * @param tableName
+ * the name of the table to insert a row into
+ * @return a builder object on which values can be added and the statement
+ * can be committed.
+ * @throws IllegalArgumentException
+ * if the tableName argument is null or invalid.
+ * @throws IllegalStateException
+ * if the connection to the DataContext is read-only or another
+ * access restriction is preventing the operation.
+ * @throws UnsupportedOperationException
+ * in case {@link #isInsertSupported()} is false
+ */
+ public RowInsertionBuilder insertInto(String tableName) throws IllegalArgumentException, IllegalStateException,
+ UnsupportedOperationException;
+
+ /**
+ * Initiates the building of a row insertion operation.
+ *
+ * @param schemaName
+ * the name of the schema
+ * @param tableName
+ * the name of the table to insert a row into
+ * @return a builder object on which values can be added and the statement
+ * can be committed.
+ * @throws IllegalArgumentException
+ * if the tableName argument is null or invalid.
+ * @throws IllegalStateException
+ * if the connection to the DataContext is read-only or another
+ * access restriction is preventing the operation.
+ * @throws UnsupportedOperationException
+ * in case {@link #isInsertSupported()} is false
+ */
+ public RowInsertionBuilder insertInto(String schemaName, String tableName) throws IllegalArgumentException,
+ IllegalStateException, UnsupportedOperationException;
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/insert/RowInsertionBuilder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/insert/RowInsertionBuilder.java b/core/src/main/java/org/apache/metamodel/insert/RowInsertionBuilder.java
new file mode 100644
index 0000000..05714a3
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/insert/RowInsertionBuilder.java
@@ -0,0 +1,68 @@
+/**
+ * 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.insert;
+
+import org.eobjects.metamodel.DataContext;
+import org.eobjects.metamodel.MetaModelException;
+import org.eobjects.metamodel.data.Row;
+import org.eobjects.metamodel.data.RowBuilder;
+import org.eobjects.metamodel.schema.Table;
+
+/**
+ * Builder object for row insertion, into a {@link Table}.
+ *
+ * @author Kasper Sørensen
+ */
+public interface RowInsertionBuilder extends RowBuilder<RowInsertionBuilder> {
+
+ /**
+ * Gets the table that this insert pertains to.
+ *
+ * @return the table that this insert pertains to.
+ */
+ @Override
+ public Table getTable();
+
+ /**
+ * Sets all values like the provided row (for easy duplication of a row).
+ *
+ * @param row
+ * the row from which to take values
+ * @return the builder itself
+ */
+ public RowInsertionBuilder like(Row row);
+
+ /**
+ * Commits the row insertion operation. This operation will write the row to
+ * the {@link DataContext}.
+ *
+ * @throws MetaModelException
+ * if the operation was rejected
+ */
+ public void execute() throws MetaModelException;
+
+ /**
+ * Gets a SQL representation of this insert operation. Note that the
+ * generated SQL is dialect agnostic, so it is not accurately the same as
+ * what will be passed to a potential backing database.
+ *
+ * @return a SQL representation of this insert operation.
+ */
+ public String toSql();
+}
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/insert/package-info.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/insert/package-info.java b/core/src/main/java/org/apache/metamodel/insert/package-info.java
new file mode 100644
index 0000000..5a78f04
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/insert/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.
+ */
+/**
+ * API for inserting rows
+ */
+package org.eobjects.metamodel.insert;
+
http://git-wip-us.apache.org/repos/asf/incubator-metamodel/blob/e2e2b37a/core/src/main/java/org/apache/metamodel/intercept/DataSetInterceptor.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/metamodel/intercept/DataSetInterceptor.java b/core/src/main/java/org/apache/metamodel/intercept/DataSetInterceptor.java
new file mode 100644
index 0000000..3661374
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/intercept/DataSetInterceptor.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.data.DataSet;
+
+/**
+ * An {@link Interceptor} for {@link DataSet}s, allowing to touch, enrich or
+ * modify a dataset before it is returned to the user.
+ *
+ * @author Kasper Sørensen
+ */
+public interface DataSetInterceptor extends Interceptor<DataSet> {
+
+}