Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Old question, but I think this project contains what you need:</p> <p><a href="http://www.eclipse.org/datatools/project_sqldevtools/" rel="nofollow">Data Tools Project - SQL Development Tools</a></p> <p>Here's the documentation for the <a href="http://www.eclipse.org/datatools/project_sqldevtools/sqltools_doc/SQL%20Query%20Parser%20User%20documentation.htm" rel="nofollow">SQL Query Parser</a>.</p> <p>Also, here's a small sample program. I'm no Java programmer so use with care.</p> <pre><code>package org.lala; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.util.Iterator; import java.util.List; import org.eclipse.datatools.modelbase.sql.query.QuerySelectStatement; import org.eclipse.datatools.modelbase.sql.query.QueryStatement; import org.eclipse.datatools.modelbase.sql.query.TableReference; import org.eclipse.datatools.modelbase.sql.query.ValueExpressionColumn; import org.eclipse.datatools.modelbase.sql.query.helper.StatementHelper; import org.eclipse.datatools.sqltools.parsers.sql.SQLParseErrorInfo; import org.eclipse.datatools.sqltools.parsers.sql.SQLParserException; import org.eclipse.datatools.sqltools.parsers.sql.SQLParserInternalException; import org.eclipse.datatools.sqltools.parsers.sql.query.SQLQueryParseResult; import org.eclipse.datatools.sqltools.parsers.sql.query.SQLQueryParserManager; import org.eclipse.datatools.sqltools.parsers.sql.query.SQLQueryParserManagerProvider; public class SQLTest { private static String readFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } } /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { try { // Create an instance the Parser Manager // SQLQueryParserManagerProvider.getInstance().getParserManager // returns the best compliant SQLQueryParserManager // supporting the SQL dialect of the database described by the given // database product information. In the code below null is passed // for both the database and version // in which case a generic parser is returned SQLQueryParserManager parserManager = SQLQueryParserManagerProvider .getInstance().getParserManager("DB2 UDB", "v9.1"); // Sample query String sql = readFile("c:\\test.sql"); // Parse SQLQueryParseResult parseResult = parserManager.parseQuery(sql); // Get the Query Model object from the result QueryStatement resultObject = parseResult.getQueryStatement(); // Get the SQL text String parsedSQL = resultObject.getSQL(); System.out.println(parsedSQL); // Here we have the SQL code parsed! QuerySelectStatement querySelect = (QuerySelectStatement) parseResult .getSQLStatement(); List columnExprList = StatementHelper .getEffectiveResultColumns(querySelect); Iterator columnIt = columnExprList.iterator(); while (columnIt.hasNext()) { ValueExpressionColumn colExpr = (ValueExpressionColumn) columnIt .next(); // DataType dataType = colExpr.getDataType(); System.out.println("effective result column: " + colExpr.getName());// + " with data type: " + // dataType.getName()); } List tableList = StatementHelper.getTablesForStatement(resultObject); // List tableList = StatementHelper.getTablesForStatement(querySelect); for (Object obj : tableList) { TableReference t = (TableReference) obj; System.out.println(t.getName()); } } catch (SQLParserException spe) { // handle the syntax error System.out.println(spe.getMessage()); @SuppressWarnings("unchecked") List&lt;SQLParseErrorInfo&gt; syntacticErrors = spe.getErrorInfoList(); Iterator&lt;SQLParseErrorInfo&gt; itr = syntacticErrors.iterator(); while (itr.hasNext()) { SQLParseErrorInfo errorInfo = (SQLParseErrorInfo) itr.next(); // Example usage of the SQLParseErrorInfo object // the error message String errorMessage = errorInfo.getParserErrorMessage(); String expectedText = errorInfo.getExpectedText(); String errorSourceText = errorInfo.getErrorSourceText(); // the line numbers of error int errorLine = errorInfo.getLineNumberStart(); int errorColumn = errorInfo.getColumnNumberStart(); System.err.println("Error in line " + errorLine + ", column " + errorColumn + ": " + expectedText + " " + errorMessage + " " + errorSourceText); } } catch (SQLParserInternalException spie) { // handle the exception System.out.println(spie.getMessage()); } System.exit(0); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload