mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge branch 'master' into style-changes-bulletfactory
Conflicts: pkg/api/index.go public/less/sidemenu.less
This commit is contained in:
commit
2ea548850e
@ -28,6 +28,7 @@
|
|||||||
|
|
||||||
"globals": {
|
"globals": {
|
||||||
"System": true,
|
"System": true,
|
||||||
|
"Promise": true,
|
||||||
"define": true,
|
"define": true,
|
||||||
"require": true,
|
"require": true,
|
||||||
"Chromath": false,
|
"Chromath": false,
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
* **Metadata**: Settings panel now shows dashboard metadata, closes [#3304](https://github.com/grafana/grafana/issues/3304)
|
* **Metadata**: Settings panel now shows dashboard metadata, closes [#3304](https://github.com/grafana/grafana/issues/3304)
|
||||||
* **InfluxDB**: Support for policy selection in query editor, closes [#2018](https://github.com/grafana/grafana/issues/2018)
|
* **InfluxDB**: Support for policy selection in query editor, closes [#2018](https://github.com/grafana/grafana/issues/2018)
|
||||||
* **Snapshots UI**: Dashboard snapshots list can be managed through UI, closes[#1984](https://github.com/grafana/grafana/issues/1984)
|
* **Snapshots UI**: Dashboard snapshots list can be managed through UI, closes[#1984](https://github.com/grafana/grafana/issues/1984)
|
||||||
|
* **Prometheus**: Prometheus annotation support, closes[#2883](https://github.com/grafana/grafana/pull/2883)
|
||||||
|
|
||||||
### Breaking changes
|
### Breaking changes
|
||||||
* **Plugin API**: Both datasource and panel plugin api (and plugin.json schema) have been updated, requiring an update to plugins. See [plugin api](https://github.com/grafana/grafana/blob/master/public/app/plugins/plugin_api.md) for more info.
|
* **Plugin API**: Both datasource and panel plugin api (and plugin.json schema) have been updated, requiring an update to plugins. See [plugin api](https://github.com/grafana/grafana/blob/master/public/app/plugins/plugin_api.md) for more info.
|
||||||
@ -16,6 +17,8 @@
|
|||||||
* **mysql**: Grafana now supports ssl for mysql, closes [#3584](https://github.com/grafana/grafana/issues/3584)
|
* **mysql**: Grafana now supports ssl for mysql, closes [#3584](https://github.com/grafana/grafana/issues/3584)
|
||||||
* **snapshot**: Annotations are now included in snapshots, closes [#3635](https://github.com/grafana/grafana/issues/3635)
|
* **snapshot**: Annotations are now included in snapshots, closes [#3635](https://github.com/grafana/grafana/issues/3635)
|
||||||
* **Admin**: Admin can now have global overview of Grafana setup, closes [#3812](https://github.com/grafana/grafana/issues/3812)
|
* **Admin**: Admin can now have global overview of Grafana setup, closes [#3812](https://github.com/grafana/grafana/issues/3812)
|
||||||
|
* **graph**: Right side legend height is now fixed at row height, closes [#1277](https://github.com/grafana/grafana/issues/1277)
|
||||||
|
* **Table**: All content in table panel is now html escaped, closes [#3673](https://github.com/grafana/grafana/issues/3673)
|
||||||
|
|
||||||
### Bug fixes
|
### Bug fixes
|
||||||
* **Playlist**: Fix for memory leak when running a playlist, closes [#3794](https://github.com/grafana/grafana/pull/3794)
|
* **Playlist**: Fix for memory leak when running a playlist, closes [#3794](https://github.com/grafana/grafana/pull/3794)
|
||||||
|
13
Godeps/Godeps.json
generated
13
Godeps/Godeps.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"ImportPath": "github.com/grafana/grafana",
|
"ImportPath": "github.com/grafana/grafana",
|
||||||
"GoVersion": "go1.5",
|
"GoVersion": "go1.5.1",
|
||||||
"Packages": [
|
"Packages": [
|
||||||
"./pkg/..."
|
"./pkg/..."
|
||||||
],
|
],
|
||||||
@ -101,17 +101,18 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/go-sql-driver/mysql",
|
"ImportPath": "github.com/go-sql-driver/mysql",
|
||||||
"Comment": "v1.2-26-g9543750",
|
"Comment": "v1.2-171-g267b128",
|
||||||
"Rev": "9543750295406ef070f7de8ae9c43ccddd44e15e"
|
"Rev": "267b128680c46286b9ca13475c3cca5de8f79bd7"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/go-xorm/core",
|
"ImportPath": "github.com/go-xorm/core",
|
||||||
"Rev": "be6e7ac47dc57bd0ada25322fa526944f66ccaa6"
|
"Comment": "v0.4.4-7-g9e608f7",
|
||||||
|
"Rev": "9e608f7330b9d16fe2818cfe731128b3f156cb9a"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/go-xorm/xorm",
|
"ImportPath": "github.com/go-xorm/xorm",
|
||||||
"Comment": "v0.4.2-58-ge2889e5",
|
"Comment": "v0.4.4-44-gf561133",
|
||||||
"Rev": "e2889e5517600b82905f1d2ba8b70deb71823ffe"
|
"Rev": "f56113384f2c63dfe4cd8e768e349f1c35122b58"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"ImportPath": "github.com/gosimple/slug",
|
"ImportPath": "github.com/gosimple/slug",
|
||||||
|
4
Godeps/_workspace/src/github.com/go-sql-driver/mysql/.travis.yml
generated
vendored
4
Godeps/_workspace/src/github.com/go-sql-driver/mysql/.travis.yml
generated
vendored
@ -1,8 +1,10 @@
|
|||||||
|
sudo: false
|
||||||
language: go
|
language: go
|
||||||
go:
|
go:
|
||||||
- 1.1
|
|
||||||
- 1.2
|
- 1.2
|
||||||
- 1.3
|
- 1.3
|
||||||
|
- 1.4
|
||||||
|
- 1.5
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
|
14
Godeps/_workspace/src/github.com/go-sql-driver/mysql/AUTHORS
generated
vendored
14
Godeps/_workspace/src/github.com/go-sql-driver/mysql/AUTHORS
generated
vendored
@ -14,19 +14,32 @@
|
|||||||
Aaron Hopkins <go-sql-driver at die.net>
|
Aaron Hopkins <go-sql-driver at die.net>
|
||||||
Arne Hormann <arnehormann at gmail.com>
|
Arne Hormann <arnehormann at gmail.com>
|
||||||
Carlos Nieto <jose.carlos at menteslibres.net>
|
Carlos Nieto <jose.carlos at menteslibres.net>
|
||||||
|
Chris Moos <chris at tech9computers.com>
|
||||||
|
Daniel Nichter <nil at codenode.com>
|
||||||
DisposaBoy <disposaboy at dby.me>
|
DisposaBoy <disposaboy at dby.me>
|
||||||
Frederick Mayle <frederickmayle at gmail.com>
|
Frederick Mayle <frederickmayle at gmail.com>
|
||||||
Gustavo Kristic <gkristic at gmail.com>
|
Gustavo Kristic <gkristic at gmail.com>
|
||||||
Hanno Braun <mail at hannobraun.com>
|
Hanno Braun <mail at hannobraun.com>
|
||||||
Henri Yandell <flamefew at gmail.com>
|
Henri Yandell <flamefew at gmail.com>
|
||||||
|
Hirotaka Yamamoto <ymmt2005 at gmail.com>
|
||||||
|
INADA Naoki <songofacandy at gmail.com>
|
||||||
James Harr <james.harr at gmail.com>
|
James Harr <james.harr at gmail.com>
|
||||||
Jian Zhen <zhenjl at gmail.com>
|
Jian Zhen <zhenjl at gmail.com>
|
||||||
|
Joshua Prunier <joshua.prunier at gmail.com>
|
||||||
|
Julien Lefevre <julien.lefevr at gmail.com>
|
||||||
Julien Schmidt <go-sql-driver at julienschmidt.com>
|
Julien Schmidt <go-sql-driver at julienschmidt.com>
|
||||||
|
Kamil Dziedzic <kamil at klecza.pl>
|
||||||
|
Kevin Malachowski <kevin at chowski.com>
|
||||||
Leonardo YongUk Kim <dalinaum at gmail.com>
|
Leonardo YongUk Kim <dalinaum at gmail.com>
|
||||||
|
Luca Looz <luca.looz92 at gmail.com>
|
||||||
Lucas Liu <extrafliu at gmail.com>
|
Lucas Liu <extrafliu at gmail.com>
|
||||||
Luke Scott <luke at webconnex.com>
|
Luke Scott <luke at webconnex.com>
|
||||||
Michael Woolnough <michael.woolnough at gmail.com>
|
Michael Woolnough <michael.woolnough at gmail.com>
|
||||||
Nicola Peduzzi <thenikso at gmail.com>
|
Nicola Peduzzi <thenikso at gmail.com>
|
||||||
|
Runrioter Wung <runrioter at gmail.com>
|
||||||
|
Soroush Pour <me at soroushjp.com>
|
||||||
|
Stan Putrya <root.vagner at gmail.com>
|
||||||
|
Stanley Gunawan <gunawan.stanley at gmail.com>
|
||||||
Xiaobing Jiang <s7v7nislands at gmail.com>
|
Xiaobing Jiang <s7v7nislands at gmail.com>
|
||||||
Xiuming Chen <cc at cxm.cc>
|
Xiuming Chen <cc at cxm.cc>
|
||||||
|
|
||||||
@ -34,3 +47,4 @@ Xiuming Chen <cc at cxm.cc>
|
|||||||
|
|
||||||
Barracuda Networks, Inc.
|
Barracuda Networks, Inc.
|
||||||
Google Inc.
|
Google Inc.
|
||||||
|
Stripe Inc.
|
||||||
|
24
Godeps/_workspace/src/github.com/go-sql-driver/mysql/CHANGELOG.md
generated
vendored
24
Godeps/_workspace/src/github.com/go-sql-driver/mysql/CHANGELOG.md
generated
vendored
@ -2,11 +2,31 @@
|
|||||||
|
|
||||||
Changes:
|
Changes:
|
||||||
|
|
||||||
- Use decimals field from MySQL to format time types
|
- Go 1.1 is no longer supported
|
||||||
|
- Use decimals field from MySQL to format time types (#249)
|
||||||
|
- Buffer optimizations (#269)
|
||||||
|
- TLS ServerName defaults to the host (#283)
|
||||||
|
|
||||||
Bugfixes:
|
Bugfixes:
|
||||||
|
|
||||||
- Enable microsecond resolution on TIME, DATETIME and TIMESTAMP
|
- Enable microsecond resolution on TIME, DATETIME and TIMESTAMP (#249)
|
||||||
|
- Fixed handling of queries without columns and rows (#255)
|
||||||
|
- Fixed a panic when SetKeepAlive() failed (#298)
|
||||||
|
- Support receiving ERR packet while reading rows (#321)
|
||||||
|
- Fixed reading NULL length-encoded integers in MySQL 5.6+ (#349)
|
||||||
|
- Fixed absolute paths support in LOAD LOCAL DATA INFILE (#356)
|
||||||
|
- Actually zero out bytes in handshake response (#378)
|
||||||
|
- Fixed race condition in registering LOAD DATA INFILE handler (#383)
|
||||||
|
- Fixed tests with MySQL 5.7.9+ (#380)
|
||||||
|
- QueryUnescape TLS config names (#397)
|
||||||
|
- Fixed "broken pipe" error by writing to closed socket (#390)
|
||||||
|
|
||||||
|
New Features:
|
||||||
|
- Support for returning table alias on Columns() (#289, #359, #382)
|
||||||
|
- Placeholder interpolation, can be actived with the DSN parameter `interpolateParams=true` (#309, #318)
|
||||||
|
- Support for uint64 parameters with high bit set (#332, #345)
|
||||||
|
- Cleartext authentication plugin support (#327)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## Version 1.2 (2014-06-03)
|
## Version 1.2 (2014-06-03)
|
||||||
|
82
Godeps/_workspace/src/github.com/go-sql-driver/mysql/README.md
generated
vendored
82
Godeps/_workspace/src/github.com/go-sql-driver/mysql/README.md
generated
vendored
@ -30,7 +30,7 @@ A MySQL-Driver for Go's [database/sql](http://golang.org/pkg/database/sql) packa
|
|||||||
## Features
|
## Features
|
||||||
* Lightweight and [fast](https://github.com/go-sql-driver/sql-benchmark "golang MySQL-Driver performance")
|
* Lightweight and [fast](https://github.com/go-sql-driver/sql-benchmark "golang MySQL-Driver performance")
|
||||||
* Native Go implementation. No C-bindings, just pure Go
|
* Native Go implementation. No C-bindings, just pure Go
|
||||||
* Connections over TCP/IPv4, TCP/IPv6 or Unix domain sockets
|
* Connections over TCP/IPv4, TCP/IPv6, Unix domain sockets or [custom protocols](http://godoc.org/github.com/go-sql-driver/mysql#DialFunc)
|
||||||
* Automatic handling of broken connections
|
* Automatic handling of broken connections
|
||||||
* Automatic Connection Pooling *(by database/sql package)*
|
* Automatic Connection Pooling *(by database/sql package)*
|
||||||
* Supports queries larger than 16MB
|
* Supports queries larger than 16MB
|
||||||
@ -38,9 +38,10 @@ A MySQL-Driver for Go's [database/sql](http://golang.org/pkg/database/sql) packa
|
|||||||
* Intelligent `LONG DATA` handling in prepared statements
|
* Intelligent `LONG DATA` handling in prepared statements
|
||||||
* Secure `LOAD DATA LOCAL INFILE` support with file Whitelisting and `io.Reader` support
|
* Secure `LOAD DATA LOCAL INFILE` support with file Whitelisting and `io.Reader` support
|
||||||
* Optional `time.Time` parsing
|
* Optional `time.Time` parsing
|
||||||
|
* Optional placeholder interpolation
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
* Go 1.1 or higher
|
* Go 1.2 or higher
|
||||||
* MySQL (4.1+), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3+)
|
* MySQL (4.1+), MariaDB, Percona Server, Google CloudSQL or Sphinx (2.2.3+)
|
||||||
|
|
||||||
---------------------------------------
|
---------------------------------------
|
||||||
@ -92,6 +93,8 @@ This has the same effect as an empty DSN string:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Alternatively, [Config.FormatDSN](https://godoc.org/github.com/go-sql-driver/mysql#Config.FormatDSN) can be used to create a DSN string by filling a struct.
|
||||||
|
|
||||||
#### Password
|
#### Password
|
||||||
Passwords can consist of any character. Escaping is **not** necessary.
|
Passwords can consist of any character. Escaping is **not** necessary.
|
||||||
|
|
||||||
@ -122,6 +125,16 @@ Default: false
|
|||||||
`allowAllFiles=true` disables the file Whitelist for `LOAD DATA LOCAL INFILE` and allows *all* files.
|
`allowAllFiles=true` disables the file Whitelist for `LOAD DATA LOCAL INFILE` and allows *all* files.
|
||||||
[*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)
|
[*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)
|
||||||
|
|
||||||
|
##### `allowCleartextPasswords`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
`allowCleartextPasswords=true` allows using the [cleartext client side plugin](http://dev.mysql.com/doc/en/cleartext-authentication-plugin.html) if required by an account, such as one defined with the [PAM authentication plugin](http://dev.mysql.com/doc/en/pam-authentication-plugin.html). Sending passwords in clear text may be a security problem in some configurations. To avoid problems if there is any possibility that the password would be intercepted, clients should connect to MySQL Server using a method that protects the password. Possibilities include [TLS / SSL](#tls), IPsec, or a private network.
|
||||||
|
|
||||||
##### `allowOldPasswords`
|
##### `allowOldPasswords`
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -166,6 +179,33 @@ Default: false
|
|||||||
|
|
||||||
`clientFoundRows=true` causes an UPDATE to return the number of matching rows instead of the number of rows changed.
|
`clientFoundRows=true` causes an UPDATE to return the number of matching rows instead of the number of rows changed.
|
||||||
|
|
||||||
|
##### `columnsWithAlias`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
When `columnsWithAlias` is true, calls to `sql.Rows.Columns()` will return the table alias and the column name separated by a dot. For example:
|
||||||
|
|
||||||
|
```
|
||||||
|
SELECT u.id FROM users as u
|
||||||
|
```
|
||||||
|
|
||||||
|
will return `u.id` instead of just `id` if `columnsWithAlias=true`.
|
||||||
|
|
||||||
|
##### `interpolateParams`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
If `interpolateParams` is true, placeholders (`?`) in calls to `db.Query()` and `db.Exec()` are interpolated into a single query string with given parameters. This reduces the number of roundtrips, since the driver has to prepare a statement, execute it with given parameters and close the statement again with `interpolateParams=false`.
|
||||||
|
|
||||||
|
*This can not be used together with the multibyte encodings BIG5, CP932, GB2312, GBK or SJIS. These are blacklisted as they may [introduce a SQL injection vulnerability](http://stackoverflow.com/a/12118602/3430118)!*
|
||||||
|
|
||||||
##### `loc`
|
##### `loc`
|
||||||
|
|
||||||
@ -177,8 +217,20 @@ Default: UTC
|
|||||||
|
|
||||||
Sets the location for time.Time values (when using `parseTime=true`). *"Local"* sets the system's location. See [time.LoadLocation](http://golang.org/pkg/time/#LoadLocation) for details.
|
Sets the location for time.Time values (when using `parseTime=true`). *"Local"* sets the system's location. See [time.LoadLocation](http://golang.org/pkg/time/#LoadLocation) for details.
|
||||||
|
|
||||||
|
Note that this sets the location for time.Time values but does not change MySQL's [time_zone setting](https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html). For that see the [time_zone system variable](#system-variables), which can also be set as a DSN parameter.
|
||||||
|
|
||||||
Please keep in mind, that param values must be [url.QueryEscape](http://golang.org/pkg/net/url/#QueryEscape)'ed. Alternatively you can manually replace the `/` with `%2F`. For example `US/Pacific` would be `loc=US%2FPacific`.
|
Please keep in mind, that param values must be [url.QueryEscape](http://golang.org/pkg/net/url/#QueryEscape)'ed. Alternatively you can manually replace the `/` with `%2F`. For example `US/Pacific` would be `loc=US%2FPacific`.
|
||||||
|
|
||||||
|
##### `multiStatements`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: bool
|
||||||
|
Valid Values: true, false
|
||||||
|
Default: false
|
||||||
|
```
|
||||||
|
|
||||||
|
Allow multiple statements in one query. While this allows batch queries, it also greatly increases the risk of SQL injections. Only the result of the first query is returned, all other results are silently discarded.
|
||||||
|
|
||||||
|
|
||||||
##### `parseTime`
|
##### `parseTime`
|
||||||
|
|
||||||
@ -191,6 +243,16 @@ Default: false
|
|||||||
`parseTime=true` changes the output type of `DATE` and `DATETIME` values to `time.Time` instead of `[]byte` / `string`
|
`parseTime=true` changes the output type of `DATE` and `DATETIME` values to `time.Time` instead of `[]byte` / `string`
|
||||||
|
|
||||||
|
|
||||||
|
##### `readTimeout`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: decimal number
|
||||||
|
Default: 0
|
||||||
|
```
|
||||||
|
|
||||||
|
I/O read timeout. The value must be a decimal number with an unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*.
|
||||||
|
|
||||||
|
|
||||||
##### `strict`
|
##### `strict`
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -211,7 +273,7 @@ Type: decimal number
|
|||||||
Default: OS default
|
Default: OS default
|
||||||
```
|
```
|
||||||
|
|
||||||
*Driver* side connection timeout. The value must be a string of decimal numbers, each with optional fraction and a unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*. To set a server side timeout, use the parameter [`wait_timeout`](http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_wait_timeout).
|
*Driver* side connection timeout. The value must be a decimal number with an unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*. To set a server side timeout, use the parameter [`wait_timeout`](http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_wait_timeout).
|
||||||
|
|
||||||
|
|
||||||
##### `tls`
|
##### `tls`
|
||||||
@ -225,11 +287,21 @@ Default: false
|
|||||||
`tls=true` enables TLS / SSL encrypted connection to the server. Use `skip-verify` if you want to use a self-signed or invalid certificate (server side). Use a custom value registered with [`mysql.RegisterTLSConfig`](http://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).
|
`tls=true` enables TLS / SSL encrypted connection to the server. Use `skip-verify` if you want to use a self-signed or invalid certificate (server side). Use a custom value registered with [`mysql.RegisterTLSConfig`](http://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig).
|
||||||
|
|
||||||
|
|
||||||
|
##### `writeTimeout`
|
||||||
|
|
||||||
|
```
|
||||||
|
Type: decimal number
|
||||||
|
Default: 0
|
||||||
|
```
|
||||||
|
|
||||||
|
I/O write timeout. The value must be a decimal number with an unit suffix ( *"ms"*, *"s"*, *"m"*, *"h"* ), such as *"30s"*, *"0.5m"* or *"1m30s"*.
|
||||||
|
|
||||||
|
|
||||||
##### System Variables
|
##### System Variables
|
||||||
|
|
||||||
All other parameters are interpreted as system variables:
|
All other parameters are interpreted as system variables:
|
||||||
* `autocommit`: `"SET autocommit=<value>"`
|
* `autocommit`: `"SET autocommit=<value>"`
|
||||||
* `time_zone`: `"SET time_zone=<value>"`
|
* [`time_zone`](https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html): `"SET time_zone=<value>"`
|
||||||
* [`tx_isolation`](https://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_tx_isolation): `"SET tx_isolation=<value>"`
|
* [`tx_isolation`](https://dev.mysql.com/doc/refman/5.5/en/server-system-variables.html#sysvar_tx_isolation): `"SET tx_isolation=<value>"`
|
||||||
* `param`: `"SET <param>=<value>"`
|
* `param`: `"SET <param>=<value>"`
|
||||||
|
|
||||||
@ -291,7 +363,7 @@ import "github.com/go-sql-driver/mysql"
|
|||||||
|
|
||||||
Files must be whitelisted by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the Whitelist check must be deactivated by using the DSN parameter `allowAllFiles=true` ([*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)).
|
Files must be whitelisted by registering them with `mysql.RegisterLocalFile(filepath)` (recommended) or the Whitelist check must be deactivated by using the DSN parameter `allowAllFiles=true` ([*Might be insecure!*](http://dev.mysql.com/doc/refman/5.7/en/load-data-local.html)).
|
||||||
|
|
||||||
To use a `io.Reader` a handler function must be registered with `mysql.RegisterReaderHandler(name, handler)` which returns a `io.Reader` or `io.ReadCloser`. The Reader is available with the filepath `Reader::<name>` then.
|
To use a `io.Reader` a handler function must be registered with `mysql.RegisterReaderHandler(name, handler)` which returns a `io.Reader` or `io.ReadCloser`. The Reader is available with the filepath `Reader::<name>` then. Choose different names for different handlers and `DeregisterReaderHandler` when you don't need it anymore.
|
||||||
|
|
||||||
See the [godoc of Go-MySQL-Driver](http://godoc.org/github.com/go-sql-driver/mysql "golang mysql driver documentation") for details.
|
See the [godoc of Go-MySQL-Driver](http://godoc.org/github.com/go-sql-driver/mysql "golang mysql driver documentation") for details.
|
||||||
|
|
||||||
|
40
Godeps/_workspace/src/github.com/go-sql-driver/mysql/benchmark_test.go
generated
vendored
40
Godeps/_workspace/src/github.com/go-sql-driver/mysql/benchmark_test.go
generated
vendored
@ -11,10 +11,13 @@ package mysql
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"database/sql/driver"
|
||||||
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TB testing.B
|
type TB testing.B
|
||||||
@ -45,7 +48,11 @@ func initDB(b *testing.B, queries ...string) *sql.DB {
|
|||||||
db := tb.checkDB(sql.Open("mysql", dsn))
|
db := tb.checkDB(sql.Open("mysql", dsn))
|
||||||
for _, query := range queries {
|
for _, query := range queries {
|
||||||
if _, err := db.Exec(query); err != nil {
|
if _, err := db.Exec(query); err != nil {
|
||||||
b.Fatalf("Error on %q: %v", query, err)
|
if w, ok := err.(MySQLWarnings); ok {
|
||||||
|
b.Logf("warning on %q: %v", query, w)
|
||||||
|
} else {
|
||||||
|
b.Fatalf("error on %q: %v", query, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return db
|
return db
|
||||||
@ -206,3 +213,34 @@ func BenchmarkRoundtripBin(b *testing.B) {
|
|||||||
rows.Close()
|
rows.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkInterpolation(b *testing.B) {
|
||||||
|
mc := &mysqlConn{
|
||||||
|
cfg: &Config{
|
||||||
|
InterpolateParams: true,
|
||||||
|
Loc: time.UTC,
|
||||||
|
},
|
||||||
|
maxPacketAllowed: maxPacketSize,
|
||||||
|
maxWriteSize: maxPacketSize - 1,
|
||||||
|
buf: newBuffer(nil),
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []driver.Value{
|
||||||
|
int64(42424242),
|
||||||
|
float64(math.Pi),
|
||||||
|
false,
|
||||||
|
time.Unix(1423411542, 807015000),
|
||||||
|
[]byte("bytes containing special chars ' \" \a \x00"),
|
||||||
|
"string containing special chars ' \" \a \x00",
|
||||||
|
}
|
||||||
|
q := "SELECT ?, ?, ?, ?, ?, ?"
|
||||||
|
|
||||||
|
b.ReportAllocs()
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, err := mc.interpolateParams(q, args)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
21
Godeps/_workspace/src/github.com/go-sql-driver/mysql/buffer.go
generated
vendored
21
Godeps/_workspace/src/github.com/go-sql-driver/mysql/buffer.go
generated
vendored
@ -8,7 +8,11 @@
|
|||||||
|
|
||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import "io"
|
import (
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
const defaultBufSize = 4096
|
const defaultBufSize = 4096
|
||||||
|
|
||||||
@ -19,16 +23,17 @@ const defaultBufSize = 4096
|
|||||||
// Also highly optimized for this particular use case.
|
// Also highly optimized for this particular use case.
|
||||||
type buffer struct {
|
type buffer struct {
|
||||||
buf []byte
|
buf []byte
|
||||||
rd io.Reader
|
nc net.Conn
|
||||||
idx int
|
idx int
|
||||||
length int
|
length int
|
||||||
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBuffer(rd io.Reader) buffer {
|
func newBuffer(nc net.Conn) buffer {
|
||||||
var b [defaultBufSize]byte
|
var b [defaultBufSize]byte
|
||||||
return buffer{
|
return buffer{
|
||||||
buf: b[:],
|
buf: b[:],
|
||||||
rd: rd,
|
nc: nc,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,7 +59,13 @@ func (b *buffer) fill(need int) error {
|
|||||||
b.idx = 0
|
b.idx = 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
nn, err := b.rd.Read(b.buf[n:])
|
if b.timeout > 0 {
|
||||||
|
if err := b.nc.SetReadDeadline(time.Now().Add(b.timeout)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nn, err := b.nc.Read(b.buf[n:])
|
||||||
n += nn
|
n += nn
|
||||||
|
|
||||||
switch err {
|
switch err {
|
||||||
|
16
Godeps/_workspace/src/github.com/go-sql-driver/mysql/collations.go
generated
vendored
16
Godeps/_workspace/src/github.com/go-sql-driver/mysql/collations.go
generated
vendored
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
const defaultCollation byte = 33 // utf8_general_ci
|
const defaultCollation = "utf8_general_ci"
|
||||||
|
|
||||||
// A list of available collations mapped to the internal ID.
|
// A list of available collations mapped to the internal ID.
|
||||||
// To update this map use the following MySQL query:
|
// To update this map use the following MySQL query:
|
||||||
@ -234,3 +234,17 @@ var collations = map[string]byte{
|
|||||||
"utf8mb4_unicode_520_ci": 246,
|
"utf8mb4_unicode_520_ci": 246,
|
||||||
"utf8mb4_vietnamese_ci": 247,
|
"utf8mb4_vietnamese_ci": 247,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A blacklist of collations which is unsafe to interpolate parameters.
|
||||||
|
// These multibyte encodings may contains 0x5c (`\`) in their trailing bytes.
|
||||||
|
var unsafeCollations = map[string]bool{
|
||||||
|
"big5_chinese_ci": true,
|
||||||
|
"sjis_japanese_ci": true,
|
||||||
|
"gbk_chinese_ci": true,
|
||||||
|
"big5_bin": true,
|
||||||
|
"gb2312_bin": true,
|
||||||
|
"gbk_bin": true,
|
||||||
|
"sjis_bin": true,
|
||||||
|
"cp932_japanese_ci": true,
|
||||||
|
"cp932_bin": true,
|
||||||
|
}
|
||||||
|
224
Godeps/_workspace/src/github.com/go-sql-driver/mysql/connection.go
generated
vendored
224
Godeps/_workspace/src/github.com/go-sql-driver/mysql/connection.go
generated
vendored
@ -9,10 +9,9 @@
|
|||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"errors"
|
|
||||||
"net"
|
"net"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -22,34 +21,20 @@ type mysqlConn struct {
|
|||||||
netConn net.Conn
|
netConn net.Conn
|
||||||
affectedRows uint64
|
affectedRows uint64
|
||||||
insertId uint64
|
insertId uint64
|
||||||
cfg *config
|
cfg *Config
|
||||||
maxPacketAllowed int
|
maxPacketAllowed int
|
||||||
maxWriteSize int
|
maxWriteSize int
|
||||||
|
writeTimeout time.Duration
|
||||||
flags clientFlag
|
flags clientFlag
|
||||||
|
status statusFlag
|
||||||
sequence uint8
|
sequence uint8
|
||||||
parseTime bool
|
parseTime bool
|
||||||
strict bool
|
strict bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type config struct {
|
|
||||||
user string
|
|
||||||
passwd string
|
|
||||||
net string
|
|
||||||
addr string
|
|
||||||
dbname string
|
|
||||||
params map[string]string
|
|
||||||
loc *time.Location
|
|
||||||
tls *tls.Config
|
|
||||||
timeout time.Duration
|
|
||||||
collation uint8
|
|
||||||
allowAllFiles bool
|
|
||||||
allowOldPasswords bool
|
|
||||||
clientFoundRows bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handles parameters set in DSN after the connection is established
|
// Handles parameters set in DSN after the connection is established
|
||||||
func (mc *mysqlConn) handleParams() (err error) {
|
func (mc *mysqlConn) handleParams() (err error) {
|
||||||
for param, val := range mc.cfg.params {
|
for param, val := range mc.cfg.Params {
|
||||||
switch param {
|
switch param {
|
||||||
// Charset
|
// Charset
|
||||||
case "charset":
|
case "charset":
|
||||||
@ -65,27 +50,6 @@ func (mc *mysqlConn) handleParams() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// time.Time parsing
|
|
||||||
case "parseTime":
|
|
||||||
var isBool bool
|
|
||||||
mc.parseTime, isBool = readBool(val)
|
|
||||||
if !isBool {
|
|
||||||
return errors.New("Invalid Bool value: " + val)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strict mode
|
|
||||||
case "strict":
|
|
||||||
var isBool bool
|
|
||||||
mc.strict, isBool = readBool(val)
|
|
||||||
if !isBool {
|
|
||||||
return errors.New("Invalid Bool value: " + val)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compression
|
|
||||||
case "compress":
|
|
||||||
err = errors.New("Compression not implemented yet")
|
|
||||||
return
|
|
||||||
|
|
||||||
// System Vars
|
// System Vars
|
||||||
default:
|
default:
|
||||||
err = mc.exec("SET " + param + "=" + val + "")
|
err = mc.exec("SET " + param + "=" + val + "")
|
||||||
@ -115,18 +79,27 @@ func (mc *mysqlConn) Close() (err error) {
|
|||||||
// Makes Close idempotent
|
// Makes Close idempotent
|
||||||
if mc.netConn != nil {
|
if mc.netConn != nil {
|
||||||
err = mc.writeCommandPacket(comQuit)
|
err = mc.writeCommandPacket(comQuit)
|
||||||
if err == nil {
|
}
|
||||||
err = mc.netConn.Close()
|
|
||||||
} else {
|
mc.cleanup()
|
||||||
mc.netConn.Close()
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Closes the network connection and unsets internal variables. Do not call this
|
||||||
|
// function after successfully authentication, call Close instead. This function
|
||||||
|
// is called before auth or on auth failure because MySQL will have already
|
||||||
|
// closed the network connection.
|
||||||
|
func (mc *mysqlConn) cleanup() {
|
||||||
|
// Makes cleanup idempotent
|
||||||
|
if mc.netConn != nil {
|
||||||
|
if err := mc.netConn.Close(); err != nil {
|
||||||
|
errLog.Print(err)
|
||||||
}
|
}
|
||||||
mc.netConn = nil
|
mc.netConn = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
mc.cfg = nil
|
mc.cfg = nil
|
||||||
mc.buf.rd = nil
|
mc.buf.nc = nil
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
|
func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
|
||||||
@ -161,12 +134,140 @@ func (mc *mysqlConn) Prepare(query string) (driver.Stmt, error) {
|
|||||||
return stmt, err
|
return stmt, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) interpolateParams(query string, args []driver.Value) (string, error) {
|
||||||
|
buf := mc.buf.takeCompleteBuffer()
|
||||||
|
if buf == nil {
|
||||||
|
// can not take the buffer. Something must be wrong with the connection
|
||||||
|
errLog.Print(ErrBusyBuffer)
|
||||||
|
return "", driver.ErrBadConn
|
||||||
|
}
|
||||||
|
buf = buf[:0]
|
||||||
|
argPos := 0
|
||||||
|
|
||||||
|
for i := 0; i < len(query); i++ {
|
||||||
|
q := strings.IndexByte(query[i:], '?')
|
||||||
|
if q == -1 {
|
||||||
|
buf = append(buf, query[i:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
buf = append(buf, query[i:i+q]...)
|
||||||
|
i += q
|
||||||
|
|
||||||
|
arg := args[argPos]
|
||||||
|
argPos++
|
||||||
|
|
||||||
|
if arg == nil {
|
||||||
|
buf = append(buf, "NULL"...)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
switch v := arg.(type) {
|
||||||
|
case int64:
|
||||||
|
buf = strconv.AppendInt(buf, v, 10)
|
||||||
|
case float64:
|
||||||
|
buf = strconv.AppendFloat(buf, v, 'g', -1, 64)
|
||||||
|
case bool:
|
||||||
|
if v {
|
||||||
|
buf = append(buf, '1')
|
||||||
|
} else {
|
||||||
|
buf = append(buf, '0')
|
||||||
|
}
|
||||||
|
case time.Time:
|
||||||
|
if v.IsZero() {
|
||||||
|
buf = append(buf, "'0000-00-00'"...)
|
||||||
|
} else {
|
||||||
|
v := v.In(mc.cfg.Loc)
|
||||||
|
v = v.Add(time.Nanosecond * 500) // To round under microsecond
|
||||||
|
year := v.Year()
|
||||||
|
year100 := year / 100
|
||||||
|
year1 := year % 100
|
||||||
|
month := v.Month()
|
||||||
|
day := v.Day()
|
||||||
|
hour := v.Hour()
|
||||||
|
minute := v.Minute()
|
||||||
|
second := v.Second()
|
||||||
|
micro := v.Nanosecond() / 1000
|
||||||
|
|
||||||
|
buf = append(buf, []byte{
|
||||||
|
'\'',
|
||||||
|
digits10[year100], digits01[year100],
|
||||||
|
digits10[year1], digits01[year1],
|
||||||
|
'-',
|
||||||
|
digits10[month], digits01[month],
|
||||||
|
'-',
|
||||||
|
digits10[day], digits01[day],
|
||||||
|
' ',
|
||||||
|
digits10[hour], digits01[hour],
|
||||||
|
':',
|
||||||
|
digits10[minute], digits01[minute],
|
||||||
|
':',
|
||||||
|
digits10[second], digits01[second],
|
||||||
|
}...)
|
||||||
|
|
||||||
|
if micro != 0 {
|
||||||
|
micro10000 := micro / 10000
|
||||||
|
micro100 := micro / 100 % 100
|
||||||
|
micro1 := micro % 100
|
||||||
|
buf = append(buf, []byte{
|
||||||
|
'.',
|
||||||
|
digits10[micro10000], digits01[micro10000],
|
||||||
|
digits10[micro100], digits01[micro100],
|
||||||
|
digits10[micro1], digits01[micro1],
|
||||||
|
}...)
|
||||||
|
}
|
||||||
|
buf = append(buf, '\'')
|
||||||
|
}
|
||||||
|
case []byte:
|
||||||
|
if v == nil {
|
||||||
|
buf = append(buf, "NULL"...)
|
||||||
|
} else {
|
||||||
|
buf = append(buf, "_binary'"...)
|
||||||
|
if mc.status&statusNoBackslashEscapes == 0 {
|
||||||
|
buf = escapeBytesBackslash(buf, v)
|
||||||
|
} else {
|
||||||
|
buf = escapeBytesQuotes(buf, v)
|
||||||
|
}
|
||||||
|
buf = append(buf, '\'')
|
||||||
|
}
|
||||||
|
case string:
|
||||||
|
buf = append(buf, '\'')
|
||||||
|
if mc.status&statusNoBackslashEscapes == 0 {
|
||||||
|
buf = escapeStringBackslash(buf, v)
|
||||||
|
} else {
|
||||||
|
buf = escapeStringQuotes(buf, v)
|
||||||
|
}
|
||||||
|
buf = append(buf, '\'')
|
||||||
|
default:
|
||||||
|
return "", driver.ErrSkip
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(buf)+4 > mc.maxPacketAllowed {
|
||||||
|
return "", driver.ErrSkip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if argPos != len(args) {
|
||||||
|
return "", driver.ErrSkip
|
||||||
|
}
|
||||||
|
return string(buf), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, error) {
|
||||||
if mc.netConn == nil {
|
if mc.netConn == nil {
|
||||||
errLog.Print(ErrInvalidConn)
|
errLog.Print(ErrInvalidConn)
|
||||||
return nil, driver.ErrBadConn
|
return nil, driver.ErrBadConn
|
||||||
}
|
}
|
||||||
if len(args) == 0 { // no args, fastpath
|
if len(args) != 0 {
|
||||||
|
if !mc.cfg.InterpolateParams {
|
||||||
|
return nil, driver.ErrSkip
|
||||||
|
}
|
||||||
|
// try to interpolate the parameters to save extra roundtrips for preparing and closing a statement
|
||||||
|
prepared, err := mc.interpolateParams(query, args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query = prepared
|
||||||
|
args = nil
|
||||||
|
}
|
||||||
mc.affectedRows = 0
|
mc.affectedRows = 0
|
||||||
mc.insertId = 0
|
mc.insertId = 0
|
||||||
|
|
||||||
@ -180,11 +281,6 @@ func (mc *mysqlConn) Exec(query string, args []driver.Value) (driver.Result, err
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// with args, must use prepared stmt
|
|
||||||
return nil, driver.ErrSkip
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Internal function to execute commands
|
// Internal function to execute commands
|
||||||
func (mc *mysqlConn) exec(query string) error {
|
func (mc *mysqlConn) exec(query string) error {
|
||||||
// Send command
|
// Send command
|
||||||
@ -211,7 +307,18 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
|
|||||||
errLog.Print(ErrInvalidConn)
|
errLog.Print(ErrInvalidConn)
|
||||||
return nil, driver.ErrBadConn
|
return nil, driver.ErrBadConn
|
||||||
}
|
}
|
||||||
if len(args) == 0 { // no args, fastpath
|
if len(args) != 0 {
|
||||||
|
if !mc.cfg.InterpolateParams {
|
||||||
|
return nil, driver.ErrSkip
|
||||||
|
}
|
||||||
|
// try client-side prepare to reduce roundtrip
|
||||||
|
prepared, err := mc.interpolateParams(query, args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
query = prepared
|
||||||
|
args = nil
|
||||||
|
}
|
||||||
// Send command
|
// Send command
|
||||||
err := mc.writeCommandPacketStr(comQuery, query)
|
err := mc.writeCommandPacketStr(comQuery, query)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -234,10 +341,6 @@ func (mc *mysqlConn) Query(query string, args []driver.Value) (driver.Rows, erro
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// with args, must use prepared stmt
|
|
||||||
return nil, driver.ErrSkip
|
|
||||||
}
|
|
||||||
|
|
||||||
// Gets the value of the given MySQL System Variable
|
// Gets the value of the given MySQL System Variable
|
||||||
// The returned byte slice is only valid until the next read
|
// The returned byte slice is only valid until the next read
|
||||||
func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
|
func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
|
||||||
@ -251,6 +354,7 @@ func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
rows := new(textRows)
|
rows := new(textRows)
|
||||||
rows.mc = mc
|
rows.mc = mc
|
||||||
|
rows.columns = []mysqlField{{fieldType: fieldTypeVarChar}}
|
||||||
|
|
||||||
if resLen > 0 {
|
if resLen > 0 {
|
||||||
// Columns
|
// Columns
|
||||||
|
35
Godeps/_workspace/src/github.com/go-sql-driver/mysql/const.go
generated
vendored
35
Godeps/_workspace/src/github.com/go-sql-driver/mysql/const.go
generated
vendored
@ -24,6 +24,7 @@ const (
|
|||||||
iERR byte = 0xff
|
iERR byte = 0xff
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// https://dev.mysql.com/doc/internals/en/capability-flags.html#packet-Protocol::CapabilityFlags
|
||||||
type clientFlag uint32
|
type clientFlag uint32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -45,6 +46,13 @@ const (
|
|||||||
clientSecureConn
|
clientSecureConn
|
||||||
clientMultiStatements
|
clientMultiStatements
|
||||||
clientMultiResults
|
clientMultiResults
|
||||||
|
clientPSMultiResults
|
||||||
|
clientPluginAuth
|
||||||
|
clientConnectAttrs
|
||||||
|
clientPluginAuthLenEncClientData
|
||||||
|
clientCanHandleExpiredPasswords
|
||||||
|
clientSessionTrack
|
||||||
|
clientDeprecateEOF
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -68,7 +76,7 @@ const (
|
|||||||
comBinlogDump
|
comBinlogDump
|
||||||
comTableDump
|
comTableDump
|
||||||
comConnectOut
|
comConnectOut
|
||||||
comRegiserSlave
|
comRegisterSlave
|
||||||
comStmtPrepare
|
comStmtPrepare
|
||||||
comStmtExecute
|
comStmtExecute
|
||||||
comStmtSendLongData
|
comStmtSendLongData
|
||||||
@ -78,6 +86,7 @@ const (
|
|||||||
comStmtFetch
|
comStmtFetch
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// https://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnType
|
||||||
const (
|
const (
|
||||||
fieldTypeDecimal byte = iota
|
fieldTypeDecimal byte = iota
|
||||||
fieldTypeTiny
|
fieldTypeTiny
|
||||||
@ -98,7 +107,8 @@ const (
|
|||||||
fieldTypeBit
|
fieldTypeBit
|
||||||
)
|
)
|
||||||
const (
|
const (
|
||||||
fieldTypeNewDecimal byte = iota + 0xf6
|
fieldTypeJSON byte = iota + 0xf5
|
||||||
|
fieldTypeNewDecimal
|
||||||
fieldTypeEnum
|
fieldTypeEnum
|
||||||
fieldTypeSet
|
fieldTypeSet
|
||||||
fieldTypeTinyBLOB
|
fieldTypeTinyBLOB
|
||||||
@ -130,3 +140,24 @@ const (
|
|||||||
flagUnknown3
|
flagUnknown3
|
||||||
flagUnknown4
|
flagUnknown4
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// http://dev.mysql.com/doc/internals/en/status-flags.html
|
||||||
|
type statusFlag uint16
|
||||||
|
|
||||||
|
const (
|
||||||
|
statusInTrans statusFlag = 1 << iota
|
||||||
|
statusInAutocommit
|
||||||
|
statusReserved // Not in documentation
|
||||||
|
statusMoreResultsExists
|
||||||
|
statusNoGoodIndexUsed
|
||||||
|
statusNoIndexUsed
|
||||||
|
statusCursorExists
|
||||||
|
statusLastRowSent
|
||||||
|
statusDbDropped
|
||||||
|
statusNoBackslashEscapes
|
||||||
|
statusMetadataChanged
|
||||||
|
statusQueryWasSlow
|
||||||
|
statusPsOutParams
|
||||||
|
statusInTransReadonly
|
||||||
|
statusSessionStateChanged
|
||||||
|
)
|
||||||
|
83
Godeps/_workspace/src/github.com/go-sql-driver/mysql/driver.go
generated
vendored
83
Godeps/_workspace/src/github.com/go-sql-driver/mysql/driver.go
generated
vendored
@ -4,7 +4,7 @@
|
|||||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
// Package mysql provides a MySQL driver for Go's database/sql package
|
||||||
//
|
//
|
||||||
// The driver should be used via the database/sql package:
|
// The driver should be used via the database/sql package:
|
||||||
//
|
//
|
||||||
@ -22,7 +22,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
// This struct is exported to make the driver directly accessible.
|
// MySQLDriver is exported to make the driver directly accessible.
|
||||||
// In general the driver is used via the database/sql package.
|
// In general the driver is used via the database/sql package.
|
||||||
type MySQLDriver struct{}
|
type MySQLDriver struct{}
|
||||||
|
|
||||||
@ -53,17 +53,19 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
maxPacketAllowed: maxPacketSize,
|
maxPacketAllowed: maxPacketSize,
|
||||||
maxWriteSize: maxPacketSize - 1,
|
maxWriteSize: maxPacketSize - 1,
|
||||||
}
|
}
|
||||||
mc.cfg, err = parseDSN(dsn)
|
mc.cfg, err = ParseDSN(dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
mc.parseTime = mc.cfg.ParseTime
|
||||||
|
mc.strict = mc.cfg.Strict
|
||||||
|
|
||||||
// Connect to Server
|
// Connect to Server
|
||||||
if dial, ok := dials[mc.cfg.net]; ok {
|
if dial, ok := dials[mc.cfg.Net]; ok {
|
||||||
mc.netConn, err = dial(mc.cfg.addr)
|
mc.netConn, err = dial(mc.cfg.Addr)
|
||||||
} else {
|
} else {
|
||||||
nd := net.Dialer{Timeout: mc.cfg.timeout}
|
nd := net.Dialer{Timeout: mc.cfg.Timeout}
|
||||||
mc.netConn, err = nd.Dial(mc.cfg.net, mc.cfg.addr)
|
mc.netConn, err = nd.Dial(mc.cfg.Net, mc.cfg.Addr)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -72,45 +74,40 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
// Enable TCP Keepalives on TCP connections
|
// Enable TCP Keepalives on TCP connections
|
||||||
if tc, ok := mc.netConn.(*net.TCPConn); ok {
|
if tc, ok := mc.netConn.(*net.TCPConn); ok {
|
||||||
if err := tc.SetKeepAlive(true); err != nil {
|
if err := tc.SetKeepAlive(true); err != nil {
|
||||||
mc.Close()
|
// Don't send COM_QUIT before handshake.
|
||||||
|
mc.netConn.Close()
|
||||||
|
mc.netConn = nil
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mc.buf = newBuffer(mc.netConn)
|
mc.buf = newBuffer(mc.netConn)
|
||||||
|
|
||||||
|
// Set I/O timeouts
|
||||||
|
mc.buf.timeout = mc.cfg.ReadTimeout
|
||||||
|
mc.writeTimeout = mc.cfg.WriteTimeout
|
||||||
|
|
||||||
// Reading Handshake Initialization Packet
|
// Reading Handshake Initialization Packet
|
||||||
cipher, err := mc.readInitPacket()
|
cipher, err := mc.readInitPacket()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
mc.Close()
|
mc.cleanup()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send Client Authentication Packet
|
// Send Client Authentication Packet
|
||||||
if err = mc.writeAuthPacket(cipher); err != nil {
|
if err = mc.writeAuthPacket(cipher); err != nil {
|
||||||
mc.Close()
|
mc.cleanup()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read Result Packet
|
// Handle response to auth packet, switch methods if possible
|
||||||
err = mc.readResultOK()
|
if err = handleAuthResult(mc, cipher); err != nil {
|
||||||
if err != nil {
|
// Authentication failed and MySQL has already closed the connection
|
||||||
// Retry with old authentication method, if allowed
|
// (https://dev.mysql.com/doc/internals/en/authentication-fails.html).
|
||||||
if mc.cfg != nil && mc.cfg.allowOldPasswords && err == ErrOldPassword {
|
// Do not send COM_QUIT, just cleanup and return the error.
|
||||||
if err = mc.writeOldAuthPacket(cipher); err != nil {
|
mc.cleanup()
|
||||||
mc.Close()
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if err = mc.readResultOK(); err != nil {
|
|
||||||
mc.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
mc.Close()
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get max allowed packet size
|
// Get max allowed packet size
|
||||||
maxap, err := mc.getSystemVar("max_allowed_packet")
|
maxap, err := mc.getSystemVar("max_allowed_packet")
|
||||||
@ -133,6 +130,38 @@ func (d MySQLDriver) Open(dsn string) (driver.Conn, error) {
|
|||||||
return mc, nil
|
return mc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func handleAuthResult(mc *mysqlConn, cipher []byte) error {
|
||||||
|
// Read Result Packet
|
||||||
|
err := mc.readResultOK()
|
||||||
|
if err == nil {
|
||||||
|
return nil // auth successful
|
||||||
|
}
|
||||||
|
|
||||||
|
if mc.cfg == nil {
|
||||||
|
return err // auth failed and retry not possible
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retry auth if configured to do so.
|
||||||
|
if mc.cfg.AllowOldPasswords && err == ErrOldPassword {
|
||||||
|
// Retry with old authentication method. Note: there are edge cases
|
||||||
|
// where this should work but doesn't; this is currently "wontfix":
|
||||||
|
// https://github.com/go-sql-driver/mysql/issues/184
|
||||||
|
if err = mc.writeOldAuthPacket(cipher); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = mc.readResultOK()
|
||||||
|
} else if mc.cfg.AllowCleartextPasswords && err == ErrCleartextPassword {
|
||||||
|
// Retry with clear text password for
|
||||||
|
// http://dev.mysql.com/doc/refman/5.7/en/cleartext-authentication-plugin.html
|
||||||
|
// http://dev.mysql.com/doc/refman/5.7/en/pam-authentication-plugin.html
|
||||||
|
if err = mc.writeClearAuthPacket(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = mc.readResultOK()
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
sql.Register("mysql", &MySQLDriver{})
|
sql.Register("mysql", &MySQLDriver{})
|
||||||
}
|
}
|
||||||
|
435
Godeps/_workspace/src/github.com/go-sql-driver/mysql/driver_test.go
generated
vendored
435
Godeps/_workspace/src/github.com/go-sql-driver/mysql/driver_test.go
generated
vendored
@ -9,12 +9,14 @@
|
|||||||
package mysql
|
package mysql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@ -74,18 +76,20 @@ type DBTest struct {
|
|||||||
db *sql.DB
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func runTests(t *testing.T, dsn string, tests ...func(dbt *DBTest)) {
|
func runTestsWithMultiStatement(t *testing.T, dsn string, tests ...func(dbt *DBTest)) {
|
||||||
if !available {
|
if !available {
|
||||||
t.Skipf("MySQL-Server not running on %s", netAddr)
|
t.Skipf("MySQL server not running on %s", netAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("mysql", dsn)
|
dsn += "&multiStatements=true"
|
||||||
|
var db *sql.DB
|
||||||
|
if _, err := ParseDSN(dsn); err != errInvalidDSNUnsafeCollation {
|
||||||
|
db, err = sql.Open("mysql", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error connecting: %s", err.Error())
|
t.Fatalf("error connecting: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
}
|
||||||
db.Exec("DROP TABLE IF EXISTS test")
|
|
||||||
|
|
||||||
dbt := &DBTest{t, db}
|
dbt := &DBTest{t, db}
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
@ -94,17 +98,67 @@ func runTests(t *testing.T, dsn string, tests ...func(dbt *DBTest)) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func runTests(t *testing.T, dsn string, tests ...func(dbt *DBTest)) {
|
||||||
|
if !available {
|
||||||
|
t.Skipf("MySQL server not running on %s", netAddr)
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sql.Open("mysql", dsn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error connecting: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
db.Exec("DROP TABLE IF EXISTS test")
|
||||||
|
|
||||||
|
dsn2 := dsn + "&interpolateParams=true"
|
||||||
|
var db2 *sql.DB
|
||||||
|
if _, err := ParseDSN(dsn2); err != errInvalidDSNUnsafeCollation {
|
||||||
|
db2, err = sql.Open("mysql", dsn2)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error connecting: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer db2.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
dsn3 := dsn + "&multiStatements=true"
|
||||||
|
var db3 *sql.DB
|
||||||
|
if _, err := ParseDSN(dsn3); err != errInvalidDSNUnsafeCollation {
|
||||||
|
db3, err = sql.Open("mysql", dsn3)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error connecting: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer db3.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
dbt := &DBTest{t, db}
|
||||||
|
dbt2 := &DBTest{t, db2}
|
||||||
|
dbt3 := &DBTest{t, db3}
|
||||||
|
for _, test := range tests {
|
||||||
|
test(dbt)
|
||||||
|
dbt.db.Exec("DROP TABLE IF EXISTS test")
|
||||||
|
if db2 != nil {
|
||||||
|
test(dbt2)
|
||||||
|
dbt2.db.Exec("DROP TABLE IF EXISTS test")
|
||||||
|
}
|
||||||
|
if db3 != nil {
|
||||||
|
test(dbt3)
|
||||||
|
dbt3.db.Exec("DROP TABLE IF EXISTS test")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (dbt *DBTest) fail(method, query string, err error) {
|
func (dbt *DBTest) fail(method, query string, err error) {
|
||||||
if len(query) > 300 {
|
if len(query) > 300 {
|
||||||
query = "[query too large to print]"
|
query = "[query too large to print]"
|
||||||
}
|
}
|
||||||
dbt.Fatalf("Error on %s %s: %s", method, query, err.Error())
|
dbt.Fatalf("error on %s %s: %s", method, query, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dbt *DBTest) mustExec(query string, args ...interface{}) (res sql.Result) {
|
func (dbt *DBTest) mustExec(query string, args ...interface{}) (res sql.Result) {
|
||||||
res, err := dbt.db.Exec(query, args...)
|
res, err := dbt.db.Exec(query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.fail("Exec", query, err)
|
dbt.fail("exec", query, err)
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@ -112,7 +166,7 @@ func (dbt *DBTest) mustExec(query string, args ...interface{}) (res sql.Result)
|
|||||||
func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows) {
|
func (dbt *DBTest) mustQuery(query string, args ...interface{}) (rows *sql.Rows) {
|
||||||
rows, err := dbt.db.Query(query, args...)
|
rows, err := dbt.db.Query(query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.fail("Query", query, err)
|
dbt.fail("query", query, err)
|
||||||
}
|
}
|
||||||
return rows
|
return rows
|
||||||
}
|
}
|
||||||
@ -123,7 +177,7 @@ func TestEmptyQuery(t *testing.T) {
|
|||||||
rows := dbt.mustQuery("--")
|
rows := dbt.mustQuery("--")
|
||||||
// will hang before #255
|
// will hang before #255
|
||||||
if rows.Next() {
|
if rows.Next() {
|
||||||
dbt.Errorf("Next on rows must be false")
|
dbt.Errorf("next on rows must be false")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -147,7 +201,7 @@ func TestCRUD(t *testing.T) {
|
|||||||
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
||||||
}
|
}
|
||||||
if count != 1 {
|
if count != 1 {
|
||||||
dbt.Fatalf("Expected 1 affected row, got %d", count)
|
dbt.Fatalf("expected 1 affected row, got %d", count)
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := res.LastInsertId()
|
id, err := res.LastInsertId()
|
||||||
@ -155,7 +209,7 @@ func TestCRUD(t *testing.T) {
|
|||||||
dbt.Fatalf("res.LastInsertId() returned error: %s", err.Error())
|
dbt.Fatalf("res.LastInsertId() returned error: %s", err.Error())
|
||||||
}
|
}
|
||||||
if id != 0 {
|
if id != 0 {
|
||||||
dbt.Fatalf("Expected InsertID 0, got %d", id)
|
dbt.Fatalf("expected InsertId 0, got %d", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read
|
// Read
|
||||||
@ -180,7 +234,7 @@ func TestCRUD(t *testing.T) {
|
|||||||
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
||||||
}
|
}
|
||||||
if count != 1 {
|
if count != 1 {
|
||||||
dbt.Fatalf("Expected 1 affected row, got %d", count)
|
dbt.Fatalf("expected 1 affected row, got %d", count)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check Update
|
// Check Update
|
||||||
@ -205,7 +259,7 @@ func TestCRUD(t *testing.T) {
|
|||||||
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
||||||
}
|
}
|
||||||
if count != 1 {
|
if count != 1 {
|
||||||
dbt.Fatalf("Expected 1 affected row, got %d", count)
|
dbt.Fatalf("expected 1 affected row, got %d", count)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for unexpected rows
|
// Check for unexpected rows
|
||||||
@ -215,11 +269,55 @@ func TestCRUD(t *testing.T) {
|
|||||||
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
||||||
}
|
}
|
||||||
if count != 0 {
|
if count != 0 {
|
||||||
dbt.Fatalf("Expected 0 affected row, got %d", count)
|
dbt.Fatalf("expected 0 affected row, got %d", count)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMultiQuery(t *testing.T) {
|
||||||
|
runTestsWithMultiStatement(t, dsn, func(dbt *DBTest) {
|
||||||
|
// Create Table
|
||||||
|
dbt.mustExec("CREATE TABLE `test` (`id` int(11) NOT NULL, `value` int(11) NOT NULL) ")
|
||||||
|
|
||||||
|
// Create Data
|
||||||
|
res := dbt.mustExec("INSERT INTO test VALUES (1, 1)")
|
||||||
|
count, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
||||||
|
}
|
||||||
|
if count != 1 {
|
||||||
|
dbt.Fatalf("expected 1 affected row, got %d", count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update
|
||||||
|
res = dbt.mustExec("UPDATE test SET value = 3 WHERE id = 1; UPDATE test SET value = 4 WHERE id = 1; UPDATE test SET value = 5 WHERE id = 1;")
|
||||||
|
count, err = res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
|
||||||
|
}
|
||||||
|
if count != 1 {
|
||||||
|
dbt.Fatalf("expected 1 affected row, got %d", count)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read
|
||||||
|
var out int
|
||||||
|
rows := dbt.mustQuery("SELECT value FROM test WHERE id=1;")
|
||||||
|
if rows.Next() {
|
||||||
|
rows.Scan(&out)
|
||||||
|
if 5 != out {
|
||||||
|
dbt.Errorf("5 != %t", out)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rows.Next() {
|
||||||
|
dbt.Error("unexpected data")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dbt.Error("no data")
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestInt(t *testing.T) {
|
func TestInt(t *testing.T) {
|
||||||
runTests(t, dsn, func(dbt *DBTest) {
|
runTests(t, dsn, func(dbt *DBTest) {
|
||||||
types := [5]string{"TINYINT", "SMALLINT", "MEDIUMINT", "INT", "BIGINT"}
|
types := [5]string{"TINYINT", "SMALLINT", "MEDIUMINT", "INT", "BIGINT"}
|
||||||
@ -636,14 +734,14 @@ func TestNULL(t *testing.T) {
|
|||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if nb.Valid {
|
if nb.Valid {
|
||||||
dbt.Error("Valid NullBool which should be invalid")
|
dbt.Error("valid NullBool which should be invalid")
|
||||||
}
|
}
|
||||||
// Valid
|
// Valid
|
||||||
if err = nonNullStmt.QueryRow().Scan(&nb); err != nil {
|
if err = nonNullStmt.QueryRow().Scan(&nb); err != nil {
|
||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if !nb.Valid {
|
if !nb.Valid {
|
||||||
dbt.Error("Invalid NullBool which should be valid")
|
dbt.Error("invalid NullBool which should be valid")
|
||||||
} else if nb.Bool != true {
|
} else if nb.Bool != true {
|
||||||
dbt.Errorf("Unexpected NullBool value: %t (should be true)", nb.Bool)
|
dbt.Errorf("Unexpected NullBool value: %t (should be true)", nb.Bool)
|
||||||
}
|
}
|
||||||
@ -655,16 +753,16 @@ func TestNULL(t *testing.T) {
|
|||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if nf.Valid {
|
if nf.Valid {
|
||||||
dbt.Error("Valid NullFloat64 which should be invalid")
|
dbt.Error("valid NullFloat64 which should be invalid")
|
||||||
}
|
}
|
||||||
// Valid
|
// Valid
|
||||||
if err = nonNullStmt.QueryRow().Scan(&nf); err != nil {
|
if err = nonNullStmt.QueryRow().Scan(&nf); err != nil {
|
||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if !nf.Valid {
|
if !nf.Valid {
|
||||||
dbt.Error("Invalid NullFloat64 which should be valid")
|
dbt.Error("invalid NullFloat64 which should be valid")
|
||||||
} else if nf.Float64 != float64(1) {
|
} else if nf.Float64 != float64(1) {
|
||||||
dbt.Errorf("Unexpected NullFloat64 value: %f (should be 1.0)", nf.Float64)
|
dbt.Errorf("unexpected NullFloat64 value: %f (should be 1.0)", nf.Float64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NullInt64
|
// NullInt64
|
||||||
@ -674,16 +772,16 @@ func TestNULL(t *testing.T) {
|
|||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if ni.Valid {
|
if ni.Valid {
|
||||||
dbt.Error("Valid NullInt64 which should be invalid")
|
dbt.Error("valid NullInt64 which should be invalid")
|
||||||
}
|
}
|
||||||
// Valid
|
// Valid
|
||||||
if err = nonNullStmt.QueryRow().Scan(&ni); err != nil {
|
if err = nonNullStmt.QueryRow().Scan(&ni); err != nil {
|
||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if !ni.Valid {
|
if !ni.Valid {
|
||||||
dbt.Error("Invalid NullInt64 which should be valid")
|
dbt.Error("invalid NullInt64 which should be valid")
|
||||||
} else if ni.Int64 != int64(1) {
|
} else if ni.Int64 != int64(1) {
|
||||||
dbt.Errorf("Unexpected NullInt64 value: %d (should be 1)", ni.Int64)
|
dbt.Errorf("unexpected NullInt64 value: %d (should be 1)", ni.Int64)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NullString
|
// NullString
|
||||||
@ -693,16 +791,16 @@ func TestNULL(t *testing.T) {
|
|||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if ns.Valid {
|
if ns.Valid {
|
||||||
dbt.Error("Valid NullString which should be invalid")
|
dbt.Error("valid NullString which should be invalid")
|
||||||
}
|
}
|
||||||
// Valid
|
// Valid
|
||||||
if err = nonNullStmt.QueryRow().Scan(&ns); err != nil {
|
if err = nonNullStmt.QueryRow().Scan(&ns); err != nil {
|
||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if !ns.Valid {
|
if !ns.Valid {
|
||||||
dbt.Error("Invalid NullString which should be valid")
|
dbt.Error("invalid NullString which should be valid")
|
||||||
} else if ns.String != `1` {
|
} else if ns.String != `1` {
|
||||||
dbt.Error("Unexpected NullString value:" + ns.String + " (should be `1`)")
|
dbt.Error("unexpected NullString value:" + ns.String + " (should be `1`)")
|
||||||
}
|
}
|
||||||
|
|
||||||
// nil-bytes
|
// nil-bytes
|
||||||
@ -712,14 +810,14 @@ func TestNULL(t *testing.T) {
|
|||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if b != nil {
|
if b != nil {
|
||||||
dbt.Error("Non-nil []byte wich should be nil")
|
dbt.Error("non-nil []byte wich should be nil")
|
||||||
}
|
}
|
||||||
// Read non-nil
|
// Read non-nil
|
||||||
if err = nonNullStmt.QueryRow().Scan(&b); err != nil {
|
if err = nonNullStmt.QueryRow().Scan(&b); err != nil {
|
||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if b == nil {
|
if b == nil {
|
||||||
dbt.Error("Nil []byte wich should be non-nil")
|
dbt.Error("nil []byte wich should be non-nil")
|
||||||
}
|
}
|
||||||
// Insert nil
|
// Insert nil
|
||||||
b = nil
|
b = nil
|
||||||
@ -728,7 +826,7 @@ func TestNULL(t *testing.T) {
|
|||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if !success {
|
if !success {
|
||||||
dbt.Error("Inserting []byte(nil) as NULL failed")
|
dbt.Error("inserting []byte(nil) as NULL failed")
|
||||||
}
|
}
|
||||||
// Check input==output with input==nil
|
// Check input==output with input==nil
|
||||||
b = nil
|
b = nil
|
||||||
@ -736,7 +834,7 @@ func TestNULL(t *testing.T) {
|
|||||||
dbt.Fatal(err)
|
dbt.Fatal(err)
|
||||||
}
|
}
|
||||||
if b != nil {
|
if b != nil {
|
||||||
dbt.Error("Non-nil echo from nil input")
|
dbt.Error("non-nil echo from nil input")
|
||||||
}
|
}
|
||||||
// Check input==output with input!=nil
|
// Check input==output with input!=nil
|
||||||
b = []byte("")
|
b = []byte("")
|
||||||
@ -765,6 +863,49 @@ func TestNULL(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUint64(t *testing.T) {
|
||||||
|
const (
|
||||||
|
u0 = uint64(0)
|
||||||
|
uall = ^u0
|
||||||
|
uhigh = uall >> 1
|
||||||
|
utop = ^uhigh
|
||||||
|
s0 = int64(0)
|
||||||
|
sall = ^s0
|
||||||
|
shigh = int64(uhigh)
|
||||||
|
stop = ^shigh
|
||||||
|
)
|
||||||
|
runTests(t, dsn, func(dbt *DBTest) {
|
||||||
|
stmt, err := dbt.db.Prepare(`SELECT ?, ?, ? ,?, ?, ?, ?, ?`)
|
||||||
|
if err != nil {
|
||||||
|
dbt.Fatal(err)
|
||||||
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
row := stmt.QueryRow(
|
||||||
|
u0, uhigh, utop, uall,
|
||||||
|
s0, shigh, stop, sall,
|
||||||
|
)
|
||||||
|
|
||||||
|
var ua, ub, uc, ud uint64
|
||||||
|
var sa, sb, sc, sd int64
|
||||||
|
|
||||||
|
err = row.Scan(&ua, &ub, &uc, &ud, &sa, &sb, &sc, &sd)
|
||||||
|
if err != nil {
|
||||||
|
dbt.Fatal(err)
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case ua != u0,
|
||||||
|
ub != uhigh,
|
||||||
|
uc != utop,
|
||||||
|
ud != uall,
|
||||||
|
sa != s0,
|
||||||
|
sb != shigh,
|
||||||
|
sc != stop,
|
||||||
|
sd != sall:
|
||||||
|
dbt.Fatal("unexpected result value")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestLongData(t *testing.T) {
|
func TestLongData(t *testing.T) {
|
||||||
runTests(t, dsn, func(dbt *DBTest) {
|
runTests(t, dsn, func(dbt *DBTest) {
|
||||||
var maxAllowedPacketSize int
|
var maxAllowedPacketSize int
|
||||||
@ -855,7 +996,7 @@ func TestLoadData(t *testing.T) {
|
|||||||
dbt.Fatalf("%d != %d", i, id)
|
dbt.Fatalf("%d != %d", i, id)
|
||||||
}
|
}
|
||||||
if values[i-1] != value {
|
if values[i-1] != value {
|
||||||
dbt.Fatalf("%s != %s", values[i-1], value)
|
dbt.Fatalf("%q != %q", values[i-1], value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err = rows.Err()
|
err = rows.Err()
|
||||||
@ -864,7 +1005,7 @@ func TestLoadData(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if i != 4 {
|
if i != 4 {
|
||||||
dbt.Fatalf("Rows count mismatch. Got %d, want 4", i)
|
dbt.Fatalf("rows count mismatch. Got %d, want 4", i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file, err := ioutil.TempFile("", "gotest")
|
file, err := ioutil.TempFile("", "gotest")
|
||||||
@ -880,13 +1021,13 @@ func TestLoadData(t *testing.T) {
|
|||||||
|
|
||||||
// Local File
|
// Local File
|
||||||
RegisterLocalFile(file.Name())
|
RegisterLocalFile(file.Name())
|
||||||
dbt.mustExec(fmt.Sprintf("LOAD DATA LOCAL INFILE '%q' INTO TABLE test", file.Name()))
|
dbt.mustExec(fmt.Sprintf("LOAD DATA LOCAL INFILE %q INTO TABLE test", file.Name()))
|
||||||
verifyLoadDataResult()
|
verifyLoadDataResult()
|
||||||
// negative test
|
// negative test
|
||||||
_, err = dbt.db.Exec("LOAD DATA LOCAL INFILE 'doesnotexist' INTO TABLE test")
|
_, err = dbt.db.Exec("LOAD DATA LOCAL INFILE 'doesnotexist' INTO TABLE test")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
dbt.Fatal("Load non-existent file didn't fail")
|
dbt.Fatal("load non-existent file didn't fail")
|
||||||
} else if err.Error() != "Local File 'doesnotexist' is not registered. Use the DSN parameter 'allowAllFiles=true' to allow all files" {
|
} else if err.Error() != "local file 'doesnotexist' is not registered" {
|
||||||
dbt.Fatal(err.Error())
|
dbt.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -906,7 +1047,7 @@ func TestLoadData(t *testing.T) {
|
|||||||
// negative test
|
// negative test
|
||||||
_, err = dbt.db.Exec("LOAD DATA LOCAL INFILE 'Reader::doesnotexist' INTO TABLE test")
|
_, err = dbt.db.Exec("LOAD DATA LOCAL INFILE 'Reader::doesnotexist' INTO TABLE test")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
dbt.Fatal("Load non-existent Reader didn't fail")
|
dbt.Fatal("load non-existent Reader didn't fail")
|
||||||
} else if err.Error() != "Reader 'doesnotexist' is not registered" {
|
} else if err.Error() != "Reader 'doesnotexist' is not registered" {
|
||||||
dbt.Fatal(err.Error())
|
dbt.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
@ -960,7 +1101,7 @@ func TestFoundRows(t *testing.T) {
|
|||||||
|
|
||||||
func TestStrict(t *testing.T) {
|
func TestStrict(t *testing.T) {
|
||||||
// ALLOW_INVALID_DATES to get rid of stricter modes - we want to test for warnings, not errors
|
// ALLOW_INVALID_DATES to get rid of stricter modes - we want to test for warnings, not errors
|
||||||
relaxedDsn := dsn + "&sql_mode=ALLOW_INVALID_DATES"
|
relaxedDsn := dsn + "&sql_mode='ALLOW_INVALID_DATES,NO_AUTO_CREATE_USER'"
|
||||||
// make sure the MySQL version is recent enough with a separate connection
|
// make sure the MySQL version is recent enough with a separate connection
|
||||||
// before running the test
|
// before running the test
|
||||||
conn, err := MySQLDriver{}.Open(relaxedDsn)
|
conn, err := MySQLDriver{}.Open(relaxedDsn)
|
||||||
@ -986,7 +1127,7 @@ func TestStrict(t *testing.T) {
|
|||||||
|
|
||||||
var checkWarnings = func(err error, mode string, idx int) {
|
var checkWarnings = func(err error, mode string, idx int) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
dbt.Errorf("Expected STRICT error on query [%s] %s", mode, queries[idx].in)
|
dbt.Errorf("expected STRICT error on query [%s] %s", mode, queries[idx].in)
|
||||||
}
|
}
|
||||||
|
|
||||||
if warnings, ok := err.(MySQLWarnings); ok {
|
if warnings, ok := err.(MySQLWarnings); ok {
|
||||||
@ -995,18 +1136,18 @@ func TestStrict(t *testing.T) {
|
|||||||
codes[i] = warnings[i].Code
|
codes[i] = warnings[i].Code
|
||||||
}
|
}
|
||||||
if len(codes) != len(queries[idx].codes) {
|
if len(codes) != len(queries[idx].codes) {
|
||||||
dbt.Errorf("Unexpected STRICT error count on query [%s] %s: Wanted %v, Got %v", mode, queries[idx].in, queries[idx].codes, codes)
|
dbt.Errorf("unexpected STRICT error count on query [%s] %s: Wanted %v, Got %v", mode, queries[idx].in, queries[idx].codes, codes)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range warnings {
|
for i := range warnings {
|
||||||
if codes[i] != queries[idx].codes[i] {
|
if codes[i] != queries[idx].codes[i] {
|
||||||
dbt.Errorf("Unexpected STRICT error codes on query [%s] %s: Wanted %v, Got %v", mode, queries[idx].in, queries[idx].codes, codes)
|
dbt.Errorf("unexpected STRICT error codes on query [%s] %s: Wanted %v, Got %v", mode, queries[idx].in, queries[idx].codes, codes)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
dbt.Errorf("Unexpected error on query [%s] %s: %s", mode, queries[idx].in, err.Error())
|
dbt.Errorf("unexpected error on query [%s] %s: %s", mode, queries[idx].in, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1022,7 +1163,7 @@ func TestStrict(t *testing.T) {
|
|||||||
for i := range queries {
|
for i := range queries {
|
||||||
stmt, err = dbt.db.Prepare(queries[i].in)
|
stmt, err = dbt.db.Prepare(queries[i].in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.Errorf("Error on preparing query %s: %s", queries[i].in, err.Error())
|
dbt.Errorf("error on preparing query %s: %s", queries[i].in, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = stmt.Exec()
|
_, err = stmt.Exec()
|
||||||
@ -1030,7 +1171,7 @@ func TestStrict(t *testing.T) {
|
|||||||
|
|
||||||
err = stmt.Close()
|
err = stmt.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.Errorf("Error on closing stmt for query %s: %s", queries[i].in, err.Error())
|
dbt.Errorf("error on closing stmt for query %s: %s", queries[i].in, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -1040,9 +1181,9 @@ func TestTLS(t *testing.T) {
|
|||||||
tlsTest := func(dbt *DBTest) {
|
tlsTest := func(dbt *DBTest) {
|
||||||
if err := dbt.db.Ping(); err != nil {
|
if err := dbt.db.Ping(); err != nil {
|
||||||
if err == ErrNoTLS {
|
if err == ErrNoTLS {
|
||||||
dbt.Skip("Server does not support TLS")
|
dbt.Skip("server does not support TLS")
|
||||||
} else {
|
} else {
|
||||||
dbt.Fatalf("Error on Ping: %s", err.Error())
|
dbt.Fatalf("error on Ping: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1055,7 +1196,7 @@ func TestTLS(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if value == nil {
|
if value == nil {
|
||||||
dbt.Fatal("No Cipher")
|
dbt.Fatal("no Cipher")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1072,42 +1213,42 @@ func TestTLS(t *testing.T) {
|
|||||||
func TestReuseClosedConnection(t *testing.T) {
|
func TestReuseClosedConnection(t *testing.T) {
|
||||||
// this test does not use sql.database, it uses the driver directly
|
// this test does not use sql.database, it uses the driver directly
|
||||||
if !available {
|
if !available {
|
||||||
t.Skipf("MySQL-Server not running on %s", netAddr)
|
t.Skipf("MySQL server not running on %s", netAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
md := &MySQLDriver{}
|
md := &MySQLDriver{}
|
||||||
conn, err := md.Open(dsn)
|
conn, err := md.Open(dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error connecting: %s", err.Error())
|
t.Fatalf("error connecting: %s", err.Error())
|
||||||
}
|
}
|
||||||
stmt, err := conn.Prepare("DO 1")
|
stmt, err := conn.Prepare("DO 1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error preparing statement: %s", err.Error())
|
t.Fatalf("error preparing statement: %s", err.Error())
|
||||||
}
|
}
|
||||||
_, err = stmt.Exec(nil)
|
_, err = stmt.Exec(nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error executing statement: %s", err.Error())
|
t.Fatalf("error executing statement: %s", err.Error())
|
||||||
}
|
}
|
||||||
err = conn.Close()
|
err = conn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error closing connection: %s", err.Error())
|
t.Fatalf("error closing connection: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
t.Errorf("Panic after reusing a closed connection: %v", err)
|
t.Errorf("panic after reusing a closed connection: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
_, err = stmt.Exec(nil)
|
_, err = stmt.Exec(nil)
|
||||||
if err != nil && err != driver.ErrBadConn {
|
if err != nil && err != driver.ErrBadConn {
|
||||||
t.Errorf("Unexpected error '%s', expected '%s'",
|
t.Errorf("unexpected error '%s', expected '%s'",
|
||||||
err.Error(), driver.ErrBadConn.Error())
|
err.Error(), driver.ErrBadConn.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCharset(t *testing.T) {
|
func TestCharset(t *testing.T) {
|
||||||
if !available {
|
if !available {
|
||||||
t.Skipf("MySQL-Server not running on %s", netAddr)
|
t.Skipf("MySQL server not running on %s", netAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
mustSetCharset := func(charsetParam, expected string) {
|
mustSetCharset := func(charsetParam, expected string) {
|
||||||
@ -1116,14 +1257,14 @@ func TestCharset(t *testing.T) {
|
|||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
|
|
||||||
if !rows.Next() {
|
if !rows.Next() {
|
||||||
dbt.Fatalf("Error getting connection charset: %s", rows.Err())
|
dbt.Fatalf("error getting connection charset: %s", rows.Err())
|
||||||
}
|
}
|
||||||
|
|
||||||
var got string
|
var got string
|
||||||
rows.Scan(&got)
|
rows.Scan(&got)
|
||||||
|
|
||||||
if got != expected {
|
if got != expected {
|
||||||
dbt.Fatalf("Expected connection charset %s but got %s", expected, got)
|
dbt.Fatalf("expected connection charset %s but got %s", expected, got)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -1145,14 +1286,14 @@ func TestFailingCharset(t *testing.T) {
|
|||||||
_, err := dbt.db.Exec("SELECT 1")
|
_, err := dbt.db.Exec("SELECT 1")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
dbt.db.Close()
|
dbt.db.Close()
|
||||||
t.Fatalf("Connection must not succeed without a valid charset")
|
t.Fatalf("connection must not succeed without a valid charset")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCollation(t *testing.T) {
|
func TestCollation(t *testing.T) {
|
||||||
if !available {
|
if !available {
|
||||||
t.Skipf("MySQL-Server not running on %s", netAddr)
|
t.Skipf("MySQL server not running on %s", netAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultCollation := "utf8_general_ci"
|
defaultCollation := "utf8_general_ci"
|
||||||
@ -1182,12 +1323,36 @@ func TestCollation(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if got != expected {
|
if got != expected {
|
||||||
dbt.Fatalf("Expected connection collation %s but got %s", expected, got)
|
dbt.Fatalf("expected connection collation %s but got %s", expected, got)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestColumnsWithAlias(t *testing.T) {
|
||||||
|
runTests(t, dsn+"&columnsWithAlias=true", func(dbt *DBTest) {
|
||||||
|
rows := dbt.mustQuery("SELECT 1 AS A")
|
||||||
|
defer rows.Close()
|
||||||
|
cols, _ := rows.Columns()
|
||||||
|
if len(cols) != 1 {
|
||||||
|
t.Fatalf("expected 1 column, got %d", len(cols))
|
||||||
|
}
|
||||||
|
if cols[0] != "A" {
|
||||||
|
t.Fatalf("expected column name \"A\", got \"%s\"", cols[0])
|
||||||
|
}
|
||||||
|
rows.Close()
|
||||||
|
|
||||||
|
rows = dbt.mustQuery("SELECT * FROM (SELECT 1 AS one) AS A")
|
||||||
|
cols, _ = rows.Columns()
|
||||||
|
if len(cols) != 1 {
|
||||||
|
t.Fatalf("expected 1 column, got %d", len(cols))
|
||||||
|
}
|
||||||
|
if cols[0] != "A.one" {
|
||||||
|
t.Fatalf("expected column name \"A.one\", got \"%s\"", cols[0])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestRawBytesResultExceedsBuffer(t *testing.T) {
|
func TestRawBytesResultExceedsBuffer(t *testing.T) {
|
||||||
runTests(t, dsn, func(dbt *DBTest) {
|
runTests(t, dsn, func(dbt *DBTest) {
|
||||||
// defaultBufSize from buffer.go
|
// defaultBufSize from buffer.go
|
||||||
@ -1223,7 +1388,7 @@ func TestTimezoneConversion(t *testing.T) {
|
|||||||
// Retrieve time from DB
|
// Retrieve time from DB
|
||||||
rows := dbt.mustQuery("SELECT ts FROM test")
|
rows := dbt.mustQuery("SELECT ts FROM test")
|
||||||
if !rows.Next() {
|
if !rows.Next() {
|
||||||
dbt.Fatal("Didn't get any rows out")
|
dbt.Fatal("did not get any rows out")
|
||||||
}
|
}
|
||||||
|
|
||||||
var dbTime time.Time
|
var dbTime time.Time
|
||||||
@ -1234,7 +1399,7 @@ func TestTimezoneConversion(t *testing.T) {
|
|||||||
|
|
||||||
// Check that dates match
|
// Check that dates match
|
||||||
if reftime.Unix() != dbTime.Unix() {
|
if reftime.Unix() != dbTime.Unix() {
|
||||||
dbt.Errorf("Times don't match.\n")
|
dbt.Errorf("times do not match.\n")
|
||||||
dbt.Errorf(" Now(%v)=%v\n", usCentral, reftime)
|
dbt.Errorf(" Now(%v)=%v\n", usCentral, reftime)
|
||||||
dbt.Errorf(" Now(UTC)=%v\n", dbTime)
|
dbt.Errorf(" Now(UTC)=%v\n", dbTime)
|
||||||
}
|
}
|
||||||
@ -1260,7 +1425,7 @@ func TestRowsClose(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if rows.Next() {
|
if rows.Next() {
|
||||||
dbt.Fatal("Unexpected row after rows.Close()")
|
dbt.Fatal("unexpected row after rows.Close()")
|
||||||
}
|
}
|
||||||
|
|
||||||
err = rows.Err()
|
err = rows.Err()
|
||||||
@ -1292,7 +1457,7 @@ func TestCloseStmtBeforeRows(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !rows.Next() {
|
if !rows.Next() {
|
||||||
dbt.Fatal("Getting row failed")
|
dbt.Fatal("getting row failed")
|
||||||
} else {
|
} else {
|
||||||
err = rows.Err()
|
err = rows.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1302,7 +1467,7 @@ func TestCloseStmtBeforeRows(t *testing.T) {
|
|||||||
var out bool
|
var out bool
|
||||||
err = rows.Scan(&out)
|
err = rows.Scan(&out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.Fatalf("Error on rows.Scan(): %s", err.Error())
|
dbt.Fatalf("error on rows.Scan(): %s", err.Error())
|
||||||
}
|
}
|
||||||
if out != true {
|
if out != true {
|
||||||
dbt.Errorf("true != %t", out)
|
dbt.Errorf("true != %t", out)
|
||||||
@ -1338,7 +1503,7 @@ func TestStmtMultiRows(t *testing.T) {
|
|||||||
|
|
||||||
// 1
|
// 1
|
||||||
if !rows1.Next() {
|
if !rows1.Next() {
|
||||||
dbt.Fatal("1st rows1.Next failed")
|
dbt.Fatal("first rows1.Next failed")
|
||||||
} else {
|
} else {
|
||||||
err = rows1.Err()
|
err = rows1.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1347,7 +1512,7 @@ func TestStmtMultiRows(t *testing.T) {
|
|||||||
|
|
||||||
err = rows1.Scan(&out)
|
err = rows1.Scan(&out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.Fatalf("Error on rows.Scan(): %s", err.Error())
|
dbt.Fatalf("error on rows.Scan(): %s", err.Error())
|
||||||
}
|
}
|
||||||
if out != true {
|
if out != true {
|
||||||
dbt.Errorf("true != %t", out)
|
dbt.Errorf("true != %t", out)
|
||||||
@ -1355,7 +1520,7 @@ func TestStmtMultiRows(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !rows2.Next() {
|
if !rows2.Next() {
|
||||||
dbt.Fatal("1st rows2.Next failed")
|
dbt.Fatal("first rows2.Next failed")
|
||||||
} else {
|
} else {
|
||||||
err = rows2.Err()
|
err = rows2.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1364,7 +1529,7 @@ func TestStmtMultiRows(t *testing.T) {
|
|||||||
|
|
||||||
err = rows2.Scan(&out)
|
err = rows2.Scan(&out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.Fatalf("Error on rows.Scan(): %s", err.Error())
|
dbt.Fatalf("error on rows.Scan(): %s", err.Error())
|
||||||
}
|
}
|
||||||
if out != true {
|
if out != true {
|
||||||
dbt.Errorf("true != %t", out)
|
dbt.Errorf("true != %t", out)
|
||||||
@ -1373,7 +1538,7 @@ func TestStmtMultiRows(t *testing.T) {
|
|||||||
|
|
||||||
// 2
|
// 2
|
||||||
if !rows1.Next() {
|
if !rows1.Next() {
|
||||||
dbt.Fatal("2nd rows1.Next failed")
|
dbt.Fatal("second rows1.Next failed")
|
||||||
} else {
|
} else {
|
||||||
err = rows1.Err()
|
err = rows1.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1382,14 +1547,14 @@ func TestStmtMultiRows(t *testing.T) {
|
|||||||
|
|
||||||
err = rows1.Scan(&out)
|
err = rows1.Scan(&out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.Fatalf("Error on rows.Scan(): %s", err.Error())
|
dbt.Fatalf("error on rows.Scan(): %s", err.Error())
|
||||||
}
|
}
|
||||||
if out != false {
|
if out != false {
|
||||||
dbt.Errorf("false != %t", out)
|
dbt.Errorf("false != %t", out)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rows1.Next() {
|
if rows1.Next() {
|
||||||
dbt.Fatal("Unexpected row on rows1")
|
dbt.Fatal("unexpected row on rows1")
|
||||||
}
|
}
|
||||||
err = rows1.Close()
|
err = rows1.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1398,7 +1563,7 @@ func TestStmtMultiRows(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !rows2.Next() {
|
if !rows2.Next() {
|
||||||
dbt.Fatal("2nd rows2.Next failed")
|
dbt.Fatal("second rows2.Next failed")
|
||||||
} else {
|
} else {
|
||||||
err = rows2.Err()
|
err = rows2.Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1407,14 +1572,14 @@ func TestStmtMultiRows(t *testing.T) {
|
|||||||
|
|
||||||
err = rows2.Scan(&out)
|
err = rows2.Scan(&out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.Fatalf("Error on rows.Scan(): %s", err.Error())
|
dbt.Fatalf("error on rows.Scan(): %s", err.Error())
|
||||||
}
|
}
|
||||||
if out != false {
|
if out != false {
|
||||||
dbt.Errorf("false != %t", out)
|
dbt.Errorf("false != %t", out)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rows2.Next() {
|
if rows2.Next() {
|
||||||
dbt.Fatal("Unexpected row on rows2")
|
dbt.Fatal("unexpected row on rows2")
|
||||||
}
|
}
|
||||||
err = rows2.Close()
|
err = rows2.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1459,7 +1624,7 @@ func TestConcurrent(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
dbt.Fatalf("%s", err.Error())
|
dbt.Fatalf("%s", err.Error())
|
||||||
}
|
}
|
||||||
dbt.Logf("Testing up to %d concurrent connections \r\n", max)
|
dbt.Logf("testing up to %d concurrent connections \r\n", max)
|
||||||
|
|
||||||
var remaining, succeeded int32 = int32(max), 0
|
var remaining, succeeded int32 = int32(max), 0
|
||||||
|
|
||||||
@ -1483,7 +1648,7 @@ func TestConcurrent(t *testing.T) {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() != "Error 1040: Too many connections" {
|
if err.Error() != "Error 1040: Too many connections" {
|
||||||
fatalf("Error on Conn %d: %s", id, err.Error())
|
fatalf("error on conn %d: %s", id, err.Error())
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -1491,13 +1656,13 @@ func TestConcurrent(t *testing.T) {
|
|||||||
// keep the connection busy until all connections are open
|
// keep the connection busy until all connections are open
|
||||||
for remaining > 0 {
|
for remaining > 0 {
|
||||||
if _, err = tx.Exec("DO 1"); err != nil {
|
if _, err = tx.Exec("DO 1"); err != nil {
|
||||||
fatalf("Error on Conn %d: %s", id, err.Error())
|
fatalf("error on conn %d: %s", id, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = tx.Commit(); err != nil {
|
if err = tx.Commit(); err != nil {
|
||||||
fatalf("Error on Conn %d: %s", id, err.Error())
|
fatalf("error on conn %d: %s", id, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1513,14 +1678,14 @@ func TestConcurrent(t *testing.T) {
|
|||||||
dbt.Fatal(fatalError)
|
dbt.Fatal(fatalError)
|
||||||
}
|
}
|
||||||
|
|
||||||
dbt.Logf("Reached %d concurrent connections\r\n", succeeded)
|
dbt.Logf("reached %d concurrent connections\r\n", succeeded)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests custom dial functions
|
// Tests custom dial functions
|
||||||
func TestCustomDial(t *testing.T) {
|
func TestCustomDial(t *testing.T) {
|
||||||
if !available {
|
if !available {
|
||||||
t.Skipf("MySQL-Server not running on %s", netAddr)
|
t.Skipf("MySQL server not running on %s", netAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
// our custom dial function which justs wraps net.Dial here
|
// our custom dial function which justs wraps net.Dial here
|
||||||
@ -1530,11 +1695,117 @@ func TestCustomDial(t *testing.T) {
|
|||||||
|
|
||||||
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@mydial(%s)/%s?timeout=30s&strict=true", user, pass, addr, dbname))
|
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@mydial(%s)/%s?timeout=30s&strict=true", user, pass, addr, dbname))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error connecting: %s", err.Error())
|
t.Fatalf("error connecting: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
if _, err = db.Exec("DO 1"); err != nil {
|
if _, err = db.Exec("DO 1"); err != nil {
|
||||||
t.Fatalf("Connection failed: %s", err.Error())
|
t.Fatalf("connection failed: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSQLInjection(t *testing.T) {
|
||||||
|
createTest := func(arg string) func(dbt *DBTest) {
|
||||||
|
return func(dbt *DBTest) {
|
||||||
|
dbt.mustExec("CREATE TABLE test (v INTEGER)")
|
||||||
|
dbt.mustExec("INSERT INTO test VALUES (?)", 1)
|
||||||
|
|
||||||
|
var v int
|
||||||
|
// NULL can't be equal to anything, the idea here is to inject query so it returns row
|
||||||
|
// This test verifies that escapeQuotes and escapeBackslash are working properly
|
||||||
|
err := dbt.db.QueryRow("SELECT v FROM test WHERE NULL = ?", arg).Scan(&v)
|
||||||
|
if err == sql.ErrNoRows {
|
||||||
|
return // success, sql injection failed
|
||||||
|
} else if err == nil {
|
||||||
|
dbt.Errorf("sql injection successful with arg: %s", arg)
|
||||||
|
} else {
|
||||||
|
dbt.Errorf("error running query with arg: %s; err: %s", arg, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dsns := []string{
|
||||||
|
dsn,
|
||||||
|
dsn + "&sql_mode='NO_BACKSLASH_ESCAPES,NO_AUTO_CREATE_USER'",
|
||||||
|
}
|
||||||
|
for _, testdsn := range dsns {
|
||||||
|
runTests(t, testdsn, createTest("1 OR 1=1"))
|
||||||
|
runTests(t, testdsn, createTest("' OR '1'='1"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test if inserted data is correctly retrieved after being escaped
|
||||||
|
func TestInsertRetrieveEscapedData(t *testing.T) {
|
||||||
|
testData := func(dbt *DBTest) {
|
||||||
|
dbt.mustExec("CREATE TABLE test (v VARCHAR(255))")
|
||||||
|
|
||||||
|
// All sequences that are escaped by escapeQuotes and escapeBackslash
|
||||||
|
v := "foo \x00\n\r\x1a\"'\\"
|
||||||
|
dbt.mustExec("INSERT INTO test VALUES (?)", v)
|
||||||
|
|
||||||
|
var out string
|
||||||
|
err := dbt.db.QueryRow("SELECT v FROM test").Scan(&out)
|
||||||
|
if err != nil {
|
||||||
|
dbt.Fatalf("%s", err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
if out != v {
|
||||||
|
dbt.Errorf("%q != %q", out, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dsns := []string{
|
||||||
|
dsn,
|
||||||
|
dsn + "&sql_mode='NO_BACKSLASH_ESCAPES,NO_AUTO_CREATE_USER'",
|
||||||
|
}
|
||||||
|
for _, testdsn := range dsns {
|
||||||
|
runTests(t, testdsn, testData)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnixSocketAuthFail(t *testing.T) {
|
||||||
|
runTests(t, dsn, func(dbt *DBTest) {
|
||||||
|
// Save the current logger so we can restore it.
|
||||||
|
oldLogger := errLog
|
||||||
|
|
||||||
|
// Set a new logger so we can capture its output.
|
||||||
|
buffer := bytes.NewBuffer(make([]byte, 0, 64))
|
||||||
|
newLogger := log.New(buffer, "prefix: ", 0)
|
||||||
|
SetLogger(newLogger)
|
||||||
|
|
||||||
|
// Restore the logger.
|
||||||
|
defer SetLogger(oldLogger)
|
||||||
|
|
||||||
|
// Make a new DSN that uses the MySQL socket file and a bad password, which
|
||||||
|
// we can make by simply appending any character to the real password.
|
||||||
|
badPass := pass + "x"
|
||||||
|
socket := ""
|
||||||
|
if prot == "unix" {
|
||||||
|
socket = addr
|
||||||
|
} else {
|
||||||
|
// Get socket file from MySQL.
|
||||||
|
err := dbt.db.QueryRow("SELECT @@socket").Scan(&socket)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error on SELECT @@socket: %s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Logf("socket: %s", socket)
|
||||||
|
badDSN := fmt.Sprintf("%s:%s@unix(%s)/%s?timeout=30s&strict=true", user, badPass, socket, dbname)
|
||||||
|
db, err := sql.Open("mysql", badDSN)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error connecting: %s", err.Error())
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
// Connect to MySQL for real. This will cause an auth failure.
|
||||||
|
err = db.Ping()
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected Ping() to return an error")
|
||||||
|
}
|
||||||
|
|
||||||
|
// The driver should not log anything.
|
||||||
|
if actual := buffer.String(); actual != "" {
|
||||||
|
t.Errorf("expected no output, got %q", actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
513
Godeps/_workspace/src/github.com/go-sql-driver/mysql/dsn.go
generated
vendored
Normal file
513
Godeps/_workspace/src/github.com/go-sql-driver/mysql/dsn.go
generated
vendored
Normal file
@ -0,0 +1,513 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2016 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/tls"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errInvalidDSNUnescaped = errors.New("invalid DSN: did you forget to escape a param value?")
|
||||||
|
errInvalidDSNAddr = errors.New("invalid DSN: network address not terminated (missing closing brace)")
|
||||||
|
errInvalidDSNNoSlash = errors.New("invalid DSN: missing the slash separating the database name")
|
||||||
|
errInvalidDSNUnsafeCollation = errors.New("invalid DSN: interpolateParams can not be used with unsafe collations")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config is a configuration parsed from a DSN string
|
||||||
|
type Config struct {
|
||||||
|
User string // Username
|
||||||
|
Passwd string // Password
|
||||||
|
Net string // Network type
|
||||||
|
Addr string // Network address
|
||||||
|
DBName string // Database name
|
||||||
|
Params map[string]string // Connection parameters
|
||||||
|
Collation string // Connection collation
|
||||||
|
Loc *time.Location // Location for time.Time values
|
||||||
|
TLSConfig string // TLS configuration name
|
||||||
|
tls *tls.Config // TLS configuration
|
||||||
|
Timeout time.Duration // Dial timeout
|
||||||
|
ReadTimeout time.Duration // I/O read timeout
|
||||||
|
WriteTimeout time.Duration // I/O write timeout
|
||||||
|
|
||||||
|
AllowAllFiles bool // Allow all files to be used with LOAD DATA LOCAL INFILE
|
||||||
|
AllowCleartextPasswords bool // Allows the cleartext client side plugin
|
||||||
|
AllowOldPasswords bool // Allows the old insecure password method
|
||||||
|
ClientFoundRows bool // Return number of matching rows instead of rows changed
|
||||||
|
ColumnsWithAlias bool // Prepend table alias to column names
|
||||||
|
InterpolateParams bool // Interpolate placeholders into query string
|
||||||
|
MultiStatements bool // Allow multiple statements in one query
|
||||||
|
ParseTime bool // Parse time values to time.Time
|
||||||
|
Strict bool // Return warnings as errors
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatDSN formats the given Config into a DSN string which can be passed to
|
||||||
|
// the driver.
|
||||||
|
func (cfg *Config) FormatDSN() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
// [username[:password]@]
|
||||||
|
if len(cfg.User) > 0 {
|
||||||
|
buf.WriteString(cfg.User)
|
||||||
|
if len(cfg.Passwd) > 0 {
|
||||||
|
buf.WriteByte(':')
|
||||||
|
buf.WriteString(cfg.Passwd)
|
||||||
|
}
|
||||||
|
buf.WriteByte('@')
|
||||||
|
}
|
||||||
|
|
||||||
|
// [protocol[(address)]]
|
||||||
|
if len(cfg.Net) > 0 {
|
||||||
|
buf.WriteString(cfg.Net)
|
||||||
|
if len(cfg.Addr) > 0 {
|
||||||
|
buf.WriteByte('(')
|
||||||
|
buf.WriteString(cfg.Addr)
|
||||||
|
buf.WriteByte(')')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// /dbname
|
||||||
|
buf.WriteByte('/')
|
||||||
|
buf.WriteString(cfg.DBName)
|
||||||
|
|
||||||
|
// [?param1=value1&...¶mN=valueN]
|
||||||
|
hasParam := false
|
||||||
|
|
||||||
|
if cfg.AllowAllFiles {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?allowAllFiles=true")
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.AllowCleartextPasswords {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&allowCleartextPasswords=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?allowCleartextPasswords=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.AllowOldPasswords {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&allowOldPasswords=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?allowOldPasswords=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ClientFoundRows {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&clientFoundRows=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?clientFoundRows=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if col := cfg.Collation; col != defaultCollation && len(col) > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&collation=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?collation=")
|
||||||
|
}
|
||||||
|
buf.WriteString(col)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ColumnsWithAlias {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&columnsWithAlias=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?columnsWithAlias=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.InterpolateParams {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&interpolateParams=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?interpolateParams=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Loc != time.UTC && cfg.Loc != nil {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&loc=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?loc=")
|
||||||
|
}
|
||||||
|
buf.WriteString(url.QueryEscape(cfg.Loc.String()))
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.MultiStatements {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&multiStatements=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?multiStatements=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ParseTime {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&parseTime=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?parseTime=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.ReadTimeout > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&readTimeout=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?readTimeout=")
|
||||||
|
}
|
||||||
|
buf.WriteString(cfg.ReadTimeout.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Strict {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&strict=true")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?strict=true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Timeout > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&timeout=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?timeout=")
|
||||||
|
}
|
||||||
|
buf.WriteString(cfg.Timeout.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cfg.TLSConfig) > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&tls=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?tls=")
|
||||||
|
}
|
||||||
|
buf.WriteString(url.QueryEscape(cfg.TLSConfig))
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.WriteTimeout > 0 {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteString("&writeTimeout=")
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteString("?writeTimeout=")
|
||||||
|
}
|
||||||
|
buf.WriteString(cfg.WriteTimeout.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// other params
|
||||||
|
if cfg.Params != nil {
|
||||||
|
for param, value := range cfg.Params {
|
||||||
|
if hasParam {
|
||||||
|
buf.WriteByte('&')
|
||||||
|
} else {
|
||||||
|
hasParam = true
|
||||||
|
buf.WriteByte('?')
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.WriteString(param)
|
||||||
|
buf.WriteByte('=')
|
||||||
|
buf.WriteString(url.QueryEscape(value))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ParseDSN parses the DSN string to a Config
|
||||||
|
func ParseDSN(dsn string) (cfg *Config, err error) {
|
||||||
|
// New config with some default values
|
||||||
|
cfg = &Config{
|
||||||
|
Loc: time.UTC,
|
||||||
|
Collation: defaultCollation,
|
||||||
|
}
|
||||||
|
|
||||||
|
// [user[:password]@][net[(addr)]]/dbname[?param1=value1¶mN=valueN]
|
||||||
|
// Find the last '/' (since the password or the net addr might contain a '/')
|
||||||
|
foundSlash := false
|
||||||
|
for i := len(dsn) - 1; i >= 0; i-- {
|
||||||
|
if dsn[i] == '/' {
|
||||||
|
foundSlash = true
|
||||||
|
var j, k int
|
||||||
|
|
||||||
|
// left part is empty if i <= 0
|
||||||
|
if i > 0 {
|
||||||
|
// [username[:password]@][protocol[(address)]]
|
||||||
|
// Find the last '@' in dsn[:i]
|
||||||
|
for j = i; j >= 0; j-- {
|
||||||
|
if dsn[j] == '@' {
|
||||||
|
// username[:password]
|
||||||
|
// Find the first ':' in dsn[:j]
|
||||||
|
for k = 0; k < j; k++ {
|
||||||
|
if dsn[k] == ':' {
|
||||||
|
cfg.Passwd = dsn[k+1 : j]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cfg.User = dsn[:k]
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// [protocol[(address)]]
|
||||||
|
// Find the first '(' in dsn[j+1:i]
|
||||||
|
for k = j + 1; k < i; k++ {
|
||||||
|
if dsn[k] == '(' {
|
||||||
|
// dsn[i-1] must be == ')' if an address is specified
|
||||||
|
if dsn[i-1] != ')' {
|
||||||
|
if strings.ContainsRune(dsn[k+1:i], ')') {
|
||||||
|
return nil, errInvalidDSNUnescaped
|
||||||
|
}
|
||||||
|
return nil, errInvalidDSNAddr
|
||||||
|
}
|
||||||
|
cfg.Addr = dsn[k+1 : i-1]
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cfg.Net = dsn[j+1 : k]
|
||||||
|
}
|
||||||
|
|
||||||
|
// dbname[?param1=value1&...¶mN=valueN]
|
||||||
|
// Find the first '?' in dsn[i+1:]
|
||||||
|
for j = i + 1; j < len(dsn); j++ {
|
||||||
|
if dsn[j] == '?' {
|
||||||
|
if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cfg.DBName = dsn[i+1 : j]
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !foundSlash && len(dsn) > 0 {
|
||||||
|
return nil, errInvalidDSNNoSlash
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.InterpolateParams && unsafeCollations[cfg.Collation] {
|
||||||
|
return nil, errInvalidDSNUnsafeCollation
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set default network if empty
|
||||||
|
if cfg.Net == "" {
|
||||||
|
cfg.Net = "tcp"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set default address if empty
|
||||||
|
if cfg.Addr == "" {
|
||||||
|
switch cfg.Net {
|
||||||
|
case "tcp":
|
||||||
|
cfg.Addr = "127.0.0.1:3306"
|
||||||
|
case "unix":
|
||||||
|
cfg.Addr = "/tmp/mysql.sock"
|
||||||
|
default:
|
||||||
|
return nil, errors.New("default addr for network '" + cfg.Net + "' unknown")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseDSNParams parses the DSN "query string"
|
||||||
|
// Values must be url.QueryEscape'ed
|
||||||
|
func parseDSNParams(cfg *Config, params string) (err error) {
|
||||||
|
for _, v := range strings.Split(params, "&") {
|
||||||
|
param := strings.SplitN(v, "=", 2)
|
||||||
|
if len(param) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// cfg params
|
||||||
|
switch value := param[1]; param[0] {
|
||||||
|
|
||||||
|
// Disable INFILE whitelist / enable all files
|
||||||
|
case "allowAllFiles":
|
||||||
|
var isBool bool
|
||||||
|
cfg.AllowAllFiles, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use cleartext authentication mode (MySQL 5.5.10+)
|
||||||
|
case "allowCleartextPasswords":
|
||||||
|
var isBool bool
|
||||||
|
cfg.AllowCleartextPasswords, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use old authentication mode (pre MySQL 4.1)
|
||||||
|
case "allowOldPasswords":
|
||||||
|
var isBool bool
|
||||||
|
cfg.AllowOldPasswords, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Switch "rowsAffected" mode
|
||||||
|
case "clientFoundRows":
|
||||||
|
var isBool bool
|
||||||
|
cfg.ClientFoundRows, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collation
|
||||||
|
case "collation":
|
||||||
|
cfg.Collation = value
|
||||||
|
break
|
||||||
|
|
||||||
|
case "columnsWithAlias":
|
||||||
|
var isBool bool
|
||||||
|
cfg.ColumnsWithAlias, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compression
|
||||||
|
case "compress":
|
||||||
|
return errors.New("compression not implemented yet")
|
||||||
|
|
||||||
|
// Enable client side placeholder substitution
|
||||||
|
case "interpolateParams":
|
||||||
|
var isBool bool
|
||||||
|
cfg.InterpolateParams, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Time Location
|
||||||
|
case "loc":
|
||||||
|
if value, err = url.QueryUnescape(value); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cfg.Loc, err = time.LoadLocation(value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// multiple statements in one query
|
||||||
|
case "multiStatements":
|
||||||
|
var isBool bool
|
||||||
|
cfg.MultiStatements, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// time.Time parsing
|
||||||
|
case "parseTime":
|
||||||
|
var isBool bool
|
||||||
|
cfg.ParseTime, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// I/O read Timeout
|
||||||
|
case "readTimeout":
|
||||||
|
cfg.ReadTimeout, err = time.ParseDuration(value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strict mode
|
||||||
|
case "strict":
|
||||||
|
var isBool bool
|
||||||
|
cfg.Strict, isBool = readBool(value)
|
||||||
|
if !isBool {
|
||||||
|
return errors.New("invalid bool value: " + value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial Timeout
|
||||||
|
case "timeout":
|
||||||
|
cfg.Timeout, err = time.ParseDuration(value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// TLS-Encryption
|
||||||
|
case "tls":
|
||||||
|
boolValue, isBool := readBool(value)
|
||||||
|
if isBool {
|
||||||
|
if boolValue {
|
||||||
|
cfg.TLSConfig = "true"
|
||||||
|
cfg.tls = &tls.Config{}
|
||||||
|
} else {
|
||||||
|
cfg.TLSConfig = "false"
|
||||||
|
}
|
||||||
|
} else if vl := strings.ToLower(value); vl == "skip-verify" {
|
||||||
|
cfg.TLSConfig = vl
|
||||||
|
cfg.tls = &tls.Config{InsecureSkipVerify: true}
|
||||||
|
} else {
|
||||||
|
name, err := url.QueryUnescape(value)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("invalid value for TLS config name: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tlsConfig, ok := tlsConfigRegister[name]; ok {
|
||||||
|
if len(tlsConfig.ServerName) == 0 && !tlsConfig.InsecureSkipVerify {
|
||||||
|
host, _, err := net.SplitHostPort(cfg.Addr)
|
||||||
|
if err == nil {
|
||||||
|
tlsConfig.ServerName = host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg.TLSConfig = name
|
||||||
|
cfg.tls = tlsConfig
|
||||||
|
} else {
|
||||||
|
return errors.New("invalid value / unknown config name: " + name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// I/O write Timeout
|
||||||
|
case "writeTimeout":
|
||||||
|
cfg.WriteTimeout, err = time.ParseDuration(value)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
// lazy init
|
||||||
|
if cfg.Params == nil {
|
||||||
|
cfg.Params = make(map[string]string)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.Params[param[0]], err = url.QueryUnescape(value); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
207
Godeps/_workspace/src/github.com/go-sql-driver/mysql/dsn_test.go
generated
vendored
Normal file
207
Godeps/_workspace/src/github.com/go-sql-driver/mysql/dsn_test.go
generated
vendored
Normal file
@ -0,0 +1,207 @@
|
|||||||
|
// Go MySQL Driver - A MySQL-Driver for Go's database/sql package
|
||||||
|
//
|
||||||
|
// Copyright 2016 The Go-MySQL-Driver Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||||
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
|
package mysql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
var testDSNs = []struct {
|
||||||
|
in string
|
||||||
|
out string
|
||||||
|
}{
|
||||||
|
{"username:password@protocol(address)/dbname?param=value", "&{User:username Passwd:password Net:protocol Addr:address DBName:dbname Params:map[param:value] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"username:password@protocol(address)/dbname?param=value&columnsWithAlias=true", "&{User:username Passwd:password Net:protocol Addr:address DBName:dbname Params:map[param:value] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:true InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"username:password@protocol(address)/dbname?param=value&columnsWithAlias=true&multiStatements=true", "&{User:username Passwd:password Net:protocol Addr:address DBName:dbname Params:map[param:value] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:true InterpolateParams:false MultiStatements:true ParseTime:false Strict:false}"},
|
||||||
|
{"user@unix(/path/to/socket)/dbname?charset=utf8", "&{User:user Passwd: Net:unix Addr:/path/to/socket DBName:dbname Params:map[charset:utf8] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"user:password@tcp(localhost:5555)/dbname?charset=utf8&tls=true", "&{User:user Passwd:password Net:tcp Addr:localhost:5555 DBName:dbname Params:map[charset:utf8] Collation:utf8_general_ci Loc:UTC TLSConfig:true tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify", "&{User:user Passwd:password Net:tcp Addr:localhost:5555 DBName:dbname Params:map[charset:utf8mb4,utf8] Collation:utf8_general_ci Loc:UTC TLSConfig:skip-verify tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"user:password@/dbname?loc=UTC&timeout=30s&readTimeout=1s&writeTimeout=1s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci", "&{User:user Passwd:password Net:tcp Addr:127.0.0.1:3306 DBName:dbname Params:map[] Collation:utf8mb4_unicode_ci Loc:UTC TLSConfig: tls:<nil> Timeout:30s ReadTimeout:1s WriteTimeout:1s AllowAllFiles:true AllowCleartextPasswords:false AllowOldPasswords:true ClientFoundRows:true ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local", "&{User:user Passwd:p@ss(word) Net:tcp Addr:[de:ad:be:ef::ca:fe]:80 DBName:dbname Params:map[] Collation:utf8_general_ci Loc:Local TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"/dbname", "&{User: Passwd: Net:tcp Addr:127.0.0.1:3306 DBName:dbname Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"@/", "&{User: Passwd: Net:tcp Addr:127.0.0.1:3306 DBName: Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"/", "&{User: Passwd: Net:tcp Addr:127.0.0.1:3306 DBName: Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"", "&{User: Passwd: Net:tcp Addr:127.0.0.1:3306 DBName: Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"user:p@/ssword@/", "&{User:user Passwd:p@/ssword Net:tcp Addr:127.0.0.1:3306 DBName: Params:map[] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
{"unix/?arg=%2Fsome%2Fpath.ext", "&{User: Passwd: Net:unix Addr:/tmp/mysql.sock DBName: Params:map[arg:/some/path.ext] Collation:utf8_general_ci Loc:UTC TLSConfig: tls:<nil> Timeout:0 ReadTimeout:0 WriteTimeout:0 AllowAllFiles:false AllowCleartextPasswords:false AllowOldPasswords:false ClientFoundRows:false ColumnsWithAlias:false InterpolateParams:false MultiStatements:false ParseTime:false Strict:false}"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDSNParser(t *testing.T) {
|
||||||
|
var cfg *Config
|
||||||
|
var err error
|
||||||
|
var res string
|
||||||
|
|
||||||
|
for i, tst := range testDSNs {
|
||||||
|
cfg, err = ParseDSN(tst.in)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
// pointer not static
|
||||||
|
cfg.tls = nil
|
||||||
|
|
||||||
|
res = fmt.Sprintf("%+v", cfg)
|
||||||
|
if res != tst.out {
|
||||||
|
t.Errorf("%d. ParseDSN(%q) => %q, want %q", i, tst.in, res, tst.out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDSNParserInvalid(t *testing.T) {
|
||||||
|
var invalidDSNs = []string{
|
||||||
|
"@net(addr/", // no closing brace
|
||||||
|
"@tcp(/", // no closing brace
|
||||||
|
"tcp(/", // no closing brace
|
||||||
|
"(/", // no closing brace
|
||||||
|
"net(addr)//", // unescaped
|
||||||
|
"User:pass@tcp(1.2.3.4:3306)", // no trailing slash
|
||||||
|
//"/dbname?arg=/some/unescaped/path",
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, tst := range invalidDSNs {
|
||||||
|
if _, err := ParseDSN(tst); err == nil {
|
||||||
|
t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDSNReformat(t *testing.T) {
|
||||||
|
for i, tst := range testDSNs {
|
||||||
|
dsn1 := tst.in
|
||||||
|
cfg1, err := ParseDSN(dsn1)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cfg1.tls = nil // pointer not static
|
||||||
|
res1 := fmt.Sprintf("%+v", cfg1)
|
||||||
|
|
||||||
|
dsn2 := cfg1.FormatDSN()
|
||||||
|
cfg2, err := ParseDSN(dsn2)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cfg2.tls = nil // pointer not static
|
||||||
|
res2 := fmt.Sprintf("%+v", cfg2)
|
||||||
|
|
||||||
|
if res1 != res2 {
|
||||||
|
t.Errorf("%d. %q does not match %q", i, res2, res1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDSNWithCustomTLS(t *testing.T) {
|
||||||
|
baseDSN := "User:password@tcp(localhost:5555)/dbname?tls="
|
||||||
|
tlsCfg := tls.Config{}
|
||||||
|
|
||||||
|
RegisterTLSConfig("utils_test", &tlsCfg)
|
||||||
|
|
||||||
|
// Custom TLS is missing
|
||||||
|
tst := baseDSN + "invalid_tls"
|
||||||
|
cfg, err := ParseDSN(tst)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("invalid custom TLS in DSN (%s) but did not error. Got config: %#v", tst, cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
tst = baseDSN + "utils_test"
|
||||||
|
|
||||||
|
// Custom TLS with a server name
|
||||||
|
name := "foohost"
|
||||||
|
tlsCfg.ServerName = name
|
||||||
|
cfg, err = ParseDSN(tst)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err.Error())
|
||||||
|
} else if cfg.tls.ServerName != name {
|
||||||
|
t.Errorf("did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, tst)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Custom TLS without a server name
|
||||||
|
name = "localhost"
|
||||||
|
tlsCfg.ServerName = ""
|
||||||
|
cfg, err = ParseDSN(tst)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err.Error())
|
||||||
|
} else if cfg.tls.ServerName != name {
|
||||||
|
t.Errorf("did not get the correct ServerName (%s) parsing DSN (%s).", name, tst)
|
||||||
|
}
|
||||||
|
|
||||||
|
DeregisterTLSConfig("utils_test")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDSNWithCustomTLSQueryEscape(t *testing.T) {
|
||||||
|
const configKey = "&%!:"
|
||||||
|
dsn := "User:password@tcp(localhost:5555)/dbname?tls=" + url.QueryEscape(configKey)
|
||||||
|
name := "foohost"
|
||||||
|
tlsCfg := tls.Config{ServerName: name}
|
||||||
|
|
||||||
|
RegisterTLSConfig(configKey, &tlsCfg)
|
||||||
|
|
||||||
|
cfg, err := ParseDSN(dsn)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err.Error())
|
||||||
|
} else if cfg.tls.ServerName != name {
|
||||||
|
t.Errorf("did not get the correct TLS ServerName (%s) parsing DSN (%s).", name, dsn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDSNUnsafeCollation(t *testing.T) {
|
||||||
|
_, err := ParseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=true")
|
||||||
|
if err != errInvalidDSNUnsafeCollation {
|
||||||
|
t.Errorf("expected %v, got %v", errInvalidDSNUnsafeCollation, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ParseDSN("/dbname?collation=gbk_chinese_ci&interpolateParams=false")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected %v, got %v", nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ParseDSN("/dbname?collation=gbk_chinese_ci")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected %v, got %v", nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ParseDSN("/dbname?collation=ascii_bin&interpolateParams=true")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected %v, got %v", nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ParseDSN("/dbname?collation=latin1_german1_ci&interpolateParams=true")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected %v, got %v", nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ParseDSN("/dbname?collation=utf8_general_ci&interpolateParams=true")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected %v, got %v", nil, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = ParseDSN("/dbname?collation=utf8mb4_general_ci&interpolateParams=true")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("expected %v, got %v", nil, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkParseDSN(b *testing.B) {
|
||||||
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
for _, tst := range testDSNs {
|
||||||
|
if _, err := ParseDSN(tst.in); err != nil {
|
||||||
|
b.Error(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
22
Godeps/_workspace/src/github.com/go-sql-driver/mysql/errors.go
generated
vendored
22
Godeps/_workspace/src/github.com/go-sql-driver/mysql/errors.go
generated
vendored
@ -19,18 +19,20 @@ import (
|
|||||||
|
|
||||||
// Various errors the driver might return. Can change between driver versions.
|
// Various errors the driver might return. Can change between driver versions.
|
||||||
var (
|
var (
|
||||||
ErrInvalidConn = errors.New("Invalid Connection")
|
ErrInvalidConn = errors.New("invalid connection")
|
||||||
ErrMalformPkt = errors.New("Malformed Packet")
|
ErrMalformPkt = errors.New("malformed packet")
|
||||||
ErrNoTLS = errors.New("TLS encryption requested but server does not support TLS")
|
ErrNoTLS = errors.New("TLS requested but server does not support TLS")
|
||||||
ErrOldPassword = errors.New("This server only supports the insecure old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords")
|
ErrOldPassword = errors.New("this user requires old password authentication. If you still want to use it, please add 'allowOldPasswords=1' to your DSN. See also https://github.com/go-sql-driver/mysql/wiki/old_passwords")
|
||||||
ErrOldProtocol = errors.New("MySQL-Server does not support required Protocol 41+")
|
ErrCleartextPassword = errors.New("this user requires clear text authentication. If you still want to use it, please add 'allowCleartextPasswords=1' to your DSN")
|
||||||
ErrPktSync = errors.New("Commands out of sync. You can't run this command now")
|
ErrUnknownPlugin = errors.New("this authentication plugin is not supported")
|
||||||
ErrPktSyncMul = errors.New("Commands out of sync. Did you run multiple statements at once?")
|
ErrOldProtocol = errors.New("MySQL server does not support required protocol 41+")
|
||||||
ErrPktTooLarge = errors.New("Packet for query is too large. You can change this value on the server by adjusting the 'max_allowed_packet' variable.")
|
ErrPktSync = errors.New("commands out of sync. You can't run this command now")
|
||||||
ErrBusyBuffer = errors.New("Busy buffer")
|
ErrPktSyncMul = errors.New("commands out of sync. Did you run multiple statements at once?")
|
||||||
|
ErrPktTooLarge = errors.New("packet for query is too large. Try adjusting the 'max_allowed_packet' variable on the server")
|
||||||
|
ErrBusyBuffer = errors.New("busy buffer")
|
||||||
)
|
)
|
||||||
|
|
||||||
var errLog Logger = log.New(os.Stderr, "[MySQL] ", log.Ldate|log.Ltime|log.Lshortfile)
|
var errLog = Logger(log.New(os.Stderr, "[mysql] ", log.Ldate|log.Ltime|log.Lshortfile))
|
||||||
|
|
||||||
// Logger is used to log critical error messages.
|
// Logger is used to log critical error messages.
|
||||||
type Logger interface {
|
type Logger interface {
|
||||||
|
36
Godeps/_workspace/src/github.com/go-sql-driver/mysql/infile.go
generated
vendored
36
Godeps/_workspace/src/github.com/go-sql-driver/mysql/infile.go
generated
vendored
@ -13,11 +13,14 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
fileRegister map[string]bool
|
fileRegister map[string]bool
|
||||||
|
fileRegisterLock sync.RWMutex
|
||||||
readerRegister map[string]func() io.Reader
|
readerRegister map[string]func() io.Reader
|
||||||
|
readerRegisterLock sync.RWMutex
|
||||||
)
|
)
|
||||||
|
|
||||||
// RegisterLocalFile adds the given file to the file whitelist,
|
// RegisterLocalFile adds the given file to the file whitelist,
|
||||||
@ -32,17 +35,21 @@ var (
|
|||||||
// ...
|
// ...
|
||||||
//
|
//
|
||||||
func RegisterLocalFile(filePath string) {
|
func RegisterLocalFile(filePath string) {
|
||||||
|
fileRegisterLock.Lock()
|
||||||
// lazy map init
|
// lazy map init
|
||||||
if fileRegister == nil {
|
if fileRegister == nil {
|
||||||
fileRegister = make(map[string]bool)
|
fileRegister = make(map[string]bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
fileRegister[strings.Trim(filePath, `"`)] = true
|
fileRegister[strings.Trim(filePath, `"`)] = true
|
||||||
|
fileRegisterLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeregisterLocalFile removes the given filepath from the whitelist.
|
// DeregisterLocalFile removes the given filepath from the whitelist.
|
||||||
func DeregisterLocalFile(filePath string) {
|
func DeregisterLocalFile(filePath string) {
|
||||||
|
fileRegisterLock.Lock()
|
||||||
delete(fileRegister, strings.Trim(filePath, `"`))
|
delete(fileRegister, strings.Trim(filePath, `"`))
|
||||||
|
fileRegisterLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterReaderHandler registers a handler function which is used
|
// RegisterReaderHandler registers a handler function which is used
|
||||||
@ -61,18 +68,22 @@ func DeregisterLocalFile(filePath string) {
|
|||||||
// ...
|
// ...
|
||||||
//
|
//
|
||||||
func RegisterReaderHandler(name string, handler func() io.Reader) {
|
func RegisterReaderHandler(name string, handler func() io.Reader) {
|
||||||
|
readerRegisterLock.Lock()
|
||||||
// lazy map init
|
// lazy map init
|
||||||
if readerRegister == nil {
|
if readerRegister == nil {
|
||||||
readerRegister = make(map[string]func() io.Reader)
|
readerRegister = make(map[string]func() io.Reader)
|
||||||
}
|
}
|
||||||
|
|
||||||
readerRegister[name] = handler
|
readerRegister[name] = handler
|
||||||
|
readerRegisterLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeregisterReaderHandler removes the ReaderHandler function with
|
// DeregisterReaderHandler removes the ReaderHandler function with
|
||||||
// the given name from the registry.
|
// the given name from the registry.
|
||||||
func DeregisterReaderHandler(name string) {
|
func DeregisterReaderHandler(name string) {
|
||||||
|
readerRegisterLock.Lock()
|
||||||
delete(readerRegister, name)
|
delete(readerRegister, name)
|
||||||
|
readerRegisterLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func deferredClose(err *error, closer io.Closer) {
|
func deferredClose(err *error, closer io.Closer) {
|
||||||
@ -86,9 +97,15 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
|
|||||||
var rdr io.Reader
|
var rdr io.Reader
|
||||||
var data []byte
|
var data []byte
|
||||||
|
|
||||||
if strings.HasPrefix(name, "Reader::") { // io.Reader
|
if idx := strings.Index(name, "Reader::"); idx == 0 || (idx > 0 && name[idx-1] == '/') { // io.Reader
|
||||||
name = name[8:]
|
// The server might return an an absolute path. See issue #355.
|
||||||
if handler, inMap := readerRegister[name]; inMap {
|
name = name[idx+8:]
|
||||||
|
|
||||||
|
readerRegisterLock.RLock()
|
||||||
|
handler, inMap := readerRegister[name]
|
||||||
|
readerRegisterLock.RUnlock()
|
||||||
|
|
||||||
|
if inMap {
|
||||||
rdr = handler()
|
rdr = handler()
|
||||||
if rdr != nil {
|
if rdr != nil {
|
||||||
data = make([]byte, 4+mc.maxWriteSize)
|
data = make([]byte, 4+mc.maxWriteSize)
|
||||||
@ -104,7 +121,10 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
|
|||||||
}
|
}
|
||||||
} else { // File
|
} else { // File
|
||||||
name = strings.Trim(name, `"`)
|
name = strings.Trim(name, `"`)
|
||||||
if mc.cfg.allowAllFiles || fileRegister[name] {
|
fileRegisterLock.RLock()
|
||||||
|
fr := fileRegister[name]
|
||||||
|
fileRegisterLock.RUnlock()
|
||||||
|
if mc.cfg.AllowAllFiles || fr {
|
||||||
var file *os.File
|
var file *os.File
|
||||||
var fi os.FileInfo
|
var fi os.FileInfo
|
||||||
|
|
||||||
@ -119,12 +139,12 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
|
|||||||
} else if fileSize <= mc.maxPacketAllowed {
|
} else if fileSize <= mc.maxPacketAllowed {
|
||||||
data = make([]byte, 4+mc.maxWriteSize)
|
data = make([]byte, 4+mc.maxWriteSize)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("Local File '%s' too large: Size: %d, Max: %d", name, fileSize, mc.maxPacketAllowed)
|
err = fmt.Errorf("local file '%s' too large: size: %d, max: %d", name, fileSize, mc.maxPacketAllowed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("Local File '%s' is not registered. Use the DSN parameter 'allowAllFiles=true' to allow all files", name)
|
err = fmt.Errorf("local file '%s' is not registered", name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,8 +175,8 @@ func (mc *mysqlConn) handleInFileRequest(name string) (err error) {
|
|||||||
// read OK packet
|
// read OK packet
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return mc.readResultOK()
|
return mc.readResultOK()
|
||||||
} else {
|
|
||||||
mc.readPacket()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mc.readPacket()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
187
Godeps/_workspace/src/github.com/go-sql-driver/mysql/packets.go
generated
vendored
187
Godeps/_workspace/src/github.com/go-sql-driver/mysql/packets.go
generated
vendored
@ -13,6 +13,7 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
@ -47,9 +48,8 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
|
|||||||
if data[3] != mc.sequence {
|
if data[3] != mc.sequence {
|
||||||
if data[3] > mc.sequence {
|
if data[3] > mc.sequence {
|
||||||
return nil, ErrPktSyncMul
|
return nil, ErrPktSyncMul
|
||||||
} else {
|
|
||||||
return nil, ErrPktSync
|
|
||||||
}
|
}
|
||||||
|
return nil, ErrPktSync
|
||||||
}
|
}
|
||||||
mc.sequence++
|
mc.sequence++
|
||||||
|
|
||||||
@ -100,6 +100,12 @@ func (mc *mysqlConn) writePacket(data []byte) error {
|
|||||||
data[3] = mc.sequence
|
data[3] = mc.sequence
|
||||||
|
|
||||||
// Write packet
|
// Write packet
|
||||||
|
if mc.writeTimeout > 0 {
|
||||||
|
if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
n, err := mc.netConn.Write(data[:4+size])
|
n, err := mc.netConn.Write(data[:4+size])
|
||||||
if err == nil && n == 4+size {
|
if err == nil && n == 4+size {
|
||||||
mc.sequence++
|
mc.sequence++
|
||||||
@ -140,7 +146,7 @@ func (mc *mysqlConn) readInitPacket() ([]byte, error) {
|
|||||||
// protocol version [1 byte]
|
// protocol version [1 byte]
|
||||||
if data[0] < minProtocolVersion {
|
if data[0] < minProtocolVersion {
|
||||||
return nil, fmt.Errorf(
|
return nil, fmt.Errorf(
|
||||||
"Unsupported MySQL Protocol Version %d. Protocol Version %d or higher is required",
|
"unsupported protocol version %d. Version %d or higher is required",
|
||||||
data[0],
|
data[0],
|
||||||
minProtocolVersion,
|
minProtocolVersion,
|
||||||
)
|
)
|
||||||
@ -196,7 +202,11 @@ func (mc *mysqlConn) readInitPacket() ([]byte, error) {
|
|||||||
// return
|
// return
|
||||||
//}
|
//}
|
||||||
//return ErrMalformPkt
|
//return ErrMalformPkt
|
||||||
return cipher, nil
|
|
||||||
|
// make a memory safe copy of the cipher slice
|
||||||
|
var b [20]byte
|
||||||
|
copy(b[:], cipher)
|
||||||
|
return b[:], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// make a memory safe copy of the cipher slice
|
// make a memory safe copy of the cipher slice
|
||||||
@ -214,9 +224,11 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
|
|||||||
clientLongPassword |
|
clientLongPassword |
|
||||||
clientTransactions |
|
clientTransactions |
|
||||||
clientLocalFiles |
|
clientLocalFiles |
|
||||||
|
clientPluginAuth |
|
||||||
|
clientMultiResults |
|
||||||
mc.flags&clientLongFlag
|
mc.flags&clientLongFlag
|
||||||
|
|
||||||
if mc.cfg.clientFoundRows {
|
if mc.cfg.ClientFoundRows {
|
||||||
clientFlags |= clientFoundRows
|
clientFlags |= clientFoundRows
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,13 +237,17 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
|
|||||||
clientFlags |= clientSSL
|
clientFlags |= clientSSL
|
||||||
}
|
}
|
||||||
|
|
||||||
// User Password
|
if mc.cfg.MultiStatements {
|
||||||
scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.passwd))
|
clientFlags |= clientMultiStatements
|
||||||
|
}
|
||||||
|
|
||||||
pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.user) + 1 + 1 + len(scrambleBuff)
|
// User Password
|
||||||
|
scrambleBuff := scramblePassword(cipher, []byte(mc.cfg.Passwd))
|
||||||
|
|
||||||
|
pktLen := 4 + 4 + 1 + 23 + len(mc.cfg.User) + 1 + 1 + len(scrambleBuff) + 21 + 1
|
||||||
|
|
||||||
// To specify a db name
|
// To specify a db name
|
||||||
if n := len(mc.cfg.dbname); n > 0 {
|
if n := len(mc.cfg.DBName); n > 0 {
|
||||||
clientFlags |= clientConnectWithDB
|
clientFlags |= clientConnectWithDB
|
||||||
pktLen += n + 1
|
pktLen += n + 1
|
||||||
}
|
}
|
||||||
@ -257,7 +273,14 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
|
|||||||
data[11] = 0x00
|
data[11] = 0x00
|
||||||
|
|
||||||
// Charset [1 byte]
|
// Charset [1 byte]
|
||||||
data[12] = mc.cfg.collation
|
var found bool
|
||||||
|
data[12], found = collations[mc.cfg.Collation]
|
||||||
|
if !found {
|
||||||
|
// Note possibility for false negatives:
|
||||||
|
// could be triggered although the collation is valid if the
|
||||||
|
// collations map does not contain entries the server supports.
|
||||||
|
return errors.New("unknown collation")
|
||||||
|
}
|
||||||
|
|
||||||
// SSL Connection Request Packet
|
// SSL Connection Request Packet
|
||||||
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::SSLRequest
|
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::SSLRequest
|
||||||
@ -273,15 +296,18 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mc.netConn = tlsConn
|
mc.netConn = tlsConn
|
||||||
mc.buf.rd = tlsConn
|
mc.buf.nc = tlsConn
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filler [23 bytes] (all 0x00)
|
// Filler [23 bytes] (all 0x00)
|
||||||
pos := 13 + 23
|
pos := 13
|
||||||
|
for ; pos < 13+23; pos++ {
|
||||||
|
data[pos] = 0
|
||||||
|
}
|
||||||
|
|
||||||
// User [null terminated string]
|
// User [null terminated string]
|
||||||
if len(mc.cfg.user) > 0 {
|
if len(mc.cfg.User) > 0 {
|
||||||
pos += copy(data[pos:], mc.cfg.user)
|
pos += copy(data[pos:], mc.cfg.User)
|
||||||
}
|
}
|
||||||
data[pos] = 0x00
|
data[pos] = 0x00
|
||||||
pos++
|
pos++
|
||||||
@ -291,11 +317,16 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
|
|||||||
pos += 1 + copy(data[pos+1:], scrambleBuff)
|
pos += 1 + copy(data[pos+1:], scrambleBuff)
|
||||||
|
|
||||||
// Databasename [null terminated string]
|
// Databasename [null terminated string]
|
||||||
if len(mc.cfg.dbname) > 0 {
|
if len(mc.cfg.DBName) > 0 {
|
||||||
pos += copy(data[pos:], mc.cfg.dbname)
|
pos += copy(data[pos:], mc.cfg.DBName)
|
||||||
data[pos] = 0x00
|
data[pos] = 0x00
|
||||||
|
pos++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Assume native client during response
|
||||||
|
pos += copy(data[pos:], "mysql_native_password")
|
||||||
|
data[pos] = 0x00
|
||||||
|
|
||||||
// Send Auth packet
|
// Send Auth packet
|
||||||
return mc.writePacket(data)
|
return mc.writePacket(data)
|
||||||
}
|
}
|
||||||
@ -304,9 +335,9 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error {
|
|||||||
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
|
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
|
||||||
func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
|
func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
|
||||||
// User password
|
// User password
|
||||||
scrambleBuff := scrambleOldPassword(cipher, []byte(mc.cfg.passwd))
|
scrambleBuff := scrambleOldPassword(cipher, []byte(mc.cfg.Passwd))
|
||||||
|
|
||||||
// Calculate the packet lenght and add a tailing 0
|
// Calculate the packet length and add a tailing 0
|
||||||
pktLen := len(scrambleBuff) + 1
|
pktLen := len(scrambleBuff) + 1
|
||||||
data := mc.buf.takeSmallBuffer(4 + pktLen)
|
data := mc.buf.takeSmallBuffer(4 + pktLen)
|
||||||
if data == nil {
|
if data == nil {
|
||||||
@ -322,6 +353,25 @@ func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error {
|
|||||||
return mc.writePacket(data)
|
return mc.writePacket(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Client clear text authentication packet
|
||||||
|
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchResponse
|
||||||
|
func (mc *mysqlConn) writeClearAuthPacket() error {
|
||||||
|
// Calculate the packet length and add a tailing 0
|
||||||
|
pktLen := len(mc.cfg.Passwd) + 1
|
||||||
|
data := mc.buf.takeSmallBuffer(4 + pktLen)
|
||||||
|
if data == nil {
|
||||||
|
// can not take the buffer. Something must be wrong with the connection
|
||||||
|
errLog.Print(ErrBusyBuffer)
|
||||||
|
return driver.ErrBadConn
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the clear password [null terminated string]
|
||||||
|
copy(data[4:], mc.cfg.Passwd)
|
||||||
|
data[4+pktLen-1] = 0x00
|
||||||
|
|
||||||
|
return mc.writePacket(data)
|
||||||
|
}
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Command Packets *
|
* Command Packets *
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
@ -405,8 +455,20 @@ func (mc *mysqlConn) readResultOK() error {
|
|||||||
return mc.handleOkPacket(data)
|
return mc.handleOkPacket(data)
|
||||||
|
|
||||||
case iEOF:
|
case iEOF:
|
||||||
// someone is using old_passwords
|
if len(data) > 1 {
|
||||||
|
plugin := string(data[1:bytes.IndexByte(data, 0x00)])
|
||||||
|
if plugin == "mysql_old_password" {
|
||||||
|
// using old_passwords
|
||||||
return ErrOldPassword
|
return ErrOldPassword
|
||||||
|
} else if plugin == "mysql_clear_password" {
|
||||||
|
// using clear text password
|
||||||
|
return ErrCleartextPassword
|
||||||
|
} else {
|
||||||
|
return ErrUnknownPlugin
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return ErrOldPassword
|
||||||
|
}
|
||||||
|
|
||||||
default: // Error otherwise
|
default: // Error otherwise
|
||||||
return mc.handleErrorPacket(data)
|
return mc.handleErrorPacket(data)
|
||||||
@ -470,6 +532,10 @@ func (mc *mysqlConn) handleErrorPacket(data []byte) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readStatus(b []byte) statusFlag {
|
||||||
|
return statusFlag(b[0]) | statusFlag(b[1])<<8
|
||||||
|
}
|
||||||
|
|
||||||
// Ok Packet
|
// Ok Packet
|
||||||
// http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-OK_Packet
|
// http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-OK_Packet
|
||||||
func (mc *mysqlConn) handleOkPacket(data []byte) error {
|
func (mc *mysqlConn) handleOkPacket(data []byte) error {
|
||||||
@ -484,18 +550,22 @@ func (mc *mysqlConn) handleOkPacket(data []byte) error {
|
|||||||
mc.insertId, _, m = readLengthEncodedInteger(data[1+n:])
|
mc.insertId, _, m = readLengthEncodedInteger(data[1+n:])
|
||||||
|
|
||||||
// server_status [2 bytes]
|
// server_status [2 bytes]
|
||||||
|
mc.status = readStatus(data[1+n+m : 1+n+m+2])
|
||||||
|
if err := mc.discardResults(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// warning count [2 bytes]
|
// warning count [2 bytes]
|
||||||
if !mc.strict {
|
if !mc.strict {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
}
|
||||||
|
|
||||||
pos := 1 + n + m + 2
|
pos := 1 + n + m + 2
|
||||||
if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
|
if binary.LittleEndian.Uint16(data[pos:pos+2]) > 0 {
|
||||||
return mc.getWarnings()
|
return mc.getWarnings()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Read Packets as Field Packets until EOF-Packet or an Error appears
|
// Read Packets as Field Packets until EOF-Packet or an Error appears
|
||||||
// http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnDefinition41
|
// http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-Protocol::ColumnDefinition41
|
||||||
@ -513,7 +583,7 @@ func (mc *mysqlConn) readColumns(count int) ([]mysqlField, error) {
|
|||||||
if i == count {
|
if i == count {
|
||||||
return columns, nil
|
return columns, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("ColumnsCount mismatch n:%d len:%d", count, len(columns))
|
return nil, fmt.Errorf("column count mismatch n:%d len:%d", count, len(columns))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Catalog
|
// Catalog
|
||||||
@ -530,11 +600,20 @@ func (mc *mysqlConn) readColumns(count int) ([]mysqlField, error) {
|
|||||||
pos += n
|
pos += n
|
||||||
|
|
||||||
// Table [len coded string]
|
// Table [len coded string]
|
||||||
|
if mc.cfg.ColumnsWithAlias {
|
||||||
|
tableName, _, n, err := readLengthEncodedString(data[pos:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
pos += n
|
||||||
|
columns[i].tableName = string(tableName)
|
||||||
|
} else {
|
||||||
n, err = skipLengthEncodedString(data[pos:])
|
n, err = skipLengthEncodedString(data[pos:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
pos += n
|
pos += n
|
||||||
|
}
|
||||||
|
|
||||||
// Original table [len coded string]
|
// Original table [len coded string]
|
||||||
n, err = skipLengthEncodedString(data[pos:])
|
n, err = skipLengthEncodedString(data[pos:])
|
||||||
@ -593,8 +672,18 @@ func (rows *textRows) readRow(dest []driver.Value) error {
|
|||||||
|
|
||||||
// EOF Packet
|
// EOF Packet
|
||||||
if data[0] == iEOF && len(data) == 5 {
|
if data[0] == iEOF && len(data) == 5 {
|
||||||
|
// server_status [2 bytes]
|
||||||
|
rows.mc.status = readStatus(data[3:])
|
||||||
|
if err := rows.mc.discardResults(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows.mc = nil
|
||||||
return io.EOF
|
return io.EOF
|
||||||
}
|
}
|
||||||
|
if data[0] == iERR {
|
||||||
|
rows.mc = nil
|
||||||
|
return mc.handleErrorPacket(data)
|
||||||
|
}
|
||||||
|
|
||||||
// RowSet Packet
|
// RowSet Packet
|
||||||
var n int
|
var n int
|
||||||
@ -615,7 +704,7 @@ func (rows *textRows) readRow(dest []driver.Value) error {
|
|||||||
fieldTypeDate, fieldTypeNewDate:
|
fieldTypeDate, fieldTypeNewDate:
|
||||||
dest[i], err = parseDateTime(
|
dest[i], err = parseDateTime(
|
||||||
string(dest[i].([]byte)),
|
string(dest[i].([]byte)),
|
||||||
mc.cfg.loc,
|
mc.cfg.Loc,
|
||||||
)
|
)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
continue
|
continue
|
||||||
@ -645,6 +734,10 @@ func (mc *mysqlConn) readUntilEOF() error {
|
|||||||
if err == nil && data[0] != iEOF {
|
if err == nil && data[0] != iEOF {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if err == nil && data[0] == iEOF && len(data) == 5 {
|
||||||
|
mc.status = readStatus(data[3:])
|
||||||
|
}
|
||||||
|
|
||||||
return err // Err or EOF
|
return err // Err or EOF
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -677,14 +770,14 @@ func (stmt *mysqlStmt) readPrepareResultPacket() (uint16, error) {
|
|||||||
// Warning count [16 bit uint]
|
// Warning count [16 bit uint]
|
||||||
if !stmt.mc.strict {
|
if !stmt.mc.strict {
|
||||||
return columnCount, nil
|
return columnCount, nil
|
||||||
} else {
|
}
|
||||||
|
|
||||||
// Check for warnings count > 0, only available in MySQL > 4.1
|
// Check for warnings count > 0, only available in MySQL > 4.1
|
||||||
if len(data) >= 12 && binary.LittleEndian.Uint16(data[10:12]) > 0 {
|
if len(data) >= 12 && binary.LittleEndian.Uint16(data[10:12]) > 0 {
|
||||||
return columnCount, stmt.mc.getWarnings()
|
return columnCount, stmt.mc.getWarnings()
|
||||||
}
|
}
|
||||||
return columnCount, nil
|
return columnCount, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -745,7 +838,7 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
|
|||||||
func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
|
func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
|
||||||
if len(args) != stmt.paramCount {
|
if len(args) != stmt.paramCount {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"Arguments count mismatch (Got: %d Has: %d)",
|
"argument count mismatch (got: %d; has: %d)",
|
||||||
len(args),
|
len(args),
|
||||||
stmt.paramCount,
|
stmt.paramCount,
|
||||||
)
|
)
|
||||||
@ -922,7 +1015,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
|
|||||||
if v.IsZero() {
|
if v.IsZero() {
|
||||||
val = []byte("0000-00-00")
|
val = []byte("0000-00-00")
|
||||||
} else {
|
} else {
|
||||||
val = []byte(v.In(mc.cfg.loc).Format(timeFormat))
|
val = []byte(v.In(mc.cfg.Loc).Format(timeFormat))
|
||||||
}
|
}
|
||||||
|
|
||||||
paramValues = appendLengthEncodedInteger(paramValues,
|
paramValues = appendLengthEncodedInteger(paramValues,
|
||||||
@ -931,7 +1024,7 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
|
|||||||
paramValues = append(paramValues, val...)
|
paramValues = append(paramValues, val...)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Can't convert type: %T", arg)
|
return fmt.Errorf("can not convert type: %T", arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -949,6 +1042,28 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error {
|
|||||||
return mc.writePacket(data)
|
return mc.writePacket(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mc *mysqlConn) discardResults() error {
|
||||||
|
for mc.status&statusMoreResultsExists != 0 {
|
||||||
|
resLen, err := mc.readResultSetHeaderPacket()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if resLen > 0 {
|
||||||
|
// columns
|
||||||
|
if err := mc.readUntilEOF(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// rows
|
||||||
|
if err := mc.readUntilEOF(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
mc.status &^= statusMoreResultsExists
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// http://dev.mysql.com/doc/internals/en/binary-protocol-resultset-row.html
|
// http://dev.mysql.com/doc/internals/en/binary-protocol-resultset-row.html
|
||||||
func (rows *binaryRows) readRow(dest []driver.Value) error {
|
func (rows *binaryRows) readRow(dest []driver.Value) error {
|
||||||
data, err := rows.mc.readPacket()
|
data, err := rows.mc.readPacket()
|
||||||
@ -960,8 +1075,14 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
|
|||||||
if data[0] != iOK {
|
if data[0] != iOK {
|
||||||
// EOF Packet
|
// EOF Packet
|
||||||
if data[0] == iEOF && len(data) == 5 {
|
if data[0] == iEOF && len(data) == 5 {
|
||||||
|
rows.mc.status = readStatus(data[3:])
|
||||||
|
if err := rows.mc.discardResults(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
rows.mc = nil
|
||||||
return io.EOF
|
return io.EOF
|
||||||
}
|
}
|
||||||
|
rows.mc = nil
|
||||||
|
|
||||||
// Error otherwise
|
// Error otherwise
|
||||||
return rows.mc.handleErrorPacket(data)
|
return rows.mc.handleErrorPacket(data)
|
||||||
@ -1041,7 +1162,7 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
|
|||||||
case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
|
case fieldTypeDecimal, fieldTypeNewDecimal, fieldTypeVarChar,
|
||||||
fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
|
fieldTypeBit, fieldTypeEnum, fieldTypeSet, fieldTypeTinyBLOB,
|
||||||
fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
|
fieldTypeMediumBLOB, fieldTypeLongBLOB, fieldTypeBLOB,
|
||||||
fieldTypeVarString, fieldTypeString, fieldTypeGeometry:
|
fieldTypeVarString, fieldTypeString, fieldTypeGeometry, fieldTypeJSON:
|
||||||
var isNull bool
|
var isNull bool
|
||||||
var n int
|
var n int
|
||||||
dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
|
dest[i], isNull, n, err = readLengthEncodedString(data[pos:])
|
||||||
@ -1078,13 +1199,13 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
|
|||||||
dstlen = 8 + 1 + decimals
|
dstlen = 8 + 1 + decimals
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"MySQL protocol error, illegal decimals value %d",
|
"protocol error, illegal decimals value %d",
|
||||||
rows.columns[i].decimals,
|
rows.columns[i].decimals,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
dest[i], err = formatBinaryDateTime(data[pos:pos+int(num)], dstlen, true)
|
dest[i], err = formatBinaryDateTime(data[pos:pos+int(num)], dstlen, true)
|
||||||
case rows.mc.parseTime:
|
case rows.mc.parseTime:
|
||||||
dest[i], err = parseBinaryDateTime(num, data[pos:], rows.mc.cfg.loc)
|
dest[i], err = parseBinaryDateTime(num, data[pos:], rows.mc.cfg.Loc)
|
||||||
default:
|
default:
|
||||||
var dstlen uint8
|
var dstlen uint8
|
||||||
if rows.columns[i].fieldType == fieldTypeDate {
|
if rows.columns[i].fieldType == fieldTypeDate {
|
||||||
@ -1097,7 +1218,7 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
|
|||||||
dstlen = 19 + 1 + decimals
|
dstlen = 19 + 1 + decimals
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"MySQL protocol error, illegal decimals value %d",
|
"protocol error, illegal decimals value %d",
|
||||||
rows.columns[i].decimals,
|
rows.columns[i].decimals,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -1114,7 +1235,7 @@ func (rows *binaryRows) readRow(dest []driver.Value) error {
|
|||||||
|
|
||||||
// Please report if this happens!
|
// Please report if this happens!
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("Unknown FieldType %d", rows.columns[i].fieldType)
|
return fmt.Errorf("unknown field type %d", rows.columns[i].fieldType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
27
Godeps/_workspace/src/github.com/go-sql-driver/mysql/rows.go
generated
vendored
27
Godeps/_workspace/src/github.com/go-sql-driver/mysql/rows.go
generated
vendored
@ -14,6 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type mysqlField struct {
|
type mysqlField struct {
|
||||||
|
tableName string
|
||||||
name string
|
name string
|
||||||
flags fieldFlag
|
flags fieldFlag
|
||||||
fieldType byte
|
fieldType byte
|
||||||
@ -37,9 +38,19 @@ type emptyRows struct{}
|
|||||||
|
|
||||||
func (rows *mysqlRows) Columns() []string {
|
func (rows *mysqlRows) Columns() []string {
|
||||||
columns := make([]string, len(rows.columns))
|
columns := make([]string, len(rows.columns))
|
||||||
|
if rows.mc != nil && rows.mc.cfg.ColumnsWithAlias {
|
||||||
|
for i := range columns {
|
||||||
|
if tableName := rows.columns[i].tableName; len(tableName) > 0 {
|
||||||
|
columns[i] = tableName + "." + rows.columns[i].name
|
||||||
|
} else {
|
||||||
|
columns[i] = rows.columns[i].name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for i := range columns {
|
for i := range columns {
|
||||||
columns[i] = rows.columns[i].name
|
columns[i] = rows.columns[i].name
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return columns
|
return columns
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,6 +65,12 @@ func (rows *mysqlRows) Close() error {
|
|||||||
|
|
||||||
// Remove unread packets from stream
|
// Remove unread packets from stream
|
||||||
err := mc.readUntilEOF()
|
err := mc.readUntilEOF()
|
||||||
|
if err == nil {
|
||||||
|
if err = mc.discardResults(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rows.mc = nil
|
rows.mc = nil
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -65,10 +82,7 @@ func (rows *binaryRows) Next(dest []driver.Value) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch next row from stream
|
// Fetch next row from stream
|
||||||
if err := rows.readRow(dest); err != io.EOF {
|
return rows.readRow(dest)
|
||||||
return err
|
|
||||||
}
|
|
||||||
rows.mc = nil
|
|
||||||
}
|
}
|
||||||
return io.EOF
|
return io.EOF
|
||||||
}
|
}
|
||||||
@ -80,10 +94,7 @@ func (rows *textRows) Next(dest []driver.Value) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch next row from stream
|
// Fetch next row from stream
|
||||||
if err := rows.readRow(dest); err != io.EOF {
|
return rows.readRow(dest)
|
||||||
return err
|
|
||||||
}
|
|
||||||
rows.mc = nil
|
|
||||||
}
|
}
|
||||||
return io.EOF
|
return io.EOF
|
||||||
}
|
}
|
||||||
|
40
Godeps/_workspace/src/github.com/go-sql-driver/mysql/statement.go
generated
vendored
40
Godeps/_workspace/src/github.com/go-sql-driver/mysql/statement.go
generated
vendored
@ -10,6 +10,9 @@ package mysql
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mysqlStmt struct {
|
type mysqlStmt struct {
|
||||||
@ -34,6 +37,10 @@ func (stmt *mysqlStmt) NumInput() int {
|
|||||||
return stmt.paramCount
|
return stmt.paramCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (stmt *mysqlStmt) ColumnConverter(idx int) driver.ValueConverter {
|
||||||
|
return converter{}
|
||||||
|
}
|
||||||
|
|
||||||
func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
|
func (stmt *mysqlStmt) Exec(args []driver.Value) (driver.Result, error) {
|
||||||
if stmt.mc.netConn == nil {
|
if stmt.mc.netConn == nil {
|
||||||
errLog.Print(ErrInvalidConn)
|
errLog.Print(ErrInvalidConn)
|
||||||
@ -94,9 +101,9 @@ func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rows := new(binaryRows)
|
rows := new(binaryRows)
|
||||||
rows.mc = mc
|
|
||||||
|
|
||||||
if resLen > 0 {
|
if resLen > 0 {
|
||||||
|
rows.mc = mc
|
||||||
// Columns
|
// Columns
|
||||||
// If not cached, read them and cache them
|
// If not cached, read them and cache them
|
||||||
if stmt.columns == nil {
|
if stmt.columns == nil {
|
||||||
@ -110,3 +117,34 @@ func (stmt *mysqlStmt) Query(args []driver.Value) (driver.Rows, error) {
|
|||||||
|
|
||||||
return rows, err
|
return rows, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type converter struct{}
|
||||||
|
|
||||||
|
func (c converter) ConvertValue(v interface{}) (driver.Value, error) {
|
||||||
|
if driver.IsValue(v) {
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rv := reflect.ValueOf(v)
|
||||||
|
switch rv.Kind() {
|
||||||
|
case reflect.Ptr:
|
||||||
|
// indirect pointers
|
||||||
|
if rv.IsNil() {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return c.ConvertValue(rv.Elem().Interface())
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
return rv.Int(), nil
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
||||||
|
return int64(rv.Uint()), nil
|
||||||
|
case reflect.Uint64:
|
||||||
|
u64 := rv.Uint()
|
||||||
|
if u64 >= 1<<63 {
|
||||||
|
return strconv.FormatUint(u64, 10), nil
|
||||||
|
}
|
||||||
|
return int64(u64), nil
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
return rv.Float(), nil
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("unsupported type %T, a %s", v, rv.Kind())
|
||||||
|
}
|
||||||
|
377
Godeps/_workspace/src/github.com/go-sql-driver/mysql/utils.go
generated
vendored
377
Godeps/_workspace/src/github.com/go-sql-driver/mysql/utils.go
generated
vendored
@ -13,26 +13,16 @@ import (
|
|||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/url"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tlsConfigRegister map[string]*tls.Config // Register for custom tls.Configs
|
tlsConfigRegister map[string]*tls.Config // Register for custom tls.Configs
|
||||||
|
|
||||||
errInvalidDSNUnescaped = errors.New("Invalid DSN: Did you forget to escape a param value?")
|
|
||||||
errInvalidDSNAddr = errors.New("Invalid DSN: Network Address not terminated (missing closing brace)")
|
|
||||||
errInvalidDSNNoSlash = errors.New("Invalid DSN: Missing the slash separating the database name")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
tlsConfigRegister = make(map[string]*tls.Config)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
|
// RegisterTLSConfig registers a custom tls.Config to be used with sql.Open.
|
||||||
// Use the key as a value in the DSN where tls=value.
|
// Use the key as a value in the DSN where tls=value.
|
||||||
//
|
//
|
||||||
@ -58,7 +48,11 @@ func init() {
|
|||||||
//
|
//
|
||||||
func RegisterTLSConfig(key string, config *tls.Config) error {
|
func RegisterTLSConfig(key string, config *tls.Config) error {
|
||||||
if _, isBool := readBool(key); isBool || strings.ToLower(key) == "skip-verify" {
|
if _, isBool := readBool(key); isBool || strings.ToLower(key) == "skip-verify" {
|
||||||
return fmt.Errorf("Key '%s' is reserved", key)
|
return fmt.Errorf("key '%s' is reserved", key)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tlsConfigRegister == nil {
|
||||||
|
tlsConfigRegister = make(map[string]*tls.Config)
|
||||||
}
|
}
|
||||||
|
|
||||||
tlsConfigRegister[key] = config
|
tlsConfigRegister[key] = config
|
||||||
@ -67,202 +61,9 @@ func RegisterTLSConfig(key string, config *tls.Config) error {
|
|||||||
|
|
||||||
// DeregisterTLSConfig removes the tls.Config associated with key.
|
// DeregisterTLSConfig removes the tls.Config associated with key.
|
||||||
func DeregisterTLSConfig(key string) {
|
func DeregisterTLSConfig(key string) {
|
||||||
|
if tlsConfigRegister != nil {
|
||||||
delete(tlsConfigRegister, key)
|
delete(tlsConfigRegister, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseDSN parses the DSN string to a config
|
|
||||||
func parseDSN(dsn string) (cfg *config, err error) {
|
|
||||||
// New config with some default values
|
|
||||||
cfg = &config{
|
|
||||||
loc: time.UTC,
|
|
||||||
collation: defaultCollation,
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: use strings.IndexByte when we can depend on Go 1.2
|
|
||||||
|
|
||||||
// [user[:password]@][net[(addr)]]/dbname[?param1=value1¶mN=valueN]
|
|
||||||
// Find the last '/' (since the password or the net addr might contain a '/')
|
|
||||||
foundSlash := false
|
|
||||||
for i := len(dsn) - 1; i >= 0; i-- {
|
|
||||||
if dsn[i] == '/' {
|
|
||||||
foundSlash = true
|
|
||||||
var j, k int
|
|
||||||
|
|
||||||
// left part is empty if i <= 0
|
|
||||||
if i > 0 {
|
|
||||||
// [username[:password]@][protocol[(address)]]
|
|
||||||
// Find the last '@' in dsn[:i]
|
|
||||||
for j = i; j >= 0; j-- {
|
|
||||||
if dsn[j] == '@' {
|
|
||||||
// username[:password]
|
|
||||||
// Find the first ':' in dsn[:j]
|
|
||||||
for k = 0; k < j; k++ {
|
|
||||||
if dsn[k] == ':' {
|
|
||||||
cfg.passwd = dsn[k+1 : j]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cfg.user = dsn[:k]
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// [protocol[(address)]]
|
|
||||||
// Find the first '(' in dsn[j+1:i]
|
|
||||||
for k = j + 1; k < i; k++ {
|
|
||||||
if dsn[k] == '(' {
|
|
||||||
// dsn[i-1] must be == ')' if an address is specified
|
|
||||||
if dsn[i-1] != ')' {
|
|
||||||
if strings.ContainsRune(dsn[k+1:i], ')') {
|
|
||||||
return nil, errInvalidDSNUnescaped
|
|
||||||
}
|
|
||||||
return nil, errInvalidDSNAddr
|
|
||||||
}
|
|
||||||
cfg.addr = dsn[k+1 : i-1]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cfg.net = dsn[j+1 : k]
|
|
||||||
}
|
|
||||||
|
|
||||||
// dbname[?param1=value1&...¶mN=valueN]
|
|
||||||
// Find the first '?' in dsn[i+1:]
|
|
||||||
for j = i + 1; j < len(dsn); j++ {
|
|
||||||
if dsn[j] == '?' {
|
|
||||||
if err = parseDSNParams(cfg, dsn[j+1:]); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cfg.dbname = dsn[i+1 : j]
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !foundSlash && len(dsn) > 0 {
|
|
||||||
return nil, errInvalidDSNNoSlash
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set default network if empty
|
|
||||||
if cfg.net == "" {
|
|
||||||
cfg.net = "tcp"
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set default address if empty
|
|
||||||
if cfg.addr == "" {
|
|
||||||
switch cfg.net {
|
|
||||||
case "tcp":
|
|
||||||
cfg.addr = "127.0.0.1:3306"
|
|
||||||
case "unix":
|
|
||||||
cfg.addr = "/tmp/mysql.sock"
|
|
||||||
default:
|
|
||||||
return nil, errors.New("Default addr for network '" + cfg.net + "' unknown")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// parseDSNParams parses the DSN "query string"
|
|
||||||
// Values must be url.QueryEscape'ed
|
|
||||||
func parseDSNParams(cfg *config, params string) (err error) {
|
|
||||||
for _, v := range strings.Split(params, "&") {
|
|
||||||
param := strings.SplitN(v, "=", 2)
|
|
||||||
if len(param) != 2 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// cfg params
|
|
||||||
switch value := param[1]; param[0] {
|
|
||||||
|
|
||||||
// Disable INFILE whitelist / enable all files
|
|
||||||
case "allowAllFiles":
|
|
||||||
var isBool bool
|
|
||||||
cfg.allowAllFiles, isBool = readBool(value)
|
|
||||||
if !isBool {
|
|
||||||
return fmt.Errorf("Invalid Bool value: %s", value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use old authentication mode (pre MySQL 4.1)
|
|
||||||
case "allowOldPasswords":
|
|
||||||
var isBool bool
|
|
||||||
cfg.allowOldPasswords, isBool = readBool(value)
|
|
||||||
if !isBool {
|
|
||||||
return fmt.Errorf("Invalid Bool value: %s", value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Switch "rowsAffected" mode
|
|
||||||
case "clientFoundRows":
|
|
||||||
var isBool bool
|
|
||||||
cfg.clientFoundRows, isBool = readBool(value)
|
|
||||||
if !isBool {
|
|
||||||
return fmt.Errorf("Invalid Bool value: %s", value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Collation
|
|
||||||
case "collation":
|
|
||||||
collation, ok := collations[value]
|
|
||||||
if !ok {
|
|
||||||
// Note possibility for false negatives:
|
|
||||||
// could be triggered although the collation is valid if the
|
|
||||||
// collations map does not contain entries the server supports.
|
|
||||||
err = errors.New("unknown collation")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cfg.collation = collation
|
|
||||||
break
|
|
||||||
|
|
||||||
// Time Location
|
|
||||||
case "loc":
|
|
||||||
if value, err = url.QueryUnescape(value); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cfg.loc, err = time.LoadLocation(value)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dial Timeout
|
|
||||||
case "timeout":
|
|
||||||
cfg.timeout, err = time.ParseDuration(value)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// TLS-Encryption
|
|
||||||
case "tls":
|
|
||||||
boolValue, isBool := readBool(value)
|
|
||||||
if isBool {
|
|
||||||
if boolValue {
|
|
||||||
cfg.tls = &tls.Config{}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if strings.ToLower(value) == "skip-verify" {
|
|
||||||
cfg.tls = &tls.Config{InsecureSkipVerify: true}
|
|
||||||
} else if tlsConfig, ok := tlsConfigRegister[value]; ok {
|
|
||||||
cfg.tls = tlsConfig
|
|
||||||
} else {
|
|
||||||
return fmt.Errorf("Invalid value / unknown config name: %s", value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
// lazy init
|
|
||||||
if cfg.params == nil {
|
|
||||||
cfg.params = make(map[string]string)
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.params[param[0]], err = url.QueryUnescape(value); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the bool value of the input.
|
// Returns the bool value of the input.
|
||||||
@ -459,7 +260,7 @@ func parseDateTime(str string, loc *time.Location) (t time.Time, err error) {
|
|||||||
}
|
}
|
||||||
t, err = time.Parse(timeFormat[:len(str)], str)
|
t, err = time.Parse(timeFormat[:len(str)], str)
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("Invalid Time-String: %s", str)
|
err = fmt.Errorf("invalid time string: %s", str)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -508,7 +309,7 @@ func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Va
|
|||||||
loc,
|
loc,
|
||||||
), nil
|
), nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("Invalid DATETIME-packet length %d", num)
|
return nil, fmt.Errorf("invalid DATETIME packet length %d", num)
|
||||||
}
|
}
|
||||||
|
|
||||||
// zeroDateTime is used in formatBinaryDateTime to avoid an allocation
|
// zeroDateTime is used in formatBinaryDateTime to avoid an allocation
|
||||||
@ -517,11 +318,12 @@ func parseBinaryDateTime(num uint64, data []byte, loc *time.Location) (driver.Va
|
|||||||
// The current behavior depends on database/sql copying the result.
|
// The current behavior depends on database/sql copying the result.
|
||||||
var zeroDateTime = []byte("0000-00-00 00:00:00.000000")
|
var zeroDateTime = []byte("0000-00-00 00:00:00.000000")
|
||||||
|
|
||||||
|
const digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
||||||
|
const digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
|
||||||
|
|
||||||
func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value, error) {
|
func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value, error) {
|
||||||
// length expects the deterministic length of the zero value,
|
// length expects the deterministic length of the zero value,
|
||||||
// negative time and 100+ hours are automatically added if needed
|
// negative time and 100+ hours are automatically added if needed
|
||||||
const digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
|
|
||||||
const digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
|
|
||||||
if len(src) == 0 {
|
if len(src) == 0 {
|
||||||
if justTime {
|
if justTime {
|
||||||
return zeroDateTime[11 : 11+length], nil
|
return zeroDateTime[11 : 11+length], nil
|
||||||
@ -542,7 +344,7 @@ func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value
|
|||||||
switch len(src) {
|
switch len(src) {
|
||||||
case 8, 12:
|
case 8, 12:
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Invalid TIME-packet length %d", len(src))
|
return nil, fmt.Errorf("invalid TIME packet length %d", len(src))
|
||||||
}
|
}
|
||||||
// +2 to enable negative time and 100+ hours
|
// +2 to enable negative time and 100+ hours
|
||||||
dst = make([]byte, 0, length+2)
|
dst = make([]byte, 0, length+2)
|
||||||
@ -576,7 +378,7 @@ func formatBinaryDateTime(src []byte, length uint8, justTime bool) (driver.Value
|
|||||||
if length > 10 {
|
if length > 10 {
|
||||||
t += "TIME"
|
t += "TIME"
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("illegal %s-packet length %d", t, len(src))
|
return nil, fmt.Errorf("illegal %s packet length %d", t, len(src))
|
||||||
}
|
}
|
||||||
dst = make([]byte, 0, length)
|
dst = make([]byte, 0, length)
|
||||||
// start with the date
|
// start with the date
|
||||||
@ -742,6 +544,10 @@ func skipLengthEncodedString(b []byte) (int, error) {
|
|||||||
|
|
||||||
// returns the number read, whether the value is NULL and the number of bytes read
|
// returns the number read, whether the value is NULL and the number of bytes read
|
||||||
func readLengthEncodedInteger(b []byte) (uint64, bool, int) {
|
func readLengthEncodedInteger(b []byte) (uint64, bool, int) {
|
||||||
|
// See issue #349
|
||||||
|
if len(b) == 0 {
|
||||||
|
return 0, true, 1
|
||||||
|
}
|
||||||
switch b[0] {
|
switch b[0] {
|
||||||
|
|
||||||
// 251: NULL
|
// 251: NULL
|
||||||
@ -783,3 +589,152 @@ func appendLengthEncodedInteger(b []byte, n uint64) []byte {
|
|||||||
return append(b, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
|
return append(b, 0xfe, byte(n), byte(n>>8), byte(n>>16), byte(n>>24),
|
||||||
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
|
byte(n>>32), byte(n>>40), byte(n>>48), byte(n>>56))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// reserveBuffer checks cap(buf) and expand buffer to len(buf) + appendSize.
|
||||||
|
// If cap(buf) is not enough, reallocate new buffer.
|
||||||
|
func reserveBuffer(buf []byte, appendSize int) []byte {
|
||||||
|
newSize := len(buf) + appendSize
|
||||||
|
if cap(buf) < newSize {
|
||||||
|
// Grow buffer exponentially
|
||||||
|
newBuf := make([]byte, len(buf)*2+appendSize)
|
||||||
|
copy(newBuf, buf)
|
||||||
|
buf = newBuf
|
||||||
|
}
|
||||||
|
return buf[:newSize]
|
||||||
|
}
|
||||||
|
|
||||||
|
// escapeBytesBackslash escapes []byte with backslashes (\)
|
||||||
|
// This escapes the contents of a string (provided as []byte) by adding backslashes before special
|
||||||
|
// characters, and turning others into specific escape sequences, such as
|
||||||
|
// turning newlines into \n and null bytes into \0.
|
||||||
|
// https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L823-L932
|
||||||
|
func escapeBytesBackslash(buf, v []byte) []byte {
|
||||||
|
pos := len(buf)
|
||||||
|
buf = reserveBuffer(buf, len(v)*2)
|
||||||
|
|
||||||
|
for _, c := range v {
|
||||||
|
switch c {
|
||||||
|
case '\x00':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '0'
|
||||||
|
pos += 2
|
||||||
|
case '\n':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'n'
|
||||||
|
pos += 2
|
||||||
|
case '\r':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'r'
|
||||||
|
pos += 2
|
||||||
|
case '\x1a':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'Z'
|
||||||
|
pos += 2
|
||||||
|
case '\'':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '\''
|
||||||
|
pos += 2
|
||||||
|
case '"':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '"'
|
||||||
|
pos += 2
|
||||||
|
case '\\':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '\\'
|
||||||
|
pos += 2
|
||||||
|
default:
|
||||||
|
buf[pos] = c
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[:pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
// escapeStringBackslash is similar to escapeBytesBackslash but for string.
|
||||||
|
func escapeStringBackslash(buf []byte, v string) []byte {
|
||||||
|
pos := len(buf)
|
||||||
|
buf = reserveBuffer(buf, len(v)*2)
|
||||||
|
|
||||||
|
for i := 0; i < len(v); i++ {
|
||||||
|
c := v[i]
|
||||||
|
switch c {
|
||||||
|
case '\x00':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '0'
|
||||||
|
pos += 2
|
||||||
|
case '\n':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'n'
|
||||||
|
pos += 2
|
||||||
|
case '\r':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'r'
|
||||||
|
pos += 2
|
||||||
|
case '\x1a':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = 'Z'
|
||||||
|
pos += 2
|
||||||
|
case '\'':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '\''
|
||||||
|
pos += 2
|
||||||
|
case '"':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '"'
|
||||||
|
pos += 2
|
||||||
|
case '\\':
|
||||||
|
buf[pos] = '\\'
|
||||||
|
buf[pos+1] = '\\'
|
||||||
|
pos += 2
|
||||||
|
default:
|
||||||
|
buf[pos] = c
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[:pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
// escapeBytesQuotes escapes apostrophes in []byte by doubling them up.
|
||||||
|
// This escapes the contents of a string by doubling up any apostrophes that
|
||||||
|
// it contains. This is used when the NO_BACKSLASH_ESCAPES SQL_MODE is in
|
||||||
|
// effect on the server.
|
||||||
|
// https://github.com/mysql/mysql-server/blob/mysql-5.7.5/mysys/charset.c#L963-L1038
|
||||||
|
func escapeBytesQuotes(buf, v []byte) []byte {
|
||||||
|
pos := len(buf)
|
||||||
|
buf = reserveBuffer(buf, len(v)*2)
|
||||||
|
|
||||||
|
for _, c := range v {
|
||||||
|
if c == '\'' {
|
||||||
|
buf[pos] = '\''
|
||||||
|
buf[pos+1] = '\''
|
||||||
|
pos += 2
|
||||||
|
} else {
|
||||||
|
buf[pos] = c
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[:pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
// escapeStringQuotes is similar to escapeBytesQuotes but for string.
|
||||||
|
func escapeStringQuotes(buf []byte, v string) []byte {
|
||||||
|
pos := len(buf)
|
||||||
|
buf = reserveBuffer(buf, len(v)*2)
|
||||||
|
|
||||||
|
for i := 0; i < len(v); i++ {
|
||||||
|
c := v[i]
|
||||||
|
if c == '\'' {
|
||||||
|
buf[pos] = '\''
|
||||||
|
buf[pos+1] = '\''
|
||||||
|
pos += 2
|
||||||
|
} else {
|
||||||
|
buf[pos] = c
|
||||||
|
pos++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf[:pos]
|
||||||
|
}
|
||||||
|
125
Godeps/_workspace/src/github.com/go-sql-driver/mysql/utils_test.go
generated
vendored
125
Godeps/_workspace/src/github.com/go-sql-driver/mysql/utils_test.go
generated
vendored
@ -16,76 +16,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var testDSNs = []struct {
|
|
||||||
in string
|
|
||||||
out string
|
|
||||||
loc *time.Location
|
|
||||||
}{
|
|
||||||
{"username:password@protocol(address)/dbname?param=value", "&{user:username passwd:password net:protocol addr:address dbname:dbname params:map[param:value] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
{"user@unix(/path/to/socket)/dbname?charset=utf8", "&{user:user passwd: net:unix addr:/path/to/socket dbname:dbname params:map[charset:utf8] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
{"user:password@tcp(localhost:5555)/dbname?charset=utf8&tls=true", "&{user:user passwd:password net:tcp addr:localhost:5555 dbname:dbname params:map[charset:utf8] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
{"user:password@tcp(localhost:5555)/dbname?charset=utf8mb4,utf8&tls=skip-verify", "&{user:user passwd:password net:tcp addr:localhost:5555 dbname:dbname params:map[charset:utf8mb4,utf8] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
{"user:password@/dbname?loc=UTC&timeout=30s&allowAllFiles=1&clientFoundRows=true&allowOldPasswords=TRUE&collation=utf8mb4_unicode_ci", "&{user:user passwd:password net:tcp addr:127.0.0.1:3306 dbname:dbname params:map[] loc:%p tls:<nil> timeout:30000000000 collation:224 allowAllFiles:true allowOldPasswords:true clientFoundRows:true}", time.UTC},
|
|
||||||
{"user:p@ss(word)@tcp([de:ad:be:ef::ca:fe]:80)/dbname?loc=Local", "&{user:user passwd:p@ss(word) net:tcp addr:[de:ad:be:ef::ca:fe]:80 dbname:dbname params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.Local},
|
|
||||||
{"/dbname", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname:dbname params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
{"@/", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
{"/", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
{"", "&{user: passwd: net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
{"user:p@/ssword@/", "&{user:user passwd:p@/ssword net:tcp addr:127.0.0.1:3306 dbname: params:map[] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
{"unix/?arg=%2Fsome%2Fpath.ext", "&{user: passwd: net:unix addr:/tmp/mysql.sock dbname: params:map[arg:/some/path.ext] loc:%p tls:<nil> timeout:0 collation:33 allowAllFiles:false allowOldPasswords:false clientFoundRows:false}", time.UTC},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDSNParser(t *testing.T) {
|
|
||||||
var cfg *config
|
|
||||||
var err error
|
|
||||||
var res string
|
|
||||||
|
|
||||||
for i, tst := range testDSNs {
|
|
||||||
cfg, err = parseDSN(tst.in)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
// pointer not static
|
|
||||||
cfg.tls = nil
|
|
||||||
|
|
||||||
res = fmt.Sprintf("%+v", cfg)
|
|
||||||
if res != fmt.Sprintf(tst.out, tst.loc) {
|
|
||||||
t.Errorf("%d. parseDSN(%q) => %q, want %q", i, tst.in, res, fmt.Sprintf(tst.out, tst.loc))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDSNParserInvalid(t *testing.T) {
|
|
||||||
var invalidDSNs = []string{
|
|
||||||
"@net(addr/", // no closing brace
|
|
||||||
"@tcp(/", // no closing brace
|
|
||||||
"tcp(/", // no closing brace
|
|
||||||
"(/", // no closing brace
|
|
||||||
"net(addr)//", // unescaped
|
|
||||||
"user:pass@tcp(1.2.3.4:3306)", // no trailing slash
|
|
||||||
//"/dbname?arg=/some/unescaped/path",
|
|
||||||
}
|
|
||||||
|
|
||||||
for i, tst := range invalidDSNs {
|
|
||||||
if _, err := parseDSN(tst); err == nil {
|
|
||||||
t.Errorf("invalid DSN #%d. (%s) didn't error!", i, tst)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkParseDSN(b *testing.B) {
|
|
||||||
b.ReportAllocs()
|
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
for _, tst := range testDSNs {
|
|
||||||
if _, err := parseDSN(tst.in); err != nil {
|
|
||||||
b.Error(err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestScanNullTime(t *testing.T) {
|
func TestScanNullTime(t *testing.T) {
|
||||||
var scanTests = []struct {
|
var scanTests = []struct {
|
||||||
in interface{}
|
in interface{}
|
||||||
@ -210,3 +140,58 @@ func TestFormatBinaryDateTime(t *testing.T) {
|
|||||||
expect("1978-12-30 15:46:23", 7, 19)
|
expect("1978-12-30 15:46:23", 7, 19)
|
||||||
expect("1978-12-30 15:46:23.987654", 11, 26)
|
expect("1978-12-30 15:46:23.987654", 11, 26)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEscapeBackslash(t *testing.T) {
|
||||||
|
expect := func(expected, value string) {
|
||||||
|
actual := string(escapeBytesBackslash([]byte{}, []byte(value)))
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf(
|
||||||
|
"expected %s, got %s",
|
||||||
|
expected, actual,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual = string(escapeStringBackslash([]byte{}, value))
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf(
|
||||||
|
"expected %s, got %s",
|
||||||
|
expected, actual,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expect("foo\\0bar", "foo\x00bar")
|
||||||
|
expect("foo\\nbar", "foo\nbar")
|
||||||
|
expect("foo\\rbar", "foo\rbar")
|
||||||
|
expect("foo\\Zbar", "foo\x1abar")
|
||||||
|
expect("foo\\\"bar", "foo\"bar")
|
||||||
|
expect("foo\\\\bar", "foo\\bar")
|
||||||
|
expect("foo\\'bar", "foo'bar")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEscapeQuotes(t *testing.T) {
|
||||||
|
expect := func(expected, value string) {
|
||||||
|
actual := string(escapeBytesQuotes([]byte{}, []byte(value)))
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf(
|
||||||
|
"expected %s, got %s",
|
||||||
|
expected, actual,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
actual = string(escapeStringQuotes([]byte{}, value))
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf(
|
||||||
|
"expected %s, got %s",
|
||||||
|
expected, actual,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
expect("foo\x00bar", "foo\x00bar") // not affected
|
||||||
|
expect("foo\nbar", "foo\nbar") // not affected
|
||||||
|
expect("foo\rbar", "foo\rbar") // not affected
|
||||||
|
expect("foo\x1abar", "foo\x1abar") // not affected
|
||||||
|
expect("foo''bar", "foo'bar") // affected
|
||||||
|
expect("foo\"bar", "foo\"bar") // not affected
|
||||||
|
}
|
||||||
|
27
Godeps/_workspace/src/github.com/go-xorm/core/LICENSE
generated
vendored
Normal file
27
Godeps/_workspace/src/github.com/go-xorm/core/LICENSE
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
Copyright (c) 2013 - 2015 Lunny Xiao <xiaolunwen@gmail.com>
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
* Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
* Neither the name of the {organization} nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived from
|
||||||
|
this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
19
Godeps/_workspace/src/github.com/go-xorm/core/cache.go
generated
vendored
19
Godeps/_workspace/src/github.com/go-xorm/core/cache.go
generated
vendored
@ -1,10 +1,11 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
"bytes"
|
||||||
|
"encoding/gob"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -47,16 +48,20 @@ type Cacher interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func encodeIds(ids []PK) (string, error) {
|
func encodeIds(ids []PK) (string, error) {
|
||||||
b, err := json.Marshal(ids)
|
buf := new(bytes.Buffer)
|
||||||
if err != nil {
|
enc := gob.NewEncoder(buf)
|
||||||
return "", err
|
err := enc.Encode(ids)
|
||||||
}
|
|
||||||
return string(b), nil
|
return buf.String(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func decodeIds(s string) ([]PK, error) {
|
func decodeIds(s string) ([]PK, error) {
|
||||||
pks := make([]PK, 0)
|
pks := make([]PK, 0)
|
||||||
err := json.Unmarshal([]byte(s), &pks)
|
|
||||||
|
dec := gob.NewDecoder(bytes.NewBufferString(s))
|
||||||
|
err := dec.Decode(&pks)
|
||||||
|
|
||||||
return pks, err
|
return pks, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
56
Godeps/_workspace/src/github.com/go-xorm/core/column.go
generated
vendored
56
Godeps/_workspace/src/github.com/go-xorm/core/column.go
generated
vendored
@ -1,10 +1,10 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -35,6 +35,8 @@ type Column struct {
|
|||||||
DefaultIsEmpty bool
|
DefaultIsEmpty bool
|
||||||
EnumOptions map[string]int
|
EnumOptions map[string]int
|
||||||
SetOptions map[string]int
|
SetOptions map[string]int
|
||||||
|
DisableTimeZone bool
|
||||||
|
TimeZone *time.Location // column specified time zone
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
|
func NewColumn(name, fieldName string, sqlType SQLType, len1, len2 int, nullable bool) *Column {
|
||||||
@ -122,50 +124,34 @@ func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if dataStruct.Type().Kind() == reflect.Map {
|
if dataStruct.Type().Kind() == reflect.Map {
|
||||||
var keyValue reflect.Value
|
keyValue := reflect.ValueOf(col.fieldPath[len(col.fieldPath)-1])
|
||||||
|
|
||||||
if len(col.fieldPath) == 1 {
|
|
||||||
keyValue = reflect.ValueOf(col.FieldName)
|
|
||||||
} else if len(col.fieldPath) == 2 {
|
|
||||||
keyValue = reflect.ValueOf(col.fieldPath[1])
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("Unsupported mutliderive %v", col.FieldName)
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldValue = dataStruct.MapIndex(keyValue)
|
fieldValue = dataStruct.MapIndex(keyValue)
|
||||||
return &fieldValue, nil
|
return &fieldValue, nil
|
||||||
|
} else if dataStruct.Type().Kind() == reflect.Interface {
|
||||||
|
structValue := reflect.ValueOf(dataStruct.Interface())
|
||||||
|
dataStruct = &structValue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(col.fieldPath) == 1 {
|
level := len(col.fieldPath)
|
||||||
fieldValue = dataStruct.FieldByName(col.FieldName)
|
fieldValue = dataStruct.FieldByName(col.fieldPath[0])
|
||||||
} else if len(col.fieldPath) == 2 {
|
for i := 0; i < level-1; i++ {
|
||||||
parentField := dataStruct.FieldByName(col.fieldPath[0])
|
if !fieldValue.IsValid() {
|
||||||
if parentField.IsValid() {
|
break
|
||||||
if parentField.Kind() == reflect.Struct {
|
}
|
||||||
fieldValue = parentField.FieldByName(col.fieldPath[1])
|
if fieldValue.Kind() == reflect.Struct {
|
||||||
} else if parentField.Kind() == reflect.Ptr {
|
fieldValue = fieldValue.FieldByName(col.fieldPath[i+1])
|
||||||
if parentField.IsNil() {
|
} else if fieldValue.Kind() == reflect.Ptr {
|
||||||
parentField.Set(reflect.New(parentField.Type().Elem()))
|
if fieldValue.IsNil() {
|
||||||
fieldValue = parentField.Elem().FieldByName(col.fieldPath[1])
|
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
|
||||||
} else {
|
}
|
||||||
parentField = parentField.Elem()
|
fieldValue = fieldValue.Elem().FieldByName(col.fieldPath[i+1])
|
||||||
if parentField.IsValid() {
|
|
||||||
fieldValue = parentField.FieldByName(col.fieldPath[1])
|
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
|
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// so we can use a different struct as conditions
|
|
||||||
fieldValue = dataStruct.FieldByName(col.fieldPath[1])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, fmt.Errorf("Unsupported mutliderive %v", col.FieldName)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !fieldValue.IsValid() {
|
if !fieldValue.IsValid() {
|
||||||
return nil, errors.New("no find field matched")
|
return nil, fmt.Errorf("field %v is not valid", col.FieldName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &fieldValue, nil
|
return &fieldValue, nil
|
||||||
|
123
Godeps/_workspace/src/github.com/go-xorm/core/db.go
generated
vendored
123
Godeps/_workspace/src/github.com/go-xorm/core/db.go
generated
vendored
@ -2,6 +2,7 @@ package core
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"database/sql/driver"
|
||||||
"errors"
|
"errors"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -29,10 +30,24 @@ func StructToSlice(query string, st interface{}) (string, []interface{}, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
args := make([]interface{}, 0)
|
args := make([]interface{}, 0)
|
||||||
|
var err error
|
||||||
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
query = re.ReplaceAllStringFunc(query, func(src string) string {
|
||||||
args = append(args, vv.Elem().FieldByName(src[1:]).Interface())
|
fv := vv.Elem().FieldByName(src[1:]).Interface()
|
||||||
|
if v, ok := fv.(driver.Valuer); ok {
|
||||||
|
var value driver.Value
|
||||||
|
value, err = v.Value()
|
||||||
|
if err != nil {
|
||||||
|
return "?"
|
||||||
|
}
|
||||||
|
args = append(args, value)
|
||||||
|
} else {
|
||||||
|
args = append(args, fv)
|
||||||
|
}
|
||||||
return "?"
|
return "?"
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", []interface{}{}, err
|
||||||
|
}
|
||||||
return query, args, nil
|
return query, args, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,12 +58,25 @@ type DB struct {
|
|||||||
|
|
||||||
func Open(driverName, dataSourceName string) (*DB, error) {
|
func Open(driverName, dataSourceName string) (*DB, error) {
|
||||||
db, err := sql.Open(driverName, dataSourceName)
|
db, err := sql.Open(driverName, dataSourceName)
|
||||||
return &DB{db, NewCacheMapper(&SnakeMapper{})}, err
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &DB{db, NewCacheMapper(&SnakeMapper{})}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func FromDB(db *sql.DB) *DB {
|
||||||
|
return &DB{db, NewCacheMapper(&SnakeMapper{})}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
|
func (db *DB) Query(query string, args ...interface{}) (*Rows, error) {
|
||||||
rows, err := db.DB.Query(query, args...)
|
rows, err := db.DB.Query(query, args...)
|
||||||
return &Rows{rows, db.Mapper}, err
|
if err != nil {
|
||||||
|
if rows != nil {
|
||||||
|
rows.Close()
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &Rows{rows, db.Mapper}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) QueryMap(query string, mp interface{}) (*Rows, error) {
|
func (db *DB) QueryMap(query string, mp interface{}) (*Rows, error) {
|
||||||
@ -68,28 +96,87 @@ func (db *DB) QueryStruct(query string, st interface{}) (*Rows, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Row struct {
|
type Row struct {
|
||||||
*sql.Row
|
rows *Rows
|
||||||
// One of these two will be non-nil:
|
// One of these two will be non-nil:
|
||||||
err error // deferred error for easy chaining
|
err error // deferred error for easy chaining
|
||||||
Mapper IMapper
|
}
|
||||||
|
|
||||||
|
func (row *Row) Columns() ([]string, error) {
|
||||||
|
if row.err != nil {
|
||||||
|
return nil, row.err
|
||||||
|
}
|
||||||
|
return row.rows.Columns()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (row *Row) Scan(dest ...interface{}) error {
|
func (row *Row) Scan(dest ...interface{}) error {
|
||||||
if row.err != nil {
|
if row.err != nil {
|
||||||
return row.err
|
return row.err
|
||||||
}
|
}
|
||||||
return row.Row.Scan(dest...)
|
defer row.rows.Close()
|
||||||
|
|
||||||
|
for _, dp := range dest {
|
||||||
|
if _, ok := dp.(*sql.RawBytes); ok {
|
||||||
|
return errors.New("sql: RawBytes isn't allowed on Row.Scan")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !row.rows.Next() {
|
||||||
|
if err := row.rows.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return sql.ErrNoRows
|
||||||
|
}
|
||||||
|
err := row.rows.Scan(dest...)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Make sure the query can be processed to completion with no errors.
|
||||||
|
if err := row.rows.Close(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (row *Row) ScanStructByName(dest interface{}) error {
|
||||||
|
if row.err != nil {
|
||||||
|
return row.err
|
||||||
|
}
|
||||||
|
return row.rows.ScanStructByName(dest)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (row *Row) ScanStructByIndex(dest interface{}) error {
|
||||||
|
if row.err != nil {
|
||||||
|
return row.err
|
||||||
|
}
|
||||||
|
return row.rows.ScanStructByIndex(dest)
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan data to a slice's pointer, slice's length should equal to columns' number
|
||||||
|
func (row *Row) ScanSlice(dest interface{}) error {
|
||||||
|
if row.err != nil {
|
||||||
|
return row.err
|
||||||
|
}
|
||||||
|
return row.rows.ScanSlice(dest)
|
||||||
|
}
|
||||||
|
|
||||||
|
// scan data to a map's pointer
|
||||||
|
func (row *Row) ScanMap(dest interface{}) error {
|
||||||
|
if row.err != nil {
|
||||||
|
return row.err
|
||||||
|
}
|
||||||
|
return row.rows.ScanMap(dest)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) QueryRow(query string, args ...interface{}) *Row {
|
func (db *DB) QueryRow(query string, args ...interface{}) *Row {
|
||||||
row := db.DB.QueryRow(query, args...)
|
rows, err := db.Query(query, args...)
|
||||||
return &Row{row, nil, db.Mapper}
|
return &Row{rows, err}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *DB) QueryRowMap(query string, mp interface{}) *Row {
|
func (db *DB) QueryRowMap(query string, mp interface{}) *Row {
|
||||||
query, args, err := MapToSlice(query, mp)
|
query, args, err := MapToSlice(query, mp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &Row{nil, err, db.Mapper}
|
return &Row{nil, err}
|
||||||
}
|
}
|
||||||
return db.QueryRow(query, args...)
|
return db.QueryRow(query, args...)
|
||||||
}
|
}
|
||||||
@ -97,7 +184,7 @@ func (db *DB) QueryRowMap(query string, mp interface{}) *Row {
|
|||||||
func (db *DB) QueryRowStruct(query string, st interface{}) *Row {
|
func (db *DB) QueryRowStruct(query string, st interface{}) *Row {
|
||||||
query, args, err := StructToSlice(query, st)
|
query, args, err := StructToSlice(query, st)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &Row{nil, err, db.Mapper}
|
return &Row{nil, err}
|
||||||
}
|
}
|
||||||
return db.QueryRow(query, args...)
|
return db.QueryRow(query, args...)
|
||||||
}
|
}
|
||||||
@ -187,14 +274,14 @@ func (s *Stmt) QueryStruct(st interface{}) (*Rows, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stmt) QueryRow(args ...interface{}) *Row {
|
func (s *Stmt) QueryRow(args ...interface{}) *Row {
|
||||||
row := s.Stmt.QueryRow(args...)
|
rows, err := s.Query(args...)
|
||||||
return &Row{row, nil, s.Mapper}
|
return &Row{rows, err}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Stmt) QueryRowMap(mp interface{}) *Row {
|
func (s *Stmt) QueryRowMap(mp interface{}) *Row {
|
||||||
vv := reflect.ValueOf(mp)
|
vv := reflect.ValueOf(mp)
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
|
||||||
return &Row{nil, errors.New("mp should be a map's pointer"), s.Mapper}
|
return &Row{nil, errors.New("mp should be a map's pointer")}
|
||||||
}
|
}
|
||||||
|
|
||||||
args := make([]interface{}, len(s.names))
|
args := make([]interface{}, len(s.names))
|
||||||
@ -208,7 +295,7 @@ func (s *Stmt) QueryRowMap(mp interface{}) *Row {
|
|||||||
func (s *Stmt) QueryRowStruct(st interface{}) *Row {
|
func (s *Stmt) QueryRowStruct(st interface{}) *Row {
|
||||||
vv := reflect.ValueOf(st)
|
vv := reflect.ValueOf(st)
|
||||||
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Struct {
|
||||||
return &Row{nil, errors.New("st should be a struct's pointer"), s.Mapper}
|
return &Row{nil, errors.New("st should be a struct's pointer")}
|
||||||
}
|
}
|
||||||
|
|
||||||
args := make([]interface{}, len(s.names))
|
args := make([]interface{}, len(s.names))
|
||||||
@ -540,14 +627,14 @@ func (tx *Tx) QueryStruct(query string, st interface{}) (*Rows, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
|
func (tx *Tx) QueryRow(query string, args ...interface{}) *Row {
|
||||||
row := tx.Tx.QueryRow(query, args...)
|
rows, err := tx.Query(query, args...)
|
||||||
return &Row{row, nil, tx.Mapper}
|
return &Row{rows, err}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row {
|
func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row {
|
||||||
query, args, err := MapToSlice(query, mp)
|
query, args, err := MapToSlice(query, mp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &Row{nil, err, tx.Mapper}
|
return &Row{nil, err}
|
||||||
}
|
}
|
||||||
return tx.QueryRow(query, args...)
|
return tx.QueryRow(query, args...)
|
||||||
}
|
}
|
||||||
@ -555,7 +642,7 @@ func (tx *Tx) QueryRowMap(query string, mp interface{}) *Row {
|
|||||||
func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row {
|
func (tx *Tx) QueryRowStruct(query string, st interface{}) *Row {
|
||||||
query, args, err := StructToSlice(query, st)
|
query, args, err := StructToSlice(query, st)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &Row{nil, err, tx.Mapper}
|
return &Row{nil, err}
|
||||||
}
|
}
|
||||||
return tx.QueryRow(query, args...)
|
return tx.QueryRow(query, args...)
|
||||||
}
|
}
|
||||||
|
8
Godeps/_workspace/src/github.com/go-xorm/core/db_test.go
generated
vendored
8
Godeps/_workspace/src/github.com/go-xorm/core/db_test.go
generated
vendored
@ -24,7 +24,7 @@ type User struct {
|
|||||||
Age float32
|
Age float32
|
||||||
Alias string
|
Alias string
|
||||||
NickName string
|
NickName string
|
||||||
Created time.Time
|
Created NullTime
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -85,7 +85,7 @@ func BenchmarkOriQuery(b *testing.B) {
|
|||||||
var Id int64
|
var Id int64
|
||||||
var Name, Title, Alias, NickName string
|
var Name, Title, Alias, NickName string
|
||||||
var Age float32
|
var Age float32
|
||||||
var Created time.Time
|
var Created NullTime
|
||||||
err = rows.Scan(&Id, &Name, &Title, &Age, &Alias, &NickName, &Created)
|
err = rows.Scan(&Id, &Name, &Title, &Age, &Alias, &NickName, &Created)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Error(err)
|
b.Error(err)
|
||||||
@ -600,7 +600,7 @@ func TestExecStruct(t *testing.T) {
|
|||||||
Age: 1.2,
|
Age: 1.2,
|
||||||
Alias: "lunny",
|
Alias: "lunny",
|
||||||
NickName: "lunny xiao",
|
NickName: "lunny xiao",
|
||||||
Created: time.Now(),
|
Created: NullTime(time.Now()),
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.ExecStruct("insert into user (`name`, title, age, alias, nick_name,created) "+
|
_, err = db.ExecStruct("insert into user (`name`, title, age, alias, nick_name,created) "+
|
||||||
@ -645,7 +645,7 @@ func BenchmarkExecStruct(b *testing.B) {
|
|||||||
Age: 1.2,
|
Age: 1.2,
|
||||||
Alias: "lunny",
|
Alias: "lunny",
|
||||||
NickName: "lunny xiao",
|
NickName: "lunny xiao",
|
||||||
Created: time.Now(),
|
Created: NullTime(time.Now()),
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
|
33
Godeps/_workspace/src/github.com/go-xorm/core/dialect.go
generated
vendored
33
Godeps/_workspace/src/github.com/go-xorm/core/dialect.go
generated
vendored
@ -54,7 +54,7 @@ type Dialect interface {
|
|||||||
IndexCheckSql(tableName, idxName string) (string, []interface{})
|
IndexCheckSql(tableName, idxName string) (string, []interface{})
|
||||||
TableCheckSql(tableName string) (string, []interface{})
|
TableCheckSql(tableName string) (string, []interface{})
|
||||||
|
|
||||||
IsColumnExist(tableName string, col *Column) (bool, error)
|
IsColumnExist(tableName string, colName string) (bool, error)
|
||||||
|
|
||||||
CreateTableSql(table *Table, tableName, storeEngine, charset string) string
|
CreateTableSql(table *Table, tableName, storeEngine, charset string) string
|
||||||
DropTableSql(tableName string) string
|
DropTableSql(tableName string) string
|
||||||
@ -63,6 +63,8 @@ type Dialect interface {
|
|||||||
|
|
||||||
ModifyColumnSql(tableName string, col *Column) string
|
ModifyColumnSql(tableName string, col *Column) string
|
||||||
|
|
||||||
|
ForUpdateSql(query string) string
|
||||||
|
|
||||||
//CreateTableIfNotExists(table *Table, tableName, storeEngine, charset string) error
|
//CreateTableIfNotExists(table *Table, tableName, storeEngine, charset string) error
|
||||||
//MustDropTable(tableName string) error
|
//MustDropTable(tableName string) error
|
||||||
|
|
||||||
@ -164,10 +166,10 @@ func (db *Base) HasRecords(query string, args ...interface{}) (bool, error) {
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *Base) IsColumnExist(tableName string, col *Column) (bool, error) {
|
func (db *Base) IsColumnExist(tableName, colName string) (bool, error) {
|
||||||
query := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
query := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
||||||
query = strings.Replace(query, "`", db.dialect.QuoteStr(), -1)
|
query = strings.Replace(query, "`", db.dialect.QuoteStr(), -1)
|
||||||
return db.HasRecords(query, db.DbName, tableName, col.Name)
|
return db.HasRecords(query, db.DbName, tableName, colName)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -229,8 +231,10 @@ func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset stri
|
|||||||
tableName = table.Name
|
tableName = table.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
sql += b.dialect.Quote(tableName) + " ("
|
sql += b.dialect.Quote(tableName)
|
||||||
|
sql += " ("
|
||||||
|
|
||||||
|
if len(table.ColumnsSeq()) > 0 {
|
||||||
pkList := table.PrimaryKeys
|
pkList := table.PrimaryKeys
|
||||||
|
|
||||||
for _, colName := range table.ColumnsSeq() {
|
for _, colName := range table.ColumnsSeq() {
|
||||||
@ -250,7 +254,10 @@ func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset stri
|
|||||||
sql += " ), "
|
sql += " ), "
|
||||||
}
|
}
|
||||||
|
|
||||||
sql = sql[:len(sql)-2] + ")"
|
sql = sql[:len(sql)-2]
|
||||||
|
}
|
||||||
|
sql += ")"
|
||||||
|
|
||||||
if b.dialect.SupportEngine() && storeEngine != "" {
|
if b.dialect.SupportEngine() && storeEngine != "" {
|
||||||
sql += " ENGINE=" + storeEngine
|
sql += " ENGINE=" + storeEngine
|
||||||
}
|
}
|
||||||
@ -262,21 +269,25 @@ func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset stri
|
|||||||
sql += " DEFAULT CHARSET " + charset
|
sql += " DEFAULT CHARSET " + charset
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sql += ";"
|
|
||||||
return sql
|
return sql
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Base) ForUpdateSql(query string) string {
|
||||||
|
return query + " FOR UPDATE"
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
dialects = map[DbType]Dialect{}
|
dialects = map[DbType]func() Dialect{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterDialect(dbName DbType, dialect Dialect) {
|
func RegisterDialect(dbName DbType, dialectFunc func() Dialect) {
|
||||||
if dialect == nil {
|
if dialectFunc == nil {
|
||||||
panic("core: Register dialect is nil")
|
panic("core: Register dialect is nil")
|
||||||
}
|
}
|
||||||
dialects[dbName] = dialect // !nashtsai! allow override dialect
|
dialects[dbName] = dialectFunc // !nashtsai! allow override dialect
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryDialect(dbName DbType) Dialect {
|
func QueryDialect(dbName DbType) Dialect {
|
||||||
return dialects[dbName]
|
return dialects[dbName]()
|
||||||
}
|
}
|
||||||
|
2
Godeps/_workspace/src/github.com/go-xorm/core/error.go
generated
vendored
2
Godeps/_workspace/src/github.com/go-xorm/core/error.go
generated
vendored
@ -4,7 +4,7 @@ import "errors"
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
ErrNoMapPointer = errors.New("mp should be a map's pointer")
|
ErrNoMapPointer = errors.New("mp should be a map's pointer")
|
||||||
ErrNoStructPointer = errors.New("mp should be a map's pointer")
|
ErrNoStructPointer = errors.New("mp should be a struct's pointer")
|
||||||
//ErrNotExist = errors.New("Not exist")
|
//ErrNotExist = errors.New("Not exist")
|
||||||
//ErrIgnore = errors.New("Ignore")
|
//ErrIgnore = errors.New("Ignore")
|
||||||
)
|
)
|
||||||
|
52
Godeps/_workspace/src/github.com/go-xorm/core/scan.go
generated
vendored
Normal file
52
Godeps/_workspace/src/github.com/go-xorm/core/scan.go
generated
vendored
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql/driver"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type NullTime time.Time
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ driver.Valuer = NullTime{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (ns *NullTime) Scan(value interface{}) error {
|
||||||
|
if value == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return convertTime(ns, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value implements the driver Valuer interface.
|
||||||
|
func (ns NullTime) Value() (driver.Value, error) {
|
||||||
|
if (time.Time)(ns).IsZero() {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
return (time.Time)(ns).Format("2006-01-02 15:04:05"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertTime(dest *NullTime, src interface{}) error {
|
||||||
|
// Common cases, without reflect.
|
||||||
|
switch s := src.(type) {
|
||||||
|
case string:
|
||||||
|
t, err := time.Parse("2006-01-02 15:04:05", s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*dest = NullTime(t)
|
||||||
|
return nil
|
||||||
|
case []uint8:
|
||||||
|
t, err := time.Parse("2006-01-02 15:04:05", string(s))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*dest = NullTime(t)
|
||||||
|
return nil
|
||||||
|
case nil:
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported driver -> Scan pair: %T -> %T", src, dest)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
10
Godeps/_workspace/src/github.com/go-xorm/core/type.go
generated
vendored
10
Godeps/_workspace/src/github.com/go-xorm/core/type.go
generated
vendored
@ -53,6 +53,10 @@ func (s *SQLType) IsNumeric() bool {
|
|||||||
return s.IsType(NUMERIC_TYPE)
|
return s.IsType(NUMERIC_TYPE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SQLType) IsJson() bool {
|
||||||
|
return s.Name == Json
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Bit = "BIT"
|
Bit = "BIT"
|
||||||
TinyInt = "TINYINT"
|
TinyInt = "TINYINT"
|
||||||
@ -101,6 +105,8 @@ var (
|
|||||||
Serial = "SERIAL"
|
Serial = "SERIAL"
|
||||||
BigSerial = "BIGSERIAL"
|
BigSerial = "BIGSERIAL"
|
||||||
|
|
||||||
|
Json = "JSON"
|
||||||
|
|
||||||
SqlTypes = map[string]int{
|
SqlTypes = map[string]int{
|
||||||
Bit: NUMERIC_TYPE,
|
Bit: NUMERIC_TYPE,
|
||||||
TinyInt: NUMERIC_TYPE,
|
TinyInt: NUMERIC_TYPE,
|
||||||
@ -112,6 +118,7 @@ var (
|
|||||||
|
|
||||||
Enum: TEXT_TYPE,
|
Enum: TEXT_TYPE,
|
||||||
Set: TEXT_TYPE,
|
Set: TEXT_TYPE,
|
||||||
|
Json: TEXT_TYPE,
|
||||||
|
|
||||||
Char: TEXT_TYPE,
|
Char: TEXT_TYPE,
|
||||||
Varchar: TEXT_TYPE,
|
Varchar: TEXT_TYPE,
|
||||||
@ -229,7 +236,6 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Type2SQLType(t reflect.Type) (st SQLType) {
|
func Type2SQLType(t reflect.Type) (st SQLType) {
|
||||||
|
|
||||||
switch k := t.Kind(); k {
|
switch k := t.Kind(); k {
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
||||||
st = SQLType{Int, 0, 0}
|
st = SQLType{Int, 0, 0}
|
||||||
@ -252,7 +258,7 @@ func Type2SQLType(t reflect.Type) (st SQLType) {
|
|||||||
case reflect.String:
|
case reflect.String:
|
||||||
st = SQLType{Varchar, 255, 0}
|
st = SQLType{Varchar, 255, 0}
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if t.ConvertibleTo(reflect.TypeOf(c_TIME_DEFAULT)) {
|
if t.ConvertibleTo(TimeType) {
|
||||||
st = SQLType{DateTime, 0, 0}
|
st = SQLType{DateTime, 0, 0}
|
||||||
} else {
|
} else {
|
||||||
// TODO need to handle association struct
|
// TODO need to handle association struct
|
||||||
|
2
Godeps/_workspace/src/github.com/go-xorm/xorm/LICENSE
generated
vendored
2
Godeps/_workspace/src/github.com/go-xorm/xorm/LICENSE
generated
vendored
@ -1,4 +1,4 @@
|
|||||||
Copyright (c) 2013 - 2014
|
Copyright (c) 2013 - 2015 The Xorm Authors
|
||||||
All rights reserved.
|
All rights reserved.
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
182
Godeps/_workspace/src/github.com/go-xorm/xorm/README.md
generated
vendored
182
Godeps/_workspace/src/github.com/go-xorm/xorm/README.md
generated
vendored
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
Xorm is a simple and powerful ORM for Go.
|
Xorm is a simple and powerful ORM for Go.
|
||||||
|
|
||||||
|
[](https://gitter.im/go-xorm/xorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||||
|
|
||||||
[](https://drone.io/github.com/go-xorm/tests/latest) [](http://gowalker.org/github.com/go-xorm/xorm) [](https://bitdeli.com/free "Bitdeli Badge")
|
[](https://drone.io/github.com/go-xorm/tests/latest) [](http://gowalker.org/github.com/go-xorm/xorm) [](https://bitdeli.com/free "Bitdeli Badge")
|
||||||
|
|
||||||
# Features
|
# Features
|
||||||
@ -33,34 +35,40 @@ Drivers for Go's sql package which currently support database/sql includes:
|
|||||||
|
|
||||||
* MyMysql: [github.com/ziutek/mymysql/godrv](https://github.com/ziutek/mymysql/godrv)
|
* MyMysql: [github.com/ziutek/mymysql/godrv](https://github.com/ziutek/mymysql/godrv)
|
||||||
|
|
||||||
* SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
|
|
||||||
|
|
||||||
* Postgres: [github.com/lib/pq](https://github.com/lib/pq)
|
* Postgres: [github.com/lib/pq](https://github.com/lib/pq)
|
||||||
|
|
||||||
|
* Tidb: [github.com/pingcap/tidb](https://github.com/pingcap/tidb)
|
||||||
|
|
||||||
|
* SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
|
||||||
|
|
||||||
* MsSql: [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb)
|
* MsSql: [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb)
|
||||||
|
|
||||||
* MsSql: [github.com/lunny/godbc](https://github.com/lunny/godbc)
|
* MsSql: [github.com/lunny/godbc](https://github.com/lunny/godbc)
|
||||||
|
|
||||||
|
* Oracle: [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (experiment)
|
||||||
|
|
||||||
|
* ql: [github.com/cznic/ql](https://github.com/cznic/ql) (experiment)
|
||||||
|
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
* **v0.4.1**
|
* **v0.4.4**
|
||||||
Features:
|
* ql database expriment support
|
||||||
* Add deleted xorm tag for soft delete and add unscoped
|
* tidb database expriment support
|
||||||
|
* sql.NullString and etc. field support
|
||||||
|
* select ForUpdate support
|
||||||
|
* many bugs fixed
|
||||||
|
|
||||||
* **v0.4.0 RC1**
|
* **v0.4.3**
|
||||||
Changes:
|
* Json column type support
|
||||||
* moved xorm cmd to [github.com/go-xorm/cmd](github.com/go-xorm/cmd)
|
* oracle expirement support
|
||||||
* refactored general DB operation a core lib at [github.com/go-xorm/core](https://github.com/go-xorm/core)
|
* bug fixed
|
||||||
* moved tests to github.com/go-xorm/tests [github.com/go-xorm/tests](github.com/go-xorm/tests)
|
|
||||||
|
|
||||||
Improvements:
|
* **v0.4.2**
|
||||||
* Prepared statement cache
|
* Transaction will auto rollback if not Rollback or Commit be called.
|
||||||
* Add Incr API
|
* Gonic Mapper support
|
||||||
* Specify Timezone Location
|
* bug fixed
|
||||||
|
|
||||||
[More changelogs ...](https://github.com/go-xorm/manual-en-US/tree/master/chapter-15)
|
[More changelogs ...](https://github.com/go-xorm/manual-en-US/tree/master/chapter-16)
|
||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
@ -80,8 +88,152 @@ Or
|
|||||||
|
|
||||||
* [GoWalker](http://gowalker.org/github.com/go-xorm/xorm)
|
* [GoWalker](http://gowalker.org/github.com/go-xorm/xorm)
|
||||||
|
|
||||||
|
# Quick Start
|
||||||
|
|
||||||
|
* Create Engine
|
||||||
|
|
||||||
|
```Go
|
||||||
|
engine, err := xorm.NewEngine(driverName, dataSourceName)
|
||||||
|
```
|
||||||
|
|
||||||
|
* Define a struct and Sync2 table struct to database
|
||||||
|
|
||||||
|
```Go
|
||||||
|
type User struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
Salt string
|
||||||
|
Age int
|
||||||
|
Passwd string `xorm:"varchar(200)"`
|
||||||
|
Created time.Time `xorm:"created"`
|
||||||
|
Updated time.Time `xorm:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := engine.Sync2(new(User))
|
||||||
|
```
|
||||||
|
|
||||||
|
* Query a SQL string, the returned results is []map[string][]byte
|
||||||
|
|
||||||
|
```Go
|
||||||
|
results, err := engine.Query("select * from user")
|
||||||
|
```
|
||||||
|
|
||||||
|
* Execute a SQL string, the returned results
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Exec("update user set age = ? where name = ?", age, name)
|
||||||
|
```
|
||||||
|
|
||||||
|
* Insert one or multipe records to database
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Insert(&user)
|
||||||
|
// INSERT INTO struct () values ()
|
||||||
|
affected, err := engine.Insert(&user1, &user2)
|
||||||
|
// INSERT INTO struct1 () values ()
|
||||||
|
// INSERT INTO struct2 () values ()
|
||||||
|
affected, err := engine.Insert(&users)
|
||||||
|
// INSERT INTO struct () values (),(),()
|
||||||
|
affected, err := engine.Insert(&user1, &users)
|
||||||
|
// INSERT INTO struct1 () values ()
|
||||||
|
// INSERT INTO struct2 () values (),(),()
|
||||||
|
```
|
||||||
|
|
||||||
|
* Query one record from database
|
||||||
|
|
||||||
|
```Go
|
||||||
|
has, err := engine.Get(&user)
|
||||||
|
// SELECT * FROM user LIMIT 1
|
||||||
|
has, err := engine.Where("name = ?", name).Desc("id").Get(&user)
|
||||||
|
// SELECT * FROM user WHERE name = ? ORDER BY id DESC LIMIT 1
|
||||||
|
```
|
||||||
|
|
||||||
|
* Query multiple records from database, also you can use join and extends
|
||||||
|
|
||||||
|
```Go
|
||||||
|
var users []User
|
||||||
|
err := engine.Where("name = ?", name).And("age > 10").Limit(10, 0).Find(&users)
|
||||||
|
// SELECT * FROM user WHERE name = ? AND age > 10 limit 0 offset 10
|
||||||
|
|
||||||
|
type Detail struct {
|
||||||
|
Id int64
|
||||||
|
UserId int64 `xorm:"index"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserDetail struct {
|
||||||
|
User `xorm:"extends"`
|
||||||
|
Detail `xorm:"extends"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var users []UserDetail
|
||||||
|
err := engine.Table("user").Select("user.*, detail.*")
|
||||||
|
Join("INNER", "detail", "detail.user_id = user.id").
|
||||||
|
Where("user.name = ?", name).Limit(10, 0).
|
||||||
|
Find(&users)
|
||||||
|
// SELECT user.*, detail.* FROM user INNER JOIN detail WHERE user.name = ? limit 0 offset 10
|
||||||
|
```
|
||||||
|
|
||||||
|
* Query multiple records and record by record handle, there two methods Iterate and Rows
|
||||||
|
|
||||||
|
```Go
|
||||||
|
err := engine.Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
|
||||||
|
user := bean.(*User)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
// SELECT * FROM user
|
||||||
|
|
||||||
|
rows, err := engine.Rows(&User{Name:name})
|
||||||
|
// SELECT * FROM user
|
||||||
|
defer rows.Close()
|
||||||
|
bean := new(Struct)
|
||||||
|
for rows.Next() {
|
||||||
|
err = rows.Scan(bean)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* Update one or more records, default will update non-empty and non-zero fields except to use Cols, AllCols and etc.
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Id(1).Update(&user)
|
||||||
|
// UPDATE user SET ... Where id = ?
|
||||||
|
|
||||||
|
affected, err := engine.Update(&user, &User{Name:name})
|
||||||
|
// UPDATE user SET ... Where name = ?
|
||||||
|
|
||||||
|
var ids = []int64{1, 2, 3}
|
||||||
|
affected, err := engine.In(ids).Update(&user)
|
||||||
|
// UPDATE user SET ... Where id IN (?, ?, ?)
|
||||||
|
|
||||||
|
// force update indicated columns by Cols
|
||||||
|
affected, err := engine.Id(1).Cols("age").Update(&User{Name:name, Age: 12})
|
||||||
|
// UPDATE user SET age = ?, updated=? Where id = ?
|
||||||
|
|
||||||
|
// force NOT update indicated columns by Omit
|
||||||
|
affected, err := engine.Id(1).Omit("name").Update(&User{Name:name, Age: 12})
|
||||||
|
// UPDATE user SET age = ?, updated=? Where id = ?
|
||||||
|
|
||||||
|
affected, err := engine.Id(1).AllCols().Update(&user)
|
||||||
|
// UPDATE user SET name=?,age=?,salt=?,passwd=?,updated=? Where id = ?
|
||||||
|
```
|
||||||
|
|
||||||
|
* Delete one or more records, Delete MUST has conditon
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Where(...).Delete(&user)
|
||||||
|
// DELETE FROM user Where ...
|
||||||
|
```
|
||||||
|
|
||||||
|
* Count records
|
||||||
|
|
||||||
|
```Go
|
||||||
|
counts, err := engine.Count(&user)
|
||||||
|
// SELECT count(*) AS total FROM user
|
||||||
|
```
|
||||||
|
|
||||||
# Cases
|
# Cases
|
||||||
|
|
||||||
|
* [github.com/m3ng9i/qreader](https://github.com/m3ng9i/qreader)
|
||||||
|
|
||||||
* [Wego](http://github.com/go-tango/wego)
|
* [Wego](http://github.com/go-tango/wego)
|
||||||
|
|
||||||
* [Docker.cn](https://docker.cn/)
|
* [Docker.cn](https://docker.cn/)
|
||||||
|
172
Godeps/_workspace/src/github.com/go-xorm/xorm/README_CN.md
generated
vendored
172
Godeps/_workspace/src/github.com/go-xorm/xorm/README_CN.md
generated
vendored
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便。
|
xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作非常简便。
|
||||||
|
|
||||||
|
[](https://gitter.im/go-xorm/xorm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||||
|
|
||||||
[](https://drone.io/github.com/go-xorm/tests/latest) [](http://gowalker.org/github.com/go-xorm/xorm)
|
[](https://drone.io/github.com/go-xorm/tests/latest) [](http://gowalker.org/github.com/go-xorm/xorm)
|
||||||
|
|
||||||
## 特性
|
## 特性
|
||||||
@ -34,20 +36,33 @@ xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作
|
|||||||
|
|
||||||
* MyMysql: [github.com/ziutek/mymysql/godrv](https://github.com/ziutek/mymysql/godrv)
|
* MyMysql: [github.com/ziutek/mymysql/godrv](https://github.com/ziutek/mymysql/godrv)
|
||||||
|
|
||||||
* SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
|
|
||||||
|
|
||||||
* Postgres: [github.com/lib/pq](https://github.com/lib/pq)
|
* Postgres: [github.com/lib/pq](https://github.com/lib/pq)
|
||||||
|
|
||||||
|
* Tidb: [github.com/pingcap/tidb](https://github.com/pingcap/tidb)
|
||||||
|
|
||||||
|
* SQLite: [github.com/mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
|
||||||
|
|
||||||
* MsSql: [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb)
|
* MsSql: [github.com/denisenkom/go-mssqldb](https://github.com/denisenkom/go-mssqldb)
|
||||||
|
|
||||||
* MsSql: [github.com/lunny/godbc](https://github.com/lunny/godbc)
|
* MsSql: [github.com/lunny/godbc](https://github.com/lunny/godbc)
|
||||||
|
|
||||||
|
* Oracle: [github.com/mattn/go-oci8](https://github.com/mattn/go-oci8) (试验性支持)
|
||||||
|
|
||||||
|
* ql: [github.com/cznic/ql](https://github.com/cznic/ql) (试验性支持)
|
||||||
|
|
||||||
## 更新日志
|
## 更新日志
|
||||||
|
|
||||||
* **v0.4.2**
|
* **v0.4.4**
|
||||||
新特性:
|
* Tidb 数据库支持
|
||||||
* deleted标记
|
* QL 试验性支持
|
||||||
* bug fixed
|
* sql.NullString支持
|
||||||
|
* ForUpdate 支持
|
||||||
|
* bug修正
|
||||||
|
|
||||||
|
* **v0.4.3**
|
||||||
|
* Json 字段类型支持
|
||||||
|
* oracle实验性支持
|
||||||
|
* bug修正
|
||||||
|
|
||||||
[更多更新日志...](https://github.com/go-xorm/manual-zh-CN/tree/master/chapter-16)
|
[更多更新日志...](https://github.com/go-xorm/manual-zh-CN/tree/master/chapter-16)
|
||||||
|
|
||||||
@ -69,8 +84,151 @@ xorm是一个简单而强大的Go语言ORM库. 通过它可以使数据库操作
|
|||||||
|
|
||||||
* [Godoc代码文档](http://godoc.org/github.com/go-xorm/xorm)
|
* [Godoc代码文档](http://godoc.org/github.com/go-xorm/xorm)
|
||||||
|
|
||||||
|
# 快速开始
|
||||||
|
|
||||||
## 案例
|
* 第一步创建引擎,driverName, dataSourceName和database/sql接口相同
|
||||||
|
|
||||||
|
```Go
|
||||||
|
engine, err := xorm.NewEngine(driverName, dataSourceName)
|
||||||
|
```
|
||||||
|
|
||||||
|
* 定义一个和表同步的结构体,并且自动同步结构体到数据库
|
||||||
|
|
||||||
|
```Go
|
||||||
|
type User struct {
|
||||||
|
Id int64
|
||||||
|
Name string
|
||||||
|
Salt string
|
||||||
|
Age int
|
||||||
|
Passwd string `xorm:"varchar(200)"`
|
||||||
|
Created time.Time `xorm:"created"`
|
||||||
|
Updated time.Time `xorm:"updated"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err := engine.Sync2(new(User))
|
||||||
|
```
|
||||||
|
|
||||||
|
* 最原始的也支持SQL语句查询,返回的结果类型为 []map[string][]byte
|
||||||
|
|
||||||
|
```Go
|
||||||
|
results, err := engine.Query("select * from user")
|
||||||
|
```
|
||||||
|
|
||||||
|
* 执行一个SQL语句
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Exec("update user set age = ? where name = ?", age, name)
|
||||||
|
```
|
||||||
|
|
||||||
|
* 插入一条或者多条记录
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Insert(&user)
|
||||||
|
// INSERT INTO struct () values ()
|
||||||
|
affected, err := engine.Insert(&user1, &user2)
|
||||||
|
// INSERT INTO struct1 () values ()
|
||||||
|
// INSERT INTO struct2 () values ()
|
||||||
|
affected, err := engine.Insert(&users)
|
||||||
|
// INSERT INTO struct () values (),(),()
|
||||||
|
affected, err := engine.Insert(&user1, &users)
|
||||||
|
// INSERT INTO struct1 () values ()
|
||||||
|
// INSERT INTO struct2 () values (),(),()
|
||||||
|
```
|
||||||
|
|
||||||
|
* 查询单条记录
|
||||||
|
|
||||||
|
```Go
|
||||||
|
has, err := engine.Get(&user)
|
||||||
|
// SELECT * FROM user LIMIT 1
|
||||||
|
has, err := engine.Where("name = ?", name).Desc("id").Get(&user)
|
||||||
|
// SELECT * FROM user WHERE name = ? ORDER BY id DESC LIMIT 1
|
||||||
|
```
|
||||||
|
|
||||||
|
* 查询多条记录,当然可以使用Join和extends来组合使用
|
||||||
|
|
||||||
|
```Go
|
||||||
|
var users []User
|
||||||
|
err := engine.Where("name = ?", name).And("age > 10").Limit(10, 0).Find(&users)
|
||||||
|
// SELECT * FROM user WHERE name = ? AND age > 10 limit 0 offset 10
|
||||||
|
|
||||||
|
type Detail struct {
|
||||||
|
Id int64
|
||||||
|
UserId int64 `xorm:"index"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UserDetail struct {
|
||||||
|
User `xorm:"extends"`
|
||||||
|
Detail `xorm:"extends"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var users []UserDetail
|
||||||
|
err := engine.Table("user").Select("user.*, detail.*")
|
||||||
|
Join("INNER", "detail", "detail.user_id = user.id").
|
||||||
|
Where("user.name = ?", name).Limit(10, 0).
|
||||||
|
Find(&users)
|
||||||
|
// SELECT user.*, detail.* FROM user INNER JOIN detail WHERE user.name = ? limit 0 offset 10
|
||||||
|
```
|
||||||
|
|
||||||
|
* 根据条件遍历数据库,可以有两种方式: Iterate and Rows
|
||||||
|
|
||||||
|
```Go
|
||||||
|
err := engine.Iterate(&User{Name:name}, func(idx int, bean interface{}) error {
|
||||||
|
user := bean.(*User)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
// SELECT * FROM user
|
||||||
|
|
||||||
|
rows, err := engine.Rows(&User{Name:name})
|
||||||
|
// SELECT * FROM user
|
||||||
|
defer rows.Close()
|
||||||
|
bean := new(Struct)
|
||||||
|
for rows.Next() {
|
||||||
|
err = rows.Scan(bean)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* 更新数据,除非使用Cols,AllCols函数指明,默认只更新非空和非0的字段
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Id(1).Update(&user)
|
||||||
|
// UPDATE user SET ... Where id = ?
|
||||||
|
|
||||||
|
affected, err := engine.Update(&user, &User{Name:name})
|
||||||
|
// UPDATE user SET ... Where name = ?
|
||||||
|
|
||||||
|
var ids = []int64{1, 2, 3}
|
||||||
|
affected, err := engine.In(ids).Update(&user)
|
||||||
|
// UPDATE user SET ... Where id IN (?, ?, ?)
|
||||||
|
|
||||||
|
// force update indicated columns by Cols
|
||||||
|
affected, err := engine.Id(1).Cols("age").Update(&User{Name:name, Age: 12})
|
||||||
|
// UPDATE user SET age = ?, updated=? Where id = ?
|
||||||
|
|
||||||
|
// force NOT update indicated columns by Omit
|
||||||
|
affected, err := engine.Id(1).Omit("name").Update(&User{Name:name, Age: 12})
|
||||||
|
// UPDATE user SET age = ?, updated=? Where id = ?
|
||||||
|
|
||||||
|
affected, err := engine.Id(1).AllCols().Update(&user)
|
||||||
|
// UPDATE user SET name=?,age=?,salt=?,passwd=?,updated=? Where id = ?
|
||||||
|
```
|
||||||
|
|
||||||
|
* 删除记录,需要注意,删除必须至少有一个条件,否则会报错。要清空数据库可以用EmptyTable
|
||||||
|
|
||||||
|
```Go
|
||||||
|
affected, err := engine.Where(...).Delete(&user)
|
||||||
|
// DELETE FROM user Where ...
|
||||||
|
```
|
||||||
|
|
||||||
|
* 获取记录条数
|
||||||
|
|
||||||
|
```Go
|
||||||
|
counts, err := engine.Count(&user)
|
||||||
|
// SELECT count(*) AS total FROM user
|
||||||
|
```
|
||||||
|
|
||||||
|
# 案例
|
||||||
|
|
||||||
|
* [github.com/m3ng9i/qreader](https://github.com/m3ng9i/qreader)
|
||||||
|
|
||||||
* [Wego](http://github.com/go-tango/wego)
|
* [Wego](http://github.com/go-tango/wego)
|
||||||
|
|
||||||
|
2
Godeps/_workspace/src/github.com/go-xorm/xorm/VERSION
generated
vendored
2
Godeps/_workspace/src/github.com/go-xorm/xorm/VERSION
generated
vendored
@ -1 +1 @@
|
|||||||
xorm v0.4.2.0225
|
xorm v0.4.5.0204
|
||||||
|
2
Godeps/_workspace/src/github.com/go-xorm/xorm/doc.go
generated
vendored
2
Godeps/_workspace/src/github.com/go-xorm/xorm/doc.go
generated
vendored
@ -1,4 +1,4 @@
|
|||||||
// Copyright 2013 - 2014 The XORM Authors. All rights reserved.
|
// Copyright 2013 - 2015 The Xorm Authors. All rights reserved.
|
||||||
// Use of this source code is governed by a BSD
|
// Use of this source code is governed by a BSD
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
129
Godeps/_workspace/src/github.com/go-xorm/xorm/engine.go
generated
vendored
129
Godeps/_workspace/src/github.com/go-xorm/xorm/engine.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -316,6 +320,12 @@ func (engine *Engine) NoAutoTime() *Session {
|
|||||||
return session.NoAutoTime()
|
return session.NoAutoTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) NoAutoCondition(no ...bool) *Session {
|
||||||
|
session := engine.NewSession()
|
||||||
|
session.IsAutoClose = true
|
||||||
|
return session.NoAutoCondition(no...)
|
||||||
|
}
|
||||||
|
|
||||||
// Retrieve all tables, columns, indexes' informations from database.
|
// Retrieve all tables, columns, indexes' informations from database.
|
||||||
func (engine *Engine) DBMetas() ([]*core.Table, error) {
|
func (engine *Engine) DBMetas() ([]*core.Table, error) {
|
||||||
tables, err := engine.dialect.GetTables()
|
tables, err := engine.dialect.GetTables()
|
||||||
@ -373,13 +383,25 @@ func (engine *Engine) DumpAll(w io.Writer) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, table := range tables {
|
_, err = io.WriteString(w, fmt.Sprintf("/*Generated by xorm v%s %s*/\n\n",
|
||||||
_, err = io.WriteString(w, engine.dialect.CreateTableSql(table, "", table.StoreEngine, "")+"\n\n")
|
Version, time.Now().In(engine.TZLocation).Format("2006-01-02 15:04:05")))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, table := range tables {
|
||||||
|
if i > 0 {
|
||||||
|
_, err = io.WriteString(w, "\n")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_, err = io.WriteString(w, engine.dialect.CreateTableSql(table, "", table.StoreEngine, "")+";\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, index := range table.Indexes {
|
for _, index := range table.Indexes {
|
||||||
_, err = io.WriteString(w, engine.dialect.CreateIndexSql(table.Name, index)+"\n\n")
|
_, err = io.WriteString(w, engine.dialect.CreateIndexSql(table.Name, index)+";\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -439,7 +461,7 @@ func (engine *Engine) DumpAll(w io.Writer) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_, err = io.WriteString(w, temp[2:]+");\n\n")
|
_, err = io.WriteString(w, temp[2:]+");\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -506,6 +528,12 @@ func (engine *Engine) Distinct(columns ...string) *Session {
|
|||||||
return session.Distinct(columns...)
|
return session.Distinct(columns...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) Select(str string) *Session {
|
||||||
|
session := engine.NewSession()
|
||||||
|
session.IsAutoClose = true
|
||||||
|
return session.Select(str)
|
||||||
|
}
|
||||||
|
|
||||||
// only use the paramters as select or update columns
|
// only use the paramters as select or update columns
|
||||||
func (engine *Engine) Cols(columns ...string) *Session {
|
func (engine *Engine) Cols(columns ...string) *Session {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
@ -543,6 +571,13 @@ func (engine *Engine) Omit(columns ...string) *Session {
|
|||||||
return session.Omit(columns...)
|
return session.Omit(columns...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set null when column is zero-value and nullable for update
|
||||||
|
func (engine *Engine) Nullable(columns ...string) *Session {
|
||||||
|
session := engine.NewSession()
|
||||||
|
session.IsAutoClose = true
|
||||||
|
return session.Nullable(columns...)
|
||||||
|
}
|
||||||
|
|
||||||
// This method will generate "column IN (?, ?)"
|
// This method will generate "column IN (?, ?)"
|
||||||
func (engine *Engine) In(column string, args ...interface{}) *Session {
|
func (engine *Engine) In(column string, args ...interface{}) *Session {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
@ -642,20 +677,20 @@ func (engine *Engine) Having(conditions string) *Session {
|
|||||||
|
|
||||||
func (engine *Engine) autoMapType(v reflect.Value) *core.Table {
|
func (engine *Engine) autoMapType(v reflect.Value) *core.Table {
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
engine.mutex.RLock()
|
engine.mutex.Lock()
|
||||||
table, ok := engine.Tables[t]
|
table, ok := engine.Tables[t]
|
||||||
engine.mutex.RUnlock()
|
|
||||||
if !ok {
|
if !ok {
|
||||||
table = engine.mapType(v)
|
table = engine.mapType(v)
|
||||||
engine.mutex.Lock()
|
|
||||||
engine.Tables[t] = table
|
engine.Tables[t] = table
|
||||||
|
if engine.Cacher != nil {
|
||||||
if v.CanAddr() {
|
if v.CanAddr() {
|
||||||
engine.GobRegister(v.Addr().Interface())
|
engine.GobRegister(v.Addr().Interface())
|
||||||
} else {
|
} else {
|
||||||
engine.GobRegister(v.Interface())
|
engine.GobRegister(v.Interface())
|
||||||
}
|
}
|
||||||
engine.mutex.Unlock()
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
engine.mutex.Unlock()
|
||||||
return table
|
return table
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -691,33 +726,24 @@ func (engine *Engine) newTable() *core.Table {
|
|||||||
return table
|
return table
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TableName interface {
|
||||||
|
TableName() string
|
||||||
|
}
|
||||||
|
|
||||||
func (engine *Engine) mapType(v reflect.Value) *core.Table {
|
func (engine *Engine) mapType(v reflect.Value) *core.Table {
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
table := engine.newTable()
|
table := engine.newTable()
|
||||||
method := v.MethodByName("TableName")
|
if tb, ok := v.Interface().(TableName); ok {
|
||||||
if !method.IsValid() {
|
table.Name = tb.TableName()
|
||||||
if v.CanAddr() {
|
} else {
|
||||||
method = v.Addr().MethodByName("TableName")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if method.IsValid() {
|
|
||||||
params := []reflect.Value{}
|
|
||||||
results := method.Call(params)
|
|
||||||
if len(results) == 1 {
|
|
||||||
table.Name = results[0].Interface().(string)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if table.Name == "" {
|
|
||||||
table.Name = engine.TableMapper.Obj2Table(t.Name())
|
table.Name = engine.TableMapper.Obj2Table(t.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Type = t
|
table.Type = t
|
||||||
|
|
||||||
var idFieldColName string
|
var idFieldColName string
|
||||||
var err error
|
var err error
|
||||||
|
var hasCacheTag, hasNoCacheTag bool
|
||||||
hasCacheTag := false
|
|
||||||
hasNoCacheTag := false
|
|
||||||
|
|
||||||
for i := 0; i < t.NumField(); i++ {
|
for i := 0; i < t.NumField(); i++ {
|
||||||
tag := t.Field(i).Tag
|
tag := t.Field(i).Tag
|
||||||
@ -730,7 +756,7 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table {
|
|||||||
if ormTagStr != "" {
|
if ormTagStr != "" {
|
||||||
col = &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
|
col = &core.Column{FieldName: t.Field(i).Name, Nullable: true, IsPrimaryKey: false,
|
||||||
IsAutoIncrement: false, MapType: core.TWOSIDES, Indexes: make(map[string]bool)}
|
IsAutoIncrement: false, MapType: core.TWOSIDES, Indexes: make(map[string]bool)}
|
||||||
tags := strings.Split(ormTagStr, " ")
|
tags := splitTag(ormTagStr)
|
||||||
|
|
||||||
if len(tags) > 0 {
|
if len(tags) > 0 {
|
||||||
if tags[0] == "-" {
|
if tags[0] == "-" {
|
||||||
@ -804,6 +830,16 @@ func (engine *Engine) mapType(v reflect.Value) *core.Table {
|
|||||||
case k == "VERSION":
|
case k == "VERSION":
|
||||||
col.IsVersion = true
|
col.IsVersion = true
|
||||||
col.Default = "1"
|
col.Default = "1"
|
||||||
|
case k == "UTC":
|
||||||
|
col.TimeZone = time.UTC
|
||||||
|
case k == "LOCAL":
|
||||||
|
col.TimeZone = time.Local
|
||||||
|
case strings.HasPrefix(k, "LOCALE(") && strings.HasSuffix(k, ")"):
|
||||||
|
location := k[len("INDEX")+1 : len(k)-1]
|
||||||
|
col.TimeZone, err = time.LoadLocation(location)
|
||||||
|
if err != nil {
|
||||||
|
engine.LogError(err)
|
||||||
|
}
|
||||||
case k == "UPDATED":
|
case k == "UPDATED":
|
||||||
col.IsUpdated = true
|
col.IsUpdated = true
|
||||||
case k == "DELETED":
|
case k == "DELETED":
|
||||||
@ -1112,7 +1148,7 @@ func (engine *Engine) Sync(beans ...interface{}) error {
|
|||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
session.Statement.RefTable = table
|
session.Statement.RefTable = table
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
isExist, err := session.Engine.dialect.IsColumnExist(table.Name, col)
|
isExist, err := session.Engine.dialect.IsColumnExist(table.Name, col.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -1387,7 +1423,6 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func (engine *Engine) TZTime(t time.Time) time.Time {
|
func (engine *Engine) TZTime(t time.Time) time.Time {
|
||||||
|
|
||||||
if NULL_TIME != t { // if time is not initialized it's not suitable for Time.In()
|
if NULL_TIME != t { // if time is not initialized it's not suitable for Time.In()
|
||||||
return t.In(engine.TZLocation)
|
return t.In(engine.TZLocation)
|
||||||
}
|
}
|
||||||
@ -1405,35 +1440,51 @@ func (engine *Engine) NowTime2(sqlTypeName string) (interface{}, time.Time) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{}) {
|
func (engine *Engine) FormatTime(sqlTypeName string, t time.Time) (v interface{}) {
|
||||||
|
return engine.formatTime(engine.TZLocation, sqlTypeName, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) formatColTime(col *core.Column, t time.Time) (v interface{}) {
|
||||||
|
if col.DisableTimeZone {
|
||||||
|
return engine.formatTime(nil, col.SQLType.Name, t)
|
||||||
|
} else if col.TimeZone != nil {
|
||||||
|
return engine.formatTime(col.TimeZone, col.SQLType.Name, t)
|
||||||
|
}
|
||||||
|
return engine.formatTime(engine.TZLocation, col.SQLType.Name, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) formatTime(tz *time.Location, sqlTypeName string, t time.Time) (v interface{}) {
|
||||||
if engine.dialect.DBType() == core.ORACLE {
|
if engine.dialect.DBType() == core.ORACLE {
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
if tz != nil {
|
||||||
|
t = engine.TZTime(t)
|
||||||
|
}
|
||||||
switch sqlTypeName {
|
switch sqlTypeName {
|
||||||
case core.Time:
|
case core.Time:
|
||||||
s := engine.TZTime(t).Format("2006-01-02 15:04:05") //time.RFC3339
|
s := t.Format("2006-01-02 15:04:05") //time.RFC3339
|
||||||
v = s[11:19]
|
v = s[11:19]
|
||||||
case core.Date:
|
case core.Date:
|
||||||
v = engine.TZTime(t).Format("2006-01-02")
|
v = t.Format("2006-01-02")
|
||||||
case core.DateTime, core.TimeStamp:
|
case core.DateTime, core.TimeStamp:
|
||||||
if engine.dialect.DBType() == "ql" {
|
if engine.dialect.DBType() == "ql" {
|
||||||
v = engine.TZTime(t)
|
v = t
|
||||||
} else if engine.dialect.DBType() == "sqlite3" {
|
} else if engine.dialect.DBType() == "sqlite3" {
|
||||||
v = engine.TZTime(t).UTC().Format("2006-01-02 15:04:05")
|
v = t.UTC().Format("2006-01-02 15:04:05")
|
||||||
} else {
|
} else {
|
||||||
v = engine.TZTime(t).Format("2006-01-02 15:04:05")
|
v = t.Format("2006-01-02 15:04:05")
|
||||||
}
|
}
|
||||||
case core.TimeStampz:
|
case core.TimeStampz:
|
||||||
if engine.dialect.DBType() == core.MSSQL {
|
if engine.dialect.DBType() == core.MSSQL {
|
||||||
v = engine.TZTime(t).Format("2006-01-02T15:04:05.9999999Z07:00")
|
v = t.Format("2006-01-02T15:04:05.9999999Z07:00")
|
||||||
} else if engine.DriverName() == "mssql" {
|
} else if engine.DriverName() == "mssql" {
|
||||||
v = engine.TZTime(t)
|
v = t
|
||||||
} else {
|
} else {
|
||||||
v = engine.TZTime(t).Format(time.RFC3339Nano)
|
v = t.Format(time.RFC3339Nano)
|
||||||
}
|
}
|
||||||
case core.BigInt, core.Int:
|
case core.BigInt, core.Int:
|
||||||
v = engine.TZTime(t).Unix()
|
v = t.Unix()
|
||||||
default:
|
default:
|
||||||
v = engine.TZTime(t)
|
v = t
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
4
Godeps/_workspace/src/github.com/go-xorm/xorm/error.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/error.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
BIN
Godeps/_workspace/src/github.com/go-xorm/xorm/examples/goroutine.db-journal
generated
vendored
BIN
Godeps/_workspace/src/github.com/go-xorm/xorm/examples/goroutine.db-journal
generated
vendored
Binary file not shown.
4
Godeps/_workspace/src/github.com/go-xorm/xorm/examples/goroutine.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/examples/goroutine.go
generated
vendored
@ -33,7 +33,7 @@ func test(engine *xorm.Engine) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
size := 500
|
size := 100
|
||||||
queue := make(chan int, size)
|
queue := make(chan int, size)
|
||||||
|
|
||||||
for i := 0; i < size; i++ {
|
for i := 0; i < size; i++ {
|
||||||
@ -83,7 +83,7 @@ func test(engine *xorm.Engine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
runtime.GOMAXPROCS(1)
|
runtime.GOMAXPROCS(2)
|
||||||
fmt.Println("-----start sqlite go routines-----")
|
fmt.Println("-----start sqlite go routines-----")
|
||||||
engine, err := sqliteEngine()
|
engine, err := sqliteEngine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
4
Godeps/_workspace/src/github.com/go-xorm/xorm/goracle_driver.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/goracle_driver.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
73
Godeps/_workspace/src/github.com/go-xorm/xorm/helpers.go
generated
vendored
73
Godeps/_workspace/src/github.com/go-xorm/xorm/helpers.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -11,6 +15,30 @@ import (
|
|||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func splitTag(tag string) (tags []string) {
|
||||||
|
tag = strings.TrimSpace(tag)
|
||||||
|
var hasQuote = false
|
||||||
|
var lastIdx = 0
|
||||||
|
for i, t := range tag {
|
||||||
|
if t == '\'' {
|
||||||
|
hasQuote = !hasQuote
|
||||||
|
} else if t == ' ' {
|
||||||
|
if lastIdx < i && !hasQuote {
|
||||||
|
tags = append(tags, strings.TrimSpace(tag[lastIdx:i]))
|
||||||
|
lastIdx = i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if lastIdx < len(tag) {
|
||||||
|
tags = append(tags, strings.TrimSpace(tag[lastIdx:len(tag)]))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type zeroable interface {
|
||||||
|
IsZero() bool
|
||||||
|
}
|
||||||
|
|
||||||
func isZero(k interface{}) bool {
|
func isZero(k interface{}) bool {
|
||||||
switch k.(type) {
|
switch k.(type) {
|
||||||
case int:
|
case int:
|
||||||
@ -33,12 +61,41 @@ func isZero(k interface{}) bool {
|
|||||||
return k.(uint32) == 0
|
return k.(uint32) == 0
|
||||||
case uint64:
|
case uint64:
|
||||||
return k.(uint64) == 0
|
return k.(uint64) == 0
|
||||||
|
case float32:
|
||||||
|
return k.(float32) == 0
|
||||||
|
case float64:
|
||||||
|
return k.(float64) == 0
|
||||||
|
case bool:
|
||||||
|
return k.(bool) == false
|
||||||
case string:
|
case string:
|
||||||
return k.(string) == ""
|
return k.(string) == ""
|
||||||
|
case zeroable:
|
||||||
|
return k.(zeroable).IsZero()
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func int64ToInt(id int64, k reflect.Kind) interface{} {
|
||||||
|
var v interface{} = id
|
||||||
|
switch k {
|
||||||
|
case reflect.Int16:
|
||||||
|
v = int16(id)
|
||||||
|
case reflect.Int32:
|
||||||
|
v = int32(id)
|
||||||
|
case reflect.Int:
|
||||||
|
v = int(id)
|
||||||
|
case reflect.Uint16:
|
||||||
|
v = uint16(id)
|
||||||
|
case reflect.Uint32:
|
||||||
|
v = uint32(id)
|
||||||
|
case reflect.Uint64:
|
||||||
|
v = uint64(id)
|
||||||
|
case reflect.Uint:
|
||||||
|
v = uint(id)
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func isPKZero(pk core.PK) bool {
|
func isPKZero(pk core.PK) bool {
|
||||||
for _, k := range pk {
|
for _, k := range pk {
|
||||||
if isZero(k) {
|
if isZero(k) {
|
||||||
@ -48,6 +105,10 @@ func isPKZero(pk core.PK) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func equalNoCase(s1, s2 string) bool {
|
||||||
|
return strings.ToLower(s1) == strings.ToLower(s2)
|
||||||
|
}
|
||||||
|
|
||||||
func indexNoCase(s, sep string) int {
|
func indexNoCase(s, sep string) int {
|
||||||
return strings.Index(strings.ToLower(s), strings.ToLower(sep))
|
return strings.Index(strings.ToLower(s), strings.ToLower(sep))
|
||||||
}
|
}
|
||||||
@ -129,8 +190,8 @@ func reflect2value(rawValue *reflect.Value) (str string, err error) {
|
|||||||
}
|
}
|
||||||
//时间类型
|
//时间类型
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if aa == core.TimeType {
|
if aa.ConvertibleTo(core.TimeType) {
|
||||||
str = rawValue.Interface().(time.Time).Format(time.RFC3339Nano)
|
str = vv.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano)
|
||||||
} else {
|
} else {
|
||||||
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
||||||
}
|
}
|
||||||
@ -352,6 +413,14 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// !evalphobia! set fieldValue as nil when column is nullable and zero-value
|
||||||
|
if _, ok := session.Statement.nullableMap[lColName]; ok {
|
||||||
|
if col.Nullable && isZero(fieldValue.Interface()) {
|
||||||
|
var nilValue *int
|
||||||
|
fieldValue = reflect.ValueOf(nilValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
|
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
|
||||||
val, t := session.Engine.NowTime2(col.SQLType.Name)
|
val, t := session.Engine.NowTime2(col.SQLType.Name)
|
||||||
args = append(args, val)
|
args = append(args, val)
|
||||||
|
22
Godeps/_workspace/src/github.com/go-xorm/xorm/helpers_test.go
generated
vendored
Normal file
22
Godeps/_workspace/src/github.com/go-xorm/xorm/helpers_test.go
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package xorm
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestSplitTag(t *testing.T) {
|
||||||
|
var cases = []struct {
|
||||||
|
tag string
|
||||||
|
tags []string
|
||||||
|
}{
|
||||||
|
{"not null default '2000-01-01 00:00:00' TIMESTAMP", []string{"not", "null", "default", "'2000-01-01 00:00:00'", "TIMESTAMP"}},
|
||||||
|
{"TEXT", []string{"TEXT"}},
|
||||||
|
{"default('2000-01-01 00:00:00')", []string{"default('2000-01-01 00:00:00')"}},
|
||||||
|
{"json binary", []string{"json", "binary"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, kase := range cases {
|
||||||
|
tags := splitTag(kase.tag)
|
||||||
|
if !sliceEq(tags, kase.tags) {
|
||||||
|
t.Fatalf("[%d]%v is not equal [%d]%v", len(tags), tags, len(kase.tags), kase.tags)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
4
Godeps/_workspace/src/github.com/go-xorm/xorm/logger.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/logger.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
5
Godeps/_workspace/src/github.com/go-xorm/xorm/lru_cacher.go
generated
vendored
5
Godeps/_workspace/src/github.com/go-xorm/xorm/lru_cacher.go
generated
vendored
@ -1,4 +1,7 @@
|
|||||||
//LRUCacher implements Cacher according to LRU algorithm
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
// MemoryStore implements CacheStore provide local machine
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
19
Godeps/_workspace/src/github.com/go-xorm/xorm/mssql_dialect.go
generated
vendored
19
Godeps/_workspace/src/github.com/go-xorm/xorm/mssql_dialect.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -216,6 +220,11 @@ func (db *mssql) SqlType(c *core.Column) string {
|
|||||||
switch t := c.SQLType.Name; t {
|
switch t := c.SQLType.Name; t {
|
||||||
case core.Bool:
|
case core.Bool:
|
||||||
res = core.TinyInt
|
res = core.TinyInt
|
||||||
|
if c.Default == "true" {
|
||||||
|
c.Default = "1"
|
||||||
|
} else if c.Default == "false" {
|
||||||
|
c.Default = "0"
|
||||||
|
}
|
||||||
case core.Serial:
|
case core.Serial:
|
||||||
c.IsAutoIncrement = true
|
c.IsAutoIncrement = true
|
||||||
c.IsPrimaryKey = true
|
c.IsPrimaryKey = true
|
||||||
@ -238,7 +247,7 @@ func (db *mssql) SqlType(c *core.Column) string {
|
|||||||
c.Length = 7
|
c.Length = 7
|
||||||
case core.MediumInt:
|
case core.MediumInt:
|
||||||
res = core.Int
|
res = core.Int
|
||||||
case core.MediumText, core.TinyText, core.LongText:
|
case core.MediumText, core.TinyText, core.LongText, core.Json:
|
||||||
res = core.Text
|
res = core.Text
|
||||||
case core.Double:
|
case core.Double:
|
||||||
res = core.Real
|
res = core.Real
|
||||||
@ -311,10 +320,10 @@ func (db *mssql) IndexCheckSql(tableName, idxName string) (string, []interface{}
|
|||||||
return sql, args
|
return sql, args
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
func (db *mssql) IsColumnExist(tableName string, col *core.Column) (bool, error) {
|
func (db *mssql) IsColumnExist(tableName, colName string) (bool, error) {
|
||||||
query := `SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "COLUMN_NAME" = ?`
|
query := `SELECT "COLUMN_NAME" FROM "INFORMATION_SCHEMA"."COLUMNS" WHERE "TABLE_NAME" = ? AND "COLUMN_NAME" = ?`
|
||||||
|
|
||||||
return db.HasRecords(query, tableName, col.Name)
|
return db.HasRecords(query, tableName, colName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *mssql) TableCheckSql(tableName string) (string, []interface{}) {
|
func (db *mssql) TableCheckSql(tableName string) (string, []interface{}) {
|
||||||
@ -500,6 +509,10 @@ func (db *mssql) CreateTableSql(table *core.Table, tableName, storeEngine, chars
|
|||||||
return sql
|
return sql
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *mssql) ForUpdateSql(query string) string {
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
func (db *mssql) Filters() []core.Filter {
|
func (db *mssql) Filters() []core.Filter {
|
||||||
return []core.Filter{&core.IdFilter{}, &core.QuoteFilter{}}
|
return []core.Filter{&core.IdFilter{}, &core.QuoteFilter{}}
|
||||||
}
|
}
|
||||||
|
4
Godeps/_workspace/src/github.com/go-xorm/xorm/mymysql_driver.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/mymysql_driver.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
6
Godeps/_workspace/src/github.com/go-xorm/xorm/mysql_dialect.go
generated
vendored
6
Godeps/_workspace/src/github.com/go-xorm/xorm/mysql_dialect.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -221,6 +225,8 @@ func (db *mysql) SqlType(c *core.Column) string {
|
|||||||
case core.Uuid:
|
case core.Uuid:
|
||||||
res = core.Varchar
|
res = core.Varchar
|
||||||
c.Length = 40
|
c.Length = 40
|
||||||
|
case core.Json:
|
||||||
|
res = core.Text
|
||||||
default:
|
default:
|
||||||
res = t
|
res = t
|
||||||
}
|
}
|
||||||
|
4
Godeps/_workspace/src/github.com/go-xorm/xorm/mysql_driver.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/mysql_driver.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
4
Godeps/_workspace/src/github.com/go-xorm/xorm/oci8_driver.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/oci8_driver.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
4
Godeps/_workspace/src/github.com/go-xorm/xorm/odbc_driver.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/odbc_driver.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
12
Godeps/_workspace/src/github.com/go-xorm/xorm/oracle_dialect.go
generated
vendored
12
Godeps/_workspace/src/github.com/go-xorm/xorm/oracle_dialect.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -518,7 +522,7 @@ func (db *oracle) SqlType(c *core.Column) string {
|
|||||||
res = "TIMESTAMP WITH TIME ZONE"
|
res = "TIMESTAMP WITH TIME ZONE"
|
||||||
case core.Float, core.Double, core.Numeric, core.Decimal:
|
case core.Float, core.Double, core.Numeric, core.Decimal:
|
||||||
res = "NUMBER"
|
res = "NUMBER"
|
||||||
case core.Text, core.MediumText, core.LongText:
|
case core.Text, core.MediumText, core.LongText, core.Json:
|
||||||
res = "CLOB"
|
res = "CLOB"
|
||||||
case core.Char, core.Varchar, core.TinyText:
|
case core.Char, core.Varchar, core.TinyText:
|
||||||
res = "VARCHAR2"
|
res = "VARCHAR2"
|
||||||
@ -661,8 +665,8 @@ func (db *oracle) MustDropTable(tableName string) error {
|
|||||||
" AND column_name = ?", args
|
" AND column_name = ?", args
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
func (db *oracle) IsColumnExist(tableName string, col *core.Column) (bool, error) {
|
func (db *oracle) IsColumnExist(tableName, colName string) (bool, error) {
|
||||||
args := []interface{}{tableName, col.Name}
|
args := []interface{}{tableName, colName}
|
||||||
query := "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = :1" +
|
query := "SELECT column_name FROM USER_TAB_COLUMNS WHERE table_name = :1" +
|
||||||
" AND column_name = :2"
|
" AND column_name = :2"
|
||||||
rows, err := db.DB().Query(query, args...)
|
rows, err := db.DB().Query(query, args...)
|
||||||
@ -740,6 +744,8 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*core.Colum
|
|||||||
switch dt {
|
switch dt {
|
||||||
case "VARCHAR2":
|
case "VARCHAR2":
|
||||||
col.SQLType = core.SQLType{core.Varchar, len1, len2}
|
col.SQLType = core.SQLType{core.Varchar, len1, len2}
|
||||||
|
case "NVARCHAR2":
|
||||||
|
col.SQLType = core.SQLType{core.NVarchar, len1, len2}
|
||||||
case "TIMESTAMP WITH TIME ZONE":
|
case "TIMESTAMP WITH TIME ZONE":
|
||||||
col.SQLType = core.SQLType{core.TimeStampz, 0, 0}
|
col.SQLType = core.SQLType{core.TimeStampz, 0, 0}
|
||||||
case "NUMBER":
|
case "NUMBER":
|
||||||
|
13
Godeps/_workspace/src/github.com/go-xorm/xorm/postgres_dialect.go
generated
vendored
13
Godeps/_workspace/src/github.com/go-xorm/xorm/postgres_dialect.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -892,8 +896,8 @@ func (db *postgres) DropIndexSql(tableName string, index *core.Index) string {
|
|||||||
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *postgres) IsColumnExist(tableName string, col *core.Column) (bool, error) {
|
func (db *postgres) IsColumnExist(tableName, colName string) (bool, error) {
|
||||||
args := []interface{}{tableName, col.Name}
|
args := []interface{}{tableName, colName}
|
||||||
query := "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1" +
|
query := "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = $1" +
|
||||||
" AND column_name = $2"
|
" AND column_name = $2"
|
||||||
rows, err := db.DB().Query(query, args...)
|
rows, err := db.DB().Query(query, args...)
|
||||||
@ -909,7 +913,8 @@ func (db *postgres) IsColumnExist(tableName string, col *core.Column) (bool, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (db *postgres) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
func (db *postgres) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
|
||||||
args := []interface{}{tableName}
|
pgSchema := "public"
|
||||||
|
args := []interface{}{tableName,pgSchema}
|
||||||
s := `SELECT column_name, column_default, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_precision_radix ,
|
s := `SELECT column_name, column_default, is_nullable, data_type, character_maximum_length, numeric_precision, numeric_precision_radix ,
|
||||||
CASE WHEN p.contype = 'p' THEN true ELSE false END AS primarykey,
|
CASE WHEN p.contype = 'p' THEN true ELSE false END AS primarykey,
|
||||||
CASE WHEN p.contype = 'u' THEN true ELSE false END AS uniquekey
|
CASE WHEN p.contype = 'u' THEN true ELSE false END AS uniquekey
|
||||||
@ -920,7 +925,7 @@ FROM pg_attribute f
|
|||||||
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
|
LEFT JOIN pg_constraint p ON p.conrelid = c.oid AND f.attnum = ANY (p.conkey)
|
||||||
LEFT JOIN pg_class AS g ON p.confrelid = g.oid
|
LEFT JOIN pg_class AS g ON p.confrelid = g.oid
|
||||||
LEFT JOIN INFORMATION_SCHEMA.COLUMNS s ON s.column_name=f.attname AND c.relname=s.table_name
|
LEFT JOIN INFORMATION_SCHEMA.COLUMNS s ON s.column_name=f.attname AND c.relname=s.table_name
|
||||||
WHERE c.relkind = 'r'::char AND c.relname = $1 AND f.attnum > 0 ORDER BY f.attnum;`
|
WHERE c.relkind = 'r'::char AND c.relname = $1 AND s.table_schema = $2 AND f.attnum > 0 ORDER BY f.attnum;`
|
||||||
|
|
||||||
rows, err := db.DB().Query(s, args...)
|
rows, err := db.DB().Query(s, args...)
|
||||||
if db.Logger != nil {
|
if db.Logger != nil {
|
||||||
|
8
Godeps/_workspace/src/github.com/go-xorm/xorm/pq_driver.go
generated
vendored
8
Godeps/_workspace/src/github.com/go-xorm/xorm/pq_driver.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -37,7 +41,7 @@ func parseURL(connstr string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if u.Scheme != "postgres" {
|
if u.Scheme != "postgresql" && u.Scheme != "postgres" {
|
||||||
return "", fmt.Errorf("invalid connection protocol: %s", u.Scheme)
|
return "", fmt.Errorf("invalid connection protocol: %s", u.Scheme)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +103,7 @@ func (p *pqDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
|
|||||||
db := &core.Uri{DbType: core.POSTGRES}
|
db := &core.Uri{DbType: core.POSTGRES}
|
||||||
o := make(values)
|
o := make(values)
|
||||||
var err error
|
var err error
|
||||||
if strings.HasPrefix(dataSourceName, "postgres://") {
|
if strings.HasPrefix(dataSourceName, "postgresql://") || strings.HasPrefix(dataSourceName, "postgres://") {
|
||||||
dataSourceName, err = parseURL(dataSourceName)
|
dataSourceName, err = parseURL(dataSourceName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
8
Godeps/_workspace/src/github.com/go-xorm/xorm/processors.go
generated
vendored
8
Godeps/_workspace/src/github.com/go-xorm/xorm/processors.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
// Executed before an object is initially persisted to the database
|
// Executed before an object is initially persisted to the database
|
||||||
@ -19,6 +23,10 @@ type BeforeSetProcessor interface {
|
|||||||
BeforeSet(string, Cell)
|
BeforeSet(string, Cell)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AfterSetProcessor interface {
|
||||||
|
AfterSet(string, Cell)
|
||||||
|
}
|
||||||
|
|
||||||
// !nashtsai! TODO enable BeforeValidateProcessor when xorm start to support validations
|
// !nashtsai! TODO enable BeforeValidateProcessor when xorm start to support validations
|
||||||
//// Executed before an object is validated
|
//// Executed before an object is validated
|
||||||
//type BeforeValidateProcessor interface {
|
//type BeforeValidateProcessor interface {
|
||||||
|
6
Godeps/_workspace/src/github.com/go-xorm/xorm/rows.go
generated
vendored
6
Godeps/_workspace/src/github.com/go-xorm/xorm/rows.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -41,7 +45,7 @@ func newRows(session *Session, bean interface{}) (*Rows, error) {
|
|||||||
sqlStr = filter.Do(sqlStr, session.Engine.dialect, rows.session.Statement.RefTable)
|
sqlStr = filter.Do(sqlStr, session.Engine.dialect, rows.session.Statement.RefTable)
|
||||||
}
|
}
|
||||||
|
|
||||||
rows.session.Engine.logSQL(sqlStr, args)
|
rows.session.saveLastSQL(sqlStr, args)
|
||||||
var err error
|
var err error
|
||||||
rows.stmt, err = rows.session.DB().Prepare(sqlStr)
|
rows.stmt, err = rows.session.DB().Prepare(sqlStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
661
Godeps/_workspace/src/github.com/go-xorm/xorm/session.go
generated
vendored
661
Godeps/_workspace/src/github.com/go-xorm/xorm/session.go
generated
vendored
File diff suppressed because it is too large
Load Diff
30
Godeps/_workspace/src/github.com/go-xorm/xorm/sqlite3_dialect.go
generated
vendored
30
Godeps/_workspace/src/github.com/go-xorm/xorm/sqlite3_dialect.go
generated
vendored
@ -1,9 +1,14 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
@ -152,13 +157,21 @@ func (db *sqlite3) Init(d *core.DB, uri *core.Uri, drivername, dataSourceName st
|
|||||||
|
|
||||||
func (db *sqlite3) SqlType(c *core.Column) string {
|
func (db *sqlite3) SqlType(c *core.Column) string {
|
||||||
switch t := c.SQLType.Name; t {
|
switch t := c.SQLType.Name; t {
|
||||||
|
case core.Bool:
|
||||||
|
if c.Default == "true" {
|
||||||
|
c.Default = "1"
|
||||||
|
} else if c.Default == "false" {
|
||||||
|
c.Default = "0"
|
||||||
|
}
|
||||||
|
return core.Integer
|
||||||
case core.Date, core.DateTime, core.TimeStamp, core.Time:
|
case core.Date, core.DateTime, core.TimeStamp, core.Time:
|
||||||
return core.DateTime
|
return core.DateTime
|
||||||
case core.TimeStampz:
|
case core.TimeStampz:
|
||||||
return core.Text
|
return core.Text
|
||||||
case core.Char, core.Varchar, core.NVarchar, core.TinyText, core.Text, core.MediumText, core.LongText:
|
case core.Char, core.Varchar, core.NVarchar, core.TinyText,
|
||||||
|
core.Text, core.MediumText, core.LongText, core.Json:
|
||||||
return core.Text
|
return core.Text
|
||||||
case core.Bit, core.TinyInt, core.SmallInt, core.MediumInt, core.Int, core.Integer, core.BigInt, core.Bool:
|
case core.Bit, core.TinyInt, core.SmallInt, core.MediumInt, core.Int, core.Integer, core.BigInt:
|
||||||
return core.Integer
|
return core.Integer
|
||||||
case core.Float, core.Double, core.Real:
|
case core.Float, core.Double, core.Real:
|
||||||
return core.Real
|
return core.Real
|
||||||
@ -238,15 +251,19 @@ func (db *sqlite3) DropIndexSql(tableName string, index *core.Index) string {
|
|||||||
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *sqlite3) ForUpdateSql(query string) string {
|
||||||
|
return query
|
||||||
|
}
|
||||||
|
|
||||||
/*func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
/*func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
||||||
args := []interface{}{tableName}
|
args := []interface{}{tableName}
|
||||||
sql := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))"
|
sql := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))"
|
||||||
return sql, args
|
return sql, args
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
func (db *sqlite3) IsColumnExist(tableName string, col *core.Column) (bool, error) {
|
func (db *sqlite3) IsColumnExist(tableName, colName string) (bool, error) {
|
||||||
args := []interface{}{tableName}
|
args := []interface{}{tableName}
|
||||||
query := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + col.Name + "`%') or (sql like '%[" + col.Name + "]%'))"
|
query := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))"
|
||||||
rows, err := db.DB().Query(query, args...)
|
rows, err := db.DB().Query(query, args...)
|
||||||
if db.Logger != nil {
|
if db.Logger != nil {
|
||||||
db.Logger.Info("[sql]", query, args)
|
db.Logger.Info("[sql]", query, args)
|
||||||
@ -290,10 +307,13 @@ func (db *sqlite3) GetColumns(tableName string) ([]string, map[string]*core.Colu
|
|||||||
|
|
||||||
nStart := strings.Index(name, "(")
|
nStart := strings.Index(name, "(")
|
||||||
nEnd := strings.LastIndex(name, ")")
|
nEnd := strings.LastIndex(name, ")")
|
||||||
colCreates := strings.Split(name[nStart+1:nEnd], ",")
|
reg := regexp.MustCompile(`[^\(,\)]*(\([^\(]*\))?`)
|
||||||
|
colCreates := reg.FindAllString(name[nStart+1:nEnd], -1)
|
||||||
cols := make(map[string]*core.Column)
|
cols := make(map[string]*core.Column)
|
||||||
colSeq := make([]string, 0)
|
colSeq := make([]string, 0)
|
||||||
for _, colStr := range colCreates {
|
for _, colStr := range colCreates {
|
||||||
|
reg = regexp.MustCompile(`,\s`)
|
||||||
|
colStr = reg.ReplaceAllString(colStr, ",")
|
||||||
fields := strings.Fields(strings.TrimSpace(colStr))
|
fields := strings.Fields(strings.TrimSpace(colStr))
|
||||||
col := new(core.Column)
|
col := new(core.Column)
|
||||||
col.Indexes = make(map[string]bool)
|
col.Indexes = make(map[string]bool)
|
||||||
|
4
Godeps/_workspace/src/github.com/go-xorm/xorm/sqlite3_driver.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/sqlite3_driver.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
473
Godeps/_workspace/src/github.com/go-xorm/xorm/statement.go
generated
vendored
473
Godeps/_workspace/src/github.com/go-xorm/xorm/statement.go
generated
vendored
@ -1,6 +1,12 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"database/sql/driver"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -45,6 +51,7 @@ type Statement struct {
|
|||||||
GroupByStr string
|
GroupByStr string
|
||||||
HavingStr string
|
HavingStr string
|
||||||
ColumnStr string
|
ColumnStr string
|
||||||
|
selectStr string
|
||||||
columnMap map[string]bool
|
columnMap map[string]bool
|
||||||
useAllCols bool
|
useAllCols bool
|
||||||
OmitStr string
|
OmitStr string
|
||||||
@ -59,12 +66,15 @@ type Statement struct {
|
|||||||
BeanArgs []interface{}
|
BeanArgs []interface{}
|
||||||
UseCache bool
|
UseCache bool
|
||||||
UseAutoTime bool
|
UseAutoTime bool
|
||||||
|
noAutoCondition bool
|
||||||
IsDistinct bool
|
IsDistinct bool
|
||||||
|
IsForUpdate bool
|
||||||
TableAlias string
|
TableAlias string
|
||||||
allUseBool bool
|
allUseBool bool
|
||||||
checkVersion bool
|
checkVersion bool
|
||||||
unscoped bool
|
unscoped bool
|
||||||
mustColumnMap map[string]bool
|
mustColumnMap map[string]bool
|
||||||
|
nullableMap map[string]bool
|
||||||
inColumns map[string]*inParam
|
inColumns map[string]*inParam
|
||||||
incrColumns map[string]incrParam
|
incrColumns map[string]incrParam
|
||||||
decrColumns map[string]decrParam
|
decrColumns map[string]decrParam
|
||||||
@ -94,11 +104,15 @@ func (statement *Statement) Init() {
|
|||||||
statement.BeanArgs = make([]interface{}, 0)
|
statement.BeanArgs = make([]interface{}, 0)
|
||||||
statement.UseCache = true
|
statement.UseCache = true
|
||||||
statement.UseAutoTime = true
|
statement.UseAutoTime = true
|
||||||
|
statement.noAutoCondition = false
|
||||||
statement.IsDistinct = false
|
statement.IsDistinct = false
|
||||||
|
statement.IsForUpdate = false
|
||||||
statement.TableAlias = ""
|
statement.TableAlias = ""
|
||||||
|
statement.selectStr = ""
|
||||||
statement.allUseBool = false
|
statement.allUseBool = false
|
||||||
statement.useAllCols = false
|
statement.useAllCols = false
|
||||||
statement.mustColumnMap = make(map[string]bool)
|
statement.mustColumnMap = make(map[string]bool)
|
||||||
|
statement.nullableMap = make(map[string]bool)
|
||||||
statement.checkVersion = true
|
statement.checkVersion = true
|
||||||
statement.unscoped = false
|
statement.unscoped = false
|
||||||
statement.inColumns = make(map[string]*inParam)
|
statement.inColumns = make(map[string]*inParam)
|
||||||
@ -107,20 +121,29 @@ func (statement *Statement) Init() {
|
|||||||
statement.exprColumns = make(map[string]exprParam)
|
statement.exprColumns = make(map[string]exprParam)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add the raw sql statement
|
// NoAutoCondition if you do not want convert bean's field as query condition, then use this function
|
||||||
|
func (statement *Statement) NoAutoCondition(no ...bool) *Statement {
|
||||||
|
statement.noAutoCondition = true
|
||||||
|
if len(no) > 0 {
|
||||||
|
statement.noAutoCondition = no[0]
|
||||||
|
}
|
||||||
|
return statement
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sql add the raw sql statement
|
||||||
func (statement *Statement) Sql(querystring string, args ...interface{}) *Statement {
|
func (statement *Statement) Sql(querystring string, args ...interface{}) *Statement {
|
||||||
statement.RawSQL = querystring
|
statement.RawSQL = querystring
|
||||||
statement.RawParams = args
|
statement.RawParams = args
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the table alias
|
// Alias set the table alias
|
||||||
func (statement *Statement) Alias(alias string) *Statement {
|
func (statement *Statement) Alias(alias string) *Statement {
|
||||||
statement.TableAlias = alias
|
statement.TableAlias = alias
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// add Where statment
|
// Where add Where statment
|
||||||
func (statement *Statement) Where(querystring string, args ...interface{}) *Statement {
|
func (statement *Statement) Where(querystring string, args ...interface{}) *Statement {
|
||||||
if !strings.Contains(querystring, statement.Engine.dialect.EqStr()) {
|
if !strings.Contains(querystring, statement.Engine.dialect.EqStr()) {
|
||||||
querystring = strings.Replace(querystring, "=", statement.Engine.dialect.EqStr(), -1)
|
querystring = strings.Replace(querystring, "=", statement.Engine.dialect.EqStr(), -1)
|
||||||
@ -130,11 +153,13 @@ func (statement *Statement) Where(querystring string, args ...interface{}) *Stat
|
|||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// add Where & and statment
|
// And add Where & and statment
|
||||||
func (statement *Statement) And(querystring string, args ...interface{}) *Statement {
|
func (statement *Statement) And(querystring string, args ...interface{}) *Statement {
|
||||||
if statement.WhereStr != "" {
|
if len(statement.WhereStr) > 0 {
|
||||||
statement.WhereStr = fmt.Sprintf("(%v) %s (%v)", statement.WhereStr,
|
var buf bytes.Buffer
|
||||||
|
fmt.Fprintf(&buf, "(%v) %s (%v)", statement.WhereStr,
|
||||||
statement.Engine.dialect.AndStr(), querystring)
|
statement.Engine.dialect.AndStr(), querystring)
|
||||||
|
statement.WhereStr = buf.String()
|
||||||
} else {
|
} else {
|
||||||
statement.WhereStr = querystring
|
statement.WhereStr = querystring
|
||||||
}
|
}
|
||||||
@ -142,11 +167,13 @@ func (statement *Statement) And(querystring string, args ...interface{}) *Statem
|
|||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// add Where & Or statment
|
// Or add Where & Or statment
|
||||||
func (statement *Statement) Or(querystring string, args ...interface{}) *Statement {
|
func (statement *Statement) Or(querystring string, args ...interface{}) *Statement {
|
||||||
if statement.WhereStr != "" {
|
if len(statement.WhereStr) > 0 {
|
||||||
statement.WhereStr = fmt.Sprintf("(%v) %s (%v)", statement.WhereStr,
|
var buf bytes.Buffer
|
||||||
|
fmt.Fprintf(&buf, "(%v) %s (%v)", statement.WhereStr,
|
||||||
statement.Engine.dialect.OrStr(), querystring)
|
statement.Engine.dialect.OrStr(), querystring)
|
||||||
|
statement.WhereStr = buf.String()
|
||||||
} else {
|
} else {
|
||||||
statement.WhereStr = querystring
|
statement.WhereStr = querystring
|
||||||
}
|
}
|
||||||
@ -154,7 +181,7 @@ func (statement *Statement) Or(querystring string, args ...interface{}) *Stateme
|
|||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// tempororily set table name
|
// Table tempororily set table name, the parameter could be a string or a pointer of struct
|
||||||
func (statement *Statement) Table(tableNameOrBean interface{}) *Statement {
|
func (statement *Statement) Table(tableNameOrBean interface{}) *Statement {
|
||||||
v := rValue(tableNameOrBean)
|
v := rValue(tableNameOrBean)
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
@ -166,127 +193,12 @@ func (statement *Statement) Table(tableNameOrBean interface{}) *Statement {
|
|||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
/*func (statement *Statement) genFields(bean interface{}) map[string]interface{} {
|
// Auto generating update columnes and values according a struct
|
||||||
results := make(map[string]interface{})
|
|
||||||
table := statement.Engine.TableInfo(bean)
|
|
||||||
for _, col := range table.Columns {
|
|
||||||
fieldValue := col.ValueOf(bean)
|
|
||||||
fieldType := reflect.TypeOf(fieldValue.Interface())
|
|
||||||
var val interface{}
|
|
||||||
switch fieldType.Kind() {
|
|
||||||
case reflect.Bool:
|
|
||||||
if allUseBool {
|
|
||||||
val = fieldValue.Interface()
|
|
||||||
} else if _, ok := boolColumnMap[col.Name]; ok {
|
|
||||||
val = fieldValue.Interface()
|
|
||||||
} else {
|
|
||||||
// if a bool in a struct, it will not be as a condition because it default is false,
|
|
||||||
// please use Where() instead
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
case reflect.String:
|
|
||||||
if fieldValue.String() == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// for MyString, should convert to string or panic
|
|
||||||
if fieldType.String() != reflect.String.String() {
|
|
||||||
val = fieldValue.String()
|
|
||||||
} else {
|
|
||||||
val = fieldValue.Interface()
|
|
||||||
}
|
|
||||||
case reflect.Int8, reflect.Int16, reflect.Int, reflect.Int32, reflect.Int64:
|
|
||||||
if fieldValue.Int() == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val = fieldValue.Interface()
|
|
||||||
case reflect.Float32, reflect.Float64:
|
|
||||||
if fieldValue.Float() == 0.0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val = fieldValue.Interface()
|
|
||||||
case reflect.Uint8, reflect.Uint16, reflect.Uint, reflect.Uint32, reflect.Uint64:
|
|
||||||
if fieldValue.Uint() == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val = fieldValue.Interface()
|
|
||||||
case reflect.Struct:
|
|
||||||
if fieldType == reflect.TypeOf(time.Now()) {
|
|
||||||
t := fieldValue.Interface().(time.Time)
|
|
||||||
if t.IsZero() || !fieldValue.IsValid() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
var str string
|
|
||||||
if col.SQLType.Name == Time {
|
|
||||||
s := t.UTC().Format("2006-01-02 15:04:05")
|
|
||||||
val = s[11:19]
|
|
||||||
} else if col.SQLType.Name == Date {
|
|
||||||
str = t.Format("2006-01-02")
|
|
||||||
val = str
|
|
||||||
} else {
|
|
||||||
val = t
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
engine.autoMapType(fieldValue.Type())
|
|
||||||
if table, ok := engine.Tables[fieldValue.Type()]; ok {
|
|
||||||
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumn().FieldName)
|
|
||||||
if pkField.Int() != 0 {
|
|
||||||
val = pkField.Interface()
|
|
||||||
} else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
val = fieldValue.Interface()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case reflect.Array, reflect.Slice, reflect.Map:
|
|
||||||
if fieldValue == reflect.Zero(fieldType) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if fieldValue.IsNil() || !fieldValue.IsValid() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if col.SQLType.IsText() {
|
|
||||||
bytes, err := json.Marshal(fieldValue.Interface())
|
|
||||||
if err != nil {
|
|
||||||
engine.LogError(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val = string(bytes)
|
|
||||||
} else if col.SQLType.IsBlob() {
|
|
||||||
var bytes []byte
|
|
||||||
var err error
|
|
||||||
if (fieldType.Kind() == reflect.Array || fieldType.Kind() == reflect.Slice) &&
|
|
||||||
fieldType.Elem().Kind() == reflect.Uint8 {
|
|
||||||
if fieldValue.Len() > 0 {
|
|
||||||
val = fieldValue.Bytes()
|
|
||||||
} else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
bytes, err = json.Marshal(fieldValue.Interface())
|
|
||||||
if err != nil {
|
|
||||||
engine.LogError(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
val = bytes
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
val = fieldValue.Interface()
|
|
||||||
}
|
|
||||||
results[col.Name] = val
|
|
||||||
}
|
|
||||||
return results
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// Auto generating conditions according a struct
|
|
||||||
func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
||||||
includeVersion bool, includeUpdated bool, includeNil bool,
|
includeVersion bool, includeUpdated bool, includeNil bool,
|
||||||
includeAutoIncr bool, allUseBool bool, useAllCols bool,
|
includeAutoIncr bool, allUseBool bool, useAllCols bool,
|
||||||
mustColumnMap map[string]bool, columnMap map[string]bool, update bool) ([]string, []interface{}) {
|
mustColumnMap map[string]bool, nullableMap map[string]bool,
|
||||||
|
columnMap map[string]bool, update, unscoped bool) ([]string, []interface{}) {
|
||||||
|
|
||||||
colNames := make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
var args = make([]interface{}, 0)
|
var args = make([]interface{}, 0)
|
||||||
@ -303,17 +215,13 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||||||
if !includeAutoIncr && col.IsAutoIncrement {
|
if !includeAutoIncr && col.IsAutoIncrement {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if col.IsDeleted {
|
if col.IsDeleted && !unscoped {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if use, ok := columnMap[col.Name]; ok && !use {
|
if use, ok := columnMap[col.Name]; ok && !use {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldValuePtr, err := col.ValueOf(bean)
|
fieldValuePtr, err := col.ValueOf(bean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
engine.LogError(err)
|
engine.LogError(err)
|
||||||
@ -325,7 +233,9 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||||||
|
|
||||||
requiredField := useAllCols
|
requiredField := useAllCols
|
||||||
includeNil := useAllCols
|
includeNil := useAllCols
|
||||||
if b, ok := mustColumnMap[strings.ToLower(col.Name)]; ok {
|
lColName := strings.ToLower(col.Name)
|
||||||
|
|
||||||
|
if b, ok := mustColumnMap[lColName]; ok {
|
||||||
if b {
|
if b {
|
||||||
requiredField = true
|
requiredField = true
|
||||||
} else {
|
} else {
|
||||||
@ -333,6 +243,16 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// !evalphobia! set fieldValue as nil when column is nullable and zero-value
|
||||||
|
if b, ok := nullableMap[lColName]; ok {
|
||||||
|
if b && col.Nullable && isZero(fieldValue.Interface()) {
|
||||||
|
var nilValue *int
|
||||||
|
fieldValue = reflect.ValueOf(nilValue)
|
||||||
|
fieldType = reflect.TypeOf(fieldValue.Interface())
|
||||||
|
includeNil = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var val interface{}
|
var val interface{}
|
||||||
|
|
||||||
if fieldValue.CanAddr() {
|
if fieldValue.CanAddr() {
|
||||||
@ -410,19 +330,21 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||||||
t := int64(fieldValue.Uint())
|
t := int64(fieldValue.Uint())
|
||||||
val = reflect.ValueOf(&t).Interface()
|
val = reflect.ValueOf(&t).Interface()
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if fieldType == reflect.TypeOf(time.Now()) {
|
if fieldType.ConvertibleTo(core.TimeType) {
|
||||||
t := fieldValue.Interface().(time.Time)
|
t := fieldValue.Convert(core.TimeType).Interface().(time.Time)
|
||||||
if !requiredField && (t.IsZero() || !fieldValue.IsValid()) {
|
if !requiredField && (t.IsZero() || !fieldValue.IsValid()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
val = engine.FormatTime(col.SQLType.Name, t)
|
val = engine.FormatTime(col.SQLType.Name, t)
|
||||||
|
} else if nulType, ok := fieldValue.Interface().(driver.Valuer); ok {
|
||||||
|
val, _ = nulType.Value()
|
||||||
} else {
|
} else {
|
||||||
|
if !col.SQLType.IsJson() {
|
||||||
engine.autoMapType(fieldValue)
|
engine.autoMapType(fieldValue)
|
||||||
if table, ok := engine.Tables[fieldValue.Type()]; ok {
|
if table, ok := engine.Tables[fieldValue.Type()]; ok {
|
||||||
if len(table.PrimaryKeys) == 1 {
|
if len(table.PrimaryKeys) == 1 {
|
||||||
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
|
pkField := reflect.Indirect(fieldValue).FieldByName(table.PKColumns()[0].FieldName)
|
||||||
// fix non-int pk issues
|
// fix non-int pk issues
|
||||||
//if pkField.Int() != 0 {
|
|
||||||
if pkField.IsValid() && !isZero(pkField.Interface()) {
|
if pkField.IsValid() && !isZero(pkField.Interface()) {
|
||||||
val = pkField.Interface()
|
val = pkField.Interface()
|
||||||
} else {
|
} else {
|
||||||
@ -435,14 +357,27 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||||||
} else {
|
} else {
|
||||||
val = fieldValue.Interface()
|
val = fieldValue.Interface()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
bytes, err := json.Marshal(fieldValue.Interface())
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("mashal %v failed", fieldValue.Interface()))
|
||||||
|
}
|
||||||
|
if col.SQLType.IsText() {
|
||||||
|
val = string(bytes)
|
||||||
|
} else if col.SQLType.IsBlob() {
|
||||||
|
val = bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
case reflect.Array, reflect.Slice, reflect.Map:
|
case reflect.Array, reflect.Slice, reflect.Map:
|
||||||
|
if !requiredField {
|
||||||
if fieldValue == reflect.Zero(fieldType) {
|
if fieldValue == reflect.Zero(fieldType) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if fieldValue.IsNil() || !fieldValue.IsValid() || fieldValue.Len() == 0 {
|
if fieldValue.IsNil() || !fieldValue.IsValid() || fieldValue.Len() == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if col.SQLType.IsText() {
|
if col.SQLType.IsText() {
|
||||||
bytes, err := json.Marshal(fieldValue.Interface())
|
bytes, err := json.Marshal(fieldValue.Interface())
|
||||||
@ -492,8 +427,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
|||||||
func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
includeVersion bool, includeUpdated bool, includeNil bool,
|
includeVersion bool, includeUpdated bool, includeNil bool,
|
||||||
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
|
includeAutoIncr bool, allUseBool bool, useAllCols bool, unscoped bool,
|
||||||
mustColumnMap map[string]bool) ([]string, []interface{}) {
|
mustColumnMap map[string]bool, tableName, aliasName string, addedTableName bool) ([]string, []interface{}) {
|
||||||
|
|
||||||
colNames := make([]string, 0)
|
colNames := make([]string, 0)
|
||||||
var args = make([]interface{}, 0)
|
var args = make([]interface{}, 0)
|
||||||
for _, col := range table.Columns() {
|
for _, col := range table.Columns() {
|
||||||
@ -510,6 +444,21 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|||||||
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
|
if engine.dialect.DBType() == core.MSSQL && col.SQLType.Name == core.Text {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if col.SQLType.IsJson() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var colName string
|
||||||
|
if addedTableName {
|
||||||
|
var nm = tableName
|
||||||
|
if len(aliasName) > 0 {
|
||||||
|
nm = aliasName
|
||||||
|
}
|
||||||
|
colName = engine.Quote(nm) + "." + engine.Quote(col.Name)
|
||||||
|
} else {
|
||||||
|
colName = engine.Quote(col.Name)
|
||||||
|
}
|
||||||
|
|
||||||
fieldValuePtr, err := col.ValueOf(bean)
|
fieldValuePtr, err := col.ValueOf(bean)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
engine.LogError(err)
|
engine.LogError(err)
|
||||||
@ -517,7 +466,8 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|||||||
}
|
}
|
||||||
|
|
||||||
if col.IsDeleted && !unscoped { // tag "deleted" is enabled
|
if col.IsDeleted && !unscoped { // tag "deleted" is enabled
|
||||||
colNames = append(colNames, fmt.Sprintf("(%v IS NULL or %v = '0001-01-01 00:00:00')", engine.Quote(col.Name), engine.Quote(col.Name)))
|
colNames = append(colNames, fmt.Sprintf("%v IS NULL or %v = '0001-01-01 00:00:00'",
|
||||||
|
colName, colName))
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldValue := *fieldValuePtr
|
fieldValue := *fieldValuePtr
|
||||||
@ -539,7 +489,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|||||||
if fieldValue.IsNil() {
|
if fieldValue.IsNil() {
|
||||||
if includeNil {
|
if includeNil {
|
||||||
args = append(args, nil)
|
args = append(args, nil)
|
||||||
colNames = append(colNames, fmt.Sprintf("%v %s ?", engine.Quote(col.Name), engine.dialect.EqStr()))
|
colNames = append(colNames, fmt.Sprintf("%v %s ?", colName, engine.dialect.EqStr()))
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
} else if !fieldValue.IsValid() {
|
} else if !fieldValue.IsValid() {
|
||||||
@ -597,6 +547,30 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|||||||
val = engine.FormatTime(col.SQLType.Name, t)
|
val = engine.FormatTime(col.SQLType.Name, t)
|
||||||
} else if _, ok := reflect.New(fieldType).Interface().(core.Conversion); ok {
|
} else if _, ok := reflect.New(fieldType).Interface().(core.Conversion); ok {
|
||||||
continue
|
continue
|
||||||
|
} else if valNul, ok := fieldValue.Interface().(driver.Valuer); ok {
|
||||||
|
val, _ = valNul.Value()
|
||||||
|
if val == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if col.SQLType.IsJson() {
|
||||||
|
if col.SQLType.IsText() {
|
||||||
|
bytes, err := json.Marshal(fieldValue.Interface())
|
||||||
|
if err != nil {
|
||||||
|
engine.LogError(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val = string(bytes)
|
||||||
|
} else if col.SQLType.IsBlob() {
|
||||||
|
var bytes []byte
|
||||||
|
var err error
|
||||||
|
bytes, err = json.Marshal(fieldValue.Interface())
|
||||||
|
if err != nil {
|
||||||
|
engine.LogError(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
val = bytes
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
engine.autoMapType(fieldValue)
|
engine.autoMapType(fieldValue)
|
||||||
if table, ok := engine.Tables[fieldValue.Type()]; ok {
|
if table, ok := engine.Tables[fieldValue.Type()]; ok {
|
||||||
@ -611,12 +585,13 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//TODO: how to handler?
|
//TODO: how to handler?
|
||||||
panic("not supported")
|
panic(fmt.Sprintln("not supported", fieldValue.Interface(), "as", table.PrimaryKeys))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val = fieldValue.Interface()
|
val = fieldValue.Interface()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
case reflect.Array, reflect.Slice, reflect.Map:
|
case reflect.Array, reflect.Slice, reflect.Map:
|
||||||
if fieldValue == reflect.Zero(fieldType) {
|
if fieldValue == reflect.Zero(fieldType) {
|
||||||
continue
|
continue
|
||||||
@ -662,7 +637,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
|||||||
if col.IsPrimaryKey && engine.dialect.DBType() == "ql" {
|
if col.IsPrimaryKey && engine.dialect.DBType() == "ql" {
|
||||||
condi = "id() == ?"
|
condi = "id() == ?"
|
||||||
} else {
|
} else {
|
||||||
condi = fmt.Sprintf("%v %s ?", engine.Quote(col.Name), engine.dialect.EqStr())
|
condi = fmt.Sprintf("%v %s ?", colName, engine.dialect.EqStr())
|
||||||
}
|
}
|
||||||
colNames = append(colNames, condi)
|
colNames = append(colNames, condi)
|
||||||
}
|
}
|
||||||
@ -709,7 +684,7 @@ func (statement *Statement) Id(id interface{}) *Statement {
|
|||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate "Update ... Set column = column + arg" statment
|
// Incr Generate "Update ... Set column = column + arg" statment
|
||||||
func (statement *Statement) Incr(column string, arg ...interface{}) *Statement {
|
func (statement *Statement) Incr(column string, arg ...interface{}) *Statement {
|
||||||
k := strings.ToLower(column)
|
k := strings.ToLower(column)
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
@ -720,7 +695,7 @@ func (statement *Statement) Incr(column string, arg ...interface{}) *Statement {
|
|||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate "Update ... Set column = column - arg" statment
|
// Decr Generate "Update ... Set column = column - arg" statment
|
||||||
func (statement *Statement) Decr(column string, arg ...interface{}) *Statement {
|
func (statement *Statement) Decr(column string, arg ...interface{}) *Statement {
|
||||||
k := strings.ToLower(column)
|
k := strings.ToLower(column)
|
||||||
if len(arg) > 0 {
|
if len(arg) > 0 {
|
||||||
@ -731,7 +706,7 @@ func (statement *Statement) Decr(column string, arg ...interface{}) *Statement {
|
|||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate "Update ... Set column = {expression}" statment
|
// SetExpr Generate "Update ... Set column = {expression}" statment
|
||||||
func (statement *Statement) SetExpr(column string, expression string) *Statement {
|
func (statement *Statement) SetExpr(column string, expression string) *Statement {
|
||||||
k := strings.ToLower(column)
|
k := strings.ToLower(column)
|
||||||
statement.exprColumns[k] = exprParam{column, expression}
|
statement.exprColumns[k] = exprParam{column, expression}
|
||||||
@ -755,9 +730,14 @@ func (statement *Statement) getExpr() map[string]exprParam {
|
|||||||
|
|
||||||
// Generate "Where column IN (?) " statment
|
// Generate "Where column IN (?) " statment
|
||||||
func (statement *Statement) In(column string, args ...interface{}) *Statement {
|
func (statement *Statement) In(column string, args ...interface{}) *Statement {
|
||||||
|
length := len(args)
|
||||||
|
if length == 0 {
|
||||||
|
return statement
|
||||||
|
}
|
||||||
|
|
||||||
k := strings.ToLower(column)
|
k := strings.ToLower(column)
|
||||||
var newargs []interface{}
|
var newargs []interface{}
|
||||||
if len(args) == 1 &&
|
if length == 1 &&
|
||||||
reflect.TypeOf(args[0]).Kind() == reflect.Slice {
|
reflect.TypeOf(args[0]).Kind() == reflect.Slice {
|
||||||
newargs = make([]interface{}, 0)
|
newargs = make([]interface{}, 0)
|
||||||
v := reflect.ValueOf(args[0])
|
v := reflect.ValueOf(args[0])
|
||||||
@ -781,12 +761,17 @@ func (statement *Statement) genInSql() (string, []interface{}) {
|
|||||||
return "", []interface{}{}
|
return "", []interface{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
inStrs := make([]string, 0, len(statement.inColumns))
|
inStrs := make([]string, len(statement.inColumns), len(statement.inColumns))
|
||||||
args := make([]interface{}, 0)
|
args := make([]interface{}, 0)
|
||||||
|
var buf bytes.Buffer
|
||||||
|
var i int
|
||||||
for _, params := range statement.inColumns {
|
for _, params := range statement.inColumns {
|
||||||
inStrs = append(inStrs, fmt.Sprintf("(%v IN (%v))",
|
buf.Reset()
|
||||||
|
fmt.Fprintf(&buf, "(%v IN (%v))",
|
||||||
statement.Engine.autoQuote(params.colName),
|
statement.Engine.autoQuote(params.colName),
|
||||||
strings.Join(makeArray("?", len(params.args)), ",")))
|
strings.Join(makeArray("?", len(params.args)), ","))
|
||||||
|
inStrs[i] = buf.String()
|
||||||
|
i++
|
||||||
args = append(args, params.args...)
|
args = append(args, params.args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -799,7 +784,7 @@ func (statement *Statement) genInSql() (string, []interface{}) {
|
|||||||
func (statement *Statement) attachInSql() {
|
func (statement *Statement) attachInSql() {
|
||||||
inSql, inArgs := statement.genInSql()
|
inSql, inArgs := statement.genInSql()
|
||||||
if len(inSql) > 0 {
|
if len(inSql) > 0 {
|
||||||
if statement.ConditionStr != "" {
|
if len(statement.ConditionStr) > 0 {
|
||||||
statement.ConditionStr += " " + statement.Engine.dialect.AndStr() + " "
|
statement.ConditionStr += " " + statement.Engine.dialect.AndStr() + " "
|
||||||
}
|
}
|
||||||
statement.ConditionStr += inSql
|
statement.ConditionStr += inSql
|
||||||
@ -858,6 +843,18 @@ func (statement *Statement) Distinct(columns ...string) *Statement {
|
|||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate "SELECT ... FOR UPDATE" statment
|
||||||
|
func (statement *Statement) ForUpdate() *Statement {
|
||||||
|
statement.IsForUpdate = true
|
||||||
|
return statement
|
||||||
|
}
|
||||||
|
|
||||||
|
// replace select
|
||||||
|
func (s *Statement) Select(str string) *Statement {
|
||||||
|
s.selectStr = str
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
// Generate "col1, col2" statement
|
// Generate "col1, col2" statement
|
||||||
func (statement *Statement) Cols(columns ...string) *Statement {
|
func (statement *Statement) Cols(columns ...string) *Statement {
|
||||||
newColumns := col2NewCols(columns...)
|
newColumns := col2NewCols(columns...)
|
||||||
@ -868,6 +865,7 @@ func (statement *Statement) Cols(columns ...string) *Statement {
|
|||||||
if strings.Contains(statement.ColumnStr, ".") {
|
if strings.Contains(statement.ColumnStr, ".") {
|
||||||
statement.ColumnStr = strings.Replace(statement.ColumnStr, ".", statement.Engine.Quote("."), -1)
|
statement.ColumnStr = strings.Replace(statement.ColumnStr, ".", statement.Engine.Quote("."), -1)
|
||||||
}
|
}
|
||||||
|
statement.ColumnStr = strings.Replace(statement.ColumnStr, statement.Engine.Quote("*"), "*", -1)
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -886,15 +884,6 @@ func (statement *Statement) MustCols(columns ...string) *Statement {
|
|||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update use only: not update columns
|
|
||||||
/*func (statement *Statement) NotCols(columns ...string) *Statement {
|
|
||||||
newColumns := col2NewCols(columns...)
|
|
||||||
for _, nc := range newColumns {
|
|
||||||
statement.mustColumnMap[strings.ToLower(nc)] = false
|
|
||||||
}
|
|
||||||
return statement
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// indicates that use bool fields as update contents and query contiditions
|
// indicates that use bool fields as update contents and query contiditions
|
||||||
func (statement *Statement) UseBool(columns ...string) *Statement {
|
func (statement *Statement) UseBool(columns ...string) *Statement {
|
||||||
if len(columns) > 0 {
|
if len(columns) > 0 {
|
||||||
@ -914,6 +903,14 @@ func (statement *Statement) Omit(columns ...string) {
|
|||||||
statement.OmitStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", ")))
|
statement.OmitStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", ")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update use only: update columns to null when value is nullable and zero-value
|
||||||
|
func (statement *Statement) Nullable(columns ...string) {
|
||||||
|
newColumns := col2NewCols(columns...)
|
||||||
|
for _, nc := range newColumns {
|
||||||
|
statement.nullableMap[strings.ToLower(nc)] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate LIMIT limit statement
|
// Generate LIMIT limit statement
|
||||||
func (statement *Statement) Top(limit int) *Statement {
|
func (statement *Statement) Top(limit int) *Statement {
|
||||||
statement.Limit(limit)
|
statement.Limit(limit)
|
||||||
@ -931,7 +928,7 @@ func (statement *Statement) Limit(limit int, start ...int) *Statement {
|
|||||||
|
|
||||||
// Generate "Order By order" statement
|
// Generate "Order By order" statement
|
||||||
func (statement *Statement) OrderBy(order string) *Statement {
|
func (statement *Statement) OrderBy(order string) *Statement {
|
||||||
if statement.OrderStr != "" {
|
if len(statement.OrderStr) > 0 {
|
||||||
statement.OrderStr += ", "
|
statement.OrderStr += ", "
|
||||||
}
|
}
|
||||||
statement.OrderStr += order
|
statement.OrderStr += order
|
||||||
@ -939,44 +936,51 @@ func (statement *Statement) OrderBy(order string) *Statement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) Desc(colNames ...string) *Statement {
|
func (statement *Statement) Desc(colNames ...string) *Statement {
|
||||||
if statement.OrderStr != "" {
|
var buf bytes.Buffer
|
||||||
statement.OrderStr += ", "
|
fmt.Fprintf(&buf, statement.OrderStr)
|
||||||
|
if len(statement.OrderStr) > 0 {
|
||||||
|
fmt.Fprint(&buf, ", ")
|
||||||
}
|
}
|
||||||
newColNames := statement.col2NewColsWithQuote(colNames...)
|
newColNames := statement.col2NewColsWithQuote(colNames...)
|
||||||
sqlStr := strings.Join(newColNames, " DESC, ")
|
fmt.Fprintf(&buf, "%v DESC", strings.Join(newColNames, " DESC, "))
|
||||||
statement.OrderStr += sqlStr + " DESC"
|
statement.OrderStr = buf.String()
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method Asc provide asc order by query condition, the input parameters are columns.
|
// Method Asc provide asc order by query condition, the input parameters are columns.
|
||||||
func (statement *Statement) Asc(colNames ...string) *Statement {
|
func (statement *Statement) Asc(colNames ...string) *Statement {
|
||||||
if statement.OrderStr != "" {
|
var buf bytes.Buffer
|
||||||
statement.OrderStr += ", "
|
fmt.Fprintf(&buf, statement.OrderStr)
|
||||||
|
if len(statement.OrderStr) > 0 {
|
||||||
|
fmt.Fprint(&buf, ", ")
|
||||||
}
|
}
|
||||||
newColNames := statement.col2NewColsWithQuote(colNames...)
|
newColNames := statement.col2NewColsWithQuote(colNames...)
|
||||||
sqlStr := strings.Join(newColNames, " ASC, ")
|
fmt.Fprintf(&buf, "%v ASC", strings.Join(newColNames, " ASC, "))
|
||||||
statement.OrderStr += sqlStr + " ASC"
|
statement.OrderStr = buf.String()
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||||
func (statement *Statement) Join(join_operator string, tablename interface{}, condition string) *Statement {
|
func (statement *Statement) Join(join_operator string, tablename interface{}, condition string) *Statement {
|
||||||
var joinTable string
|
var buf bytes.Buffer
|
||||||
|
if len(statement.JoinStr) > 0 {
|
||||||
|
fmt.Fprintf(&buf, "%v %v JOIN ", statement.JoinStr, join_operator)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(&buf, "%v JOIN ", join_operator)
|
||||||
|
}
|
||||||
|
|
||||||
switch tablename.(type) {
|
switch tablename.(type) {
|
||||||
case []string:
|
case []string:
|
||||||
t := tablename.([]string)
|
t := tablename.([]string)
|
||||||
l := len(t)
|
if len(t) > 1 {
|
||||||
if l > 1 {
|
fmt.Fprintf(&buf, "%v AS %v", statement.Engine.Quote(t[0]), statement.Engine.Quote(t[1]))
|
||||||
table := t[0]
|
} else if len(t) == 1 {
|
||||||
joinTable = statement.Engine.Quote(table) + " AS " + statement.Engine.Quote(t[1])
|
fmt.Fprintf(&buf, statement.Engine.Quote(t[0]))
|
||||||
} else if l == 1 {
|
|
||||||
table := t[0]
|
|
||||||
joinTable = statement.Engine.Quote(table)
|
|
||||||
}
|
}
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
t := tablename.([]interface{})
|
t := tablename.([]interface{})
|
||||||
l := len(t)
|
l := len(t)
|
||||||
table := ""
|
var table string
|
||||||
if l > 0 {
|
if l > 0 {
|
||||||
f := t[0]
|
f := t[0]
|
||||||
v := rValue(f)
|
v := rValue(f)
|
||||||
@ -989,21 +993,17 @@ func (statement *Statement) Join(join_operator string, tablename interface{}, co
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if l > 1 {
|
if l > 1 {
|
||||||
joinTable = statement.Engine.Quote(table) + " AS " + statement.Engine.Quote(fmt.Sprintf("%v", t[1]))
|
fmt.Fprintf(&buf, "%v AS %v", statement.Engine.Quote(table),
|
||||||
|
statement.Engine.Quote(fmt.Sprintf("%v", t[1])))
|
||||||
} else if l == 1 {
|
} else if l == 1 {
|
||||||
joinTable = statement.Engine.Quote(table)
|
fmt.Fprintf(&buf, statement.Engine.Quote(table))
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
t := fmt.Sprintf("%v", tablename)
|
fmt.Fprintf(&buf, statement.Engine.Quote(fmt.Sprintf("%v", tablename)))
|
||||||
joinTable = statement.Engine.Quote(t)
|
|
||||||
}
|
|
||||||
if statement.JoinStr != "" {
|
|
||||||
statement.JoinStr = statement.JoinStr + fmt.Sprintf(" %v JOIN %v ON %v", join_operator,
|
|
||||||
joinTable, condition)
|
|
||||||
} else {
|
|
||||||
statement.JoinStr = fmt.Sprintf("%v JOIN %v ON %v", join_operator,
|
|
||||||
joinTable, condition)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Fprintf(&buf, " ON %v", condition)
|
||||||
|
statement.JoinStr = buf.String()
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1120,11 +1120,6 @@ func (s *Statement) genDelIndexSQL() []string {
|
|||||||
return sqls
|
return sqls
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
func (s *Statement) genDropSQL() string {
|
|
||||||
return s.Engine.dialect.MustDropTa(s.TableName()) + ";"
|
|
||||||
}*/
|
|
||||||
|
|
||||||
func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) {
|
func (statement *Statement) genGetSql(bean interface{}) (string, []interface{}) {
|
||||||
var table *core.Table
|
var table *core.Table
|
||||||
if statement.RefTable == nil {
|
if statement.RefTable == nil {
|
||||||
@ -1134,14 +1129,19 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
|
|||||||
table = statement.RefTable
|
table = statement.RefTable
|
||||||
}
|
}
|
||||||
|
|
||||||
colNames, args := buildConditions(statement.Engine, table, bean, true, true,
|
var addedTableName = (len(statement.JoinStr) > 0)
|
||||||
false, true, statement.allUseBool, statement.useAllCols,
|
|
||||||
statement.unscoped, statement.mustColumnMap)
|
if !statement.noAutoCondition {
|
||||||
|
colNames, args := statement.buildConditions(table, bean, true, true, false, true, addedTableName)
|
||||||
|
|
||||||
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ")
|
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.dialect.AndStr()+" ")
|
||||||
statement.BeanArgs = args
|
statement.BeanArgs = args
|
||||||
|
}
|
||||||
|
|
||||||
var columnStr string = statement.ColumnStr
|
var columnStr string = statement.ColumnStr
|
||||||
|
if len(statement.selectStr) > 0 {
|
||||||
|
columnStr = statement.selectStr
|
||||||
|
} else {
|
||||||
if len(statement.JoinStr) == 0 {
|
if len(statement.JoinStr) == 0 {
|
||||||
if len(columnStr) == 0 {
|
if len(columnStr) == 0 {
|
||||||
if statement.GroupByStr != "" {
|
if statement.GroupByStr != "" {
|
||||||
@ -1159,6 +1159,7 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
statement.attachInSql() // !admpub! fix bug:Iterate func missing "... IN (...)"
|
statement.attachInSql() // !admpub! fix bug:Iterate func missing "... IN (...)"
|
||||||
return statement.genSelectSql(columnStr), append(statement.Params, statement.BeanArgs...)
|
return statement.genSelectSql(columnStr), append(statement.Params, statement.BeanArgs...)
|
||||||
@ -1185,16 +1186,23 @@ func (s *Statement) genAddUniqueStr(uqeName string, cols []string) (string, []in
|
|||||||
return sql, []interface{}{}
|
return sql, []interface{}{}
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
func (statement *Statement) buildConditions(table *core.Table, bean interface{}, includeVersion bool, includeUpdated bool, includeNil bool, includeAutoIncr bool, addedTableName bool) ([]string, []interface{}) {
|
||||||
|
return buildConditions(statement.Engine, table, bean, includeVersion, includeUpdated, includeNil, includeAutoIncr, statement.allUseBool, statement.useAllCols,
|
||||||
|
statement.unscoped, statement.mustColumnMap, statement.TableName(), statement.TableAlias, addedTableName)
|
||||||
|
}
|
||||||
|
|
||||||
func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}) {
|
func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}) {
|
||||||
table := statement.Engine.TableInfo(bean)
|
table := statement.Engine.TableInfo(bean)
|
||||||
statement.RefTable = table
|
statement.RefTable = table
|
||||||
|
|
||||||
colNames, args := buildConditions(statement.Engine, table, bean, true, true, false,
|
var addedTableName = (len(statement.JoinStr) > 0)
|
||||||
true, statement.allUseBool, statement.useAllCols,
|
|
||||||
statement.unscoped, statement.mustColumnMap)
|
if !statement.noAutoCondition {
|
||||||
|
colNames, args := statement.buildConditions(table, bean, true, true, false, true, addedTableName)
|
||||||
|
|
||||||
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
|
statement.ConditionStr = strings.Join(colNames, " "+statement.Engine.Dialect().AndStr()+" ")
|
||||||
statement.BeanArgs = args
|
statement.BeanArgs = args
|
||||||
|
}
|
||||||
|
|
||||||
// count(index fieldname) > count(0) > count(*)
|
// count(index fieldname) > count(0) > count(*)
|
||||||
var id string = "*"
|
var id string = "*"
|
||||||
@ -1206,47 +1214,46 @@ func (statement *Statement) genCountSql(bean interface{}) (string, []interface{}
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
||||||
/*if statement.GroupByStr != "" {
|
|
||||||
if columnStr == "" {
|
|
||||||
columnStr = statement.Engine.Quote(strings.Replace(statement.GroupByStr, ",", statement.Engine.Quote(","), -1))
|
|
||||||
}
|
|
||||||
//statement.GroupByStr = columnStr
|
|
||||||
}*/
|
|
||||||
var distinct string
|
var distinct string
|
||||||
if statement.IsDistinct {
|
if statement.IsDistinct {
|
||||||
distinct = "DISTINCT "
|
distinct = "DISTINCT "
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var dialect = statement.Engine.Dialect()
|
||||||
|
var quote = statement.Engine.Quote
|
||||||
var top string
|
var top string
|
||||||
var mssqlCondi string
|
var mssqlCondi string
|
||||||
/*var orderBy string
|
|
||||||
if statement.OrderStr != "" {
|
|
||||||
orderBy = fmt.Sprintf(" ORDER BY %v", statement.OrderStr)
|
|
||||||
}*/
|
|
||||||
statement.processIdParam()
|
statement.processIdParam()
|
||||||
var whereStr string
|
|
||||||
if statement.WhereStr != "" {
|
var buf bytes.Buffer
|
||||||
whereStr = fmt.Sprintf(" WHERE %v", statement.WhereStr)
|
if len(statement.WhereStr) > 0 {
|
||||||
if statement.ConditionStr != "" {
|
if len(statement.ConditionStr) > 0 {
|
||||||
whereStr = fmt.Sprintf("%v %s %v", whereStr, statement.Engine.Dialect().AndStr(),
|
fmt.Fprintf(&buf, " WHERE (%v)", statement.WhereStr)
|
||||||
statement.ConditionStr)
|
|
||||||
}
|
|
||||||
} else if statement.ConditionStr != "" {
|
|
||||||
whereStr = fmt.Sprintf(" WHERE %v", statement.ConditionStr)
|
|
||||||
}
|
|
||||||
var fromStr string = " FROM " + statement.Engine.Quote(statement.TableName())
|
|
||||||
if statement.TableAlias != "" {
|
|
||||||
if statement.Engine.dialect.DBType() == core.ORACLE {
|
|
||||||
fromStr += " " + statement.Engine.Quote(statement.TableAlias)
|
|
||||||
} else {
|
} else {
|
||||||
fromStr += " AS " + statement.Engine.Quote(statement.TableAlias)
|
fmt.Fprintf(&buf, " WHERE %v", statement.WhereStr)
|
||||||
|
}
|
||||||
|
if statement.ConditionStr != "" {
|
||||||
|
fmt.Fprintf(&buf, " %s (%v)", dialect.AndStr(), statement.ConditionStr)
|
||||||
|
}
|
||||||
|
} else if len(statement.ConditionStr) > 0 {
|
||||||
|
fmt.Fprintf(&buf, " WHERE %v", statement.ConditionStr)
|
||||||
|
}
|
||||||
|
var whereStr = buf.String()
|
||||||
|
|
||||||
|
var fromStr string = " FROM " + quote(statement.TableName())
|
||||||
|
if statement.TableAlias != "" {
|
||||||
|
if dialect.DBType() == core.ORACLE {
|
||||||
|
fromStr += " " + quote(statement.TableAlias)
|
||||||
|
} else {
|
||||||
|
fromStr += " AS " + quote(statement.TableAlias)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if statement.JoinStr != "" {
|
if statement.JoinStr != "" {
|
||||||
fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr)
|
fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.Engine.dialect.DBType() == core.MSSQL {
|
if dialect.DBType() == core.MSSQL {
|
||||||
if statement.LimitN > 0 {
|
if statement.LimitN > 0 {
|
||||||
top = fmt.Sprintf(" TOP %d ", statement.LimitN)
|
top = fmt.Sprintf(" TOP %d ", statement.LimitN)
|
||||||
}
|
}
|
||||||
@ -1277,10 +1284,9 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// !nashtsai! REVIEW Sprintf is considered slowest mean of string concatnation, better to work with builder pattern
|
// !nashtsai! REVIEW Sprintf is considered slowest mean of string concatnation, better to work with builder pattern
|
||||||
a = fmt.Sprintf("SELECT %v%v%v%v%v", top, distinct, columnStr,
|
a = fmt.Sprintf("SELECT %v%v%v%v%v", top, distinct, columnStr, fromStr, whereStr)
|
||||||
fromStr, whereStr)
|
if len(mssqlCondi) > 0 {
|
||||||
if mssqlCondi != "" {
|
if len(whereStr) > 0 {
|
||||||
if whereStr != "" {
|
|
||||||
a += " AND " + mssqlCondi
|
a += " AND " + mssqlCondi
|
||||||
} else {
|
} else {
|
||||||
a += " WHERE " + mssqlCondi
|
a += " WHERE " + mssqlCondi
|
||||||
@ -1296,17 +1302,20 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
|||||||
if statement.OrderStr != "" {
|
if statement.OrderStr != "" {
|
||||||
a = fmt.Sprintf("%v ORDER BY %v", a, statement.OrderStr)
|
a = fmt.Sprintf("%v ORDER BY %v", a, statement.OrderStr)
|
||||||
}
|
}
|
||||||
if statement.Engine.dialect.DBType() != core.MSSQL && statement.Engine.dialect.DBType() != core.ORACLE {
|
if dialect.DBType() != core.MSSQL && dialect.DBType() != core.ORACLE {
|
||||||
if statement.Start > 0 {
|
if statement.Start > 0 {
|
||||||
a = fmt.Sprintf("%v LIMIT %v OFFSET %v", a, statement.LimitN, statement.Start)
|
a = fmt.Sprintf("%v LIMIT %v OFFSET %v", a, statement.LimitN, statement.Start)
|
||||||
} else if statement.LimitN > 0 {
|
} else if statement.LimitN > 0 {
|
||||||
a = fmt.Sprintf("%v LIMIT %v", a, statement.LimitN)
|
a = fmt.Sprintf("%v LIMIT %v", a, statement.LimitN)
|
||||||
}
|
}
|
||||||
} else if statement.Engine.dialect.DBType() == core.ORACLE {
|
} else if dialect.DBType() == core.ORACLE {
|
||||||
if statement.Start != 0 || statement.LimitN != 0 {
|
if statement.Start != 0 || statement.LimitN != 0 {
|
||||||
a = fmt.Sprintf("SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", columnStr, columnStr, a, statement.Start+statement.LimitN, statement.Start)
|
a = fmt.Sprintf("SELECT %v FROM (SELECT %v,ROWNUM RN FROM (%v) at WHERE ROWNUM <= %d) aat WHERE RN > %d", columnStr, columnStr, a, statement.Start+statement.LimitN, statement.Start)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if statement.IsForUpdate {
|
||||||
|
a = dialect.ForUpdateSql(a)
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
4
Godeps/_workspace/src/github.com/go-xorm/xorm/syslogger.go
generated
vendored
4
Godeps/_workspace/src/github.com/go-xorm/xorm/syslogger.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
// +build !windows,!nacl,!plan9
|
// +build !windows,!nacl,!plan9
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
8
Godeps/_workspace/src/github.com/go-xorm/xorm/xorm.go
generated
vendored
8
Godeps/_workspace/src/github.com/go-xorm/xorm/xorm.go
generated
vendored
@ -1,3 +1,7 @@
|
|||||||
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -13,7 +17,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version string = "0.4.2.0225"
|
Version string = "0.4.5.0204"
|
||||||
)
|
)
|
||||||
|
|
||||||
func regDrvsNDialects() bool {
|
func regDrvsNDialects() bool {
|
||||||
@ -35,7 +39,7 @@ func regDrvsNDialects() bool {
|
|||||||
for driverName, v := range providedDrvsNDialects {
|
for driverName, v := range providedDrvsNDialects {
|
||||||
if driver := core.QueryDriver(driverName); driver == nil {
|
if driver := core.QueryDriver(driverName); driver == nil {
|
||||||
core.RegisterDriver(driverName, v.getDriver())
|
core.RegisterDriver(driverName, v.getDriver())
|
||||||
core.RegisterDialect(v.dbType, v.getDialect())
|
core.RegisterDialect(v.dbType, v.getDialect)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
10
README.md
10
README.md
@ -80,13 +80,13 @@ the latest master builds [here](http://grafana.org/download/builds)
|
|||||||
|
|
||||||
### Get Code
|
### Get Code
|
||||||
|
|
||||||
```
|
```bash
|
||||||
go get github.com/grafana/grafana
|
go get github.com/grafana/grafana
|
||||||
```
|
```
|
||||||
|
|
||||||
### Building the backend
|
### Building the backend
|
||||||
Replace X.Y.Z by actual version number.
|
Replace X.Y.Z by actual version number.
|
||||||
```
|
```bash
|
||||||
cd $GOPATH/src/github.com/grafana/grafana
|
cd $GOPATH/src/github.com/grafana/grafana
|
||||||
go run build.go setup (only needed once to install godep)
|
go run build.go setup (only needed once to install godep)
|
||||||
godep restore (will pull down all golang lib dependencies in your current GOPATH)
|
godep restore (will pull down all golang lib dependencies in your current GOPATH)
|
||||||
@ -98,7 +98,7 @@ go run build.go build
|
|||||||
To build less to css for the frontend you will need a recent version of of node (v0.12.0),
|
To build less to css for the frontend you will need a recent version of of node (v0.12.0),
|
||||||
npm (v2.5.0) and grunt (v0.4.5). Run the following:
|
npm (v2.5.0) and grunt (v0.4.5). Run the following:
|
||||||
|
|
||||||
```
|
```bash
|
||||||
npm install
|
npm install
|
||||||
npm install -g grunt-cli
|
npm install -g grunt-cli
|
||||||
grunt
|
grunt
|
||||||
@ -106,13 +106,13 @@ grunt
|
|||||||
|
|
||||||
### Recompile backend on source change
|
### Recompile backend on source change
|
||||||
To rebuild on source change (requires that you executed godep restore)
|
To rebuild on source change (requires that you executed godep restore)
|
||||||
```
|
```bash
|
||||||
go get github.com/Unknwon/bra
|
go get github.com/Unknwon/bra
|
||||||
bra run
|
bra run
|
||||||
```
|
```
|
||||||
|
|
||||||
### Running
|
### Running
|
||||||
```
|
```bash
|
||||||
./bin/grafana-server
|
./bin/grafana-server
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -14,10 +14,10 @@
|
|||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"jquery": "~2.1.4",
|
"jquery": "~2.1.4",
|
||||||
"angular": "~1.5.0",
|
"angular": "~1.5.1",
|
||||||
"angular-route": "~1.5.0",
|
"angular-route": "~1.5.1",
|
||||||
"angular-mocks": "~1.5.0",
|
"angular-mocks": "~1.5.1",
|
||||||
"angular-sanitize": "~1.5.0",
|
"angular-sanitize": "~1.5.1",
|
||||||
"angular-bindonce": "~0.3.3"
|
"angular-bindonce": "~0.3.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,7 @@ Name | Description
|
|||||||
`label_values(label)` | Returns a list of label values for the `label` in every metric.
|
`label_values(label)` | Returns a list of label values for the `label` in every metric.
|
||||||
`label_values(metric, label)` | Returns a list of label values for the `label` in the specified metric.
|
`label_values(metric, label)` | Returns a list of label values for the `label` in the specified metric.
|
||||||
`metrics(metric)` | Returns a list of metrics matching the specified `metric` regex.
|
`metrics(metric)` | Returns a list of metrics matching the specified `metric` regex.
|
||||||
|
`query_result(query)` | Returns a list of Prometheus query result for the `query`.
|
||||||
|
|
||||||
For details of `metric names` & `label names`, and `label values`, please refer to the [Prometheus documentation](http://prometheus.io/docs/concepts/data_model/#metric-names-and-labels).
|
For details of `metric names` & `label names`, and `label values`, please refer to the [Prometheus documentation](http://prometheus.io/docs/concepts/data_model/#metric-names-and-labels).
|
||||||
|
|
||||||
|
7
examples/nginx-app/.gitignore
vendored
Normal file
7
examples/nginx-app/.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
.DS_Store
|
||||||
|
|
||||||
|
node_modules
|
||||||
|
tmp/*
|
||||||
|
npm-debug.log
|
||||||
|
dist/*
|
||||||
|
|
13
examples/nginx-app/.jscs.json
Normal file
13
examples/nginx-app/.jscs.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"disallowImplicitTypeConversion": ["string"],
|
||||||
|
"disallowKeywords": ["with"],
|
||||||
|
"disallowMultipleLineBreaks": true,
|
||||||
|
"disallowMixedSpacesAndTabs": true,
|
||||||
|
"disallowTrailingWhitespace": true,
|
||||||
|
"requireSpacesInFunctionExpression": {
|
||||||
|
"beforeOpeningCurlyBrace": true
|
||||||
|
},
|
||||||
|
"disallowSpacesInsideArrayBrackets": true,
|
||||||
|
"disallowSpacesInsideParentheses": true,
|
||||||
|
"validateIndentation": 2
|
||||||
|
}
|
36
examples/nginx-app/.jshintrc
Normal file
36
examples/nginx-app/.jshintrc
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"browser": true,
|
||||||
|
"esnext": true,
|
||||||
|
|
||||||
|
"bitwise":false,
|
||||||
|
"curly": true,
|
||||||
|
"eqnull": true,
|
||||||
|
"devel": true,
|
||||||
|
"eqeqeq": true,
|
||||||
|
"forin": false,
|
||||||
|
"immed": true,
|
||||||
|
"supernew": true,
|
||||||
|
"expr": true,
|
||||||
|
"indent": 2,
|
||||||
|
"latedef": true,
|
||||||
|
"newcap": true,
|
||||||
|
"noarg": true,
|
||||||
|
"noempty": true,
|
||||||
|
"undef": true,
|
||||||
|
"boss": true,
|
||||||
|
"trailing": true,
|
||||||
|
"laxbreak": true,
|
||||||
|
"laxcomma": true,
|
||||||
|
"sub": true,
|
||||||
|
"unused": true,
|
||||||
|
"maxdepth": 6,
|
||||||
|
"maxlen": 140,
|
||||||
|
|
||||||
|
"globals": {
|
||||||
|
"System": true,
|
||||||
|
"define": true,
|
||||||
|
"require": true,
|
||||||
|
"Chromath": false,
|
||||||
|
"setImmediate": true
|
||||||
|
}
|
||||||
|
}
|
54
examples/nginx-app/Gruntfile.js
Normal file
54
examples/nginx-app/Gruntfile.js
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
module.exports = function(grunt) {
|
||||||
|
|
||||||
|
require('load-grunt-tasks')(grunt);
|
||||||
|
|
||||||
|
grunt.loadNpmTasks('grunt-execute');
|
||||||
|
grunt.loadNpmTasks('grunt-contrib-clean');
|
||||||
|
|
||||||
|
grunt.initConfig({
|
||||||
|
|
||||||
|
clean: ["dist"],
|
||||||
|
|
||||||
|
copy: {
|
||||||
|
src_to_dist: {
|
||||||
|
cwd: 'src',
|
||||||
|
expand: true,
|
||||||
|
src: ['**/*', '!**/*.js', '!**/*.scss'],
|
||||||
|
dest: 'dist'
|
||||||
|
},
|
||||||
|
pluginDef: {
|
||||||
|
expand: true,
|
||||||
|
src: 'plugin.json',
|
||||||
|
dest: 'dist',
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
watch: {
|
||||||
|
rebuild_all: {
|
||||||
|
files: ['src/**/*', 'plugin.json'],
|
||||||
|
tasks: ['default'],
|
||||||
|
options: {spawn: false}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
babel: {
|
||||||
|
options: {
|
||||||
|
sourceMap: true,
|
||||||
|
presets: ["es2015"],
|
||||||
|
plugins: ['transform-es2015-modules-systemjs', "transform-es2015-for-of"],
|
||||||
|
},
|
||||||
|
dist: {
|
||||||
|
files: [{
|
||||||
|
cwd: 'src',
|
||||||
|
expand: true,
|
||||||
|
src: ['**/*.js'],
|
||||||
|
dest: 'dist',
|
||||||
|
ext:'.js'
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
grunt.registerTask('default', ['clean', 'copy:src_to_dist', 'copy:pluginDef', 'babel']);
|
||||||
|
};
|
37
examples/nginx-app/package.json
Normal file
37
examples/nginx-app/package.json
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
{
|
||||||
|
"name": "kentik-app",
|
||||||
|
"private": true,
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/raintank/kentik-app-poc.git"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/raintank/kentik-app-poc/issues"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"grunt": "~0.4.5",
|
||||||
|
"babel": "~6.5.1",
|
||||||
|
"grunt-babel": "~6.0.0",
|
||||||
|
"grunt-contrib-copy": "~0.8.2",
|
||||||
|
"grunt-contrib-watch": "^0.6.1",
|
||||||
|
"grunt-contrib-uglify": "~0.11.0",
|
||||||
|
"grunt-systemjs-builder": "^0.2.5",
|
||||||
|
"load-grunt-tasks": "~3.2.0",
|
||||||
|
"grunt-execute": "~0.2.2",
|
||||||
|
"grunt-contrib-clean": "~0.6.0"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"babel-plugin-transform-es2015-modules-systemjs": "^6.5.0",
|
||||||
|
"babel-preset-es2015": "^6.5.0",
|
||||||
|
"lodash": "~4.0.0"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/raintank/kentik-app-poc#readme"
|
||||||
|
}
|
44
examples/nginx-app/plugin.json
Normal file
44
examples/nginx-app/plugin.json
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
{
|
||||||
|
"type": "app",
|
||||||
|
"name": "Nginx",
|
||||||
|
"id": "nginx-app",
|
||||||
|
|
||||||
|
"staticRoot": ".",
|
||||||
|
|
||||||
|
"pages": [
|
||||||
|
{ "name": "Live stream", "component": "StreamPageCtrl", "role": "Editor"},
|
||||||
|
{ "name": "Log view", "component": "LogsPageCtrl", "role": "Viewer"}
|
||||||
|
],
|
||||||
|
|
||||||
|
"css": {
|
||||||
|
"dark": "css/dark.css",
|
||||||
|
"light": "css/light.css"
|
||||||
|
},
|
||||||
|
|
||||||
|
"info": {
|
||||||
|
"description": "Official Grafana Nginx App & Dashboard bundle",
|
||||||
|
"author": {
|
||||||
|
"name": "Nginx Inc.",
|
||||||
|
"url": "http://nginx.com"
|
||||||
|
},
|
||||||
|
"keywords": ["nginx"],
|
||||||
|
"logos": {
|
||||||
|
"small": "img/logo_small.png",
|
||||||
|
"large": "img/logo_large.png"
|
||||||
|
},
|
||||||
|
"links": [
|
||||||
|
{"name": "Project site", "url": "http://project.com"},
|
||||||
|
{"name": "License & Terms", "url": "http://license.com"}
|
||||||
|
],
|
||||||
|
"version": "1.0.0",
|
||||||
|
"updated": "2015-02-10"
|
||||||
|
},
|
||||||
|
|
||||||
|
"dependencies": {
|
||||||
|
"grafanaVersion": "2.6.x",
|
||||||
|
"plugins": [
|
||||||
|
{"type": "datasource", "id": "graphite", "name": "Graphite", "version": "1.0.0"},
|
||||||
|
{"type": "panel", "id": "graph", "name": "Graph", "version": "1.0.0"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
3
examples/nginx-app/src/components/config.html
Normal file
3
examples/nginx-app/src/components/config.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<h3>
|
||||||
|
Nginx config!
|
||||||
|
</h3>
|
6
examples/nginx-app/src/components/config.js
Normal file
6
examples/nginx-app/src/components/config.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
export class NginxAppConfigCtrl {
|
||||||
|
}
|
||||||
|
NginxAppConfigCtrl.templateUrl = 'components/config.html';
|
||||||
|
|
||||||
|
|
3
examples/nginx-app/src/components/logs.html
Normal file
3
examples/nginx-app/src/components/logs.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<h3>
|
||||||
|
Logs page!
|
||||||
|
</h3>
|
6
examples/nginx-app/src/components/logs.js
Normal file
6
examples/nginx-app/src/components/logs.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
export class LogsPageCtrl {
|
||||||
|
}
|
||||||
|
LogsPageCtrl.templateUrl = 'components/logs.html';
|
||||||
|
|
||||||
|
|
3
examples/nginx-app/src/components/stream.html
Normal file
3
examples/nginx-app/src/components/stream.html
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
<h3>
|
||||||
|
Stream page!
|
||||||
|
</h3>
|
6
examples/nginx-app/src/components/stream.js
Normal file
6
examples/nginx-app/src/components/stream.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
export class StreamPageCtrl {
|
||||||
|
}
|
||||||
|
StreamPageCtrl.templateUrl = 'components/stream.html';
|
||||||
|
|
||||||
|
|
0
examples/nginx-app/src/css/light.css
Normal file
0
examples/nginx-app/src/css/light.css
Normal file
17
examples/nginx-app/src/dashboards/dashboard.js
Normal file
17
examples/nginx-app/src/dashboards/dashboard.js
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
require([
|
||||||
|
], function () {
|
||||||
|
|
||||||
|
function Dashboard() {
|
||||||
|
|
||||||
|
this.getInputs = function() {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
this.buildDashboard = function() {
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return Dashboard;
|
||||||
|
});
|
||||||
|
|
BIN
examples/nginx-app/src/img/logo_large.png
Normal file
BIN
examples/nginx-app/src/img/logo_large.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
BIN
examples/nginx-app/src/img/logo_small.png
Normal file
BIN
examples/nginx-app/src/img/logo_small.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.3 KiB |
9
examples/nginx-app/src/module.js
Normal file
9
examples/nginx-app/src/module.js
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import {LogsPageCtrl} from './components/logs';
|
||||||
|
import {StreamPageCtrl} from './components/stream';
|
||||||
|
import {NginxAppConfigCtrl} from './components/config';
|
||||||
|
|
||||||
|
export {
|
||||||
|
NginxAppConfigCtrl as ConfigCtrl,
|
||||||
|
StreamPageCtrl,
|
||||||
|
LogsPageCtrl
|
||||||
|
};
|
15
examples/nginx-app/src/panel/module.js
Normal file
15
examples/nginx-app/src/panel/module.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import {PanelCtrl} from 'app/plugins/sdk';
|
||||||
|
|
||||||
|
class NginxPanelCtrl extends PanelCtrl {
|
||||||
|
|
||||||
|
constructor($scope, $injector) {
|
||||||
|
super($scope, $injector);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
NginxPanelCtrl.template = '<h2>nginx!</h2>';
|
||||||
|
|
||||||
|
export {
|
||||||
|
NginxPanelCtrl as PanelCtrl
|
||||||
|
};
|
||||||
|
|
5
examples/nginx-app/src/panel/plugin.json
Normal file
5
examples/nginx-app/src/panel/plugin.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"type": "panel",
|
||||||
|
"name": "Nginx Panel",
|
||||||
|
"id": "nginx-panel"
|
||||||
|
}
|
28
examples/panel-boilerplate-es5/module.js
Normal file
28
examples/panel-boilerplate-es5/module.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
define([
|
||||||
|
'app/plugins/sdk'
|
||||||
|
], function(sdk) {
|
||||||
|
|
||||||
|
var BoilerPlatePanel = (function(_super) {
|
||||||
|
|
||||||
|
function BoilerPlatePanel($scope, $injector) {
|
||||||
|
_super.call(this, $scope, $injector);
|
||||||
|
}
|
||||||
|
|
||||||
|
// you do not need a templateUrl, you can use a inline template here
|
||||||
|
// BoilerPlatePanel.template = '<h2>boilerplate</h2>';
|
||||||
|
|
||||||
|
// all panel static assets can be accessed via 'public/plugins/<plugin-id>/<file>
|
||||||
|
BoilerPlatePanel.templateUrl = 'panel.html';
|
||||||
|
|
||||||
|
BoilerPlatePanel.prototype = Object.create(_super.prototype);
|
||||||
|
BoilerPlatePanel.prototype.constructor = BoilerPlatePanel;
|
||||||
|
|
||||||
|
return BoilerPlatePanel;
|
||||||
|
|
||||||
|
})(sdk.PanelCtrl);
|
||||||
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
PanelCtrl: BoilerPlatePanel
|
||||||
|
};
|
||||||
|
});
|
4
examples/panel-boilerplate-es5/panel.html
Normal file
4
examples/panel-boilerplate-es5/panel.html
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<h2 class="text-center">
|
||||||
|
Boilerplate panel
|
||||||
|
</h2>
|
||||||
|
|
6
examples/panel-boilerplate-es5/plugin.json
Normal file
6
examples/panel-boilerplate-es5/plugin.json
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"type": "panel",
|
||||||
|
"name": "Panel Boilerplate",
|
||||||
|
"id": "panel-boilerplate-es5",
|
||||||
|
"staticRoot": "."
|
||||||
|
}
|
@ -51,7 +51,7 @@
|
|||||||
"phantomjs": "^1.9.19",
|
"phantomjs": "^1.9.19",
|
||||||
"reflect-metadata": "0.1.2",
|
"reflect-metadata": "0.1.2",
|
||||||
"rxjs": "5.0.0-beta.0",
|
"rxjs": "5.0.0-beta.0",
|
||||||
"systemjs": "0.19.6",
|
"systemjs": "0.19.20",
|
||||||
"zone.js": "0.5.10"
|
"zone.js": "0.5.10"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
@ -69,9 +69,9 @@
|
|||||||
"grunt-jscs": "~1.5.x",
|
"grunt-jscs": "~1.5.x",
|
||||||
"grunt-sync": "^0.4.1",
|
"grunt-sync": "^0.4.1",
|
||||||
"karma-sinon": "^1.0.3",
|
"karma-sinon": "^1.0.3",
|
||||||
"lodash": "^2.4.1",
|
"lodash": "^4.0.0",
|
||||||
"sinon": "1.16.1",
|
"sinon": "1.16.1",
|
||||||
"systemjs-builder": "^0.14.15",
|
"systemjs-builder": "^0.15.7",
|
||||||
"tslint": "^3.2.1",
|
"tslint": "^3.2.1",
|
||||||
"typescript": "^1.7.5"
|
"typescript": "^1.7.5"
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,9 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httputil"
|
|
||||||
"net/url"
|
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/bus"
|
"github.com/grafana/grafana/pkg/api/pluginproxy"
|
||||||
"github.com/grafana/grafana/pkg/log"
|
"github.com/grafana/grafana/pkg/log"
|
||||||
"github.com/grafana/grafana/pkg/middleware"
|
"github.com/grafana/grafana/pkg/middleware"
|
||||||
m "github.com/grafana/grafana/pkg/models"
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
@ -22,16 +14,14 @@ import (
|
|||||||
func InitAppPluginRoutes(r *macaron.Macaron) {
|
func InitAppPluginRoutes(r *macaron.Macaron) {
|
||||||
for _, plugin := range plugins.Apps {
|
for _, plugin := range plugins.Apps {
|
||||||
for _, route := range plugin.Routes {
|
for _, route := range plugin.Routes {
|
||||||
log.Info("Plugin: Adding proxy route for app plugin")
|
url := util.JoinUrlFragments("/api/plugin-proxy/"+plugin.Id, route.Path)
|
||||||
url := util.JoinUrlFragments("/api/plugin-proxy/", route.Path)
|
|
||||||
handlers := make([]macaron.Handler, 0)
|
handlers := make([]macaron.Handler, 0)
|
||||||
if route.ReqSignedIn {
|
handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{
|
||||||
handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true}))
|
ReqSignedIn: true,
|
||||||
}
|
ReqGrafanaAdmin: route.ReqGrafanaAdmin,
|
||||||
if route.ReqGrafanaAdmin {
|
}))
|
||||||
handlers = append(handlers, middleware.Auth(&middleware.AuthOptions{ReqSignedIn: true, ReqGrafanaAdmin: true}))
|
|
||||||
}
|
if route.ReqRole != "" {
|
||||||
if route.ReqSignedIn && route.ReqRole != "" {
|
|
||||||
if route.ReqRole == m.ROLE_ADMIN {
|
if route.ReqRole == m.ROLE_ADMIN {
|
||||||
handlers = append(handlers, middleware.RoleAuth(m.ROLE_ADMIN))
|
handlers = append(handlers, middleware.RoleAuth(m.ROLE_ADMIN))
|
||||||
} else if route.ReqRole == m.ROLE_EDITOR {
|
} else if route.ReqRole == m.ROLE_EDITOR {
|
||||||
@ -40,7 +30,7 @@ func InitAppPluginRoutes(r *macaron.Macaron) {
|
|||||||
}
|
}
|
||||||
handlers = append(handlers, AppPluginRoute(route, plugin.Id))
|
handlers = append(handlers, AppPluginRoute(route, plugin.Id))
|
||||||
r.Route(url, route.Method, handlers...)
|
r.Route(url, route.Method, handlers...)
|
||||||
log.Info("Plugin: Adding route %s", url)
|
log.Info("Plugins: Adding proxy route %s", url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -49,68 +39,8 @@ func AppPluginRoute(route *plugins.AppPluginRoute, appId string) macaron.Handler
|
|||||||
return func(c *middleware.Context) {
|
return func(c *middleware.Context) {
|
||||||
path := c.Params("*")
|
path := c.Params("*")
|
||||||
|
|
||||||
proxy := NewApiPluginProxy(c, path, route, appId)
|
proxy := pluginproxy.NewApiPluginProxy(c, path, route, appId)
|
||||||
proxy.Transport = dataProxyTransport
|
proxy.Transport = dataProxyTransport
|
||||||
proxy.ServeHTTP(c.Resp, c.Req.Request)
|
proxy.ServeHTTP(c.Resp, c.Req.Request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewApiPluginProxy(ctx *middleware.Context, proxyPath string, route *plugins.AppPluginRoute, appId string) *httputil.ReverseProxy {
|
|
||||||
targetUrl, _ := url.Parse(route.Url)
|
|
||||||
|
|
||||||
director := func(req *http.Request) {
|
|
||||||
|
|
||||||
req.URL.Scheme = targetUrl.Scheme
|
|
||||||
req.URL.Host = targetUrl.Host
|
|
||||||
req.Host = targetUrl.Host
|
|
||||||
|
|
||||||
req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
|
|
||||||
|
|
||||||
// clear cookie headers
|
|
||||||
req.Header.Del("Cookie")
|
|
||||||
req.Header.Del("Set-Cookie")
|
|
||||||
|
|
||||||
//Create a HTTP header with the context in it.
|
|
||||||
ctxJson, err := json.Marshal(ctx.SignedInUser)
|
|
||||||
if err != nil {
|
|
||||||
ctx.JsonApiErr(500, "failed to marshal context to json.", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Add("Grafana-Context", string(ctxJson))
|
|
||||||
// add custom headers defined in the plugin config.
|
|
||||||
for _, header := range route.Headers {
|
|
||||||
var contentBuf bytes.Buffer
|
|
||||||
t, err := template.New("content").Parse(header.Content)
|
|
||||||
if err != nil {
|
|
||||||
ctx.JsonApiErr(500, fmt.Sprintf("could not parse header content template for header %s.", header.Name), err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//lookup appSettings
|
|
||||||
query := m.GetAppSettingByAppIdQuery{OrgId: ctx.OrgId, AppId: appId}
|
|
||||||
|
|
||||||
if err := bus.Dispatch(&query); err != nil {
|
|
||||||
ctx.JsonApiErr(500, "failed to get AppSettings.", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
type templateData struct {
|
|
||||||
JsonData map[string]interface{}
|
|
||||||
SecureJsonData map[string]string
|
|
||||||
}
|
|
||||||
data := templateData{
|
|
||||||
JsonData: query.Result.JsonData,
|
|
||||||
SecureJsonData: query.Result.SecureJsonData.Decrypt(),
|
|
||||||
}
|
|
||||||
err = t.Execute(&contentBuf, data)
|
|
||||||
if err != nil {
|
|
||||||
ctx.JsonApiErr(500, fmt.Sprintf("failed to execute header content template for header %s.", header.Name), err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
log.Debug("Adding header to proxy request. %s: %s", header.Name, contentBuf.String())
|
|
||||||
req.Header.Add(header.Name, contentBuf.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &httputil.ReverseProxy{Director: director}
|
|
||||||
}
|
|
||||||
|
@ -11,9 +11,10 @@ type AppSettings struct {
|
|||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
Pinned bool `json:"pinned"`
|
Pinned bool `json:"pinned"`
|
||||||
Module string `json:"module"`
|
Module string `json:"module"`
|
||||||
|
BaseUrl string `json:"baseUrl"`
|
||||||
Info *plugins.PluginInfo `json:"info"`
|
Info *plugins.PluginInfo `json:"info"`
|
||||||
Pages []plugins.AppPluginPage `json:"pages"`
|
Pages []*plugins.AppPluginPage `json:"pages"`
|
||||||
Includes []plugins.AppIncludeInfo `json:"includes"`
|
Includes []*plugins.AppIncludeInfo `json:"includes"`
|
||||||
JsonData map[string]interface{} `json:"jsonData"`
|
JsonData map[string]interface{} `json:"jsonData"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,6 +24,7 @@ func NewAppSettingsDto(def *plugins.AppPlugin, data *models.AppSettings) *AppSet
|
|||||||
Name: def.Name,
|
Name: def.Name,
|
||||||
Info: &def.Info,
|
Info: &def.Info,
|
||||||
Module: def.Module,
|
Module: def.Module,
|
||||||
|
BaseUrl: def.BaseUrl,
|
||||||
Pages: def.Pages,
|
Pages: def.Pages,
|
||||||
Includes: def.Includes,
|
Includes: def.Includes,
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,6 @@ type IndexViewData struct {
|
|||||||
AppSubUrl string
|
AppSubUrl string
|
||||||
GoogleAnalyticsId string
|
GoogleAnalyticsId string
|
||||||
GoogleTagManagerId string
|
GoogleTagManagerId string
|
||||||
|
|
||||||
PluginCss []*PluginCss
|
|
||||||
PluginModules []string
|
|
||||||
MainNavLinks []*NavLink
|
MainNavLinks []*NavLink
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,4 +20,5 @@ type NavLink struct {
|
|||||||
Icon string `json:"icon"`
|
Icon string `json:"icon"`
|
||||||
Img string `json:"img"`
|
Img string `json:"img"`
|
||||||
Url string `json:"url"`
|
Url string `json:"url"`
|
||||||
|
Children []*NavLink `json:"children"`
|
||||||
}
|
}
|
||||||
|
@ -122,6 +122,7 @@ func getFrontendSettingsMap(c *middleware.Context) (map[string]interface{}, erro
|
|||||||
for _, panel := range enabledPlugins.Panels {
|
for _, panel := range enabledPlugins.Panels {
|
||||||
panels[panel.Id] = map[string]interface{}{
|
panels[panel.Id] = map[string]interface{}{
|
||||||
"module": panel.Module,
|
"module": panel.Module,
|
||||||
|
"baseUrl": panel.BaseUrl,
|
||||||
"name": panel.Name,
|
"name": panel.Name,
|
||||||
"id": panel.Id,
|
"id": panel.Id,
|
||||||
"info": panel.Info,
|
"info": panel.Info,
|
||||||
|
@ -36,7 +36,7 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if setting.DisableGravatar {
|
if setting.DisableGravatar {
|
||||||
data.User.GravatarUrl = setting.AppSubUrl + "/img/user_profile.png"
|
data.User.GravatarUrl = setting.AppSubUrl + "/public/img/user_profile.png"
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(data.User.Name) == 0 {
|
if len(data.User.Name) == 0 {
|
||||||
@ -51,20 +51,27 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
|
|||||||
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
||||||
Text: "Dashboards",
|
Text: "Dashboards",
|
||||||
Icon: "icon-gf icon-gf-dashboard",
|
Icon: "icon-gf icon-gf-dashboard",
|
||||||
Url: "/",
|
Url: setting.AppSubUrl + "/",
|
||||||
|
Children: []*dtos.NavLink{
|
||||||
|
{Text: "Playlists", Icon: "fa fa-fw fa-list", Url: setting.AppSubUrl + "/playlists"},
|
||||||
|
{Text: "Snapshots", Icon: "fa-fw icon-gf icon-gf-snapshot", Url: setting.AppSubUrl + "/dashboard/snapshots"},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{Text: "Playlists", Icon: "fa fa-fw fa-list", Url: setting.AppSubUrl + "/playlists"})
|
||||||
|
// data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{Text: "Snapshots", Icon: "fa-fw icon-gf icon-gf-snapshot", Url: setting.AppSubUrl + "/dashboard/snapshots"})
|
||||||
|
|
||||||
if c.OrgRole == m.ROLE_ADMIN {
|
if c.OrgRole == m.ROLE_ADMIN {
|
||||||
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
||||||
Text: "Data Sources",
|
Text: "Data Sources",
|
||||||
Icon: "icon-gf icon-gf-datasources",
|
Icon: "icon-gf icon-gf-datasources",
|
||||||
Url: "/datasources",
|
Url: setting.AppSubUrl + "/datasources",
|
||||||
})
|
})
|
||||||
|
|
||||||
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
||||||
Text: "Plugins",
|
Text: "Plugins",
|
||||||
Icon: "icon-gf icon-gf-apps",
|
Icon: "icon-gf icon-gf-apps",
|
||||||
Url: "/apps",
|
Url: setting.AppSubUrl + "/apps",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,21 +81,22 @@ func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, plugin := range enabledPlugins.Apps {
|
for _, plugin := range enabledPlugins.Apps {
|
||||||
if plugin.Module != "" {
|
|
||||||
data.PluginModules = append(data.PluginModules, plugin.Module)
|
|
||||||
}
|
|
||||||
|
|
||||||
if plugin.Css != nil {
|
|
||||||
data.PluginCss = append(data.PluginCss, &dtos.PluginCss{Light: plugin.Css.Light, Dark: plugin.Css.Dark})
|
|
||||||
}
|
|
||||||
|
|
||||||
if plugin.Pinned {
|
if plugin.Pinned {
|
||||||
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
pageLink := &dtos.NavLink{
|
||||||
Text: plugin.Name,
|
Text: plugin.Name,
|
||||||
Url: "/apps/edit/" + plugin.Id,
|
Url: setting.AppSubUrl + "/apps/" + plugin.Id + "/edit",
|
||||||
Img: plugin.Info.Logos.Small,
|
Img: plugin.Info.Logos.Small,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, page := range plugin.Pages {
|
||||||
|
pageLink.Children = append(pageLink.Children, &dtos.NavLink{
|
||||||
|
Url: setting.AppSubUrl + "/apps/" + plugin.Id + "/page/" + page.Slug,
|
||||||
|
Text: page.Name,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data.MainNavLinks = append(data.MainNavLinks, pageLink)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &data, nil
|
return &data, nil
|
||||||
|
99
pkg/api/pluginproxy/pluginproxy.go
Normal file
99
pkg/api/pluginproxy/pluginproxy.go
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package pluginproxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httputil"
|
||||||
|
"net/url"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
|
"github.com/grafana/grafana/pkg/log"
|
||||||
|
"github.com/grafana/grafana/pkg/middleware"
|
||||||
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
|
"github.com/grafana/grafana/pkg/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
type templateData struct {
|
||||||
|
JsonData map[string]interface{}
|
||||||
|
SecureJsonData map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func getHeaders(route *plugins.AppPluginRoute, orgId int64, appId string) (http.Header, error) {
|
||||||
|
result := http.Header{}
|
||||||
|
|
||||||
|
query := m.GetAppSettingByAppIdQuery{OrgId: orgId, AppId: appId}
|
||||||
|
|
||||||
|
if err := bus.Dispatch(&query); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
data := templateData{
|
||||||
|
JsonData: query.Result.JsonData,
|
||||||
|
SecureJsonData: query.Result.SecureJsonData.Decrypt(),
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, header := range route.Headers {
|
||||||
|
var contentBuf bytes.Buffer
|
||||||
|
t, err := template.New("content").Parse(header.Content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New(fmt.Sprintf("could not parse header content template for header %s.", header.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = t.Execute(&contentBuf, data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New(fmt.Sprintf("failed to execute header content template for header %s.", header.Name))
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Trace("Adding header to proxy request. %s: %s", header.Name, contentBuf.String())
|
||||||
|
result.Add(header.Name, contentBuf.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewApiPluginProxy(ctx *middleware.Context, proxyPath string, route *plugins.AppPluginRoute, appId string) *httputil.ReverseProxy {
|
||||||
|
targetUrl, _ := url.Parse(route.Url)
|
||||||
|
|
||||||
|
director := func(req *http.Request) {
|
||||||
|
|
||||||
|
req.URL.Scheme = targetUrl.Scheme
|
||||||
|
req.URL.Host = targetUrl.Host
|
||||||
|
req.Host = targetUrl.Host
|
||||||
|
|
||||||
|
req.URL.Path = util.JoinUrlFragments(targetUrl.Path, proxyPath)
|
||||||
|
|
||||||
|
// clear cookie headers
|
||||||
|
req.Header.Del("Cookie")
|
||||||
|
req.Header.Del("Set-Cookie")
|
||||||
|
|
||||||
|
//Create a HTTP header with the context in it.
|
||||||
|
ctxJson, err := json.Marshal(ctx.SignedInUser)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JsonApiErr(500, "failed to marshal context to json.", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
req.Header.Add("X-Grafana-Context", string(ctxJson))
|
||||||
|
|
||||||
|
if len(route.Headers) > 0 {
|
||||||
|
headers, err := getHeaders(route, ctx.OrgId, appId)
|
||||||
|
if err != nil {
|
||||||
|
ctx.JsonApiErr(500, "Could not generate plugin route header", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range headers {
|
||||||
|
log.Info("setting key %v value %v", key, value[0])
|
||||||
|
req.Header.Set(key, value[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return &httputil.ReverseProxy{Director: director}
|
||||||
|
}
|
42
pkg/api/pluginproxy/pluginproxy_test.go
Normal file
42
pkg/api/pluginproxy/pluginproxy_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package pluginproxy
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/bus"
|
||||||
|
m "github.com/grafana/grafana/pkg/models"
|
||||||
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
|
"github.com/grafana/grafana/pkg/util"
|
||||||
|
. "github.com/smartystreets/goconvey/convey"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPluginProxy(t *testing.T) {
|
||||||
|
|
||||||
|
Convey("When getting proxy headers", t, func() {
|
||||||
|
route := &plugins.AppPluginRoute{
|
||||||
|
Headers: []plugins.AppPluginRouteHeader{
|
||||||
|
{Name: "x-header", Content: "my secret {{.SecureJsonData.key}}"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
setting.SecretKey = "password"
|
||||||
|
|
||||||
|
bus.AddHandler("test", func(query *m.GetAppSettingByAppIdQuery) error {
|
||||||
|
query.Result = &m.AppSettings{
|
||||||
|
SecureJsonData: map[string][]byte{
|
||||||
|
"key": util.Encrypt([]byte("123"), "password"),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
header, err := getHeaders(route, 1, "my-app")
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
|
Convey("Should render header template", func() {
|
||||||
|
So(header.Get("x-header"), ShouldEqual, "my secret 123")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
@ -31,14 +31,11 @@ func newMacaron() *macaron.Macaron {
|
|||||||
|
|
||||||
for _, route := range plugins.StaticRoutes {
|
for _, route := range plugins.StaticRoutes {
|
||||||
pluginRoute := path.Join("/public/plugins/", route.PluginId)
|
pluginRoute := path.Join("/public/plugins/", route.PluginId)
|
||||||
log.Info("Plugin: Adding static route %s -> %s", pluginRoute, route.Directory)
|
log.Info("Plugins: Adding route %s -> %s", pluginRoute, route.Directory)
|
||||||
mapStatic(m, route.Directory, "", pluginRoute)
|
mapStatic(m, route.Directory, "", pluginRoute)
|
||||||
}
|
}
|
||||||
|
|
||||||
mapStatic(m, setting.StaticRootPath, "", "public")
|
mapStatic(m, setting.StaticRootPath, "", "public")
|
||||||
mapStatic(m, setting.StaticRootPath, "css", "css")
|
|
||||||
mapStatic(m, setting.StaticRootPath, "img", "img")
|
|
||||||
mapStatic(m, setting.StaticRootPath, "fonts", "fonts")
|
|
||||||
mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
|
mapStatic(m, setting.StaticRootPath, "robots.txt", "robots.txt")
|
||||||
|
|
||||||
m.Use(macaron.Renderer(macaron.RenderOptions{
|
m.Use(macaron.Renderer(macaron.RenderOptions{
|
||||||
|
@ -49,6 +49,14 @@ type UpdateAppSettingsCmd struct {
|
|||||||
OrgId int64 `json:"-"`
|
OrgId int64 `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cmd *UpdateAppSettingsCmd) GetEncryptedJsonData() SecureJsonData {
|
||||||
|
encrypted := make(SecureJsonData)
|
||||||
|
for key, data := range cmd.SecureJsonData {
|
||||||
|
encrypted[key] = util.Encrypt([]byte(data), setting.SecretKey)
|
||||||
|
}
|
||||||
|
return encrypted
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------------
|
// ---------------------
|
||||||
// QUERIES
|
// QUERIES
|
||||||
type GetAppSettingsQuery struct {
|
type GetAppSettingsQuery struct {
|
||||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user