aboutsummaryrefslogtreecommitdiff
path: root/v1/src/main/java/com/xtremelabs/robolectric/util/DatabaseConfig.java
blob: e14dcc5075898fea7ff1172a35f67e0f8229ce80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.xtremelabs.robolectric.util;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DatabaseConfig {
    private static DatabaseMap dbMap = null;
    private static boolean isLoaded = false;

    public static void setDatabaseMap(DatabaseMap map) {
        dbMap = map;
        isLoaded = false; //make sure to reset isLoaded or mixing databases in a test suite will fail.
    }

    public static DatabaseMap getDatabaseMap() {
        return dbMap;
    }

    /**
     * check if the map has been loaded
     *
     * @return
     */
    public static boolean isMapLoaded() {
        return isLoaded;
    }

    /**
     * Check if the map has been set at all.
     *
     * @return
     */
    public static boolean isMapNull() {
        return dbMap == null;
    }

    /**
     * Sets what database will be used and loads the database driver, based on what DBmap is provided.
     */
    private static void LoadSQLiteDriver() {
        if (isMapNull()) throw new NullDatabaseMapException("Error in DatabaseConfig: DatabaseMap has not been set.");
        try {
            Class.forName(dbMap.getDriverClassName()).newInstance();
        } catch (InstantiationException e) {
            throw new CannotLoadDatabaseMapDriverException("Error in DatabaseConfig: SQLite driver could not be instantiated;", e);
        } catch (IllegalAccessException e) {
            throw new CannotLoadDatabaseMapDriverException("Error in DatabaseConfig: SQLite driver could not be accessed;", e);
        } catch (ClassNotFoundException e) {
            throw new CannotLoadDatabaseMapDriverException("Error in DatabaseConfig: SQLite driver class could not be found;", e);
        }
        isLoaded = true;
    }

    /**
     * Gets an in memory DB connection.  Will load DB Driver if not already loaded.
     *
     * @return Connection to In Memory Database.
     */
    public static Connection getMemoryConnection() {
        if (!isMapLoaded()) LoadSQLiteDriver();
        try {
            return DriverManager.getConnection(dbMap.getConnectionString());
        } catch (SQLException e) {
            throw new CannotLoadDatabaseMapDriverException("Error in DatabaseConfig, could not retrieve connection to in memory database.", e);
        }
    }

    /**
     * Makes any edits necessary in the SQL string for it to be compatible with the database in use.
     *
     * @return
     * @throws SQLException
     */
    public static String getScrubSQL(String sql) throws SQLException {
        if (isMapNull()) throw new NullDatabaseMapException("No database map set!");
        return dbMap.getScrubSQL(sql);
    }

    public static String getSelectLastInsertIdentity() {
        if (isMapNull()) throw new NullDatabaseMapException("No database map set!");
        return dbMap.getSelectLastInsertIdentity();
    }

    public static int getResultSetType() {
        if (isMapNull()) throw new NullDatabaseMapException("No database map set!");
        return dbMap.getResultSetType();
    }

    public interface DatabaseMap {
        String getDriverClassName();

        String getConnectionString();

        String getScrubSQL(String sql) throws SQLException;

        String getSelectLastInsertIdentity();

        int getResultSetType();
    }

    public static class NullDatabaseMapException extends RuntimeException {
        private static final long serialVersionUID = -4580960157495617424L;

        public NullDatabaseMapException(String message) {
            super(message);
        }
    }

    public static class CannotLoadDatabaseMapDriverException extends RuntimeException {
        private static final long serialVersionUID = 2614876121296128364L;

        public CannotLoadDatabaseMapDriverException(String message, Throwable cause) {
            super(message, cause);
        }
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    @Inherited
    public @interface UsingDatabaseMap {
        /**
         * @return the classes to be run
         */
        public Class<? extends DatabaseMap> value();
    }
}