📄 table.java
字号:
};
createIndex(col, "SYSTEM_PK", true);
}
/**
* Method declaration
*
*
* @throws SQLException
*/
void createPrimaryKey() throws SQLException {
Trace.assert(iPrimaryKey == -1, "Table.createPrimaryKey");
addColumn("SYSTEM_ID", Column.INTEGER, true, true);
createPrimaryKey(iColumnCount - 1);
iVisibleColumns = iColumnCount - 1;
}
/**
* Method declaration
*
*
* @param index
*
* @throws SQLException
*/
void createIndex(Index index) throws SQLException {
createIndex(index.getColumns(), index.getName(), index.isUnique());
}
/**
* Method declaration
*
*
* @param column
* @param name
* @param unique
*
* @throws SQLException
*/
void createIndex(int column[], String name,
boolean unique) throws SQLException {
Trace.assert(iPrimaryKey != -1, "createIndex");
for (int i = 0; i < iIndexCount; i++) {
Index index = getIndex(i);
if (name.equals(index.getName())) {
throw Trace.error(Trace.INDEX_ALREADY_EXISTS);
}
}
int s = column.length;
// The primary key field is added for non-unique indexes
// making all indexes unique
int col[] = new int[unique ? s : s + 1];
int type[] = new int[unique ? s : s + 1];
for (int j = 0; j < s; j++) {
col[j] = column[j];
type[j] = getColumn(col[j]).iType;
}
if (!unique) {
col[s] = iPrimaryKey;
type[s] = getColumn(iPrimaryKey).iType;
}
Index index = new Index(name, col, type, unique);
if (iIndexCount != 0) {
Trace.assert(isEmpty(), "createIndex");
}
vIndex.addElement(index);
iIndexCount++;
}
/**
* Method declaration
*
*
* @param index
*
* @throws SQLException
*/
void checkDropIndex(String index) throws SQLException {
for (int i = 0; i < iIndexCount; i++) {
if (index.equals(getIndex(i).getName())) {
Trace.check(i != 0, Trace.DROP_PRIMARY_KEY);
return;
}
}
throw Trace.error(Trace.INDEX_NOT_FOUND, index);
}
/**
* Method declaration
*
*
* @return
*/
boolean isEmpty() {
return getIndex(0).getRoot() == null;
}
/**
* Method declaration
*
*
* @return
*/
Object[] getNewRow() {
return new Object[iColumnCount];
}
/**
* Method declaration
*
*
* @param from
*
* @throws SQLException
*/
void moveData(Table from) throws SQLException {
Index index = from.getPrimaryIndex();
Node n = index.first();
while (n != null) {
if (Trace.STOP) {
Trace.stop();
}
Object o[] = n.getData();
insertNoCheck(o, null);
n = index.next(n);
}
index = getPrimaryIndex();
n = index.first();
while (n != null) {
if (Trace.STOP) {
Trace.stop();
}
Object o[] = n.getData();
from.deleteNoCheck(o, null);
n = index.next(n);
}
}
/**
* Method declaration
*
*
* @param col
* @param deleted
* @param inserted
*
* @throws SQLException
*/
void checkUpdate(int col[], Result deleted,
Result inserted) throws SQLException {
if (dDatabase.isReferentialIntegrity()) {
for (int i = 0; i < iConstraintCount; i++) {
Constraint v = (Constraint) vConstraint.elementAt(i);
v.checkUpdate(col, deleted, inserted);
}
}
}
/**
* Method declaration
*
*
* @param result
* @param c
*
* @throws SQLException
*/
void insert(Result result, Channel c) throws SQLException {
// if violation of constraints can occur, insert must be rolled back
// outside of this function!
Record r = result.rRoot;
int len = result.getColumnCount();
while (r != null) {
Object row[] = getNewRow();
for (int i = 0; i < len; i++) {
row[i] = r.data[i];
}
insert(row, c);
r = r.next;
}
}
/**
* Method declaration
*
*
* @param row
* @param c
*
* @throws SQLException
*/
void insert(Object row[], Channel c) throws SQLException {
fireAll(TriggerDef.INSERT_BEFORE, row);
if (dDatabase.isReferentialIntegrity()) {
for (int i = 0; i < iConstraintCount; i++) {
((Constraint) vConstraint.elementAt(i)).checkInsert(row);
}
}
insertNoCheck(row, c);
fireAll(TriggerDef.INSERT_AFTER, row);
}
/**
* Method declaration
*
*
* @param row
* @param c
*
* @throws SQLException
*/
void insertNoCheck(Object row[], Channel c) throws SQLException {
insertNoCheck(row, c, true);
}
/**
* Method declaration
*
*
* @param row
* @param c
* @param log
*
* @throws SQLException
*/
void insertNoCheck(Object row[], Channel c,
boolean log) throws SQLException {
if (iIdentityColumn != -1) {
Integer id = (Integer) row[iIdentityColumn];
if (id == null) {
if (c != null) {
c.setLastIdentity(iIdentityId);
}
row[iIdentityColumn] = new Integer(iIdentityId++);
} else {
int i = id.intValue();
if (iIdentityId <= i) {
if (c != null) {
c.setLastIdentity(i);
}
iIdentityId = i + 1;
}
}
}
for (int i = 0; i < iColumnCount; i++) {
if (row[i] == null &&!getColumn(i).isNullable()) {
throw Trace.error(Trace.TRY_TO_INSERT_NULL);
}
}
int i = 0;
try {
Row r = new Row(this, row);
for (; i < iIndexCount; i++) {
Node n = r.getNode(i);
getIndex(i).insert(n);
}
} catch (SQLException e) { // rollback insert
for (--i; i >= 0; i--) {
getIndex(i).delete(row, i == 0);
}
throw e; // and throw error again
}
if (c != null) {
c.addTransactionInsert(this, row);
}
if (lLog != null) {
lLog.write(c, getInsertStatement(row));
}
}
/**
* Method declaration
*
*
* @param trigVecIndx
* @param row
*/
void fireAll(int trigVecIndx, Object row[]) {
if (!dDatabase.isReferentialIntegrity()) { // reloading db
return;
}
Vector trigVec = vTrigs[trigVecIndx];
int trCount = trigVec.size();
for (int i = 0; i < trCount; i++) {
TriggerDef td = (TriggerDef) trigVec.elementAt(i);
td.push(row); // tell the trigger thread to fire with this row
}
}
// statement-level triggers
/**
* Method declaration
*
*
* @param trigVecIndx
*/
void fireAll(int trigVecIndx) {
Object row[] = new Object[1];
row[0] = new String("Statement-level");
fireAll(trigVecIndx, row);
}
/**
* Method declaration
*
*
* @param trigDef
*/
void addTrigger(TriggerDef trigDef) {
if (Trace.TRACE) {
Trace.trace("Trigger added "
+ String.valueOf(trigDef.vectorIndx));
}
vTrigs[trigDef.vectorIndx].addElement(trigDef);
}
/**
* Method declaration
*
*
* @param row
* @param c
*
* @throws SQLException
*/
void delete(Object row[], Channel c) throws SQLException {
fireAll(TriggerDef.DELETE_BEFORE_ROW, row);
if (dDatabase.isReferentialIntegrity()) {
for (int i = 0; i < iConstraintCount; i++) {
((Constraint) vConstraint.elementAt(i)).checkDelete(row);
}
}
deleteNoCheck(row, c);
// fire the delete after statement trigger
fireAll(TriggerDef.DELETE_AFTER_ROW, row);
}
/**
* Method declaration
*
*
* @param row
* @param c
*
* @throws SQLException
*/
void deleteNoCheck(Object row[], Channel c) throws SQLException {
deleteNoCheck(row, c, true);
}
/**
* Method declaration
*
*
* @param row
* @param c
* @param log
*
* @throws SQLException
*/
void deleteNoCheck(Object row[], Channel c,
boolean log) throws SQLException {
for (int i = 1; i < iIndexCount; i++) {
getIndex(i).delete(row, false);
}
// must delete data last
getIndex(0).delete(row, true);
if (c != null) {
c.addTransactionDelete(this, row);
}
if (lLog != null) {
lLog.write(c, getDeleteStatement(row));
}
}
/**
* Method declaration
*
*
* @param row
*
* @return
*
* @throws SQLException
*/
String getInsertStatement(Object row[]) throws SQLException {
StringBuffer a = new StringBuffer("INSERT INTO ");
a.append(getName());
a.append(" VALUES(");
for (int i = 0; i < iVisibleColumns; i++) {
a.append(Column.createString(row[i], getColumn(i).iType));
a.append(',');
}
a.setCharAt(a.length() - 1, ')');
return a.toString();
}
/**
* Method declaration
*
*
* @return
*/
boolean isCached() {
return bCached;
}
/**
* Method declaration
*
*
* @param s
*
* @return
*/
Index getIndex(String s) {
for (int i = 0; i < iIndexCount; i++) {
Index h = getIndex(i);
if (s.equals(h.getName())) {
return h;
}
}
// no such index
return null;
}
/**
* Method declaration
*
*
* @param i
*
* @return
*/
Column getColumn(int i) {
return (Column) vColumn.elementAt(i);
}
/**
* Method declaration
*
*
* @param i
*
* @return
*/
private Index getIndex(int i) {
return (Index) vIndex.elementAt(i);
}
/**
* Method declaration
*
*
* @param row
*
* @return
*
* @throws SQLException
*/
private String getDeleteStatement(Object row[]) throws SQLException {
StringBuffer a = new StringBuffer("DELETE FROM ");
a.append(sName);
a.append(" WHERE ");
if (iVisibleColumns < iColumnCount) {
for (int i = 0; i < iVisibleColumns; i++) {
a.append(getColumn(i).sName);
a.append('=');
a.append(Column.createString(row[i], getColumn(i).iType));
if (i < iVisibleColumns - 1) {
a.append(" AND ");
}
}
} else {
a.append(getColumn(iPrimaryKey).sName);
a.append("=");
a.append(Column.createString(row[iPrimaryKey],
getColumn(iPrimaryKey).iType));
}
return a.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -