From eeca7e2e6c1429dd237777f4d8eb89b057cdc6ad Mon Sep 17 00:00:00 2001 From: salmonumbrella <182032677+salmonumbrella@users.noreply.github.com> Date: Thu, 28 May 2026 20:28:47 -0700 Subject: [PATCH 1/4] Add Beancount driver plugin --- .gitignore | 7 + .../BeancountLedgerParser.swift | 453 +++++++++++ .../BeancountPlugin.swift | 73 ++ .../BeancountPluginDriver.swift | 748 ++++++++++++++++++ Plugins/BeancountDriverPlugin/Info.plist | 8 + README.md | 1 + README.vi.md | 1 + TablePro.xcodeproj/project.pbxproj | 177 +++++ ...ginMetadataRegistry+RegistryDefaults.swift | 76 ++ TablePro/Info.plist | 34 + .../BeancountLedgerParser.swift | 1 + .../BeancountPluginDriver.swift | 1 + .../BeancountDriverMetadataTests.swift | 57 ++ .../Plugins/BeancountLedgerParserTests.swift | 92 +++ .../Plugins/BeancountPluginDriverTests.swift | 150 ++++ scripts/download-rustledger.sh | 146 ++++ 16 files changed, 2025 insertions(+) create mode 100644 Plugins/BeancountDriverPlugin/BeancountLedgerParser.swift create mode 100644 Plugins/BeancountDriverPlugin/BeancountPlugin.swift create mode 100644 Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift create mode 100644 Plugins/BeancountDriverPlugin/Info.plist create mode 120000 TableProTests/PluginTestSources/BeancountLedgerParser.swift create mode 120000 TableProTests/PluginTestSources/BeancountPluginDriver.swift create mode 100644 TableProTests/Plugins/BeancountDriverMetadataTests.swift create mode 100644 TableProTests/Plugins/BeancountLedgerParserTests.swift create mode 100644 TableProTests/Plugins/BeancountPluginDriverTests.swift create mode 100755 scripts/download-rustledger.sh diff --git a/.gitignore b/.gitignore index 2c9051166..150b05c4f 100644 --- a/.gitignore +++ b/.gitignore @@ -150,7 +150,14 @@ Libs/*.a Libs/.downloaded Libs/dylibs/ Libs/ios/ +Libs/rustledger/ fix-1322-plugin-abi-and-registry-overhaul.diff # Issue analysis blueprints (local only) .analysis/ + +# Local planning and assistant history +.specstory/ +.planning/ +.plans/ +planning/ diff --git a/Plugins/BeancountDriverPlugin/BeancountLedgerParser.swift b/Plugins/BeancountDriverPlugin/BeancountLedgerParser.swift new file mode 100644 index 000000000..44729400e --- /dev/null +++ b/Plugins/BeancountDriverPlugin/BeancountLedgerParser.swift @@ -0,0 +1,453 @@ +// +// BeancountLedgerParser.swift +// BeancountDriverPlugin +// + +import Foundation + +struct BeancountLedger: Sendable { + let transactions: [BeancountTransaction] + let postings: [BeancountPosting] + let accounts: [BeancountAccount] + let prices: [BeancountPrice] + let balances: [BeancountBalance] + let sourceFiles: [URL] +} + +struct BeancountTransaction: Sendable { + let id: Int + let date: String + let flag: String + let payee: String? + let narration: String? + let sourceFile: URL + let line: Int +} + +struct BeancountPosting: Sendable { + let id: Int + let transactionId: Int + let date: String + let account: String + let amount: String? + let commodity: String? + let sourceFile: URL + let line: Int +} + +struct BeancountAccount: Sendable { + let name: String + let openDate: String + let currencies: String? + let sourceFile: URL + let line: Int +} + +struct BeancountPrice: Sendable { + let id: Int + let date: String + let commodity: String + let amount: String + let currency: String + let sourceFile: URL + let line: Int +} + +struct BeancountBalance: Sendable { + let id: Int + let date: String + let account: String + let amount: String + let commodity: String + let sourceFile: URL + let line: Int +} + +enum BeancountParserError: LocalizedError { + case includeCycle(String) + case unreadable(URL, Error) + + var errorDescription: String? { + switch self { + case .includeCycle(let path): + return "Beancount include cycle detected at \(path)" + case .unreadable(let url, let error): + return "Could not read \(url.path): \(error.localizedDescription)" + } + } +} + +final class BeancountLedgerParser { + private var visited: Set = [] + private var activeStack: Set = [] + private var sourceFiles: [URL] = [] + private var transactions: [BeancountTransaction] = [] + private var postings: [BeancountPosting] = [] + private var accountsByName: [String: BeancountAccount] = [:] + private var prices: [BeancountPrice] = [] + private var balances: [BeancountBalance] = [] + + func parse(fileURL: URL) throws -> BeancountLedger { + visited.removeAll() + activeStack.removeAll() + sourceFiles.removeAll() + transactions.removeAll() + postings.removeAll() + accountsByName.removeAll() + prices.removeAll() + balances.removeAll() + + try parseFile(fileURL.standardizedFileURL) + + return BeancountLedger( + transactions: transactions, + postings: postings, + accounts: accountsByName.values.sorted { $0.name < $1.name }, + prices: prices, + balances: balances, + sourceFiles: sourceFiles + ) + } + + private func parseFile(_ url: URL) throws { + let normalized = url.standardizedFileURL + if activeStack.contains(normalized) { + throw BeancountParserError.includeCycle(normalized.path) + } + guard !visited.contains(normalized) else { return } + + activeStack.insert(normalized) + defer { activeStack.remove(normalized) } + + let contents: String + do { + contents = try String(contentsOf: normalized, encoding: .utf8) + } catch { + throw BeancountParserError.unreadable(normalized, error) + } + + visited.insert(normalized) + sourceFiles.append(normalized) + + let lines = contents.components(separatedBy: .newlines) + var index = 0 + while index < lines.count { + let lineNumber = index + 1 + let rawLine = lines[index] + let trimmed = stripComment(rawLine).trimmingCharacters(in: .whitespaces) + defer { index += 1 } + + guard !trimmed.isEmpty else { continue } + + if let includePath = parseInclude(trimmed) { + let includeURLs = try resolveIncludeURLs( + includePath, + relativeTo: normalized.deletingLastPathComponent() + ) + for includeURL in includeURLs { + try parseFile(includeURL) + } + continue + } + + guard let date = parseDatePrefix(trimmed) else { continue } + let remainder = String(trimmed.dropFirst(11)) + + if remainder.hasPrefix("open ") { + parseOpen(remainder: remainder, date: date, sourceFile: normalized, line: lineNumber) + } else if remainder.hasPrefix("price ") { + parsePrice(remainder: remainder, date: date, sourceFile: normalized, line: lineNumber) + } else if remainder.hasPrefix("balance ") { + parseBalance(remainder: remainder, date: date, sourceFile: normalized, line: lineNumber) + } else if let flag = remainder.first, flag == "*" || flag == "!" { + let transactionId = transactions.count + 1 + let transaction = parseTransaction( + id: transactionId, + remainder: remainder, + date: date, + sourceFile: normalized, + line: lineNumber + ) + transactions.append(transaction) + + var postingIndex = index + 1 + while postingIndex < lines.count { + let postingLine = lines[postingIndex] + guard postingLine.first?.isWhitespace == true else { break } + if let posting = parsePosting( + postingLine, + id: postings.count + 1, + transactionId: transactionId, + date: date, + sourceFile: normalized, + line: postingIndex + 1 + ) { + postings.append(posting) + } + postingIndex += 1 + } + index = postingIndex - 1 + } + } + } + + private func parseInclude(_ line: String) -> String? { + guard line.hasPrefix("include ") else { return nil } + return quotedStrings(in: line).first + } + + private func resolveIncludeURLs(_ includePath: String, relativeTo directory: URL) throws -> [URL] { + guard containsGlobPattern(includePath) else { + return [resolveIncludeURL(includePath, relativeTo: directory)] + } + + let patternURL = resolveIncludeURL(includePath, relativeTo: directory) + let patternPath = patternURL.path + let searchRoot = globSearchRoot(for: patternPath) + let fileManager = FileManager.default + guard fileManager.fileExists(atPath: searchRoot.path) else { return [] } + + let regex = try NSRegularExpression(pattern: globRegex(for: patternPath)) + let enumerator = fileManager.enumerator( + at: searchRoot, + includingPropertiesForKeys: [.isRegularFileKey], + options: [.skipsHiddenFiles] + ) + + var matches: [URL] = [] + while let candidate = enumerator?.nextObject() as? URL { + let values = try? candidate.resourceValues(forKeys: [.isRegularFileKey]) + guard values?.isRegularFile == true else { continue } + + let path = candidate.standardizedFileURL.path + let range = NSRange(location: 0, length: (path as NSString).length) + if regex.firstMatch(in: path, range: range) != nil { + matches.append(candidate.standardizedFileURL) + } + } + + return matches.sorted { $0.path < $1.path } + } + + private func resolveIncludeURL(_ includePath: String, relativeTo directory: URL) -> URL { + if includePath.hasPrefix("/") { + return URL(fileURLWithPath: includePath).standardizedFileURL + } + return directory.appendingPathComponent(includePath).standardizedFileURL + } + + private func containsGlobPattern(_ path: String) -> Bool { + path.contains("*") || path.contains("?") || path.contains("[") + } + + private func globSearchRoot(for patternPath: String) -> URL { + let components = (patternPath as NSString).pathComponents + let prefix = components.prefix { !containsGlobPattern($0) } + let rootPath = NSString.path(withComponents: Array(prefix)) + return URL(fileURLWithPath: rootPath.isEmpty ? "/" : rootPath).standardizedFileURL + } + + private func globRegex(for patternPath: String) -> String { + let characters = Array(patternPath) + var regex = "^" + var index = 0 + + while index < characters.count { + let character = characters[index] + if character == "*" { + let nextIndex = index + 1 + if nextIndex < characters.count, characters[nextIndex] == "*" { + let slashIndex = index + 2 + if slashIndex < characters.count, characters[slashIndex] == "/" { + regex += "(?:.*/)?" + index += 3 + } else { + regex += ".*" + index += 2 + } + } else { + regex += "[^/]*" + index += 1 + } + } else if character == "?" { + regex += "[^/]" + index += 1 + } else if character == "[" { + let start = index + index += 1 + while index < characters.count, characters[index] != "]" { + index += 1 + } + if index < characters.count { + regex += String(characters[start...index]) + index += 1 + } else { + regex += NSRegularExpression.escapedPattern(for: String(character)) + } + } else { + regex += NSRegularExpression.escapedPattern(for: String(character)) + index += 1 + } + } + + return regex + "$" + } + + private func parseOpen(remainder: String, date: String, sourceFile: URL, line: Int) { + let parts = remainder.split(whereSeparator: \.isWhitespace).map(String.init) + guard parts.count >= 2 else { return } + let account = parts[1] + let currencies = parts.dropFirst(2).joined(separator: " ") + accountsByName[account] = BeancountAccount( + name: account, + openDate: date, + currencies: currencies.isEmpty ? nil : currencies, + sourceFile: sourceFile, + line: line + ) + } + + private func parsePrice(remainder: String, date: String, sourceFile: URL, line: Int) { + let parts = remainder.split(whereSeparator: \.isWhitespace).map(String.init) + guard parts.count >= 4 else { return } + prices.append(BeancountPrice( + id: prices.count + 1, + date: date, + commodity: parts[1], + amount: parts[2], + currency: parts[3], + sourceFile: sourceFile, + line: line + )) + } + + private func parseBalance(remainder: String, date: String, sourceFile: URL, line: Int) { + let parts = remainder.split(whereSeparator: \.isWhitespace).map(String.init) + guard parts.count >= 4 else { return } + balances.append(BeancountBalance( + id: balances.count + 1, + date: date, + account: parts[1], + amount: parts[2], + commodity: parts[3], + sourceFile: sourceFile, + line: line + )) + } + + private func parseTransaction( + id: Int, + remainder: String, + date: String, + sourceFile: URL, + line: Int + ) -> BeancountTransaction { + let quoted = quotedStrings(in: remainder) + return BeancountTransaction( + id: id, + date: date, + flag: String(remainder.prefix(1)), + payee: quoted.count >= 2 ? quoted[0] : nil, + narration: quoted.count >= 2 ? quoted[1] : quoted.first, + sourceFile: sourceFile, + line: line + ) + } + + private func parsePosting( + _ rawLine: String, + id: Int, + transactionId: Int, + date: String, + sourceFile: URL, + line: Int + ) -> BeancountPosting? { + let trimmed = stripComment(rawLine).trimmingCharacters(in: .whitespaces) + guard !trimmed.isEmpty, !trimmed.hasPrefix(";"), !trimmed.hasPrefix("#") else { return nil } + + let parts = trimmed.split(whereSeparator: \.isWhitespace).map(String.init) + guard let account = parts.first, account.contains(":") else { return nil } + let amount = parts.count >= 2 ? parts[1] : nil + let commodity = parts.count >= 3 ? parts[2] : nil + + return BeancountPosting( + id: id, + transactionId: transactionId, + date: date, + account: account, + amount: amount, + commodity: commodity, + sourceFile: sourceFile, + line: line + ) + } + + private func parseDatePrefix(_ line: String) -> String? { + guard line.count >= 11 else { return nil } + let prefix = String(line.prefix(10)) + let pattern = #"^\d{4}-\d{2}-\d{2}$"# + guard prefix.range(of: pattern, options: .regularExpression) != nil, + line.dropFirst(10).first?.isWhitespace == true else { + return nil + } + return prefix + } + + private func quotedStrings(in line: String) -> [String] { + var values: [String] = [] + var current = "" + var inQuote = false + var isEscaped = false + + for character in line { + if isEscaped { + current.append(character) + isEscaped = false + continue + } + if character == "\\" { + isEscaped = true + continue + } + if character == "\"" { + if inQuote { + values.append(current) + current = "" + } + inQuote.toggle() + continue + } + if inQuote { + current.append(character) + } + } + + return values + } + + private func stripComment(_ line: String) -> String { + var inQuote = false + var isEscaped = false + var result = "" + for character in line { + if isEscaped { + result.append(character) + isEscaped = false + continue + } + if character == "\\" { + result.append(character) + isEscaped = true + continue + } + if character == "\"" { + inQuote.toggle() + } + if character == ";" && !inQuote { + break + } + result.append(character) + } + return result + } +} diff --git a/Plugins/BeancountDriverPlugin/BeancountPlugin.swift b/Plugins/BeancountDriverPlugin/BeancountPlugin.swift new file mode 100644 index 000000000..62e918995 --- /dev/null +++ b/Plugins/BeancountDriverPlugin/BeancountPlugin.swift @@ -0,0 +1,73 @@ +// +// BeancountPlugin.swift +// BeancountDriverPlugin +// + +import Foundation +import TableProPluginKit + +final class BeancountPlugin: NSObject, TableProPlugin, DriverPlugin { + static let pluginName = "Beancount Driver" + static let pluginVersion = "1.0.0" + static let pluginDescription = "Read-only Beancount ledger support" + static let capabilities: [PluginCapability] = [.databaseDriver] + + static let databaseTypeId = "Beancount" + static let databaseDisplayName = "Beancount" + static let iconName = "beancount-icon" + static let defaultPort = 0 + + static let isDownloadable = true + static let pathFieldRole: PathFieldRole = .filePath + static let requiresAuthentication = false + static let supportsSSH = false + static let supportsSSL = false + static let connectionMode: ConnectionMode = .fileBased + static let urlSchemes: [String] = ["beancount"] + static let fileExtensions: [String] = ["beancount"] + static let brandColorHex = "#3F7D20" + static let supportsForeignKeys = false + static let supportsSchemaEditing = false + static let supportsDatabaseSwitching = false + static let supportsSchemaSwitching = false + static let supportsImport = false + static let supportsHealthMonitor = false + static let databaseGroupingStrategy: GroupingStrategy = .flat + static let tableEntityName = "Ledger Tables" + static let columnTypesByCategory: [String: [String]] = [ + "Integer": ["INTEGER"], + "String": ["TEXT"], + "Date": ["DATE"], + "Decimal": ["DECIMAL"] + ] + static let immutableColumns: [String] = [ + "id", "transaction_id", "date", "flag", "payee", "narration", + "account", "amount", "commodity", "currency", "source_file", "line" + ] + + static let sqlDialect: SQLDialectDescriptor? = SQLDialectDescriptor( + identifierQuote: "\"", + keywords: [ + "SELECT", "FROM", "WHERE", "JOIN", "INNER", "LEFT", "RIGHT", "OUTER", + "ON", "AND", "OR", "NOT", "IN", "LIKE", "BETWEEN", "AS", + "ORDER", "BY", "GROUP", "HAVING", "LIMIT", "OFFSET", + "WITH", "RECURSIVE", "UNION", "INTERSECT", "EXCEPT", + "CASE", "WHEN", "THEN", "ELSE", "END", "NULL", "IS", + "ASC", "DESC", "DISTINCT" + ], + functions: [ + "COUNT", "SUM", "AVG", "MAX", "MIN", + "COALESCE", "NULLIF", "ROUND", "ABS", + "DATE", "STRFTIME", "SUBSTR", "LOWER", "UPPER" + ], + dataTypes: ["INTEGER", "TEXT", "DATE", "DECIMAL"], + regexSyntax: .unsupported, + booleanLiteralStyle: .numeric, + likeEscapeStyle: .explicit, + paginationStyle: .limit + ) + + func createDriver(config: DriverConnectionConfig) -> any PluginDatabaseDriver { + BeancountPluginDriver(config: config) + } +} diff --git a/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift b/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift new file mode 100644 index 000000000..f18b3154d --- /dev/null +++ b/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift @@ -0,0 +1,748 @@ +// +// BeancountPluginDriver.swift +// BeancountDriverPlugin +// + +import Foundation +import SQLite3 +import TableProPluginKit + +enum BeancountDriverError: LocalizedError { + case notConnected + case connectionFailed(String) + case queryFailed(String) + case readOnly + case rustledgerUnavailable + + var errorDescription: String? { + switch self { + case .notConnected: + return "Not connected to Beancount ledger" + case .connectionFailed(let message): + return "Failed to open Beancount ledger: \(message)" + case .queryFailed(let message): + return message + case .readOnly: + return "Beancount ledgers are exposed as a read-only SQL database" + case .rustledgerUnavailable: + return "BQL requires the bundled rustledger helper" + } + } +} + +extension BeancountDriverError: PluginDriverError { + var pluginErrorMessage: String { errorDescription ?? "Beancount driver error" } +} + +private struct BeancountSQLiteResult { + let columns: [String] + let columnTypeNames: [String] + let rows: [[PluginCellValue]] + let rowsAffected: Int + let executionTime: TimeInterval + let isTruncated: Bool +} + +private struct BeancountSourceSignature: Equatable { + let modificationDate: Date? + let fileSize: UInt64? +} + +final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { + private let config: DriverConnectionConfig + private let lock = NSLock() + private var db: OpaquePointer? + private var ledgerURL: URL? + private var ledger: BeancountLedger? + private var sourceSignatures: [String: BeancountSourceSignature] = [:] + + var currentSchema: String? { nil } + var serverVersion: String? { "Beancount" } + var supportsSchemas: Bool { false } + var supportsTransactions: Bool { false } + var parameterStyle: ParameterStyle { .questionMark } + + init(config: DriverConnectionConfig) { + self.config = config + } + + func connect() async throws { + let path = expandPath(config.database) + let fileURL = URL(fileURLWithPath: path) + guard FileManager.default.fileExists(atPath: path) else { + throw BeancountDriverError.connectionFailed("File does not exist at \(path)") + } + + let parsed = try BeancountLedgerParser().parse(fileURL: fileURL) + let signatures = try Self.signatures(for: parsed.sourceFiles) + var handle: OpaquePointer? + guard sqlite3_open(":memory:", &handle) == SQLITE_OK, let handle else { + throw BeancountDriverError.connectionFailed("Could not initialize SQL projection") + } + + do { + try Self.load(parsed, into: handle) + } catch { + sqlite3_close(handle) + throw error + } + + lock.withLock { + db = handle + ledgerURL = fileURL + ledger = parsed + sourceSignatures = signatures + } + } + + func disconnect() { + lock.withLock { + if db != nil { + sqlite3_close(db) + db = nil + } + ledgerURL = nil + ledger = nil + sourceSignatures.removeAll() + } + } + + func ping() async throws { + _ = try await execute(query: "SELECT 1") + } + + func beginTransaction() async throws { + throw BeancountDriverError.readOnly + } + + func commitTransaction() async throws { + throw BeancountDriverError.readOnly + } + + func rollbackTransaction() async throws { + throw BeancountDriverError.readOnly + } + + func quoteIdentifier(_ name: String) -> String { + "\"\(name.replacingOccurrences(of: "\"", with: "\"\""))\"" + } + + func escapeStringLiteral(_ value: String) -> String { + value.replacingOccurrences(of: "'", with: "''") + } + + func execute(query: String) async throws -> PluginQueryResult { + if let bql = Self.extractBQLQuery(from: query) { + return try executeBQL(query: bql) + } + let raw = try executeSQLite(query: query, parameters: []) + return PluginQueryResult( + columns: raw.columns, + columnTypeNames: raw.columnTypeNames, + rows: raw.rows, + rowsAffected: raw.rowsAffected, + executionTime: raw.executionTime, + isTruncated: raw.isTruncated + ) + } + + func executeParameterized(query: String, parameters: [PluginCellValue]) async throws -> PluginQueryResult { + if Self.extractBQLQuery(from: query) != nil { + throw BeancountDriverError.queryFailed("BQL queries do not support SQL parameters") + } + let raw = try executeSQLite(query: query, parameters: parameters) + return PluginQueryResult( + columns: raw.columns, + columnTypeNames: raw.columnTypeNames, + rows: raw.rows, + rowsAffected: raw.rowsAffected, + executionTime: raw.executionTime, + isTruncated: raw.isTruncated + ) + } + + func fetchRowCount(query: String) async throws -> Int { + let escaped = query.replacingOccurrences(of: ";", with: "") + let result = try await execute(query: "SELECT COUNT(*) FROM (\(escaped))") + guard let text = result.rows.first?.first?.asText, let count = Int(text) else { return 0 } + return count + } + + func fetchRows(query: String, offset: Int, limit: Int) async throws -> PluginQueryResult { + try await execute(query: "SELECT * FROM (\(query)) LIMIT \(limit) OFFSET \(offset)") + } + + func fetchTables(schema: String?) async throws -> [PluginTableInfo] { + let result = try await execute(query: """ + SELECT name, type FROM sqlite_master + WHERE type IN ('table', 'view') + AND name NOT LIKE 'sqlite_%' + ORDER BY name + """) + return result.rows.compactMap { row in + guard let name = row[safe: 0]?.asText else { return nil } + let type = row[safe: 1]?.asText?.uppercased() ?? "TABLE" + return PluginTableInfo(name: name, type: type) + } + } + + func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { + let result = try await execute(query: "PRAGMA table_info('\(escapeStringLiteral(table))')") + return result.rows.compactMap { row in + guard row.count >= 6, + let name = row[1].asText, + let type = row[2].asText else { + return nil + } + return PluginColumnInfo( + name: name, + dataType: type, + isNullable: row[3].asText == "0", + isPrimaryKey: (row[5].asText ?? "0") != "0", + defaultValue: row[4].asText + ) + } + } + + func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { [] } + func fetchForeignKeys(table: String, schema: String?) async throws -> [PluginForeignKeyInfo] { [] } + + func fetchTableDDL(table: String, schema: String?) async throws -> String { + let result = try await execute(query: """ + SELECT sql FROM sqlite_master + WHERE type = 'table' AND name = '\(escapeStringLiteral(table))' + """) + guard let ddl = result.rows.first?.first?.asText else { + throw BeancountDriverError.queryFailed("Failed to fetch DDL for table '\(table)'") + } + return ddl.hasSuffix(";") ? ddl : ddl + ";" + } + + func fetchViewDefinition(view: String, schema: String?) async throws -> String { + let result = try await execute(query: """ + SELECT sql FROM sqlite_master + WHERE type = 'view' AND name = '\(escapeStringLiteral(view))' + """) + return result.rows.first?.first?.asText ?? "" + } + + func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata { + let result = try await execute(query: "SELECT COUNT(*) FROM \(quoteIdentifier(table))") + let rowCount = result.rows.first?.first?.asText.flatMap(Int64.init) + return PluginTableMetadata(tableName: table, rowCount: rowCount, engine: "Beancount") + } + + func fetchDatabases() async throws -> [String] { [] } + + func fetchDatabaseMetadata(_ database: String) async throws -> PluginDatabaseMetadata { + PluginDatabaseMetadata(name: database) + } + + func fetchApproximateRowCount(table: String, schema: String?) async throws -> Int? { + let result = try await execute(query: "SELECT COUNT(*) FROM \(quoteIdentifier(table))") + return result.rows.first?.first?.asText.flatMap(Int.init) + } + + func buildBrowseQuery( + table: String, + sortColumns: [(columnIndex: Int, ascending: Bool)], + columns: [String], + limit: Int, + offset: Int + ) -> String? { + var query = "SELECT * FROM \(quoteIdentifier(table))" + if !sortColumns.isEmpty, !columns.isEmpty { + let order = sortColumns.compactMap { sort -> String? in + guard columns.indices.contains(sort.columnIndex) else { return nil } + return "\(quoteIdentifier(columns[sort.columnIndex])) \(sort.ascending ? "ASC" : "DESC")" + } + if !order.isEmpty { + query += " ORDER BY " + order.joined(separator: ", ") + } + } + query += " LIMIT \(limit) OFFSET \(offset)" + return query + } + + func defaultExportQuery(table: String) -> String? { + "SELECT * FROM \(quoteIdentifier(table))" + } + + func streamRows(query: String) -> AsyncThrowingStream { + AsyncThrowingStream { continuation in + do { + let result: BeancountSQLiteResult + if let bql = Self.extractBQLQuery(from: query) { + let bqlResult = try executeBQL(query: bql) + result = BeancountSQLiteResult( + columns: bqlResult.columns, + columnTypeNames: bqlResult.columnTypeNames, + rows: bqlResult.rows, + rowsAffected: bqlResult.rowsAffected, + executionTime: bqlResult.executionTime, + isTruncated: bqlResult.isTruncated + ) + } else { + result = try executeSQLite(query: query, parameters: []) + } + continuation.yield(.header(PluginStreamHeader( + columns: result.columns, + columnTypeNames: result.columnTypeNames, + estimatedRowCount: result.rows.count + ))) + continuation.yield(.rows(result.rows)) + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + } + + private func executeBQL(query: String) throws -> PluginQueryResult { + let ledgerPath = try lock.withLock { () -> String in + guard let ledgerURL else { throw BeancountDriverError.notConnected } + return ledgerURL.path + } + let rustledgerPath = try Self.rustledgerExecutablePath() + let start = Date() + + let process = Process() + process.executableURL = URL(fileURLWithPath: rustledgerPath) + process.arguments = ["query", "-f", "json", "--no-errors", ledgerPath, query] + + let stdout = Pipe() + let stderr = Pipe() + process.standardOutput = stdout + process.standardError = stderr + + try process.run() + process.waitUntilExit() + + let output = stdout.fileHandleForReading.readDataToEndOfFile() + let errorOutput = stderr.fileHandleForReading.readDataToEndOfFile() + guard process.terminationStatus == 0 else { + let message = String(data: errorOutput, encoding: .utf8)? + .trimmingCharacters(in: .whitespacesAndNewlines) + throw BeancountDriverError.queryFailed(message?.isEmpty == false ? message! : "rustledger query failed") + } + + return try Self.decodeRustledgerQueryOutput( + output, + executionTime: Date().timeIntervalSince(start) + ) + } + + private func executeSQLite(query: String, parameters: [PluginCellValue]) throws -> BeancountSQLiteResult { + guard Self.isReadOnlyQuery(query) else { + throw BeancountDriverError.readOnly + } + + return try lock.withLock { + try reloadProjectionIfNeeded() + guard let db = self.db else { throw BeancountDriverError.notConnected } + + let start = Date() + var statement: OpaquePointer? + guard sqlite3_prepare_v2(db, query, -1, &statement, nil) == SQLITE_OK else { + throw BeancountDriverError.queryFailed(String(cString: sqlite3_errmsg(db))) + } + defer { sqlite3_finalize(statement) } + + for (index, parameter) in parameters.enumerated() { + let position = Int32(index + 1) + switch parameter { + case .null: + sqlite3_bind_null(statement, position) + case .text(let value): + sqlite3_bind_text(statement, position, value, -1, SQLITE_TRANSIENT) + case .bytes(let data): + _ = data.withUnsafeBytes { buffer in + sqlite3_bind_blob(statement, position, buffer.baseAddress, Int32(data.count), SQLITE_TRANSIENT) + } + } + } + + let columnCount = sqlite3_column_count(statement) + let columns = (0.. String in + sqlite3_column_name(statement, index).map { String(cString: $0) } ?? "column_\(index)" + } + let columnTypeNames = (0.. String in + sqlite3_column_decltype(statement, index).map { String(cString: $0) } ?? "" + } + + var rows: [[PluginCellValue]] = [] + var truncated = false + + while true { + let step = sqlite3_step(statement) + if step == SQLITE_DONE { break } + guard step == SQLITE_ROW else { + throw BeancountDriverError.queryFailed(String(cString: sqlite3_errmsg(db))) + } + if rows.count >= PluginRowLimits.emergencyMax { + truncated = true + break + } + rows.append((0.. PluginCellValue { + let type = sqlite3_column_type(statement, column) + if type == SQLITE_NULL { + return .null + } + if type == SQLITE_BLOB { + let byteCount = Int(sqlite3_column_bytes(statement, column)) + guard byteCount > 0, let blob = sqlite3_column_blob(statement, column) else { + return .bytes(Data()) + } + return .bytes(Data(bytes: blob, count: byteCount)) + } + guard let text = sqlite3_column_text(statement, column) else { + return .null + } + return .text(String(cString: text)) + } + + private static func isReadOnlyQuery(_ query: String) -> Bool { + let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines) + guard !trimmed.isEmpty else { return true } + let lower = trimmed.lowercased() + return lower.hasPrefix("select") + || lower.hasPrefix("with") + || lower.hasPrefix("pragma table_info") + || lower.hasPrefix("pragma database_list") + || lower.hasPrefix("explain") + } + + private static func extractBQLQuery(from query: String) -> String? { + let trimmed = query.trimmingCharacters(in: .whitespacesAndNewlines) + let lowercased = trimmed.lowercased() + if lowercased.hasPrefix("bql:") { + return String(trimmed.dropFirst(4)).trimmingCharacters(in: .whitespacesAndNewlines) + } + if lowercased.hasPrefix("bql ") { + return String(trimmed.dropFirst(4)).trimmingCharacters(in: .whitespacesAndNewlines) + } + return nil + } + + private static func rustledgerExecutablePath() throws -> String { + let environment = ProcessInfo.processInfo.environment + if let path = environment["TABLEPRO_RUSTLEDGER_BINARY"], + FileManager.default.isExecutableFile(atPath: path) { + return path + } + + let bundleCandidates = [ + Bundle(for: BeancountPluginDriver.self).url(forResource: "rledger", withExtension: nil)?.path, + Bundle.main.builtInPlugInsURL? + .appendingPathComponent("BeancountDriver.tableplugin") + .appendingPathComponent("Contents/Resources/rledger") + .path + ].compactMap { $0 } + if let path = bundleCandidates.first(where: { FileManager.default.isExecutableFile(atPath: $0) }) { + return path + } + + let pathCandidates = [ + "/opt/homebrew/bin/rledger", + "/usr/local/bin/rledger" + ] + (environment["PATH"] ?? "") + .split(separator: ":") + .map { "\($0)/rledger" } + + if let path = pathCandidates.first(where: { FileManager.default.isExecutableFile(atPath: $0) }) { + return path + } + + throw BeancountDriverError.rustledgerUnavailable + } + + private static func decodeRustledgerQueryOutput( + _ data: Data, + executionTime: TimeInterval + ) throws -> PluginQueryResult { + let object = try JSONSerialization.jsonObject(with: data) + guard let dictionary = object as? [String: Any], + let columns = dictionary["columns"] as? [String], + let rawRows = dictionary["rows"] as? [[String: Any]] else { + throw BeancountDriverError.queryFailed("Invalid rustledger JSON output") + } + + let rows = rawRows.prefix(PluginRowLimits.emergencyMax).map { rawRow in + columns.map { column -> PluginCellValue in + guard let value = rawRow[column], !(value is NSNull) else { return .null } + return .text(rustledgerCellValue(value)) + } + } + + return PluginQueryResult( + columns: columns, + columnTypeNames: Array(repeating: "TEXT", count: columns.count), + rows: rows, + rowsAffected: 0, + executionTime: executionTime, + isTruncated: rawRows.count > rows.count + ) + } + + private static func rustledgerCellValue(_ value: Any) -> String { + if let string = value as? String { + return string + } + if let number = value as? NSNumber { + return number.stringValue + } + if let amount = value as? [String: Any], + let number = amount["number"] as? String, + let currency = amount["currency"] as? String { + return "\(number) \(currency)" + } + if let inventory = value as? [String: Any], + let positions = inventory["positions"] as? [[String: Any]] { + return positions.compactMap { position in + guard let number = position["number"] as? String, + let currency = position["currency"] as? String else { + return nil + } + return "\(number) \(currency)" + }.joined(separator: ", ") + } + if JSONSerialization.isValidJSONObject(value), + let data = try? JSONSerialization.data(withJSONObject: value, options: [.sortedKeys]), + let string = String(data: data, encoding: .utf8) { + return string + } + return String(describing: value) + } + + private static func load(_ ledger: BeancountLedger, into db: OpaquePointer) throws { + try exec(db, """ + CREATE TABLE transactions ( + id INTEGER PRIMARY KEY, + date DATE NOT NULL, + flag TEXT NOT NULL, + payee TEXT, + narration TEXT, + source_file TEXT NOT NULL, + line INTEGER NOT NULL + ); + CREATE TABLE postings ( + id INTEGER PRIMARY KEY, + transaction_id INTEGER NOT NULL, + date DATE NOT NULL, + account TEXT NOT NULL, + amount DECIMAL, + commodity TEXT, + source_file TEXT NOT NULL, + line INTEGER NOT NULL + ); + CREATE TABLE accounts ( + name TEXT PRIMARY KEY, + open_date DATE NOT NULL, + currencies TEXT, + source_file TEXT NOT NULL, + line INTEGER NOT NULL + ); + CREATE TABLE prices ( + id INTEGER PRIMARY KEY, + date DATE NOT NULL, + commodity TEXT NOT NULL, + amount DECIMAL NOT NULL, + currency TEXT NOT NULL, + source_file TEXT NOT NULL, + line INTEGER NOT NULL + ); + CREATE TABLE balances ( + id INTEGER PRIMARY KEY, + date DATE NOT NULL, + account TEXT NOT NULL, + amount DECIMAL NOT NULL, + commodity TEXT NOT NULL, + source_file TEXT NOT NULL, + line INTEGER NOT NULL + ); + CREATE TABLE source_files ( + path TEXT PRIMARY KEY + ); + """) + + for transaction in ledger.transactions { + try insert(db, sql: """ + INSERT INTO transactions (id, date, flag, payee, narration, source_file, line) + VALUES (?, ?, ?, ?, ?, ?, ?) + """, values: [ + String(transaction.id), + transaction.date, + transaction.flag, + transaction.payee, + transaction.narration, + transaction.sourceFile.path, + String(transaction.line) + ]) + } + for posting in ledger.postings { + try insert(db, sql: """ + INSERT INTO postings (id, transaction_id, date, account, amount, commodity, source_file, line) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) + """, values: [ + String(posting.id), + String(posting.transactionId), + posting.date, + posting.account, + posting.amount, + posting.commodity, + posting.sourceFile.path, + String(posting.line) + ]) + } + for account in ledger.accounts { + try insert(db, sql: """ + INSERT INTO accounts (name, open_date, currencies, source_file, line) + VALUES (?, ?, ?, ?, ?) + """, values: [ + account.name, + account.openDate, + account.currencies, + account.sourceFile.path, + String(account.line) + ]) + } + for price in ledger.prices { + try insert(db, sql: """ + INSERT INTO prices (id, date, commodity, amount, currency, source_file, line) + VALUES (?, ?, ?, ?, ?, ?, ?) + """, values: [ + String(price.id), + price.date, + price.commodity, + price.amount, + price.currency, + price.sourceFile.path, + String(price.line) + ]) + } + for balance in ledger.balances { + try insert(db, sql: """ + INSERT INTO balances (id, date, account, amount, commodity, source_file, line) + VALUES (?, ?, ?, ?, ?, ?, ?) + """, values: [ + String(balance.id), + balance.date, + balance.account, + balance.amount, + balance.commodity, + balance.sourceFile.path, + String(balance.line) + ]) + } + for sourceFile in ledger.sourceFiles { + try insert(db, sql: "INSERT INTO source_files (path) VALUES (?)", values: [sourceFile.path]) + } + + try exec(db, "PRAGMA query_only = ON") + } + + private static func exec(_ db: OpaquePointer, _ sql: String) throws { + var error: UnsafeMutablePointer? + guard sqlite3_exec(db, sql, nil, nil, &error) == SQLITE_OK else { + let message = error.map { String(cString: $0) } ?? String(cString: sqlite3_errmsg(db)) + if error != nil { + sqlite3_free(error) + } + throw BeancountDriverError.queryFailed(message) + } + } + + private static func insert(_ db: OpaquePointer, sql: String, values: [String?]) throws { + var statement: OpaquePointer? + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { + throw BeancountDriverError.queryFailed(String(cString: sqlite3_errmsg(db))) + } + defer { sqlite3_finalize(statement) } + + for (index, value) in values.enumerated() { + let position = Int32(index + 1) + if let value { + sqlite3_bind_text(statement, position, value, -1, SQLITE_TRANSIENT) + } else { + sqlite3_bind_null(statement, position) + } + } + + guard sqlite3_step(statement) == SQLITE_DONE else { + throw BeancountDriverError.queryFailed(String(cString: sqlite3_errmsg(db))) + } + } + + private static func signatures(for sourceFiles: [URL]) throws -> [String: BeancountSourceSignature] { + try sourceFiles.reduce(into: [:]) { signatures, fileURL in + let path = fileURL.path + let attributes = try FileManager.default.attributesOfItem(atPath: path) + signatures[path] = BeancountSourceSignature( + modificationDate: attributes[.modificationDate] as? Date, + fileSize: (attributes[.size] as? NSNumber)?.uint64Value + ) + } + } + + private func expandPath(_ path: String) -> String { + guard path.hasPrefix("~") else { return path } + return NSString(string: path).expandingTildeInPath + } +} + +private let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self) + +private extension NSLock { + func withLock(_ body: () throws -> T) rethrows -> T { + lock() + defer { unlock() } + return try body() + } +} + +private extension Array { + subscript(safe index: Int) -> Element? { + indices.contains(index) ? self[index] : nil + } +} diff --git a/Plugins/BeancountDriverPlugin/Info.plist b/Plugins/BeancountDriverPlugin/Info.plist new file mode 100644 index 000000000..c48cad80a --- /dev/null +++ b/Plugins/BeancountDriverPlugin/Info.plist @@ -0,0 +1,8 @@ + + + + + TableProPluginKitVersion + 16 + + diff --git a/README.md b/README.md index 8abab9c1e..e8375cd6c 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ TablePro is the missing fourth: native, multi-database, and open source. | MongoDB | Plugin | | Oracle Database | Plugin | | DuckDB | Plugin | +| Beancount | Plugin | | Cassandra / ScyllaDB | Plugin | | Etcd | Plugin | | Cloudflare D1 | Plugin | diff --git a/README.vi.md b/README.vi.md index 19ab87eb4..458911e77 100644 --- a/README.vi.md +++ b/README.vi.md @@ -82,6 +82,7 @@ TablePro là mảnh thứ tư còn thiếu: native, đa database, và mã nguồ | MongoDB | Plugin | | Oracle Database | Plugin | | DuckDB | Plugin | +| Beancount | Plugin | | Cassandra / ScyllaDB | Plugin | | Etcd | Plugin | | Cloudflare D1 | Plugin | diff --git a/TablePro.xcodeproj/project.pbxproj b/TablePro.xcodeproj/project.pbxproj index 9c9b74a42..2178f46a3 100644 --- a/TablePro.xcodeproj/project.pbxproj +++ b/TablePro.xcodeproj/project.pbxproj @@ -26,6 +26,8 @@ 5A868000A00000000 /* TableProPluginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A860000100000000 /* TableProPluginKit.framework */; }; 5A868000D00000000 /* PostgreSQLDriver.tableplugin in Copy Plug-Ins (12 items) */ = {isa = PBXBuildFile; fileRef = 5A868000100000000 /* PostgreSQLDriver.tableplugin */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 5A869000A00000000 /* TableProPluginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A860000100000000 /* TableProPluginKit.framework */; }; + 5ABC147400000000000001 /* TableProPluginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A860000100000000 /* TableProPluginKit.framework */; }; + 5ABC147400000000000002 /* BeancountDriver.tableplugin in Copy Plug-Ins (13 items) */ = {isa = PBXBuildFile; fileRef = 5ABC147400000000000003 /* BeancountDriver.tableplugin */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 5A86A000A00000000 /* TableProPluginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A860000100000000 /* TableProPluginKit.framework */; }; 5A86A000D00000000 /* CSVExport.tableplugin in Copy Plug-Ins (12 items) */ = {isa = PBXBuildFile; fileRef = 5A86A000100000000 /* CSVExport.tableplugin */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 5A86B000A00000000 /* TableProPluginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A860000100000000 /* TableProPluginKit.framework */; }; @@ -146,6 +148,20 @@ remoteGlobalIDString = 5A869000000000000; remoteInfo = DuckDBDriver; }; + 5ABC147400000000000004 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 5A1091BF2EF17EDC0055EA7C /* Project object */; + proxyType = 1; + remoteGlobalIDString = 5ABC147400000000000005; + remoteInfo = BeancountDriver; + }; + 5ABC14740000000000000F /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 5A1091BF2EF17EDC0055EA7C /* Project object */; + proxyType = 1; + remoteGlobalIDString = 5A860000000000000; + remoteInfo = TableProPluginKit; + }; 5A86A000B00000000 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 5A1091BF2EF17EDC0055EA7C /* Project object */; @@ -250,6 +266,7 @@ 5A862000D00000000 /* SQLiteDriver.tableplugin in Copy Plug-Ins (12 items) */, 5A863000D00000000 /* ClickHouseDriver.tableplugin in Copy Plug-Ins (12 items) */, 5A867000D00000000 /* RedisDriver.tableplugin in Copy Plug-Ins (12 items) */, + 5ABC147400000000000002 /* BeancountDriver.tableplugin in Copy Plug-Ins (13 items) */, 5A86A000D00000000 /* CSVExport.tableplugin in Copy Plug-Ins (12 items) */, 5A86B000D00000000 /* JSONExport.tableplugin in Copy Plug-Ins (12 items) */, 5A86C000D00000000 /* SQLExport.tableplugin in Copy Plug-Ins (12 items) */, @@ -288,6 +305,7 @@ 5A867000100000000 /* RedisDriver.tableplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RedisDriver.tableplugin; sourceTree = BUILT_PRODUCTS_DIR; }; 5A868000100000000 /* PostgreSQLDriver.tableplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PostgreSQLDriver.tableplugin; sourceTree = BUILT_PRODUCTS_DIR; }; 5A869000100000000 /* DuckDBDriver.tableplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DuckDBDriver.tableplugin; sourceTree = BUILT_PRODUCTS_DIR; }; + 5ABC147400000000000003 /* BeancountDriver.tableplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = BeancountDriver.tableplugin; sourceTree = BUILT_PRODUCTS_DIR; }; 5A86A000100000000 /* CSVExport.tableplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CSVExport.tableplugin; sourceTree = BUILT_PRODUCTS_DIR; }; 5A86B000100000000 /* JSONExport.tableplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JSONExport.tableplugin; sourceTree = BUILT_PRODUCTS_DIR; }; 5A86C000100000000 /* SQLExport.tableplugin */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SQLExport.tableplugin; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -430,6 +448,13 @@ ); target = 5A869000000000000 /* DuckDBDriver */; }; + 5ABC147400000000000006 /* Exceptions for "Plugins/BeancountDriverPlugin" folder in "BeancountDriver" target */ = { + isa = PBXFileSystemSynchronizedBuildFileExceptionSet; + membershipExceptions = ( + Info.plist, + ); + target = 5ABC147400000000000005 /* BeancountDriver */; + }; 5A86A000900000000 /* Exceptions for "Plugins/CSVExportPlugin" folder in "CSVExport" target */ = { isa = PBXFileSystemSynchronizedBuildFileExceptionSet; membershipExceptions = ( @@ -608,6 +633,14 @@ path = Plugins/DuckDBDriverPlugin; sourceTree = ""; }; + 5ABC147400000000000007 /* Plugins/BeancountDriverPlugin */ = { + isa = PBXFileSystemSynchronizedRootGroup; + exceptions = ( + 5ABC147400000000000006 /* Exceptions for "Plugins/BeancountDriverPlugin" folder in "BeancountDriver" target */, + ); + path = Plugins/BeancountDriverPlugin; + sourceTree = ""; + }; 5A86A000500000000 /* Plugins/CSVExportPlugin */ = { isa = PBXFileSystemSynchronizedRootGroup; exceptions = ( @@ -797,6 +830,14 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 5ABC147400000000000008 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 5ABC147400000000000001 /* TableProPluginKit.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 5A86A000300000000 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -930,6 +971,7 @@ 5A867000500000000 /* Plugins/RedisDriverPlugin */, 5A868000500000000 /* Plugins/PostgreSQLDriverPlugin */, 5A869000500000000 /* Plugins/DuckDBDriverPlugin */, + 5ABC147400000000000007 /* Plugins/BeancountDriverPlugin */, 5A87A000500000000 /* Plugins/CassandraDriverPlugin */, 5A86A000500000000 /* Plugins/CSVExportPlugin */, 5ABBED7C2FB55E1400A78382 /* Plugins/CSVInspectorPlugin */, @@ -960,6 +1002,7 @@ 5A867000100000000 /* RedisDriver.tableplugin */, 5A868000100000000 /* PostgreSQLDriver.tableplugin */, 5A869000100000000 /* DuckDBDriver.tableplugin */, + 5ABC147400000000000003 /* BeancountDriver.tableplugin */, 5A87A000100000000 /* CassandraDriver.tableplugin */, 5A86A000100000000 /* CSVExport.tableplugin */, 5A86B000100000000 /* JSONExport.tableplugin */, @@ -1066,6 +1109,7 @@ 5A867000C00000000 /* PBXTargetDependency */, 5A868000C00000000 /* PBXTargetDependency */, 5A869000C00000000 /* PBXTargetDependency */, + 5ABC14740000000000000C /* PBXTargetDependency */, 5A86A000C00000000 /* PBXTargetDependency */, 5A86B000C00000000 /* PBXTargetDependency */, 5A86C000C00000000 /* PBXTargetDependency */, @@ -1343,6 +1387,28 @@ productReference = 5A869000100000000 /* DuckDBDriver.tableplugin */; productType = "com.apple.product-type.bundle"; }; + 5ABC147400000000000005 /* BeancountDriver */ = { + isa = PBXNativeTarget; + buildConfigurationList = 5ABC147400000000000009 /* Build configuration list for PBXNativeTarget "BeancountDriver" */; + buildPhases = ( + 5ABC14740000000000000A /* Sources */, + 5ABC147400000000000008 /* Frameworks */, + 5ABC14740000000000000B /* Resources */, + 5ABC147400000000000012 /* Bundle RustLedger */, + ); + buildRules = ( + ); + dependencies = ( + 5ABC147400000000000010 /* PBXTargetDependency */, + ); + fileSystemSynchronizedGroups = ( + 5ABC147400000000000007 /* Plugins/BeancountDriverPlugin */, + ); + name = BeancountDriver; + productName = BeancountDriver; + productReference = 5ABC147400000000000003 /* BeancountDriver.tableplugin */; + productType = "com.apple.product-type.bundle"; + }; 5A86A000000000000 /* CSVExport */ = { isa = PBXNativeTarget; buildConfigurationList = 5A86A000800000000 /* Build configuration list for PBXNativeTarget "CSVExport" */; @@ -1652,6 +1718,9 @@ 5A869000000000000 = { CreatedOnToolsVersion = 26.2; }; + 5ABC147400000000000005 = { + CreatedOnToolsVersion = 26.5; + }; 5A86A000000000000 = { CreatedOnToolsVersion = 26.2; }; @@ -1717,6 +1786,7 @@ 5A867000000000000 /* RedisDriver */, 5A868000000000000 /* PostgreSQLDriver */, 5A869000000000000 /* DuckDBDriver */, + 5ABC147400000000000005 /* BeancountDriver */, 5A87A000000000000 /* CassandraDriver */, 5A86A000000000000 /* CSVExport */, 5A86B000000000000 /* JSONExport */, @@ -1821,6 +1891,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 5ABC14740000000000000B /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 5A86A000400000000 /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; @@ -1914,6 +1991,30 @@ }; /* End PBXResourcesBuildPhase section */ +/* Begin PBXShellScriptBuildPhase section */ + 5ABC147400000000000012 /* Bundle RustLedger */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + "$(SRCROOT)/scripts/download-rustledger.sh", + ); + name = "Bundle RustLedger"; + outputFileListPaths = ( + ); + outputPaths = ( + "$(TARGET_BUILD_DIR)/$(UNLOCALIZED_RESOURCES_FOLDER_PATH)/rledger", + "$(TARGET_TEMP_DIR)/rustledger-cache", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "TABLEPRO_RUSTLEDGER_CACHE=\"$TARGET_TEMP_DIR/rustledger-cache\" \"$SRCROOT/scripts/download-rustledger.sh\" \"$TARGET_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH/rledger\"\n"; + }; +/* End PBXShellScriptBuildPhase section */ + /* Begin PBXSourcesBuildPhase section */ 5A1091C32EF17EDC0055EA7C /* Sources */ = { isa = PBXSourcesBuildPhase; @@ -2006,6 +2107,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 5ABC14740000000000000A /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 5A86A000200000000 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -2172,6 +2280,16 @@ target = 5A869000000000000 /* DuckDBDriver */; targetProxy = 5A869000B00000000 /* PBXContainerItemProxy */; }; + 5ABC14740000000000000C /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 5ABC147400000000000005 /* BeancountDriver */; + targetProxy = 5ABC147400000000000004 /* PBXContainerItemProxy */; + }; + 5ABC147400000000000010 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 5A860000000000000 /* TableProPluginKit */; + targetProxy = 5ABC14740000000000000F /* PBXContainerItemProxy */; + }; 5A86A000C00000000 /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 5A86A000000000000 /* CSVExport */; @@ -3253,6 +3371,56 @@ }; name = Release; }; + 5ABC14740000000000000D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = ""; + ENABLE_USER_SCRIPT_SANDBOXING = NO; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_FILE = Plugins/BeancountDriverPlugin/Info.plist; + INFOPLIST_KEY_NSPrincipalClass = "$(PRODUCT_MODULE_NAME).BeancountPlugin"; + LD_RUNPATH_SEARCH_PATHS = "@executable_path/../Frameworks"; + MACOSX_DEPLOYMENT_TARGET = 14.0; + MARKETING_VERSION = 1.0; + OTHER_LDFLAGS = "-lsqlite3"; + PRODUCT_BUNDLE_IDENTIFIER = com.TablePro.BeancountDriver; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + SKIP_INSTALL = YES; + SUPPORTED_PLATFORMS = macosx; + SWIFT_VERSION = 5.9; + WRAPPER_EXTENSION = tableplugin; + }; + name = Debug; + }; + 5ABC14740000000000000E /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = ""; + ENABLE_USER_SCRIPT_SANDBOXING = NO; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_FILE = Plugins/BeancountDriverPlugin/Info.plist; + INFOPLIST_KEY_NSPrincipalClass = "$(PRODUCT_MODULE_NAME).BeancountPlugin"; + LD_RUNPATH_SEARCH_PATHS = "@executable_path/../Frameworks"; + MACOSX_DEPLOYMENT_TARGET = 14.0; + MARKETING_VERSION = 1.0; + OTHER_LDFLAGS = "-lsqlite3"; + PRODUCT_BUNDLE_IDENTIFIER = com.TablePro.BeancountDriver; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = macosx; + SKIP_INSTALL = YES; + SUPPORTED_PLATFORMS = macosx; + SWIFT_VERSION = 5.9; + WRAPPER_EXTENSION = tableplugin; + }; + name = Release; + }; 5A86A000600000000 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -4035,6 +4203,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 5ABC147400000000000009 /* Build configuration list for PBXNativeTarget "BeancountDriver" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 5ABC14740000000000000D /* Debug */, + 5ABC14740000000000000E /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 5A86A000800000000 /* Build configuration list for PBXNativeTarget "CSVExport" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift b/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift index 7637a0b4e..652868123 100644 --- a/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift +++ b/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift @@ -855,6 +855,82 @@ extension PluginMetadataRegistry { tagline: String(localized: "Embedded analytical SQL") ) )), + ("Beancount", PluginMetadataSnapshot( + displayName: "Beancount", iconName: "beancount-icon", defaultPort: 0, + requiresAuthentication: false, supportsForeignKeys: false, supportsSchemaEditing: false, + isDownloadable: true, primaryUrlScheme: "beancount", parameterStyle: .questionMark, + navigationModel: .standard, explainVariants: [], pathFieldRole: .filePath, + supportsHealthMonitor: false, urlSchemes: ["beancount"], postConnectActions: [], + brandColorHex: "#3F7D20", + queryLanguageName: "SQL", editorLanguage: .sql, + connectionMode: .fileBased, supportsDatabaseSwitching: false, + supportsColumnReorder: false, + capabilities: PluginMetadataSnapshot.CapabilityFlags( + supportsSchemaSwitching: false, + supportsImport: false, + supportsExport: true, + supportsSSH: false, + supportsSSL: false, + supportsCascadeDrop: false, + supportsForeignKeyDisable: false, + supportsReadOnlyMode: true, + supportsQueryProgress: false, + requiresReconnectForDatabaseSwitch: false, + supportsDropDatabase: false, + supportsAddColumn: false, + supportsModifyColumn: false, + supportsDropColumn: false, + supportsRenameColumn: false, + supportsAddIndex: false, + supportsDropIndex: false, + supportsModifyPrimaryKey: false + ), + schema: PluginMetadataSnapshot.SchemaInfo( + defaultSchemaName: "public", + defaultGroupName: "main", + tableEntityName: "Ledger Tables", + defaultPrimaryKeyColumn: nil, + immutableColumns: [ + "id", "transaction_id", "date", "flag", "payee", "narration", + "account", "amount", "commodity", "currency", "source_file", "line" + ], + systemDatabaseNames: [], + systemSchemaNames: [], + fileExtensions: ["beancount"], + databaseGroupingStrategy: .flat, + structureColumnFields: [.name, .type, .nullable, .comment] + ), + editor: PluginMetadataSnapshot.EditorConfig( + sqlDialect: SQLDialectDescriptor( + identifierQuote: "\"", + keywords: [ + "SELECT", "FROM", "WHERE", "JOIN", "INNER", "LEFT", "RIGHT", "OUTER", + "ON", "AND", "OR", "NOT", "IN", "LIKE", "BETWEEN", "AS", + "ORDER", "BY", "GROUP", "HAVING", "LIMIT", "OFFSET", + "WITH", "UNION", "INTERSECT", "EXCEPT", + "CASE", "WHEN", "THEN", "ELSE", "END", "NULL", "IS", + "ASC", "DESC", "DISTINCT" + ], + functions: ["COUNT", "SUM", "AVG", "MAX", "MIN", "ROUND", "DATE", "STRFTIME"], + dataTypes: ["INTEGER", "TEXT", "DATE", "DECIMAL"], + regexSyntax: .unsupported, + booleanLiteralStyle: .numeric, + likeEscapeStyle: .explicit, + paginationStyle: .limit + ), + statementCompletions: [], + columnTypesByCategory: [ + "Integer": ["INTEGER"], + "String": ["TEXT"], + "Date": ["DATE"], + "Decimal": ["DECIMAL"] + ] + ), + connection: PluginMetadataSnapshot.ConnectionConfig( + category: .analytical, + tagline: String(localized: "Plain-text accounting ledgers") + ) + )), ("Cassandra", PluginMetadataSnapshot( displayName: "Cassandra / ScyllaDB", iconName: "cassandra-icon", defaultPort: 9_042, requiresAuthentication: false, supportsForeignKeys: false, supportsSchemaEditing: true, diff --git a/TablePro/Info.plist b/TablePro/Info.plist index fe3c5d7ae..24622cdcd 100644 --- a/TablePro/Info.plist +++ b/TablePro/Info.plist @@ -103,6 +103,22 @@ com.tablepro.duckdb + + CFBundleTypeName + Beancount Ledger + CFBundleTypeRole + Viewer + LSHandlerRank + Owner + CFBundleTypeExtensions + + beancount + + LSItemContentTypes + + com.tablepro.beancount + + CFBundleTypeExtensions @@ -202,6 +218,24 @@ + + UTTypeIdentifier + com.tablepro.beancount + UTTypeDescription + Beancount Ledger + UTTypeConformsTo + + public.plain-text + public.data + + UTTypeTagSpecification + + public.filename-extension + + beancount + + + UTTypeIdentifier com.tablepro.connection-share diff --git a/TableProTests/PluginTestSources/BeancountLedgerParser.swift b/TableProTests/PluginTestSources/BeancountLedgerParser.swift new file mode 120000 index 000000000..80626376b --- /dev/null +++ b/TableProTests/PluginTestSources/BeancountLedgerParser.swift @@ -0,0 +1 @@ +../../Plugins/BeancountDriverPlugin/BeancountLedgerParser.swift \ No newline at end of file diff --git a/TableProTests/PluginTestSources/BeancountPluginDriver.swift b/TableProTests/PluginTestSources/BeancountPluginDriver.swift new file mode 120000 index 000000000..24339cc9c --- /dev/null +++ b/TableProTests/PluginTestSources/BeancountPluginDriver.swift @@ -0,0 +1 @@ +../../Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift \ No newline at end of file diff --git a/TableProTests/Plugins/BeancountDriverMetadataTests.swift b/TableProTests/Plugins/BeancountDriverMetadataTests.swift new file mode 100644 index 000000000..ed892cb81 --- /dev/null +++ b/TableProTests/Plugins/BeancountDriverMetadataTests.swift @@ -0,0 +1,57 @@ +// +// BeancountDriverMetadataTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +@MainActor +@Suite("Beancount driver metadata") +struct BeancountDriverMetadataTests { + @Test("registry exposes Beancount as a downloadable file-based driver") + func registryMetadata() throws { + let snapshot = try #require(PluginMetadataRegistry.shared.snapshot(forTypeId: "Beancount")) + #expect(snapshot.displayName == "Beancount") + #expect(snapshot.isDownloadable == true) + #expect(snapshot.connectionMode == .fileBased) + #expect(snapshot.schema.fileExtensions == ["beancount"]) + #expect(snapshot.pathFieldRole == .filePath) + #expect(snapshot.supportsSchemaEditing == false) + #expect(snapshot.supportsDatabaseSwitching == false) + #expect(snapshot.supportsHealthMonitor == false) + } + + @Test("URLClassifier resolves .beancount files to the Beancount database type") + func urlClassifierResolvesBeancountFiles() { + #expect(PluginManager.shared.allRegisteredFileExtensions["beancount"] == DatabaseType(rawValue: "Beancount")) + } + + @Test("app bundle claims .beancount files as the owner viewer") + func appBundleClaimsBeancountFilesAsOwnerViewer() throws { + let plistURL = Bundle(for: AppDelegate.self) + .bundleURL + .appendingPathComponent("Contents/Info.plist") + let data = try Data(contentsOf: plistURL) + let plistObject = try PropertyListSerialization.propertyList(from: data, format: nil) + let plist = try #require(plistObject as? [String: Any]) + let documentTypes = try #require(plist["CFBundleDocumentTypes"] as? [[String: Any]]) + let beancountDocumentType = try #require(documentTypes.first { documentType in + let contentTypes = documentType["LSItemContentTypes"] as? [String] + return contentTypes?.contains("com.tablepro.beancount") == true + }) + + #expect(beancountDocumentType["CFBundleTypeRole"] as? String == "Viewer") + #expect(beancountDocumentType["LSHandlerRank"] as? String == "Owner") + #expect(beancountDocumentType["CFBundleTypeExtensions"] as? [String] == ["beancount"]) + + let exportedTypes = try #require(plist["UTExportedTypeDeclarations"] as? [[String: Any]]) + let beancountType = try #require(exportedTypes.first { + $0["UTTypeIdentifier"] as? String == "com.tablepro.beancount" + }) + let tags = try #require(beancountType["UTTypeTagSpecification"] as? [String: Any]) + #expect(tags["public.filename-extension"] as? [String] == ["beancount"]) + } +} diff --git a/TableProTests/Plugins/BeancountLedgerParserTests.swift b/TableProTests/Plugins/BeancountLedgerParserTests.swift new file mode 100644 index 000000000..e57549682 --- /dev/null +++ b/TableProTests/Plugins/BeancountLedgerParserTests.swift @@ -0,0 +1,92 @@ +// +// BeancountLedgerParserTests.swift +// TableProTests +// + +import Foundation +import Testing + +@Suite("Beancount ledger parser") +struct BeancountLedgerParserTests { + @Test("loads transactions, postings, accounts, prices, balances, and includes") + func parsesCoreTablesAndIncludes() throws { + let tempDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("beancount-parser-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: tempDirectory, withIntermediateDirectories: true) + defer { + try? FileManager.default.removeItem(at: tempDirectory) + } + + let included = tempDirectory.appendingPathComponent("prices.beancount") + try """ + 2024-01-02 price USD 1.35 CAD + 2024-01-31 balance Assets:Bank:Checking 900.00 USD + """.write(to: included, atomically: true, encoding: .utf8) + + let ledger = tempDirectory.appendingPathComponent("main.beancount") + try """ + option "title" "Household" + include "prices.beancount" + + 2024-01-01 open Assets:Bank:Checking USD + 2024-01-01 open Expenses:Food USD + + 2024-01-15 * "Grocery Store" "Weekly shop" + Assets:Bank:Checking -100.00 USD + Expenses:Food 100.00 USD + """.write(to: ledger, atomically: true, encoding: .utf8) + + let parsed = try BeancountLedgerParser().parse(fileURL: ledger) + + #expect(parsed.accounts.map(\.name).sorted() == ["Assets:Bank:Checking", "Expenses:Food"]) + #expect(parsed.transactions.map(\.payee) == ["Grocery Store"]) + #expect(parsed.transactions.map(\.narration) == ["Weekly shop"]) + #expect(parsed.postings.count == 2) + #expect(parsed.prices.first?.commodity == "USD") + #expect(parsed.prices.first?.currency == "CAD") + #expect(parsed.balances.first?.account == "Assets:Bank:Checking") + #expect(parsed.sourceFiles.map(\.lastPathComponent).sorted() == ["main.beancount", "prices.beancount"]) + } + + @Test("expands glob include paths") + func parsesGlobIncludes() throws { + let tempDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("beancount-parser-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: tempDirectory, withIntermediateDirectories: true) + defer { + try? FileManager.default.removeItem(at: tempDirectory) + } + + let imports = tempDirectory.appendingPathComponent("imports", isDirectory: true) + let nested = imports.appendingPathComponent("nested", isDirectory: true) + try FileManager.default.createDirectory(at: nested, withIntermediateDirectories: true) + + try """ + 2024-01-01 open Assets:Bank:Checking USD + """.write(to: imports.appendingPathComponent("accounts.beancount"), atomically: true, encoding: .utf8) + + try """ + 2024-01-01 open Expenses:Food USD + """.write(to: nested.appendingPathComponent("expenses.beancount"), atomically: true, encoding: .utf8) + + let ledger = tempDirectory.appendingPathComponent("main.beancount") + try """ + include "imports/*.beancount" + include "imports/**/*.beancount" + + 2024-01-15 * "Grocery Store" "Weekly shop" + Assets:Bank:Checking -100.00 USD + Expenses:Food 100.00 USD + """.write(to: ledger, atomically: true, encoding: .utf8) + + let parsed = try BeancountLedgerParser().parse(fileURL: ledger) + + #expect(parsed.accounts.map(\.name).sorted() == ["Assets:Bank:Checking", "Expenses:Food"]) + #expect(parsed.transactions.count == 1) + #expect(parsed.sourceFiles.map(\.lastPathComponent).sorted() == [ + "accounts.beancount", + "expenses.beancount", + "main.beancount" + ]) + } +} diff --git a/TableProTests/Plugins/BeancountPluginDriverTests.swift b/TableProTests/Plugins/BeancountPluginDriverTests.swift new file mode 100644 index 000000000..844e346d0 --- /dev/null +++ b/TableProTests/Plugins/BeancountPluginDriverTests.swift @@ -0,0 +1,150 @@ +// +// BeancountPluginDriverTests.swift +// TableProTests +// + +import Foundation +import TableProPluginKit +import Testing + +@Suite("Beancount plugin driver") +struct BeancountPluginDriverTests { + @Test("reloads the SQL projection when an included ledger file changes") + func reloadsWhenIncludedFileChanges() async throws { + let tempDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("beancount-driver-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: tempDirectory, withIntermediateDirectories: true) + defer { + try? FileManager.default.removeItem(at: tempDirectory) + } + + let included = tempDirectory.appendingPathComponent("accounts.beancount") + try """ + 2024-01-01 open Assets:Bank:Checking USD + """.write(to: included, atomically: true, encoding: .utf8) + + let ledger = tempDirectory.appendingPathComponent("main.beancount") + try """ + include "accounts.beancount" + """.write(to: ledger, atomically: true, encoding: .utf8) + + let driver = BeancountPluginDriver(config: DriverConnectionConfig( + host: "", + port: 0, + username: "", + password: "", + database: ledger.path + )) + try await driver.connect() + defer { + driver.disconnect() + } + + var result = try await driver.execute(query: "SELECT name FROM accounts ORDER BY name") + #expect(result.rows.map { $0[0].asText } == ["Assets:Bank:Checking"]) + + try """ + 2024-01-01 open Assets:Bank:Checking USD + 2024-01-02 open Expenses:Food USD + """.write(to: included, atomically: true, encoding: .utf8) + + result = try await driver.execute(query: "SELECT name FROM accounts ORDER BY name") + #expect(result.rows.map { $0[0].asText } == ["Assets:Bank:Checking", "Expenses:Food"]) + } + + @Test("rejects write queries") + func rejectsWriteQueries() async throws { + let tempDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("beancount-driver-read-only-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: tempDirectory, withIntermediateDirectories: true) + defer { + try? FileManager.default.removeItem(at: tempDirectory) + } + + let ledger = tempDirectory.appendingPathComponent("main.beancount") + try """ + 2024-01-01 open Assets:Bank:Checking USD + """.write(to: ledger, atomically: true, encoding: .utf8) + + let driver = BeancountPluginDriver(config: DriverConnectionConfig( + host: "", + port: 0, + username: "", + password: "", + database: ledger.path + )) + try await driver.connect() + defer { + driver.disconnect() + } + + await #expect(throws: BeancountDriverError.self) { + _ = try await driver.execute(query: "DELETE FROM accounts") + } + } + + @Test("executes BQL queries through the rustledger helper") + func executesBQLQueriesThroughRustledgerHelper() async throws { + let rustledger = try #require(Self.bundledRustledgerPath() ?? Self.installedRustledgerPath()) + let tempDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("beancount-driver-bql-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: tempDirectory, withIntermediateDirectories: true) + defer { + unsetenv("TABLEPRO_RUSTLEDGER_BINARY") + try? FileManager.default.removeItem(at: tempDirectory) + } + + let ledger = tempDirectory.appendingPathComponent("main.beancount") + try """ + 2024-01-01 open Assets:Bank:Checking USD + 2024-01-01 open Expenses:Food USD + 2024-01-01 open Income:Salary USD + """.write(to: ledger, atomically: true, encoding: .utf8) + + setenv("TABLEPRO_RUSTLEDGER_BINARY", rustledger, 1) + + let driver = BeancountPluginDriver(config: DriverConnectionConfig( + host: "", + port: 0, + username: "", + password: "", + database: ledger.path + )) + try await driver.connect() + defer { + driver.disconnect() + } + + let result = try await driver.execute(query: "BQL: SELECT account FROM accounts ORDER BY account") + + #expect(result.columns == ["account"]) + #expect(result.rows.map { $0.first?.asText } == [ + "Assets:Bank:Checking", + "Expenses:Food", + "Income:Salary" + ]) + } + + private static func installedRustledgerPath() -> String? { + let candidates = [ + ProcessInfo.processInfo.environment["TABLEPRO_RUSTLEDGER_BINARY"], + "/opt/homebrew/bin/rledger", + "/usr/local/bin/rledger" + ].compactMap { $0 } + + return candidates.first { path in + FileManager.default.isExecutableFile(atPath: path) + } + } + + private static func bundledRustledgerPath() -> String? { + guard let path = Bundle.main.builtInPlugInsURL? + .appendingPathComponent("BeancountDriver.tableplugin") + .appendingPathComponent("Contents/Resources/rledger") + .path else { + return nil + } + + return FileManager.default.isExecutableFile(atPath: path) ? path : nil + } +} diff --git a/scripts/download-rustledger.sh b/scripts/download-rustledger.sh new file mode 100755 index 000000000..644476058 --- /dev/null +++ b/scripts/download-rustledger.sh @@ -0,0 +1,146 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Bundle the rustledger CLI helper used by the Beancount driver for BQL queries. +# Usage: scripts/download-rustledger.sh [output-rledger-path] + +VERSION="v0.15.0" +REPO="rustledger/rustledger" +PROJECT_ROOT="${SRCROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)}" +CACHE_ROOT="${TABLEPRO_RUSTLEDGER_CACHE:-$PROJECT_ROOT/Libs/rustledger}" +OUTPUT="${1:-${TARGET_BUILD_DIR:?}/${UNLOCALIZED_RESOURCES_FOLDER_PATH:?}/rledger}" + +triple_for_arch() { + case "$1" in + arm64|aarch64) echo "aarch64-apple-darwin" ;; + x86_64) echo "x86_64-apple-darwin" ;; + *) return 1 ;; + esac +} + +sha_for_triple() { + case "$1" in + aarch64-apple-darwin) echo "b8f1190898b1e7ed1585ce3833a9a1814b30b5703e75eed7c276dafac00bc00a" ;; + x86_64-apple-darwin) echo "b264a51d792d00d138725d8d7eaa6e5354e7ad54e8e93e9efde535d830e217ff" ;; + *) return 1 ;; + esac +} + +host_triple() { + triple_for_arch "$(uname -m)" +} + +copy_helper() { + local source_path="$1" + local message="$2" + + if [[ ! -x "$source_path" ]]; then + return 1 + fi + + mkdir -p "$(dirname "$OUTPUT")" + cp -f "$source_path" "$OUTPUT" + chmod 755 "$OUTPUT" + echo "$message: $OUTPUT" + return 0 +} + +download_release() { + local triple="$1" + local archive="rustledger-${VERSION}-${triple}.tar.gz" + local sha256 tmpdir archive_path actual_sha extracted_helper + + sha256="$(sha_for_triple "$triple")" + tmpdir="$(mktemp -d)" + trap "rm -rf '$tmpdir'" RETURN + archive_path="$tmpdir/$archive" + + if command -v gh >/dev/null 2>&1; then + gh release download "$VERSION" \ + --repo "$REPO" \ + --pattern "$archive" \ + --dir "$tmpdir" \ + --clobber + else + curl -fSL -o "$archive_path" "https://github.com/$REPO/releases/download/$VERSION/$archive" + fi + + actual_sha="$(shasum -a 256 "$archive_path" | awk '{print $1}')" + if [[ "$actual_sha" != "$sha256" ]]; then + echo "Checksum mismatch for $archive" >&2 + echo "Expected: $sha256" >&2 + echo "Actual: $actual_sha" >&2 + return 1 + fi + + mkdir -p "$tmpdir/extract" "$CACHE_ROOT/$VERSION/$triple" + tar -xzf "$archive_path" -C "$tmpdir/extract" + extracted_helper="$(find "$tmpdir/extract" -type f -name rledger | head -n 1)" + if [[ -z "$extracted_helper" ]]; then + echo "Could not find rledger in $archive" >&2 + return 1 + fi + + cp -f "$extracted_helper" "$CACHE_ROOT/$VERSION/$triple/rledger" + chmod 755 "$CACHE_ROOT/$VERSION/$triple/rledger" +} + +ensure_cached_helper() { + local triple="$1" + local helper="$CACHE_ROOT/$VERSION/$triple/rledger" + + if [[ -x "$helper" ]]; then + echo "$helper" + return 0 + fi + + download_release "$triple" >&2 + echo "$helper" +} + +requested_triples=() +if [[ -n "${ARCHS:-}" ]]; then + for arch in $ARCHS; do + triple="$(triple_for_arch "$arch" 2>/dev/null || true)" + if [[ -n "${triple:-}" && " ${requested_triples[*]} " != *" $triple "* ]]; then + requested_triples+=("$triple") + fi + done +fi +if [[ "${#requested_triples[@]}" -eq 0 ]]; then + requested_triples+=("$(host_triple)") +fi + +if [[ -n "${TABLEPRO_RUSTLEDGER_BINARY:-}" ]]; then + copy_helper "$TABLEPRO_RUSTLEDGER_BINARY" "Bundled rustledger helper from TABLEPRO_RUSTLEDGER_BINARY" + exit 0 +fi + +helpers=() +for triple in "${requested_triples[@]}"; do + if helper="$(ensure_cached_helper "$triple")"; then + helpers+=("$helper") + fi +done + +if [[ "${#helpers[@]}" -eq 1 ]]; then + copy_helper "${helpers[0]}" "Bundled rustledger helper" + exit 0 +fi + +if [[ "${#helpers[@]}" -gt 1 && -x "$(command -v lipo)" ]]; then + mkdir -p "$(dirname "$OUTPUT")" + lipo -create "${helpers[@]}" -output "$OUTPUT" + chmod 755 "$OUTPUT" + echo "Bundled universal rustledger helper: $OUTPUT" + exit 0 +fi + +for installed in /opt/homebrew/bin/rledger /usr/local/bin/rledger; do + if copy_helper "$installed" "Bundled installed rustledger helper after release download failed"; then + exit 0 + fi +done + +echo "Unable to bundle rustledger. Install rledger or set TABLEPRO_RUSTLEDGER_BINARY." >&2 +exit 1 From b2581268950caa1a491f7b334d8eed2b46a21aec Mon Sep 17 00:00:00 2001 From: salmonumbrella <182032677+salmonumbrella@users.noreply.github.com> Date: Sat, 30 May 2026 11:26:41 -0700 Subject: [PATCH 2/4] Address Beancount driver review feedback --- CHANGELOG.md | 4 + .../Sources/TableProModels/DatabaseType.swift | 4 +- .../DatabaseTypeTests.swift | 4 +- .../BeancountLedgerParser.swift | 53 +++++++++++-- .../BeancountPlugin.swift | 2 +- .../BeancountPluginDriver.swift | 79 +++++++++++++++---- README.md | 2 +- README.vi.md | 2 +- .../beancount-icon.imageset/Contents.json | 16 ++++ .../beancount-icon.imageset/beancount.svg | 1 + ...ginMetadataRegistry+RegistryDefaults.swift | 76 ------------------ .../Core/Plugins/PluginMetadataRegistry.swift | 76 ++++++++++++++++++ .../Connection/DatabaseConnection.swift | 2 + TableProTests/Models/DatabaseTypeTests.swift | 3 +- .../BeancountDriverMetadataTests.swift | 4 +- .../Plugins/BeancountLedgerParserTests.swift | 24 ++++++ .../Plugins/BeancountPluginDriverTests.swift | 53 +++++++++++++ docs/databases/beancount.mdx | 66 ++++++++++++++++ docs/databases/overview.mdx | 4 +- docs/docs.json | 1 + docs/index.mdx | 3 +- scripts/download-rustledger.sh | 8 +- 22 files changed, 372 insertions(+), 115 deletions(-) create mode 100644 TablePro/Assets.xcassets/beancount-icon.imageset/Contents.json create mode 100644 TablePro/Assets.xcassets/beancount-icon.imageset/beancount.svg create mode 100644 docs/databases/beancount.mdx diff --git a/CHANGELOG.md b/CHANGELOG.md index d643cac0e..6eef9ed54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Beancount ledger support as a bundled, read-only file-based driver with SQL projection and BQL queries. + ## [0.46.0] - 2026-05-28 ### Added diff --git a/Packages/TableProCore/Sources/TableProModels/DatabaseType.swift b/Packages/TableProCore/Sources/TableProModels/DatabaseType.swift index 4272a587b..bcad2d50f 100644 --- a/Packages/TableProCore/Sources/TableProModels/DatabaseType.swift +++ b/Packages/TableProCore/Sources/TableProModels/DatabaseType.swift @@ -26,11 +26,12 @@ public struct DatabaseType: Hashable, Codable, Sendable, RawRepresentable { public static let dynamodb = DatabaseType(rawValue: "DynamoDB") public static let bigquery = DatabaseType(rawValue: "BigQuery") public static let libsql = DatabaseType(rawValue: "libSQL") + public static let beancount = DatabaseType(rawValue: "Beancount") public static let allKnownTypes: [DatabaseType] = [ .mysql, .mariadb, .postgresql, .sqlite, .redis, .mongodb, .clickhouse, .mssql, .oracle, .duckdb, .cassandra, .redshift, - .etcd, .cloudflareD1, .dynamodb, .bigquery, .libsql + .etcd, .cloudflareD1, .dynamodb, .bigquery, .libsql, .beancount ] /// Icon name for this database type — asset catalog name (e.g. "mysql-icon") or SF Symbol fallback @@ -53,6 +54,7 @@ public struct DatabaseType: Hashable, Codable, Sendable, RawRepresentable { case .dynamodb: return "dynamodb-icon" case .bigquery: return "bigquery-icon" case .libsql: return "libsql-icon" + case .beancount: return "beancount-icon" default: return "externaldrive" } } diff --git a/Packages/TableProCore/Tests/TableProModelsTests/DatabaseTypeTests.swift b/Packages/TableProCore/Tests/TableProModelsTests/DatabaseTypeTests.swift index e8eeacbeb..3d92cdf57 100644 --- a/Packages/TableProCore/Tests/TableProModelsTests/DatabaseTypeTests.swift +++ b/Packages/TableProCore/Tests/TableProModelsTests/DatabaseTypeTests.swift @@ -15,6 +15,7 @@ struct DatabaseTypeTests { #expect(DatabaseType.mssql.rawValue == "SQL Server") #expect(DatabaseType.cloudflareD1.rawValue == "Cloudflare D1") #expect(DatabaseType.bigquery.rawValue == "BigQuery") + #expect(DatabaseType.beancount.rawValue == "Beancount") } @Test("pluginTypeId maps multi-type databases") @@ -51,10 +52,11 @@ struct DatabaseTypeTests { @Test("allKnownTypes contains all expected types") func allKnownTypesComplete() { - #expect(DatabaseType.allKnownTypes.count == 17) + #expect(DatabaseType.allKnownTypes.count == 18) #expect(DatabaseType.allKnownTypes.contains(.mysql)) #expect(DatabaseType.allKnownTypes.contains(.bigquery)) #expect(DatabaseType.allKnownTypes.contains(.libsql)) + #expect(DatabaseType.allKnownTypes.contains(.beancount)) } @Test("Hashable conformance") diff --git a/Plugins/BeancountDriverPlugin/BeancountLedgerParser.swift b/Plugins/BeancountDriverPlugin/BeancountLedgerParser.swift index 44729400e..32212367a 100644 --- a/Plugins/BeancountDriverPlugin/BeancountLedgerParser.swift +++ b/Plugins/BeancountDriverPlugin/BeancountLedgerParser.swift @@ -12,6 +12,7 @@ struct BeancountLedger: Sendable { let prices: [BeancountPrice] let balances: [BeancountBalance] let sourceFiles: [URL] + let watchedDirectories: [URL] } struct BeancountTransaction: Sendable { @@ -86,6 +87,7 @@ final class BeancountLedgerParser { private var accountsByName: [String: BeancountAccount] = [:] private var prices: [BeancountPrice] = [] private var balances: [BeancountBalance] = [] + private var watchedDirectories: Set = [] func parse(fileURL: URL) throws -> BeancountLedger { visited.removeAll() @@ -96,6 +98,7 @@ final class BeancountLedgerParser { accountsByName.removeAll() prices.removeAll() balances.removeAll() + watchedDirectories.removeAll() try parseFile(fileURL.standardizedFileURL) @@ -105,7 +108,8 @@ final class BeancountLedgerParser { accounts: accountsByName.values.sorted { $0.name < $1.name }, prices: prices, balances: balances, - sourceFiles: sourceFiles + sourceFiles: sourceFiles, + watchedDirectories: watchedDirectories.sorted { $0.path < $1.path } ) } @@ -205,18 +209,26 @@ final class BeancountLedgerParser { let patternPath = patternURL.path let searchRoot = globSearchRoot(for: patternPath) let fileManager = FileManager.default - guard fileManager.fileExists(atPath: searchRoot.path) else { return [] } + guard fileManager.fileExists(atPath: searchRoot.path) else { + watchedDirectories.insert(existingWatchDirectory(for: searchRoot)) + return [] + } + watchedDirectories.insert(searchRoot) let regex = try NSRegularExpression(pattern: globRegex(for: patternPath)) let enumerator = fileManager.enumerator( at: searchRoot, - includingPropertiesForKeys: [.isRegularFileKey], + includingPropertiesForKeys: [.isDirectoryKey, .isRegularFileKey], options: [.skipsHiddenFiles] ) var matches: [URL] = [] while let candidate = enumerator?.nextObject() as? URL { - let values = try? candidate.resourceValues(forKeys: [.isRegularFileKey]) + let values = try? candidate.resourceValues(forKeys: [.isDirectoryKey, .isRegularFileKey]) + if values?.isDirectory == true { + watchedDirectories.insert(candidate.standardizedFileURL) + continue + } guard values?.isRegularFile == true else { continue } let path = candidate.standardizedFileURL.path @@ -247,6 +259,20 @@ final class BeancountLedgerParser { return URL(fileURLWithPath: rootPath.isEmpty ? "/" : rootPath).standardizedFileURL } + private func existingWatchDirectory(for missingDirectory: URL) -> URL { + var candidate = missingDirectory.standardizedFileURL + let fileManager = FileManager.default + while candidate.path != "/" { + var isDirectory: ObjCBool = false + if fileManager.fileExists(atPath: candidate.path, isDirectory: &isDirectory), + isDirectory.boolValue { + return candidate + } + candidate.deleteLastPathComponent() + } + return URL(fileURLWithPath: "/") + } + private func globRegex(for patternPath: String) -> String { let characters = Array(patternPath) var regex = "^" @@ -366,7 +392,7 @@ final class BeancountLedgerParser { guard !trimmed.isEmpty, !trimmed.hasPrefix(";"), !trimmed.hasPrefix("#") else { return nil } let parts = trimmed.split(whereSeparator: \.isWhitespace).map(String.init) - guard let account = parts.first, account.contains(":") else { return nil } + guard let account = parts.first, isAccountName(account) else { return nil } let amount = parts.count >= 2 ? parts[1] : nil let commodity = parts.count >= 3 ? parts[2] : nil @@ -382,6 +408,23 @@ final class BeancountLedgerParser { ) } + private func isAccountName(_ value: String) -> Bool { + guard value.contains(":"), !value.hasSuffix(":") else { return false } + let components = value.split(separator: ":", omittingEmptySubsequences: false) + guard components.count >= 2 else { return false } + + let allowedSymbols = CharacterSet(charactersIn: "-_") + return components.allSatisfy { component in + guard let first = component.unicodeScalars.first, + CharacterSet.uppercaseLetters.contains(first) else { + return false + } + return component.unicodeScalars.allSatisfy { scalar in + CharacterSet.alphanumerics.contains(scalar) || allowedSymbols.contains(scalar) + } + } + } + private func parseDatePrefix(_ line: String) -> String? { guard line.count >= 11 else { return nil } let prefix = String(line.prefix(10)) diff --git a/Plugins/BeancountDriverPlugin/BeancountPlugin.swift b/Plugins/BeancountDriverPlugin/BeancountPlugin.swift index 62e918995..cb68c4875 100644 --- a/Plugins/BeancountDriverPlugin/BeancountPlugin.swift +++ b/Plugins/BeancountDriverPlugin/BeancountPlugin.swift @@ -17,7 +17,7 @@ final class BeancountPlugin: NSObject, TableProPlugin, DriverPlugin { static let iconName = "beancount-icon" static let defaultPort = 0 - static let isDownloadable = true + static let isDownloadable = false static let pathFieldRole: PathFieldRole = .filePath static let requiresAuthentication = false static let supportsSSH = false diff --git a/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift b/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift index f18b3154d..658b55114 100644 --- a/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift +++ b/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift @@ -4,6 +4,7 @@ // import Foundation +import Dispatch import SQLite3 import TableProPluginKit @@ -74,7 +75,7 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } let parsed = try BeancountLedgerParser().parse(fileURL: fileURL) - let signatures = try Self.signatures(for: parsed.sourceFiles) + let signatures = try Self.signatures(for: Self.watchedURLs(for: parsed)) var handle: OpaquePointer? guard sqlite3_open(":memory:", &handle) == SQLITE_OK, let handle else { throw BeancountDriverError.connectionFailed("Could not initialize SQL projection") @@ -162,6 +163,9 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func fetchRowCount(query: String) async throws -> Int { + if let bql = Self.extractBQLQuery(from: query) { + return try executeBQL(query: bql).rows.count + } let escaped = query.replacingOccurrences(of: ";", with: "") let result = try await execute(query: "SELECT COUNT(*) FROM (\(escaped))") guard let text = result.rows.first?.first?.asText, let count = Int(text) else { return 0 } @@ -169,7 +173,10 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func fetchRows(query: String, offset: Int, limit: Int) async throws -> PluginQueryResult { - try await execute(query: "SELECT * FROM (\(query)) LIMIT \(limit) OFFSET \(offset)") + if let bql = Self.extractBQLQuery(from: query) { + return Self.paginatedResult(try executeBQL(query: bql), offset: offset, limit: limit) + } + return try await execute(query: "SELECT * FROM (\(query)) LIMIT \(limit) OFFSET \(offset)") } func fetchTables(schema: String?) async throws -> [PluginTableInfo] { @@ -315,11 +322,26 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { process.standardOutput = stdout process.standardError = stderr + let outputCollector = PipeDataCollector() + let errorCollector = PipeDataCollector() + let readers = DispatchGroup() + readers.enter() + DispatchQueue.global(qos: .userInitiated).async { + outputCollector.set(stdout.fileHandleForReading.readDataToEndOfFile()) + readers.leave() + } + readers.enter() + DispatchQueue.global(qos: .userInitiated).async { + errorCollector.set(stderr.fileHandleForReading.readDataToEndOfFile()) + readers.leave() + } + try process.run() process.waitUntilExit() + readers.wait() - let output = stdout.fileHandleForReading.readDataToEndOfFile() - let errorOutput = stderr.fileHandleForReading.readDataToEndOfFile() + let output = outputCollector.data + let errorOutput = errorCollector.data guard process.terminationStatus == 0 else { let message = String(data: errorOutput, encoding: .utf8)? .trimmingCharacters(in: .whitespacesAndNewlines) @@ -397,13 +419,28 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } } + private static func paginatedResult(_ result: PluginQueryResult, offset: Int, limit: Int) -> PluginQueryResult { + let safeOffset = max(offset, 0) + let safeLimit = max(limit, 0) + let start = min(safeOffset, result.rows.count) + let end = min(start + safeLimit, result.rows.count) + return PluginQueryResult( + columns: result.columns, + columnTypeNames: result.columnTypeNames, + rows: Array(result.rows[start.. [URL] { + Array(Set(ledger.sourceFiles + ledger.watchedDirectories)).sorted { $0.path < $1.path } + } + private func expandPath(_ path: String) -> String { guard path.hasPrefix("~") else { return path } return NSString(string: path).expandingTildeInPath @@ -733,6 +763,21 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self) +private final class PipeDataCollector: @unchecked Sendable { + private let lock = NSLock() + private var storage = Data() + + var data: Data { + lock.withLock { storage } + } + + func set(_ data: Data) { + lock.withLock { + storage = data + } + } +} + private extension NSLock { func withLock(_ body: () throws -> T) rethrows -> T { lock() diff --git a/README.md b/README.md index e8375cd6c..0abdcd192 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ TablePro is the missing fourth: native, multi-database, and open source. | MongoDB | Plugin | | Oracle Database | Plugin | | DuckDB | Plugin | -| Beancount | Plugin | +| Beancount | Built-in | | Cassandra / ScyllaDB | Plugin | | Etcd | Plugin | | Cloudflare D1 | Plugin | diff --git a/README.vi.md b/README.vi.md index 458911e77..939ab0e69 100644 --- a/README.vi.md +++ b/README.vi.md @@ -82,7 +82,7 @@ TablePro là mảnh thứ tư còn thiếu: native, đa database, và mã nguồ | MongoDB | Plugin | | Oracle Database | Plugin | | DuckDB | Plugin | -| Beancount | Plugin | +| Beancount | Tích hợp sẵn | | Cassandra / ScyllaDB | Plugin | | Etcd | Plugin | | Cloudflare D1 | Plugin | diff --git a/TablePro/Assets.xcassets/beancount-icon.imageset/Contents.json b/TablePro/Assets.xcassets/beancount-icon.imageset/Contents.json new file mode 100644 index 000000000..ba8496a7b --- /dev/null +++ b/TablePro/Assets.xcassets/beancount-icon.imageset/Contents.json @@ -0,0 +1,16 @@ +{ + "images" : [ + { + "filename" : "beancount.svg", + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + }, + "properties" : { + "preserves-vector-representation" : true, + "template-rendering-intent" : "template" + } +} diff --git a/TablePro/Assets.xcassets/beancount-icon.imageset/beancount.svg b/TablePro/Assets.xcassets/beancount-icon.imageset/beancount.svg new file mode 100644 index 000000000..f9f4702c7 --- /dev/null +++ b/TablePro/Assets.xcassets/beancount-icon.imageset/beancount.svg @@ -0,0 +1 @@ +Beancount diff --git a/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift b/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift index 652868123..7637a0b4e 100644 --- a/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift +++ b/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift @@ -855,82 +855,6 @@ extension PluginMetadataRegistry { tagline: String(localized: "Embedded analytical SQL") ) )), - ("Beancount", PluginMetadataSnapshot( - displayName: "Beancount", iconName: "beancount-icon", defaultPort: 0, - requiresAuthentication: false, supportsForeignKeys: false, supportsSchemaEditing: false, - isDownloadable: true, primaryUrlScheme: "beancount", parameterStyle: .questionMark, - navigationModel: .standard, explainVariants: [], pathFieldRole: .filePath, - supportsHealthMonitor: false, urlSchemes: ["beancount"], postConnectActions: [], - brandColorHex: "#3F7D20", - queryLanguageName: "SQL", editorLanguage: .sql, - connectionMode: .fileBased, supportsDatabaseSwitching: false, - supportsColumnReorder: false, - capabilities: PluginMetadataSnapshot.CapabilityFlags( - supportsSchemaSwitching: false, - supportsImport: false, - supportsExport: true, - supportsSSH: false, - supportsSSL: false, - supportsCascadeDrop: false, - supportsForeignKeyDisable: false, - supportsReadOnlyMode: true, - supportsQueryProgress: false, - requiresReconnectForDatabaseSwitch: false, - supportsDropDatabase: false, - supportsAddColumn: false, - supportsModifyColumn: false, - supportsDropColumn: false, - supportsRenameColumn: false, - supportsAddIndex: false, - supportsDropIndex: false, - supportsModifyPrimaryKey: false - ), - schema: PluginMetadataSnapshot.SchemaInfo( - defaultSchemaName: "public", - defaultGroupName: "main", - tableEntityName: "Ledger Tables", - defaultPrimaryKeyColumn: nil, - immutableColumns: [ - "id", "transaction_id", "date", "flag", "payee", "narration", - "account", "amount", "commodity", "currency", "source_file", "line" - ], - systemDatabaseNames: [], - systemSchemaNames: [], - fileExtensions: ["beancount"], - databaseGroupingStrategy: .flat, - structureColumnFields: [.name, .type, .nullable, .comment] - ), - editor: PluginMetadataSnapshot.EditorConfig( - sqlDialect: SQLDialectDescriptor( - identifierQuote: "\"", - keywords: [ - "SELECT", "FROM", "WHERE", "JOIN", "INNER", "LEFT", "RIGHT", "OUTER", - "ON", "AND", "OR", "NOT", "IN", "LIKE", "BETWEEN", "AS", - "ORDER", "BY", "GROUP", "HAVING", "LIMIT", "OFFSET", - "WITH", "UNION", "INTERSECT", "EXCEPT", - "CASE", "WHEN", "THEN", "ELSE", "END", "NULL", "IS", - "ASC", "DESC", "DISTINCT" - ], - functions: ["COUNT", "SUM", "AVG", "MAX", "MIN", "ROUND", "DATE", "STRFTIME"], - dataTypes: ["INTEGER", "TEXT", "DATE", "DECIMAL"], - regexSyntax: .unsupported, - booleanLiteralStyle: .numeric, - likeEscapeStyle: .explicit, - paginationStyle: .limit - ), - statementCompletions: [], - columnTypesByCategory: [ - "Integer": ["INTEGER"], - "String": ["TEXT"], - "Date": ["DATE"], - "Decimal": ["DECIMAL"] - ] - ), - connection: PluginMetadataSnapshot.ConnectionConfig( - category: .analytical, - tagline: String(localized: "Plain-text accounting ledgers") - ) - )), ("Cassandra", PluginMetadataSnapshot( displayName: "Cassandra / ScyllaDB", iconName: "cassandra-icon", defaultPort: 9_042, requiresAuthentication: false, supportsForeignKeys: false, supportsSchemaEditing: true, diff --git a/TablePro/Core/Plugins/PluginMetadataRegistry.swift b/TablePro/Core/Plugins/PluginMetadataRegistry.swift index fcf845315..6a73909ec 100644 --- a/TablePro/Core/Plugins/PluginMetadataRegistry.swift +++ b/TablePro/Core/Plugins/PluginMetadataRegistry.swift @@ -775,6 +775,82 @@ final class PluginMetadataRegistry: @unchecked Sendable { category: .relational, tagline: String(localized: "Embedded zero-config SQL database") ) + )), + ("Beancount", PluginMetadataSnapshot( + displayName: "Beancount", iconName: "beancount-icon", defaultPort: 0, + requiresAuthentication: false, supportsForeignKeys: false, supportsSchemaEditing: false, + isDownloadable: false, primaryUrlScheme: "beancount", parameterStyle: .questionMark, + navigationModel: .standard, explainVariants: [], pathFieldRole: .filePath, + supportsHealthMonitor: false, urlSchemes: ["beancount"], postConnectActions: [], + brandColorHex: "#3F7D20", + queryLanguageName: "SQL", editorLanguage: .sql, + connectionMode: .fileBased, supportsDatabaseSwitching: false, + supportsColumnReorder: false, + capabilities: PluginMetadataSnapshot.CapabilityFlags( + supportsSchemaSwitching: false, + supportsImport: false, + supportsExport: true, + supportsSSH: false, + supportsSSL: false, + supportsCascadeDrop: false, + supportsForeignKeyDisable: false, + supportsReadOnlyMode: true, + supportsQueryProgress: false, + requiresReconnectForDatabaseSwitch: false, + supportsDropDatabase: false, + supportsAddColumn: false, + supportsModifyColumn: false, + supportsDropColumn: false, + supportsRenameColumn: false, + supportsAddIndex: false, + supportsDropIndex: false, + supportsModifyPrimaryKey: false + ), + schema: PluginMetadataSnapshot.SchemaInfo( + defaultSchemaName: "public", + defaultGroupName: "main", + tableEntityName: "Ledger Tables", + defaultPrimaryKeyColumn: nil, + immutableColumns: [ + "id", "transaction_id", "date", "flag", "payee", "narration", + "account", "amount", "commodity", "currency", "source_file", "line" + ], + systemDatabaseNames: [], + systemSchemaNames: [], + fileExtensions: ["beancount"], + databaseGroupingStrategy: .flat, + structureColumnFields: [.name, .type, .nullable, .comment] + ), + editor: PluginMetadataSnapshot.EditorConfig( + sqlDialect: SQLDialectDescriptor( + identifierQuote: "\"", + keywords: [ + "SELECT", "FROM", "WHERE", "JOIN", "INNER", "LEFT", "RIGHT", "OUTER", + "ON", "AND", "OR", "NOT", "IN", "LIKE", "BETWEEN", "AS", + "ORDER", "BY", "GROUP", "HAVING", "LIMIT", "OFFSET", + "WITH", "UNION", "INTERSECT", "EXCEPT", + "CASE", "WHEN", "THEN", "ELSE", "END", "NULL", "IS", + "ASC", "DESC", "DISTINCT" + ], + functions: ["COUNT", "SUM", "AVG", "MAX", "MIN", "ROUND", "DATE", "STRFTIME"], + dataTypes: ["INTEGER", "TEXT", "DATE", "DECIMAL"], + regexSyntax: .unsupported, + booleanLiteralStyle: .numeric, + likeEscapeStyle: .explicit, + paginationStyle: .limit + ), + statementCompletions: [], + columnTypesByCategory: [ + "Integer": ["INTEGER"], + "String": ["TEXT"], + "Date": ["DATE"], + "Decimal": ["DECIMAL"] + ] + ), + connection: PluginMetadataSnapshot.ConnectionConfig( + category: .analytical, + tagline: String(localized: "Plain-text accounting ledgers") + ) )) ] // swiftlint:enable function_body_length diff --git a/TablePro/Models/Connection/DatabaseConnection.swift b/TablePro/Models/Connection/DatabaseConnection.swift index dda3adc46..219cdddcc 100644 --- a/TablePro/Models/Connection/DatabaseConnection.swift +++ b/TablePro/Models/Connection/DatabaseConnection.swift @@ -45,6 +45,7 @@ extension DatabaseType { static let bigQuery = DatabaseType(rawValue: "BigQuery") static let libsql = DatabaseType(rawValue: "libSQL") static let turso = DatabaseType(rawValue: "Turso") + static let beancount = DatabaseType(rawValue: "Beancount") } extension DatabaseType: Codable { @@ -179,6 +180,7 @@ extension DatabaseType { case "libSQL", "Turso": Color(hex: "4FF8D2") case "DynamoDB": Color(hex: "4053D6") case "BigQuery": Color(hex: "4285F4") + case "Beancount": Color(hex: "3F7D20") default: Color.accentColor } } diff --git a/TableProTests/Models/DatabaseTypeTests.swift b/TableProTests/Models/DatabaseTypeTests.swift index ee4d1616b..f987721f0 100644 --- a/TableProTests/Models/DatabaseTypeTests.swift +++ b/TableProTests/Models/DatabaseTypeTests.swift @@ -65,7 +65,8 @@ struct DatabaseTypeTests { (DatabaseType.clickhouse, "ClickHouse"), (DatabaseType.duckdb, "DuckDB"), (DatabaseType.cassandra, "Cassandra"), - (DatabaseType.scylladb, "ScyllaDB") + (DatabaseType.scylladb, "ScyllaDB"), + (DatabaseType.beancount, "Beancount") ]) func testRawValueMatchesDisplayName(dbType: DatabaseType, expectedRawValue: String) { #expect(dbType.rawValue == expectedRawValue) diff --git a/TableProTests/Plugins/BeancountDriverMetadataTests.swift b/TableProTests/Plugins/BeancountDriverMetadataTests.swift index ed892cb81..a64705e2c 100644 --- a/TableProTests/Plugins/BeancountDriverMetadataTests.swift +++ b/TableProTests/Plugins/BeancountDriverMetadataTests.swift @@ -11,11 +11,11 @@ import Testing @MainActor @Suite("Beancount driver metadata") struct BeancountDriverMetadataTests { - @Test("registry exposes Beancount as a downloadable file-based driver") + @Test("registry exposes Beancount as a bundled file-based driver") func registryMetadata() throws { let snapshot = try #require(PluginMetadataRegistry.shared.snapshot(forTypeId: "Beancount")) #expect(snapshot.displayName == "Beancount") - #expect(snapshot.isDownloadable == true) + #expect(snapshot.isDownloadable == false) #expect(snapshot.connectionMode == .fileBased) #expect(snapshot.schema.fileExtensions == ["beancount"]) #expect(snapshot.pathFieldRole == .filePath) diff --git a/TableProTests/Plugins/BeancountLedgerParserTests.swift b/TableProTests/Plugins/BeancountLedgerParserTests.swift index e57549682..99207b6b0 100644 --- a/TableProTests/Plugins/BeancountLedgerParserTests.swift +++ b/TableProTests/Plugins/BeancountLedgerParserTests.swift @@ -88,5 +88,29 @@ struct BeancountLedgerParserTests { "expenses.beancount", "main.beancount" ]) + #expect(parsed.watchedDirectories.map(\.lastPathComponent).contains("imports")) + #expect(parsed.watchedDirectories.map(\.lastPathComponent).contains("nested")) + } + + @Test("ignores transaction metadata lines when parsing postings") + func ignoresMetadataLinesInsideTransactions() throws { + let tempDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("beancount-parser-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: tempDirectory, withIntermediateDirectories: true) + defer { + try? FileManager.default.removeItem(at: tempDirectory) + } + + let ledger = tempDirectory.appendingPathComponent("main.beancount") + try """ + 2024-01-15 * "Grocery Store" "Weekly shop" + receipt: "abc.pdf" + Assets:Bank:Checking -100.00 USD + Expenses:Food 100.00 USD + """.write(to: ledger, atomically: true, encoding: .utf8) + + let parsed = try BeancountLedgerParser().parse(fileURL: ledger) + + #expect(parsed.postings.map(\.account) == ["Assets:Bank:Checking", "Expenses:Food"]) } } diff --git a/TableProTests/Plugins/BeancountPluginDriverTests.swift b/TableProTests/Plugins/BeancountPluginDriverTests.swift index 844e346d0..f5648ca77 100644 --- a/TableProTests/Plugins/BeancountPluginDriverTests.swift +++ b/TableProTests/Plugins/BeancountPluginDriverTests.swift @@ -52,6 +52,49 @@ struct BeancountPluginDriverTests { #expect(result.rows.map { $0[0].asText } == ["Assets:Bank:Checking", "Expenses:Food"]) } + @Test("reloads the SQL projection when a glob include matches a new file") + func reloadsWhenGlobIncludeMatchesNewFile() async throws { + let tempDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("beancount-driver-glob-\(UUID().uuidString)", isDirectory: true) + try FileManager.default.createDirectory(at: tempDirectory, withIntermediateDirectories: true) + defer { + try? FileManager.default.removeItem(at: tempDirectory) + } + + let imports = tempDirectory.appendingPathComponent("imports", isDirectory: true) + try FileManager.default.createDirectory(at: imports, withIntermediateDirectories: true) + try """ + 2024-01-01 open Assets:Bank:Checking USD + """.write(to: imports.appendingPathComponent("accounts.beancount"), atomically: true, encoding: .utf8) + + let ledger = tempDirectory.appendingPathComponent("main.beancount") + try """ + include "imports/*.beancount" + """.write(to: ledger, atomically: true, encoding: .utf8) + + let driver = BeancountPluginDriver(config: DriverConnectionConfig( + host: "", + port: 0, + username: "", + password: "", + database: ledger.path + )) + try await driver.connect() + defer { + driver.disconnect() + } + + var result = try await driver.execute(query: "SELECT name FROM accounts ORDER BY name") + #expect(result.rows.map { $0[0].asText } == ["Assets:Bank:Checking"]) + + try """ + 2024-01-02 open Expenses:Food USD + """.write(to: imports.appendingPathComponent("expenses.beancount"), atomically: true, encoding: .utf8) + + result = try await driver.execute(query: "SELECT name FROM accounts ORDER BY name") + #expect(result.rows.map { $0[0].asText } == ["Assets:Bank:Checking", "Expenses:Food"]) + } + @Test("rejects write queries") func rejectsWriteQueries() async throws { let tempDirectory = FileManager.default.temporaryDirectory @@ -123,6 +166,16 @@ struct BeancountPluginDriverTests { "Expenses:Food", "Income:Salary" ]) + + let count = try await driver.fetchRowCount(query: "BQL: SELECT account FROM accounts ORDER BY account") + #expect(count == 3) + + let page = try await driver.fetchRows( + query: "BQL: SELECT account FROM accounts ORDER BY account", + offset: 1, + limit: 1 + ) + #expect(page.rows.map { $0.first?.asText } == ["Expenses:Food"]) } private static func installedRustledgerPath() -> String? { diff --git a/docs/databases/beancount.mdx b/docs/databases/beancount.mdx new file mode 100644 index 000000000..e6a586501 --- /dev/null +++ b/docs/databases/beancount.mdx @@ -0,0 +1,66 @@ +--- +title: Beancount +description: Open Beancount ledgers with TablePro +--- + +# Beancount + +TablePro opens `.beancount` ledgers as read-only, file-based connections. The driver projects transactions, postings, accounts, prices, balances, and source files into SQL tables for browsing and exports. + +The bundled driver also supports BQL queries through the bundled `rustledger` helper. + +## Connecting to a Beancount ledger + + + + Open TablePro and click **New Connection** or press ⌘N. + + + Choose **Beancount** from the database type list. + + + Click **Browse** and select a `.beancount` file. + + + Click **Connect** to open the ledger. + + + +## Connection URL + +```text +beancount:///path/to/main.beancount +``` + +See [Connection URL Reference](/databases/connection-urls) for all parameters. + +## Tables + +| Table | Contents | +|-------|----------| +| `transactions` | Transaction date, flag, payee, narration, source file, and line | +| `postings` | Posting account, amount, commodity, source file, and line | +| `accounts` | Opened accounts and declared currencies | +| `prices` | Price directives | +| `balances` | Balance directives | +| `source_files` | Parsed ledger and include files | + +## Includes + +The parser follows Beancount `include` directives. Literal includes and glob patterns such as `include "imports/*.beancount"` and `include "imports/**/*.beancount"` are supported. + +## BQL + +Prefix a query with `BQL:` to run it through `rustledger`: + +```sql +BQL: SELECT account FROM accounts ORDER BY account +``` + +Table browsing, row counts, and pagination work for BQL results. BQL queries do not support SQL parameters. + +## Limitations + +- Beancount connections are read-only. +- Schema editing, imports, SSH, SSL, and database switching are not available for ledgers. +- The SQL projection covers common ledger directives; unsupported directives remain available in the original source files. diff --git a/docs/databases/overview.mdx b/docs/databases/overview.mdx index cde26ce68..544712b7a 100644 --- a/docs/databases/overview.mdx +++ b/docs/databases/overview.mdx @@ -49,6 +49,9 @@ Natively supported: DuckDB embedded OLAP database. File-based, no server required + + Plain-text accounting ledgers. File-based, read-only + Amazon DynamoDB via AWS SDK. NoSQL key-value and document database @@ -497,4 +500,3 @@ Right-click a connection to edit or delete it. Changes take effect on the next c ## Backup and Restore Connections are stored in `~/Library/Preferences/com.TablePro.plist`. Passwords are in the macOS Keychain. Copy the `.plist` file to back up. You'll need to re-enter passwords after restoring since Keychain entries don't transfer. - diff --git a/docs/docs.json b/docs/docs.json index e1a0367c9..a564515f9 100644 --- a/docs/docs.json +++ b/docs/docs.json @@ -59,6 +59,7 @@ "pages": [ "databases/sqlite", "databases/duckdb", + "databases/beancount", "databases/libsql" ] }, diff --git a/docs/index.mdx b/docs/index.mdx index a7ce035cb..62b42340c 100644 --- a/docs/index.mdx +++ b/docs/index.mdx @@ -28,7 +28,7 @@ Native macOS client for every database. Built on SwiftUI and AppKit. Ships under Swift & Apple frameworks. No Electron. Pure macOS responsiveness. - 17 databases: MySQL, PostgreSQL, SQLite, MongoDB, Redis, Oracle, and more. + 18 databases: MySQL, PostgreSQL, SQLite, MongoDB, Redis, Oracle, and more. Context-aware autocomplete with schema and syntax awareness. @@ -72,6 +72,7 @@ Native macOS client for every database. Built on SwiftUI and AppKit. Ships under | MongoDB | 27017 | Plugin | | Oracle Database | 1521 | Plugin | | DuckDB | N/A (file-based) | Plugin | +| Beancount | N/A (file-based) | Built-in | | Cassandra / ScyllaDB | 9042 | Plugin | | Etcd | 2379 | Plugin | | Cloudflare D1 | N/A (API-based) | Plugin | diff --git a/scripts/download-rustledger.sh b/scripts/download-rustledger.sh index 644476058..1912b9f04 100755 --- a/scripts/download-rustledger.sh +++ b/scripts/download-rustledger.sh @@ -136,11 +136,5 @@ if [[ "${#helpers[@]}" -gt 1 && -x "$(command -v lipo)" ]]; then exit 0 fi -for installed in /opt/homebrew/bin/rledger /usr/local/bin/rledger; do - if copy_helper "$installed" "Bundled installed rustledger helper after release download failed"; then - exit 0 - fi -done - -echo "Unable to bundle rustledger. Install rledger or set TABLEPRO_RUSTLEDGER_BINARY." >&2 +echo "Unable to bundle rustledger from the pinned release. Set TABLEPRO_RUSTLEDGER_BINARY for local builds or retry the release download." >&2 exit 1 From 1b76e0ff6819fcc50f990740e2bf951a64f79d81 Mon Sep 17 00:00:00 2001 From: salmonumbrella <182032677+salmonumbrella@users.noreply.github.com> Date: Sat, 30 May 2026 12:21:23 -0700 Subject: [PATCH 3/4] Make Beancount a downloadable plugin --- .github/workflows/build-plugin.yml | 5 ++ CHANGELOG.md | 2 +- .../BeancountPlugin.swift | 2 +- README.md | 2 +- TablePro.xcodeproj/project.pbxproj | 15 ---- ...ginMetadataRegistry+RegistryDefaults.swift | 76 +++++++++++++++++++ .../Core/Plugins/PluginMetadataRegistry.swift | 76 ------------------- .../BeancountDriverMetadataTests.swift | 4 +- docs/index.mdx | 2 +- scripts/build-plugin.sh | 10 +++ scripts/download-rustledger.sh | 4 + scripts/release-all-plugins.sh | 1 + 12 files changed, 102 insertions(+), 97 deletions(-) diff --git a/.github/workflows/build-plugin.yml b/.github/workflows/build-plugin.yml index 33b0cdfdb..210d44129 100644 --- a/.github/workflows/build-plugin.yml +++ b/.github/workflows/build-plugin.yml @@ -154,6 +154,11 @@ jobs: DISPLAY_NAME="DuckDB Driver"; SUMMARY="DuckDB analytical database driver" DB_TYPE_IDS='["DuckDB"]'; ICON="bird"; BUNDLE_NAME="DuckDBDriver" CATEGORY="database-driver"; HOMEPAGE="https://docs.tablepro.app/databases/duckdb" ;; + beancount) + TARGET="BeancountDriver"; BUNDLE_ID="com.TablePro.BeancountDriver" + DISPLAY_NAME="Beancount Driver"; SUMMARY="Read-only Beancount ledger driver with bundled rustledger BQL helper" + DB_TYPE_IDS='["Beancount"]'; ICON="beancount-icon"; BUNDLE_NAME="BeancountDriver" + CATEGORY="database-driver"; HOMEPAGE="https://docs.tablepro.app/databases/beancount" ;; cassandra) TARGET="CassandraDriver"; BUNDLE_ID="com.TablePro.CassandraDriver" DISPLAY_NAME="Cassandra Driver"; SUMMARY="Apache Cassandra and ScyllaDB driver via DataStax C driver" diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eef9ed54..bc3b69826 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- Beancount ledger support as a bundled, read-only file-based driver with SQL projection and BQL queries. +- Beancount ledger support as a downloadable, read-only file-based driver with SQL projection and BQL queries. ## [0.46.0] - 2026-05-28 diff --git a/Plugins/BeancountDriverPlugin/BeancountPlugin.swift b/Plugins/BeancountDriverPlugin/BeancountPlugin.swift index cb68c4875..62e918995 100644 --- a/Plugins/BeancountDriverPlugin/BeancountPlugin.swift +++ b/Plugins/BeancountDriverPlugin/BeancountPlugin.swift @@ -17,7 +17,7 @@ final class BeancountPlugin: NSObject, TableProPlugin, DriverPlugin { static let iconName = "beancount-icon" static let defaultPort = 0 - static let isDownloadable = false + static let isDownloadable = true static let pathFieldRole: PathFieldRole = .filePath static let requiresAuthentication = false static let supportsSSH = false diff --git a/README.md b/README.md index 0abdcd192..e8375cd6c 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ TablePro is the missing fourth: native, multi-database, and open source. | MongoDB | Plugin | | Oracle Database | Plugin | | DuckDB | Plugin | -| Beancount | Built-in | +| Beancount | Plugin | | Cassandra / ScyllaDB | Plugin | | Etcd | Plugin | | Cloudflare D1 | Plugin | diff --git a/TablePro.xcodeproj/project.pbxproj b/TablePro.xcodeproj/project.pbxproj index 2178f46a3..79940d485 100644 --- a/TablePro.xcodeproj/project.pbxproj +++ b/TablePro.xcodeproj/project.pbxproj @@ -27,7 +27,6 @@ 5A868000D00000000 /* PostgreSQLDriver.tableplugin in Copy Plug-Ins (12 items) */ = {isa = PBXBuildFile; fileRef = 5A868000100000000 /* PostgreSQLDriver.tableplugin */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 5A869000A00000000 /* TableProPluginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A860000100000000 /* TableProPluginKit.framework */; }; 5ABC147400000000000001 /* TableProPluginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A860000100000000 /* TableProPluginKit.framework */; }; - 5ABC147400000000000002 /* BeancountDriver.tableplugin in Copy Plug-Ins (13 items) */ = {isa = PBXBuildFile; fileRef = 5ABC147400000000000003 /* BeancountDriver.tableplugin */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 5A86A000A00000000 /* TableProPluginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A860000100000000 /* TableProPluginKit.framework */; }; 5A86A000D00000000 /* CSVExport.tableplugin in Copy Plug-Ins (12 items) */ = {isa = PBXBuildFile; fileRef = 5A86A000100000000 /* CSVExport.tableplugin */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 5A86B000A00000000 /* TableProPluginKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5A860000100000000 /* TableProPluginKit.framework */; }; @@ -148,13 +147,6 @@ remoteGlobalIDString = 5A869000000000000; remoteInfo = DuckDBDriver; }; - 5ABC147400000000000004 /* PBXContainerItemProxy */ = { - isa = PBXContainerItemProxy; - containerPortal = 5A1091BF2EF17EDC0055EA7C /* Project object */; - proxyType = 1; - remoteGlobalIDString = 5ABC147400000000000005; - remoteInfo = BeancountDriver; - }; 5ABC14740000000000000F /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 5A1091BF2EF17EDC0055EA7C /* Project object */; @@ -266,7 +258,6 @@ 5A862000D00000000 /* SQLiteDriver.tableplugin in Copy Plug-Ins (12 items) */, 5A863000D00000000 /* ClickHouseDriver.tableplugin in Copy Plug-Ins (12 items) */, 5A867000D00000000 /* RedisDriver.tableplugin in Copy Plug-Ins (12 items) */, - 5ABC147400000000000002 /* BeancountDriver.tableplugin in Copy Plug-Ins (13 items) */, 5A86A000D00000000 /* CSVExport.tableplugin in Copy Plug-Ins (12 items) */, 5A86B000D00000000 /* JSONExport.tableplugin in Copy Plug-Ins (12 items) */, 5A86C000D00000000 /* SQLExport.tableplugin in Copy Plug-Ins (12 items) */, @@ -1109,7 +1100,6 @@ 5A867000C00000000 /* PBXTargetDependency */, 5A868000C00000000 /* PBXTargetDependency */, 5A869000C00000000 /* PBXTargetDependency */, - 5ABC14740000000000000C /* PBXTargetDependency */, 5A86A000C00000000 /* PBXTargetDependency */, 5A86B000C00000000 /* PBXTargetDependency */, 5A86C000C00000000 /* PBXTargetDependency */, @@ -2280,11 +2270,6 @@ target = 5A869000000000000 /* DuckDBDriver */; targetProxy = 5A869000B00000000 /* PBXContainerItemProxy */; }; - 5ABC14740000000000000C /* PBXTargetDependency */ = { - isa = PBXTargetDependency; - target = 5ABC147400000000000005 /* BeancountDriver */; - targetProxy = 5ABC147400000000000004 /* PBXContainerItemProxy */; - }; 5ABC147400000000000010 /* PBXTargetDependency */ = { isa = PBXTargetDependency; target = 5A860000000000000 /* TableProPluginKit */; diff --git a/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift b/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift index 7637a0b4e..652868123 100644 --- a/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift +++ b/TablePro/Core/Plugins/PluginMetadataRegistry+RegistryDefaults.swift @@ -855,6 +855,82 @@ extension PluginMetadataRegistry { tagline: String(localized: "Embedded analytical SQL") ) )), + ("Beancount", PluginMetadataSnapshot( + displayName: "Beancount", iconName: "beancount-icon", defaultPort: 0, + requiresAuthentication: false, supportsForeignKeys: false, supportsSchemaEditing: false, + isDownloadable: true, primaryUrlScheme: "beancount", parameterStyle: .questionMark, + navigationModel: .standard, explainVariants: [], pathFieldRole: .filePath, + supportsHealthMonitor: false, urlSchemes: ["beancount"], postConnectActions: [], + brandColorHex: "#3F7D20", + queryLanguageName: "SQL", editorLanguage: .sql, + connectionMode: .fileBased, supportsDatabaseSwitching: false, + supportsColumnReorder: false, + capabilities: PluginMetadataSnapshot.CapabilityFlags( + supportsSchemaSwitching: false, + supportsImport: false, + supportsExport: true, + supportsSSH: false, + supportsSSL: false, + supportsCascadeDrop: false, + supportsForeignKeyDisable: false, + supportsReadOnlyMode: true, + supportsQueryProgress: false, + requiresReconnectForDatabaseSwitch: false, + supportsDropDatabase: false, + supportsAddColumn: false, + supportsModifyColumn: false, + supportsDropColumn: false, + supportsRenameColumn: false, + supportsAddIndex: false, + supportsDropIndex: false, + supportsModifyPrimaryKey: false + ), + schema: PluginMetadataSnapshot.SchemaInfo( + defaultSchemaName: "public", + defaultGroupName: "main", + tableEntityName: "Ledger Tables", + defaultPrimaryKeyColumn: nil, + immutableColumns: [ + "id", "transaction_id", "date", "flag", "payee", "narration", + "account", "amount", "commodity", "currency", "source_file", "line" + ], + systemDatabaseNames: [], + systemSchemaNames: [], + fileExtensions: ["beancount"], + databaseGroupingStrategy: .flat, + structureColumnFields: [.name, .type, .nullable, .comment] + ), + editor: PluginMetadataSnapshot.EditorConfig( + sqlDialect: SQLDialectDescriptor( + identifierQuote: "\"", + keywords: [ + "SELECT", "FROM", "WHERE", "JOIN", "INNER", "LEFT", "RIGHT", "OUTER", + "ON", "AND", "OR", "NOT", "IN", "LIKE", "BETWEEN", "AS", + "ORDER", "BY", "GROUP", "HAVING", "LIMIT", "OFFSET", + "WITH", "UNION", "INTERSECT", "EXCEPT", + "CASE", "WHEN", "THEN", "ELSE", "END", "NULL", "IS", + "ASC", "DESC", "DISTINCT" + ], + functions: ["COUNT", "SUM", "AVG", "MAX", "MIN", "ROUND", "DATE", "STRFTIME"], + dataTypes: ["INTEGER", "TEXT", "DATE", "DECIMAL"], + regexSyntax: .unsupported, + booleanLiteralStyle: .numeric, + likeEscapeStyle: .explicit, + paginationStyle: .limit + ), + statementCompletions: [], + columnTypesByCategory: [ + "Integer": ["INTEGER"], + "String": ["TEXT"], + "Date": ["DATE"], + "Decimal": ["DECIMAL"] + ] + ), + connection: PluginMetadataSnapshot.ConnectionConfig( + category: .analytical, + tagline: String(localized: "Plain-text accounting ledgers") + ) + )), ("Cassandra", PluginMetadataSnapshot( displayName: "Cassandra / ScyllaDB", iconName: "cassandra-icon", defaultPort: 9_042, requiresAuthentication: false, supportsForeignKeys: false, supportsSchemaEditing: true, diff --git a/TablePro/Core/Plugins/PluginMetadataRegistry.swift b/TablePro/Core/Plugins/PluginMetadataRegistry.swift index 6a73909ec..fcf845315 100644 --- a/TablePro/Core/Plugins/PluginMetadataRegistry.swift +++ b/TablePro/Core/Plugins/PluginMetadataRegistry.swift @@ -775,82 +775,6 @@ final class PluginMetadataRegistry: @unchecked Sendable { category: .relational, tagline: String(localized: "Embedded zero-config SQL database") ) - )), - ("Beancount", PluginMetadataSnapshot( - displayName: "Beancount", iconName: "beancount-icon", defaultPort: 0, - requiresAuthentication: false, supportsForeignKeys: false, supportsSchemaEditing: false, - isDownloadable: false, primaryUrlScheme: "beancount", parameterStyle: .questionMark, - navigationModel: .standard, explainVariants: [], pathFieldRole: .filePath, - supportsHealthMonitor: false, urlSchemes: ["beancount"], postConnectActions: [], - brandColorHex: "#3F7D20", - queryLanguageName: "SQL", editorLanguage: .sql, - connectionMode: .fileBased, supportsDatabaseSwitching: false, - supportsColumnReorder: false, - capabilities: PluginMetadataSnapshot.CapabilityFlags( - supportsSchemaSwitching: false, - supportsImport: false, - supportsExport: true, - supportsSSH: false, - supportsSSL: false, - supportsCascadeDrop: false, - supportsForeignKeyDisable: false, - supportsReadOnlyMode: true, - supportsQueryProgress: false, - requiresReconnectForDatabaseSwitch: false, - supportsDropDatabase: false, - supportsAddColumn: false, - supportsModifyColumn: false, - supportsDropColumn: false, - supportsRenameColumn: false, - supportsAddIndex: false, - supportsDropIndex: false, - supportsModifyPrimaryKey: false - ), - schema: PluginMetadataSnapshot.SchemaInfo( - defaultSchemaName: "public", - defaultGroupName: "main", - tableEntityName: "Ledger Tables", - defaultPrimaryKeyColumn: nil, - immutableColumns: [ - "id", "transaction_id", "date", "flag", "payee", "narration", - "account", "amount", "commodity", "currency", "source_file", "line" - ], - systemDatabaseNames: [], - systemSchemaNames: [], - fileExtensions: ["beancount"], - databaseGroupingStrategy: .flat, - structureColumnFields: [.name, .type, .nullable, .comment] - ), - editor: PluginMetadataSnapshot.EditorConfig( - sqlDialect: SQLDialectDescriptor( - identifierQuote: "\"", - keywords: [ - "SELECT", "FROM", "WHERE", "JOIN", "INNER", "LEFT", "RIGHT", "OUTER", - "ON", "AND", "OR", "NOT", "IN", "LIKE", "BETWEEN", "AS", - "ORDER", "BY", "GROUP", "HAVING", "LIMIT", "OFFSET", - "WITH", "UNION", "INTERSECT", "EXCEPT", - "CASE", "WHEN", "THEN", "ELSE", "END", "NULL", "IS", - "ASC", "DESC", "DISTINCT" - ], - functions: ["COUNT", "SUM", "AVG", "MAX", "MIN", "ROUND", "DATE", "STRFTIME"], - dataTypes: ["INTEGER", "TEXT", "DATE", "DECIMAL"], - regexSyntax: .unsupported, - booleanLiteralStyle: .numeric, - likeEscapeStyle: .explicit, - paginationStyle: .limit - ), - statementCompletions: [], - columnTypesByCategory: [ - "Integer": ["INTEGER"], - "String": ["TEXT"], - "Date": ["DATE"], - "Decimal": ["DECIMAL"] - ] - ), - connection: PluginMetadataSnapshot.ConnectionConfig( - category: .analytical, - tagline: String(localized: "Plain-text accounting ledgers") - ) )) ] // swiftlint:enable function_body_length diff --git a/TableProTests/Plugins/BeancountDriverMetadataTests.swift b/TableProTests/Plugins/BeancountDriverMetadataTests.swift index a64705e2c..ed892cb81 100644 --- a/TableProTests/Plugins/BeancountDriverMetadataTests.swift +++ b/TableProTests/Plugins/BeancountDriverMetadataTests.swift @@ -11,11 +11,11 @@ import Testing @MainActor @Suite("Beancount driver metadata") struct BeancountDriverMetadataTests { - @Test("registry exposes Beancount as a bundled file-based driver") + @Test("registry exposes Beancount as a downloadable file-based driver") func registryMetadata() throws { let snapshot = try #require(PluginMetadataRegistry.shared.snapshot(forTypeId: "Beancount")) #expect(snapshot.displayName == "Beancount") - #expect(snapshot.isDownloadable == false) + #expect(snapshot.isDownloadable == true) #expect(snapshot.connectionMode == .fileBased) #expect(snapshot.schema.fileExtensions == ["beancount"]) #expect(snapshot.pathFieldRole == .filePath) diff --git a/docs/index.mdx b/docs/index.mdx index 62b42340c..835f9fb26 100644 --- a/docs/index.mdx +++ b/docs/index.mdx @@ -72,7 +72,7 @@ Native macOS client for every database. Built on SwiftUI and AppKit. Ships under | MongoDB | 27017 | Plugin | | Oracle Database | 1521 | Plugin | | DuckDB | N/A (file-based) | Plugin | -| Beancount | N/A (file-based) | Built-in | +| Beancount | N/A (file-based) | Plugin | | Cassandra / ScyllaDB | 9042 | Plugin | | Etcd | 2379 | Plugin | | Cloudflare D1 | N/A (API-based) | Plugin | diff --git a/scripts/build-plugin.sh b/scripts/build-plugin.sh index 836a31d9a..a3088e659 100755 --- a/scripts/build-plugin.sh +++ b/scripts/build-plugin.sh @@ -125,6 +125,16 @@ build_plugin() { done fi + # Sign executable helper resources such as Beancount's bundled rledger. + if [ -d "$plugin_bundle/Contents/Resources" ]; then + find "$plugin_bundle/Contents/Resources" -type f -perm -111 | sort | while read -r nested; do + if file "$nested" | grep -q "Mach-O"; then + echo " Signing executable resource: $(basename "$nested")" >&2 + codesign -fs "$SIGN_IDENTITY" --force --options runtime --timestamp "$nested" + fi + done + fi + # Sign the main binary if [ -f "$plugin_binary" ]; then codesign -fs "$SIGN_IDENTITY" --force --options runtime --timestamp "$plugin_binary" diff --git a/scripts/download-rustledger.sh b/scripts/download-rustledger.sh index 1912b9f04..2f60340d0 100755 --- a/scripts/download-rustledger.sh +++ b/scripts/download-rustledger.sh @@ -112,6 +112,10 @@ if [[ "${#requested_triples[@]}" -eq 0 ]]; then fi if [[ -n "${TABLEPRO_RUSTLEDGER_BINARY:-}" ]]; then + if [[ "${CI:-}" == "true" || "${GITHUB_ACTIONS:-}" == "true" ]]; then + echo "TABLEPRO_RUSTLEDGER_BINARY is only allowed for local builds; release builds must use the pinned rustledger download." >&2 + exit 1 + fi copy_helper "$TABLEPRO_RUSTLEDGER_BINARY" "Bundled rustledger helper from TABLEPRO_RUSTLEDGER_BINARY" exit 0 fi diff --git a/scripts/release-all-plugins.sh b/scripts/release-all-plugins.sh index 8db2bf85c..b48f94594 100755 --- a/scripts/release-all-plugins.sh +++ b/scripts/release-all-plugins.sh @@ -30,6 +30,7 @@ PLUGINS=( mongodb oracle duckdb + beancount mssql cassandra etcd From 0066ed0d7eb058154f051251bfbcc47b099971f9 Mon Sep 17 00:00:00 2001 From: salmonumbrella <182032677+salmonumbrella@users.noreply.github.com> Date: Sat, 30 May 2026 12:28:45 -0700 Subject: [PATCH 4/4] Clarify Beancount plugin install docs --- docs/databases/beancount.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/databases/beancount.mdx b/docs/databases/beancount.mdx index e6a586501..479cd6fe6 100644 --- a/docs/databases/beancount.mdx +++ b/docs/databases/beancount.mdx @@ -5,9 +5,9 @@ description: Open Beancount ledgers with TablePro # Beancount -TablePro opens `.beancount` ledgers as read-only, file-based connections. The driver projects transactions, postings, accounts, prices, balances, and source files into SQL tables for browsing and exports. +TablePro opens `.beancount` ledgers as read-only, file-based connections. If the Beancount plugin is not installed yet, TablePro prompts you to download it before opening the ledger. The driver projects transactions, postings, accounts, prices, balances, and source files into SQL tables for browsing and exports. -The bundled driver also supports BQL queries through the bundled `rustledger` helper. +The plugin also supports BQL queries through its packaged `rustledger` helper. ## Connecting to a Beancount ledger