From 7c13cc26fc95b6908b0e6866318f0ea50e06e198 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Thu, 30 Jun 2016 12:58:19 +1000 Subject: [PATCH] Create `Schema` interface to make it simpler to replace string literals with constants. Right now, table names are in `DBHelper.TABLE_*` constants, and each tables fields are in `*Provider.DataColumns.*` constants. This brings them all into a predictable location. In addition, it makes it easier to statically import `Schema` so that instead of, e.g., * `AppProvider.DataColumns.PACKAGE_NAME` We can choose one of the following, based on our current context: * `Schema.AppTable.Cols.PACKAGE_NAME` * `AppTable.Cols.PACKAGE_NAME` * `Cols.PACKAGE_NAME` In the worst case, it isa couple of chars shorter than now. In the best case, if we are writing a class that primarily deals with Apps (e.g. App.java or AppProvider.java) then we get a big win with just `Cols.PACKAGE_NAME`. Having these things slightly shorter may seem like it is pointless, but the length of each constant probably contributed to my lack of willingness to use constants instead of string literals when constructing queries. In the future, this should be moved towards something more akin to: > http://openhms.sourceforge.net/sqlbuilder/ and I hope that extracting all the schema stuff into one interface may help that. --- .../java/org/fdroid/fdroid/data/Schema.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 app/src/main/java/org/fdroid/fdroid/data/Schema.java diff --git a/app/src/main/java/org/fdroid/fdroid/data/Schema.java b/app/src/main/java/org/fdroid/fdroid/data/Schema.java new file mode 100644 index 000000000..0dd7f98b6 --- /dev/null +++ b/app/src/main/java/org/fdroid/fdroid/data/Schema.java @@ -0,0 +1,48 @@ +package org.fdroid.fdroid.data; + +/** + * The authoritative reference to each table/column which should exist in the database. + * Constants from this interface should be used in preference to string literals when referring to + * the tables/columns in the database. + */ +interface Schema { + + interface AppTable { + String NAME = DBHelper.TABLE_APP; + interface Cols extends AppProvider.DataColumns {} + } + + interface ApkTable { + String NAME = DBHelper.TABLE_APK; + interface Cols extends ApkProvider.DataColumns {} + } + + interface RepoTable { + String NAME = DBHelper.TABLE_REPO; + interface Cols extends BaseColumns { + + String ADDRESS = "address"; + String NAME = "name"; + String DESCRIPTION = "description"; + String IN_USE = "inuse"; + String PRIORITY = "priority"; + String SIGNING_CERT = "pubkey"; + String FINGERPRINT = "fingerprint"; + String MAX_AGE = "maxage"; + String LAST_ETAG = "lastetag"; + String LAST_UPDATED = "lastUpdated"; + String VERSION = "version"; + String IS_SWAP = "isSwap"; + String USERNAME = "username"; + String PASSWORD = "password"; + String TIMESTAMP = "timestamp"; + + String[] ALL = { + _ID, ADDRESS, NAME, DESCRIPTION, IN_USE, PRIORITY, SIGNING_CERT, + FINGERPRINT, MAX_AGE, LAST_UPDATED, LAST_ETAG, VERSION, IS_SWAP, + USERNAME, PASSWORD, TIMESTAMP, + }; + } + } + +}