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

Is is possible to set mysql with "clientMultiStatements | clientMultiResults "? #89

Open
gpmn opened this issue Jan 12, 2014 · 7 comments

Comments

@gpmn
Copy link

gpmn commented Jan 12, 2014

database/sql do not support multi record set yet;
so everything will failed once after a stored procedure is called;

I found go-sql-driver has already fixed this problem; the solution is to add two option clientMultiStatements | clientMultiResults;

Is there anyway to add such options in mymysql? That will let my project to use stored procedures from database/sql layer.

@ziutek
Copy link
Owner

ziutek commented Jan 14, 2014

I believe that it is impossible to obtain more than one result set using database/sql.

mymysql sets CLIENT_MULTI_STATEMENTS and CLIENT_MULTI_RESULTS options for connection and you can read all results from multi-statement or procedure using mymysql specific interface but you can't do that using database/sql.

For current database/sql specification I see only some possible workarounds (eg. specify option to always return N-th result) but any doesn't address this problem for all possible cases.

@gpmn
Copy link
Author

gpmn commented Jan 16, 2014

I see....

And how to get mymysql's connection from database/sql layer?

I saw your example 8, I suppose I have to get the connection "my" at first, but don't know where I can get it from.

I'm so sorry to ask you so basic question .....

@ziutek
Copy link
Owner

ziutek commented Jan 16, 2014

You can't get connection from database/sql. It automatically manages connections and can do what it want with them (eg. close unused connection) so it is bad idea to mes there.

You can use both interfaces:

import (
"database/sql"
"github.com/ziutek/mymysql/mysql" // or "github.com/ziutek/mymysql/autorc"
)

You should avoid import thrsafe engine because it replaces default native engine for database/sql too (this doesn't break anything but adds some unnecessary overhead because database/sql doesn't require thread-safety from a driver).

Now you can:

db1 := mysql.New("tcp", "", "127.0.0.1:3306", user, pass, dbname)
err1 := db1.Connect()

db2, err2 := sql.Open("mymysql", "dbname/user/pass")

and use db1 and db2 where appropriate:

Of course, you should pay attention that db2 represents only one concrete connection, whereas db1 represents some dynamic pool of connections.

This doesn't look good and it seems to not be a good programming practice. So it should be limited to some small module that provides database abstraction for other code and can be easy rewritten in the feature.

@ziutek
Copy link
Owner

ziutek commented Jan 16, 2014

Should be: "Of course, you should pay attention that db1 represents only one concrete connection, whereas db2 represents some dynamic pool of connections."

And you should import some driver for database/sql (this can be "github.com/ziutek/mymysql/godrv" but you can use other too).

@gpmn
Copy link
Author

gpmn commented Jan 16, 2014

Thank your instruction!
That's a workaround,But it will established extra db connections beside database/sql's db connection, so I do not prefer it;

I have connected to the db through database/sql/sql.go,:
func Open(driverName, dataSourceName string) (*DB, error)

it will not a mymysql's connection;

So my question should be: Is it possible to get a mymysql's connection from an opened sql.DB connection and how?

@ziutek
Copy link
Owner

ziutek commented Jan 16, 2014

Multiple connection isn't any problem. database/sql usually establishes more than one connection. Did you read database/sql documentation?

My English is poor. Maybe you didn't understand what I wrote about database/sql connections.

If you carefully read database/sql documentation you should understand that there is no way to obtain any connection from pool without hack its code. Even if you hack database/sql to obtain some connection from pool you should do it that way to effectively remove it from pool otherwise it can be unexpectedly closed or you can break it by concurrent use.

@gpmn
Copy link
Author

gpmn commented Jan 17, 2014

I'm a newbie, :)

I learn golang and revel in my leisure time since last month lonely, so I do not understand so many details .....

And you clarify all my doubts, thank you so much;

(^^_)

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

No branches or pull requests

2 participants