-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MySQL Support #69
Merged
Merged
MySQL Support #69
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
87e0962
MySQL query runner
ekampf 1a6e5b4
Include MySQL example
ekampf 95529ce
Separated query runners to diff files
ekampf a2385a1
Removed unecessary logging
ekampf 09c65ee
Merge branch 'refs/heads/master' into feature/mysql
ekampf fef5c28
Returned redshift code
ekampf b056e49
Removed unnecessary logging
ekampf b0159c8
Redshift shouldn't be here
ekampf 6408b9e
Fixed MySQL Runner
ekampf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
""" | ||
QueryRunner is the function that the workers use, to execute queries. This is the Redshift | ||
(PostgreSQL in fact) version, but easily we can write another to support additional databases | ||
(MySQL and others). | ||
|
||
Because the worker just pass the query, this can be used with any data store that has some sort of | ||
query language (for example: HiveQL). | ||
""" | ||
import logging | ||
import json | ||
import psycopg2 | ||
import MySQLdb | ||
import sys | ||
import select | ||
from .utils import JSONEncoder | ||
|
||
def mysql(connection_string): | ||
def column_friendly_name(column_name): | ||
return column_name | ||
|
||
def query_runner(query): | ||
# TODO: remove these lines | ||
import settings | ||
connection_string = settings.CONNECTION_STRING | ||
# ENDTODO | ||
|
||
|
||
if connection_string.endswith(';'): | ||
connection_string = connection_string[0:-1] | ||
|
||
connections_params = [entry.split('=')[1] for entry in connection_string.split(';')] | ||
connection = MySQLdb.connect(*connections_params) | ||
cursor = connection.cursor() | ||
|
||
logging.debug("mysql got query: %s", query) | ||
|
||
try: | ||
cursor.execute(query) | ||
|
||
data = cursor.fetchall() | ||
|
||
num_fields = len(cursor.description) | ||
column_names = [i[0] for i in cursor.description] | ||
|
||
for c in data: | ||
logging.debug(c) | ||
|
||
rows = [dict(zip(column_names, row)) for row in data] | ||
|
||
|
||
columns = [{'name': col_name, | ||
'friendly_name': column_friendly_name(col_name), | ||
'type': None} for col_name in column_names] | ||
|
||
data = {'columns': columns, 'rows': rows} | ||
json_data = json.dumps(data, cls=JSONEncoder) | ||
error = None | ||
cursor.close() | ||
except MySQLdb.Error, e: | ||
json_data = None | ||
error = e.message | ||
except Exception as e: | ||
raise sys.exc_info()[1], None, sys.exc_info()[2] | ||
finally: | ||
connection.close() | ||
|
||
return json_data, error | ||
|
||
|
||
return query_runner | ||
|
||
|
||
|
||
def redshift(connection_string): | ||
def column_friendly_name(column_name): | ||
return column_name | ||
|
||
def wait(conn): | ||
while 1: | ||
state = conn.poll() | ||
if state == psycopg2.extensions.POLL_OK: | ||
break | ||
elif state == psycopg2.extensions.POLL_WRITE: | ||
select.select([], [conn.fileno()], []) | ||
elif state == psycopg2.extensions.POLL_READ: | ||
select.select([conn.fileno()], [], []) | ||
else: | ||
raise psycopg2.OperationalError("poll() returned %s" % state) | ||
|
||
def query_runner(query): | ||
connection = psycopg2.connect(connection_string, async=True) | ||
wait(connection) | ||
|
||
cursor = connection.cursor() | ||
|
||
try: | ||
cursor.execute(query) | ||
wait(connection) | ||
|
||
column_names = [col.name for col in cursor.description] | ||
|
||
rows = [dict(zip(column_names, row)) for row in cursor] | ||
columns = [{'name': col.name, | ||
'friendly_name': column_friendly_name(col.name), | ||
'type': None} for col in cursor.description] | ||
|
||
data = {'columns': columns, 'rows': rows} | ||
json_data = json.dumps(data, cls=JSONEncoder) | ||
error = None | ||
cursor.close() | ||
except psycopg2.DatabaseError as e: | ||
json_data = None | ||
error = e.message | ||
except KeyboardInterrupt: | ||
connection.cancel() | ||
error = "Query cancelled by user." | ||
json_data = None | ||
except Exception as e: | ||
raise sys.exc_info()[1], None, sys.exc_info()[2] | ||
finally: | ||
connection.close() | ||
|
||
return json_data, error | ||
|
||
return query_runner |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You kept the mysql one here, but moved the redshift one to mysql_query_runner.py...