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

Refactor DML actions handling #73

Merged
merged 6 commits into from
Aug 31, 2024
Merged

Refactor DML actions handling #73

merged 6 commits into from
Aug 31, 2024

Conversation

jorgerojas26
Copy link
Owner

The main goal of this refactor is to improve code quality (a bit, i'm not super fluent in go) and allow for EMPTY, NULL and DEFAULT values to be used in the DML statements.

Please feel free to make suggestions.

@jorgerojas26 jorgerojas26 mentioned this pull request Jul 16, 2024
@jorgerojas26 jorgerojas26 marked this pull request as draft July 16, 2024 00:31
@jorgerojas26 jorgerojas26 mentioned this pull request Jul 19, 2024
tableName := stateChange.Value.(string)

tab := home.TabbedPane.GetTabByName(tableName)
tabReference := fmt.Sprintf("%s.%s", databaseName, tableName)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't you use .*ResultsTable. GetDatabaseAndTableName()

If not, it means maybe you could add a method to format db string, table string to format, then it would be called here and in GetDatabaseAndTableName

return nil
}

if command == commands.Edit {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use a switch here

Either to create two switch with

switch command {
     case commands.Edit:

     case commands....

     case commands....

     default:
}

```go
switch eventKey {

     case 4:
 
     case 21:

}

The second could be in the default of the first one if these switch have to be exclusive.

Or you could use the extended switch syntax

switch {
    case command == commands.Edit:

    case command == commands.…
    
     case eventKey = 4:

     case eventKey = 21:

      default:
}

Comment on lines +451 to +454
query := "DELETE FROM "
query += db.formatTableName(database, table)
query += fmt.Sprintf(" WHERE %s = ?", primaryKeyColumnName)
_, err := db.Connection.Exec(query, primaryKeyValue)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
query := "DELETE FROM "
query += db.formatTableName(database, table)
query += fmt.Sprintf(" WHERE %s = ?", primaryKeyColumnName)
_, err := db.Connection.Exec(query, primaryKeyValue)
query := fmt.Sprintf("DELETE FROM %s WHERE %s = ?", db.formatTableName(database, table), primaryKeyColumnName)
_, err := db.Connection.Exec(query, primaryKeyValue)

Copy link
Owner Author

Choose a reason for hiding this comment

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

The thing is gosec G201 rule, you think it should be disabled?

Copy link
Contributor

Choose a reason for hiding this comment

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

i think you fixed it the right way.

https://securego.io/docs/rules/g201-g202

I'm unsure if there is "clean way" when you are sprintf to set the table name

You may look at ORM how they do.

But now you understood how to write clean SQL queries with ? and arguments, maybe you could add a //nolint:gosec // disable it because of usage of table

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks for suggestion. I did look at how query builders like squirrel do it, and definitely they are not using Sprintf, they build the query writing to a "&bytes.Buffer{}" (see).

That's a good way, maybe i'll do it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Using a buffer is another way to contact string, it doesn't help at all with SQL injection.

You can use the buffer of course, as it's fast.

I think that's why they are using it

Copy link
Owner Author

Choose a reason for hiding this comment

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

By the time i'm more concerned about readability than SQL Injection, i think it does not makes sense (unless i'm missing something, if i do, please advice) to think that's a problem on a sql client, you will not self inject sql.

Copy link
Contributor

Choose a reason for hiding this comment

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

You fixed the biggest cause of bug by using ? caused by injection.

For example, when updating the content of a field that contains quote in a JSON.

Such as `{"foo":true}. With previous code, the update would be KO, as it would not escape the quotes.

It could cause data corruption and bug

Copy link
Owner Author

Choose a reason for hiding this comment

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

You are correct, using parameters we avoid those things.

Thank you, sir. :)

drivers/mysql.go Outdated
Comment on lines 505 to 507
queryStr := "INSERT INTO "
queryStr += db.formatTableName(change.Database, change.Table)
queryStr += fmt.Sprintf(" (%s) VALUES (%s)", strings.Join(columns, ", "), strings.Join(valuesPlaceholder, ", "))
Copy link
Contributor

Choose a reason for hiding this comment

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

You could/should a sprintf here to make things easier to read

See #73 (comment)

@ccoVeille
Copy link
Contributor

any progress? do you need help?

@jorgerojas26
Copy link
Owner Author

I just lost my main job, i'm busy right now preparing for interviews and those things. When I have more stability I will come back and get this done. Thanks for asking.

@ccoVeille
Copy link
Contributor

Wow, take care of you

@jorgerojas26 jorgerojas26 marked this pull request as ready for review August 25, 2024 19:42
@irdaislakhuafa
Copy link

I just lost my main job, i'm busy right now preparing for interviews and those things. When I have more stability I will come back and get this done. Thanks for asking.

goodluck bro

@jorgerojas26 jorgerojas26 changed the title [WIP] Refactor DML actions handling Refactor DML actions handling Aug 31, 2024
@jorgerojas26 jorgerojas26 merged commit d9469eb into main Aug 31, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants