Search in sources :

Example 1 with TablesNamesFinder

use of net.sf.jsqlparser.util.TablesNamesFinder in project JSqlParser by JSQLParser.

the class CreateTableTest method testRUBiSCreateList.

public void testRUBiSCreateList() throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(CreateTableTest.class.getResourceAsStream("/RUBiS-create-requests.txt")));
    TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
    try {
        int numSt = 1;
        while (true) {
            String line = getLine(in);
            if (line == null) {
                break;
            }
            if (!"#begin".equals(line)) {
                break;
            }
            line = getLine(in);
            StringBuilder buf = new StringBuilder(line);
            while (true) {
                line = getLine(in);
                if ("#end".equals(line)) {
                    break;
                }
                buf.append("\n");
                buf.append(line);
            }
            String query = buf.toString();
            if (!getLine(in).equals("true")) {
                continue;
            }
            String tableName = getLine(in);
            String cols = getLine(in);
            try {
                CreateTable createTable = (CreateTable) parserManager.parse(new StringReader(query));
                String[] colsList = null;
                if ("null".equals(cols)) {
                    colsList = new String[0];
                } else {
                    StringTokenizer tokenizer = new StringTokenizer(cols, " ");
                    List colsListList = new ArrayList();
                    while (tokenizer.hasMoreTokens()) {
                        colsListList.add(tokenizer.nextToken());
                    }
                    colsList = (String[]) colsListList.toArray(new String[colsListList.size()]);
                }
                List colsFound = new ArrayList();
                if (createTable.getColumnDefinitions() != null) {
                    for (Iterator iter = createTable.getColumnDefinitions().iterator(); iter.hasNext(); ) {
                        ColumnDefinition columnDefinition = (ColumnDefinition) iter.next();
                        String colName = columnDefinition.getColumnName();
                        boolean unique = false;
                        if (createTable.getIndexes() != null) {
                            for (Iterator iterator = createTable.getIndexes().iterator(); iterator.hasNext(); ) {
                                Index index = (Index) iterator.next();
                                if (index.getType().equals("PRIMARY KEY") && index.getColumnsNames().size() == 1 && index.getColumnsNames().get(0).equals(colName)) {
                                    unique = true;
                                }
                            }
                        }
                        if (!unique) {
                            if (columnDefinition.getColumnSpecStrings() != null) {
                                for (Iterator iterator = columnDefinition.getColumnSpecStrings().iterator(); iterator.hasNext(); ) {
                                    String par = (String) iterator.next();
                                    if (par.equals("UNIQUE")) {
                                        unique = true;
                                    } else if (par.equals("PRIMARY") && iterator.hasNext() && iterator.next().equals("KEY")) {
                                        unique = true;
                                    }
                                }
                            }
                        }
                        if (unique) {
                            colName += ".unique";
                        }
                        colsFound.add(colName.toLowerCase());
                    }
                }
                assertEquals("stm:" + query, colsList.length, colsFound.size());
                for (int i = 0; i < colsList.length; i++) {
                    assertEquals("stm:" + query, colsList[i], colsFound.get(i));
                }
            } catch (Exception e) {
                throw new TestException("error at stm num: " + numSt + "  " + query, e);
            }
            numSt++;
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TestException(net.sf.jsqlparser.test.TestException) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) ArrayList(java.util.ArrayList) Index(net.sf.jsqlparser.statement.create.table.Index) TablesNamesFinder(net.sf.jsqlparser.util.TablesNamesFinder) TestException(net.sf.jsqlparser.test.TestException) JSQLParserException(net.sf.jsqlparser.JSQLParserException) ColumnDefinition(net.sf.jsqlparser.statement.create.table.ColumnDefinition) StringTokenizer(java.util.StringTokenizer) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with TablesNamesFinder

use of net.sf.jsqlparser.util.TablesNamesFinder in project dbeaver by serge-rider.

the class SQLCompletionAnalyzer method makeProposalsFromObject.

private SQLCompletionProposalBase makeProposalsFromObject(DBPNamedObject object, boolean useShortName, @Nullable DBPImage objectIcon, @NotNull Map<String, Object> params) {
    String alias = null;
    String prevWord = request.getWordDetector().getPrevKeyWord();
    if (SQLConstants.KEYWORD_FROM.equals(prevWord) || SQLConstants.KEYWORD_JOIN.equals(prevWord)) {
        if (object instanceof DBSEntity && ((DBSEntity) object).getDataSource().getContainer().getPreferenceStore().getBoolean(SQLModelPreferences.SQL_PROPOSAL_INSERT_TABLE_ALIAS)) {
            SQLDialect dialect = SQLUtils.getDialectFromObject(object);
            if (dialect.supportsAliasInSelect()) {
                String firstKeyword = SQLUtils.getFirstKeyword(dialect, request.getActiveQuery().getText());
                if (dialect.supportsAliasInUpdate() || !ArrayUtils.contains(dialect.getDMLKeywords(), firstKeyword.toUpperCase(Locale.ENGLISH))) {
                    String queryText = request.getActiveQuery().getText();
                    Set<String> aliases = new LinkedHashSet<>();
                    if (request.getActiveQuery() instanceof SQLQuery) {
                        Statement sqlStatement = ((SQLQuery) request.getActiveQuery()).getStatement();
                        if (sqlStatement != null) {
                            TablesNamesFinder namesFinder = new TablesNamesFinder() {

                                public void visit(Table table) {
                                    if (table != null && table.getAlias() != null && table.getAlias().getName() != null) {
                                        aliases.add(table.getAlias().getName().toLowerCase(Locale.ENGLISH));
                                    }
                                }
                            };
                            sqlStatement.accept(namesFinder);
                        }
                    }
                    // It is table name completion after FROM. Auto-generate table alias
                    SQLDialect sqlDialect = SQLUtils.getDialectFromObject(object);
                    alias = SQLUtils.generateEntityAlias((DBSEntity) object, s -> {
                        if (aliases.contains(s) || sqlDialect.getKeywordType(s) != null) {
                            return true;
                        }
                        return Pattern.compile("\\s+" + s + "[^\\w]+").matcher(queryText).find();
                    });
                }
            }
        }
    }
    String objectName = useShortName ? object.getName() : DBUtils.getObjectFullName(object, DBPEvaluationContext.DML);
    boolean isSingleObject = true;
    String replaceString = null;
    DBPDataSource dataSource = request.getContext().getDataSource();
    if (dataSource != null) {
        // replace with full qualified name
        if (!request.getContext().isUseShortNames() && object instanceof DBSObjectReference) {
            if (request.getWordDetector().getFullWord().indexOf(request.getContext().getSyntaxManager().getStructSeparator()) == -1) {
                DBSObjectReference structObject = (DBSObjectReference) object;
                if (structObject.getContainer() != null) {
                    DBSObject selectedObject = DBUtils.getActiveInstanceObject(request.getContext().getExecutionContext());
                    if (selectedObject != structObject.getContainer()) {
                        replaceString = structObject.getFullyQualifiedName(DBPEvaluationContext.DML);
                        isSingleObject = false;
                    }
                }
            }
        }
        if (replaceString == null) {
            if (request.getContext().isUseFQNames() && object instanceof DBPQualifiedObject) {
                replaceString = ((DBPQualifiedObject) object).getFullyQualifiedName(DBPEvaluationContext.DML);
            } else {
                replaceString = DBUtils.getQuotedIdentifier(dataSource, object.getName());
            }
        }
    } else {
        replaceString = DBUtils.getObjectShortName(object);
    }
    if (!CommonUtils.isEmpty(alias)) {
        replaceString += " " + /*convertKeywordCase(request, "as", false) + " " + */
        alias;
    }
    return createCompletionProposal(request, replaceString, objectName, DBPKeywordType.OTHER, objectIcon, isSingleObject, object, params);
}
Also used : DBCSession(org.jkiss.dbeaver.model.exec.DBCSession) java.util(java.util) DBNNode(org.jkiss.dbeaver.model.navigator.DBNNode) Nullable(org.jkiss.code.Nullable) DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) NotNull(org.jkiss.code.NotNull) DBDLabelValuePair(org.jkiss.dbeaver.model.data.DBDLabelValuePair) RelationalObjectType(org.jkiss.dbeaver.model.impl.struct.RelationalObjectType) DBNUtils(org.jkiss.dbeaver.model.navigator.DBNUtils) Matcher(java.util.regex.Matcher) Log(org.jkiss.dbeaver.Log) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) org.jkiss.dbeaver.model(org.jkiss.dbeaver.model) DBRRunnableParametrized(org.jkiss.dbeaver.model.runtime.DBRRunnableParametrized) CommonUtils(org.jkiss.utils.CommonUtils) org.jkiss.dbeaver.model.struct(org.jkiss.dbeaver.model.struct) PatternSyntaxException(java.util.regex.PatternSyntaxException) Table(net.sf.jsqlparser.schema.Table) DBObjectNameCaseTransformer(org.jkiss.dbeaver.model.impl.DBObjectNameCaseTransformer) ArrayUtils(org.jkiss.utils.ArrayUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) DBCExecutionPurpose(org.jkiss.dbeaver.model.exec.DBCExecutionPurpose) TextUtils(org.jkiss.dbeaver.model.text.TextUtils) DBException(org.jkiss.dbeaver.DBException) Statement(net.sf.jsqlparser.statement.Statement) Pattern(java.util.regex.Pattern) SQLWordPartDetector(org.jkiss.dbeaver.model.sql.parser.SQLWordPartDetector) TablesNamesFinder(net.sf.jsqlparser.util.TablesNamesFinder) org.jkiss.dbeaver.model.sql(org.jkiss.dbeaver.model.sql) Table(net.sf.jsqlparser.schema.Table) Statement(net.sf.jsqlparser.statement.Statement) TablesNamesFinder(net.sf.jsqlparser.util.TablesNamesFinder)

Example 3 with TablesNamesFinder

use of net.sf.jsqlparser.util.TablesNamesFinder in project JSqlParser by JSQLParser.

the class SpeedTest method testSpeed.

@Test
public void testSpeed() throws Exception {
    // all the statements in testfiles/simple_parsing.txt
    BufferedReader in = new BufferedReader(new InputStreamReader(SpeedTest.class.getResourceAsStream("/simple_parsing.txt")));
    CCJSqlParserManagerTest d;
    ArrayList statementsList = new ArrayList();
    while (true) {
        String statement = CCJSqlParserManagerTest.getStatement(in);
        if (statement == null) {
            break;
        }
        statementsList.add(statement);
    }
    in.close();
    in = new BufferedReader(new InputStreamReader(SpeedTest.class.getResourceAsStream("/RUBiS-select-requests.txt")));
    // all the statements in testfiles/RUBiS-select-requests.txt
    while (true) {
        String line = CCJSqlParserManagerTest.getLine(in);
        if (line == null) {
            break;
        }
        if (line.length() == 0) {
            continue;
        }
        if (!line.equals("#begin")) {
            break;
        }
        line = CCJSqlParserManagerTest.getLine(in);
        StringBuilder buf = new StringBuilder(line);
        while (true) {
            line = CCJSqlParserManagerTest.getLine(in);
            if (line.equals("#end")) {
                break;
            }
            buf.append("\n");
            buf.append(line);
        }
        if (!CCJSqlParserManagerTest.getLine(in).equals("true")) {
            continue;
        }
        statementsList.add(buf.toString());
        String cols = CCJSqlParserManagerTest.getLine(in);
        String tables = CCJSqlParserManagerTest.getLine(in);
        String whereCols = CCJSqlParserManagerTest.getLine(in);
        String type = CCJSqlParserManagerTest.getLine(in);
    }
    in.close();
    String statement = "";
    int numTests = 0;
    // it seems that the very first parsing takes a while, so I put it aside
    Statement parsedStm = parserManager.parse(new StringReader(statement = (String) statementsList.get(0)));
    TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
    ArrayList parsedSelects = new ArrayList(NUM_REPS * statementsList.size());
    long time = System.currentTimeMillis();
    // measure the time to parse NUM_REPS times all statements in the 2 files
    for (int i = 0; i < NUM_REPS; i++) {
        try {
            int j = 0;
            for (Iterator iter = statementsList.iterator(); iter.hasNext(); j++) {
                statement = (String) iter.next();
                parsedStm = parserManager.parse(new StringReader(statement));
                numTests++;
                if (parsedStm instanceof Select) {
                    parsedSelects.add(parsedStm);
                }
            }
        } catch (JSQLParserException e) {
            throw new TestException("impossible to parse statement: " + statement, e);
        }
    }
    long elapsedTime = System.currentTimeMillis() - time;
    long statementsPerSecond = numTests * 1000 / elapsedTime;
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(7);
    df.setMinimumFractionDigits(4);
    System.out.println(numTests + " statements parsed in " + elapsedTime + " milliseconds");
    System.out.println(" (" + statementsPerSecond + " statements per second,  " + df.format(1.0 / statementsPerSecond) + " seconds per statement )");
    numTests = 0;
    time = System.currentTimeMillis();
    // measure the time to get the tables names from all the SELECTs parsed before
    for (Iterator iter = parsedSelects.iterator(); iter.hasNext(); ) {
        Select select = (Select) iter.next();
        if (select != null) {
            numTests++;
            List tableListRetr = tablesNamesFinder.getTableList(select);
        }
    }
    elapsedTime = System.currentTimeMillis() - time;
    statementsPerSecond = numTests * 1000 / elapsedTime;
    System.out.println(numTests + " select scans for table name executed in " + elapsedTime + " milliseconds");
    System.out.println(" (" + statementsPerSecond + " select scans for table name per second,  " + df.format(1.0 / statementsPerSecond) + " seconds per select scans for table name)");
}
Also used : InputStreamReader(java.io.InputStreamReader) TestException(net.sf.jsqlparser.test.TestException) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) TablesNamesFinder(net.sf.jsqlparser.util.TablesNamesFinder) CCJSqlParserManagerTest(net.sf.jsqlparser.test.simpleparsing.CCJSqlParserManagerTest) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) Iterator(java.util.Iterator) Select(net.sf.jsqlparser.statement.select.Select) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test) CCJSqlParserManagerTest(net.sf.jsqlparser.test.simpleparsing.CCJSqlParserManagerTest)

Example 4 with TablesNamesFinder

use of net.sf.jsqlparser.util.TablesNamesFinder in project sandbox by irof.

the class ParsingTest method select.

@ParameterizedTest
@MethodSource
void select(String sql, List<String> tableNames) throws Exception {
    try (InputStream inputStream = this.getClass().getResourceAsStream(sql)) {
        Statement statement = CCJSqlParserUtil.parse(inputStream);
        Select select = Select.class.cast(statement);
        List<String> tableList = new TablesNamesFinder().getTableList(select);
        assertThat(tableList).hasSameElementsAs(tableNames);
    }
}
Also used : InputStream(java.io.InputStream) Statement(net.sf.jsqlparser.statement.Statement) TablesNamesFinder(net.sf.jsqlparser.util.TablesNamesFinder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with TablesNamesFinder

use of net.sf.jsqlparser.util.TablesNamesFinder in project sandbox by irof.

the class ParsingTest method insert.

@ParameterizedTest
@MethodSource
void insert(String sql, String insertTableName, List<String> allTableNames) throws Exception {
    try (InputStream inputStream = this.getClass().getResourceAsStream(sql)) {
        Statement statement = CCJSqlParserUtil.parse(inputStream);
        Insert insert = Insert.class.cast(statement);
        assertThat(insert.getTable().getName()).isEqualTo(insertTableName);
        List<String> tableList = new TablesNamesFinder().getTableList(insert);
        assertThat(tableList).hasSameElementsAs(allTableNames);
    }
}
Also used : InputStream(java.io.InputStream) Statement(net.sf.jsqlparser.statement.Statement) Insert(net.sf.jsqlparser.statement.insert.Insert) TablesNamesFinder(net.sf.jsqlparser.util.TablesNamesFinder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

TablesNamesFinder (net.sf.jsqlparser.util.TablesNamesFinder)5 Statement (net.sf.jsqlparser.statement.Statement)4 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 JSQLParserException (net.sf.jsqlparser.JSQLParserException)2 TestException (net.sf.jsqlparser.test.TestException)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 DecimalFormat (java.text.DecimalFormat)1 java.util (java.util)1 StringTokenizer (java.util.StringTokenizer)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1