{"action":"created","comment":{"url":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/comments/60080889","id":60080889,"diff_hunk":"@@ -442,80 +443,66 @@ DB2.prototype.update = function(model, where, data, options, cb) {\n DB2.prototype.updateOrCreate = DB2.prototype.save =\n   function(model, data, options, callback) {\n     var self = this;\n-    var fields = self.buildFields(model, data);\n     var id = self.idName(model);\n-    var sql = new ParameterizedSQL('MERGE INTO ' +\n-                                   self.schema.toUpperCase() + '.' +\n-                                   self.tableEscaped(model));\n-    var columnValues = fields.columnValues;\n-    var fieldNames = fields.names;\n-    var setValues = [];\n-\n-    var definition = self.getModelDefinition(model);\n-\n-    if (fieldNames.length) {\n-      sql.merge('AS MT(');\n-\n-      Object.keys(definition.properties).forEach(function(prop) {\n-        setValues.push(new ParameterizedSQL(self.columnEscaped(model, prop)));\n-      });\n-\n-      var columns = ParameterizedSQL.join(setValues, ',');\n-      sql.merge(columns);\n-      sql.merge(')');\n-      sql.merge('USING (SELECT * FROM TABLE( VALUES(');\n-\n-      setValues = [];\n-      for (var i = 0, n = fields.names.length; i < n; i++) {\n-        setValues.push(new ParameterizedSQL('CAST (' + columnValues[i].sql +\n-          ' AS ' +\n-          self.buildColumnType(fields.properties[i]) + ')',\n-          columnValues[i].params));\n-\n-        if (i < (n - 1))\n-          setValues[i].sql = setValues[i].sql + ',';\n-      }\n-\n-      sql.merge(setValues);\n-      sql.merge('))) AS VT(' + fieldNames.join(',') + ')');\n-      sql.merge('ON');\n-      sql.merge('(MT.\\\"' + id + '\\\" = VT.\\\"' + id + '\\\")');\n-      sql.merge('WHEN NOT MATCHED THEN INSERT (' +\n-      fieldNames.join(',') + ')');\n-\n-      var values = ParameterizedSQL.join(columnValues, ',');\n-      values.sql = 'VALUES(' + values.sql + ')';\n-      sql.merge(values);\n-    } else {\n-      sql.merge(self.buildInsertDefaultValues(model, data, options));\n-    }\n-\n-    sql.merge('WHEN MATCHED THEN UPDATE SET');\n-\n-    setValues = [];\n-    for (i = 0, n = fields.names.length; i < n; i++) {\n-      if (!fields.properties[i].id) {\n-        setValues.push(new ParameterizedSQL(fields.names[i] + '=' +\n-          '(' + columnValues[i].sql + ')', columnValues[i].params));\n+    var stmt;\n+    var tableName = self.tableEscaped(model);\n+    var idData = {};\n+    var countData;\n+    var isInsert = false;\n+    var meta = {};\n+    var connection;\n+    var currentTx;\n+\n+    process.nextTick(function() {\n+      if (options.transaction) {\n+        connection = options.transaction.connection;\n+      } else {\n+        try {\n+          connection = Driver.openSync(self.connStr);\n+          currentTx = connection.beginTransactionSync();\n+        } catch (e) {\n+          return callback(Error(e));\n+        }\n       }\n-    }\n-\n-    sql.merge(ParameterizedSQL.join(setValues, ','));\n \n-    self.execute(sql.sql, sql.params, options, function(err, info) {\n-      if (!err && info && info.insertId) {\n-        data.id = info.insertId;\n-      }\n-      var meta = {};\n-      if (info) {\n-        // When using the INSERT ... ON DUPLICATE KEY UPDATE statement,\n-        // the returned value is as follows:\n-        // 1 for each successful INSERT.\n-        // 2 for each successful UPDATE.\n-        meta.isNewInstance = (info.affectedRows === 1);\n+      idData.id = data.id;\n+\n+      try {\n+        var countStmt = new ParameterizedSQL('SELECT COUNT(*) AS cnt FROM ');\n+        countStmt.merge(tableName);\n+        countStmt.merge(self.buildWhere(data));\n+\n+        countData = connection.querySync(countStmt.sql, countStmt.params);\n+\n+        if (countData[0]['CNT'] > 0) {\n+          stmt = self.buildUpdate(model, idData, data);\n+        } else {\n+          stmt = self.buildInsert(model, data);\n+          isInsert = true;\n+        }\n+\n+        connection.querySync(stmt.sql, stmt.params);","path":"lib/db2.js","position":127,"original_position":127,"commit_id":"0a078747be9f5ef18e63272f56cf2f8d50247792","original_commit_id":"0a078747be9f5ef18e63272f56cf2f8d50247792","user":{"login":"raymondfeng","id":540892,"avatar_url":"https://avatars.githubusercontent.com/u/540892?v=3","gravatar_id":"","url":"https://api.github.com/users/raymondfeng","html_url":"https://github.com/raymondfeng","followers_url":"https://api.github.com/users/raymondfeng/followers","following_url":"https://api.github.com/users/raymondfeng/following{/other_user}","gists_url":"https://api.github.com/users/raymondfeng/gists{/gist_id}","starred_url":"https://api.github.com/users/raymondfeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/raymondfeng/subscriptions","organizations_url":"https://api.github.com/users/raymondfeng/orgs","repos_url":"https://api.github.com/users/raymondfeng/repos","events_url":"https://api.github.com/users/raymondfeng/events{/privacy}","received_events_url":"https://api.github.com/users/raymondfeng/received_events","type":"User","site_admin":false},"body":"Again, why sync?","created_at":"2016-04-18T15:36:41Z","updated_at":"2016-04-18T15:36:41Z","html_url":"https://github.com/strongloop/loopback-connector-db2/pull/29#discussion_r60080889","pull_request_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/29","_links":{"self":{"href":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/comments/60080889"},"html":{"href":"https://github.com/strongloop/loopback-connector-db2/pull/29#discussion_r60080889"},"pull_request":{"href":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/29"}}},"pull_request":{"url":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/29","id":66376036,"html_url":"https://github.com/strongloop/loopback-connector-db2/pull/29","diff_url":"https://github.com/strongloop/loopback-connector-db2/pull/29.diff","patch_url":"https://github.com/strongloop/loopback-connector-db2/pull/29.patch","issue_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues/29","number":29,"state":"open","locked":false,"title":"Add DB2 for z/OS support and a fix for issues #28","user":{"login":"qpresley","id":15216139,"avatar_url":"https://avatars.githubusercontent.com/u/15216139?v=3","gravatar_id":"","url":"https://api.github.com/users/qpresley","html_url":"https://github.com/qpresley","followers_url":"https://api.github.com/users/qpresley/followers","following_url":"https://api.github.com/users/qpresley/following{/other_user}","gists_url":"https://api.github.com/users/qpresley/gists{/gist_id}","starred_url":"https://api.github.com/users/qpresley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/qpresley/subscriptions","organizations_url":"https://api.github.com/users/qpresley/orgs","repos_url":"https://api.github.com/users/qpresley/repos","events_url":"https://api.github.com/users/qpresley/events{/privacy}","received_events_url":"https://api.github.com/users/qpresley/received_events","type":"User","site_admin":false},"body":"Adding DB2 z/OS support has resulted in a couple changes to make the code more portable including removal of the MERGE statement that was used in updateOrCreate.  There are some operations that are also now transactional which both simplifies the code and makes it work across the DB2 products.\r\n\r\nThere is also a small fix to discovery.js for issue #28","created_at":"2016-04-13T19:18:20Z","updated_at":"2016-04-18T15:36:41Z","closed_at":null,"merged_at":null,"merge_commit_sha":"943f37ef25631805d1d9cbb2dbb3bba5fb6628a7","assignee":{"login":"raymondfeng","id":540892,"avatar_url":"https://avatars.githubusercontent.com/u/540892?v=3","gravatar_id":"","url":"https://api.github.com/users/raymondfeng","html_url":"https://github.com/raymondfeng","followers_url":"https://api.github.com/users/raymondfeng/followers","following_url":"https://api.github.com/users/raymondfeng/following{/other_user}","gists_url":"https://api.github.com/users/raymondfeng/gists{/gist_id}","starred_url":"https://api.github.com/users/raymondfeng/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/raymondfeng/subscriptions","organizations_url":"https://api.github.com/users/raymondfeng/orgs","repos_url":"https://api.github.com/users/raymondfeng/repos","events_url":"https://api.github.com/users/raymondfeng/events{/privacy}","received_events_url":"https://api.github.com/users/raymondfeng/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/29/commits","review_comments_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/29/comments","review_comment_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/comments{/number}","comments_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues/29/comments","statuses_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/statuses/0a078747be9f5ef18e63272f56cf2f8d50247792","head":{"label":"strongloop:zos","ref":"zos","sha":"0a078747be9f5ef18e63272f56cf2f8d50247792","user":{"login":"strongloop","id":3020012,"avatar_url":"https://avatars.githubusercontent.com/u/3020012?v=3","gravatar_id":"","url":"https://api.github.com/users/strongloop","html_url":"https://github.com/strongloop","followers_url":"https://api.github.com/users/strongloop/followers","following_url":"https://api.github.com/users/strongloop/following{/other_user}","gists_url":"https://api.github.com/users/strongloop/gists{/gist_id}","starred_url":"https://api.github.com/users/strongloop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/strongloop/subscriptions","organizations_url":"https://api.github.com/users/strongloop/orgs","repos_url":"https://api.github.com/users/strongloop/repos","events_url":"https://api.github.com/users/strongloop/events{/privacy}","received_events_url":"https://api.github.com/users/strongloop/received_events","type":"Organization","site_admin":false},"repo":{"id":46471632,"name":"loopback-connector-db2","full_name":"strongloop/loopback-connector-db2","owner":{"login":"strongloop","id":3020012,"avatar_url":"https://avatars.githubusercontent.com/u/3020012?v=3","gravatar_id":"","url":"https://api.github.com/users/strongloop","html_url":"https://github.com/strongloop","followers_url":"https://api.github.com/users/strongloop/followers","following_url":"https://api.github.com/users/strongloop/following{/other_user}","gists_url":"https://api.github.com/users/strongloop/gists{/gist_id}","starred_url":"https://api.github.com/users/strongloop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/strongloop/subscriptions","organizations_url":"https://api.github.com/users/strongloop/orgs","repos_url":"https://api.github.com/users/strongloop/repos","events_url":"https://api.github.com/users/strongloop/events{/privacy}","received_events_url":"https://api.github.com/users/strongloop/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/strongloop/loopback-connector-db2","description":"LoopBack Connector for IBM DB/2","fork":false,"url":"https://api.github.com/repos/strongloop/loopback-connector-db2","forks_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/forks","keys_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/teams","hooks_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/hooks","issue_events_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues/events{/number}","events_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/events","assignees_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/assignees{/user}","branches_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/branches{/branch}","tags_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/tags","blobs_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/refs{/sha}","trees_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/statuses/{sha}","languages_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/languages","stargazers_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/stargazers","contributors_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/contributors","subscribers_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/subscribers","subscription_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/subscription","commits_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/commits{/sha}","git_commits_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/commits{/sha}","comments_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/comments{/number}","issue_comment_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues/comments{/number}","contents_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/contents/{+path}","compare_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/merges","archive_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/downloads","issues_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues{/number}","pulls_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls{/number}","milestones_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/milestones{/number}","notifications_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/labels{/name}","releases_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/releases{/id}","deployments_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/deployments","created_at":"2015-11-19T06:10:56Z","updated_at":"2016-04-07T21:10:52Z","pushed_at":"2016-04-13T19:18:20Z","git_url":"git://github.com/strongloop/loopback-connector-db2.git","ssh_url":"git@github.com:strongloop/loopback-connector-db2.git","clone_url":"https://github.com/strongloop/loopback-connector-db2.git","svn_url":"https://github.com/strongloop/loopback-connector-db2","homepage":null,"size":87,"stargazers_count":2,"watchers_count":2,"language":"PLSQL","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":4,"forks":1,"open_issues":4,"watchers":2,"default_branch":"master"}},"base":{"label":"strongloop:master","ref":"master","sha":"14f8129ea78c9418e04119fe0ce03e7509064563","user":{"login":"strongloop","id":3020012,"avatar_url":"https://avatars.githubusercontent.com/u/3020012?v=3","gravatar_id":"","url":"https://api.github.com/users/strongloop","html_url":"https://github.com/strongloop","followers_url":"https://api.github.com/users/strongloop/followers","following_url":"https://api.github.com/users/strongloop/following{/other_user}","gists_url":"https://api.github.com/users/strongloop/gists{/gist_id}","starred_url":"https://api.github.com/users/strongloop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/strongloop/subscriptions","organizations_url":"https://api.github.com/users/strongloop/orgs","repos_url":"https://api.github.com/users/strongloop/repos","events_url":"https://api.github.com/users/strongloop/events{/privacy}","received_events_url":"https://api.github.com/users/strongloop/received_events","type":"Organization","site_admin":false},"repo":{"id":46471632,"name":"loopback-connector-db2","full_name":"strongloop/loopback-connector-db2","owner":{"login":"strongloop","id":3020012,"avatar_url":"https://avatars.githubusercontent.com/u/3020012?v=3","gravatar_id":"","url":"https://api.github.com/users/strongloop","html_url":"https://github.com/strongloop","followers_url":"https://api.github.com/users/strongloop/followers","following_url":"https://api.github.com/users/strongloop/following{/other_user}","gists_url":"https://api.github.com/users/strongloop/gists{/gist_id}","starred_url":"https://api.github.com/users/strongloop/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/strongloop/subscriptions","organizations_url":"https://api.github.com/users/strongloop/orgs","repos_url":"https://api.github.com/users/strongloop/repos","events_url":"https://api.github.com/users/strongloop/events{/privacy}","received_events_url":"https://api.github.com/users/strongloop/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/strongloop/loopback-connector-db2","description":"LoopBack Connector for IBM DB/2","fork":false,"url":"https://api.github.com/repos/strongloop/loopback-connector-db2","forks_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/forks","keys_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/teams","hooks_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/hooks","issue_events_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues/events{/number}","events_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/events","assignees_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/assignees{/user}","branches_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/branches{/branch}","tags_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/tags","blobs_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/refs{/sha}","trees_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/statuses/{sha}","languages_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/languages","stargazers_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/stargazers","contributors_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/contributors","subscribers_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/subscribers","subscription_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/subscription","commits_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/commits{/sha}","git_commits_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/git/commits{/sha}","comments_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/comments{/number}","issue_comment_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues/comments{/number}","contents_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/contents/{+path}","compare_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/merges","archive_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/downloads","issues_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues{/number}","pulls_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls{/number}","milestones_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/milestones{/number}","notifications_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/labels{/name}","releases_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/releases{/id}","deployments_url":"https://api.github.com/repos/strongloop/loopback-connector-db2/deployments","created_at":"2015-11-19T06:10:56Z","updated_at":"2016-04-07T21:10:52Z","pushed_at":"2016-04-13T19:18:20Z","git_url":"git://github.com/strongloop/loopback-connector-db2.git","ssh_url":"git@github.com:strongloop/loopback-connector-db2.git","clone_url":"https://github.com/strongloop/loopback-connector-db2.git","svn_url":"https://github.com/strongloop/loopback-connector-db2","homepage":null,"size":87,"stargazers_count":2,"watchers_count":2,"language":"PLSQL","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":4,"forks":1,"open_issues":4,"watchers":2,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/29"},"html":{"href":"https://github.com/strongloop/loopback-connector-db2/pull/29"},"issue":{"href":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues/29"},"comments":{"href":"https://api.github.com/repos/strongloop/loopback-connector-db2/issues/29/comments"},"review_comments":{"href":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/29/comments"},"review_comment":{"href":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/strongloop/loopback-connector-db2/pulls/29/commits"},"statuses":{"href":"https://api.github.com/repos/strongloop/loopback-connector-db2/statuses/0a078747be9f5ef18e63272f56cf2f8d50247792"}}}}