-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
orm: fix u8/i8 OID mapping and string default quoting #27989
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
base: master
Are you sure you want to change the base?
Changes from 8 commits
6426f76
ca7a767
026be35
5a23bd5
972a692
933c5c6
f61c62e
780851a
656c781
08492d6
96e5eeb
b0ad28e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -499,6 +499,25 @@ fn trim_attr_arg(arg string) string { | |
| return out | ||
| } | ||
|
|
||
| fn is_sql_expr(val string) bool { | ||
| // Function calls like gen_random_uuid(), NOW(), etc. | ||
| // Written as @[default: 'gen_random_uuid()'] — we detect the shape: | ||
| // must start with a letter/underscore, have ( and end with ), | ||
| // and the part before ( must be a valid SQL identifier. | ||
| if val.contains('(') && val.ends_with(')') { | ||
| before_paren := val.all_before('(') | ||
| if before_paren.len > 0 && (before_paren[0].is_letter() || before_paren[0] == `_`) { | ||
| for ch in before_paren { | ||
| if !ch.is_letter() && !ch.is_digit() && ch != `_` { | ||
| return false | ||
|
Comment on lines
+512
to
+514
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a PostgreSQL default calls a schema-qualified function, such as Useful? React with 👍 / 👎. |
||
| } | ||
| } | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| fn tenant_filter_array_primitive_type[T](value []T) int { | ||
| if value.len > 0 { | ||
| first := value[0] | ||
|
|
@@ -1417,6 +1436,7 @@ pub fn orm_table_gen(sql_dialect SQLDialect, table Table, q string, defaults boo | |
| } | ||
| mut default_val := field.default_val | ||
| mut has_default := default_val != '' | ||
| mut is_str_default := false | ||
| mut nullable := field.nullable | ||
| mut is_unique := false | ||
| mut is_skip := false | ||
|
|
@@ -1469,6 +1489,7 @@ pub fn orm_table_gen(sql_dialect SQLDialect, table Table, q string, defaults boo | |
| } | ||
| 'default' { | ||
| has_default = true | ||
| is_str_default = attr.kind == .string | ||
| if default_val == '' { | ||
| default_val = attr.arg.trim_space() | ||
| } | ||
|
|
@@ -1525,7 +1546,12 @@ pub fn orm_table_gen(sql_dialect SQLDialect, table Table, q string, defaults boo | |
| stmt = '${q}${field_name}${q} ${col_typ}' | ||
| if defaults && has_default { | ||
| if default_val != '' { | ||
| stmt += ' DEFAULT ${default_val}' | ||
| if is_str_default && !is_sql_expr(default_val) { | ||
| escaped := default_val.replace("'", "''") | ||
| stmt += " DEFAULT '${escaped}'" | ||
|
Comment on lines
+1561
to
+1563
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When this generator targets MySQL under its default SQL mode, escaping only apostrophes corrupts defaults containing backslash escape sequences. For example, a V default whose value is Useful? React with 👍 / 👎. |
||
| } else { | ||
| stmt += ' DEFAULT ${default_val}' | ||
|
Jengro777 marked this conversation as resolved.
|
||
| } | ||
| } else { | ||
| // Handle @[default: ''] - explicitly set DEFAULT '' for the column | ||
| stmt += " DEFAULT ''" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.