{"action":"created","comment":{"url":"https://api.github.com/repos/mathcamp/lightbase/pulls/comments/70009062","id":70009062,"diff_hunk":"@@ -9,24 +9,460 @@\n import Foundation\n import SQLite\n \n-class SQLBlah {\n+public struct DirectCursor: LazySequenceType, GeneratorType {\n+  public typealias Element = NSDictionary // or whatever\n   \n-  init() {\n+  private var statement: COpaquePointer\n+  \n+  init(statement: COpaquePointer) {\n+    self.statement = statement\n+  }\n+  \n+  public mutating func next() -> NSDictionary? {\n+    let rc: Int32 = sqlite3_step(statement)\n+    if (SQLITE_DONE == rc || SQLITE_ROW == rc) {\n+      let num_cols: Int32 = sqlite3_data_count(statement)\n+      \n+      if (num_cols > 0) {\n+        var dict = NSMutableDictionary()\n+        \n+        let columnCount = sqlite3_column_count(statement);\n+        \n+        for colIdx in 0..<columnCount {\n+          let columnName: String = String.fromCString(sqlite3_column_name(statement, colIdx))!\n+          print(columnName)\n+          let objectValue = self.objectForColumnIndex(Int(colIdx));\n+          dict.setValue(objectValue, forKeyPath: columnName)\n+        }\n+        \n+        return dict;\n+      } else {\n+        NSLog(\"Warning: There seem to be no columns in this set.\");\n+        return nil;\n+      }\n+      \n+    } else {\n+      return nil\n+    }\n+  }\n+  \n+  func objectForColumnIndex(colIndex: Int) -> AnyObject? {\n+    let colIdx = Int32(colIndex)\n+    let columnType = Int32(sqlite3_column_type(statement, colIdx));\n+    \n+    let returnValue: AnyObject;\n     \n+    if (columnType == SQLITE_INTEGER) {\n+      returnValue = NSNumber(longLong: sqlite3_column_int64(statement, colIdx))\n+    } else if (columnType == SQLITE_FLOAT) {\n+      returnValue = NSNumber(double: sqlite3_column_double(statement, colIdx))\n+    } else if (columnType == SQLITE_BLOB) {\n+      \n+      let bytes = sqlite3_column_blob(statement, colIdx)\n+      let length = Int(sqlite3_column_bytes(statement, colIdx))\n+      returnValue = Blob(bytes: bytes, length: length)\n+    } else {\n+      //default to a string for everything else\n+      returnValue = String.fromCString(UnsafePointer<CChar>(sqlite3_column_text(statement, colIdx)))!\n+    }\n+    \n+    return returnValue;\n   }\n   \n-  func openDatabase() -> COpaquePointer? {\n+}\n+\n+\n+\n+public class DirectDB: AbstractDB {\n+  \n+  public typealias Cursor = DirectCursor\n+  \n+  //private let dbPath = \"DirectDB\"\n+  \n+  internal let dbPointer: COpaquePointer?\n+  private var inTransaction: Bool = false\n+  private var isExecutingStatement = false\n+  private let loggingErrors = true\n+  private var errorMessage: String = \"\"\n+  \n+  let SQLITE_TRANSIENT = unsafeBitCast(-1, sqlite3_destructor_type.self)\n+  \n+  public init(dbPath: String) {\n     var db: COpaquePointer = nil\n-    let path = \"foo\"\n-    if sqlite3_open(path, &db) == SQLITE_OK {\n-      print(\"Successfully opened connection to database at \\(path)\")\n-      return db\n+    if sqlite3_open(dbPath, &db) == SQLITE_OK {\n+      self.dbPointer = db\n+      print(\"Database opened!!\")\n     } else {\n       print(\"Unable to open database. Verify that you created the directory described \" +\n         \"in the Getting Started section.\")\n-//      XCPlaygroundPage.currentPage.finishExecution()\n+      self.dbPointer = nil\n     }\n-    return nil\n   }\n   \n+  public func executeUpdate(sql:String) -> Bool {\n+    return self.executeUpdate(sql, withArgumentsInArray: [])\n+  }\n+  \n+  public func executeUpdate(sql:String, withArgumentsInArray arrayArgs:[AnyObject]) -> Bool {\n+    \n+    print(sql)\n+    print(arrayArgs)\n+    \n+    guard let _ = self.dbPointer else { return false }\n+    \n+    if (self.isExecutingStatement) {\n+      NSLog(\"WARNING: DirectDB already executing statement\")\n+      return false;\n+    }\n+    \n+    self.isExecutingStatement = true\n+    \n+    var rcs: Int32?\n+    //var updateStatement: COpaquePointer = nil\n+    var statement: COpaquePointer = nil\n+\n+    \n+    /*if (_shouldCacheStatements) {\n+     statement = [self cachedStatementForQuery:sql];\n+     pStmt = statement ? [statement statement] : 0x00;\n+     [statement reset];\n+     }*/\n+\n+    \n+    if (statement == nil) {\n+      \n+      rcs = sqlite3_prepare_v2(self.dbPointer!, sql, -1, &statement, nil);\n+      \n+      if (SQLITE_OK != rcs) {\n+        if (self.loggingErrors) {\n+          //Log error\n+        }\n+        let errorMessage = String.fromCString(sqlite3_errmsg(self.dbPointer!))!\n+        print(\"Query could not be prepared! \\(errorMessage)\")\n+        \n+        sqlite3_finalize(statement)\n+        self.isExecutingStatement = false\n+        return false;\n+      }\n+    }\n+    \n+    \n+    var idx = 0\n+    let queryCount = Int(sqlite3_bind_parameter_count(statement))\n+    if (arrayArgs.count != queryCount) {\n+      NSLog(\"Error: the bind count (\\(queryCount)) is not correct for the # of variables (\\(arrayArgs.count)) (executeQuery)\")\n+      sqlite3_finalize(statement)\n+      self.isExecutingStatement = false\n+      return false\n+    }\n+    \n+    for (idx, obj) in arrayArgs.enumerate() {\n+      let res = self.bind(obj, atIndex:idx+1, inStatement:statement)\n+      if (res == SQLITE_OK) {\n+        print(\"bind was successful\")\n+      } else {\n+        print(\"failed to bind: \\(res)\")\n+      }\n+    }\n+    \n+    /* Call sqlite3_step() to run the virtual machine. Since the SQL being\n+     ** executed is not a SELECT statement, we assume no data will be returned.\n+     */\n+    \n+    let rc = sqlite3_step(statement);\n+    \n+    if (SQLITE_DONE == rc) {\n+      // all is well, let's return.\n+    }\n+    else if (SQLITE_ERROR == rc) {\n+      if (self.loggingErrors) {\n+        NSLog(\"Error calling sqlite3_step SQLITE_ERROR \\(rc): \\(sqlite3_errmsg(self.dbPointer!))\");","path":"hldb/Lib/DirectConnector.swift","position":null,"original_position":180,"commit_id":"4508b921264814b84b265e07d63f8365e69278f7","original_commit_id":"4508b921264814b84b265e07d63f8365e69278f7","user":{"login":"bkase","id":515445,"avatar_url":"https://avatars.githubusercontent.com/u/515445?v=3","gravatar_id":"","url":"https://api.github.com/users/bkase","html_url":"https://github.com/bkase","followers_url":"https://api.github.com/users/bkase/followers","following_url":"https://api.github.com/users/bkase/following{/other_user}","gists_url":"https://api.github.com/users/bkase/gists{/gist_id}","starred_url":"https://api.github.com/users/bkase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bkase/subscriptions","organizations_url":"https://api.github.com/users/bkase/orgs","repos_url":"https://api.github.com/users/bkase/repos","events_url":"https://api.github.com/users/bkase/events{/privacy}","received_events_url":"https://api.github.com/users/bkase/received_events","type":"User","site_admin":false},"body":"and here","created_at":"2016-07-08T00:10:19Z","updated_at":"2016-07-08T00:10:20Z","html_url":"https://github.com/mathcamp/lightbase/pull/3#discussion_r70009062","pull_request_url":"https://api.github.com/repos/mathcamp/lightbase/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/mathcamp/lightbase/pulls/comments/70009062"},"html":{"href":"https://github.com/mathcamp/lightbase/pull/3#discussion_r70009062"},"pull_request":{"href":"https://api.github.com/repos/mathcamp/lightbase/pulls/3"}}},"pull_request":{"url":"https://api.github.com/repos/mathcamp/lightbase/pulls/3","id":76683891,"html_url":"https://github.com/mathcamp/lightbase/pull/3","diff_url":"https://github.com/mathcamp/lightbase/pull/3.diff","patch_url":"https://github.com/mathcamp/lightbase/pull/3.patch","issue_url":"https://api.github.com/repos/mathcamp/lightbase/issues/3","number":3,"state":"open","locked":false,"title":"Sqlite wrapper","user":{"login":"noahpicard","id":517189,"avatar_url":"https://avatars.githubusercontent.com/u/517189?v=3","gravatar_id":"","url":"https://api.github.com/users/noahpicard","html_url":"https://github.com/noahpicard","followers_url":"https://api.github.com/users/noahpicard/followers","following_url":"https://api.github.com/users/noahpicard/following{/other_user}","gists_url":"https://api.github.com/users/noahpicard/gists{/gist_id}","starred_url":"https://api.github.com/users/noahpicard/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/noahpicard/subscriptions","organizations_url":"https://api.github.com/users/noahpicard/orgs","repos_url":"https://api.github.com/users/noahpicard/repos","events_url":"https://api.github.com/users/noahpicard/events{/privacy}","received_events_url":"https://api.github.com/users/noahpicard/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-07-07T23:54:32Z","updated_at":"2016-07-08T00:10:20Z","closed_at":null,"merged_at":null,"merge_commit_sha":"aa8fe3af015fcf72d320fb415c0a92ad654dd6ce","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/mathcamp/lightbase/pulls/3/commits","review_comments_url":"https://api.github.com/repos/mathcamp/lightbase/pulls/3/comments","review_comment_url":"https://api.github.com/repos/mathcamp/lightbase/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mathcamp/lightbase/issues/3/comments","statuses_url":"https://api.github.com/repos/mathcamp/lightbase/statuses/4508b921264814b84b265e07d63f8365e69278f7","head":{"label":"mathcamp:sqlite_wrapper","ref":"sqlite_wrapper","sha":"4508b921264814b84b265e07d63f8365e69278f7","user":{"login":"mathcamp","id":3375274,"avatar_url":"https://avatars.githubusercontent.com/u/3375274?v=3","gravatar_id":"","url":"https://api.github.com/users/mathcamp","html_url":"https://github.com/mathcamp","followers_url":"https://api.github.com/users/mathcamp/followers","following_url":"https://api.github.com/users/mathcamp/following{/other_user}","gists_url":"https://api.github.com/users/mathcamp/gists{/gist_id}","starred_url":"https://api.github.com/users/mathcamp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mathcamp/subscriptions","organizations_url":"https://api.github.com/users/mathcamp/orgs","repos_url":"https://api.github.com/users/mathcamp/repos","events_url":"https://api.github.com/users/mathcamp/events{/privacy}","received_events_url":"https://api.github.com/users/mathcamp/received_events","type":"Organization","site_admin":false},"repo":{"id":39967704,"name":"lightbase","full_name":"mathcamp/lightbase","owner":{"login":"mathcamp","id":3375274,"avatar_url":"https://avatars.githubusercontent.com/u/3375274?v=3","gravatar_id":"","url":"https://api.github.com/users/mathcamp","html_url":"https://github.com/mathcamp","followers_url":"https://api.github.com/users/mathcamp/followers","following_url":"https://api.github.com/users/mathcamp/following{/other_user}","gists_url":"https://api.github.com/users/mathcamp/gists{/gist_id}","starred_url":"https://api.github.com/users/mathcamp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mathcamp/subscriptions","organizations_url":"https://api.github.com/users/mathcamp/orgs","repos_url":"https://api.github.com/users/mathcamp/repos","events_url":"https://api.github.com/users/mathcamp/events{/privacy}","received_events_url":"https://api.github.com/users/mathcamp/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mathcamp/lightbase","description":"A Swifty database wrapper around SQLLite","fork":false,"url":"https://api.github.com/repos/mathcamp/lightbase","forks_url":"https://api.github.com/repos/mathcamp/lightbase/forks","keys_url":"https://api.github.com/repos/mathcamp/lightbase/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mathcamp/lightbase/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mathcamp/lightbase/teams","hooks_url":"https://api.github.com/repos/mathcamp/lightbase/hooks","issue_events_url":"https://api.github.com/repos/mathcamp/lightbase/issues/events{/number}","events_url":"https://api.github.com/repos/mathcamp/lightbase/events","assignees_url":"https://api.github.com/repos/mathcamp/lightbase/assignees{/user}","branches_url":"https://api.github.com/repos/mathcamp/lightbase/branches{/branch}","tags_url":"https://api.github.com/repos/mathcamp/lightbase/tags","blobs_url":"https://api.github.com/repos/mathcamp/lightbase/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mathcamp/lightbase/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mathcamp/lightbase/git/refs{/sha}","trees_url":"https://api.github.com/repos/mathcamp/lightbase/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mathcamp/lightbase/statuses/{sha}","languages_url":"https://api.github.com/repos/mathcamp/lightbase/languages","stargazers_url":"https://api.github.com/repos/mathcamp/lightbase/stargazers","contributors_url":"https://api.github.com/repos/mathcamp/lightbase/contributors","subscribers_url":"https://api.github.com/repos/mathcamp/lightbase/subscribers","subscription_url":"https://api.github.com/repos/mathcamp/lightbase/subscription","commits_url":"https://api.github.com/repos/mathcamp/lightbase/commits{/sha}","git_commits_url":"https://api.github.com/repos/mathcamp/lightbase/git/commits{/sha}","comments_url":"https://api.github.com/repos/mathcamp/lightbase/comments{/number}","issue_comment_url":"https://api.github.com/repos/mathcamp/lightbase/issues/comments{/number}","contents_url":"https://api.github.com/repos/mathcamp/lightbase/contents/{+path}","compare_url":"https://api.github.com/repos/mathcamp/lightbase/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mathcamp/lightbase/merges","archive_url":"https://api.github.com/repos/mathcamp/lightbase/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mathcamp/lightbase/downloads","issues_url":"https://api.github.com/repos/mathcamp/lightbase/issues{/number}","pulls_url":"https://api.github.com/repos/mathcamp/lightbase/pulls{/number}","milestones_url":"https://api.github.com/repos/mathcamp/lightbase/milestones{/number}","notifications_url":"https://api.github.com/repos/mathcamp/lightbase/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mathcamp/lightbase/labels{/name}","releases_url":"https://api.github.com/repos/mathcamp/lightbase/releases{/id}","deployments_url":"https://api.github.com/repos/mathcamp/lightbase/deployments","created_at":"2015-07-30T19:07:22Z","updated_at":"2016-06-29T18:42:27Z","pushed_at":"2016-07-07T23:54:33Z","git_url":"git://github.com/mathcamp/lightbase.git","ssh_url":"git@github.com:mathcamp/lightbase.git","clone_url":"https://github.com/mathcamp/lightbase.git","svn_url":"https://github.com/mathcamp/lightbase","homepage":"","size":2260,"stargazers_count":0,"watchers_count":0,"language":"Swift","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"mathcamp:master","ref":"master","sha":"ca28b6a5f5a1e2837f294492ad73dfd17dc7de1e","user":{"login":"mathcamp","id":3375274,"avatar_url":"https://avatars.githubusercontent.com/u/3375274?v=3","gravatar_id":"","url":"https://api.github.com/users/mathcamp","html_url":"https://github.com/mathcamp","followers_url":"https://api.github.com/users/mathcamp/followers","following_url":"https://api.github.com/users/mathcamp/following{/other_user}","gists_url":"https://api.github.com/users/mathcamp/gists{/gist_id}","starred_url":"https://api.github.com/users/mathcamp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mathcamp/subscriptions","organizations_url":"https://api.github.com/users/mathcamp/orgs","repos_url":"https://api.github.com/users/mathcamp/repos","events_url":"https://api.github.com/users/mathcamp/events{/privacy}","received_events_url":"https://api.github.com/users/mathcamp/received_events","type":"Organization","site_admin":false},"repo":{"id":39967704,"name":"lightbase","full_name":"mathcamp/lightbase","owner":{"login":"mathcamp","id":3375274,"avatar_url":"https://avatars.githubusercontent.com/u/3375274?v=3","gravatar_id":"","url":"https://api.github.com/users/mathcamp","html_url":"https://github.com/mathcamp","followers_url":"https://api.github.com/users/mathcamp/followers","following_url":"https://api.github.com/users/mathcamp/following{/other_user}","gists_url":"https://api.github.com/users/mathcamp/gists{/gist_id}","starred_url":"https://api.github.com/users/mathcamp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mathcamp/subscriptions","organizations_url":"https://api.github.com/users/mathcamp/orgs","repos_url":"https://api.github.com/users/mathcamp/repos","events_url":"https://api.github.com/users/mathcamp/events{/privacy}","received_events_url":"https://api.github.com/users/mathcamp/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mathcamp/lightbase","description":"A Swifty database wrapper around SQLLite","fork":false,"url":"https://api.github.com/repos/mathcamp/lightbase","forks_url":"https://api.github.com/repos/mathcamp/lightbase/forks","keys_url":"https://api.github.com/repos/mathcamp/lightbase/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mathcamp/lightbase/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mathcamp/lightbase/teams","hooks_url":"https://api.github.com/repos/mathcamp/lightbase/hooks","issue_events_url":"https://api.github.com/repos/mathcamp/lightbase/issues/events{/number}","events_url":"https://api.github.com/repos/mathcamp/lightbase/events","assignees_url":"https://api.github.com/repos/mathcamp/lightbase/assignees{/user}","branches_url":"https://api.github.com/repos/mathcamp/lightbase/branches{/branch}","tags_url":"https://api.github.com/repos/mathcamp/lightbase/tags","blobs_url":"https://api.github.com/repos/mathcamp/lightbase/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mathcamp/lightbase/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mathcamp/lightbase/git/refs{/sha}","trees_url":"https://api.github.com/repos/mathcamp/lightbase/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mathcamp/lightbase/statuses/{sha}","languages_url":"https://api.github.com/repos/mathcamp/lightbase/languages","stargazers_url":"https://api.github.com/repos/mathcamp/lightbase/stargazers","contributors_url":"https://api.github.com/repos/mathcamp/lightbase/contributors","subscribers_url":"https://api.github.com/repos/mathcamp/lightbase/subscribers","subscription_url":"https://api.github.com/repos/mathcamp/lightbase/subscription","commits_url":"https://api.github.com/repos/mathcamp/lightbase/commits{/sha}","git_commits_url":"https://api.github.com/repos/mathcamp/lightbase/git/commits{/sha}","comments_url":"https://api.github.com/repos/mathcamp/lightbase/comments{/number}","issue_comment_url":"https://api.github.com/repos/mathcamp/lightbase/issues/comments{/number}","contents_url":"https://api.github.com/repos/mathcamp/lightbase/contents/{+path}","compare_url":"https://api.github.com/repos/mathcamp/lightbase/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mathcamp/lightbase/merges","archive_url":"https://api.github.com/repos/mathcamp/lightbase/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mathcamp/lightbase/downloads","issues_url":"https://api.github.com/repos/mathcamp/lightbase/issues{/number}","pulls_url":"https://api.github.com/repos/mathcamp/lightbase/pulls{/number}","milestones_url":"https://api.github.com/repos/mathcamp/lightbase/milestones{/number}","notifications_url":"https://api.github.com/repos/mathcamp/lightbase/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mathcamp/lightbase/labels{/name}","releases_url":"https://api.github.com/repos/mathcamp/lightbase/releases{/id}","deployments_url":"https://api.github.com/repos/mathcamp/lightbase/deployments","created_at":"2015-07-30T19:07:22Z","updated_at":"2016-06-29T18:42:27Z","pushed_at":"2016-07-07T23:54:33Z","git_url":"git://github.com/mathcamp/lightbase.git","ssh_url":"git@github.com:mathcamp/lightbase.git","clone_url":"https://github.com/mathcamp/lightbase.git","svn_url":"https://github.com/mathcamp/lightbase","homepage":"","size":2260,"stargazers_count":0,"watchers_count":0,"language":"Swift","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mathcamp/lightbase/pulls/3"},"html":{"href":"https://github.com/mathcamp/lightbase/pull/3"},"issue":{"href":"https://api.github.com/repos/mathcamp/lightbase/issues/3"},"comments":{"href":"https://api.github.com/repos/mathcamp/lightbase/issues/3/comments"},"review_comments":{"href":"https://api.github.com/repos/mathcamp/lightbase/pulls/3/comments"},"review_comment":{"href":"https://api.github.com/repos/mathcamp/lightbase/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mathcamp/lightbase/pulls/3/commits"},"statuses":{"href":"https://api.github.com/repos/mathcamp/lightbase/statuses/4508b921264814b84b265e07d63f8365e69278f7"}}}}
{"actor":{"display_login":"bkase"}}