Statistics
| Revision:

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

History | View | Annotate | Download (7.61 KB)

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

    
3
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
4
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
5
import com.hardcode.gdbms.engine.data.InnerDBUtils;
6
import com.hardcode.gdbms.engine.data.driver.DBDriver;
7
import com.hardcode.gdbms.engine.data.edition.DeletionInfo;
8
import com.hardcode.gdbms.engine.data.edition.EditionInfo;
9
import com.hardcode.gdbms.engine.data.edition.PKTable;
10
import com.hardcode.gdbms.engine.values.ValueCollection;
11
import com.hardcode.gdbms.engine.values.Value;
12
import com.hardcode.gdbms.engine.values.ValueFactory;
13
import com.hardcode.gdbms.engine.values.ValueWriter;
14

    
15

    
16
/**
17
 * DataWare for database data sources
18
 *
19
 * @author Fernando Gonz?lez Cort?s
20
 */
21
public class FakeTransactionDataWare extends DBDataSourceAdapter
22
    implements DBDataWare {
23

    
24
    private FakeTransactionSupport ftSupport = new FakeTransactionSupport(this);
25

    
26
    public void start() throws ReadDriverException {
27
        throw new RuntimeException("Invoke beginTrans in a DataWare");
28
    }
29

    
30
    public void stop() throws ReadDriverException {
31
        throw new RuntimeException("Invoke commitTrans/rollBackTrans in a DataWare");
32
    }
33

    
34
    /**
35
     * @throws WriteDriverException
36
     * @see com.hardcode.gdbms.engine.data.edition.DataWare#commitTrans()
37
     */
38
    public void commitTrans() throws ReadDriverException, WriteDriverException {
39
        //Deleted records
40
        for (int i = 0; i < ftSupport.getPKTable().getDeletedPKCount(); i++) {
41
            /*
42
             * Check if the deleted row is an original row. If
43
             * not it's not needed to execute any query
44
             */
45
            DeletionInfo di = ftSupport.getPKTable().getDeletedPK(i);
46

    
47
            if (di.getOriginalIndex() == -1) {
48
                continue;
49
            }
50

    
51
            /*
52
             * Get the primary key names and the string represetation
53
             * of th primary key values in a SQL statement
54
             */
55
            ValueCollection av = di.getPk();
56
            String[] names = getPKNames();
57

    
58
            //Create the delete statement
59
            String sql = InnerDBUtils.createDeleteStatement(av.getValues(), names,
60
                    ((DBTableSourceInfo) getSourceInfo()).tableName, ValueWriter.internalValueWriter);
61

    
62
            //execute the delete statement
63
            execute(sql);
64
        }
65

    
66
        //Updated and added records
67
        for (int i = 0; i < ftSupport.getPKTable().getPKCount(); i++) {
68
            EditionInfo loc = ftSupport.getPKTable().getIndexLocation(i);
69

    
70
            if (loc.getFlag() == PKTable.ADDED) {
71
                //get the index in the internal buffer table
72
                int actualIndex = loc.getIndex();
73

    
74
                /*
75
                 * get the field names and the field values of the
76
                 * added row
77
                 */
78
                String[] fieldNames = getFieldNames();
79
                Value[] row = ftSupport.getInternalBuffer().getRow(actualIndex);
80
                Value[] rowWithoutGDBMSIndex = new Value[row.length - 1];
81
                System.arraycopy(row, 0, rowWithoutGDBMSIndex, 0,
82
                    rowWithoutGDBMSIndex.length);
83

    
84
                //create and execute the insert statement
85
                String sql = InnerDBUtils.createInsertStatement(((DBTableSourceInfo) getSourceInfo()).tableName,
86
                        rowWithoutGDBMSIndex, fieldNames, ((DBDriver)getDriver()));
87

    
88
                execute(sql);
89
            } else if (loc.getFlag() == PKTable.MODIFIED) {
90
                //get the index in the internal buffer table
91
                int actualIndex = loc.getIndex();
92

    
93
                /*
94
                 * Get the primary key values. field values, fieldnames, etc
95
                 */
96
                ValueCollection av = getOriginalPKValue(loc.getOriginalIndex());
97
                Value[] pks = av.getValues();
98
                String[] pkNames = getPKNames();
99
                String[] fieldNames = getFieldNames();
100
                Value[] row = ftSupport.getInternalBuffer().getRow(actualIndex);
101

    
102
                /*
103
                 * Create and execute the update statement
104
                 */
105
                String sql = InnerDBUtils.createUpdateStatement(((DBTableSourceInfo) getSourceInfo()).tableName,
106
                        pks, pkNames, fieldNames, row, ((DBDriver)getDriver()));
107

    
108
                execute(sql);
109
            }
110
        }
111

    
112
        ftSupport.commitTrans();
113
        super.stop();
114
    }
115

    
116
    /**
117
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
118
     *      int)
119
     */
120
    public Value getFieldValue(long rowIndex, int fieldId)
121
        throws ReadDriverException {
122
        //If there is no transaction
123
        if (!ftSupport.isTransaction()) {
124
            return super.getFieldValue(rowIndex, fieldId);
125
        }
126

    
127
        //get where's the row
128
        EditionInfo fip = ftSupport.getPKTable().getIndexLocation((int) rowIndex);
129
        int flag = fip.getFlag();
130

    
131
        //get the value
132
        if ((flag == PKTable.ADDED) || (flag == PKTable.MODIFIED)) {
133
            return ftSupport.getInternalBuffer().getFieldValue(fip.getIndex(), fieldId);
134
        } else {
135
            return super.getFieldValue(fip.getIndex(), fieldId);
136
        }
137
    }
138

    
139
    /**
140
     * @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
141
     */
142
    public long getRowCount() throws ReadDriverException {
143
        if (ftSupport.isTransaction()) {
144
            //If there is a transaction
145
            return ftSupport.getPKTable().getPKCount();
146
        } else {
147
            //If there's not
148
            return super.getRowCount();
149
        }
150
    }
151

    
152
    /**
153
     * Gets the value of the original row
154
     *
155
     * @param rowIndex index of the row to be retrieved
156
     *
157
     * @return Row values
158
     * @throws ReadDriverException TODO
159
     */
160
    private Value[] getOriginalRow(long rowIndex) throws ReadDriverException {
161
        Value[] ret = new Value[getFieldCount()];
162

    
163
        for (int i = 0; i < ret.length; i++) {
164
            ret[i] = super.getFieldValue(rowIndex, i);
165
        }
166

    
167
        return ret;
168
    }
169

    
170
    /**
171
     * Gets the primary key value in the original DataSource
172
     *
173
     * @param rowIndex index of the row to be read
174
     *
175
     * @return PK
176
     * @throws ReadDriverException TODO
177
     */
178
    private ValueCollection getOriginalPKValue(long rowIndex) throws ReadDriverException {
179
        int[] fieldsId = getPrimaryKeys();
180
        Value[] pks = new Value[fieldsId.length];
181

    
182
        for (int i = 0; i < pks.length; i++) {
183
            pks[i] = super.getFieldValue(rowIndex, fieldsId[i]);
184
        }
185

    
186
        return ValueFactory.createValue(pks);
187
    }
188
    public void beginTrans() throws ReadDriverException {
189
        super.start();
190
        ftSupport.beginTrans();
191
    }
192
    public void deleteRow(long rowId) throws WriteDriverException, ReadDriverException {
193
        ftSupport.deleteRow(rowId);
194
    }
195
    public void insertEmptyRow(ValueCollection pk) throws WriteDriverException {
196
        ftSupport.insertEmptyRow(pk);
197
    }
198
    public void insertFilledRow(Value[] values) throws WriteDriverException, ReadDriverException {
199
        ftSupport.insertFilledRow(values);
200
    }
201
    public void rollBackTrans() throws ReadDriverException, WriteDriverException {
202
        ftSupport.rollBackTrans();
203
        super.stop();
204
    }
205
    public void setFieldValue(long row, int fieldId, Value value) throws WriteDriverException, ReadDriverException {
206
        ValueCollection pkValue = null;
207
        Value[] originalRow = null;
208
        if (row < super.getRowCount()){
209
            pkValue = getOriginalPKValue(row);
210
            originalRow = getOriginalRow(row);
211
        }
212
        ftSupport.setFieldValue(row, fieldId, value, pkValue,
213
                originalRow);
214
    }
215
}