• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.database.DatabaseUtils;
4 import android.database.sqlite.SQLiteProgram;
5 import com.xtremelabs.robolectric.internal.Implementation;
6 import com.xtremelabs.robolectric.internal.Implements;
7 
8 @Implements(DatabaseUtils.class)
9 public class ShadowDatabaseUtils {
10 
11     @Implementation
bindObjectToProgram(SQLiteProgram prog, int index, Object value)12     public static void bindObjectToProgram(SQLiteProgram prog, int index,
13                                            Object value) {
14         if (value == null) {
15             prog.bindNull(index);
16         } else if (value instanceof Double || value instanceof Float) {
17             prog.bindDouble(index, ((Number) value).doubleValue());
18         } else if (value instanceof Number) {
19             prog.bindLong(index, ((Number) value).longValue());
20         } else if (value instanceof Boolean) {
21             Boolean bool = (Boolean) value;
22             if (bool) {
23                 prog.bindLong(index, 1);
24             } else {
25                 prog.bindLong(index, 0);
26             }
27         } else if (value instanceof byte[]) {
28             prog.bindBlob(index, (byte[]) value);
29         } else {
30             prog.bindString(index, value.toString());
31         }
32     }
33 
34     @Implementation
sqlEscapeString( String value )35     public static String sqlEscapeString( String value ) {
36 		StringBuilder builder = new StringBuilder();
37 
38 		// SQLite quoting conventions are used.
39 		value = value.replaceAll( "'", "''" );
40 		builder.append( "'" ).append( value ).append( "'" );
41 
42 		return builder.toString();
43 	}
44 }
45