Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .claude/rules/data-sync-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ This path holds user data. A silent wrong answer here looks exactly like an empt
| Approved environment variable references, per connection, names and destination digest | UserDefaults, device-local, never synced | `ConnectionApprovalStore.envVarReferences` |
| Approved sign-ins with this Mac's own credentials (Entra, AWS profile or SSO, Google ADC or OAuth, Kerberos, `~/.pgpass`), per connection, field values and destination digest | UserDefaults `com.TablePro.localCredentialApprovals`, plus `com.TablePro.localCredentialApprovalsMigrated` for the one-time approval of this Mac's rows; device-local, never synced | `ConnectionApprovalStore.localCredentials` |
| Snowflake SSO and MFA tokens, per host and user. A bare account's `NAME.USER` item stays for 0.83.1, which reads only that key | Keychain, device-local | `SnowflakeTokenCache` |
| Whether `connections.json` was ever given an integrity tag, so a deleted tag reads as modified | Keychain `com.TablePro.connectionStoreStamped.<sha256 of path>`, device-local | `ConnectionStoreIntegrity` |
| Whether Local only connections' secrets were copied to device-only items | UserDefaults `com.TablePro.localOnlySecretsKeptOnDevice`, device-local, never synced | `ConnectionStorage` |
| Deletions the server reported for a sync category that is off, applied when the category is next read | UserDefaults, device-local, never synced | `SyncMetadataStorage.deferredDeletions` |
| Where a connection came from (`origin`): this Mac, imported, iCloud | `connections.json`, never synced or exported | `ConnectionStorage` |
| Table load timings (no identifying data, never uploaded) | JSON lines | `TableLoadHistoryStore` |
| Reopen Last Session list (`LastSession.json`, plus `LastOpenConnections.json` holding only the ids that reconnect, which older builds read) | JSON | `LastOpenConnectionsStorage` |
5 changes: 5 additions & 0 deletions apple/Packages/TableProCore/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,11 @@ let package = Package(
path: "Tests/TableProMemcachedCoreTests",
swiftSettings: pluginCoreSettings
),
.testTarget(
name: "TableProDocumentPathTests",
dependencies: ["TableProDocumentPath"],
path: "Tests/TableProDocumentPathTests"
),
.testTarget(
name: "TableProRedisCoreTests",
dependencies: ["TableProRedisCore"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import TableProSQLGrammar

public enum KeywordUppercaseHelper {
public static func isWordBoundary(_ string: String) -> Bool {
Expand All @@ -11,99 +12,39 @@ public enum KeywordUppercaseHelper {
}
}

/// Checks if a UTF-16 character is part of a SQL identifier (a-z, A-Z, 0-9, _).
static func isWordCharacter(_ ch: unichar) -> Bool {
(ch >= 0x41 && ch <= 0x5A) || (ch >= 0x61 && ch <= 0x7A) ||
(ch >= 0x30 && ch <= 0x39) || ch == 0x5F
}

/// Scans backwards up to 2,000 characters to determine if `position` is inside
/// a protected context (string literal, comment, backtick identifier, dollar-quote).
/// Keywords inside protected contexts should NOT be uppercased.
static func isInsideProtectedContext(_ text: NSString, at position: Int) -> Bool {
let scanStart = max(0, position - 2_000)
var inSingleQuote = false
var inDoubleQuote = false
var inBacktick = false
var inLineComment = false
var inBlockComment = false
var inDollarQuote = false
var i = scanStart

while i < position {
let ch = text.character(at: i)

if inBlockComment {
if ch == 0x2A && i + 1 < position && text.character(at: i + 1) == 0x2F {
inBlockComment = false
i += 2
continue
}
i += 1
continue
}
if inLineComment {
if ch == 0x0A { inLineComment = false }
i += 1
continue
}
if inDollarQuote {
if ch == 0x24 && i + 1 < position && text.character(at: i + 1) == 0x24 {
inDollarQuote = false
i += 2
continue
}
i += 1
// Read from the start of the text with the session's grammar: a literal can open any distance back, and where it
// ends is the engine's rule.
static func isInsideProtectedContext(_ text: NSString, at position: Int, grammar: SQLLexicalGrammar) -> Bool {
let length = text.length
let runsExecutableComments = grammar.contains(.executableComments)
var index = 0
while index < position {
// The server runs the body of `/*! ... */`, so it is code.
if runsExecutableComments, let opener = SqlLexer.executableCommentOpenerLength(text, at: index, length: length) {
index += opener
continue
}

if ch == 0x5C && (inSingleQuote || inDoubleQuote) {
i += 2
guard let span = SQLNonCodeSpan.span(at: index, in: text, grammar: grammar) else {
index += 1
continue
}

switch ch {
case 0x27: if !inDoubleQuote && !inBacktick { inSingleQuote.toggle() }
case 0x22: if !inSingleQuote && !inBacktick { inDoubleQuote.toggle() }
case 0x60: if !inSingleQuote && !inDoubleQuote { inBacktick.toggle() }
case 0x23:
if !inSingleQuote && !inDoubleQuote && !inBacktick {
inLineComment = true
}
case 0x2D:
if !inSingleQuote && !inDoubleQuote && !inBacktick &&
i + 1 < position && text.character(at: i + 1) == 0x2D {
inLineComment = true
i += 2
continue
}
case 0x2F:
if !inSingleQuote && !inDoubleQuote && !inBacktick &&
i + 1 < position && text.character(at: i + 1) == 0x2A {
inBlockComment = true
i += 2
continue
}
case 0x24:
if !inSingleQuote && !inDoubleQuote && !inBacktick &&
i + 1 < position && text.character(at: i + 1) == 0x24 {
inDollarQuote.toggle()
i += 2
continue
}
default: break
}
i += 1
if SQLContextAnalyzer.holds(span, cursor: position) { return true }
index = max(span.end, index + 1)
}

return inSingleQuote || inDoubleQuote || inBacktick || inLineComment || inBlockComment || inDollarQuote
return false
}

/// Nil when the word is not a keyword, sits in a protected context, or is already in the target case.
// Nil when the word is not a keyword, sits in a comment, a literal or a quoted name, or is already in the target case.
public static func keywordBeforePosition(
_ text: NSString,
at position: Int,
uppercase: Bool = true
uppercase: Bool = true,
grammar: SQLLexicalGrammar
) -> (word: String, folded: String, range: NSRange)? {
var wordStart = position
while wordStart > 0 {
Expand All @@ -117,7 +58,7 @@ public enum KeywordUppercaseHelper {

let word = text.substring(with: NSRange(location: wordStart, length: wordLength))
guard SQLKeywords.keywordSet.contains(word.lowercased()) else { return nil }
guard !isInsideProtectedContext(text, at: wordStart) else { return nil }
guard !isInsideProtectedContext(text, at: wordStart, grammar: grammar) else { return nil }

let folded = uppercase ? word.uppercased() : word.lowercased()
guard folded != word else { return nil }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//
// DocumentPathPlacement.swift
// TableProDocumentPath
//
// Builds the partial document that sets one leaf of a stored JSON document.
//

import Foundation

public enum DocumentPathPlacement {
public struct InsideArray: Error, Equatable {
public let path: String
}

@discardableResult
public static func set(_ value: Any, atKeys keys: [String], in document: inout [String: Any]) -> Bool {
guard let key = keys.first else { return false }
guard keys.count > 1 else {
document[key] = value
return true
}
var child: [String: Any]
switch document[key] {
case nil:
child = [:]
case let existing as [String: Any]:
child = existing
default:
return false
}
guard set(value, atKeys: Array(keys.dropFirst()), in: &child) else { return false }
document[key] = child
return true
}

public static func value(in document: [String: Any], atKeys keys: [String]) -> Any? {
var current: Any = document
for key in keys {
guard let dictionary = current as? [String: Any], let next = dictionary[key] else { return nil }
current = next
}
return keys.isEmpty ? nil : current
}

// A stored document can spell one field as nested objects, as a literal dotted key, or as a
// mix, and a merge matches keys literally. The value goes to every spelling the document
// already holds; a field it does not hold yet is added under the deepest object that exists,
// split where `keys` splits.
public static func place(
_ value: Any,
atKeys keys: [String],
in partial: inout [String: Any],
following stored: [String: Any]
) throws {
var atoms: [String] = []
var boundaries: Set<Int> = []
for key in keys {
atoms.append(contentsOf: key.split(separator: ".", omittingEmptySubsequences: false).map(String.init))
boundaries.insert(atoms.count)
}
guard !atoms.isEmpty else { return }
let path = atoms.joined(separator: ".")

let spellings = try storedSpellings(of: atoms[...], in: stored, path: path)
guard spellings.isEmpty else {
for spelling in spellings {
set(value, atKeys: spelling, in: &partial)
}
return
}

var consumed: [String] = []
var start = 0
var level = stored
while let step = firstObject(in: level, atoms: atoms, from: start) {
consumed.append(step.key)
start = step.end
level = step.object
}

var group: [String] = []
for index in start..<atoms.count {
group.append(atoms[index])
if boundaries.contains(index + 1) {
consumed.append(group.joined(separator: "."))
group = []
}
}
set(value, atKeys: consumed, in: &partial)
}

private static func storedSpellings(
of atoms: ArraySlice<String>,
in level: [String: Any],
path: String
) throws -> [[String]] {
var spellings: [[String]] = []
for end in atoms.indices.map({ $0 + 1 }) {
let key = atoms[atoms.startIndex..<end].joined(separator: ".")
guard let found = level[key] else { continue }
if end == atoms.endIndex {
spellings.append([key])
} else if let object = found as? [String: Any] {
spellings += try storedSpellings(of: atoms[end...], in: object, path: path).map { [key] + $0 }
} else if let array = found as? [Any], array.contains(where: { $0 is [String: Any] }) {
throw InsideArray(path: path)
}
}
return spellings
}

private static func firstObject(
in level: [String: Any],
atoms: [String],
from start: Int
) -> (key: String, end: Int, object: [String: Any])? {
guard start < atoms.count - 1 else { return nil }
for end in (start + 1)..<atoms.count {
let key = atoms[start..<end].joined(separator: ".")
if let object = level[key] as? [String: Any] {
return (key, end, object)
}
}
return nil
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import Foundation

// Counts the rows of a result read chunk by chunk against a row limit. The limit is exceeded only once a row past it
// has been seen, so a result of exactly `limit` rows is whole.
public struct DuckDBFetchBudget: Sendable, Equatable {
public let limit: Int
public private(set) var kept = 0
public private(set) var isExceeded = false

public init(limit: Int) {
self.limit = max(0, limit)
}

public mutating func take(_ count: Int) -> Int {
let room = limit - kept
guard count > room else {
kept += max(0, count)
return max(0, count)
}
kept = limit
isExceeded = true
return room
}
}

// What happens to the rest of a result the reader stops in the middle of.
public enum DuckDBUnreadRows: Sendable, Equatable {
case drain
case abandon
case abandonWhenIdle

// Dropping an unfinished statement that may write aborts the transaction it runs in, so only a session known to
// hold none drops one.
public func abandons(sessionIsIdle: () -> Bool) -> Bool {
switch self {
case .drain: false
case .abandon: true
case .abandonWhenIdle: sessionIsIdle()
}
}
}

// The session settings that change how DuckDB spells a value as text, which a second connection rendering the first
// one's rows has to share.
public enum DuckDBRenderSettings {
public static let names = ["TimeZone", "Calendar"]

public static let readQuery =
"SELECT name, value FROM duckdb_settings() WHERE name IN ('TimeZone', 'Calendar')"

public static func assignments(from current: [String: String], to wanted: [String: String]) -> [String] {
names.compactMap { name in
guard let value = wanted[name], current[name] != value else { return nil }
return "SET \(name) = '\(value.replacingOccurrences(of: "'", with: "''", options: .literal))'"
}
}
}

// Hands a row batch to a stream whose buffer is bounded. A buffering policy alone drops what does not fit, so the
// producer keeps the refused batch and offers it again once the consumer has read one.
public enum DuckDBBoundedStream {
public static let bufferedBatches = 64

// Returns false when the stream has ended and the producer should stop.
public static func deliver<Element: Sendable>(
_ element: Element,
to continuation: AsyncThrowingStream<Element, Error>.Continuation,
pause: () -> Void = { _ = usleep(2_000) }
) -> Bool {
var pending = element
while true {
switch continuation.yield(pending) {
case .enqueued:
return true
case .dropped(let refused):
pending = refused
pause()
case .terminated:
return false
@unknown default:
return false
}
}
}
}
Loading
Loading