Search in sources :

Example 1 with Prepare

use of io.trino.sql.tree.Prepare in project trino by trinodb.

the class PrepareTask method execute.

@Override
public ListenableFuture<Void> execute(Prepare prepare, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Statement statement = prepare.getStatement();
    if ((statement instanceof Prepare) || (statement instanceof Execute) || (statement instanceof Deallocate)) {
        String type = statement.getClass().getSimpleName().toUpperCase(ENGLISH);
        throw new TrinoException(NOT_SUPPORTED, "Invalid statement type for prepared statement: " + type);
    }
    String sql = getFormattedSql(statement, sqlParser);
    stateMachine.addPreparedStatement(prepare.getName().getValue(), sql);
    return immediateVoidFuture();
}
Also used : Execute(io.trino.sql.tree.Execute) Deallocate(io.trino.sql.tree.Deallocate) Statement(io.trino.sql.tree.Statement) Prepare(io.trino.sql.tree.Prepare) TrinoException(io.trino.spi.TrinoException)

Example 2 with Prepare

use of io.trino.sql.tree.Prepare in project trino by trinodb.

the class TestPrepareTask method executePrepare.

private Map<String, String> executePrepare(String statementName, Statement statement, String sqlString, Session session) {
    TransactionManager transactionManager = createTestTransactionManager();
    AccessControlManager accessControl = new AccessControlManager(transactionManager, emptyEventListenerManager(), new AccessControlConfig(), DefaultSystemAccessControl.NAME);
    accessControl.setSystemAccessControls(List.of(AllowAllSystemAccessControl.INSTANCE));
    QueryStateMachine stateMachine = QueryStateMachine.begin(Optional.empty(), sqlString, Optional.empty(), session, URI.create("fake://uri"), new ResourceGroupId("test"), false, transactionManager, accessControl, executor, metadata, WarningCollector.NOOP, Optional.empty());
    Prepare prepare = new Prepare(identifier(statementName), statement);
    new PrepareTask(new SqlParser()).execute(prepare, stateMachine, emptyList(), WarningCollector.NOOP);
    return stateMachine.getAddedPreparedStatements();
}
Also used : AccessControlManager(io.trino.security.AccessControlManager) AccessControlConfig(io.trino.security.AccessControlConfig) ResourceGroupId(io.trino.spi.resourcegroups.ResourceGroupId) TransactionManager(io.trino.transaction.TransactionManager) InMemoryTransactionManager.createTestTransactionManager(io.trino.transaction.InMemoryTransactionManager.createTestTransactionManager) SqlParser(io.trino.sql.parser.SqlParser) Prepare(io.trino.sql.tree.Prepare)

Example 3 with Prepare

use of io.trino.sql.tree.Prepare in project trino by trinodb.

the class TestSqlParser method testPrepareWithParameters.

@Test
public void testPrepareWithParameters() {
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")))));
    assertStatement("PREPARE myquery FROM SELECT * FROM foo LIMIT ?", new Prepare(identifier("myquery"), simpleQuery(selectList(new AllColumns()), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit(new Parameter(0))))));
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo LIMIT ?", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Limit(new Parameter(2))))));
    assertStatement("PREPARE myquery FROM SELECT ? FROM foo FETCH FIRST ? ROWS ONLY", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(new Parameter(1))))));
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo FETCH NEXT ? ROWS WITH TIES", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new FetchFirst(new Parameter(2), true)))));
    assertStatement("PREPARE myquery FROM SELECT ?, ? FROM foo OFFSET ? ROWS", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0), new Parameter(1)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset(new Parameter(2))), Optional.empty())));
    assertStatement("PREPARE myquery FROM SELECT ? FROM foo OFFSET ? ROWS LIMIT ?", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset(new Parameter(1))), Optional.of(new Limit(new Parameter(2))))));
    assertStatement("PREPARE myquery FROM SELECT ? FROM foo OFFSET ? ROWS FETCH FIRST ? ROWS WITH TIES", new Prepare(identifier("myquery"), simpleQuery(selectList(new Parameter(0)), table(QualifiedName.of("foo")), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.of(new Offset(new Parameter(1))), Optional.of(new FetchFirst(new Parameter(2), true)))));
}
Also used : Prepare(io.trino.sql.tree.Prepare) Parameter(io.trino.sql.tree.Parameter) AllColumns(io.trino.sql.tree.AllColumns) Limit(io.trino.sql.tree.Limit) FetchFirst(io.trino.sql.tree.FetchFirst) Offset(io.trino.sql.tree.Offset) Test(org.junit.jupiter.api.Test)

Aggregations

Prepare (io.trino.sql.tree.Prepare)3 AccessControlConfig (io.trino.security.AccessControlConfig)1 AccessControlManager (io.trino.security.AccessControlManager)1 TrinoException (io.trino.spi.TrinoException)1 ResourceGroupId (io.trino.spi.resourcegroups.ResourceGroupId)1 SqlParser (io.trino.sql.parser.SqlParser)1 AllColumns (io.trino.sql.tree.AllColumns)1 Deallocate (io.trino.sql.tree.Deallocate)1 Execute (io.trino.sql.tree.Execute)1 FetchFirst (io.trino.sql.tree.FetchFirst)1 Limit (io.trino.sql.tree.Limit)1 Offset (io.trino.sql.tree.Offset)1 Parameter (io.trino.sql.tree.Parameter)1 Statement (io.trino.sql.tree.Statement)1 InMemoryTransactionManager.createTestTransactionManager (io.trino.transaction.InMemoryTransactionManager.createTestTransactionManager)1 TransactionManager (io.trino.transaction.TransactionManager)1 Test (org.junit.jupiter.api.Test)1