-
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.
- Loading branch information
Showing
6 changed files
with
154 additions
and
9 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,42 @@ | ||
// | ||
// Attachment.swift | ||
// MMEX | ||
// | ||
// Created 2024-09-26 by George Ef ([email protected]) | ||
// | ||
|
||
import Foundation | ||
import SQLite | ||
|
||
enum RefType: String, EnumCollateNoCase { | ||
case transaction = "Transaction" | ||
case stock = "Stock" | ||
case asset = "Asset" | ||
case account = "BankAccount" | ||
case scheduled = "RecurringTransaction" | ||
case payee = "Payee" | ||
case transactionSplit = "TransactionSplit" | ||
case scheduledSplit = "RecurringTransactionSplit" | ||
static let defaultValue = Self.transaction | ||
} | ||
|
||
struct AttachmentData: ExportableEntity { | ||
var id : Int64 = 0 | ||
var refType : RefType = RefType.defaultValue | ||
var refId : Int64 = 0 | ||
var description : String = "" | ||
var filename : String = "" | ||
} | ||
|
||
extension AttachmentData: DataProtocol { | ||
static let dataName = "Attachment" | ||
|
||
func shortDesc() -> String { | ||
"\(self.id)" | ||
} | ||
} | ||
|
||
extension AttachmentData { | ||
static let sampleData: [AttachmentData] = [ | ||
] | ||
} |
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,74 @@ | ||
// | ||
// AttachmentRepository.swift | ||
// MMEX | ||
// | ||
// Created 2024-09-26 by George Ef ([email protected]) | ||
// | ||
|
||
import Foundation | ||
import SQLite | ||
|
||
class AttachmentRepository: RepositoryProtocol { | ||
typealias RepositoryData = AttachmentData | ||
|
||
let db: Connection? | ||
init(db: Connection?) { | ||
self.db = db | ||
} | ||
|
||
static let repositoryName = "ATTACHMENT_V1" | ||
static let table = SQLite.Table(repositoryName) | ||
|
||
// column | type | other | ||
// -------------+---------+------ | ||
// ATTACHMENTID | INTEGER | PRIMARY KEY | ||
// REFTYPE | TEXT | NOT NULL (BankAccount, Asset, Stock, ...) | ||
// REFID | INTEGER | NOT NULL | ||
// DESCRIPTION | TEXT | COLLATE NOCASE | ||
// FILENAME | TEXT | NOT NULL COLLATE NOCASE | ||
|
||
// column expressions | ||
static let col_id = SQLite.Expression<Int64>("ATTACHMENTID") | ||
static let col_refType = SQLite.Expression<String>("REFTYPE") | ||
static let col_refId = SQLite.Expression<Int64>("REFID") | ||
static let col_description = SQLite.Expression<String?>("DESCRIPTION") | ||
static let col_filename = SQLite.Expression<String>("FILENAME") | ||
|
||
static func selectQuery(from table: SQLite.Table) -> SQLite.Table { | ||
return table.select( | ||
col_id, | ||
col_refType, | ||
col_refId, | ||
col_description, | ||
col_filename | ||
) | ||
} | ||
|
||
static func selectData(_ row: SQLite.Row) -> AttachmentData { | ||
return AttachmentData( | ||
id : row[col_id], | ||
refType : RefType(collateNoCase: row[col_refType]), | ||
refId : row[col_refId], | ||
description : row[col_description] ?? "", | ||
filename : row[col_filename] | ||
) | ||
} | ||
|
||
static func itemSetters(_ attachment: AttachmentData) -> [SQLite.Setter] { | ||
return [ | ||
col_refType <- attachment.refType.rawValue, | ||
col_refId <- attachment.refId, | ||
col_description <- attachment.description, | ||
col_filename <- attachment.filename | ||
] | ||
} | ||
} | ||
|
||
extension AttachmentRepository { | ||
// load all attachments | ||
func load() -> [AttachmentData] { | ||
return select(from: Self.table | ||
.order(Self.col_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
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