Last active
November 23, 2016 22:48
Revisions
-
joshfriend revised this gist
Nov 23, 2016 . No changes.There are no files selected for viewing
-
joshfriend created this gist
Nov 23, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ @DatabaseTable(tableName = Address.TABLE) class Address() : BaseModel() { companion object { const val TABLE = "address" const val NUMBER = "number" const val STREET = "street" const val DIRECTION = "direction" const val UNIT = "unit" const val CITY = "city" const val STATE = "state" const val ZIPCODE = "zipcode" } @DatabaseField(columnName = NUMBER) lateinit var number: String @DatabaseField(columnName = STREET) lateinit var street: String @DatabaseField(columnName = DIRECTION) var direction: String? = null @DatabaseField(columnName = UNIT) var unit: String? = null @DatabaseField(columnName = CITY) lateinit var city: String @DatabaseField(columnName = STATE) lateinit var state: String @DatabaseField(columnName = ZIPCODE) lateinit var zipcode: String } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ class AddressStore { @Inject lateinit internal var database: DatabaseHelper init { Injector.inject(this) } val streetLetterList: List<String> get() { val results = database.addressDao.queryBuilder() .selectRaw(Address.STREET) { substr(1, 1).upper().distinct().label(Address.STREET) } .order(by = Address.STREET, ascending = true) .queryRaw() return results.map { it.first() } } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ class ColumnExpressionBuilder(column: String) { var statement: String = "`$column`" private set fun distinct(): ColumnExpressionBuilder { statement = "DISTINCT($statement)" return this } fun upper(): ColumnExpressionBuilder { statement = "UPPER($statement)" return this } fun substr(start: Int, end: Int): ColumnExpressionBuilder { statement = "SUBSTR($statement, $start, $end)" return this } fun label(name: String): ColumnExpressionBuilder { statement = "$statement AS `$name`" return this } } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ @Suppress("NOTHING_TO_INLINE") inline fun <T, ID> QueryBuilder<T, ID>.order( by: String, ascending: Boolean = true ): QueryBuilder<T, ID> { return this.orderBy(by, ascending) } inline fun <T, ID> QueryBuilder<T, ID>.selectRaw( column: String, expr: ColumnExpressionBuilder.() -> ColumnExpressionBuilder ): QueryBuilder<T, ID> { return this.selectRaw(expr(ColumnExpressionBuilder(column)).statement) }