-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Add firebird query runner #6730
Open
dvandonkelaar
wants to merge
6
commits into
getredash:master
Choose a base branch
from
dvandonkelaar:feature/firebird_query_runner
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10e32a5
Add firebird query runner
dvandonkelaar f231c49
Add missing query_runner import
dvandonkelaar c1788fb
Add Firebird library files to Dockerfile
dvandonkelaar 0b0692e
Changed get_tables query to Firebird FAQ
dvandonkelaar 3838ccc
Fixed wrong firebird query_runner
dvandonkelaar 49a17bf
Removed testing logs
dvandonkelaar File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,152 @@ | ||
import datetime | ||
import logging | ||
|
||
from redash.query_runner import ( | ||
TYPE_DATE, | ||
TYPE_DATETIME, | ||
TYPE_FLOAT, | ||
TYPE_INTEGER, | ||
TYPE_STRING, | ||
BaseSQLQueryRunner, | ||
JobTimeoutException, | ||
register, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
try: | ||
import fdb | ||
|
||
enabled = True | ||
except ImportError: | ||
enabled = False | ||
|
||
types_map = { | ||
str: TYPE_STRING, | ||
int: TYPE_INTEGER, | ||
float: TYPE_FLOAT, | ||
datetime.date: TYPE_DATE, | ||
datetime.datetime: TYPE_DATETIME, | ||
} | ||
|
||
|
||
class firebird(BaseSQLQueryRunner): | ||
noop_query = "SELECT 1 FROM RDB$DATABASE;" | ||
|
||
limit_query = " FIRST 1000" | ||
limit_keywords = ["FIRST"] | ||
limit_after_select = True | ||
|
||
@classmethod | ||
def configuration_schema(cls): | ||
return { | ||
"type": "object", | ||
"properties": { | ||
"user": {"type": "string"}, | ||
"password": {"type": "string"}, | ||
"server": {"type": "string", "default": "127.0.0.1"}, | ||
"port": {"type": "number", "default": 3050}, | ||
"charset": { | ||
"type": "string", | ||
"default": "UTF-8", | ||
"title": "Character Set", | ||
}, | ||
"db": {"type": "string", "title": "Database Name"}, | ||
}, | ||
"required": ["db"], | ||
"secret": ["password"], | ||
} | ||
|
||
@classmethod | ||
def enabled(cls): | ||
return enabled | ||
|
||
@classmethod | ||
def name(cls): | ||
return "Firebird" | ||
|
||
@classmethod | ||
def type(cls): | ||
return "firebird" | ||
|
||
def _get_tables(self, schema): | ||
query = """ | ||
SELECT TRIM(f.rdb$relation_name) AS table_name | ||
,TRIM(f.rdb$field_name) AS column_name | ||
FROM rdb$relation_fields f | ||
JOIN rdb$relations r ON f.rdb$relation_name = r.rdb$relation_name | ||
AND r.rdb$view_blr IS NULL | ||
AND (r.rdb$system_flag IS NULL OR r.rdb$system_flag = 0) | ||
ORDER BY 1, f.rdb$field_position; | ||
""" | ||
|
||
results, error = self.run_query(query, None) | ||
|
||
if error is not None: | ||
self._handle_run_query_error(error) | ||
|
||
for row in results["rows"]: | ||
table_name = row["TABLE_NAME"] | ||
|
||
if table_name not in schema: | ||
schema[table_name] = {"name": table_name, "columns": []} | ||
|
||
schema[table_name]["columns"].append(row["COLUMN_NAME"]) | ||
|
||
return list(schema.values()) | ||
|
||
def run_query(self, query, user): | ||
connection = None | ||
|
||
try: | ||
server = self.configuration.get("server", "") | ||
user = self.configuration.get("user", "") | ||
password = self.configuration.get("password", "") | ||
db = self.configuration["db"] | ||
port = self.configuration.get("port", 3050) | ||
charset = self.configuration.get("charset", "UTF-8") | ||
|
||
if port != 3050: | ||
server = server + "/" + str(port) | ||
|
||
connection = fdb.connect( | ||
database=f"{server}:{db}", | ||
user=user, | ||
password=password, | ||
charset=charset, | ||
) | ||
|
||
if isinstance(query, str): | ||
query = query.encode(charset) | ||
|
||
cursor = connection.cursor() | ||
|
||
cursor.execute(query) | ||
data = cursor.fetchall() | ||
|
||
if cursor.description is not None: | ||
columns = self.fetch_columns([(i[0], types_map.get(i[1], None)) for i in cursor.description]) | ||
rows = [dict(zip((column["name"] for column in columns), row)) for row in data] | ||
|
||
data = {"columns": columns, "rows": rows} | ||
error = None | ||
else: | ||
error = "No data was returned." | ||
data = None | ||
|
||
cursor.close() | ||
connection.commit() | ||
except fdb.Error as e: | ||
error = e.args[0] | ||
data = None | ||
except (KeyboardInterrupt, JobTimeoutException): | ||
connection.cancel() | ||
raise | ||
finally: | ||
if connection: | ||
connection.close() | ||
|
||
return data, error | ||
|
||
|
||
register(firebird) |
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
Oops, something went wrong.
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.
Does this really need to be installed for a firebird connector to work? I'd imagine we can just ping an existing firebird db to query it - I don't think we'd have to actually install it in the redash image.
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.
Unfortunately, we need some client libraries to be able to connect to a Firebird database.
See the docs for more info.
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.
Got it, thanks.