svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / dataaccess / CreateInMemoryTableFunction.java @ 47787
History | View | Annotate | Download (5.45 KB)
1 | 44738 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2020 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | package org.gvsig.expressionevaluator.impl.function.dataaccess; |
||
25 | |||
26 | 44858 | jjdelcerro | import java.util.Collections; |
27 | import java.util.HashMap; |
||
28 | import java.util.Iterator; |
||
29 | import java.util.Map; |
||
30 | import java.util.Set; |
||
31 | 44738 | jjdelcerro | import org.apache.commons.lang3.Range; |
32 | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
||
33 | import org.gvsig.expressionevaluator.Interpreter; |
||
34 | import org.gvsig.expressionevaluator.impl.DALFunctions; |
||
35 | 44858 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
36 | import org.gvsig.fmap.dal.AbstractStoresRepository; |
||
37 | import org.gvsig.fmap.dal.DALLocator; |
||
38 | import org.gvsig.fmap.dal.DataManager; |
||
39 | import static org.gvsig.fmap.dal.DataManager.FUNCTION_CREATE_IN_MEMORY_TABLE; |
||
40 | import org.gvsig.fmap.dal.DataStore; |
||
41 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
42 | import org.gvsig.fmap.dal.StoresRepository; |
||
43 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
44 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
45 | 45100 | jjdelcerro | import org.gvsig.tools.dispose.DisposeUtils; |
46 | 44858 | jjdelcerro | import org.gvsig.tools.util.UnmodifiableBasicSet; |
47 | import org.gvsig.tools.util.UnmodifiableBasicSetAdapter; |
||
48 | 44738 | jjdelcerro | |
49 | /**
|
||
50 | *
|
||
51 | * @author jjdelcerro
|
||
52 | */
|
||
53 | 44858 | jjdelcerro | public class CreateInMemoryTableFunction extends AbstractFunction { |
54 | 44738 | jjdelcerro | |
55 | 44858 | jjdelcerro | public CreateInMemoryTableFunction() {
|
56 | 44896 | jjdelcerro | super( DALFunctions.GROUP_DATA_ACCESS,
|
57 | 44858 | jjdelcerro | FUNCTION_CREATE_IN_MEMORY_TABLE, |
58 | Range.is(2),
|
||
59 | 44925 | jjdelcerro | "Create a table in memory with the same structure as the indicated table. Return a reference to the new table.",
|
60 | "CREATE IN MEMORY TABLE {{new_table_name}} FROM source_table_name",
|
||
61 | new String[]{ |
||
62 | "new_table_name - Name of the new table to create.",
|
||
63 | "source_table_name - Name of the table to copy their structure."
|
||
64 | }, |
||
65 | 44858 | jjdelcerro | "Table",
|
66 | false
|
||
67 | 44738 | jjdelcerro | ); |
68 | } |
||
69 | |||
70 | @Override
|
||
71 | public boolean allowConstantFolding() { |
||
72 | return false; |
||
73 | } |
||
74 | |||
75 | @Override
|
||
76 | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
||
77 | 44858 | jjdelcerro | DataManager dataManager = DALLocator.getDataManager(); |
78 | StoresRepository storesRepository = dataManager.getStoresRepository(); |
||
79 | MemoryStoresRepository repo = (MemoryStoresRepository) storesRepository.getSubrepository("Temporary");
|
||
80 | if( repo == null ) { |
||
81 | repo = new MemoryStoresRepository("Temporary"); |
||
82 | storesRepository.addRepository(repo); |
||
83 | } |
||
84 | String targetName = getStr(args,0); |
||
85 | String sourceName = getStr(args,1); |
||
86 | FeatureStore source = (FeatureStore) storesRepository.getStore(sourceName); |
||
87 | if( source == null ) { |
||
88 | throw new ExpressionRuntimeException("Can't locate source table '"+sourceName+"'."); |
||
89 | } |
||
90 | EditableFeatureType featureType = source.getDefaultFeatureType().getEditable(); |
||
91 | |||
92 | FeatureStore target = dataManager.createMemoryStore(null);
|
||
93 | target.edit(); |
||
94 | target.update(featureType); |
||
95 | target.finishEditing(); |
||
96 | repo.add(targetName, target); |
||
97 | return target;
|
||
98 | 44738 | jjdelcerro | } |
99 | 44858 | jjdelcerro | |
100 | private class MemoryStoresRepository extends AbstractStoresRepository { |
||
101 | private final Map<String,FeatureStore> repository; |
||
102 | 44738 | jjdelcerro | |
103 | 44858 | jjdelcerro | public MemoryStoresRepository(String name) { |
104 | super(name);
|
||
105 | this.repository = new HashMap<>(); |
||
106 | } |
||
107 | |||
108 | public void add(String name, FeatureStore store) { |
||
109 | this.repository.put(name, store);
|
||
110 | } |
||
111 | |||
112 | @Override
|
||
113 | protected DataStoreParameters getMyParameters(String name) { |
||
114 | FeatureStore store = this.repository.get(name);
|
||
115 | return store.getParameters();
|
||
116 | } |
||
117 | |||
118 | @Override
|
||
119 | protected boolean isEmptyMyRepository() { |
||
120 | return this.repository.isEmpty(); |
||
121 | } |
||
122 | |||
123 | @Override
|
||
124 | protected int getMySize() { |
||
125 | return this.repository.size(); |
||
126 | } |
||
127 | |||
128 | @Override
|
||
129 | protected UnmodifiableBasicSet<String> getMyKeySet() { |
||
130 | Set<String> keyset = this.repository.keySet(); |
||
131 | if( keyset == null || keyset.isEmpty() ) { |
||
132 | return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
|
||
133 | 44738 | jjdelcerro | } |
134 | 44858 | jjdelcerro | return new UnmodifiableBasicSetAdapter<>(keyset); |
135 | 44738 | jjdelcerro | } |
136 | |||
137 | 44858 | jjdelcerro | @Override
|
138 | public DataStore getStore(String name) { |
||
139 | FeatureStore store = this.repository.get(name);
|
||
140 | 45100 | jjdelcerro | DisposeUtils.bind(store); |
141 | 44858 | jjdelcerro | return store;
|
142 | } |
||
143 | |||
144 | @Override
|
||
145 | public void add(String name, DataStoreParameters parameters) { |
||
146 | |||
147 | } |
||
148 | |||
149 | @Override
|
||
150 | public boolean contains(DataStoreParameters parameters) { |
||
151 | 44738 | jjdelcerro | return false; |
152 | } |
||
153 | 44858 | jjdelcerro | |
154 | @Override
|
||
155 | public boolean containsKey(String key) { |
||
156 | return this.repository.containsKey(key); |
||
157 | 44738 | jjdelcerro | } |
158 | 44858 | jjdelcerro | |
159 | @Override
|
||
160 | public Iterator<DataStoreParameters> iterator() { |
||
161 | return Collections.EMPTY_LIST.iterator(); |
||
162 | } |
||
163 | |||
164 | @Override
|
||
165 | public void remove(String name) { |
||
166 | this.repository.remove(name);
|
||
167 | } |
||
168 | |||
169 | 44738 | jjdelcerro | } |
170 | } |