{"action":"created","comment":{"url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/comments/24033924","id":24033924,"diff_hunk":"@@ -0,0 +1,123 @@\n+# #######\n+# Copyright (c) 2015 GigaSpaces Technologies Ltd. All rights reserved\n+#\n+# Licensed under the Apache License, Version 2.0 (the \"License\");\n+# you may not use this file except in compliance with the License.\n+# You may obtain a copy of the License at\n+#\n+# http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing, software\n+# distributed under the License is distributed on an \"AS IS\" BASIS,\n+# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+# * See the License for the specific language governing permissions and\n+# * limitations under the License.\n+import json\n+import sqlite3\n+from base import AbstractStorage\n+\n+\n+class SQLiteStorage(AbstractStorage):\n+    \"\"\" Storage wrapper for SQLite DB implementing AbstractStorage interface\"\"\"\n+    _CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS servers ' \\\n+                    '(global_id integer PRIMARY KEY, server_id text, ' \\\n+                    'address text, auth text, port integer, alive integer, ' \\\n+                    'reserved integer)'\n+\n+    def __init__(self, db_filename):\n+        self._filename = db_filename\n+        self._create_table()\n+\n+    def _create_table(self):\n+        with sqlite3.connect(self._filename) as conn:\n+            cursor = conn.cursor()\n+            cursor.execute(SQLiteStorage._CREATE_TABLE)\n+\n+    def _dict_factory(self, cursor, row):\n+        \"\"\" Create dictionary out of fetched row by db cursor\"\"\"\n+        d = {}\n+        for idx, col in enumerate(cursor.description):\n+            if col[0] == 'auth':\n+                # \"auth\" column value is dictionary serialized to string\n+                # -> json.dumps(auth).\n+                d[col[0]] = json.loads(row[idx])\n+            else:\n+                d[col[0]] = row[idx]\n+        return d\n+\n+    def _get_sql_and_values_from_kwargs(self, **kwargs):\n+        \"\"\" Helper method to create sql query \"\"\"\n+        values = tuple(kwargs.itervalues())\n+        sql_part = ' AND '.join('{0}=?'.format(k) for k in kwargs)\n+        return sql_part, values\n+\n+    def get_servers(self, **kwargs):\n+        with sqlite3.connect(self._filename) as conn:\n+            conn.row_factory = self._dict_factory\n+            cursor = conn.cursor()\n+            if not kwargs:\n+                cursor.execute('SELECT * FROM servers')\n+            else:\n+                sql_cond, values = self._get_sql_and_values_from_kwargs(\n+                    **kwargs)\n+                cursor.execute('SELECT * FROM servers WHERE ' + sql_cond,\n+                               values)\n+            result = cursor.fetchall()\n+            return list(result)\n+\n+    def add_server(self, server):\n+        with sqlite3.connect(self._filename) as conn:\n+            cursor = conn.cursor()\n+            values = (server.get('server_id'), server['address'],\n+                      json.dumps(server['auth']), server['port'],\n+                      server['alive'], server['reserved'])\n+            cursor.execute('INSERT INTO servers '\n+                           '(server_id, address, auth, port, alive, reserved) '\n+                           'VALUES(?, ?, ?, ?, ?, ?)', values)\n+            server['global_id'] = cursor.lastrowid\n+            return server\n+\n+    def update_server(self, server, **kwargs):\n+        if not kwargs:\n+            return False, None\n+        with sqlite3.connect(self._filename) as conn:\n+            conn.row_factory = self._dict_factory\n+            cursor = conn.cursor()\n+            values = tuple(kwargs.itervalues())\n+            sql_part = \", \".join('{0}=?'.format(k) for k in kwargs)\n+            cursor.execute('UPDATE servers SET {0} WHERE global_id=?'\n+                           .format(sql_part),\n+                           values + (server['global_id'],))\n+            if cursor.rowcount == 0:\n+                return False, None\n+            conn.commit()\n+            cursor.execute('SELECT * FROM servers WHERE global_id=?',\n+                           (server['global_id'],))\n+            return True, cursor.fetchone()\n+\n+    def get_server(self, **kwargs):","path":"cloudify_hostpool/storage/sqlite.py","position":98,"original_position":98,"commit_id":"0d2a4e714e139123bef60b44c78df79dce483d18","original_commit_id":"0d2a4e714e139123bef60b44c78df79dce483d18","user":{"login":"iliapolo","id":1428812,"avatar_url":"https://avatars.githubusercontent.com/u/1428812?v=3","gravatar_id":"","url":"https://api.github.com/users/iliapolo","html_url":"https://github.com/iliapolo","followers_url":"https://api.github.com/users/iliapolo/followers","following_url":"https://api.github.com/users/iliapolo/following{/other_user}","gists_url":"https://api.github.com/users/iliapolo/gists{/gist_id}","starred_url":"https://api.github.com/users/iliapolo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iliapolo/subscriptions","organizations_url":"https://api.github.com/users/iliapolo/orgs","repos_url":"https://api.github.com/users/iliapolo/repos","events_url":"https://api.github.com/users/iliapolo/events{/privacy}","received_events_url":"https://api.github.com/users/iliapolo/received_events","type":"User","site_admin":false},"body":"same here","created_at":"2015-02-03T19:55:07Z","updated_at":"2015-02-03T19:55:07Z","html_url":"https://github.com/cloudify-cosmo/cloudify-byon-service/pull/8#discussion_r24033924","pull_request_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/8","_links":{"self":{"href":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/comments/24033924"},"html":{"href":"https://github.com/cloudify-cosmo/cloudify-byon-service/pull/8#discussion_r24033924"},"pull_request":{"href":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/8"}}},"pull_request":{"url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/8","id":28569937,"html_url":"https://github.com/cloudify-cosmo/cloudify-byon-service/pull/8","diff_url":"https://github.com/cloudify-cosmo/cloudify-byon-service/pull/8.diff","patch_url":"https://github.com/cloudify-cosmo/cloudify-byon-service/pull/8.patch","issue_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues/8","number":8,"state":"open","locked":false,"title":"CFY-2032 Add sqlite implementation","user":{"login":"szpotona","id":9609709,"avatar_url":"https://avatars.githubusercontent.com/u/9609709?v=3","gravatar_id":"","url":"https://api.github.com/users/szpotona","html_url":"https://github.com/szpotona","followers_url":"https://api.github.com/users/szpotona/followers","following_url":"https://api.github.com/users/szpotona/following{/other_user}","gists_url":"https://api.github.com/users/szpotona/gists{/gist_id}","starred_url":"https://api.github.com/users/szpotona/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/szpotona/subscriptions","organizations_url":"https://api.github.com/users/szpotona/orgs","repos_url":"https://api.github.com/users/szpotona/repos","events_url":"https://api.github.com/users/szpotona/events{/privacy}","received_events_url":"https://api.github.com/users/szpotona/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-02-03T17:08:33Z","updated_at":"2015-02-03T19:55:07Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a11da02ec21b973c3097eb9926c42f40f6117d3d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/8/commits","review_comments_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/8/comments","review_comment_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/comments/{number}","comments_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues/8/comments","statuses_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/statuses/0d2a4e714e139123bef60b44c78df79dce483d18","head":{"label":"cloudify-cosmo:cfy-2032_sqlite_implementation","ref":"cfy-2032_sqlite_implementation","sha":"0d2a4e714e139123bef60b44c78df79dce483d18","user":{"login":"cloudify-cosmo","id":6260555,"avatar_url":"https://avatars.githubusercontent.com/u/6260555?v=3","gravatar_id":"","url":"https://api.github.com/users/cloudify-cosmo","html_url":"https://github.com/cloudify-cosmo","followers_url":"https://api.github.com/users/cloudify-cosmo/followers","following_url":"https://api.github.com/users/cloudify-cosmo/following{/other_user}","gists_url":"https://api.github.com/users/cloudify-cosmo/gists{/gist_id}","starred_url":"https://api.github.com/users/cloudify-cosmo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloudify-cosmo/subscriptions","organizations_url":"https://api.github.com/users/cloudify-cosmo/orgs","repos_url":"https://api.github.com/users/cloudify-cosmo/repos","events_url":"https://api.github.com/users/cloudify-cosmo/events{/privacy}","received_events_url":"https://api.github.com/users/cloudify-cosmo/received_events","type":"Organization","site_admin":false},"repo":{"id":29589622,"name":"cloudify-byon-service","full_name":"cloudify-cosmo/cloudify-byon-service","owner":{"login":"cloudify-cosmo","id":6260555,"avatar_url":"https://avatars.githubusercontent.com/u/6260555?v=3","gravatar_id":"","url":"https://api.github.com/users/cloudify-cosmo","html_url":"https://github.com/cloudify-cosmo","followers_url":"https://api.github.com/users/cloudify-cosmo/followers","following_url":"https://api.github.com/users/cloudify-cosmo/following{/other_user}","gists_url":"https://api.github.com/users/cloudify-cosmo/gists{/gist_id}","starred_url":"https://api.github.com/users/cloudify-cosmo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloudify-cosmo/subscriptions","organizations_url":"https://api.github.com/users/cloudify-cosmo/orgs","repos_url":"https://api.github.com/users/cloudify-cosmo/repos","events_url":"https://api.github.com/users/cloudify-cosmo/events{/privacy}","received_events_url":"https://api.github.com/users/cloudify-cosmo/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cloudify-cosmo/cloudify-byon-service","description":"BYON service, used with the Cloudify BYON plugin","fork":false,"url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service","forks_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/forks","keys_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/teams","hooks_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/hooks","issue_events_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues/events{/number}","events_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/events","assignees_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/assignees{/user}","branches_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/branches{/branch}","tags_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/tags","blobs_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/refs{/sha}","trees_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/statuses/{sha}","languages_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/languages","stargazers_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/stargazers","contributors_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/contributors","subscribers_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/subscribers","subscription_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/subscription","commits_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/commits{/sha}","git_commits_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/commits{/sha}","comments_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/comments{/number}","issue_comment_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues/comments/{number}","contents_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/contents/{+path}","compare_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/merges","archive_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/downloads","issues_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues{/number}","pulls_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls{/number}","milestones_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/milestones{/number}","notifications_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/labels{/name}","releases_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/releases{/id}","created_at":"2015-01-21T13:00:36Z","updated_at":"2015-02-03T16:12:53Z","pushed_at":"2015-02-03T18:43:32Z","git_url":"git://github.com/cloudify-cosmo/cloudify-byon-service.git","ssh_url":"git@github.com:cloudify-cosmo/cloudify-byon-service.git","clone_url":"https://github.com/cloudify-cosmo/cloudify-byon-service.git","svn_url":"https://github.com/cloudify-cosmo/cloudify-byon-service","homepage":null,"size":246,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":5,"forks":1,"open_issues":5,"watchers":0,"default_branch":"master"}},"base":{"label":"cloudify-cosmo:master","ref":"master","sha":"320c89ccae20c9192cc5f842effdbce076587664","user":{"login":"cloudify-cosmo","id":6260555,"avatar_url":"https://avatars.githubusercontent.com/u/6260555?v=3","gravatar_id":"","url":"https://api.github.com/users/cloudify-cosmo","html_url":"https://github.com/cloudify-cosmo","followers_url":"https://api.github.com/users/cloudify-cosmo/followers","following_url":"https://api.github.com/users/cloudify-cosmo/following{/other_user}","gists_url":"https://api.github.com/users/cloudify-cosmo/gists{/gist_id}","starred_url":"https://api.github.com/users/cloudify-cosmo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloudify-cosmo/subscriptions","organizations_url":"https://api.github.com/users/cloudify-cosmo/orgs","repos_url":"https://api.github.com/users/cloudify-cosmo/repos","events_url":"https://api.github.com/users/cloudify-cosmo/events{/privacy}","received_events_url":"https://api.github.com/users/cloudify-cosmo/received_events","type":"Organization","site_admin":false},"repo":{"id":29589622,"name":"cloudify-byon-service","full_name":"cloudify-cosmo/cloudify-byon-service","owner":{"login":"cloudify-cosmo","id":6260555,"avatar_url":"https://avatars.githubusercontent.com/u/6260555?v=3","gravatar_id":"","url":"https://api.github.com/users/cloudify-cosmo","html_url":"https://github.com/cloudify-cosmo","followers_url":"https://api.github.com/users/cloudify-cosmo/followers","following_url":"https://api.github.com/users/cloudify-cosmo/following{/other_user}","gists_url":"https://api.github.com/users/cloudify-cosmo/gists{/gist_id}","starred_url":"https://api.github.com/users/cloudify-cosmo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cloudify-cosmo/subscriptions","organizations_url":"https://api.github.com/users/cloudify-cosmo/orgs","repos_url":"https://api.github.com/users/cloudify-cosmo/repos","events_url":"https://api.github.com/users/cloudify-cosmo/events{/privacy}","received_events_url":"https://api.github.com/users/cloudify-cosmo/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cloudify-cosmo/cloudify-byon-service","description":"BYON service, used with the Cloudify BYON plugin","fork":false,"url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service","forks_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/forks","keys_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/teams","hooks_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/hooks","issue_events_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues/events{/number}","events_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/events","assignees_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/assignees{/user}","branches_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/branches{/branch}","tags_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/tags","blobs_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/refs{/sha}","trees_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/statuses/{sha}","languages_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/languages","stargazers_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/stargazers","contributors_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/contributors","subscribers_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/subscribers","subscription_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/subscription","commits_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/commits{/sha}","git_commits_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/git/commits{/sha}","comments_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/comments{/number}","issue_comment_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues/comments/{number}","contents_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/contents/{+path}","compare_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/merges","archive_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/downloads","issues_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues{/number}","pulls_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls{/number}","milestones_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/milestones{/number}","notifications_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/labels{/name}","releases_url":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/releases{/id}","created_at":"2015-01-21T13:00:36Z","updated_at":"2015-02-03T16:12:53Z","pushed_at":"2015-02-03T18:43:32Z","git_url":"git://github.com/cloudify-cosmo/cloudify-byon-service.git","ssh_url":"git@github.com:cloudify-cosmo/cloudify-byon-service.git","clone_url":"https://github.com/cloudify-cosmo/cloudify-byon-service.git","svn_url":"https://github.com/cloudify-cosmo/cloudify-byon-service","homepage":null,"size":246,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":5,"forks":1,"open_issues":5,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/8"},"html":{"href":"https://github.com/cloudify-cosmo/cloudify-byon-service/pull/8"},"issue":{"href":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues/8"},"comments":{"href":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/issues/8/comments"},"review_comments":{"href":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/8/comments"},"review_comment":{"href":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/pulls/8/commits"},"statuses":{"href":"https://api.github.com/repos/cloudify-cosmo/cloudify-byon-service/statuses/0d2a4e714e139123bef60b44c78df79dce483d18"}}}}