Browse Source

Add query evaluation

master
Shadowfacts 5 months ago
parent
commit
8104853f13
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5

+ 4
- 0
MongoView.xcodeproj/project.pbxproj View File

@@ -40,6 +40,7 @@
40 40
 		D63CDF4123C839010012D658 /* QueryViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63CDF3F23C839010012D658 /* QueryViewController.xib */; };
41 41
 		D63CDF4423C970C50012D658 /* DatabaseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDF4223C970C50012D658 /* DatabaseViewController.swift */; };
42 42
 		D63CDF4523C970C50012D658 /* DatabaseViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63CDF4323C970C50012D658 /* DatabaseViewController.xib */; };
43
+		D6D4665323CB730C00F13B1B /* MongoEvaluator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D4665223CB730C00F13B1B /* MongoEvaluator.swift */; };
43 44
 /* End PBXBuildFile section */
44 45
 
45 46
 /* Begin PBXCopyFilesBuildPhase section */
@@ -92,6 +93,7 @@
92 93
 		D63CDF3F23C839010012D658 /* QueryViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = QueryViewController.xib; sourceTree = "<group>"; };
93 94
 		D63CDF4223C970C50012D658 /* DatabaseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatabaseViewController.swift; sourceTree = "<group>"; };
94 95
 		D63CDF4323C970C50012D658 /* DatabaseViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = DatabaseViewController.xib; sourceTree = "<group>"; };
96
+		D6D4665223CB730C00F13B1B /* MongoEvaluator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MongoEvaluator.swift; sourceTree = "<group>"; };
95 97
 /* End PBXFileReference section */
96 98
 
97 99
 /* Begin PBXFrameworksBuildPhase section */
@@ -161,6 +163,7 @@
161 163
 				D63CDEBD23C837DC0012D658 /* AppDelegate.swift */,
162 164
 				D63CDF3423C838190012D658 /* MongoController.swift */,
163 165
 				D63CDF3323C838190012D658 /* Node.swift */,
166
+				D6D4665223CB730C00F13B1B /* MongoEvaluator.swift */,
164 167
 				D60C863B23CA2DD600C9DB8E /* Windows */,
165 168
 				D60C863C23CA2DDD00C9DB8E /* View Controllers */,
166 169
 				D63CDEBF23C837DD0012D658 /* Assets.xcassets */,
@@ -264,6 +267,7 @@
264 267
 			buildActionMask = 2147483647;
265 268
 			files = (
266 269
 				D63CDEBE23C837DC0012D658 /* AppDelegate.swift in Sources */,
270
+				D6D4665323CB730C00F13B1B /* MongoEvaluator.swift in Sources */,
267 271
 				D63CDF3823C8381A0012D658 /* MongoController.swift in Sources */,
268 272
 				D60C863923CA2DD100C9DB8E /* ServerConnectWindowController.swift in Sources */,
269 273
 				D63CDF3723C8381A0012D658 /* Node.swift in Sources */,

+ 84
- 0
MongoView/MongoEvaluator.swift View File

@@ -0,0 +1,84 @@
1
+//
2
+//  MongoEvaluator.swift
3
+//  MongoView
4
+//
5
+//  Created by Shadowfacts on 1/12/20.
6
+//  Copyright © 2020 Shadowfacts. All rights reserved.
7
+//
8
+
9
+import Foundation
10
+import JavaScriptCore
11
+import MongoSwift
12
+
13
+struct MongoEvaluator {
14
+    
15
+    static let context: JSContext = {
16
+        let context = JSContext()!
17
+        let date: @convention(block) (JSValue) -> Dictionary<String, Any> = { (input) in
18
+            ["$date": input]
19
+        }
20
+        let functions: [NSString: @convention(block) (JSValue) -> Dictionary<String, Any>] = [
21
+            "ObjectId": { (input) in
22
+                ["$oid": input]
23
+            },
24
+            "NumberLong": { (input) in
25
+                ["$numberLong": input]
26
+            },
27
+            "NumberInt": { (input) in
28
+                ["$numberInt": input]
29
+            },
30
+            "NumberDecimal": { (input) in
31
+                ["$numberDecimal": input]
32
+            },
33
+            "Date": date,
34
+            "ISODate": date,
35
+        ]
36
+        functions.forEach { (key, value) in
37
+            context.setObject(value, forKeyedSubscript: key)
38
+        }
39
+        let dbRef: @convention(block) (String, String) -> Dictionary<String, Any> = { (ref, id) in
40
+            ["$ref": ref, "$id": ["$oid": id]]
41
+        }
42
+        context.setObject(dbRef, forKeyedSubscript: "DBRef" as NSString)
43
+        return context
44
+    }()
45
+
46
+    static func parse(shellJSON: String) -> Document? {
47
+        guard !shellJSON.isEmpty,
48
+            let result = context.evaluateScript("JSON.stringify(\(shellJSON))") else {
49
+                return nil
50
+        }
51
+        do {
52
+            return try Document(fromJSON: result.toString())
53
+        } catch {
54
+            print("Unable to parse Document: \(error)")
55
+            return nil
56
+        }
57
+    }
58
+    
59
+    static func eval(command: String, connectingTo connStr: String) -> [Document] {
60
+        print("Running command \"\(command)\" against \(connStr)")
61
+        
62
+        let mongoOutputPipe = Pipe()
63
+
64
+        let mongoProcess = Process()
65
+        mongoProcess.launchPath = "/usr/local/bin/mongo"
66
+        mongoProcess.arguments = [connStr, "--quiet", "--norc", "--eval", command]
67
+        mongoProcess.standardOutput = mongoOutputPipe
68
+        mongoProcess.launch()
69
+        
70
+        let mongoOutputHandle = mongoOutputPipe.fileHandleForReading
71
+        
72
+        var strs = [String]()
73
+        var data: Data!
74
+        repeat {
75
+            data = mongoOutputHandle.availableData
76
+            strs.append(String(data: data, encoding: .utf8)!)
77
+        } while (data.count > 0)
78
+        mongoOutputHandle.closeFile()
79
+        
80
+        let lines = strs.joined(separator: "").components(separatedBy: "\n")
81
+        return lines.compactMap(parse(shellJSON:))
82
+    }
83
+    
84
+}

+ 1
- 8
MongoView/MongoView.entitlements View File

@@ -1,12 +1,5 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3 3
 <plist version="1.0">
4
-<dict>
5
-	<key>com.apple.security.app-sandbox</key>
6
-	<true/>
7
-	<key>com.apple.security.files.user-selected.read-only</key>
8
-	<true/>
9
-	<key>com.apple.security.network.client</key>
10
-	<true/>
11
-</dict>
4
+<dict/>
12 5
 </plist>

+ 7
- 4
MongoView/View Controllers/QueryViewController.swift View File

@@ -81,10 +81,13 @@ class QueryViewController: NSViewController {
81 81
     }
82 82
     
83 83
     func runQuery() {
84
-        print("run query: \(queryTextView.string)")
85
-//        mongoController.db.runCommand(["eval": .code(Code(code: queryTextView.string))]).whenComplete { (res) in
86
-//            print(res)
87
-//        }
84
+        let query = queryTextView.string
85
+        let connStr = "\(mongoController.connectionString)/\(collection.database)"
86
+
87
+        rootNodes = MongoEvaluator.eval(command: query, connectingTo: connStr).map {
88
+            Node(document: $0)
89
+        }
90
+        outlineView.reloadData()
88 91
     }
89 92
     
90 93
     @objc func outlineCellDoubleClicked() {

Loading…
Cancel
Save