Skip to content

Commit

Permalink
rename table repositoryTable
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeef committed Sep 22, 2024
1 parent 6017837 commit 1dc5cbb
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 26 deletions.
15 changes: 10 additions & 5 deletions MMEX/Protocols/RepositoryProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol RepositoryProtocol {
associatedtype RepositoryItem: ModelProtocol

static var repositoryName: String { get }
static var table: SQLite.Table { get }
static var repositoryTable: SQLite.Table { get }
static func selectQuery(from table: SQLite.Table) -> SQLite.Table
static func selectResult(_ row: SQLite.Row) -> RepositoryItem
static var col_id: SQLite.Expression<Int64> { get }
Expand All @@ -22,7 +22,7 @@ protocol RepositoryProtocol {
}

extension RepositoryProtocol {
func select(table: SQLite.Table = Self.table) -> [RepositoryItem] {
func select(table: SQLite.Table = Self.repositoryTable) -> [RepositoryItem] {
guard let db else { return [] }
do {
var results: [RepositoryItem] = []
Expand All @@ -40,7 +40,8 @@ extension RepositoryProtocol {
func insert(_ item: inout RepositoryItem) -> Bool {
guard let db else { return false }
do {
let query = Self.table.insert(Self.itemSetters(item))
let query = Self.repositoryTable
.insert(Self.itemSetters(item))
let rowid = try db.run(query)
item.id = rowid
print("Successfully added \(RepositoryItem.modelName): \(item.shortDesc())")
Expand All @@ -54,7 +55,9 @@ extension RepositoryProtocol {
func update(_ item: RepositoryItem) -> Bool {
guard let db else { return false }
do {
let query = Self.table.filter(Self.col_id == item.id).update(Self.itemSetters(item))
let query = Self.repositoryTable
.filter(Self.col_id == item.id)
.update(Self.itemSetters(item))
try db.run(query)
print("Successfully updated \(RepositoryItem.modelName): \(item.shortDesc())")
return true
Expand All @@ -67,7 +70,9 @@ extension RepositoryProtocol {
func delete(_ item: RepositoryItem) -> Bool {
guard let db else { return false }
do {
let query = Self.table.filter(Self.col_id == item.id).delete()
let query = Self.repositoryTable
.filter(Self.col_id == item.id)
.delete()
try db.run(query)
print("Successfully deleted \(RepositoryItem.modelName): \(item.shortDesc())")
return true
Expand Down
4 changes: 2 additions & 2 deletions MMEX/Repositories/AccountRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension AccountRepository: RepositoryProtocol {
typealias RepositoryItem = Account

static let repositoryName = "ACCOUNTLIST_V1"
static let table = SQLite.Table(repositoryName)
static let repositoryTable = SQLite.Table(repositoryName)

// column | type | other
// ----------------+---------+------
Expand Down Expand Up @@ -158,7 +158,7 @@ extension AccountRepository: RepositoryProtocol {

extension AccountRepository {
func load() -> [Account] {
return select(table: Self.table
return select(table: Self.repositoryTable
.order(Self.col_name)
)
}
Expand Down
4 changes: 2 additions & 2 deletions MMEX/Repositories/AssetRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension AssetRepository: RepositoryProtocol {
typealias RepositoryItem = Asset

static let repositoryName = "ASSETS_V1"
static let table = SQLite.Table(repositoryName)
static let repositoryTable = SQLite.Table(repositoryName)

// column | type | other
// ----------------+---------+------
Expand Down Expand Up @@ -103,7 +103,7 @@ extension AssetRepository: RepositoryProtocol {
}

extension AssetRepository {
func load() -> [Asset] { select(table: Self.table
func load() -> [Asset] { select(table: Self.repositoryTable
.order(Self.col_type, Self.col_status.desc, Self.col_name)
) }
}
4 changes: 2 additions & 2 deletions MMEX/Repositories/CategoryRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension CategoryRepository: RepositoryProtocol {
typealias RepositoryItem = Category

static let repositoryName = "CATEGORY_V1"
static let table = SQLite.Table(repositoryName)
static let repositoryTable = SQLite.Table(repositoryName)

// column | type | other
// ----------+---------+------
Expand Down Expand Up @@ -65,6 +65,6 @@ extension CategoryRepository: RepositoryProtocol {
extension CategoryRepository {
// load all categories
func load() -> [Category] {
return select(table: Self.table)
return select(table: Self.repositoryTable)
}
}
4 changes: 2 additions & 2 deletions MMEX/Repositories/CurrencyRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension CurrencyRepository: RepositoryProtocol {
typealias RepositoryItem = Currency

static let repositoryName = "CURRENCYFORMATS_V1"
static let table = SQLite.Table(repositoryName)
static let repositoryTable = SQLite.Table(repositoryName)

// column | type | other
// ----------------+---------+------
Expand Down Expand Up @@ -109,7 +109,7 @@ extension CurrencyRepository: RepositoryProtocol {
extension CurrencyRepository {
// load all currencies
func load() -> [Currency] {
return select(table: Self.table
return select(table: Self.repositoryTable
.order(Self.col_name)
)
}
Expand Down
10 changes: 5 additions & 5 deletions MMEX/Repositories/InfotableRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension InfotableRepository: RepositoryProtocol {
typealias RepositoryItem = Infotable

static let repositoryName = "INFOTABLE_V1"
static let table = SQLite.Table(repositoryName)
static let repositoryTable = SQLite.Table(repositoryName)

// column | type | other
// ----------+---------+------
Expand Down Expand Up @@ -69,7 +69,7 @@ extension InfotableRepository {
do {
var results: [InfoKey: Infotable] = [:]
for key in keys {
if let row = try db.pluck(Self.selectQuery(from: Self.table
if let row = try db.pluck(Self.selectQuery(from: Self.repositoryTable
.filter(Self.col_name == key.rawValue)
) ) {
results[key] = Self.selectResult(row)
Expand All @@ -91,7 +91,7 @@ extension InfotableRepository {
func getValue<T>(for key: String, as type: T.Type) -> T? {
guard let db else { return nil }
do {
if let row = try db.pluck(Self.selectQuery(from: Self.table
if let row = try db.pluck(Self.selectQuery(from: Self.repositoryTable
.filter(Self.col_name == key)
) ) {
let value = row[Self.col_value]
Expand Down Expand Up @@ -121,7 +121,7 @@ extension InfotableRepository {
return
}

let query = InfotableRepository.table.filter(Self.col_name == key)
let query = Self.repositoryTable.filter(Self.col_name == key)
do {
if let _ = try db.pluck(query) {
// Update existing setting
Expand All @@ -130,7 +130,7 @@ extension InfotableRepository {
) )
} else {
// Insert new setting
try db.run(Self.table.insert(
try db.run(Self.repositoryTable.insert(
Self.col_name <- key,
Self.col_value <- stringValue
) )
Expand Down
4 changes: 2 additions & 2 deletions MMEX/Repositories/PayeeRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension PayeeRepository: RepositoryProtocol {
typealias RepositoryItem = Payee

static let repositoryName = "PAYEE_V1"
static let table = SQLite.Table(repositoryName)
static let repositoryTable = SQLite.Table(repositoryName)

// column | type | other
// ----------+---------+------
Expand Down Expand Up @@ -84,7 +84,7 @@ extension PayeeRepository: RepositoryProtocol {

extension PayeeRepository {
func load() -> [Payee] {
return select(table: Self.table
return select(table: Self.repositoryTable
.order(Self.col_active.desc, Self.col_name)
)
}
Expand Down
4 changes: 2 additions & 2 deletions MMEX/Repositories/StockRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension StockRepository: RepositoryProtocol {
typealias RepositoryItem = Stock

static let repositoryName = "STOCK_V1"
static let table = SQLite.Table(repositoryName)
static let repositoryTable = SQLite.Table(repositoryName)

// column | type | other
// --------------+---------+------
Expand Down Expand Up @@ -107,7 +107,7 @@ extension StockRepository: RepositoryProtocol {

extension StockRepository {
func load() -> [Stock] {
return select(table: Self.table
return select(table: Self.repositoryTable
.order(Self.col_name)
)
}
Expand Down
9 changes: 5 additions & 4 deletions MMEX/Repositories/TransactionRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension TransactionRepository: RepositoryProtocol {
typealias RepositoryItem = Transaction

static let repositoryName = "CHECKINGACCOUNT_V1"
static let table = SQLite.Table(repositoryName)
static let repositoryTable = SQLite.Table(repositoryName)

// column | type | other
// ------------------+---------+------
Expand Down Expand Up @@ -129,7 +129,7 @@ extension TransactionRepository: RepositoryProtocol {
extension TransactionRepository {
// load all transactions
func load() -> [Transaction] {
return select(table: Self.table)
return select(table: Self.repositoryTable)
}

// load recent transactions
Expand All @@ -138,9 +138,10 @@ extension TransactionRepository {
endDate: Date? = Date()
) -> [Transaction] {
let table = if let startDate {
Self.table.filter(Self.col_transDate >= startDate.ISO8601Format())
Self.repositoryTable
.filter(Self.col_transDate >= startDate.ISO8601Format())
} else {
Self.table
Self.repositoryTable
}
return select(table: table)
}
Expand Down

0 comments on commit 1dc5cbb

Please sign in to comment.