Statistics
| Revision:

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

History | View | Annotate | Download (3.02 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.values.Value;
9

    
10

    
11
/**
12
 * DataSource que hace la union de dos datasources
13
 *
14
 * @author Fernando Gonz?lez Cort?s
15
 */
16
public class UnionDataSource extends OperationDataSource {
17
        private DataSource dataSource1;
18
        private DataSource dataSource2;
19

    
20
        /**
21
         * Creates a new UnionDataSource object.
22
         *
23
         * @param ds1 Primera tabla de la union
24
         * @param ds2 Segunda tabla de la union
25
         */
26
        public UnionDataSource(DataSource ds1, DataSource ds2) {
27
                dataSource1 = ds1;
28
                dataSource2 = ds2;
29
        }
30

    
31
        /**
32
         * @see com.hardcode.gdbms.engine.data.DataSource#open()
33
         */
34
        public void start() throws ReadDriverException {
35
                dataSource1.start();
36

    
37
                try {
38
                        dataSource2.start();
39
                } catch (ReadDriverException e) {
40
                        dataSource1.stop();
41

    
42
                        throw e;
43
                }
44
        }
45

    
46
        /**
47
         * @see com.hardcode.gdbms.engine.data.DataSource#close()
48
         */
49
        public void stop() throws ReadDriverException {
50
                dataSource1.stop();
51
                dataSource2.stop();
52
        }
53

    
54
        /**
55
         * @see com.hardcode.gdbms.engine.data.FieldNameAccess#getFieldIndexByName(java.lang.String)
56
         */
57
        public int getFieldIndexByName(String fieldName) throws ReadDriverException {
58
                return dataSource1.getFieldIndexByName(fieldName);
59
        }
60

    
61
        /**
62
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
63
         *                 int)
64
         */
65
        public Value getFieldValue(long rowIndex, int fieldId)
66
                throws ReadDriverException {
67
                long tamTabla1 = dataSource1.getRowCount();
68

    
69
                if (rowIndex < tamTabla1) {
70
                        return dataSource1.getFieldValue(rowIndex, fieldId);
71
                } else {
72
                        return dataSource2.getFieldValue(rowIndex - tamTabla1, fieldId);
73
                }
74
        }
75

    
76
        /**
77
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
78
         */
79
        public int getFieldCount() throws ReadDriverException {
80
                return dataSource1.getFieldCount();
81
        }
82

    
83
        /**
84
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
85
         */
86
        public String getFieldName(int fieldId) throws ReadDriverException {
87
                return dataSource1.getFieldName(fieldId);
88
        }
89

    
90
        /**
91
         * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
92
         */
93
        public long getRowCount() throws ReadDriverException {
94
                return dataSource1.getRowCount() + dataSource2.getRowCount();
95
        }
96

    
97
        /**
98
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
99
         */
100
        public int getFieldType(int i) throws ReadDriverException {
101
                return dataSource1.getFieldType(i);
102
        }
103

    
104
        /**
105
         * @see com.hardcode.gdbms.engine.data.DataSource#getMemento()
106
         */
107
        public Memento getMemento() throws MementoException {
108
                return new OperationLayerMemento(getName(),
109
                        new Memento[] { dataSource1.getMemento(), dataSource2.getMemento() }, getSQL());
110
        }
111

    
112
        public int getFieldWidth(int i) throws ReadDriverException {
113
                return dataSource1.getFieldWidth(i);
114
        }
115
}