qustavo / sqlhooks

Attach hooks to any database/sql driver
MIT License
652 stars 44 forks source link

Issues with gorm #19

Closed dobegor closed 6 years ago

dobegor commented 6 years ago

I've recently tried out sqlhooks and encountered a problem:

(/Users/dobegor/go/src/smartback/v2/service/label_service.go:65)
[2018-05-23 14:51:15]  Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"labels"  WHERE (user_id = 0 AND is_system = 1 AND label IN (?,?,?,?))' at line 1

(/Users/dobegor/go/src/smartback/v2/service/label_service.go:65)
[2018-05-23 14:51:15]  [2.00ms]  SELECT * FROM "labels"  WHERE (user_id = 0 AND is_system = 1 AND label IN ('Inbox','Archive','Trash','Snoozed'))

As you can see, for some reason requests went into mysql without substitution. This only occurs if I wrap the driver with sqlhooks:


    sql.Register("mysqlWithHooks", sqlhooks.Wrap(&mysql.MySQLDriver{}, &Hooks{}))

    db, err := gorm.Open("mysqlWithHooks", config.DB.DSN())
    if err != nil {
        return stacktrace.Propagate(err, "Failed to connect to DB")
    }

I wonder if I'm doing something wrong here.

dobegor commented 6 years ago

I've just tried using https://github.com/ExpansiveWorlds/instrumentedsql and got absolutely same error. Does it have to do something with driver.NamedValue args?

WARN[May 23 15:00:21.891] sql-tx-begin [err <nil>]                      category=sql
WARN[May 23 15:00:21.893] sql-conn-query [query SELECT * FROM "labels"  WHERE (user_id = 0 AND is_system = 1 AND label IN (?,?,?,?)) args []driver.NamedValue{
    {
        Name:    "",
        Ordinal: 1,
        Value:   "Inbox",
    },
    {
        Name:    "",
        Ordinal: 2,
        Value:   "Archive",
    },
    {
        Name:    "",
        Ordinal: 3,
        Value:   "Trash",
    },
    {
        Name:    "",
        Ordinal: 4,
        Value:   "Snoozed",
    },
} err driver: skip fast-path; continue as if unimplemented]  category=sql
WARN[May 23 15:00:21.894] sql-prepare [err Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"labels"  WHERE (user_id = 0 AND is_system = 1 AND label IN (?,?,?,?))' at line 1]  category=sql

(/Users/dobegor/go/src/smartback/v2/service/label_service.go:65)
[2018-05-23 15:00:21]  Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"labels"  WHERE (user_id = 0 AND is_system = 1 AND label IN (?,?,?,?))' at line 1
keegancsmith commented 6 years ago

note that gorm.Open first parameter is dialect not driverName. It passes the dialect as the driverName to sql.Open though. If you look at the gorm source code is maintains a map from dialect to how it treats the strings. So you just need to tell gorm which dialect to use for mysqlWithHooks.

If I am not mistaken something like this should work:

dialect, ok := gorm.GetDialect("mysql")
if !ok {
  // oh noes
}
gorm.RegisterDialect("mysqlWithHooks", dialect)
dobegor commented 6 years ago

@keegancsmith Yes, thanks! Unfortunately, it's only in master branch, but thanks for the tip!