📄 parser.cs
字号:
dDatabase.execute("DROP TABLE " + tName, cChannel);
// cream tabelul vechi cu proprietatile celui nou...
initialTable = new Table(dDatabase, true, tName,
swapTable.isCached());
for (int i = 0; i < swapTable.getColumnCount(); i++)
{
Column aColumn = swapTable.getColumn(i);
initialTable.addColumn(aColumn);
}
if (swapTable.getColumnCount()
< swapTable.getInternalColumnCount())
{
initialTable.createPrimaryKey();
}
else
{
initialTable.createPrimaryKey(swapTable.getPrimaryIndex().getColumns()[0]);
}
// endof PrimaryKey...
// sa ne farimam cu indicii... ;-((
idx = null;
while (true)
{
idx = swapTable.getNextIndex(idx);
if (idx == null)
{
break;
}
if (idx == swapTable.getPrimaryIndex())
{
continue;
}
initialTable.createIndex(idx);
}
// end of Index...
cChannel.commit();
dDatabase.linkTable(initialTable);
// end of cream...
// copiem datele din swap in tabel...
tmpTokenizer = new Tokenizer("SELECT * FROM " + swap);
pp = new Parser(dDatabase, tmpTokenizer, cChannel);
ff = tmpTokenizer.getstring();
if (!swapTable.isEmpty())
{
Record n = ((Result) pp.processSelect()).rRoot;
do
{
object[] row = initialTable.getNewRow();
object[] row1 = n.data;
for (int i = 0; i < swapTable.getColumnCount(); i++)
{
row[i] = row1[i];
}
initialTable.insertNoCheck(row, cChannel);
n = n.next;
} while (n != null);
// end of copiem...
}
dDatabase.execute("DROP TABLE " + swap, cChannel);
count = 4;
}
else
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, token);
}
}
else if (token.Equals("DELETE"))
{
token = tTokenizer.getstring();
if (token.Equals("COLUMN"))
{
Table swapTable = new Table(dDatabase, true, swap,
initialTable.isCached());
string cName = tTokenizer.getstring();
int undesired = initialTable.getColumnNr(cName);
for (int i = 0; i < initialTable.getColumnCount(); i++)
{
Column aColumn = initialTable.getColumn(i);
if (i != undesired)
{
swapTable.addColumn(aColumn);
}
}
int pKey = -1;
// !--
if (initialTable.getColumnCount()
< initialTable.getInternalColumnCount())
{
swapTable.createPrimaryKey();
}
else
{
int[] cols = initialTable.getPrimaryIndex().getColumns();
pKey = cols[0];
if ((cols[0] > undesired)
|| (cols[0] + cols.Length < undesired))
{
if (undesired
< initialTable.getPrimaryIndex().getColumns()[0])
{
// reindexarea...
for (int i = 0; i < cols.Length; i++)
{
cols[i]--;
}
// endOf reindexarea...
}
// MT: This initially wouldn't compile, missing the array index on cols[]
swapTable.createPrimaryKey(cols[0]);
}
else
{
swapTable.createPrimaryKey();
}
}
// endof PrimaryKey...
// sa ne farimam cu indicii... ;-((
Index idx = null;
while (true)
{
idx = initialTable.getNextIndex(idx);
if (idx == null)
{
break;
}
if (idx == initialTable.getPrimaryIndex())
{
continue;
}
bool flag = true;
int[] cols = idx.getColumns();
for (int i = 0; i < cols.Length; i++)
{
if (cols[i] == undesired)
{
flag = false;
}
}
if (flag)
{
Index tIdx;
for (int i = 0; i < cols.Length; i++)
{
if (cols[i] > undesired)
{
cols[i]--;
}
}
tIdx = new Index(idx.getName(), idx.getColumns(),
idx.getType(), idx.isUnique());
swapTable.createIndex(tIdx);
}
}
// !--
cChannel.commit();
dDatabase.linkTable(swapTable);
Tokenizer tmpTokenizer = new Tokenizer("SELECT * FROM "
+ tName);
Parser pp = new Parser(dDatabase, tmpTokenizer, cChannel);
string ff = tmpTokenizer.getstring();
if (!initialTable.isEmpty())
{
Record n = ((Result) pp.processSelect()).rRoot;
do
{
object[] row = swapTable.getNewRow();
object[] row1 = n.data;
int j = 0;
for (int i = 0; i < initialTable.getColumnCount();
i++)
{
if (i != undesired)
{
row[j] = row1[i];
j++;
}
}
swapTable.insertNoCheck(row, cChannel);
n = n.next;
} while (n != null);
}
dDatabase.execute("DROP TABLE " + tName, cChannel);
// cream tabelul vechi cu proprietatile celui nou...
initialTable = new Table(dDatabase, true, tName,
swapTable.isCached());
for (int i = 0; i < swapTable.getColumnCount(); i++)
{
Column aColumn = swapTable.getColumn(i);
initialTable.addColumn(aColumn);
}
// !--
if (swapTable.getColumnCount()
< swapTable.getInternalColumnCount())
{
initialTable.createPrimaryKey();
}
else
{
initialTable.createPrimaryKey(swapTable.getPrimaryIndex().getColumns()[0]);
}
// endof PrimaryKey...
// sa ne farimam cu indicii... ;-((
idx = null;
while (true)
{
idx = swapTable.getNextIndex(idx);
if (idx == null)
{
break;
}
if (idx == swapTable.getPrimaryIndex())
{
continue;
}
initialTable.createIndex(idx);
}
// end of Index...
// !--
cChannel.commit();
dDatabase.linkTable(initialTable);
// end of cream...
// copiem datele din swap in tabel...
tmpTokenizer = new Tokenizer("SELECT * FROM " + swap);
pp = new Parser(dDatabase, tmpTokenizer, cChannel);
ff = tmpTokenizer.getstring();
if (!swapTable.isEmpty())
{
Record n = ((Result) pp.processSelect()).rRoot;
do
{
object[] row = initialTable.getNewRow();
object[] row1 = n.data;
for (int i = 0; i < swapTable.getColumnCount(); i++)
{
row[i] = row1[i];
}
initialTable.insertNoCheck(row, cChannel);
n = n.next;
} while (n != null);
// end of copiem...
}
dDatabase.execute("DROP TABLE " + swap, cChannel);
count = 3;
}
else
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, token);
}
count = 3;
}
Result r = new Result();
r.iUpdateCount = count; // --> nu tre inca...??
return r;
}
/**
* Method declaration
*
*
* @return
*
* @throws Exception
*/
private Select parseSelect()
{
Select select = new Select();
// fredt@users.sourceforge.net begin changes from 1.50
select.limitStart = 0;
select.limitCount = cChannel.getMaxRows();
// fredt@users.sourceforge.net end changes from 1.50
string token = tTokenizer.getstring();
if (token.Equals("DISTINCT"))
{
select.bDistinct = true;
// fredt@users.sourceforge.net begin changes from 1.50
}
else if( token.Equals("LIMIT"))
{
string limStart = tTokenizer.getstring();
string limEnd = tTokenizer.getstring();
//System.out.println( "LIMIT used from "+limStart+","+limEnd);
select.limitStart = limStart.ToInt32();
select.limitCount = limEnd.ToInt32();
// fredt@users.sourceforge.net end changes from 1.50
}
else
{
tTokenizer.back();
}
// parse column list
ArrayList vcolumn = new ArrayList();
do
{
Expression e = parseExpression();
token = tTokenizer.getstring();
if (token.Equals("AS"))
{
e.setAlias(tTokenizer.getName());
token = tTokenizer.getstring();
}
else if (tTokenizer.wasName())
{
e.setAlias(token);
token = tTokenizer.getstring();
}
vcolumn.Add(e);
} while (token.Equals(","));
if (token.Equals("INTO"))
{
select.sIntoTable = tTokenizer.getstring();
token = tTokenizer.getstring();
}
if (!token.Equals("FROM"))
{
throw Trace.error(Trace.UNEXPECTED_TOKEN, token);
}
Expression condition = null;
// parse table list
ArrayList vfilter = new ArrayList();
vfilter.Add(parseTableFilter(false));
while (true)
{
token = tTokenizer.getstring();
if (token.Equals("LEFT"))
{
token = tTokenizer.getstring();
if (token.Equals("OUTER"))
{
token = tTokenizer.getstring();
}
Trace.check(token.Equals("JOIN"), Trace.UNEXPECTED_TOKEN,
token);
vfilter.Add(parseTableFilter(true));
tTokenizer.getThis("ON");
condition = addCondition(condition, parseExpression());
}
else if (token.Equals("INNER"))
{
tTokenizer.getThis("JOIN");
vfilter.Add(parseTableFilter(false));
tTokenizer.getThis("ON");
condition = addCondition(condition, parseExpression());
}
else if (token.Equals(","))
{
vfilter.Add(parseTableFilter(false));
}
else
{
break;
}
}
tTokenizer.back();
int len = vfilter.Count;
TableFilter[] filter = new TableFilter[len];
vfilter.CopyTo(filter);
select.tFilter = filter;
// expand [table.]* columns
len = vcolumn.Count;
for (int i = 0; i < len; i++)
{
Expression e = (Expression) (vcolumn[i]);
if (e.getType() == Expression.ASTERIX)
{
int current = i;
Table table = null;
string n = e.getTableName();
for (int t = 0; t < filter.Length; t++)
{
TableFilter f = filter[t];
e.resolve(f);
if (n != null &&!n.Equals(f.getName()))
{
continue;
}
table = f.getTable();
int col = table.getColumnCount();
for (int c = 0; c < col; c++)
{
Expression ins =
new Expression(f.getName(),
table.getColumnName(c));
vcolumn.Insert(current++,ins);
// now there is one element more to parse
len++;
}
}
Trace.check(table != null, Trace.TABLE_NOT_FOUND, n);
// minus the asterix element
len--;
vcolumn.RemoveAt(current);
}
else if (e.getType()==Expression.COLUMN)
{
if (e.getTableName() == null)
{
for (int filterIndex=0; filterIndex < filter.Length; filterIndex++)
{
e.resolve(filter[filterIndex]);
}
}
}
}
select.iResultLen = len;
// where
token = tTokenizer.getstring();
if (token.Equals("WHERE"))
{
condition = addCondition(condition, parseExpression());
token = tTokenizer.getstring();
}
select.eCondition = condition;
if (token.Equals("GROUP"))
{
tTokenizer.getThis("BY");
len = 0;
do
{
vcolumn.Add(parseExpression());
token = tTokenizer.getstring();
len++;
} while (token.Equals(","));
select.iGroupLen = len;
}
if (token.Equals("ORDER"))
{
tTokenizer.getThis("BY");
len = 0;
do
{
Expression e = parseExpression();
if (e.getType() == Expression.VALUE)
{
// order by 1,2,3
if (e.getDataType() == Column.INTEGER)
{
int i = ((int) e.getValue()).ToInt32();
e = (Expression) vcolumn[i - 1];
}
}
else if (e.getType() == Expression.COLUMN
&& e.getTableName() == null)
{
// this could be an alias column
string s = e.getColumnName();
for (int i = 0; i < vcolumn.Count; i++)
{
Expression ec = (Expression) vcolumn[i];
if (s.Equals(ec.getAlias()))
{
e = ec;
break;
}
}
}
token = tTokenizer.getstring();
if (token.Equals("DESC"))
{
e.setDescending();
token = tTokenizer.getstring();
}
else if (token.Equals("ASC"))
{
token = tTokenizer.getstring();
}
vcolumn.Add(e);
len++;
} while (token.Equals(","));
select.iOrderLen = len;
}
len = vcolumn.Count;
select.eColumn = new Expression[len];
vcolumn.CopyTo(select.eColumn);
if (token.Equals("UNION"))
{
token = tTokenizer.getstring();
if (token.Equals("ALL"))
{
select.iUnionType = Select.UNIONALL;
}
else
{
select.iUnionType = Select.UNION;
tTokenizer.back();
}
tTokenizer.getThis("SELECT");
select.sUnion = parseSelect();
}
else if (token.Equals("INTERSECT"))
{
tTokenizer.getThis("SELECT");
select.iUnionType = Select.INTERSECT;
select.sUnion = parseSelect();
}
else if (token.Equals("EXCEPT") || token.Equals("MINUS"))
{
tTokenizer.getThis("SELECT");
select.iUnionType = Select.EXCEPT;
select.sUnion = parseSelect();
}
else
{
tTokenizer.back();
}
return select;
}
/**
* Method declaration
*
*
* @param outerjoin
*
* @return
*
* @throws Exception
*/
private TableFilter parseTableFilter(bool outerjoin)
{
string token = tTokenizer.getstring();
Table t = null;
if (token.Equals("("))
{
tTokenizer.getThis("SELECT");
Select s = parseSelect();
Result r = s.getResult(0);
// it's not a problem that this table has not a unique name
t = new Table(dDatabase, false, "SYSTEM_SUBQUERY", false);
tTokenizer.getThis(")");
t.addColumns(r);
t.createPrimaryKey();
// subquery creation can't fail because of violation of primary key
t.insert(r, cChannel);
}
else
{
cChannel.check(token, Access.SELECT);
t = dDatabase.getTable(token, cChannel);
}
string sAlias = null;
token = tTokenizer.getstring();
if (token.Equals("AS"))
{
sAlias = tTokenizer.getName();
}
else if (tTokenizer.wasName())
{
sAlias = token;
}
else
{
tTokenizer.back();
}
return new TableFilter(t, sAlias, outerjoin);
}
/**
* Method declaration
*
*
* @param e1
* @param e2
*
* @return
*/
private Expression addCondition(Expression e1, Expression e2)
{
if (e1 == null)
{
return e2;
}
else if (e2 == null)
{
return e1;
}
else
{
return new Expression(Expression.AND, e1, e2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -