📄 database.cs
字号:
c.getThis("REFERENCES");
Table t2 = getTable(c.getstring(), channel);
int[] col2 = processColumnList(c, t2);
if (t.getIndexForColumns(col) == null)
{
createIndex(channel, t, col, "SYSTEM_FOREIGN_KEY_" + name, false);
}
if (t2.getIndexForColumns(col2) == null)
{
createIndex(channel, t2, col2, "SYSTEM_REFERENCE_" + name, false);
}
t.addConstraint(new Constraint(Constraint.FOREIGN_KEY, t2, t, col2,
col));
t2.addConstraint(new Constraint(Constraint.MAIN, t2, t, col2, col));
}
/**
* Method declaration
*
*
* @param c
* @param channel
* @param name
* @param t
*
* @throws Exception
*/
private void addUniqueConstraintOn(Tokenizer c, Channel channel,
string name,
Table t)
{
int[] col = processColumnList(c, t);
createIndex(channel, t, col, name, true);
t.addConstraint(new Constraint(Constraint.UNIQUE, t, col));
}
/**
* Method declaration
*
*
* @param c
* @param channel
* @param name
* @param t
* @param unique
*
* @throws Exception
*/
private void addIndexOn(Tokenizer c, Channel channel, string name,
Table t, bool unique)
{
int[] col = processColumnList(c, t);
createIndex(channel, t, col, name, unique);
}
/**
* Method declaration
*
*
* @param c
* @param channel
* @param cached
*
* @throws Exception
*/
private void processCreateTable(Tokenizer c, Channel channel,
bool cached)
{
Table t;
string sToken = c.getName();
if (cached && lLog != null)
{
t = new Table(this, true, sToken, true);
}
else
{
t = new Table(this, true, sToken, false);
}
c.getThis("(");
int primarykeycolumn = -1;
int column = 0;
bool constraint = false;
while (true)
{
bool identity = false;
sToken = c.getstring();
if (sToken.Equals("CONSTRAINT") || sToken.Equals("PRIMARY")
|| sToken.Equals("FOREIGN") || sToken.Equals("UNIQUE"))
{
c.back();
constraint = true;
break;
}
string sColumn = sToken;
int iType = Column.getTypeNr(c.getstring());
if (iType == Column.VARCHAR && bIgnoreCase)
{
iType = Column.VARCHAR_IGNORECASE;
}
sToken = c.getstring();
if (iType == Column.DOUBLE && sToken.Equals("PRECISION"))
{
sToken = c.getstring();
}
if (sToken.Equals("("))
{
// overread length
do
{
sToken = c.getstring();
} while (!sToken.Equals(")"));
sToken = c.getstring();
}
bool nullable = true;
if (sToken.Equals("NULL"))
{
sToken = c.getstring();
}
else if (sToken.Equals("NOT"))
{
c.getThis("NULL");
nullable = false;
sToken = c.getstring();
}
if (sToken.Equals("IDENTITY"))
{
identity = true;
Trace.check(primarykeycolumn == -1, Trace.SECOND_PRIMARY_KEY,
sColumn);
sToken = c.getstring();
primarykeycolumn = column;
}
if (sToken.Equals("PRIMARY"))
{
c.getThis("KEY");
Trace.check(identity || primarykeycolumn == -1,
Trace.SECOND_PRIMARY_KEY, sColumn);
primarykeycolumn = column;
sToken = c.getstring();
}
t.addColumn(sColumn, iType, nullable, identity);
if (sToken.Equals(")"))
{
break;
}
if (!sToken.Equals(","))
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
}
column++;
}
if (primarykeycolumn != -1)
{
t.createPrimaryKey(primarykeycolumn);
}
else
{
t.createPrimaryKey();
}
if (constraint)
{
int i = 0;
while (true)
{
sToken = c.getstring();
string name = "SYSTEM_CONSTRAINT" + i;
i++;
if (sToken.Equals("CONSTRAINT"))
{
name = c.getstring();
sToken = c.getstring();
}
if (sToken.Equals("PRIMARY"))
{
c.getThis("KEY");
addUniqueConstraintOn(c, channel, name, t);
}
else if (sToken.Equals("UNIQUE"))
{
addUniqueConstraintOn(c, channel, name, t);
}
else if (sToken.Equals("FOREIGN"))
{
c.getThis("KEY");
addForeignKeyOn(c, channel, name, t);
}
sToken = c.getstring();
if (sToken.Equals(")"))
{
break;
}
if (!sToken.Equals(","))
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
}
}
}
channel.commit();
linkTable(t);
}
/**
* Method declaration
*
*
* @param c
* @param channel
*
* @return
*
* @throws Exception
*/
private Result processDrop(Tokenizer c,
Channel channel)
{
channel.checkReadWrite();
channel.checkAdmin();
string sToken = c.getstring();
if (sToken.Equals("TABLE"))
{
sToken = c.getstring();
if (sToken.Equals("IF"))
{
sToken = c.getstring(); // EXISTS
sToken = c.getstring(); // <table>
dropTable(sToken, true);
}
else
{
dropTable(sToken, false);
}
channel.commit();
}
else if (sToken.Equals("USER"))
{
aAccess.dropUser(c.getstringToken());
}
else if (sToken.Equals("INDEX"))
{
sToken = c.getstring();
if (!c.wasLongName())
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
}
string table = c.getLongNameFirst();
string index = c.getLongNameLast();
Table t = getTable(table, channel);
t.checkDropIndex(index);
Table tn = t.moveDefinition(index);
tn.moveData(t);
dropTable(table);
linkTable(tn);
channel.commit();
}
else
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
}
return new Result();
}
/**
* Method declaration
*
*
* @param c
* @param channel
* @param grant
*
* @return
*
* @throws Exception
*/
private Result processGrantOrRevoke(Tokenizer c, Channel channel,
bool grant)
{
channel.checkReadWrite();
channel.checkAdmin();
int right = 0;
string sToken;
do
{
string sRight = c.getstring();
right |= Access.getRight(sRight);
sToken = c.getstring();
} while (sToken.Equals(","));
if (!sToken.Equals("ON"))
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
}
string table = c.getstring();
if (table.Equals("CLASS"))
{
// object is saved as 'CLASS "java.lang.Math"'
// tables like 'CLASS "xy"' should not be created
table += " \"" + c.getstring() + "\"";
}
else
{
getTable(table, channel); // to make sure the table exists
}
c.getThis("TO");
string user = c.getstringToken();
// string command;
if (grant)
{
aAccess.grant(user, table, right);
// command = "GRANT";
}
else
{
aAccess.revoke(user, table, right);
// command = "REVOKE";
}
return new Result();
}
/**
* Method declaration
*
*
* @param c
* @param channel
*
* @return
*
* @throws Exception
*/
private Result processConnect(Tokenizer c,
Channel channel)
{
c.getThis("USER");
string username = c.getstringToken();
c.getThis("PASSWORD");
string password = c.getstringToken();
User user = aAccess.getUser(username, password);
channel.commit();
channel.setUser(user);
return new Result();
}
/**
* Method declaration
*
*
* @param c
* @param channel
*
* @return
*
* @throws Exception
*/
private Result processDisconnect(Tokenizer c,
Channel channel)
{
if (!channel.isClosed())
{
channel.disconnect();
cChannel.Insert(channel.getId(),null);
}
return new Result();
}
/**
* Method declaration
*
*
* @param c
* @param channel
*
* @return
*
* @throws Exception
*/
private Result processSet(Tokenizer c,
Channel channel)
{
string sToken = c.getstring();
if (sToken.Equals("PASSWORD"))
{
channel.checkReadWrite();
channel.setPassword(c.getstringToken());
}
else if (sToken.Equals("READONLY"))
{
channel.commit();
channel.setReadOnly(processTrueOrFalse(c));
}
else if (sToken.Equals("LOGSIZE"))
{
channel.checkAdmin();
int i = Int32.FromString(c.getstring());
if (lLog != null)
{
lLog.setLogSize(i);
}
}
else if (sToken.Equals("IGNORECASE"))
{
channel.checkAdmin();
bIgnoreCase = processTrueOrFalse(c);
}
else if (sToken.Equals("MAXROWS"))
{
int i = Int32.FromString(c.getstring());
channel.setMaxRows(i);
}
else if (sToken.Equals("AUTOCOMMIT"))
{
channel.setAutoCommit(processTrueOrFalse(c));
}
else if (sToken.Equals("TABLE"))
{
channel.checkReadWrite();
channel.checkAdmin();
Table t = getTable(c.getstring(), channel);
c.getThis("INDEX");
c.getstring();
t.setIndexRoots((string) c.getAsValue());
}
else if (sToken.Equals("REFERENCIAL_INTEGRITY")
|| sToken.Equals("REFERENTIAL_INTEGRITY"))
{
channel.checkAdmin();
bReferentialIntegrity = processTrueOrFalse(c);
}
else if (sToken.Equals("WRITE_DELAY"))
{
channel.checkAdmin();
bool delay = processTrueOrFalse(c);
if (lLog != null)
{
lLog.setWriteDelay(delay);
}
}
else
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
}
return new Result();
}
/**
* Method declaration
*
*
* @param c
*
* @return
*
* @throws Exception
*/
private bool processTrueOrFalse(Tokenizer c)
{
string sToken = c.getstring();
if (sToken.Equals("TRUE"))
{
return true;
}
else if (sToken.Equals("FALSE"))
{
return false;
}
else
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, sToken);
}
}
/**
* Method declaration
*
*
* @param c
* @param channel
*
* @return
*
* @throws Exception
*/
private Result processCommit(Tokenizer c,
Channel channel)
{
string sToken = c.getstring();
if (!sToken.Equals("WORK"))
{
c.back();
}
channel.commit();
return new Result();
}
/**
* Method declaration
*
*
* @param c
* @param channel
*
* @return
*
* @throws Exception
*/
private Result processRollback(Tokenizer c,
Channel channel)
{
string sToken = c.getstring();
if (!sToken.Equals("WORK"))
{
c.back();
}
channel.rollback();
return new Result();
}
/**
* Method declaration
*
*/
public void finalize()
{
try
{
close(0);
}
catch (Exception e)
{
// it's too late now
}
}
/**
* Method declaration
*
*
* @param type
*
* @throws Exception
*/
private void close(int type)
{
if (lLog == null)
{
return;
}
lLog.stop();
if (type == -1)
{
lLog.shutdown();
}
else if (type == 0)
{
lLog.close(false);
}
else if (type == 1)
{
lLog.close(true);
}
lLog = null;
bShutdown = true;
}
/**
* Method declaration
*
*
* @param c
* @param channel
*
* @return
*
* @throws Exception
*/
private Result processShutdown(Tokenizer c,
Channel channel)
{
channel.checkAdmin();
// don't disconnect system user; need it to save database
for (int i = 1; i < cChannel.Count; i++)
{
Channel d = (Channel) cChannel[i];
if (d != null)
{
d.disconnect();
}
}
cChannel.Clear();
string token = c.getstring();
if (token.Equals("IMMEDIATELY"))
{
close(-1);
}
else if (token.Equals("COMPACT"))
{
close(1);
}
else
{
c.back();
close(0);
}
processDisconnect(c, channel);
return new Result();
}
/**
* Method declaration
*
*
* @param channel
*
* @return
*
* @throws Exception
*/
private Result processCheckpoint(Channel channel)
{
channel.checkAdmin();
if (lLog != null)
{
lLog.checkpoint();
}
return new Result();
}
/**
/**
* Method declaration
*
*
* @param name
*
* @throws Exception
*/
private void dropTable(string name)
{
dropTable(name, false);
}
/**
* Method declaration
*
*
* @param name
* @param bExists
*
* @throws Exception
*/
private void dropTable(string name, bool bExists)
{
for (int i = 0; i < tTable.Count; i++)
{
Table o = (Table) tTable[i];
if (o.getName().Equals(name))
{
tTable.RemoveAt(i);
return;
}
}
if (!bExists)
{
throw Trace.error(Trace.TABLE_NOT_FOUND, name);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -