aboutsummaryrefslogtreecommitdiff
path: root/v1/src/main/java/com/xtremelabs/robolectric/shadows/ShadowSQLiteProgram.java
blob: d903bfe13c4ed8edcc35bba78970b74ee590591b (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.xtremelabs.robolectric.shadows;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteProgram;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;
import com.xtremelabs.robolectric.internal.RealObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

@Implements(SQLiteProgram.class)
public abstract class ShadowSQLiteProgram {
	@RealObject	SQLiteProgram realSQLiteProgram;
	protected SQLiteDatabase mDatabase;
	Connection connection;
	PreparedStatement actualDBstatement;
	public void init(SQLiteDatabase db, String sql) {
	 mDatabase = db;
	 connection = Robolectric.shadowOf(db).getConnection();

	 try {
			actualDBstatement = connection.prepareStatement(sql,
					Statement.RETURN_GENERATED_KEYS);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
    /**
     * Bind a NULL value to this statement. The value remains bound until
     * {@link #clearBindings} is called.
     *
     * @param index The 1-based index to the parameter to bind null to
     */
	@Implementation
    public void bindNull(int index) {
		checkDatabaseIsOpen();
		try {
			// SQLite ignores typecode
			// typecode is also ignored in H2 when using the two parameter setNUll()
			actualDBstatement.setNull(index,java.sql.Types.NULL); 
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
    }

    /**
     * Bind a long value to this statement. The value remains bound until
     * {@link #clearBindings} is called.
     *
     * @param index The 1-based index to the parameter to bind
     * @param value The value to bind
     */
    @Implementation
    public void bindLong(int index, long value) {
    	checkDatabaseIsOpen();

    	try {
			actualDBstatement.setLong(index,value);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
    }
    
    private void checkDatabaseIsOpen() {
    	if (!mDatabase.isOpen()) {
            throw new IllegalStateException("database " + mDatabase.getPath() + " already closed");
        }
    }

    public PreparedStatement getStatement() {
    	return actualDBstatement;
    }
    
    /**
     * Bind a double value to this statement. The value remains bound until
     * {@link #clearBindings} is called.
     *
     * @param index The 1-based index to the parameter to bind
     * @param value The value to bind
     */
    @Implementation
    public void bindDouble(int index, double value) {
    	checkDatabaseIsOpen();
    	try {
			actualDBstatement.setDouble(index,value);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
    }

    /**
     * Bind a String value to this statement. The value remains bound until
     * {@link #clearBindings} is called.
     *
     * @param index The 1-based index to the parameter to bind
     * @param value The value to bind
     */
    @Implementation
    public void bindString(int index, String value) {
        if (value == null) {
            throw new IllegalArgumentException("the bind value at index " + index + " is null");
        }
        checkDatabaseIsOpen();
        try {
			actualDBstatement.setString(index,value);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
    }

    /**
     * Bind a byte array value to this statement. The value remains bound until
     * {@link #clearBindings} is called.
     *
     * @param index The 1-based index to the parameter to bind
     * @param value The value to bind
     */
    @Implementation
    public void bindBlob(int index, byte[] value) {
        if (value == null) {
            throw new IllegalArgumentException("the bind value at index " + index + " is null");
        }
        checkDatabaseIsOpen();
        try {
			actualDBstatement.setBytes(index,value);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}

    }

    /**
     * Clears all existing bindings. Unset bindings are treated as NULL.
     */
    @Implementation
    public void clearBindings() {
    	checkDatabaseIsOpen();
    	
    	try {
			actualDBstatement.clearParameters();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
    }
}