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

feat: implement json search #882

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions src/masoniteorm/query/grammars/BaseGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,23 @@ def process_wheres(self, qmark=False, strip_first_where=False):

return sql

def _transform_json_column(self, column):
"""Transforms a json column into the correct syntax.

Arguments:
column {string} -- The column to transform.

Returns:
string
"""
json_split = column.split("->")
col_name = json_split[0]
json_path = json_split[1]

if "$." not in json_path:
json_path = f"$.{json_path}"
return col_name, f"->'{json_path}'"

def get_true_column_string(self):
return "{keyword} {column} = '1'"

Expand Down Expand Up @@ -881,12 +898,18 @@ def _table_column_string(self, column, alias=None, separator=""):
self
"""
table = None
json_path = ""

if column and "->" in column:
column, json_path = self._transform_json_column(column)

if column and "." in column:
table, column = column.split(".")

if column == "*":
return self.column_strings.get("select_all").format(
column=column,
json_path=json_path,
separator=separator,
table=self.process_table(table or self.table),
)
Expand All @@ -895,6 +918,7 @@ def _table_column_string(self, column, alias=None, separator=""):
alias_string = self.subquery_alias_string().format(alias=alias)
return self.column_strings.get(self._action).format(
column=column,
json_path=json_path,
separator=separator,
alias=" " + alias_string if alias else "",
table=self.process_table(table or self.table),
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/query/grammars/MySQLGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class MySQLGrammar(BaseGrammar):
"""

column_strings = {
"select": "{table}.`{column}`{alias}{separator}",
"select": "{table}.`{column}`{json_path}{alias}{separator}",
"select_all": "{table}.*{separator}",
"insert": "{table}.`{column}`{separator}",
"update": "{table}.`{column}`{separator}",
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/query/grammars/PostgresGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PostgresGrammar(BaseGrammar):
}

column_strings = {
"select": '{table}."{column}"{alias}{separator}',
"select": '{table}."{column}"{json_path}{alias}{separator}',
"select_all": "{table}.*{separator}",
"insert": '"{column}"{separator}',
"update": '"{column}"{separator}',
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/query/grammars/SQLiteGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SQLiteGrammar(BaseGrammar):
}

column_strings = {
"select": '{table}."{column}"{alias}{separator}',
"select": "{table}.`{column}`{json_path}{alias}{separator}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are are these " replaced with a ` symbol.

sqlite should be "" i think

"select_all": "{table}.*{separator}",
"insert": '"{column}"{separator}',
"update": '"{column}"{separator}',
Expand Down
10 changes: 10 additions & 0 deletions tests/mysql/grammar/test_mysql_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,13 @@ def or_where_null(self):

def select_distinct(self):
return """SELECT DISTINCT `users`.`group` FROM `users`"""

def test_can_compile_json_where(self):
to_sql = self.builder.where("options->age", 18).to_sql()
self.assertEqual(to_sql, "SELECT * FROM `users` WHERE `users`.`options`->'$.age' = '18'")

to_sql = self.builder.where("options->age", 18).to_sql()

def test_can_compile_json_select(self):
to_sql = self.builder.select("options->age", 'options').to_sql()
self.assertEqual(to_sql, "SELECT `users`.`options`->'$.age', `users`.`options` FROM `users`")
9 changes: 9 additions & 0 deletions tests/postgres/grammar/test_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,12 @@ def or_where_null(self):

def select_distinct(self):
return """SELECT DISTINCT "users"."group" FROM "users\""""

def test_can_compile_json_where(self):
to_sql = self.builder.where("options->age", 18).to_sql()
self.assertEqual(to_sql, "SELECT * FROM \"users\" WHERE \"users\".\"options\"->'$.age' = '18'")
to_sql = self.builder.where("options->age", 18).to_sql()

def test_can_compile_json_select(self):
to_sql = self.builder.select("options->age", 'options').to_sql()
self.assertEqual(to_sql, "SELECT \"users\".\"options\"->'$.age', \"users\".\"options\" FROM \"users\"")
10 changes: 10 additions & 0 deletions tests/sqlite/grammar/test_sqlite_select_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,13 @@ def or_where_null(self):

def select_distinct(self):
return """SELECT DISTINCT "users"."group" FROM "users\""""

def test_can_compile_json_where(self):
to_sql = self.builder.where("options->age", 18).to_sql()
self.assertEqual(to_sql, "SELECT * FROM \"users\" WHERE \"users\".`options`->'$.age' = '18'")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this syntax is invalid and i cant get this to run on sqlite

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird. I swear I tested it.


to_sql = self.builder.where("options->age", 18).to_sql()

def test_can_compile_json_select(self):
to_sql = self.builder.select("options->age", 'options').to_sql()
self.assertEqual(to_sql, "SELECT \"users\".`options`->'$.age', \"users\".`options` FROM \"users\"")
Loading