use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.
the class GrantRevoke method setGranteeName.
public void setGranteeName(String granteeName) {
Database db = session.getDatabase();
grantee = db.findUser(granteeName);
if (grantee == null) {
grantee = db.findRole(granteeName);
if (grantee == null) {
throw DbException.get(ErrorCode.USER_OR_ROLE_NOT_FOUND_1, granteeName);
}
}
}
use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.
the class Explain method query.
@Override
public ResultInterface query(int maxrows) {
Column column = new Column("PLAN", Value.STRING);
Database db = session.getDatabase();
ExpressionColumn expr = new ExpressionColumn(db, column);
Expression[] expressions = { expr };
result = new LocalResult(session, expressions, 1);
if (maxrows >= 0) {
String plan;
if (executeCommand) {
if (command.isQuery()) {
command.query(maxrows);
} else {
command.update();
}
plan = command.getPlanSQL();
} else {
plan = command.getPlanSQL();
}
add(plan);
}
result.done();
return result;
}
use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.
the class Query method initOrder.
/**
* Initialize the order by list. This call may extend the expressions list.
*
* @param session the session
* @param expressions the select list expressions
* @param expressionSQL the select list SQL snippets
* @param orderList the order by list
* @param visible the number of visible columns in the select list
* @param mustBeInResult all order by expressions must be in the select list
* @param filters the table filters
*/
static void initOrder(Session session, ArrayList<Expression> expressions, ArrayList<String> expressionSQL, ArrayList<SelectOrderBy> orderList, int visible, boolean mustBeInResult, ArrayList<TableFilter> filters) {
Database db = session.getDatabase();
for (SelectOrderBy o : orderList) {
Expression e = o.expression;
if (e == null) {
continue;
}
// special case: SELECT 1 AS A FROM DUAL ORDER BY A
// (oracle supports it, but only in order by, not in group by and
// not in having):
// SELECT 1 AS A FROM DUAL ORDER BY -A
boolean isAlias = false;
int idx = expressions.size();
if (e instanceof ExpressionColumn) {
// order by expression
ExpressionColumn exprCol = (ExpressionColumn) e;
String tableAlias = exprCol.getOriginalTableAliasName();
String col = exprCol.getOriginalColumnName();
for (int j = 0; j < visible; j++) {
boolean found = false;
Expression ec = expressions.get(j);
if (ec instanceof ExpressionColumn) {
// select expression
ExpressionColumn c = (ExpressionColumn) ec;
found = db.equalsIdentifiers(col, c.getColumnName());
if (found && tableAlias != null) {
String ca = c.getOriginalTableAliasName();
if (ca == null) {
found = false;
if (filters != null) {
// select id from test order by test.id
for (int i = 0, size = filters.size(); i < size; i++) {
TableFilter f = filters.get(i);
if (db.equalsIdentifiers(f.getTableAlias(), tableAlias)) {
found = true;
break;
}
}
}
} else {
found = db.equalsIdentifiers(ca, tableAlias);
}
}
} else if (!(ec instanceof Alias)) {
continue;
} else if (tableAlias == null && db.equalsIdentifiers(col, ec.getAlias())) {
found = true;
} else {
Expression ec2 = ec.getNonAliasExpression();
if (ec2 instanceof ExpressionColumn) {
ExpressionColumn c2 = (ExpressionColumn) ec2;
String ta = exprCol.getSQL();
String tb = c2.getSQL();
String s2 = c2.getColumnName();
found = db.equalsIdentifiers(col, s2);
if (!db.equalsIdentifiers(ta, tb)) {
found = false;
}
}
}
if (found) {
idx = j;
isAlias = true;
break;
}
}
} else {
String s = e.getSQL();
if (expressionSQL != null) {
for (int j = 0, size = expressionSQL.size(); j < size; j++) {
String s2 = expressionSQL.get(j);
if (db.equalsIdentifiers(s2, s)) {
idx = j;
isAlias = true;
break;
}
}
}
}
if (!isAlias) {
if (mustBeInResult) {
throw DbException.get(ErrorCode.ORDER_BY_NOT_IN_RESULT, e.getSQL());
}
expressions.add(e);
String sql = e.getSQL();
expressionSQL.add(sql);
}
o.columnIndexExpr = ValueExpression.get(ValueInt.get(idx + 1));
Expression expr = expressions.get(idx).getNonAliasExpression();
o.expression = expr;
}
}
use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.
the class Comparison method getAdditional.
/**
* Get an additional condition if possible. Example: given two conditions
* A=B AND B=C, the new condition A=C is returned. Given the two conditions
* A=1 OR A=2, the new condition A IN(1, 2) is returned.
*
* @param session the session
* @param other the second condition
* @param and true for AND, false for OR
* @return null or the third condition
*/
Expression getAdditional(Session session, Comparison other, boolean and) {
if (compareType == other.compareType && compareType == EQUAL) {
boolean lc = left.isConstant();
boolean rc = right.isConstant();
boolean l2c = other.left.isConstant();
boolean r2c = other.right.isConstant();
String l = left.getSQL();
String l2 = other.left.getSQL();
String r = right.getSQL();
String r2 = other.right.getSQL();
if (and) {
// must not compare constants. example: NOT(B=2 AND B=3)
if (!(rc && r2c) && l.equals(l2)) {
return new Comparison(session, EQUAL, right, other.right);
} else if (!(rc && l2c) && l.equals(r2)) {
return new Comparison(session, EQUAL, right, other.left);
} else if (!(lc && r2c) && r.equals(l2)) {
return new Comparison(session, EQUAL, left, other.right);
} else if (!(lc && l2c) && r.equals(r2)) {
return new Comparison(session, EQUAL, left, other.left);
}
} else {
// a=b OR a=c
Database db = session.getDatabase();
if (rc && r2c && l.equals(l2)) {
return new ConditionIn(db, left, New.arrayList(Arrays.asList(right, other.right)));
} else if (rc && l2c && l.equals(r2)) {
return new ConditionIn(db, left, New.arrayList(Arrays.asList(right, other.left)));
} else if (lc && r2c && r.equals(l2)) {
return new ConditionIn(db, right, New.arrayList(Arrays.asList(left, other.right)));
} else if (lc && l2c && r.equals(r2)) {
return new ConditionIn(db, right, New.arrayList(Arrays.asList(left, other.left)));
}
}
}
return null;
}
use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.
the class Command method executeQuery.
/**
* Execute a query and return the result.
* This method prepares everything and calls {@link #query(int)} finally.
*
* @param maxrows the maximum number of rows to return
* @param scrollable if the result set must be scrollable (ignored)
* @return the result set
*/
@Override
public ResultInterface executeQuery(int maxrows, boolean scrollable) {
startTime = 0;
Database database = session.getDatabase();
Object sync = session;
boolean callStop = true;
synchronized (sync) {
session.setCurrentCommand(this);
try {
while (true) {
try {
return query(maxrows);
} catch (DbException e) {
throw e;
} catch (OutOfMemoryError e) {
callStop = false;
// there is a serious problem:
// the transaction may be applied partially
// in this case we need to panic:
// close the database
database.shutdownImmediately();
throw DbException.convert(e);
} catch (Throwable e) {
throw DbException.convert(e);
}
}
} catch (DbException e) {
e = e.addSQL(sql);
SQLException s = e.getSQLException();
if (s.getErrorCode() == ErrorCode.OUT_OF_MEMORY) {
callStop = false;
database.shutdownImmediately();
throw e;
}
throw e;
} finally {
if (callStop) {
stop();
}
}
}
}
Aggregations