Search in sources :

Example 1 with ResultSetIterator

use of org.whole.lang.sql.iterators.ResultSetIterator in project whole by wholeplatform.

the class SQLInterpreterVisitor method visit.

@Override
public void visit(SQLStatement entity) {
    try {
        IVisitor ov = getOperation().setVisitor(entity, 0, getOperation().getVisitor(entity, 1));
        getOperation().stagedVisit(entity);
        getOperation().setVisitor(entity, 0, ov);
        entity = (SQLStatement) getResult();
        String sql = toPrettyPrintString(entity);
        if (batchMode)
            statement.addBatch(sql);
        else {
            String[] names;
            if (entity.wGetEntityDescriptor().equals(SQLEntityDescriptorEnum.Select)) {
                IEntity columnExprs = entity.wGet(SQLFeatureDescriptorEnum.columnExprs);
                int size = columnExprs.wSize();
                names = new String[size];
                for (int i = 0; i < size; i++) {
                    IEntity columnExpr = columnExprs.wGet(i);
                    if (Matcher.matchImpl(SQLEntityDescriptorEnum.ColumnExpression, columnExpr)) {
                        IEntity alias = columnExpr.wGet(SQLFeatureDescriptorEnum.alias);
                        if (DataTypeUtils.getDataKind(alias).isString())
                            names[i] = alias.wStringValue();
                        else {
                            IEntity expr = columnExpr.wGet(SQLFeatureDescriptorEnum.expression);
                            if (Matcher.matchImpl(SQLEntityDescriptorEnum.ColumnName, expr))
                                names[i] = expr.wStringValue();
                        }
                    }
                }
            } else
                names = new String[0];
            if (statement.execute(sql))
                setResultIterator(new ResultSetIterator(statement.getResultSet(), names));
            else {
                setResult(null);
                statement.close();
            }
        }
    } catch (Exception e) {
        try {
            statement.close();
        } catch (Exception e1) {
        }
        throw new VisitException(SQL_INTERPRETER_ERROR_MESSAGE, e);
    } finally {
        if (!batchMode)
            statement = null;
    }
}
Also used : ResultSetIterator(org.whole.lang.sql.iterators.ResultSetIterator) IVisitor(org.whole.lang.visitors.IVisitor) IEntity(org.whole.lang.model.IEntity) VisitException(org.whole.lang.visitors.VisitException) PrettyPrinterOperation.toPrettyPrintString(org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString) VisitException(org.whole.lang.visitors.VisitException)

Aggregations

IEntity (org.whole.lang.model.IEntity)1 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)1 ResultSetIterator (org.whole.lang.sql.iterators.ResultSetIterator)1 IVisitor (org.whole.lang.visitors.IVisitor)1 VisitException (org.whole.lang.visitors.VisitException)1