added typeaheead and introspetion
This commit is contained in:
@@ -194,6 +194,7 @@ final class DatabaseModel: ObservableObject {
|
||||
let database: String
|
||||
let loadedAt: Date
|
||||
let tables: [String]
|
||||
let views: [String]
|
||||
let columnsByTable: [String: [IntrospectedColumn]]
|
||||
}
|
||||
@Published private(set) var introspections: [String: SchemaIntrospection] = [:]
|
||||
@@ -218,13 +219,14 @@ final class DatabaseModel: ObservableObject {
|
||||
do {
|
||||
let tableResult = try await NativeMySQLRunner.run(
|
||||
profile: profile, database: database, password: password,
|
||||
sql: "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = '\(escaped)' AND TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME"
|
||||
sql: "SELECT TABLE_NAME, TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = '\(escaped)' ORDER BY TABLE_NAME"
|
||||
)
|
||||
let columnResult = try await NativeMySQLRunner.run(
|
||||
profile: profile, database: database, password: password,
|
||||
sql: "SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_KEY, COLUMN_DEFAULT, EXTRA FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '\(escaped)' ORDER BY TABLE_NAME, ORDINAL_POSITION"
|
||||
)
|
||||
let tables = tableResult.rows.compactMap { $0.first }
|
||||
let tables = tableResult.rows.compactMap { $0.count >= 2 && $0[1] == "BASE TABLE" ? $0[0] : nil }
|
||||
let views = tableResult.rows.compactMap { $0.count >= 2 && $0[1] == "VIEW" ? $0[0] : nil }
|
||||
var columns: [String: [IntrospectedColumn]] = [:]
|
||||
for row in columnResult.rows where row.count >= 7 {
|
||||
let column = IntrospectedColumn(
|
||||
@@ -238,13 +240,14 @@ final class DatabaseModel: ObservableObject {
|
||||
guard profiles.contains(where: { $0.id == profile.id }) else { return }
|
||||
introspections[key] = SchemaIntrospection(
|
||||
connectionID: profile.id, database: database,
|
||||
loadedAt: Date(), tables: tables, columnsByTable: columns
|
||||
loadedAt: Date(), tables: tables, views: views, columnsByTable: columns
|
||||
)
|
||||
schemaTables[database] = tables
|
||||
schemaViews[database] = views
|
||||
schemaColumns[database] = Array(Set(columns.values.flatMap { $0 }.flatMap {
|
||||
[$0.name, "\($0.table).\($0.name)"]
|
||||
})).sorted()
|
||||
status = "Introspection complete: \(tables.count) tables, \(columns.values.reduce(0) { $0 + $1.count }) columns in \(database) (RAM)"
|
||||
status = "Introspection complete: \(tables.count) tables, \(views.count) views, \(columns.values.reduce(0) { $0 + $1.count }) columns in \(database) (RAM)"
|
||||
} catch {
|
||||
status = "Introspection failed for \(database): \(error.localizedDescription)"
|
||||
}
|
||||
@@ -431,8 +434,13 @@ final class DatabaseModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
|
||||
schemaTables[database] = tables
|
||||
schemaViews[database] = views
|
||||
if let snapshot = introspection(for: database) {
|
||||
schemaTables[database] = snapshot.tables
|
||||
schemaViews[database] = snapshot.views
|
||||
} else {
|
||||
schemaTables[database] = tables
|
||||
schemaViews[database] = views
|
||||
}
|
||||
|
||||
// Routine metadata is separate from SHOW FULL TABLES.
|
||||
// Failure (e.g. insufficient INFORMATION_SCHEMA privileges)
|
||||
@@ -470,9 +478,9 @@ final class DatabaseModel: ObservableObject {
|
||||
names.insert(row[1])
|
||||
names.insert("\(row[0]).\(row[1])")
|
||||
}
|
||||
schemaColumns[database] = names.sorted()
|
||||
if introspection(for: database) == nil { schemaColumns[database] = names.sorted() }
|
||||
} catch {
|
||||
schemaColumns[database] = []
|
||||
if introspection(for: database) == nil { schemaColumns[database] = [] }
|
||||
}
|
||||
|
||||
status = "Loaded \(tables.count) tables and \(views.count) views from \(database)"
|
||||
|
||||
Reference in New Issue
Block a user