Created
January 3, 2017 19:37
-
-
Save izmajlowiczl/61ce2edc4d881b78ca06b45d91b91b17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.expensive.db; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class DatabaseSchemaTestHelper { | |
public static TableDescription getColumnDetailsOrNull(SQLiteDatabase db, String table, String column) { | |
Cursor c = queryForTableInfo(db, table); | |
while (c.moveToNext()) { | |
String name = ColumnProperties.getName(c); | |
if (name.equals(column)) { | |
return new TableDescription(name, | |
ColumnProperties.getType(c), | |
ColumnProperties.isNotNull(c), | |
ColumnProperties.isPrimaryKey(c)); | |
} | |
} | |
return null; | |
} | |
public static List<String> listColumnNames(SQLiteDatabase db, String tableName) { | |
List<String> columns = new ArrayList<>(); | |
Cursor c = queryForTableInfo(db, tableName); | |
while (c.moveToNext()) { | |
columns.add(c.getString(c.getColumnIndex("name"))); | |
} | |
return columns; | |
} | |
public static int getCurrentDatabaseVersion(SQLiteDatabase db) { | |
Cursor c = db.rawQuery("pragma user_version;", null); | |
if (!c.moveToNext()) { | |
throw new RuntimeException(String.format("failed executing: %s", "pragma user_version;")); | |
} | |
return c.getInt(0); | |
} | |
private static Cursor queryForTableInfo(SQLiteDatabase db, String tableName) { | |
return db.rawQuery("pragma table_info(" + tableName + ");", null); | |
} | |
public static final class TableDescription { | |
final String name; | |
final String type; | |
final boolean isNotNull; | |
final boolean isPk; | |
private TableDescription(String name, String type, boolean isNotNull, boolean isPk) { | |
this.name = name; | |
this.type = type; | |
this.isNotNull = isNotNull; | |
this.isPk = isPk; | |
} | |
} | |
public static final class ColumnProperties { | |
private static final String NAME = "name"; | |
private static final String TYPE = "type"; | |
private static final String PK = "pk"; | |
private static final String NOT_NULL = "notnull"; | |
public static String getName(Cursor c) { | |
return c.getString(c.getColumnIndex(NAME)); | |
} | |
public static String getType(Cursor c) { | |
return c.getString(c.getColumnIndex(TYPE)); | |
} | |
public static boolean isPrimaryKey(Cursor c) { | |
return c.getInt(c.getColumnIndex(PK)) == 1; | |
} | |
/** | |
* Not Null is kinda stupid.. it works only if there is explicit declaration of NOT NULL. | |
* It should work as well for each PK columns (which are not null by default), but it doesn't | |
*/ | |
public static boolean isNotNull(Cursor c) { | |
return c.getInt(c.getColumnIndex(NOT_NULL)) == 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment