Parser for Oracle SQL

前端 未结 7 861
自闭症患者
自闭症患者 2020-12-05 05:05

For my current project I need a SQL parser that parses Oracle SQL statements. Currently I\'ve been using jsqlparser, which worked well for simple queries. But when specific

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 06:00

    After working the same issue, I managed to get a SQL parser working:

    • Download Oracle SQL Developer from http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index.html.
    • Oracle SQL developer contains a set jars and some of the APIs are documented as part of Oracle Fusion Middleware Java API Reference for Oracle Extension SDK (a link to the javadoc: http://docs.oracle.com/middleware/1212/jdev/ESDKJ/index.html?oracle/javatools/db/sql/class-use/SQLQueryBuilder.SQLQueryObjectSet.html).
    • One of the available classes is oracle.javatools.db.sql.SQLQueryBuilder.

    My code looks like this:

    import oracle.jdeveloper.db.DatabaseConnections;
    import oracle.javatools.db.sql.SQLQueryBuilder;
    import oracle.javatools.db.Database;
    ...
    // load the database connections
    // this is specific to Oracle SQL developer
    DatabaseConnections connections = DatabaseConnections.getPrivateInstance(
        (new File("src/test/resources/connection.xml")).toURI().toURL(),
        "somePassword");
    // get the one we are interested in
    Database database = connections.getDatabase("the-name-of-a-sqldeveloper-connection");
    SQLQueryBuilder queryBuilder = SQLQueryBuilderFactory.createBuilder(
          database, new Schema("OPTIONAL_SCHEMA"), "select * from some_table");
    

    The challenges to get this working are:

    • Getting the Oracle SQL Developer is a challenge. To do this, you will need to hack the files Oracle SQL Developer creates to persist those connections; the connection.xml in the above example looks something like this:
    
    
       
          
      
         
            HSx10FtlsPc=
         
         
            thin
         
    ...
    

    To get such a file you will need to dig into the folder where Oracle SQL Developer settings are stored and just copy-paste that content into your own file.

    • Next challenge is to define the database password. That is encrypted but this post (https://community.oracle.com/message/12359081) should be very helpful - just check the answer at the end of the post.

    Now, assuming you managed to get this far here are the problems and the points where I got disappointed by the end solution:

    • The API in the builder is decent but parsing will perform a query execution (and this might be a big issue - in my case I needed the parsing to be fast).
    • The API is not officially public. Without being able to quote here the precise wording, I got an Oracle answer that stated there is no officially supported Oracle parser (the alluded reason was that this is a very valuable technology that will not be sold or licensed).
    • While this is more of a hack than a solution, I realized that it might be useful for some cases (not mine). I consider that using it in real life scenarios might be highly risky from both technical and legal perspectives.

    The reason I posted this answer is to drive community attention to the fact that having an Oracle SQL parser is perfectly feasible and maybe one day Oracle will consider exposing the parser as a competitive advantage (I am sure there are users out there that would happily pay some fees to get a license).

提交回复
热议问题