Search in sources :

Example 11 with QueryParser

use of org.teiid.query.parser.QueryParser in project teiid by teiid.

the class MaterializationMetadataRepository method fixScript.

/**
 * Rather than require a script to be tokenized directly
 * we expect it to be wrapped in an anon block
 * @param property
 * @param table
 * @return
 */
private String fixScript(String property, Table table) {
    String script = table.getProperty(property, false);
    if (script == null) {
        return null;
    }
    if (!script.contains(";")) {
        // $NON-NLS-1$
        return script;
    }
    QueryParser parser = QueryParser.getQueryParser();
    try {
        parser.parseCommand(script);
        return script;
    } catch (QueryParserException e) {
    }
    // $NON-NLS-1$ //$NON-NLS-2$
    String wrapped = "begin " + script + "; end";
    try {
        parser.parseCommand(wrapped);
        table.setProperty(property, wrapped);
        return wrapped;
    } catch (QueryParserException e) {
    }
    // because we don't handle empty ; in scripts also try without
    // $NON-NLS-1$ //$NON-NLS-2$
    wrapped = "begin " + script + " end";
    try {
        parser.parseCommand(wrapped);
        table.setProperty(property, wrapped);
        return wrapped;
    } catch (QueryParserException e) {
    }
    // give up
    return script;
}
Also used : QueryParser(org.teiid.query.parser.QueryParser) QueryParserException(org.teiid.api.exception.query.QueryParserException)

Example 12 with QueryParser

use of org.teiid.query.parser.QueryParser in project teiid by teiid.

the class ProcedureContainerResolver method expandCommand.

/**
 * Expand a command by finding and attaching all subcommands to the command.  If
 * some initial resolution must be done for this to be accomplished, that is ok,
 * but it should be kept to a minimum.
 * @param command The command to expand
 * @param useMetadataCommands True if resolver should use metadata commands to completely resolve
 * @param metadata Metadata access
 * @param analysis The analysis record that will be filled in if doing annotation.
 *
 * @throws QueryMetadataException If there is a metadata problem
 * @throws QueryResolverException If the query cannot be resolved
 * @throws TeiidComponentException If there is an internal error
 */
public Command expandCommand(ProcedureContainer procCommand, QueryMetadataInterface metadata, AnalysisRecord analysis) throws QueryMetadataException, QueryResolverException, TeiidComponentException {
    // Resolve group so we can tell whether it is an update procedure
    GroupSymbol group = procCommand.getGroup();
    Command subCommand = null;
    String plan = getPlan(metadata, procCommand);
    if (plan == null) {
        return null;
    }
    QueryParser parser = QueryParser.getQueryParser();
    try {
        subCommand = parser.parseProcedure(plan, !(procCommand instanceof StoredProcedure));
    } catch (QueryParserException e) {
        throw new QueryResolverException(QueryPlugin.Event.TEIID30060, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30060, group, procCommand.getClass().getSimpleName()));
    }
    return subCommand;
}
Also used : QueryParser(org.teiid.query.parser.QueryParser) QueryParserException(org.teiid.api.exception.query.QueryParserException) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Command(org.teiid.query.sql.lang.Command) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) QueryResolverException(org.teiid.api.exception.query.QueryResolverException)

Example 13 with QueryParser

use of org.teiid.query.parser.QueryParser in project teiid by teiid.

the class TestAuthorizationValidationVisitor method testPruneSelectAll.

@Test
public void testPruneSelectAll() throws Exception {
    String sql = "select * from pm1.g1";
    QueryMetadataInterface metadata = RealMetadataFactory.example1Cached();
    DataPolicyMetadata svc = new DataPolicyMetadata();
    // $NON-NLS-1$
    svc.setName("test");
    // $NON-NLS-1$
    svc.addPermission(addResource(DataPolicy.PermissionType.READ, "pm1"));
    PermissionMetaData p = addResource(DataPolicy.PermissionType.READ, "pm1.g1.e1");
    p.setAllowRead(false);
    // $NON-NLS-1$
    svc.addPermission(p);
    DataRolePolicyDecider dataRolePolicyDecider = createPolicyDecider(metadata, RealMetadataFactory.example1VDB(), svc);
    DefaultAuthorizationValidator dav = new DefaultAuthorizationValidator();
    dav.setPolicyDecider(dataRolePolicyDecider);
    this.context.setSessionVariable(DefaultAuthorizationValidator.IGNORE_UNAUTHORIZED_ASTERISK, "true");
    QueryParser parser = QueryParser.getQueryParser();
    Command command = parser.parseCommand(sql);
    QueryResolver.resolveCommand(command, metadata);
    assertEquals(4, command.getProjectedSymbols().size());
    boolean modified = dav.validate(new String[] {}, command, metadata, this.context, CommandType.USER);
    assertTrue(modified);
    assertEquals(3, command.getProjectedSymbols().size());
    p = addResource(DataPolicy.PermissionType.READ, "pm1.g1");
    p.setAllowRead(false);
    // $NON-NLS-1$
    svc.addPermission(p);
    command = parser.parseCommand(sql);
    QueryResolver.resolveCommand(command, metadata);
    assertEquals(4, command.getProjectedSymbols().size());
    try {
        dav.validate(new String[] {}, command, metadata, this.context, CommandType.USER);
        fail();
    } catch (QueryValidatorException e) {
    }
}
Also used : QueryParser(org.teiid.query.parser.QueryParser) Command(org.teiid.query.sql.lang.Command) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) DataPolicyMetadata(org.teiid.adminapi.impl.DataPolicyMetadata) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) PermissionMetaData(org.teiid.adminapi.impl.DataPolicyMetadata.PermissionMetaData) Test(org.junit.Test)

Example 14 with QueryParser

use of org.teiid.query.parser.QueryParser in project teiid by teiid.

the class TestSchemaToProtobufProcessor method testSimpleNoMetadataConversion.

@Test
public void testSimpleNoMetadataConversion() throws Exception {
    Properties props = new Properties();
    MetadataFactory mf = new MetadataFactory("proto", 1, "model", SystemMetadata.getInstance().getRuntimeTypeMap(), props, null);
    mf.setParser(new QueryParser());
    mf.parse(new FileReader(UnitTestUtil.getTestDataFile("tables_no_metadata.ddl")));
    SchemaToProtobufProcessor tool = new SchemaToProtobufProcessor();
    tool.setIndexMessages(true);
    InfinispanConnection conn = Mockito.mock(InfinispanConnection.class);
    BasicCache cache = Mockito.mock(BasicCache.class);
    Mockito.stub(cache.getName()).toReturn("default");
    Mockito.stub(conn.getCache()).toReturn(cache);
    ProtobufResource resource = tool.process(mf, conn);
    String expected = "package model;\n" + "\n" + "/* @Indexed */\n" + "message G1 {\n" + "    /* @Id */\n" + "    required int32 e1 = 1;\n" + "    required string e2 = 2;\n" + "    optional float e3 = 3;\n" + "    repeated string e4 = 4;\n" + "    repeated string e5 = 5;\n" + "}\n" + "\n" + "/* @Indexed */\n" + "message G2 {\n" + "    /* @Id */\n" + "    required int32 e1 = 1;\n" + "    optional string e2 = 2;\n" + "    optional bytes e5 = 3;\n" + "    optional int64 e6 = 4;\n" + "    repeated G4 g4 = 5;\n" + "}\n" + "\n" + "/* @Indexed */\n" + "message G4 {\n" + "    required int32 e1 = 1;\n" + "    required string e2 = 2;\n" + "    optional double e3 = 3;\n" + "    optional float e4 = 4;\n" + "    /* @Teiid(type=short) */\n" + "    optional int32 e5 = 5;\n" + "    /* @Teiid(type=byte) */\n" + "    optional int32 e6 = 6;\n" + "    /* @Teiid(type=char, length=1) */\n" + "    optional string e7 = 7;\n" + "    optional int64 e8 = 8;\n" + "    /* @Teiid(type=bigdecimal) */\n" + "    optional string e9 = 9;\n" + "    /* @Teiid(type=biginteger) */\n" + "    optional string e10 = 10;\n" + "    /* @Teiid(type=time) */\n" + "    optional int64 e11 = 11;\n" + "    /* @Teiid(type=timestamp) */\n" + "    optional int64 e12 = 12;\n" + "    /* @Teiid(type=date) */\n" + "    optional int64 e13 = 13;\n" + "    /* @Teiid(type=object) */\n" + "    optional bytes e14 = 14;\n" + "    /* @Teiid(type=blob) */\n" + "    optional bytes e15 = 15;\n" + "    /* @Teiid(type=clob) */\n" + "    optional bytes e16 = 16;\n" + "    /* @Teiid(type=xml) */\n" + "    optional bytes e17 = 17;\n" + "    /* @Teiid(type=geometry) */\n" + "    optional bytes e18 = 18;\n" + "}\n\n";
    assertEquals(expected, resource.getContents());
}
Also used : QueryParser(org.teiid.query.parser.QueryParser) MetadataFactory(org.teiid.metadata.MetadataFactory) BasicCache(org.infinispan.commons.api.BasicCache) InfinispanConnection(org.teiid.infinispan.api.InfinispanConnection) FileReader(java.io.FileReader) ProtobufResource(org.teiid.infinispan.api.ProtobufResource) Properties(java.util.Properties) Test(org.junit.Test)

Example 15 with QueryParser

use of org.teiid.query.parser.QueryParser in project teiid by teiid.

the class TestProcedurePlanner method helpPlanProcedure.

// ################ getReplacementClause tests ###################
private ProcessorPlan helpPlanProcedure(String userQuery, String procedure, TriggerEvent procedureType) throws TeiidComponentException, QueryMetadataException, TeiidProcessingException {
    QueryMetadataInterface metadata = RealMetadataFactory.exampleUpdateProc(procedureType, procedure);
    QueryParser parser = QueryParser.getQueryParser();
    Command userCommand = userQuery != null ? parser.parseCommand(userQuery) : parser.parseCommand(procedure);
    if (userCommand instanceof CreateProcedureCommand) {
        GroupSymbol gs = new GroupSymbol("proc");
        gs.setMetadataID(new TempMetadataID("proc", Collections.EMPTY_LIST));
        ((CreateProcedureCommand) userCommand).setVirtualGroup(gs);
    }
    QueryResolver.resolveCommand(userCommand, metadata);
    ValidatorReport report = Validator.validate(userCommand, metadata);
    if (report.hasItems()) {
        ValidatorFailure firstFailure = report.getItems().iterator().next();
        throw new QueryValidatorException(firstFailure.getMessage());
    }
    userCommand = QueryRewriter.rewrite(userCommand, metadata, null);
    AnalysisRecord analysisRecord = new AnalysisRecord(false, DEBUG);
    try {
        return QueryOptimizer.optimizePlan(userCommand, metadata, null, new DefaultCapabilitiesFinder(), analysisRecord, null);
    } finally {
        if (DEBUG) {
            System.out.println(analysisRecord.getDebugLog());
        }
    }
}
Also used : QueryParser(org.teiid.query.parser.QueryParser) AnalysisRecord(org.teiid.query.analysis.AnalysisRecord) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) CreateProcedureCommand(org.teiid.query.sql.proc.CreateProcedureCommand) Command(org.teiid.query.sql.lang.Command) ValidatorFailure(org.teiid.query.validator.ValidatorFailure) QueryValidatorException(org.teiid.api.exception.query.QueryValidatorException) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) TempMetadataID(org.teiid.query.metadata.TempMetadataID) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) ValidatorReport(org.teiid.query.validator.ValidatorReport)

Aggregations

QueryParser (org.teiid.query.parser.QueryParser)18 Command (org.teiid.query.sql.lang.Command)10 Test (org.junit.Test)7 MetadataFactory (org.teiid.metadata.MetadataFactory)6 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)6 ValidatorReport (org.teiid.query.validator.ValidatorReport)5 FileReader (java.io.FileReader)4 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)4 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)4 MetadataValidator (org.teiid.query.metadata.MetadataValidator)3 CreateProcedureCommand (org.teiid.query.sql.proc.CreateProcedureCommand)3 RealMetadataFactory (org.teiid.query.unittest.RealMetadataFactory)3 ArrayList (java.util.ArrayList)2 QueryParserException (org.teiid.api.exception.query.QueryParserException)2 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)2 QueryValidatorException (org.teiid.api.exception.query.QueryValidatorException)2 Column (org.teiid.metadata.Column)2 MetadataException (org.teiid.metadata.MetadataException)2 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)2 FileNotFoundException (java.io.FileNotFoundException)1