svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / engine / ProjectionDataSource.java @ 466
History | View | Annotate | Download (3.8 KB)
1 |
package com.hardcode.gdbms.engine; |
---|---|
2 |
|
3 |
import com.hardcode.gdbms.engine.data.DataSource; |
4 |
import com.hardcode.gdbms.engine.data.DriverException; |
5 |
import com.hardcode.gdbms.engine.data.ReadDriver; |
6 |
import com.hardcode.gdbms.engine.instruction.Expression; |
7 |
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException; |
8 |
import com.hardcode.gdbms.engine.instruction.SemanticException; |
9 |
import com.hardcode.gdbms.engine.values.Value; |
10 |
|
11 |
|
12 |
/**
|
13 |
* DataSource que a?ade caracter?sticas de proyecci?n sobre campos al
|
14 |
* DataSource subyacente.
|
15 |
*
|
16 |
* @author Fernando Gonz?lez Cort?s
|
17 |
*/
|
18 |
public class ProjectionDataSource implements DataSource { |
19 |
private DataSource source; |
20 |
private Expression[] fields; |
21 |
private String[] aliases; |
22 |
|
23 |
/**
|
24 |
* Creates a new ProjectionDataSource object.
|
25 |
*
|
26 |
* @param source DataSource origen de la informaci?n
|
27 |
* @param fields Con los ?ndices de los campos proyectados
|
28 |
* @param aliases Nombres asignados en la instrucci?n a los campos
|
29 |
*/
|
30 |
public ProjectionDataSource(DataSource source, Expression[] fields, |
31 |
String[] aliases) { |
32 |
this.source = source;
|
33 |
this.fields = fields;
|
34 |
this.aliases = aliases;
|
35 |
} |
36 |
|
37 |
/**
|
38 |
* Dado el ?ndice de un campo en la tabla proyecci?n, se devuelve el ?ndice
|
39 |
* real en el DataSource subyacente
|
40 |
*
|
41 |
* @param index ?ndice del campo cuyo ?ndice en el DataSource subyacente se
|
42 |
* quiere obtener
|
43 |
*
|
44 |
* @return ?ndice del campo en el DataSource subyacente
|
45 |
*/
|
46 |
private Expression getFieldByIndex(int index) { |
47 |
return fields[index];
|
48 |
} |
49 |
|
50 |
/**
|
51 |
* @see com.hardcode.gdbms.data.DataSource#
|
52 |
*/
|
53 |
public void stop() throws DriverException { |
54 |
source.stop(); |
55 |
} |
56 |
|
57 |
/**
|
58 |
* @see com.hardcode.gdbms.data.DataSource#
|
59 |
*/
|
60 |
public int getFieldCount() throws DriverException { |
61 |
return fields.length;
|
62 |
} |
63 |
|
64 |
/**
|
65 |
* @see com.hardcode.gdbms.data.DataSource#
|
66 |
*/
|
67 |
public int getFieldIndexByName(String fieldName) |
68 |
throws DriverException, FieldNotFoundException {
|
69 |
/*
|
70 |
* Se comprueba si dicho ?ndice est? mapeado o la ProjectionDataSource
|
71 |
* no lo tiene
|
72 |
*/
|
73 |
for (int i = 0; i < fields.length; i++) { |
74 |
if (fieldName.compareTo(fields[i].getFieldName()) == 0) { |
75 |
return i;
|
76 |
} |
77 |
} |
78 |
|
79 |
throw new FieldNotFoundException(); |
80 |
} |
81 |
|
82 |
/**
|
83 |
* @see com.hardcode.gdbms.data.DataSource#
|
84 |
*/
|
85 |
public String getFieldName(int fieldId) throws DriverException { |
86 |
if (aliases[fieldId] != null) { |
87 |
return aliases[fieldId];
|
88 |
} else {
|
89 |
String name = fields[fieldId].getFieldName();
|
90 |
|
91 |
if (name == null) { |
92 |
return "unknown" + fieldId; |
93 |
} else {
|
94 |
return name;
|
95 |
} |
96 |
} |
97 |
} |
98 |
|
99 |
/**
|
100 |
* @see com.hardcode.gdbms.data.DataSource#
|
101 |
*/
|
102 |
public Value getFieldValue(long rowIndex, int fieldId) |
103 |
throws DriverException {
|
104 |
try {
|
105 |
return getFieldByIndex(fieldId).evaluate(rowIndex);
|
106 |
} catch (SemanticException e) {
|
107 |
throw new DriverException(e); |
108 |
} |
109 |
} |
110 |
|
111 |
/**
|
112 |
* @see com.hardcode.gdbms.data.DataSource#
|
113 |
*/
|
114 |
public long getRowCount() throws DriverException { |
115 |
return source.getRowCount();
|
116 |
} |
117 |
|
118 |
/**
|
119 |
* @see com.hardcode.gdbms.data.DataSource#
|
120 |
*/
|
121 |
public void start() throws DriverException { |
122 |
source.start(); |
123 |
} |
124 |
|
125 |
/**
|
126 |
* @see com.hardcode.gdbms.engine.data.DataSource#getName()
|
127 |
*/
|
128 |
public String getName() { |
129 |
return ""; |
130 |
} |
131 |
|
132 |
/**
|
133 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDBMS()
|
134 |
*/
|
135 |
public String getDBMS() { |
136 |
return source.getDBMS();
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
|
141 |
*/
|
142 |
public ReadDriver getDriver() {
|
143 |
return null; |
144 |
} |
145 |
} |