-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
152 lines (119 loc) · 5.1 KB
/
AppDelegate.swift
File metadata and controls
152 lines (119 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// AppDelegate.swift
// SwiftJava
//
// Created by John Holdsworth on 13/07/2016.
// Copyright (c) 2016 John Holdsworth. All rights reserved.
//
import Cocoa
import java_lang
import java_sql
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var window: NSWindow!
@IBOutlet weak var tableView: NSTableView!
@IBOutlet weak var classPath: NSTextField!
@IBOutlet weak var driverClass: NSTextField!
@IBOutlet weak var connectionURL: NSTextField!
@IBOutlet weak var username: NSTextField!
@IBOutlet weak var password: NSTextField!
@IBOutlet weak var SQL: NSTextField!
class Model {
var columnNames = [String]()
var indexByColumn = [NSTableColumn:Int]()
var data = [[String]]()
}
var model: Model!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
tableView.rowHeight = 22
}
func alert( _ msg: String ) {
DispatchQueue.main.async {
print( "Alert: \(msg)" )
let alert = NSAlert()
alert.messageText = "JDBC Example"
alert.informativeText = msg
alert.runModal()
}
}
@IBAction func fetch( sender: AnyObject ) {
let home = String( cString: getenv("HOME") )
let classPath = self.classPath.stringValue.replacingOccurrences( of: "~", with: home )
classPath.withCString {
_ = setenv( "CLASSPATH", $0, 1 )
}
if model == nil && !JNI.initJVM() {
alert( "Could not initialise JVM" )
return
}
// Use Thread class to run in background to get the correct class loader & classpath
JavaThread( {
do {
if try JavaClass.forName( self.driverClass.stringValue ) == nil {
return self.alert( "Could not load driver class" );
}
// let cl = Object().getClass().getClassLoader()
// _ = try cl?.loadClass( self.driverClass.stringValue ).newInstance()
guard let connection = try DriverManager.getConnection( self.connectionURL.stringValue,
self.username.stringValue, self.password.stringValue )
else { return self.alert( "Unable to connect using information supplied, consult console" ) }
let statement = try connection.createStatement()!
if !(try statement.execute( sql: self.SQL.stringValue )) {
return self.alert( "Could not execute SQL: \(self.SQL.stringValue)" );
}
let model = Model()
repeat {
if let result = try statement.getResultSet(), let md = try result.getMetaData() {
let ncols = try md.getColumnCount()
for i in 1...ncols {
model.columnNames.append( try md.getColumnName(column: i) ?? "Column \(i)" )
}
while try result.next() {
var row = [String]()
for i in 1...ncols {
row.append( try result.getString( columnIndex: i ) ?? "null" )
}
model.data.append( row )
}
}
} while try statement.getMoreResults()
try connection.close()
DispatchQueue.main.async {
for column in self.tableView.tableColumns {
self.tableView.removeTableColumn( column )
}
self.model = model
self.tableView.noteNumberOfRowsChanged()
for i in 0..<model.columnNames.count {
let columnName = model.columnNames[i]
let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: columnName))
column.title = columnName
model.indexByColumn[column] = i
self.tableView.addTableColumn(column)
}
self.tableView.reloadData()
System.gc()
}
}
catch let e as Exception {
self.alert( "\(e.className): \(e.getMessage()!)" )
e.printStackTrace()
}
catch {
self.alert("Some other error")
}
} ).start()
}
func numberOfRows(in tableView: NSTableView) -> Int {
return model?.data.count ?? 0
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let view = NSTextField()
view.stringValue = model.data[row][model.indexByColumn[tableColumn!]!]
return view
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}