Statistics
| Revision:

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 @ 44925

History | View | Annotate | Download (5.37 KB)

1
/**
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
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
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
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
import org.gvsig.tools.util.UnmodifiableBasicSet;
46
import org.gvsig.tools.util.UnmodifiableBasicSetAdapter;
47

    
48
/**
49
 *
50
 * @author jjdelcerro
51
 */
52
public class CreateInMemoryTableFunction extends AbstractFunction {
53

    
54
  public CreateInMemoryTableFunction() {
55
    super(  DALFunctions.GROUP_DATA_ACCESS,
56
            FUNCTION_CREATE_IN_MEMORY_TABLE,
57
            Range.is(2),
58
            "Create a table in memory with the same structure as the indicated table. Return a reference to the new table.",
59
            "CREATE IN MEMORY TABLE {{new_table_name}} FROM source_table_name",
60
            new String[]{
61
              "new_table_name - Name of the new table to create.",
62
              "source_table_name - Name of the table to copy their structure."
63
            },
64
            "Table",
65
            false
66
    );
67
  }
68

    
69
  @Override
70
  public boolean allowConstantFolding() {
71
    return false;
72
  }
73

    
74
  @Override
75
  public Object call(Interpreter interpreter, Object[] args) throws Exception {
76
    DataManager dataManager = DALLocator.getDataManager();  
77
    StoresRepository storesRepository = dataManager.getStoresRepository();
78
    MemoryStoresRepository repo = (MemoryStoresRepository) storesRepository.getSubrepository("Temporary");
79
    if( repo == null ) {
80
      repo = new MemoryStoresRepository("Temporary");
81
      storesRepository.addRepository(repo);
82
    }
83
    String targetName = getStr(args,0);
84
    String sourceName = getStr(args,1);
85
    FeatureStore source = (FeatureStore) storesRepository.getStore(sourceName);
86
    if( source == null ) {
87
      throw new ExpressionRuntimeException("Can't locate source table '"+sourceName+"'.");    
88
    }
89
    EditableFeatureType featureType = source.getDefaultFeatureType().getEditable();
90
    
91
    FeatureStore target = dataManager.createMemoryStore(null);
92
    target.edit();
93
    target.update(featureType);
94
    target.finishEditing();
95
    repo.add(targetName, target);
96
    return target;
97
  }
98
  
99
  private class MemoryStoresRepository extends AbstractStoresRepository {
100
    private final Map<String,FeatureStore> repository;
101

    
102
    public MemoryStoresRepository(String name) {
103
      super(name);
104
      this.repository = new HashMap<>();
105
    }
106

    
107
    public void add(String name, FeatureStore store) {
108
      this.repository.put(name, store);
109
    }
110
    
111
    @Override
112
    protected DataStoreParameters getMyParameters(String name) {
113
      FeatureStore store = this.repository.get(name);
114
      return store.getParameters();
115
    }
116

    
117
    @Override
118
    protected boolean isEmptyMyRepository() {
119
      return this.repository.isEmpty();
120
    }
121

    
122
    @Override
123
    protected int getMySize() {
124
      return this.repository.size();
125
    }
126

    
127
    @Override
128
    protected UnmodifiableBasicSet<String> getMyKeySet() {
129
        Set<String> keyset = this.repository.keySet();
130
        if( keyset == null || keyset.isEmpty() ) {
131
            return UnmodifiableBasicSet.EMPTY_UNMODIFIABLEBASICSET;
132
        }
133
        return new UnmodifiableBasicSetAdapter<>(keyset);
134
    }
135

    
136
    @Override
137
    public DataStore getStore(String name) {
138
      FeatureStore store = this.repository.get(name);
139
      return store;
140
    }
141

    
142
    @Override
143
    public void add(String name, DataStoreParameters parameters) {
144

    
145
    }
146

    
147
    @Override
148
    public boolean contains(DataStoreParameters parameters) {
149
      return false;
150
    }
151

    
152
    @Override
153
    public boolean containsKey(String key) {
154
      return this.repository.containsKey(key);
155
    }
156

    
157
    @Override
158
    public Iterator<DataStoreParameters> iterator() {
159
      return Collections.EMPTY_LIST.iterator();
160
    }
161

    
162
    @Override
163
    public void remove(String name) {
164
      this.repository.remove(name);
165
    }
166

    
167
  }
168
}