Statistics
| Revision:

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

History | View | Annotate | Download (5.32 KB)

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

    
3
import java.io.File;
4
import java.sql.Types;
5

    
6
import com.hardcode.driverManager.Driver;
7
import com.hardcode.driverManager.DriverLoadException;
8
import com.hardcode.gdbms.driver.exceptions.CloseDriverException;
9
import com.hardcode.gdbms.driver.exceptions.OpenDriverException;
10
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
11
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
12
import com.hardcode.gdbms.engine.data.SourceInfo;
13
import com.hardcode.gdbms.engine.data.driver.FileDriver;
14
import com.hardcode.gdbms.engine.data.driver.GDBMSDriver;
15
import com.hardcode.gdbms.engine.data.driver.ReadAccess;
16
import com.hardcode.gdbms.engine.data.edition.DataWare;
17
import com.hardcode.gdbms.engine.values.Value;
18
import com.hardcode.gdbms.engine.values.ValueFactory;
19

    
20
/**
21
 * Adapta la interfaz FileDriver a la interfaz DataSource
22
 *
23
 * @author Fernando Gonz?lez Cort?s
24
 */
25
class FileDataSourceAdapter extends AbstractFileDataSource implements
26
                FileDataSource {
27
        private File file;
28

    
29
        private FileDriver driver;
30

    
31
        private int sem = 0;
32

    
33
        private int fieldCount = -1;
34

    
35
        /**
36
         * @see com.hardcode.gdbms.engine.data.DataSource#start()
37
         */
38
        public void start() throws ReadDriverException {
39
                try {
40
                        if (sem == 0) {
41
                                driver.open(file);
42
                        }
43

    
44
                        sem++;
45
                } catch (OpenDriverException e) {
46
                        throw new ReadDriverException(getName(),e);
47
                }
48
        }
49

    
50
        /**
51
         * @see com.hardcode.gdbms.engine.data.DataSource#stop()
52
         */
53
        public void stop() throws ReadDriverException {
54
                try {
55
                        sem--;
56

    
57
                        if (sem == 0) {
58
                                driver.close();
59
                        } else if (sem < 0) {
60
                                sem=0;
61
                                driver.close();
62
                                throw new RuntimeException("DataSource closed too many times");
63
                        }
64
                } catch (CloseDriverException e) {
65
                        throw new ReadDriverException(getName(),e);
66
                }
67
        }
68

    
69
        /**
70
         * Asigna el driver al adaptador
71
         *
72
         * @param driver
73
         *            The driver to set.
74
         */
75
        public void setDriver(FileDriver driver) {
76
                this.driver = driver;
77
        }
78

    
79
        /**
80
         * Sets the source information of the DataSource
81
         *
82
         * @param sourceInfo
83
         *            The file to set.
84
         */
85
        public void setSourceInfo(SourceInfo sourceInfo) {
86
                super.setSourceInfo(sourceInfo);
87
                file = new File(((FileSourceInfo) sourceInfo).file);
88
        }
89

    
90
        /**
91
         * @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
92
         */
93
        public ReadAccess getReadDriver() {
94
                return driver;
95
        }
96

    
97
        /**
98
         * @see com.hardcode.gdbms.engine.data.DataSource#getName()
99
         */
100
        public String getName() {
101
                return sourceInfo.name;
102
        }
103

    
104
        /**
105
         * @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
106
         */
107
        public int[] getPrimaryKeys() throws ReadDriverException {
108
                // The last field is the pk/row in FileDataSources
109
                return new int[] { getFieldCount() - 1 };
110
        }
111

    
112
        /**
113
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
114
         */
115
        public int getFieldCount() throws ReadDriverException {
116
                if (fieldCount == -1) {
117
                        fieldCount = getReadDriver().getFieldCount() + 1;
118

    
119
                }
120

    
121
                return fieldCount;
122
        }
123

    
124
        /**
125
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldName(int)
126
         */
127
        public String getFieldName(int fieldId) throws ReadDriverException {
128
                // last field is the virtual primary key
129
                if (fieldId == getFieldCount() - 1)
130
                        return "PK";
131
                return getReadDriver().getFieldName(fieldId);
132
        }
133

    
134
        /**
135
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
136
         */
137
        public int getFieldType(int i) throws ReadDriverException {
138
                // last field is the virtual primary key
139
                if (i == getFieldCount() - 1)
140
                        return Types.BIGINT;
141
                return getReadDriver().getFieldType(i);
142
        }
143

    
144
        /**
145
         * @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
146
         *      int)
147
         */
148
        public Value getFieldValue(long rowIndex, int fieldId)
149
                        throws ReadDriverException {
150
                // last field is the virtual primary key
151
                if (fieldId == getFieldCount() - 1)
152
                        return ValueFactory.createValue(rowIndex);
153
                Value v = getReadDriver().getFieldValue(rowIndex, fieldId);
154
                return (v == null) ? ValueFactory.createNullValue() : v;
155
        }
156

    
157
        /**
158
         * @see com.hardcode.gdbms.engine.data.file.FileDataSource#getDriver()
159
         */
160
        public Driver getDriver() {
161
                return driver;
162
        }
163

    
164
        /**
165
         * @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
166
         */
167
        public DataWare getDataWare(int mode) throws ReadDriverException {
168
                try {
169
                        FileDataWare dw = FileDataSourceFactory.newDataWareInstance();
170
                        FileDriver driver;
171
                        driver = (FileDriver) getDataSourceFactory().getDriverManager()
172
                                        .getDriver(getDriver().getName());
173
                        ((GDBMSDriver) driver).setDataSourceFactory(getDataSourceFactory());
174
                        dw.setDriver(driver);
175
                        dw.setDataSourceFactory(dsf);
176
                        dw.setSourceInfo(getSourceInfo());
177
                        return dw;
178
                } catch (DriverLoadException e) {
179
                        throw new ReadDriverException(getName(),e);
180
                }
181

    
182
        }
183

    
184
        public int getFieldWidth(int i) throws ReadDriverException {
185
                return getReadDriver().getFieldWidth(i);
186
        }
187

    
188
    public boolean isVirtualField(int fieldId) throws ReadDriverException {
189
                // last field is the virtual primary key
190
                if (fieldId == this.getFieldCount() - 1)
191
                        return true;
192
                return false;
193
    }
194

    
195
        public void reload() throws ReloadDriverException {
196
                try {
197
                        sem = 0;
198
                        driver.close();
199

    
200
                this.fieldCount = -1;
201

    
202
                this.start();
203

    
204
                this.raiseEventReloaded();
205
                } catch (CloseDriverException e) {
206
                        throw new ReloadDriverException(getName(),e);
207
                } catch (ReadDriverException e) {
208
                        throw new ReloadDriverException(getName(),e);
209
                }
210

    
211
        }
212

    
213
}