Skip to content

Commit

Permalink
08-23 snapshot support
Browse files Browse the repository at this point in the history
  • Loading branch information
GWlodarek committed Aug 24, 2016
1 parent 754aa87 commit 9aa17be
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 118 deletions.
15 changes: 6 additions & 9 deletions Build/linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,22 @@ docker pull ubuntu:wily

# Install dependencies
update="apt-get update"
install_primary="apt-get install -y clang unixodbc-dev unzip wget tar git sudo"
install_primary="apt-get install -y clang unixodbc-dev unzip wget tar git sudo uuid-dev autoconf libtool pkg-config systemtap-sdt-dev libblocksruntime-dev libkqueue-dev libbsd-dev"

# Install the IBM DB2 CLI
install_cli="wget https://github.com/IBM-DTeam/swift-for-db2-cli/archive/master.zip && unzip master.zip && cd swift-for-db2-cli-master && sudo ./cli.sh && . env.sh && cd .. && rm -f master.zip && rm -rf swift-for-db2-cli-master"

# Get the needed Swift snapshot
get_swift="wget https://swift.org/builds/development/ubuntu1510/swift-DEVELOPMENT-SNAPSHOT-2016-06-20-a/swift-DEVELOPMENT-SNAPSHOT-2016-06-20-a-ubuntu15.10.tar.gz"
open_swift="tar -xvzf swift-DEVELOPMENT-SNAPSHOT-2016-06-20-a-ubuntu15.10.tar.gz"
get_swift="wget https://swift.org/builds/development/ubuntu1510/swift-DEVELOPMENT-SNAPSHOT-2016-08-23-a/swift-DEVELOPMENT-SNAPSHOT-2016-08-23-a-ubuntu15.10.tar.gz"
open_swift="tar -xvzf swift-DEVELOPMENT-SNAPSHOT-2016-08-23-a-ubuntu15.10.tar.gz"
mkdir_swift="mkdir -p /home/root/swift"
cp_swift="cp -r swift-DEVELOPMENT-SNAPSHOT-2016-06-20-a-ubuntu15.10/* /home/root/swift/"
cp_swift="cp -r swift-DEVELOPMENT-SNAPSHOT-2016-08-23-a-ubuntu15.10/* /home/root/swift/"
export_path="export PATH=/home/root/swift/usr/bin:$PATH"

# DB2 database used for testing
export_db="export DB2_CONN_STRING=\"DRIVER={DB2};DATABASE=BLUDB;UID=dash6435;PWD=0NKUFZxcskVZ;HOSTNAME=dashdb-entry-yp-dal09-09.services.dal.bluemix.net;PORT=50000\""

# Dependencies for the dispatch library
install_secondary="apt-get install -y autoconf libtool pkg-config systemtap-sdt-dev libblocksruntime-dev libkqueue-dev libbsd-dev"
ldispatch="git clone -b experimental/foundation https://github.com/apple/swift-corelibs-libdispatch.git && cd swift-corelibs-libdispatch && git submodule init && git submodule update && sh ./autogen.sh && ./configure --with-swift-toolchain=/home/root/swift/usr --prefix=/home/root/swift/usr && make && make install"
# Build the project and test it
build_and_test="cd /swift-for-db2 && swift build -Xcc -fblocks -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib && swift test"
build_and_test="cd /swift-for-db2 && swift build && swift test -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib"

docker run -v ${TRAVIS_BUILD_DIR}:/swift-for-db2 -i -t ubuntu:wily /bin/bash -c "${update} && ${install_primary} && ${install_cli} && ${get_swift} && ${open_swift} && ${mkdir_swift} && ${cp_swift} && ${export_path} && ${export_db} && ${install_secondary} && ${ldispatch} && ${build_and_test}"
docker run -v ${TRAVIS_BUILD_DIR}:/swift-for-db2 -i -t ubuntu:wily /bin/bash -c "${update} && ${install_primary} && ${install_cli} && ${get_swift} && ${open_swift} && ${mkdir_swift} && ${cp_swift} && ${export_path} && ${export_db} && ${build_and_test}"
8 changes: 4 additions & 4 deletions Build/osx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ brew install wget unixodbc
wget https://github.com/IBM-DTeam/swift-for-db2-cli/archive/master.zip && unzip master.zip && cd swift-for-db2-cli-master && sudo ./cli.sh && . env.sh && cd .. && rm -f master.zip && rm -rf swift-for-db2-cli-master

# Get the needed Swift snapshot
export SWIFT_VERSION=swift-DEVELOPMENT-SNAPSHOT-2016-06-20-a
wget https://swift.org/builds/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2016-06-20-a/swift-DEVELOPMENT-SNAPSHOT-2016-06-20-a-osx.pkg
sudo installer -pkg swift-DEVELOPMENT-SNAPSHOT-2016-06-20-a-osx.pkg -target /
export SWIFT_VERSION=swift-DEVELOPMENT-SNAPSHOT-2016-08-23-a
wget https://swift.org/builds/development/xcode/swift-DEVELOPMENT-SNAPSHOT-2016-08-23-a/swift-DEVELOPMENT-SNAPSHOT-2016-08-23-a-osx.pkg
sudo installer -pkg swift-DEVELOPMENT-SNAPSHOT-2016-08-23-a-osx.pkg -target /
export TOOLCHAINS=swift

# DB2 database used for testing
export DB2_CONN_STRING="DRIVER={DB2};DATABASE=BLUDB;UID=dash6435;PWD=0NKUFZxcskVZ;HOSTNAME=dashdb-entry-yp-dal09-09.services.dal.bluemix.net;PORT=50000"

# Build the project and test it
cd ${TRAVIS_BUILD_DIR} && swift build -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib && swift test
cd ${TRAVIS_BUILD_DIR} && swift build && swift test -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib
18 changes: 4 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The Swift SDK for DB2 allows you to connect to your IBM DB2 database or products
- Query your database asynchronously or synchronously.

## Swift Version
The latest version of the Swift SDK for DB2 works with the DEVELOPMENT-SNAPSHOT-2016-06-20-a version of the Swift binaries. You can download this version of the Swift binaries by following this [link](https://swift.org/download/). Compatibility with other Swift versions is not guaranteed.
The latest version of the Swift SDK for DB2 works with the DEVELOPMENT-SNAPSHOT-2016-08-23-a version of the Swift binaries. You can download this version of the Swift binaries by following this [link](https://swift.org/download/). Compatibility with other Swift versions is not guaranteed.

## Installation (OS X)

Expand Down Expand Up @@ -72,11 +72,7 @@ The latest version of the Swift SDK for DB2 works with the DEVELOPMENT-SNAPSHOT-

Follow the instructions provided on that page. After installing it (i.e. uncompressing the tar file), make sure you update your PATH environment variable so that it includes the extracted tools: `export PATH=/<path to uncompress tar contents>/usr/bin:$PATH`. To update the PATH env variable, you can update your [.bashrc file](http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html).

5. Clone, build and install the libdispatch library.
The complete instructions for building and installing this library are [here](https://github.com/apple/swift-corelibs-libdispatch/blob/experimental/foundation/INSTALL), though, all you need to do is just this
`git clone -b experimental/foundation https://github.com/apple/swift-corelibs-libdispatch.git && cd swift-corelibs-libdispatch && git submodule init && git submodule update && sh ./autogen.sh && ./configure --with-swift-toolchain=<path-to-swift>/usr --prefix=<path-to-swift>/usr && make && make install`

6. Now you are ready to use the Swift SDK for DB2. See [Using Swift SDK for DB2](#using-swift-sdk-for-db2).
5. Now you are ready to use the Swift SDK for DB2. See [Using Swift SDK for DB2](#using-swift-sdk-for-db2).


## Using Swift SDK for DB2
Expand All @@ -100,14 +96,8 @@ let package = Package(

2.Then, run the following command in terminal (in your project folder)

### OS X
```
swift build -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib
```

### Linux
```
swift build -Xcc -fblocks -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib
swift build
```

3.Wait until the build finishes, then run the project
Expand All @@ -120,7 +110,7 @@ Visit the [Wiki](https://github.com/IBM-DTeam/swift-for-db2/wiki) for examples o

## Contributing
1. Clone this repository, `git clone https://github.com/IBM-DTeam/swift-for-db2`
2. Build and run tests
2. Build and run tests with `swift test -Xcc -I/usr/local/include -Xlinker -L/usr/local/lib`

### Notes
* You are required to set the environment variable DB2_CONN_STRING to your database connection string.
Expand Down
79 changes: 19 additions & 60 deletions Sources/IBMDB/SwiftDB2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ import Dispatch
import IBMCliDarwin
#endif

// Custom named queues
#if os(Linux)
let queue = dispatch_queue_create("swift-for-db2", DISPATCH_QUEUE_CONCURRENT)
#else
let queue = DispatchQueue(label: "swift-for-db2", attributes: .concurrent)
#endif
// Custom named queue
let queue = DispatchQueue(label: "swift-for-db2", attributes: Dispatch.DispatchQueue.Attributes.concurrent)


public typealias DB2Dictionary = [String : String]

#if os(OSX)
public typealias DB2Dictionary = [String : AnyObject]
#else
public typealias DB2Dictionary = [String : Any]
#endif

/**
* IBMDB Class
Expand All @@ -58,14 +52,14 @@ public class IBMDB {
/**
* Async method to connect to the database using the connection string 'info'
*/
public func connect(info: String, callback: (error: [DBError]?, connection: Connection?) -> Void) -> Void {
public func connect(info: String, callback: ([DBError]?, Connection?) -> Void) -> Void {

var conn: UnsafeMutablePointer<ODBC>!

func run() -> Void {
queue.sync {
// Try to connect to the database.
conn = info.withCString { cString in
db_connect(conn, UnsafeMutablePointer(cString))
db_connect(conn, UnsafeMutablePointer(mutating: cString))
}

if !connection_error_found(conn) {
Expand All @@ -74,7 +68,7 @@ public class IBMDB {
// Create a Connection object with conn to use in the callback.
let db = Connection(conn: conn)

callback(error: nil, connection: db)
callback(nil, db)
} else {
// Error! Disconnect and callback with an error.
var error_arr = [DBError]()
Expand All @@ -89,21 +83,10 @@ public class IBMDB {
} while (connection_error_found(conn))

db_disconnect(conn)
callback(error: error_arr, connection: nil)
callback(error_arr, nil)
}
}

#if os(Linux)
dispatch_async(queue) {
run()
}
#else
queue.sync {
run()
}
#endif


}

/**
Expand All @@ -115,7 +98,7 @@ public class IBMDB {

// Try to connect to the database.
conn = info.withCString { cString in
db_connect(conn, UnsafeMutablePointer(cString))
db_connect(conn, UnsafeMutablePointer(mutating: cString))
}

if !connection_error_found(conn) {
Expand Down Expand Up @@ -188,34 +171,25 @@ public class Connection {
* Runs a query asynchronously against the database.
* Executes the callback upon completion.
*/
public func query(query:String, callback: (result: [[DB2Dictionary]], error: [DBError]?) -> Void) -> Void {
public func query(query:String, callback: ([[DB2Dictionary]], [DBError]?) -> Void) -> Void {

func run() -> Void {
queue.sync {
var data = [[DB2Dictionary]]()

var response = self.data_fetch(query: query, data: &data)

if response.success {
callback(result: data, error: nil)
callback(data, nil)
} else {
data.removeAll()
if sql_error_found(response.result) {
callback(result: data, error: self.error(Error: DBErrorType().Fetch, result: &response.result!))
callback(data, self.error(Error: DBErrorType().Fetch, result: &response.result!))
} else {
callback(result: data, error: nil)
callback(data, nil)
}
}
}

#if os(Linux)
dispatch_async(queue) {
run()
}
#else
queue.sync {
run()
}
#endif
}


Expand Down Expand Up @@ -248,7 +222,7 @@ public class Connection {
private func data_fetch(query: String, data: inout [[DB2Dictionary]]) -> (success: Bool, result: UnsafeMutablePointer<TABLE_RESULT>?) {

var result = query.withCString { cString in
table_fetch(self.conn, UnsafeMutablePointer(cString))
table_fetch(self.conn, UnsafeMutablePointer(mutating: cString))
}

if sql_error_found(result) {
Expand Down Expand Up @@ -292,11 +266,7 @@ public class Connection {
}
switch Int(col_data_type_fetch(Int32(col), result)) {
default:
#if os(Linux)
let dict:DB2Dictionary = ["\(col_name)": String(value).bridge()]
#else
let dict:DB2Dictionary = ["\(col_name)" : String(value) as AnyObject]
#endif
let dict = ["\(col_name)": String(value)!]

return dict
}
Expand Down Expand Up @@ -349,24 +319,13 @@ public class Connection {
* Disconnect asynchronously from the database associated with this Connection object.
*/
public func disconnect(callback: () -> Void) -> Void {
func run() -> Void {
queue.sync {
if self.conn != nil {
db_disconnect(self.conn)
self.conn = nil
}
callback()
}

#if os(Linux)
dispatch_async(queue) {
run()
}
#else
queue.sync {
run()
}
#endif

}


Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ class ConnectTests : XCTestCase {
XCTFail("Environment Variable DB2_CONN_STRING not set.")
}

let expectation = self.expectation(withDescription: "Attempts to connect to the database and runs the callback closure")
let expectation = self.expectation(description: "Attempts to connect to the database and runs the callback closure")

db.connect(info: connStringValid!) { (error, connection) -> Void in
XCTAssertNil(error, "error is Nil")
XCTAssertNotNil(connection, "connection is not Nil")
expectation.fulfill()
}

self.waitForExpectations(withTimeout: 600) { error in
self.waitForExpectations(timeout: 600) { error in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
Expand All @@ -68,15 +68,15 @@ class ConnectTests : XCTestCase {

func testConnectInvalidConfig() {

let expectation = self.expectation(withDescription: "Attempts to connect to the database and runs the callback closure")
let expectation = self.expectation(description: "Attempts to connect to the database and runs the callback closure")

db.connect(info: connStringInvalid) { (error, connection) -> Void in
XCTAssertNotNil(error, "error is not Nil")
XCTAssertNil(connection, "connection is Nil")
expectation.fulfill()
}

self.waitForExpectations(withTimeout: 600) { error in
self.waitForExpectations(timeout: 600) { error in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ class DisconnectTests : XCTestCase {
XCTFail("Cannot establish a connection to the database.")
}

let expectation = self.expectation(withDescription: "Attempts to connect and disconnect from the database and runs the callback closure")
let expectation = self.expectation(description: "Attempts to connect and disconnect from the database and runs the callback closure")

conn!.disconnect() { () -> Void in
XCTAssertNotNil(conn!.info().error, "Cannot fetch database info.")
expectation.fulfill()
}

self.waitForExpectations(withTimeout: 600) { error in
self.waitForExpectations(timeout: 600) { error in
if let error = error {
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
}
Expand Down
File renamed without changes.
Loading

0 comments on commit 9aa17be

Please sign in to comment.