-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from moneymanagerex/new_models-2
Add TransactionSplit, ScheduledSplit
- Loading branch information
Showing
8 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// ScheduledSplit.swift | ||
// MMEX | ||
// | ||
// Created 2024-09-25 by George Ef ([email protected]) | ||
// | ||
|
||
import Foundation | ||
import SQLite | ||
|
||
struct ScheduledSplitData: ExportableEntity { | ||
var id : Int64 = 0 | ||
var schedId : Int64 = 0 | ||
var categId : Int64 = 0 | ||
var amount : Double = 0.0 | ||
var notes : String = "" | ||
} | ||
|
||
extension ScheduledSplitData: DataProtocol { | ||
static let dataName = "ScheduledSplit" | ||
|
||
func shortDesc() -> String { | ||
"\(self.id)" | ||
} | ||
} | ||
|
||
extension ScheduledSplitData { | ||
static let sampleData: [ScheduledSplitData] = [ | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// TransactionSplit.swift | ||
// MMEX | ||
// | ||
// Created 2024-09-25 by George Ef ([email protected]) | ||
// | ||
|
||
import Foundation | ||
import SQLite | ||
|
||
struct TransactionSplitData: ExportableEntity { | ||
var id : Int64 = 0 | ||
var transId : Int64 = 0 | ||
var categId : Int64 = 0 | ||
var amount : Double = 0.0 | ||
var notes : String = "" | ||
} | ||
|
||
extension TransactionSplitData: DataProtocol { | ||
static let dataName = "TransactionSplit" | ||
|
||
func shortDesc() -> String { | ||
"\(self.id)" | ||
} | ||
} | ||
|
||
extension TransactionSplitData { | ||
static let sampleData: [TransactionSplitData] = [ | ||
] | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// ScheduledSplitRepository.swift | ||
// MMEX | ||
// | ||
// Created 2024-09-25 by George Ef ([email protected]) | ||
// | ||
|
||
import Foundation | ||
import SQLite | ||
|
||
class ScheduledSplitRepository: RepositoryProtocol { | ||
typealias RepositoryData = ScheduledSplitData | ||
|
||
let db: Connection? | ||
init(db: Connection?) { | ||
self.db = db | ||
} | ||
|
||
static let repositoryName = "BUDGETSPLITTRANSACTIONS_V1" | ||
static let table = SQLite.Table(repositoryName) | ||
|
||
// column | type | other | ||
// -----------------+---------+------ | ||
// SPLITTRANSID | INTEGER | PRIMARY KEY | ||
// TRANSID | INTEGER | NOT NULL | ||
// CATEGID | INTEGER | | ||
// SPLITTRANSAMOUNT | NUMERIC | | ||
// NOTES | TEXT | | ||
|
||
// column expressions | ||
static let col_id = SQLite.Expression<Int64>("SPLITTRANSID") | ||
static let col_transId = SQLite.Expression<Int64>("TRANSID") | ||
static let col_categId = SQLite.Expression<Int64?>("CATEGID") | ||
static let col_amount = SQLite.Expression<Double?>("SPLITTRANSAMOUNT") | ||
static let col_notes = SQLite.Expression<String?>("NOTES") | ||
|
||
// cast NUMERIC to REAL | ||
static let cast_amount = cast(col_amount) as SQLite.Expression<Double?> | ||
|
||
static func selectQuery(from table: SQLite.Table) -> SQLite.Table { | ||
return table.select( | ||
col_id, | ||
col_transId, | ||
col_categId, | ||
cast_amount, | ||
col_notes | ||
) | ||
} | ||
|
||
static func selectData(_ row: SQLite.Row) -> ScheduledSplitData { | ||
return ScheduledSplitData( | ||
id : row[col_id], | ||
schedId : row[col_transId], | ||
categId : row[col_categId] ?? -1, | ||
amount : row[cast_amount] ?? 0, | ||
notes : row[col_notes] ?? "" | ||
) | ||
} | ||
|
||
static func itemSetters(_ split: ScheduledSplitData) -> [SQLite.Setter] { | ||
return [ | ||
col_transId <- split.schedId, | ||
col_categId <- split.categId, | ||
col_amount <- split.amount, | ||
col_notes <- split.notes | ||
] | ||
} | ||
} | ||
|
||
extension ScheduledSplitRepository { | ||
// load all splits | ||
func load() -> [ScheduledSplitData] { | ||
return select(from: Self.table | ||
.order(Self.col_transId, Self.col_id) | ||
) | ||
} | ||
|
||
// load splits of a scheduled transaction | ||
func load(forScheduledId schedId: Int64) -> [ScheduledSplitData] { | ||
return select(from: Self.table | ||
.filter(Self.col_transId == schedId) | ||
.order(Self.col_id) | ||
) | ||
} | ||
func load(for sched: ScheduledData) -> [ScheduledSplitData] { | ||
return load(forScheduledId: sched.id) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// | ||
// TransactionSplitRepository.swift | ||
// MMEX | ||
// | ||
// Created 2024-09-25 by George Ef ([email protected]) | ||
// | ||
|
||
import Foundation | ||
import SQLite | ||
|
||
class TransactionSplitRepository: RepositoryProtocol { | ||
typealias RepositoryData = TransactionSplitData | ||
|
||
let db: Connection? | ||
init(db: Connection?) { | ||
self.db = db | ||
} | ||
|
||
static let repositoryName = "SPLITTRANSACTIONS_V1" | ||
static let table = SQLite.Table(repositoryName) | ||
|
||
// column | type | other | ||
// -----------------+---------+------ | ||
// SPLITTRANSID | INTEGER | PRIMARY KEY | ||
// TRANSID | INTEGER | NOT NULL | ||
// CATEGID | INTEGER | | ||
// SPLITTRANSAMOUNT | NUMERIC | | ||
// NOTES | TEXT | | ||
|
||
// column expressions | ||
static let col_id = SQLite.Expression<Int64>("SPLITTRANSID") | ||
static let col_transId = SQLite.Expression<Int64>("TRANSID") | ||
static let col_categId = SQLite.Expression<Int64?>("CATEGID") | ||
static let col_amount = SQLite.Expression<Double?>("SPLITTRANSAMOUNT") | ||
static let col_notes = SQLite.Expression<String?>("NOTES") | ||
|
||
// cast NUMERIC to REAL | ||
static let cast_amount = cast(col_amount) as SQLite.Expression<Double?> | ||
|
||
static func selectQuery(from table: SQLite.Table) -> SQLite.Table { | ||
return table.select( | ||
col_id, | ||
col_transId, | ||
col_categId, | ||
cast_amount, | ||
col_notes | ||
) | ||
} | ||
|
||
static func selectData(_ row: SQLite.Row) -> TransactionSplitData { | ||
return TransactionSplitData( | ||
id : row[col_id], | ||
transId : row[col_transId], | ||
categId : row[col_categId] ?? -1, | ||
amount : row[cast_amount] ?? 0, | ||
notes : row[col_notes] ?? "" | ||
) | ||
} | ||
|
||
static func itemSetters(_ split: TransactionSplitData) -> [SQLite.Setter] { | ||
return [ | ||
col_transId <- split.transId, | ||
col_categId <- split.categId, | ||
col_amount <- split.amount, | ||
col_notes <- split.notes | ||
] | ||
} | ||
} | ||
|
||
extension TransactionSplitRepository { | ||
// load all splits | ||
func load() -> [TransactionSplitData] { | ||
return select(from: Self.table | ||
.order(Self.col_transId, Self.col_id) | ||
) | ||
} | ||
|
||
// load splits of a transaction | ||
func load(forTransactionId transId: Int64) -> [TransactionSplitData] { | ||
return select(from: Self.table | ||
.filter(Self.col_transId == transId) | ||
.order(Self.col_id) | ||
) | ||
} | ||
func load(for trans: TransactionData) -> [TransactionSplitData] { | ||
return load(forTransactionId: trans.id) | ||
} | ||
} |