METAMODEL-1165: Fixed - added default_table alias table
[metamodel.git] / pojo / src / main / java / org / apache / metamodel / pojo / PojoDataContext.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.metamodel.pojo;
20
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.TreeMap;
27 import java.util.stream.Collectors;
28
29 import org.apache.metamodel.DataContext;
30 import org.apache.metamodel.MetaModelException;
31 import org.apache.metamodel.QueryPostprocessDataContext;
32 import org.apache.metamodel.UpdateScript;
33 import org.apache.metamodel.UpdateSummary;
34 import org.apache.metamodel.UpdateableDataContext;
35 import org.apache.metamodel.data.DataSet;
36 import org.apache.metamodel.data.MaxRowsDataSet;
37 import org.apache.metamodel.query.SelectItem;
38 import org.apache.metamodel.schema.Column;
39 import org.apache.metamodel.schema.MutableSchema;
40 import org.apache.metamodel.schema.MutableTable;
41 import org.apache.metamodel.schema.Schema;
42 import org.apache.metamodel.schema.Table;
43 import org.apache.metamodel.util.SimpleTableDef;
44
45 /**
46 * A {@link DataContext} used to serve MetaModel support for collections of Java
47 * objects and key/value maps.
48 */
49 public class PojoDataContext extends QueryPostprocessDataContext implements UpdateableDataContext, Serializable {
50
51 private static final long serialVersionUID = 1L;
52
53 public static final String DEFAULT_SCHEMA_NAME = "Schema";
54
55 private final Map<String, TableDataProvider<?>> _tables;
56 private final String _schemaName;
57
58 /**
59 * Creates a new POJO data context that is empty but can be populated at
60 * will.
61 */
62 public PojoDataContext() {
63 this(new ArrayList<TableDataProvider<?>>());
64 }
65
66 /**
67 * Creates a new POJO data context based on the provided
68 * {@link TableDataProvider}s.
69 *
70 * @param tables
71 */
72 public PojoDataContext(List<TableDataProvider<?>> tables) {
73 this(DEFAULT_SCHEMA_NAME, tables);
74 }
75
76 /**
77 * Creates a new POJO data context based on the provided
78 * {@link TableDataProvider}s.
79 *
80 * @param schemaName
81 * the name of the created schema
82 * @param tableProviders
83 */
84 public PojoDataContext(String schemaName, @SuppressWarnings("rawtypes") TableDataProvider... tableProviders) {
85 this(schemaName, Arrays.<TableDataProvider<?>> asList(tableProviders));
86 }
87
88 /**
89 * Creates a new POJO data context based on the provided
90 * {@link TableDataProvider}s.
91 *
92 * @param schemaName
93 * the name of the created schema
94 * @param tables
95 */
96 public PojoDataContext(String schemaName, List<TableDataProvider<?>> tables) {
97 super(true);
98 if (schemaName == null) {
99 throw new IllegalArgumentException("Schema name cannot be null");
100 }
101 _schemaName = schemaName;
102 _tables = new TreeMap<String, TableDataProvider<?>>();
103 for (TableDataProvider<?> pojoTable : tables) {
104 addTableDataProvider(pojoTable);
105 }
106 }
107
108 @Override
109 protected DataSet materializeMainSchemaTable(Table table, List<Column> columns, int maxRows) {
110 final TableDataProvider<?> pojoTable = _tables.get(table.getName());
111 if (pojoTable == null) {
112 throw new IllegalArgumentException("No such POJO table: " + table.getName());
113 }
114
115 final List<SelectItem> selectItems = columns.stream().map(SelectItem::new).collect(Collectors.toList());
116
117 @SuppressWarnings({ "rawtypes", "unchecked" })
118 DataSet dataSet = new PojoDataSet(pojoTable, selectItems);
119
120 if (maxRows > 0) {
121 dataSet = new MaxRowsDataSet(dataSet, maxRows);
122 }
123
124 return dataSet;
125 }
126
127 @Override
128 protected Schema getMainSchema() throws MetaModelException {
129 final MutableSchema schema = new MutableSchema(getMainSchemaName());
130
131 for (TableDataProvider<?> pojoTable : _tables.values()) {
132 final SimpleTableDef tableDef = pojoTable.getTableDef();
133 final MutableTable table = tableDef.toTable();
134 table.setSchema(schema);
135 schema.addTable(table);
136 }
137
138 return schema;
139 }
140
141 @Override
142 protected String getMainSchemaName() throws MetaModelException {
143 return _schemaName;
144 }
145
146 @Override
147 public UpdateSummary executeUpdate(UpdateScript update) {
148 final PojoUpdateCallback updateCallback = new PojoUpdateCallback(this);
149 synchronized (this) {
150 update.run(updateCallback);
151 }
152 return updateCallback.getUpdateSummary();
153 }
154
155 protected void addTableDataProvider(TableDataProvider<?> tableDataProvider) {
156 _tables.put(tableDataProvider.getName(), tableDataProvider);
157 }
158
159 public void insert(String tableName, Map<String, Object> recordData) {
160 TableDataProvider<?> table = _tables.get(tableName);
161 if (table == null) {
162 throw new IllegalArgumentException("No table data provider for table: " + tableName);
163 }
164 table.insert(recordData);
165 }
166
167 }