Skip to content
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

Fixed cast VARCHAR in Oracle #2228

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions flask_admin/contrib/sqla/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
from .typefmt import DEFAULT_FORMATTERS
from .ajax import create_ajax_loader

from sqlalchemy.dialects.oracle import VARCHAR, VARCHAR2, CHAR
from sqlalchemy.types import Integer, String

# Set up logger
log = logging.getLogger("flask-admin.sqla")

Expand Down Expand Up @@ -931,6 +934,15 @@ def _apply_sorting(self, query, joins, sort_column, sort_desc):

return query, joins

@staticmethod
def _safe_cast(column, type):
if isinstance(column.type, Integer):
return cast(column, type)
if not isinstance(column.type, VARCHAR) and not isinstance(column.type, VARCHAR2) and not isinstance(column.type, CHAR):
return cast(column, String(32))
else:
return column

def _apply_search(self, query, count_query, joins, count_joins, search):
"""
Apply search to a query.
Expand Down Expand Up @@ -958,11 +970,11 @@ def _apply_search(self, query, count_query, joins, count_joins, search):
inner_join=False)

column = field if alias is None else getattr(alias, field.key)
filter_stmt.append(cast(column, Unicode).ilike(stmt))
filter_stmt.append(self._safe_cast(column, Unicode).ilike(stmt))

if count_filter_stmt is not None:
column = field if count_alias is None else getattr(count_alias, field.key)
count_filter_stmt.append(cast(column, Unicode).ilike(stmt))
count_filter_stmt.append(self._safe_cast(column, Unicode).ilike(stmt))

query = query.filter(or_(*filter_stmt))

Expand Down