Android Open Source - sqlite-analyzer Analyzer






From Project

Back to project page sqlite-analyzer.

License

The source code is released under:

Apache License

If you think the Android project sqlite-analyzer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.novoda.sqlite;
/*w  ww .  j a  va  2 s .com*/
import com.novoda.sqlite.impl.TableCreateStatementParser;
import com.novoda.sqlite.model.Column;
import com.novoda.sqlite.model.Database;
import com.novoda.sqlite.model.Table;

import java.sql.*;
import java.util.Arrays;
import java.util.List;

public final class Analyzer {
    private Statement statement;
    private final Connection connection;

    public Analyzer(Connection connection) throws SQLException {
        this.connection = connection;
    }

    public Database analyze() throws SQLException {
        statement = connection.createStatement();
        Database database = new Database();
        readTableAndViewNames(database);
        for (Table name : database.getTables()) {
            readTableInfo(name);
        }
        addTableDependencies(database);
        statement.close();
        return database;
    }

    private void addTableDependencies(Database database) {
        TableCreateStatementParser parser = new TableCreateStatementParser();
        for (Table table : database.getTables()) {
            if (!table.isView()) {
                continue;
            }
            List<String> dependsOnTables = parser.parseUsedTables(table.getSql());
            for (String dependsOnTable : dependsOnTables) {
                Table baseTable = database.findTableByName(dependsOnTable);
                // error in parser! Investigate! Does not parse stuff like 'JOIN "two words table"' correctly
                if (baseTable == null) {
                    continue;
//                    throw new RuntimeException("no table with name " + dependsOnTable + " : " + database.getTables());
//                    System.out.println("table not found: " + dependsOnTable);
//                    continue;
                }
                baseTable.addDependent(table);
            }
        }
    }

    /**
     * This queries the master table to get the names of all tables and views.
     *
     * @param database
     * @throws SQLException
     */
    private void readTableAndViewNames(Database database) throws SQLException {
        ResultSet rs = statement
                .executeQuery("select * from sqlite_master where type IN ('table', 'view') AND NOT name like 'sqlite/_%' ESCAPE '/';");
        while (rs.next()) {
            database.addTable(new Table(rs.getString("name"), rs.getString("sql"), "view".equals(rs.getString("type"))));
        }
    }

    /**
     * Uses a PRAGMA query to derive the column infos for a given table or view.
     *
     * @param table
     * @throws SQLException
     */
    private void readTableInfo(Table table) throws SQLException {
        ResultSet tableInfo = statement.executeQuery("PRAGMA table_info('" + table.getName() + "');");
        while (tableInfo.next()) {
            table.addColumn(createColumn(tableInfo));
        }
    }

    private Column createColumn(ResultSet resultSet) throws SQLException {
        String name = resultSet.getString("name");
        String type = resultSet.getString("type");
        boolean nullable = !resultSet.getBoolean("notNull");
        return new Column(name, type, nullable);
    }

    /**
     * We can also read the column info using a Limit-0 select query on a table.
     *
     * @throws SQLException
     */
    private void readLimitZeroQueryResultMetaData() throws SQLException {
        String sql = "select * from BROADCASTS LIMIT 0";
        ResultSet rs = statement.executeQuery(sql);
        ResultSetMetaData mrs = rs.getMetaData();
        for (int i = 1; i <= mrs.getColumnCount(); i++) {
            Object[] row = new Object[4];
            row[0] = mrs.getColumnLabel(i);
            row[1] = mrs.getColumnTypeName(i);
            row[2] = mrs.getPrecision(i);
            row[3] = mrs.getTableName(i);
            System.out.println(Arrays.asList(row));
        }

    }

}




Java Source Code List

com.example.MainActivity.java
com.example.MainActivity.java
com.example.MyDatabase.java
com.example.MyDatabase.java
com.novoda.sqlite.Analyzer.java
com.novoda.sqlite.Connector.java
com.novoda.sqlite.FileConnector.java
com.novoda.sqlite.MigrationsConnector.java
com.novoda.sqlite.MigrationsInDir.java
com.novoda.sqlite.Migrations.java
com.novoda.sqlite.NumberedFilesComparator.java
com.novoda.sqlite.StringUtilOld.java
com.novoda.sqlite.StringUtil.java
com.novoda.sqlite.impl.SQLFile.java
com.novoda.sqlite.impl.TableCreateStatementParser.java
com.novoda.sqlite.model.Column.java
com.novoda.sqlite.model.DataAffinity.java
com.novoda.sqlite.model.Database.java
com.novoda.sqlite.model.Table.java
com.novoda.sqliteprovider.demo.simple.provider.FireworkProvider.java
com.novoda.sqliteprovider.demo.simple.ui.MainActivity.java
com.novoda.sqliteprovider.demo.simple.ui.MainFragment.java