Comments (21)
I would appreciate this and would help if I can. Can you give me a pointer on where to look at? I'm not sure I can do it, but then, I'll try at least.
from mysql.
Well, there are quite a few problems:
- MySQL has in fact two protocols for Queries: A string-based (for normal queries) and a binary protocol for prepared statements. Remember that we prepare every Query which has args internally. So to avoid duplicate code, the
time.Time
should be created inrows.Next
(filerows.go
). The column types are available inrows.content.columns[i].fieldType
time.Time
is localized (time zone), time / date values in MySQL aren't: At least we have no information about that. So we could either ask the user for the current time zone or just assume UTC, which is wrong in most cases and would lead to unexpected behavior if the user usesTime.In()
,Time.Location()
etc- The driver gets no information about the destination type:
So we don't know WHEN to return time.Time. If we would just return time.Time for every value in a column withDATETIME
type, this would return an unexpected result if you just want the value as a String. According to the MySQL specification this returns a string in the formatYYYY-MM-DD HH:MM:SS
. If we returntime.Time
and thedatabase/sql
package converts this back to a string (which adds unnecessary overhead) this would return a string in the formatYYYY-MM-DD HH:MM:SS +0000 UTC
instead, which a) doesn't conform with the specification and b) could break existing scripts.
At this point I can't imagine a good solutions without having information about the destination type. So I guess the database/sql
package needs a patch so that it gives us this information.
from mysql.
For now I added an example workaround to the wiki.
from mysql.
I guess this requires some serious magic. For DATE
and DATETIME
, it probably also depends on the STRICT MODE setting and can contain invalid dates even if STRICT MODE
was activated after inserting invalid dates. I think the best option is to return an error with the retrieved date in string format (so it can be salvaged if invalid dates are misused for signaling), but at least the rather common default case '0000-00-00' is problematic (that's not even covered by ALLOW_INVALID_DATES = 0
). TIMESTAMP
also has the '0000-00-00' issue.
from mysql.
About the timezone issue: you could sidestep that problem by advanced guessing (SELECT @@global.time_zone, @@session.time_zone
and/or comparing the client's system time with SELECT NOW()
). It would be messy - and you couldn't be 100% sure if server time and the local system time are wrong - but it should catch most timezone related problems for most users.
Still, I'd go with UTC for everything. Document it and leave the conversion to the user - he/she should know the timezone used to fill the database. Totally oblivious DB users probably wouldn't use a language like Go in the first place. Besides, UTC for everything is the only sane way to use date/time types in MySQL anyway.
from mysql.
I've got one more... does this help with your third issue?
Reflect example for *time.Time in interface{}
from mysql.
No, sorry. This doesn't change the fact, that the driver never directly comes in contact with the dest[]
slice (destination variables wrapped into interface{}
). The database/sql
package just asks the driver for the values and then converts + copies them.
The database/sql
package needs either a way to pass the destination values along or a way to pass the dest[]
slice along, in other words something like the Valuer
interface but for the destination values instead of args
.
from mysql.
An experimental branch with Scan() support for time.Time is now available.
I'm still not sure if this is the right approach since this makes it impossible to scan DATETIME / DATE to string, uint64 & co. Therefore it breaks compatibility with previous releases.
Any thoughts?
from mysql.
I think this would be best to implement as an optional feature, to be enabled by a feature flag in the connection string perhaps.
from mysql.
I tried the exp-time branch, it works fine. thanks.
from mysql.
When will exp-time branch merged to master?
I've used the exp-time branch with no problem.
from mysql.
It will be included in the first release candidate (scheduled for 2013-04-20), but it will be an optional behavior which must be activated via the DSN since this still has some limitations (e.g. with sql.RawBytes). The long-term plan is to make this work out-of-the-box, but this needs some work on the database/sql package.
from mysql.
Is there planned support for mysql time
type as well?
from mysql.
+1
from mysql.
@tigrang @ohohco no time type is planned because there is no matching type in Go.
time.Duration
gets close but is not one of the natively supported types in database/sql
.
from mysql.
Yeah,I have known now. You can use like this : add "the DSN parameter parseTime=true"
from mysql.
+1 @ohohco THANK YOU!!!!!!!!!!
setting the DNS param solved this problem for me. :)
user:pass@tcp(endpoint:3306)/dbname?parseTime=true
from mysql.
+1 @ohohco THANK YOU!!!!!!!!!!
?parseTime=true
worked for me with MySQL
from mysql.
+1 for the parseTime trick
from mysql.
+1 @ohohco Thanks a lot!!!
parseTime trick worked for me with MySQL
from mysql.
+1 ?parseTime=true
from mysql.
Related Issues (20)
- Cut a new v1.7.2 release HOT 14
- Potential out of bound access in `(*mysqlConn).handleErrorPacket` HOT 7
- Toggle Click in Leaderboards
- mysql startWatcher panic error HOT 3
- Bizarre hanging issue in rows.Close() HOT 7
- Can't Install, bug in edwards25519/scalar.go HOT 1
- Likely race that can prevent to recycle closed connections HOT 5
- Return timeout error instead of Invalid connection err when read packet network timeout HOT 5
- 保存时报错:Prepared statement contains too many placeholders
- too may ram usage when lost connection HOT 13
- SEGV writing packet HOT 1
- Connection liveness/goodeness check and AWS Aurora HOT 1
- Data race between mysqlConn watcher and okHandler during context cancellation HOT 3
- v1.6.0 get error
- Data race between mysqlConn.cleanup() and writeHandshakeResponsePacket HOT 2
- MaxOpenConns is not working
- Receiving "(using password: NO)" when password was given HOT 2
- Why must use port 3306?
- Scanner should return one of int64,float64,bool,[]byte,string,time.Time,nil , but I got uint64 HOT 3
- Do I need logic backup mysql data using mysqldump while I have master-slave mysql cluster.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mysql.