Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / AbstractDataSource.java @ 10627

History | View | Annotate | Download (5.85 KB)

1
package com.hardcode.gdbms.engine.data;
2

    
3
import java.io.IOException;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.Iterator;
7

    
8
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
9
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
10
import com.hardcode.gdbms.engine.data.indexes.FixedIndexSet;
11
import com.hardcode.gdbms.engine.data.persistence.DataSourceLayerMemento;
12
import com.hardcode.gdbms.engine.data.persistence.Memento;
13
import com.hardcode.gdbms.engine.data.persistence.MementoException;
14
import com.hardcode.gdbms.engine.values.Value;
15
import com.hardcode.gdbms.engine.values.ValueCollection;
16
import com.hardcode.gdbms.engine.values.ValueFactory;
17

    
18

    
19
/**
20
 * Implementaci?n del tipo de datos DataSource. Decorator sobre el driver que
21
 * a?ade capacidades a la tabla como tener nombre, asociarse con un fichero
22
 * concreto o soporte para el acceso por nombre a los campos
23
 *
24
 * @author Fernando Gonz?lez Cort?s
25
 */
26
public abstract class AbstractDataSource extends DataSourceCommonImpl
27
    implements DataSource {
28
    private FieldNameAccessSupport fnaSupport = new FieldNameAccessSupport(this);
29
    protected DataSourceFactory dsf;
30
    protected ArrayList dataSourceListeners = new ArrayList();
31

    
32
    /**
33
     * Array de ?ndices de las tablas. Un indice por campo. Si el elemento
34
     * i-?simo es null significa que el campo i-?simo no est? indexado
35
     */
36
    private FixedIndexSet[] indexes;
37
    protected SourceInfo sourceInfo;
38

    
39
    /**
40
     * @see com.hardcode.gdbms.data.DataSource#getDataSourceFactory()
41
     */
42
    public DataSourceFactory getDataSourceFactory() {
43
        return dsf;
44
    }
45

    
46
    /**
47
     * @see com.hardcode.gdbms.data.DataSource#setDataSourceFactory(DataSourceFactory)
48
     */
49
    public void setDataSourceFactory(DataSourceFactory dsf) {
50
        this.dsf = dsf;
51
    }
52

    
53
    /**
54
     * @see com.hardcode.gdbms.data.DataSource#close()
55
     */
56
    public int getFieldIndexByName(String fieldName) throws ReadDriverException {
57
        return fnaSupport.getFieldIndexByName(fieldName);
58
    }
59

    
60
    /**
61
     * DOCUMENT ME!
62
     *
63
     * @return DOCUMENT ME!
64
     *
65
     * @throws IOException DOCUMENT ME!
66
     */
67
    public long[] getWhereFilter() throws IOException {
68
        return null;
69
    }
70

    
71
    /**
72
     * @see com.hardcode.gdbms.engine.data.DataSource#setSourceInfo(com.hardcode.gdbms.engine.data.DataSourceFactory.DriverInfo)
73
     */
74
    public void setSourceInfo(SourceInfo sourceInfo) {
75
        this.sourceInfo = sourceInfo;
76
    }
77

    
78
    /**
79
     * @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
80
     */
81
    public SourceInfo getSourceInfo() {
82
        return sourceInfo;
83
    }
84

    
85
    /**
86
     * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
87
     */
88
    public Memento getMemento() throws MementoException{
89
        DataSourceLayerMemento m = new DataSourceLayerMemento(sourceInfo.name,
90
                getName());
91

    
92
        return m;
93
    }
94

    
95
    /**
96
     * @see com.hardcode.gdbms.engine.data.DataSource#remove()
97
     */
98
    public void remove() throws WriteDriverException {
99
        dsf.remove(this);
100
    }
101

    
102
    /**
103
     * Gets the hashmap with the key-value pairs from the string
104
     *
105
     * @param str a & separated 'key=value' list
106
     *
107
     * @return a hashmap
108
     */
109
    private HashMap getMap(String str) {
110
        if (str == null) {
111
            return null;
112
        }
113

    
114
        HashMap m = new HashMap();
115
        String[] pairs = str.split("&");
116

    
117
        for (int i = 0; i < pairs.length; i++) {
118
            String[] keyValue = pairs[i].split("=");
119
            m.put(keyValue[0], keyValue[1]);
120
        }
121

    
122
        return m;
123
    }
124

    
125
    /**
126
     * Gets the name of the ith primary key field
127
     *
128
     * @param i index of the primary key field name to be retrieved
129
     *
130
     * @return String
131
     * @throws ReadDriverException TODO
132
     */
133
    private String getPKFieldName(int i) throws ReadDriverException {
134
        int[] fieldsId = getPrimaryKeys();
135

    
136
        return getFieldName(fieldsId[i]);
137
    }
138

    
139
    /**
140
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
141
     */
142
    public ValueCollection getPKValue(long rowIndex) throws ReadDriverException {
143
        int[] fieldsId = getPrimaryKeys();
144
        Value[] pks = new Value[fieldsId.length];
145

    
146
        for (int i = 0; i < pks.length; i++) {
147
            pks[i] = getFieldValue(rowIndex, fieldsId[i]);
148
        }
149

    
150
        return ValueFactory.createValue(pks);
151
    }
152

    
153
    /**
154
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
155
     */
156
    public String getPKName(int fieldId) throws ReadDriverException {
157
        int[] fieldsId = getPrimaryKeys();
158

    
159
        return getFieldName(fieldsId[fieldId]);
160
    }
161

    
162
    /**
163
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
164
     */
165
    public int getPKType(int i) throws ReadDriverException {
166
        int[] fieldsId = getPrimaryKeys();
167

    
168
        return getFieldType(fieldsId[i]);
169
    }
170

    
171
    /**
172
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
173
     */
174
    public int getPKCardinality() throws ReadDriverException {
175
        return 1;
176
    }
177

    
178
    /**
179
     * @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
180
     */
181
    public String[] getPKNames() throws ReadDriverException {
182
        String[] ret = new String[getPKCardinality()];
183

    
184
        for (int i = 0; i < ret.length; i++) {
185
            ret[i] = getPKName(i);
186
        }
187

    
188
        return ret;
189
    }
190

    
191
    public boolean isVirtualField(int fieldId) throws ReadDriverException  {
192
            return false;
193
    }
194

    
195
    public void addDataSourceListener(IDataSourceListener listener) {
196
            this.dataSourceListeners.add(listener);
197
    }
198

    
199
    public void removeDataSourceListener(IDataSourceListener listener) {
200
            this.dataSourceListeners.remove(listener);
201
    }
202

    
203
    public void raiseEventReloaded() {
204
            Iterator iter = this.dataSourceListeners.iterator();
205
            while (iter.hasNext()) {
206
                    ((IDataSourceListener)iter.next()).reloaded(this);
207
            }
208
    }
209
}