diff --git a/MongoView/Base.lproj/MainMenu.xib b/MongoView/Base.lproj/MainMenu.xib index 0a18bad..b074263 100644 --- a/MongoView/Base.lproj/MainMenu.xib +++ b/MongoView/Base.lproj/MainMenu.xib @@ -1,7 +1,7 @@ - + - + @@ -107,7 +107,8 @@ - + + @@ -362,6 +363,11 @@ + + + + + DQ diff --git a/MongoView/MongoController.swift b/MongoView/MongoController.swift index 01e7bba..8c0d084 100644 --- a/MongoView/MongoController.swift +++ b/MongoView/MongoController.swift @@ -33,5 +33,13 @@ class MongoController { client = try! MongoClient(connectionString, using: group) } + + func db(for collection: DatabaseCollection) -> MongoDatabase { + return client.db(collection.database) + } + + func collection(_ collection: DatabaseCollection) -> MongoCollection { + return db(for: collection).collection(collection.name) + } } diff --git a/MongoView/View Controllers/DatabaseViewController.swift b/MongoView/View Controllers/DatabaseViewController.swift index 9d4f7d5..2c0c3bc 100644 --- a/MongoView/View Controllers/DatabaseViewController.swift +++ b/MongoView/View Controllers/DatabaseViewController.swift @@ -150,10 +150,11 @@ class DatabaseViewController: NSViewController { } @IBAction func runQuery(_ sender: Any) { - guard let queryViewController = queryViewController else { - return - } - queryViewController.runQuery() + queryViewController?.runQuery() + } + + @IBAction func refresh(_ sender: Any) { + queryViewController?.refresh() } } diff --git a/MongoView/View Controllers/QueryViewController.swift b/MongoView/View Controllers/QueryViewController.swift index fac90ce..1966a8e 100644 --- a/MongoView/View Controllers/QueryViewController.swift +++ b/MongoView/View Controllers/QueryViewController.swift @@ -27,6 +27,8 @@ class QueryViewController: NSViewController { return queryTextView.string != defaultQuery } + var mostRecentQuery: String? = nil + var rootNodes: [Node] = [] init(mongoController: MongoController, collection: DatabaseCollection) { @@ -43,11 +45,8 @@ class QueryViewController: NSViewController { override func viewDidLoad() { super.viewDidLoad() - let db = mongoController.client.db(collection.database) - let collection = db.collection(self.collection.name) - let documents = try! collection.find().all() - rootNodes = documents.map { Node(document: $0) } - + refresh() + verticalSplitView.delegate = self verticalSplitView.setHoldingPriority(.defaultHigh, forSubviewAt: 0) @@ -61,9 +60,6 @@ class QueryViewController: NSViewController { outlineView.target = self outlineView.doubleAction = #selector(outlineCellDoubleClicked) - title = "\(self.collection.database).\(self.collection.name)" - - documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")" } override func viewWillAppear() { @@ -80,9 +76,32 @@ class QueryViewController: NSViewController { view.window!.makeFirstResponder(outlineView) } + func refresh(reload: Bool = true) { + if let query = mostRecentQuery { + let connStr = "\(mongoController.connectionString)/\(collection.database)" + + rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map { + Node(value: $0) + } + + title = query + documentCountLabel.stringValue = "\(rootNodes.count) result\(rootNodes.count == 1 ? "" : "s")" + } else { + let documents = try! mongoController.collection(collection).find().all() + rootNodes = documents.map { Node(document: $0) } + + title = "\(self.collection.database).\(self.collection.name)" + documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")" + } + if reload { + outlineView.reloadData() + } + } + func runQuery() { - let query = queryTextView.string - let connStr = "\(mongoController.connectionString)/\(collection.database)" + mostRecentQuery = queryTextView.string + refresh() + } rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map { Node(value: $0) @@ -102,6 +121,15 @@ class QueryViewController: NSViewController { } +extension QueryViewController: NSMenuItemValidation { + func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { + if menuItem.action == #selector(deleteNode(_:)) { + return outlineView.clickedRow != -1 + } + return true + } +} + extension QueryViewController: NSSplitViewDelegate { func splitView(_ splitView: NSSplitView, constrainSplitPosition proposedPosition: CGFloat, ofSubviewAt dividerIndex: Int) -> CGFloat { return max(80, min(splitView.bounds.height / 2, proposedPosition))