svn-gvsig-desktop / tags / v1_0_2_Build_899 / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / data / AbstractDataSource.java @ 10517
History | View | Annotate | Download (5.81 KB)
1 |
package com.hardcode.gdbms.engine.data; |
---|---|
2 |
|
3 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
4 |
import com.hardcode.gdbms.engine.data.indexes.FixedIndexSet; |
5 |
import com.hardcode.gdbms.engine.data.persistence.DataSourceLayerMemento; |
6 |
import com.hardcode.gdbms.engine.data.persistence.Memento; |
7 |
import com.hardcode.gdbms.engine.data.persistence.MementoException; |
8 |
import com.hardcode.gdbms.engine.values.Value; |
9 |
import com.hardcode.gdbms.engine.values.ValueCollection; |
10 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
11 |
|
12 |
import java.io.IOException; |
13 |
|
14 |
import java.util.ArrayList; |
15 |
import java.util.HashMap; |
16 |
import java.util.Iterator; |
17 |
import java.util.Vector; |
18 |
|
19 |
|
20 |
/**
|
21 |
* Implementaci?n del tipo de datos DataSource. Decorator sobre el driver que
|
22 |
* a?ade capacidades a la tabla como tener nombre, asociarse con un fichero
|
23 |
* concreto o soporte para el acceso por nombre a los campos
|
24 |
*
|
25 |
* @author Fernando Gonz?lez Cort?s
|
26 |
*/
|
27 |
public abstract class AbstractDataSource extends DataSourceCommonImpl |
28 |
implements DataSource { |
29 |
private FieldNameAccessSupport fnaSupport = new FieldNameAccessSupport(this); |
30 |
protected DataSourceFactory dsf;
|
31 |
protected ArrayList dataSourceListeners = new ArrayList(); |
32 |
|
33 |
/**
|
34 |
* Array de ?ndices de las tablas. Un indice por campo. Si el elemento
|
35 |
* i-?simo es null significa que el campo i-?simo no est? indexado
|
36 |
*/
|
37 |
private FixedIndexSet[] indexes; |
38 |
protected SourceInfo sourceInfo;
|
39 |
|
40 |
/**
|
41 |
* @see com.hardcode.gdbms.data.DataSource#getDataSourceFactory()
|
42 |
*/
|
43 |
public DataSourceFactory getDataSourceFactory() {
|
44 |
return dsf;
|
45 |
} |
46 |
|
47 |
/**
|
48 |
* @see com.hardcode.gdbms.data.DataSource#setDataSourceFactory(DataSourceFactory)
|
49 |
*/
|
50 |
public void setDataSourceFactory(DataSourceFactory dsf) { |
51 |
this.dsf = dsf;
|
52 |
} |
53 |
|
54 |
/**
|
55 |
* @see com.hardcode.gdbms.data.DataSource#close()
|
56 |
*/
|
57 |
public int getFieldIndexByName(String fieldName) throws DriverException { |
58 |
return fnaSupport.getFieldIndexByName(fieldName);
|
59 |
} |
60 |
|
61 |
/**
|
62 |
* DOCUMENT ME!
|
63 |
*
|
64 |
* @return DOCUMENT ME!
|
65 |
*
|
66 |
* @throws IOException DOCUMENT ME!
|
67 |
*/
|
68 |
public long[] getWhereFilter() throws IOException { |
69 |
return null; |
70 |
} |
71 |
|
72 |
/**
|
73 |
* @see com.hardcode.gdbms.engine.data.DataSource#setSourceInfo(com.hardcode.gdbms.engine.data.DataSourceFactory.DriverInfo)
|
74 |
*/
|
75 |
public void setSourceInfo(SourceInfo sourceInfo) { |
76 |
this.sourceInfo = sourceInfo;
|
77 |
} |
78 |
|
79 |
/**
|
80 |
* @see com.hardcode.gdbms.engine.data.DataSource#getSourceInfo()
|
81 |
*/
|
82 |
public SourceInfo getSourceInfo() {
|
83 |
return sourceInfo;
|
84 |
} |
85 |
|
86 |
/**
|
87 |
* @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
|
88 |
*/
|
89 |
public Memento getMemento() throws MementoException{ |
90 |
DataSourceLayerMemento m = new DataSourceLayerMemento(sourceInfo.name,
|
91 |
getName()); |
92 |
|
93 |
return m;
|
94 |
} |
95 |
|
96 |
/**
|
97 |
* @see com.hardcode.gdbms.engine.data.DataSource#remove()
|
98 |
*/
|
99 |
public void remove() throws DriverException { |
100 |
dsf.remove(this);
|
101 |
} |
102 |
|
103 |
/**
|
104 |
* Gets the hashmap with the key-value pairs from the string
|
105 |
*
|
106 |
* @param str a & separated 'key=value' list
|
107 |
*
|
108 |
* @return a hashmap
|
109 |
*/
|
110 |
private HashMap getMap(String str) { |
111 |
if (str == null) { |
112 |
return null; |
113 |
} |
114 |
|
115 |
HashMap m = new HashMap(); |
116 |
String[] pairs = str.split("&"); |
117 |
|
118 |
for (int i = 0; i < pairs.length; i++) { |
119 |
String[] keyValue = pairs[i].split("="); |
120 |
m.put(keyValue[0], keyValue[1]); |
121 |
} |
122 |
|
123 |
return m;
|
124 |
} |
125 |
|
126 |
/**
|
127 |
* Gets the name of the ith primary key field
|
128 |
*
|
129 |
* @param i index of the primary key field name to be retrieved
|
130 |
*
|
131 |
* @return String
|
132 |
*
|
133 |
* @throws DriverException If the operation fails
|
134 |
*/
|
135 |
private String getPKFieldName(int i) throws DriverException { |
136 |
int[] fieldsId = getPrimaryKeys(); |
137 |
|
138 |
return getFieldName(fieldsId[i]);
|
139 |
} |
140 |
|
141 |
/**
|
142 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKValue(long)
|
143 |
*/
|
144 |
public ValueCollection getPKValue(long rowIndex) throws DriverException { |
145 |
int[] fieldsId = getPrimaryKeys(); |
146 |
Value[] pks = new Value[fieldsId.length]; |
147 |
|
148 |
for (int i = 0; i < pks.length; i++) { |
149 |
pks[i] = getFieldValue(rowIndex, fieldsId[i]); |
150 |
} |
151 |
|
152 |
return ValueFactory.createValue(pks);
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKName(int)
|
157 |
*/
|
158 |
public String getPKName(int fieldId) throws DriverException { |
159 |
int[] fieldsId = getPrimaryKeys(); |
160 |
|
161 |
return getFieldName(fieldsId[fieldId]);
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKType(int)
|
166 |
*/
|
167 |
public int getPKType(int i) throws DriverException { |
168 |
int[] fieldsId = getPrimaryKeys(); |
169 |
|
170 |
return getFieldType(fieldsId[i]);
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKCardinality()
|
175 |
*/
|
176 |
public int getPKCardinality() throws DriverException { |
177 |
return 1; |
178 |
} |
179 |
|
180 |
/**
|
181 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPKNames()
|
182 |
*/
|
183 |
public String[] getPKNames() throws DriverException { |
184 |
String[] ret = new String[getPKCardinality()]; |
185 |
|
186 |
for (int i = 0; i < ret.length; i++) { |
187 |
ret[i] = getPKName(i); |
188 |
} |
189 |
|
190 |
return ret;
|
191 |
} |
192 |
|
193 |
public boolean isVirtualField(int fieldId) throws DriverException { |
194 |
return false; |
195 |
} |
196 |
|
197 |
public void addDataSourceListener(IDataSourceListener listener) { |
198 |
this.dataSourceListeners.add(listener);
|
199 |
} |
200 |
|
201 |
public void removeDataSourceListener(IDataSourceListener listener) { |
202 |
this.dataSourceListeners.remove(listener);
|
203 |
} |
204 |
|
205 |
public void raiseEventReloaded() { |
206 |
Iterator iter = this.dataSourceListeners.iterator(); |
207 |
while (iter.hasNext()) {
|
208 |
((IDataSourceListener)iter.next()).reloaded(this);
|
209 |
} |
210 |
} |
211 |
} |