Statistics
| Revision:

root / trunk / libraries / libGDBMS / src / main / java / com / hardcode / gdbms / engine / strategies / ProjectionDataSource.java @ 10627

History | View | Annotate | Download (3.71 KB)

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

    
3
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
4
import com.hardcode.gdbms.engine.data.DataSource;
5
import com.hardcode.gdbms.engine.data.persistence.Memento;
6
import com.hardcode.gdbms.engine.data.persistence.MementoException;
7
import com.hardcode.gdbms.engine.data.persistence.OperationLayerMemento;
8
import com.hardcode.gdbms.engine.instruction.EvaluationException;
9
import com.hardcode.gdbms.engine.instruction.Expression;
10
import com.hardcode.gdbms.engine.values.Value;
11

    
12

    
13
/**
14
 * DataSource que a?ade caracter?sticas de proyecci?n sobre campos al
15
 * DataSource subyacente.
16
 *
17
 * @author Fernando Gonz?lez Cort?s
18
 */
19
public class ProjectionDataSource extends OperationDataSource {
20
        private DataSource source;
21
        private Expression[] fields;
22
        private String[] aliases;
23

    
24
        /**
25
         * Creates a new ProjectionDataSource object.
26
         *
27
         * @param source DataSource origen de la informaci?n
28
         * @param fields Con los ?ndices de los campos proyectados
29
         * @param aliases Nombres asignados en la instrucci?n a los campos
30
         */
31
        public ProjectionDataSource(DataSource source, Expression[] fields,
32
                String[] aliases) {
33
                this.source = source;
34
                this.fields = fields;
35
                this.aliases = aliases;
36
        }
37

    
38
        /**
39
         * Dado el ?ndice de un campo en la tabla proyecci?n, se devuelve el ?ndice
40
         * real en el DataSource subyacente
41
         *
42
         * @param index ?ndice del campo cuyo ?ndice en el DataSource subyacente se
43
         *                   quiere obtener
44
         *
45
         * @return ?ndice del campo en el DataSource subyacente
46
         */
47
        private Expression getFieldByIndex(int index) {
48
                return fields[index];
49
        }
50

    
51
        /**
52
         * @see com.hardcode.gdbms.data.DataSource#
53
         */
54
        public void stop() throws ReadDriverException {
55
                source.stop();
56
        }
57

    
58
        /**
59
         * @see com.hardcode.gdbms.data.DataSource#
60
         */
61
        public int getFieldCount() throws ReadDriverException {
62
                return fields.length;
63
        }
64

    
65
        /**
66
         * @see com.hardcode.gdbms.data.DataSource#
67
         */
68
        public int getFieldIndexByName(String fieldName) throws ReadDriverException {
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
                return -1;
80
        }
81

    
82
        /**
83
         * @see com.hardcode.gdbms.data.DataSource#
84
         */
85
        public String getFieldName(int fieldId) throws ReadDriverException {
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 ReadDriverException {
104
                try {
105
                        return getFieldByIndex(fieldId).evaluate(rowIndex);
106
                } catch (EvaluationException e) {
107
                        throw new ReadDriverException(getName(),e);
108
                }
109
        }
110

    
111
        /**
112
         * @see com.hardcode.gdbms.data.DataSource#
113
         */
114
        public long getRowCount() throws ReadDriverException {
115
                return source.getRowCount();
116
        }
117

    
118
        /**
119
         * @see com.hardcode.gdbms.data.DataSource#
120
         */
121
        public void start() throws ReadDriverException {
122
                source.start();
123
        }
124

    
125
        /**
126
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
127
         */
128
        public int getFieldType(int i) throws ReadDriverException {
129
                throw new UnsupportedOperationException(
130
                        "cannot get the field type of an expression");
131
        }
132

    
133
        /**
134
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
135
         */
136
        public Memento getMemento() throws MementoException {
137
                return new OperationLayerMemento(getName(),
138
                        new Memento[] { source.getMemento() }, getSQL());
139
        }
140

    
141
        public int getFieldWidth(int i) throws ReadDriverException {
142
                throw new UnsupportedOperationException(
143
                "cannot get the field width of an expression");
144
        }
145
}