updates
This commit is contained in:
@@ -47,6 +47,7 @@ final class DatabaseModel: ObservableObject {
|
||||
@Published var result = QueryResult()
|
||||
@Published var databases: [String] = []
|
||||
@Published var schemaTables: [String: [String]] = [:]
|
||||
@Published var schemaViews: [String: [String]] = [:]
|
||||
@Published var selectedDatabase = ""
|
||||
@Published var isRunning = false
|
||||
@Published var status = "Not connected"
|
||||
@@ -74,40 +75,92 @@ final class DatabaseModel: ObservableObject {
|
||||
sql = SQLFormatter.uppercaseKeywords(sql)
|
||||
}
|
||||
|
||||
|
||||
func loadTables(database: String) {
|
||||
guard let profile = selected else { return }
|
||||
guard let profile = selected else {
|
||||
status = "Select a MySQL connection"
|
||||
return
|
||||
}
|
||||
|
||||
let password = PasswordStore.read(profile.id)
|
||||
let escaped = database.replacingOccurrences(of: "`", with: "``")
|
||||
let escaped = database.replacingOccurrences(
|
||||
of: "`",
|
||||
with: "``"
|
||||
)
|
||||
|
||||
status = "Loading tables from \(database)…"
|
||||
|
||||
Task {
|
||||
do {
|
||||
let parsed = try await NativeMySQLRunner.run(
|
||||
profile: profile,
|
||||
database: "",
|
||||
database: database,
|
||||
password: password,
|
||||
sql: "SHOW TABLES FROM `\\(escaped)`"
|
||||
sql: "SHOW FULL TABLES FROM `\(escaped)`"
|
||||
)
|
||||
|
||||
schemaTables[database] = parsed.rows.compactMap { $0.first }
|
||||
var tables: [String] = []
|
||||
var views: [String] = []
|
||||
|
||||
for row in parsed.rows {
|
||||
guard let name = row.first else {
|
||||
continue
|
||||
}
|
||||
|
||||
if row.count > 1 && row[1] == "VIEW" {
|
||||
views.append(name)
|
||||
} else {
|
||||
tables.append(name)
|
||||
}
|
||||
}
|
||||
|
||||
schemaTables[database] = tables
|
||||
schemaViews[database] = views
|
||||
|
||||
status = "Loaded \(tables.count) tables and \(views.count) views from \(database)"
|
||||
} catch {
|
||||
status = "Unable to load tables: \\(error.localizedDescription)"
|
||||
status = "Unable to load tables: \(error.localizedDescription)"
|
||||
result = QueryResult(message: error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func selectTable(database: String, table: String) {
|
||||
let db = database.replacingOccurrences(of: "`", with: "``")
|
||||
let tbl = table.replacingOccurrences(of: "`", with: "``")
|
||||
|
||||
sql = "SELECT * FROM `\\(db)`.`\\(tbl)` LIMIT 100;"
|
||||
|
||||
func selectTable(database: String, table: String) {
|
||||
let db = database.replacingOccurrences(
|
||||
of: "`",
|
||||
with: "``"
|
||||
)
|
||||
|
||||
let tbl = table.replacingOccurrences(
|
||||
of: "`",
|
||||
with: "``"
|
||||
)
|
||||
|
||||
selectedDatabase = database
|
||||
|
||||
let configuredLimit = UserDefaults.standard.integer(
|
||||
forKey: "preferences.defaultRowLimit"
|
||||
)
|
||||
|
||||
let rowLimit = configuredLimit > 0
|
||||
? min(configuredLimit, 100000)
|
||||
: 500
|
||||
|
||||
sql = "SELECT * FROM `\(db)`.`\(tbl)` LIMIT \(rowLimit);"
|
||||
|
||||
runSQL()
|
||||
}
|
||||
|
||||
func loadDatabases() { execute("SHOW DATABASES;", listingDatabases: true) }
|
||||
|
||||
func runSQL() {
|
||||
formatSQL()
|
||||
if UserDefaults.standard.object(
|
||||
forKey: "preferences.uppercaseKeywords"
|
||||
) as? Bool ?? true {
|
||||
formatSQL()
|
||||
}
|
||||
execute(sql, listingDatabases: false)
|
||||
}
|
||||
private func execute(_ statement: String, listingDatabases: Bool) {
|
||||
|
||||
Reference in New Issue
Block a user