-
-
Notifications
You must be signed in to change notification settings - Fork 58
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
fix: format table name to include backticks #67
Conversation
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.
I'm sorry because most of my feedbacks are not related to the current PR.
But I think I had to report them
@@ -313,6 +320,7 @@ func (db *MySQL) ExecuteQuery(query string) (results [][]string, err error) { | |||
|
|||
// TODO: Rewrites this logic to use the primary key instead of the id | |||
func (db *MySQL) UpdateRecord(table, column, value, primaryKeyColumnName, primaryKeyValue string) error { | |||
table = db.formatTableName(table) | |||
query := fmt.Sprintf("UPDATE %s SET %s = \"%s\" WHERE %s = \"%s\"", table, column, value, primaryKeyColumnName, primaryKeyValue) |
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.
query := fmt.Sprintf("UPDATE %s SET %s = \"%s\" WHERE %s = \"%s\"", table, column, value, primaryKeyColumnName, primaryKeyValue) | |
query := fmt.Sprintf(`UPDATE %s SET %s = "%s" WHERE %s = "%s"`, table, column, value, primaryKeyColumnName, primaryKeyValue) |
@@ -313,6 +320,7 @@ func (db *MySQL) ExecuteQuery(query string) (results [][]string, err error) { | |||
|
|||
// TODO: Rewrites this logic to use the primary key instead of the id | |||
func (db *MySQL) UpdateRecord(table, column, value, primaryKeyColumnName, primaryKeyValue string) error { | |||
table = db.formatTableName(table) | |||
query := fmt.Sprintf("UPDATE %s SET %s = \"%s\" WHERE %s = \"%s\"", table, column, value, primaryKeyColumnName, primaryKeyValue) |
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.
Also, I'm very worried about the possible SQL injection
You should consider using ?
for value, then pass value
to Exec as an arg
@@ -321,6 +329,7 @@ func (db *MySQL) UpdateRecord(table, column, value, primaryKeyColumnName, primar | |||
|
|||
// TODO: Rewrites this logic to use the primary key instead of the id | |||
func (db *MySQL) DeleteRecord(table, primaryKeyColumnName, primaryKeyValue string) error { | |||
table = db.formatTableName(table) | |||
query := fmt.Sprintf("DELETE FROM %s WHERE %s = \"%s\"", table, primaryKeyColumnName, primaryKeyValue) |
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.
Same concerns here
@@ -385,7 +394,7 @@ func (db *MySQL) ExecutePendingChanges(changes []models.DbDmlChange, inserts []m | |||
|
|||
statementType = "DELETE FROM" | |||
|
|||
query = fmt.Sprintf("%s %s WHERE %s = \"%s\"", statementType, delete.Table, delete.PrimaryKeyColumnName, delete.PrimaryKeyValue) | |||
query = fmt.Sprintf("%s %s WHERE %s = \"%s\"", statementType, db.formatTableName(delete.Table), delete.PrimaryKeyColumnName, delete.PrimaryKeyValue) |
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.
Same
@@ -405,7 +414,7 @@ func (db *MySQL) ExecutePendingChanges(changes []models.DbDmlChange, inserts []m | |||
} | |||
} | |||
|
|||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", insert.Table, strings.Join(insert.Columns, ", "), strings.Join(values, ", ")) | |||
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", db.formatTableName(insert.Table), strings.Join(insert.Columns, ", "), strings.Join(values, ", ")) |
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.
Here too the values should be passed as ?,?,?...
strings.Repeat could help
And values
should be passed to Exec
splittedTableName := strings.Split(tableName, ".") | ||
|
||
if len(splittedTableName) == 1 { | ||
return tableName |
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.
You could return this
return tableName | |
return fmt.Sprintf("`%s`", table) |
Thanks for reviewing the PR. May i ask why SQL Injection is a concern to you in this app when it is just a SQL client?. I totally understand that i should use prepared statements, i plan to make a huge refactor on all of the queries, but i want to solve the problem with a quick solution first. Does it makes sense to you? |
The table name formatting is really dirty, but it is a quick solution. That should be refactored too. My main idea is to get stable first, solve all the dirty bugs, then refactor, then add more features, like query history and saving, database/table input filter, theme system, only to name a few. |
I tend to always plan the worst. Often, I'm not disappointed and something close happens 😅
Not necessarily prepared statement, but using Exec the right way should be always preferred. |
Also please consider a table could have a varchar primary key, and that this field may contain a quote Something like It would be a bad design to rely on a varchar PK for this, but your app will be unable to work with this row |
By SQL injection I was more thinking about a source of bugs, than a attack. Maybe it's what you were asking for |
I understand. I appreciate the feedback. For now, i'll merge this PR just to resolve the connection problem and open another PR addressing your feedback. Thank you very much. |
Sure |
…ble-name fix: format table name to include backticks
Fixes #47