-
Notifications
You must be signed in to change notification settings - Fork 18
Fix loadjson failed because of sql params not match #65
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: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -49,13 +49,16 @@ func SqlInsertObject(ctx context.Context, db *sql.DB, obj any, tableNames ...Tab | |
| if len(rows) == 0 { | ||
| continue | ||
| } | ||
| placeholders := strings.Repeat("?,", rows[0].ColumnCount()) | ||
| // Get rid of the trailing comma. | ||
| placeholders = placeholders[:len(placeholders)-1] | ||
| sqlStmt := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", | ||
| table, strings.Join(rows[0].ColumnNames, ","), placeholders) | ||
| if len(rows) == 1 && rows[0].ColumnCount() <= 1 { | ||
| continue | ||
| } | ||
|
|
||
| for _, row := range rows { | ||
| placeholders := strings.Repeat("?,", row.ColumnCount()) | ||
| // Get rid of the trailing comma. | ||
| placeholders = placeholders[:len(placeholders)-1] | ||
| sqlStmt := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", | ||
| table, strings.Join(row.ColumnNames, ","), placeholders) | ||
|
Comment on lines
+57
to
+61
Author
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. This fixes cmd loadjson with the sample data
Collaborator
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. How can this happen? I don't understand
Author
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. the rows is an array of operator stats, when it extract the rows, the first row has 37 column with 37 values, and some of the other rows has 38 columns with 38 values, but the statement is created by the first row, that makes the values not matching for some of the other rows which have 38 columns |
||
| //log.Info().Str("sql", sqlStmt).Array("values", log.NewMarshaller(row.Values)).Msg("execute sql") | ||
| _, err := tx.Exec(sqlStmt, row.Values...) | ||
| if err != nil { | ||
|
|
||
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.
When data is empty, usually only has
query_id, insert will fail