Android Open Source - sqlite-analyzer Migrations Connector






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  w  w  . j av  a 2s.  c  om*/
import com.novoda.sqlite.impl.SQLFile;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class MigrationsConnector implements Connector {

    private final Iterable<File> migrations;

    public MigrationsConnector(Migrations migrations) {
        this.migrations = migrations.asIterable();
    }

    @Override
    public Connection connect() throws SQLException {
        try {
            // Dynamically load the class in order to correctly initialise JDBC drivers, as described in
            // http://stackoverflow.com/questions/6740601/what-does-class-fornameorg-sqlite-jdbc-do/6740632#6740632
            Class.forName("org.sqlite.JDBC");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection connection = DriverManager.getConnection("jdbc:sqlite::memory:");
        executeMigrations(connection);
        return connection;
    }

    private void executeMigrations(Connection connection) throws SQLException {
        Statement statement = connection.createStatement();
        for (File migration : migrations) {
            try {
                List<String> statements = null;
                statements = SQLFile.statementsFrom(migration);
                for (String sqlCommand : statements) {
                    statement.executeUpdate(sqlCommand);
                }
            } catch (IOException e) {
                throw new SQLException("error processing migration file: " + migration.toString(), e);
            }
        }
        statement.close();
    }

}




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