KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > indexsystem > BlobClobColumnObjectsTable


1 package com.daffodilwoods.daffodildb.server.datasystem.indexsystem;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.btree.*;
4 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
5 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.*;
6 import com.daffodilwoods.daffodildb.server.datasystem.utility.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.utility._Record;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
9 import com.daffodilwoods.daffodildb.utils.*;
10 import com.daffodilwoods.daffodildb.utils.byteconverter.*;
11 import com.daffodilwoods.daffodildb.utils.field.*;
12 import com.daffodilwoods.database.general.*;
13 import com.daffodilwoods.database.resource.*;
14 import com.daffodilwoods.database.utility.*;
15 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Utility;
16 import com.daffodilwoods.fulltext.common._FullTextDML;
17 import com.daffodilwoods.daffodildb.server.sql99.fulltext.dml.
18
    _FullTextIndexInformation;
19
20 /**
21  * It does conversion of record Objects into bytes and bytes into objects
22  */

23 public class BlobClobColumnObjectsTable
24     implements _IndexTableList, _IndexTable {
25
26   /**
27    * index to perform opertaions on btree nad underlying iterator
28    */

29   protected _IndexTableList table;
30
31   /**
32    * tableCharacteristics to convert objects into bytes and viceversa
33    */

34   protected TableCharacteristics tableCharacteristics;
35
36   /**
37    * To perform opertions on blob clob columns of table
38    */

39   public LobManager lobManager;
40
41   /**
42    * Name of table
43    */

44   private QualifiedIdentifier tableName;
45
46   /**
47    * Underlying table(File or memory)
48    */

49   protected _DataTable dataTable;
50
51   public BlobClobColumnObjectsTable(_DataTable tab,
52                                     QualifiedIdentifier tableName0,
53                                     LobManager lobManager0) throws DException {
54     dataTable = tab;
55     lobManager = lobManager0;
56     tableName = tableName0;
57     tableCharacteristics = (TableCharacteristics) dataTable.
58         getTableCharacteristics();
59   }
60
61   /**
62    * Converts record Objects into bytes and if any column in table is of blob clob type then inserts
63    * data through lobmanager, it gets addrees of first cluster for blob clob insertion, then update
64    * the value of this column with startAddress.
65    *
66    *
67    * @param iterator Underlying Iterator to insert data
68    * @param user to perform write operations in file
69    * @param values values which has to insert
70    * @param index index ob Btree on which Iterator is taken
71    *
72    * @return BtreeElement where Record is inserted in BTree
73    */

74
75   public Object JavaDoc insert(_TableIterator iterator, _DatabaseUser user,
76                        Object JavaDoc[] values, int index) throws DException {
77     insertIntoFullTextDML(user, values);
78     int columnCount = tableCharacteristics.getColumnCount();
79     for (int i = 0; i < columnCount; i++) {
80         if (! ( (FieldBase) values[i]).getNull() &&
81             tableCharacteristics.isBlobClob(i)) {
82           values[i] = lobManager.insertBlobBytes(user, values[i], i);
83
84         }
85
86     }
87     return table.insert(iterator, user,
88                         tableCharacteristics.getBufferRange(values), index);
89   }
90
91   /**
92    * Converts record Objects into bytes , If any column in table is of blob clob type then gets start
93    * cluster address of previously stored o
94    *
95    * @param iterator Underlying Iterator to insert data
96    * @param user to perform write operations in file
97    * @param values values which has to insert
98    * @param index index ob Btree on which Iterator is taken
99    *
100    * @return BtreeElement where Record is inserted in BTree
101    */

102
103   public Object JavaDoc update(_TableIterator iterator, _DatabaseUser user,
104                        Object JavaDoc[] values, int index) throws DException {
105     BufferRange[] bytes = (BufferRange[]) iterator.getColumnValues();
106     updateIntoFullTextDML(user, values, bytes);
107
108     int columnCount = tableCharacteristics.getColumnCount();
109
110     for (int i = 0,startAddress; i < columnCount; i++) {
111       if (tableCharacteristics.isBlobClob(i)) {
112          startAddress = bytes[i].getNull() ? 0 :
113             CCzufDpowfsufs.getIntValue(bytes[i], 0);
114         short recordNumber = bytes[i].getNull() ? 0 :
115             CCzufDpowfsufs.getShortValue(bytes[i], 4);
116         if (! ( (FieldBase) values[i]).getNull()) {
117               if(!((_LobUpdatable)values[i]).isDBlob()){
118           values[i] = lobManager.updateBlob(user, startAddress, values[i], i,
119                                             recordNumber);
120           }
121         }
122         else if ( ( (FieldBase) values[i]).getNull()) {
123           lobManager.deleteBlob(user, startAddress, i, recordNumber);
124         }
125       }
126     }
127     BufferRange[] bytesToUpdate = tableCharacteristics.getBufferRange(values);
128     return table.update(iterator, user, bytesToUpdate, index);
129   }
130
131   public Object JavaDoc delete(_TableIterator iterator, _DatabaseUser user, int index) throws
132       DException {
133     BufferRange[] bytes = (BufferRange[]) iterator.getColumnValues();
134     deleteFromFullTextDML(user, iterator);
135     int columnLength = tableCharacteristics.getColumnTypes().length;
136     for (int i = 0; i < columnLength; i++) {
137       if (!bytes[i].getNull() && tableCharacteristics.isBlobClob(i)) {
138         int startAddress = bytes[i].getNull() ? 0 :
139             CCzufDpowfsufs.getIntValue(bytes[i], 0);
140         short recordNumber = bytes[i].getNull() ? 0 :
141             CCzufDpowfsufs.getShortValue(bytes[i], 4);
142         lobManager.deleteBlob(user, startAddress, i, recordNumber);
143       }
144     }
145     return table.delete(iterator, user, index);
146   }
147
148   public _IndexTableList getTable() {
149     return table;
150   }
151
152   public void setTable(_IndexTableList table1) throws DException {
153     table = table1;
154   }
155
156   public void setFullTextDML(_FullTextDML fullTextDML[]) throws DException {
157     table.setFullTextDML(fullTextDML);
158   }
159
160   public _FullTextDML[] getFullTextDML() throws DException {
161     return table.getFullTextDML();
162   }
163
164   public Object JavaDoc getColumnObjects(_TableIterator iterator, int[] columns) throws
165       DException {
166     Object JavaDoc[] values = (Object JavaDoc[]) iterator.getColumnValues(columns);
167
168     FieldBase[] temp = new FieldBase[columns.length];
169     if (lobManager == null) {
170       for (int i = 0; i < columns.length; i++) {
171         temp[i] = tableCharacteristics.isBlobClob(columns[i]) ?
172             (FieldBase) values[i] :
173             (FieldBase) tableCharacteristics.getObject(columns[i],
174             (BufferRange) values[i]);
175       }
176       return temp;
177     }
178     else {
179       for (int i = 0; i < columns.length; i++) {
180         if (tableCharacteristics.isBlobClob(columns[i])) {
181           int columnType = tableCharacteristics.getColumnType(columns[i]);
182           if (! ( (BufferRange) values[i]).getNull())
183             temp[i] = convertBlobClob(columns[i], columnType, values[i]);
184           else {
185             temp[i] = Utility.isBlob(columnType) ? new DBlobUpdatable(true) :
186                 new DClobUpdatable(true);
187           }
188         }
189         else
190           temp[i] = (FieldBase) tableCharacteristics.getObject(columns[i],
191               (BufferRange) values[i]);
192       }
193       return temp;
194     }
195   }
196
197   public _TableCharacteristics getTableCharacteristics() throws DException {
198     return tableCharacteristics;
199   }
200
201   public Object JavaDoc insert(_TableIterator iterator, Object JavaDoc[] values, int index) throws
202       DException {
203     return table.insert(iterator, convertValuesIntoBytes(values), index);
204   }
205
206   public Object JavaDoc update(_TableIterator iterator, Object JavaDoc[] values, int index) throws
207       DException {
208     return table.update(iterator, convertValuesIntoBytes(values), index);
209   }
210
211   public Object JavaDoc delete(_TableIterator iterator, int index) throws DException {
212     return table.delete(iterator, index);
213   }
214
215
216
217   public Object JavaDoc getColumnObjects(_TableIterator iterator, int column) throws
218       DException {
219     Object JavaDoc bytes = iterator.getColumnValues(column);
220     if (tableCharacteristics.isBlobClob(column)) {
221       if (lobManager == null)
222         return (FieldBase) bytes;
223       else {
224         int columnType = tableCharacteristics.getColumnType(column);
225         if (! ( (BufferRange) bytes).getNull())
226           return convertBlobClob(column, columnType, bytes);
227         else
228           return Utility.isBlob(columnType) ? new DBlobUpdatable(true) :
229               new DClobUpdatable(true);
230       }
231     }
232     else {
233       return (FieldBase) tableCharacteristics.getObject(column,
234           (BufferRange) bytes);
235     }
236   }
237
238
239   public Object JavaDoc getColumnObjects(_TableIterator iterator) throws DException {
240     Object JavaDoc[] bytes = (Object JavaDoc[]) iterator.getColumnValues();
241     FieldBase[] values = new FieldBase[bytes.length];
242     if (lobManager == null) {
243       for (int i = 0, count = tableCharacteristics.getColumnCount(); i < count;
244            i++) {
245         values[i] = tableCharacteristics.isBlobClob(i) ? (FieldBase) bytes[i] :
246             (FieldBase) tableCharacteristics.getObject(i, (BufferRange) bytes[i]);
247       }
248       return values;
249     }
250     else {
251       for (int i = 0, count = tableCharacteristics.getColumnCount(); i < count;
252            i++) {
253         if (tableCharacteristics.isBlobClob(i)) {
254           int columnType = tableCharacteristics.getColumnType(i);
255           if (! ( (BufferRange) bytes[i]).getNull())
256             values[i] = convertBlobClob(i, columnType, bytes[i]);
257           else {
258             values[i] = Utility.isBlob(columnType) ? new DBlobUpdatable(true) :
259                 new DClobUpdatable(true);
260           }
261         }
262         else
263           values[i] = (FieldBase) tableCharacteristics.getObject(i,
264               (BufferRange) bytes[i]);
265       }
266       return values;
267     }
268   }
269
270   public _IndexInformation[] getIndexInformations() throws com.daffodilwoods.
271
      database.resource.DException {
272     return table.getIndexInformations();
273   }
274
275   public _Iterator getIterator(int index) throws com.daffodilwoods.database.
276
      resource.DException {
277     _IndexIterator it = new IndexTableIterator(this, index,
278                                                dataTable.getIterator());
279     IteratorWrapper iw = new IteratorWrapper(it);
280     iw.setTableName(tableName);
281     return iw;
282   }
283
284   public _Index getIndex(int position) {
285     return table.getIndex(position);
286   }
287
288   public _Index[] getIndexes() throws DException {
289     return table.getIndexes();
290   }
291
292   public _Record getBlankRecord() throws com.daffodilwoods.database.resource.
293
      DException {
294     return new Record(tableCharacteristics,
295                       new Object JavaDoc[tableCharacteristics.getColumnCount()]);
296   }
297
298   public void rollback() throws DException {
299     if (table instanceof Table) {
300       table.rollback();
301       ( (Table) dataTable).rollBack();
302     }
303   }
304
305   public Object JavaDoc seek(_Index btree, Object JavaDoc indexKey) throws DException {
306     int[] columnIndexes = btree.getIndexInformation().getColumnIndexes();
307     return btree.seek(getSeekKey(indexKey, columnIndexes));
308   }
309
310   public Object JavaDoc seekFromTopRelative(_Index btree, Object JavaDoc currentKey,
311                                     Object JavaDoc indexKey) throws DException {
312     int[] columnIndexes = btree.getIndexInformation().getColumnIndexes();
313     return btree.seekFromTopRelative(currentKey,
314                                      getSeekKey(indexKey, columnIndexes));
315   }
316
317   public Object JavaDoc seekFromBottomRelative(_Index btree, Object JavaDoc currentKey,
318                                        Object JavaDoc indexKey) throws DException {
319     return btree.seekFromBottomRelative(currentKey,
320                                         getSeekKey(indexKey, btree.getIndexInformation().getColumnIndexes()));
321   }
322
323   public Object JavaDoc locateKey(_Index btree, Object JavaDoc indexKey, boolean flag) throws
324       DException {
325     return (_IndexKey)btree.locateKey(getSeekKey(indexKey, btree.getIndexInformation().getColumnIndexes()),
326                                          flag);
327   }
328
329   public void setIndexes(_Index btrees[]) throws DException {
330     table.setIndexes(btrees);
331   }
332
333   public _Iterator getDefaultIterator() throws com.daffodilwoods.database.
334
      resource.DException {
335     _IndexInformation[] iinf = getIndexInformations();
336     for (int i = 0; i < iinf.length; i++) {
337       if (iinf[i].isDefault()) {
338         IteratorWrapper iw = new IteratorWrapper(new IndexTableIterator(this, i,
339             dataTable.getIterator()));
340         iw.setTableName(tableName);
341         return iw;
342       }
343     }
344     new Exception JavaDoc(" TABLENAME ::: " + tableName).printStackTrace();
345     throw new DException("DSE2015", new Object JavaDoc[] {tableName.toString()});
346   }
347
348   public int getEstimatedRowCount() throws com.daffodilwoods.database.resource.
349
      DException {
350     return ( (_IndexTable) table).getEstimatedRowCount();
351   }
352
353   public String JavaDoc toString() {
354     return "" + tableName;
355   }
356
357   private FieldBase[] getObject(Object JavaDoc[] bytes, FieldBase[] values) throws
358       DException {
359     FieldBase[] temp = new FieldBase[bytes.length];
360     for (int i = 0; i < bytes.length; i++) {
361       temp[i] = !tableCharacteristics.isBlobClob(i) ?
362           (FieldBase) tableCharacteristics.getObject(i, (BufferRange) bytes[i])
363           : (FieldBase) values[i];
364       if (temp[i] == null) {
365          ;//// Removed By Program ** System.out.println(" VALUES :: " + java.util.Arrays.asList(values));
366
new Exception JavaDoc(" COLUMN TYPE :: " + tableCharacteristics.getColumnType(i)).
367             printStackTrace();
368       }
369     }
370     return temp;
371   }
372
373   private FieldBase[] getObject(int[] columns, Object JavaDoc[] bytes,
374                                 FieldBase[] values) throws DException {
375     FieldBase[] temp = new FieldBase[bytes.length];
376     for (int i = 0; i < bytes.length; i++)
377       temp[i] = !tableCharacteristics.isBlobClob(columns[i]) ?
378           (FieldBase) tableCharacteristics.getObject(columns[i],
379           (BufferRange) bytes[i])
380           : (FieldBase) values[i];
381     return temp;
382   }
383
384   /**
385    *
386    * @param i
387    * @param columnTypes
388    * @param values
389    * @return
390    * @throws DException
391    */

392
393   private FieldBase convertBlobClob(int columnIndex, int columnType,
394                                     Object JavaDoc values) {
395     int startAddress = CCzufDpowfsufs.getIntValue( ( (BufferRange)
396         values).getBytes(), 0);
397     short recordNumber = CCzufDpowfsufs.getShortValue( ( (BufferRange) values).
398         getBytes(), 4);
399     return Utility.isBlob(columnType) ?
400         (FieldBase) lobManager.retrieveDataForBlob(startAddress, recordNumber) :
401         (FieldBase) lobManager.retrieveDataForClob(startAddress, recordNumber);
402   }
403
404   public Object JavaDoc update(_TableIterator iterator, _DatabaseUser user,
405                        int[] columns, Object JavaDoc[] columnValues, int index) throws
406       DException {
407     BufferRange[] bytes = (BufferRange[]) iterator.getColumnValues();
408     updateIntoFullTextDML(user, columnValues, bytes, columns);
409
410     for (int i = 0; i < columns.length; i++) {
411       if (tableCharacteristics.isBlobClob(columns[i])) {
412         int startAddress = bytes[columns[i]].getNull() ? 0 :
413             CCzufDpowfsufs.getIntValue(bytes[columns[i]], 0);
414         short recordNumber = bytes[columns[i]].getNull() ? 0 :
415             CCzufDpowfsufs.getShortValue(bytes[columns[i]], 4);
416         if (! ( (FieldBase) columnValues[i]).getNull()) {
417           if (! ( (_LobUpdatable) columnValues[i]).isDBlob()) {
418             columnValues[i] = lobManager.updateBlob(user, startAddress,
419                 columnValues[i], columns[i], recordNumber);
420           }
421         }
422         else if ( ( (FieldBase) columnValues[i]).getNull()) {
423           lobManager.deleteBlob(user, startAddress, columns[i], recordNumber);
424         }
425       }
426     }
427
428     BufferRange[] bytesOfColumns = tableCharacteristics.getBufferRange(columns,
429         columnValues);
430     for (int i = 0; i < columns.length; i++)
431       bytes[columns[i]] = bytesOfColumns[i];
432     return table.update(iterator, user, columns, bytes, index);
433   }
434
435   public Object JavaDoc update(_TableIterator iterator, int[] columns, Object JavaDoc[] values,
436                        int index) throws DException {
437     Object JavaDoc[] rowBytes = (Object JavaDoc[]) iterator.getColumnValues();
438     Object JavaDoc[] tempValues = new Object JavaDoc[rowBytes.length];
439       System.arraycopy(rowBytes, 0, tempValues, 0, rowBytes.length);
440     for (int i = 0; i < values.length; i++) {
441       tempValues[columns[i]] = !tableCharacteristics.isBlobClob(columns[i]) ?
442           ( (FieldBase) values[i]).getBufferRange()
443           : values[i];
444     }
445     return table.update(iterator, columns, tempValues, index);
446   }
447
448   private Object JavaDoc getSeekKey(Object JavaDoc indexKey, int[] columnIndexes) throws
449       DException {
450       if (columnIndexes.length == 1) {
451         if (! (indexKey instanceof BufferRange) &&
452             ! (indexKey instanceof BufferRange[])) {
453           try {
454             indexKey = tableCharacteristics.getBufferRange(columnIndexes[0],
455                 indexKey);
456           }
457           catch (ClassCastException JavaDoc ex) {
458             indexKey = tableCharacteristics.getBufferRange(columnIndexes[0],
459                 ( (Object JavaDoc[]) indexKey)[0]);
460           }
461         }
462       }
463       else {
464         Object JavaDoc[] key = (Object JavaDoc[]) indexKey;
465         int[] seekColumns = new int[key.length];
466         for (int i = 0; i < key.length; i++)
467           seekColumns[i] = columnIndexes[i];
468           if (! (key[0] instanceof BufferRange))
469             indexKey = tableCharacteristics.getBufferRange(seekColumns,
470                 (Object JavaDoc[]) indexKey);
471
472
473       }
474
475     return indexKey;
476   }
477
478   private Object JavaDoc[] convertValuesIntoBytes(Object JavaDoc[] values) throws DException {
479     Object JavaDoc[] bytes = new Object JavaDoc[values.length];
480     for (int i = 0; i < values.length; i++) {
481       bytes[i] = !tableCharacteristics.isBlobClob(i) ?
482           ( (FieldBase) values[i]).getBufferRange() : values[i];
483     }
484     return bytes;
485   }
486
487   public ReadWriteLock getLock() {
488     return table.getLock();
489   }
490
491   public boolean isAnyIndexUpdated(int[] columns) throws DException {
492     return table.isAnyIndexUpdated(columns);
493   }
494
495   public _FullTextIndexInformation[] getFullTextIndexInformation() throws
496       DException {
497     return table.getFullTextIndexInformation();
498   }
499
500   public _Database getDatabase() throws DException {
501     return table.getDatabase();
502   }
503
504   /**
505    * when a row is inserted than the values of those columns which is used in
506    * full text searching must be entered into the fullTextDML objects which is
507    * created on that column.
508    * @param user which inserts the new value.
509    * @param values new row values.
510    * @throws DException
511    */

512   private void insertIntoFullTextDML(_DatabaseUser user, Object JavaDoc[] values) throws
513       DException {
514     _FullTextDML[] fullTextDML = table.getFullTextDML();
515     if (fullTextDML != null) {
516       Object JavaDoc rowid = values[SystemFields.rowId];
517       for (int i = 0; i < fullTextDML.length; i++) {
518         int[] columnIndexes = fullTextDML[i].getColumnIndex();
519         Object JavaDoc[] valuesToInsertIntoDML = new FieldBase[columnIndexes.length];
520         for (int k = 0; k < columnIndexes.length; k++) {
521           if (tableCharacteristics.isBlobClob(columnIndexes[k])) {
522             BufferRange br;
523             if(( (DClobUpdatable) values[columnIndexes[k]]).isNull() )
524               br = FieldUtility.NULLBUFFERRANGE ;
525             else{
526               byte[] clobBytes = ( (DClobUpdatable) values[columnIndexes[k]]).
527                   getBytes();
528               br = new BufferRange(clobBytes, 0, clobBytes.length);
529             }
530             FieldString fieldString = new FieldString(br, Datatype.CLOB,
531                 tableCharacteristics.getCollator());
532             valuesToInsertIntoDML[k] = fieldString;
533           }
534           else
535             valuesToInsertIntoDML[k] = values[columnIndexes[k]];
536         }
537         fullTextDML[i].insert(user, valuesToInsertIntoDML, rowid);
538       }
539     }
540   }
541
542   /**
543    * To update DML objects when a row is updated.
544    * @param user which updates the row.
545    * @param newValues array of selected column values which is updated
546    * @param oldValues array of old values of all columns
547    * @param columnIndexes which is updated by user
548    * @throws DException
549    */

550
551   private void updateIntoFullTextDML(_DatabaseUser user, Object JavaDoc[] newValues,
552                                       Object JavaDoc oldValues) throws DException {
553      _FullTextDML[] fullTextDML = table.getFullTextDML();
554      if (fullTextDML != null) {
555        Object JavaDoc rowid = newValues[SystemFields.rowId];
556        for (int i = 0; i < fullTextDML.length; i++) {
557          int[] columnIndex = fullTextDML[i].getColumnIndex();
558          Object JavaDoc[] valuesToUpdateIntoDML = new FieldBase[columnIndex.length];
559          Object JavaDoc[] oldValuesIntoDML = new FieldBase[columnIndex.length];
560          for (int k = 0; k < columnIndex.length; k++) {
561            Object JavaDoc old = tableCharacteristics.getObject(columnIndex[k],
562                ( (BufferRange[]) oldValues)[columnIndex[k]]);
563            if (tableCharacteristics.isBlobClob(columnIndex[k])) {
564
565              BufferRange oldbr;
566              if((( (BufferRange[]) oldValues)[columnIndex[k]]).getNull() )
567                 oldbr = FieldUtility.NULLBUFFERRANGE ;
568              else{
569                DClobUpdatable dclobUpdatable = (DClobUpdatable) convertBlobClob(columnIndex[k],tableCharacteristics.getColumnType(columnIndex[k]), ( (BufferRange[]) oldValues)[columnIndex[k]]);
570                byte[] oldClobBytes = dclobUpdatable.getBytes();
571                oldbr = new BufferRange(oldClobBytes, 0,
572                                        oldClobBytes.length);
573              }
574              FieldString oldFieldString = new FieldString(oldbr, Datatype.CLOB,
575                  tableCharacteristics.getCollator());
576            BufferRange newbr;
577            if(((DClobUpdatable) newValues[columnIndex[k]]).isNull() )
578             newbr = FieldUtility.NULLBUFFERRANGE ;
579           else{
580              byte[] newClobBytes = ( (DClobUpdatable) newValues[columnIndex[k]]). getBytes();
581               newbr = new BufferRange(newClobBytes, 0,
582                                                  newClobBytes.length);
583           }
584            FieldString newFieldString = new FieldString(newbr, Datatype.CLOB,
585                  tableCharacteristics.getCollator());
586              valuesToUpdateIntoDML[k] = newFieldString;
587              oldValuesIntoDML[k] = oldFieldString;
588            }
589            else {
590              valuesToUpdateIntoDML[k] = newValues[columnIndex[k]];
591              oldValuesIntoDML[k] = old;
592            }
593
594          }
595          fullTextDML[i].update(user, oldValuesIntoDML, valuesToUpdateIntoDML,
596                                rowid);
597        }
598      }
599
600    }
601
602    /**
603     * To update those DML objects which are created on the columns which is to be updated
604     * @param user
605     * @param newValues array of selected column values which is updated
606     * @param oldValues array of old values of all columns
607     * @param columnIndexes which is updated by user
608     * @throws DException
609     */

610    private void updateIntoFullTextDML(_DatabaseUser user, Object JavaDoc[] newValues,
611                                       Object JavaDoc oldValues, int[] columnIndexes) throws
612        DException {
613      _FullTextDML[] fullTextDML = table.getFullTextDML();
614      if (fullTextDML != null) {
615        Object JavaDoc rowid = tableCharacteristics.getObject(SystemFields.rowId,
616            ( (BufferRange[]) oldValues)[SystemFields.rowId]);
617        for (int i = 0; i < fullTextDML.length; i++) {
618          int[] columnIndex = fullTextDML[i].getColumnIndex();
619          Object JavaDoc[] valuesToUpdateIntoDML = new FieldBase[columnIndex.length];
620          Object JavaDoc[] oldValuesIntoDML = new FieldBase[columnIndex.length];
621          boolean isAnyColumnUpdated = false;
622          for (int k = 0; k < columnIndex.length; k++) {
623            int colIndex = checkUpdateColumnsInFullText(columnIndexes,
624                columnIndex[k]);
625            Object JavaDoc oldValue = tableCharacteristics.getObject(columnIndex[k],
626                ( (BufferRange[]) oldValues)[columnIndex[k]]);
627            if (colIndex != -1) {
628              isAnyColumnUpdated = true;
629              if (tableCharacteristics.isBlobClob(columnIndex[k])) {
630                if(((DClobUpdatable) newValues[colIndex]).isDBlob() ){
631                  BufferRange oldbr;
632                  if((( (BufferRange[]) oldValues)[columnIndex[k]]).getNull() )
633                     oldbr = FieldUtility.NULLBUFFERRANGE ;
634                  else{
635                  DClobUpdatable dclobUpdatable = (DClobUpdatable) convertBlobClob(columnIndex[k],
636                      tableCharacteristics.getColumnType(columnIndex[k]),
637                      ( (BufferRange[]) oldValues)[columnIndex[k]]);
638                    byte[] oldClobBytes =dclobUpdatable.getBytes();
639                   oldbr = new BufferRange(oldClobBytes, 0,
640                        oldClobBytes.length);
641                  }
642                  FieldString oldFieldString = new FieldString(oldbr,
643                      Datatype.CLOB,
644                      tableCharacteristics.getCollator());
645                  BufferRange newbr ;
646                  if(((DClobUpdatable) newValues[colIndex]).isNull() )
647                    newbr = FieldUtility.NULLBUFFERRANGE ;
648                  else{
649                    byte[] newClobBytes = ( (DClobUpdatable) newValues[colIndex]).
650                        getBytes();
651                    newbr = new BufferRange(newClobBytes, 0,
652                        newClobBytes.length);
653                  }
654                  FieldString newFieldString = new FieldString(newbr,
655                      Datatype.CLOB,
656                      tableCharacteristics.getCollator());
657                  valuesToUpdateIntoDML[k] = newFieldString;
658
659                if (oldValuesIntoDML != null)
660                  oldValuesIntoDML[k] = oldFieldString;
661              } else {
662              valuesToUpdateIntoDML[k] = oldValue;
663              oldValuesIntoDML[k] = oldValue;
664            }
665
666              }
667              else {
668                valuesToUpdateIntoDML[k] = newValues[colIndex];
669                oldValuesIntoDML[k] = oldValue;
670              }
671            }
672            else {
673              valuesToUpdateIntoDML[k] = oldValue;
674                oldValuesIntoDML[k] = oldValue;
675            }
676
677          }
678          if (isAnyColumnUpdated) { // we are passing old values null because here we don't use them it is passed for future use
679
fullTextDML[i].update(user, oldValuesIntoDML, valuesToUpdateIntoDML, rowid);
680          }
681
682        }
683      }
684    }
685
686   private int checkUpdateColumnsInFullText(int[] updateColumns,
687                                            int fullTextColumn) {
688     for (int i = 0; i < updateColumns.length; i++) {
689       if (updateColumns[i] == fullTextColumn)
690         return i;
691     }
692     return -1;
693   }
694
695   /**
696    * when user deletes a row than we need to delete the column value which is stored in
697    * full text DML object if it created.
698    * @param user which is deleting the values.
699    * @param values row deleted by user.
700    * @throws DException
701    */

702   private void deleteFromFullTextDML(_DatabaseUser user,
703                                      _TableIterator iterator) throws DException {
704     _FullTextDML[] fullTextDML = table.getFullTextDML();
705     if (fullTextDML != null) {
706       BufferRange[] values = (BufferRange[]) iterator.getColumnValues();
707       Object JavaDoc rowid = tableCharacteristics.getObject(SystemFields.rowId,
708           ( (BufferRange[]) values)[SystemFields.rowId]);
709       for (int i = 0; i < fullTextDML.length; i++)
710         fullTextDML[i].delete(user, rowid);
711
712     }
713   }
714
715   public void setDuplicateKeysAllowedInBtrees() throws DException {
716     ( (_IndexTable) table).setDuplicateKeysAllowedInBtrees();
717   }
718
719   public Object JavaDoc deleteBlobClobRecord(_TableIterator iterator, _DatabaseUser user, int index) throws
720       DException {
721     BufferRange[] bytes = (BufferRange[]) iterator.getColumnValues();
722     deleteFromFullTextDML(user, iterator);
723     return table.delete(iterator, user, index);
724   }
725
726
727 }
728
Popular Tags