Search in sources :

Example 1 with TestException

use of net.sf.jsqlparser.test.TestException 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 TestException

use of net.sf.jsqlparser.test.TestException in project JSqlParser by JSQLParser.

the class CCJSqlParserManagerTest method testParse.

@Test
public void testParse() throws Exception {
    CCJSqlParserManager parserManager = new CCJSqlParserManager();
    BufferedReader in = new BufferedReader(new InputStreamReader(CreateTableTest.class.getResourceAsStream("/simple_parsing.txt")));
    String statement = "";
    while (true) {
        try {
            statement = CCJSqlParserManagerTest.getStatement(in);
            if (statement == null) {
                break;
            }
            parserManager.parse(new StringReader(statement));
        } catch (JSQLParserException e) {
            throw new TestException("impossible to parse statement: " + statement, e);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TestException(net.sf.jsqlparser.test.TestException) JSQLParserException(net.sf.jsqlparser.JSQLParserException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) CCJSqlParserManager(net.sf.jsqlparser.parser.CCJSqlParserManager) CreateTableTest(net.sf.jsqlparser.test.create.CreateTableTest) Test(org.junit.Test)

Example 3 with TestException

use of net.sf.jsqlparser.test.TestException in project JSqlParser by JSQLParser.

the class TablesNamesFinderTest method runTestOnResource.

private void runTestOnResource(String resPath) throws Exception {
    BufferedReader in = new BufferedReader(new InputStreamReader(TablesNamesFinderTest.class.getResourceAsStream(resPath)));
    TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
    try {
        int numSt = 1;
        while (true) {
            String line = getLine(in);
            if (line == null) {
                break;
            }
            if (line.length() == 0) {
                continue;
            }
            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 cols = getLine(in);
            String tables = getLine(in);
            String whereCols = getLine(in);
            String type = getLine(in);
            try {
                Statement statement = pm.parse(new StringReader(query));
                String[] tablesArray = tables.split("\\s+");
                List<String> tableListRetr = tablesNamesFinder.getTableList(statement);
                assertEquals("stm num:" + numSt, tablesArray.length, tableListRetr.size());
                for (int i = 0; i < tablesArray.length; i++) {
                    assertTrue("stm num:" + numSt, tableListRetr.contains(tablesArray[i]));
                }
            } catch (Exception e) {
                throw new TestException("error at stm num: " + numSt + " in file " + resPath, e);
            }
            numSt++;
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TestException(net.sf.jsqlparser.test.TestException) Statement(net.sf.jsqlparser.statement.Statement) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) OracleHint(net.sf.jsqlparser.expression.OracleHint) TestException(net.sf.jsqlparser.test.TestException) JSQLParserException(net.sf.jsqlparser.JSQLParserException)

Example 4 with TestException

use of net.sf.jsqlparser.test.TestException 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)

Aggregations

BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 StringReader (java.io.StringReader)4 JSQLParserException (net.sf.jsqlparser.JSQLParserException)4 TestException (net.sf.jsqlparser.test.TestException)4 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Statement (net.sf.jsqlparser.statement.Statement)2 TablesNamesFinder (net.sf.jsqlparser.util.TablesNamesFinder)2 Test (org.junit.Test)2 DecimalFormat (java.text.DecimalFormat)1 StringTokenizer (java.util.StringTokenizer)1 OracleHint (net.sf.jsqlparser.expression.OracleHint)1 CCJSqlParserManager (net.sf.jsqlparser.parser.CCJSqlParserManager)1 ColumnDefinition (net.sf.jsqlparser.statement.create.table.ColumnDefinition)1 CreateTable (net.sf.jsqlparser.statement.create.table.CreateTable)1 Index (net.sf.jsqlparser.statement.create.table.Index)1 Select (net.sf.jsqlparser.statement.select.Select)1 CreateTableTest (net.sf.jsqlparser.test.create.CreateTableTest)1