00001
package com.quadcap.sql;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.IOException;
00042
00043
import java.util.ArrayList;
00044
import java.util.Enumeration;
00045
00046
import java.sql.SQLException;
00047
import java.sql.Types;
00048
00049
import com.quadcap.sql.file.BlockFile;
00050
import com.quadcap.sql.file.Datafile;
00051
import com.quadcap.sql.file.PageManager;
00052
00053
import com.quadcap.sql.types.Value;
00054
import com.quadcap.sql.types.ValueBlob;
00055
import com.quadcap.sql.types.Type;
00056
00057
import com.quadcap.util.Debug;
00058
import com.quadcap.util.Util;
00059
00060
00061
00062
00063
00064
00065 public class TableOps {
00066
00067
00068
00069 public static void insertRow(
Session session,
Table table,
Row row)
00070
throws SQLException, IOException
00071 {
00072 session.getTableWriteLock(table.getName());
00073
int num = table.getNumConstraints();
00074
00075
for (
int i = 0; i < num; i++) {
00076 table.getConstraint(i).checkInsert(session, row);
00077 }
00078
00079
if (table.hasBlobs()) {
00080
holdBlobsInRow(session, table, row);
00081 }
00082
00083
InsertRow ins =
new InsertRow(session, table, row);
00084 session.doStep(ins);
00085
long rowId = ins.
getRowId();
00086
00087
for (
int i = 0; i < num; i++) {
00088 table.getConstraint(i).applyInsert(session, row, rowId, null);
00089 }
00090
00091
00092
if (
Trace.bit(14)) {
00093
Debug.println(
"insertRow(" + table.getName() +
"): " +
00094 row +
" -> " + rowId);
00095 }
00096
00097 }
00098
00099
00100
00101
00102 public static void updateRow(
Session session,
Table table,
00103 byte[] key,
long rowId,
Row row,
00104
Constraint constraint)
00105
throws SQLException, IOException
00106 {
00107 session.getTableWriteLock(table.getName());
00108
00109
if (
Trace.bit(14)) {
00110
Debug.println(
"updateRow(" + table.getName() +
"): " +
00111 row +
" -> " + rowId);
00112 }
00113
00114
00115
Database db = session.getDatabase();
00116
BlockFile file = db.getFile();
00117
Row oldRow = table.getRow(db, rowId);
00118
int num = table.getNumConstraints();
00119
00120
for (
int i = 0; i < num; i++) {
00121
Constraint c = table.getConstraint(i);
00122 c.
checkUpdate(session, key, row, oldRow, rowId, constraint);
00123 }
00124
00125
if (table.hasBlobs()) {
00126
holdBlobsInRow(session, table, row);
00127
freeBlobsInRow(session, table, oldRow);
00128 }
00129
00130
if (db.
inMemory()) {
00131
00132 row =
new Row(row, table);
00133 }
00134
UpdateRow update =
new UpdateRow(session, table, rowId, row);
00135 session.doStep(update) ;
00136
00137
for (
int i = 0; i < num; i++) {
00138
Constraint c = table.getConstraint(i);
00139
00140 c.
applyUpdate(session, key, row, oldRow, rowId, constraint);
00141 }
00142 }
00143
00144
00145
00146
00147
00148
00149 public static void deleteRow(
Session session,
Table table,
00150
long rowId,
Row row,
00151
Constraint constraint)
00152
throws SQLException, IOException
00153 {
00154 session.getTableWriteLock(table.getName());
00155
int num = table.getNumConstraints();
00156
00157
for (
int i = 0; i < num; i++) {
00158
Constraint c = table.getConstraint(i);
00159 c.
checkDelete(session, row, rowId);
00160 }
00161
00162
for (
int i = 0; i < num; i++) {
00163
Constraint c = table.getConstraint(i);
00164 c.
applyDelete(session, row, rowId, constraint);
00165 }
00166
00167
DeletedRows d =
00168 (
DeletedRows)session.getContext(
"DeleteRow:" + table.getName(),
00169
false);
00170
if (d == null) {
00171 d =
new DeletedRows(session, table);
00172 session.putContext(
"DeleteRow:" + table.getName(),
false, d);
00173 }
00174 d.
addEntry(rowId);
00175
00176
00177
if (
Trace.bit(14)) {
00178
Debug.println(
"deleteRow(" + table.getName() +
"): " +
00179 row +
" -> " + rowId);
00180 }
00181
00182 }
00183
00184 public static Cursor getCursor(
Session session,
Relation t,
00185
SelectExpression select)
00186
throws SQLException
00187 {
00188
Cursor c = null;
00189
if (select != null) {
00190 c = select.getCursor(session, null);
00191 }
else {
00192 c = t.getCursor(session, null, null, null);
00193 }
00194
return c;
00195 }
00196
00197
00198
00199
00200 static final void holdBlobsInRow(
Session session,
00201
Table tuple,
Row row)
00202
throws SQLException, IOException
00203 {
00204
if (tuple.hasBlobs()) {
00205
final long transId = session.getTransactionId();
00206
final Database db = session.getDatabase();
00207
for (
int i = 1; i <= row.size(); i++) {
00208
Value v = row.item(i);
00209
Column col = tuple.getColumn(i);
00210
Type t = col.
getType();
00211
if (
isLargeObject(t)) {
00212
00213
ValueBlob vb = null;
00214
if (!(v instanceof
ValueBlob)) {
00215 v = t.
convert(v);
00216 row.set(i, v);
00217 }
00218
if (v instanceof
ValueBlob) {
00219 vb = (
ValueBlob)v;
00220 }
00221
if (vb != null) {
00222
if (vb.
getBytesVal() != null) {
00223 byte[] ff = vb.
getBytesVal();
00224 session.doStep(
new InsertBlob(session, vb));
00225 }
00226 session.doStep(
00227
new RefcountBlob(session, vb.
getPermBlock(), 1));
00228 }
00229 }
00230 }
00231 }
00232 }
00233
00234
00235
00236
00237 static final void freeBlobsInRow(
Session session,
Table tuple,
Row row)
00238
throws SQLException, IOException
00239 {
00240
if (row.getBlobCount() > 0) {
00241
for (
int i = 1; i <= row.size(); i++) {
00242
Value v = row.item(i);
00243
Type t = v.
getType();
00244
if (
isLargeObject(t)) {
00245
long transId = session.getTransactionId();
00246
ValueBlob vb = (
ValueBlob)v;
00247 session.doStep(
00248
new RefcountBlob(session, vb.
getPermBlock(), -1));
00249 }
00250 }
00251 }
00252 }
00253
00254
00255
00256
00257 static private final boolean isLargeObject(
Type t) {
00258
final int type = t.
getJDBCType();
00259
return type == Types.BLOB || type == Types.CLOB;
00260 }
00261
00262 }