adds schema view and schema generating command
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
import Foundation
|
||||
|
||||
/// The default placement of the cards: lanes by foreign-key depth, left to right.
|
||||
public enum SchemaLayout {
|
||||
/// Lane 0 holds tables that point at nothing; every other table sits one lane right of the deepest table it points at.
|
||||
///
|
||||
/// Self-references are ignored and a cycle is broken at the edge that closes it, visiting tables by name, so the result is deterministic.
|
||||
public static func lanes(_ model: SchemaModel) -> [String: Int] {
|
||||
var targets: [String: [String]] = [:]
|
||||
for edge in model.edges where edge.from != edge.to {
|
||||
targets[edge.from, default: []].append(edge.to)
|
||||
}
|
||||
var lane: [String: Int] = [:]
|
||||
var visiting = Set<String>()
|
||||
|
||||
func visit(_ name: String) -> Int? {
|
||||
if let known = lane[name] { return known }
|
||||
guard visiting.insert(name).inserted else { return nil }
|
||||
var deepest = -1
|
||||
for target in Set(targets[name] ?? []).sorted() {
|
||||
if let value = visit(target) { deepest = max(deepest, value) }
|
||||
}
|
||||
visiting.remove(name)
|
||||
lane[name] = deepest + 1
|
||||
return deepest + 1
|
||||
}
|
||||
|
||||
for table in model.tables.map(\.name).sorted() { _ = visit(table) }
|
||||
return lane
|
||||
}
|
||||
|
||||
/// Lanes 340px apart, cards 48px apart, each lane centred against the tallest.
|
||||
///
|
||||
/// Lane 0 is in name order; later lanes follow the average height of the tables they point at, to limit crossings.
|
||||
public static func defaultPositions(_ model: SchemaModel) -> [String: SchemaPoint] {
|
||||
let lanes = lanes(model)
|
||||
let tables = Dictionary(uniqueKeysWithValues: model.tables.map { ($0.name, $0) })
|
||||
let laneCount = (lanes.values.max() ?? -1) + 1
|
||||
var members = Array(repeating: [String](), count: laneCount)
|
||||
for (name, lane) in lanes { members[lane].append(name) }
|
||||
|
||||
var offset: [String: Double] = [:]
|
||||
var laneHeight = Array(repeating: 0.0, count: laneCount)
|
||||
// Card centre as a share of its lane's height, which compares across lanes of different heights.
|
||||
var relative: [String: Double] = [:]
|
||||
|
||||
for lane in 0..<laneCount {
|
||||
let ordered: [String]
|
||||
if lane == 0 {
|
||||
ordered = members[lane].sorted()
|
||||
} else {
|
||||
let keys = Dictionary(uniqueKeysWithValues: members[lane].map { name in
|
||||
let shares = model.edges
|
||||
.filter { $0.from == name && $0.to != name && (lanes[$0.to] ?? lane) < lane }
|
||||
.compactMap { relative[$0.to] }
|
||||
return (name, shares.isEmpty ? 0.5 : shares.reduce(0, +) / Double(shares.count))
|
||||
})
|
||||
ordered = members[lane].sorted { a, b in
|
||||
keys[a]! == keys[b]! ? a < b : keys[a]! < keys[b]!
|
||||
}
|
||||
}
|
||||
var y = 0.0
|
||||
var centres: [(String, Double)] = []
|
||||
for name in ordered {
|
||||
let height = tables[name]?.cardHeight ?? SchemaGeometry.cardHeight(columns: 0)
|
||||
offset[name] = y
|
||||
centres.append((name, y + height / 2))
|
||||
y += height + SchemaMetrics.cardGap
|
||||
}
|
||||
laneHeight[lane] = y
|
||||
for (name, centre) in centres { relative[name] = y > 0 ? centre / y : 0.5 }
|
||||
}
|
||||
|
||||
let tallest = laneHeight.max() ?? 0
|
||||
let origin = SchemaMetrics.layoutOrigin
|
||||
var positions: [String: SchemaPoint] = [:]
|
||||
for (name, lane) in lanes {
|
||||
let shift = ((tallest - laneHeight[lane]) / 2).rounded()
|
||||
positions[name] = SchemaPoint(x: origin.x + Double(lane) * SchemaMetrics.laneSpacing,
|
||||
y: origin.y + shift + (offset[name] ?? 0))
|
||||
}
|
||||
return positions
|
||||
}
|
||||
|
||||
/// Saved positions for the tables that still exist, default ones for the rest.
|
||||
public static func positions(_ model: SchemaModel, saved: [String: SchemaPoint]) -> [String: SchemaPoint] {
|
||||
var result = defaultPositions(model)
|
||||
for table in model.tables {
|
||||
if let point = saved[table.name] { result[table.name] = point }
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/// The UserDefaults key that holds a project's card positions.
|
||||
public static func defaultsKey(root: String) -> String { "helder.schemaLayout:" + root }
|
||||
}
|
||||
Reference in New Issue
Block a user