-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db): added
Duration
support in Scanner/Valuer (#27)
- Loading branch information
Showing
7 changed files
with
133 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package sqle | ||
|
||
import ( | ||
"database/sql/driver" | ||
"errors" | ||
"time" | ||
) | ||
|
||
type Duration time.Duration | ||
|
||
func (d Duration) Duration() time.Duration { // skipcq: GO-W1029 | ||
return time.Duration(d) | ||
} | ||
|
||
// Value implements the driver.Valuer interface, | ||
// and turns the Duration into a VARCHAR field for MySQL storage. | ||
func (d Duration) Value() (driver.Value, error) { // skipcq: GO-W1029 | ||
return time.Duration(d).String(), nil | ||
} | ||
|
||
// Scan implements the sql.Scanner interface, | ||
// and turns the VARCHAR field incoming from MySQL into a Duration | ||
func (d *Duration) Scan(src interface{}) error { // skipcq: GO-W1029 | ||
if src == nil { | ||
return nil | ||
} | ||
|
||
var val string | ||
|
||
switch v := src.(type) { | ||
case []byte: | ||
val = string(v) | ||
case string: | ||
val = v | ||
default: | ||
return errors.New("bad duration type assertion") | ||
} | ||
|
||
td, err := time.ParseDuration(val) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*d = Duration(td) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package sqle | ||
|
||
import ( | ||
"database/sql" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDuration(t *testing.T) { | ||
d, err := sql.Open("sqlite3", "file::memory:") | ||
require.NoError(t, err) | ||
|
||
_, err = d.Exec("CREATE TABLE `users` (`id` id NOT NULL,`ttl` VARCHAR(20), `b_ttl` VARBINARY, PRIMARY KEY (`id`))") | ||
require.NoError(t, err) | ||
|
||
d10 := Duration(10 * time.Second) | ||
d11 := Duration(11 * time.Second) | ||
d12 := Duration(12 * time.Second) | ||
d13 := Duration(13 * time.Second) | ||
|
||
result, err := d.Exec("INSERT INTO `users`(`id`, `ttl`,`b_ttl`) VALUES(?, ?, ?)", 10, d10, d10) | ||
require.NoError(t, err) | ||
|
||
rows, err := result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rows) | ||
|
||
result, err = d.Exec("INSERT INTO `users`(`id`, `ttl`) VALUES(?, ?)", 11, d11) | ||
require.NoError(t, err) | ||
|
||
rows, err = result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rows) | ||
|
||
result, err = d.Exec("INSERT INTO `users`(`id`, `ttl`) VALUES(?, ?)", 12, d12) | ||
require.NoError(t, err) | ||
|
||
rows, err = result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rows) | ||
|
||
result, err = d.Exec("INSERT INTO `users`(`id`, `ttl`) VALUES(?, ?)", 13, d13) | ||
require.NoError(t, err) | ||
|
||
rows, err = result.RowsAffected() | ||
require.NoError(t, err) | ||
require.Equal(t, int64(1), rows) | ||
|
||
var b10 Duration | ||
var b_b10 Duration | ||
err = d.QueryRow("SELECT `ttl`, `b_ttl` FROM `users` WHERE id=?", 10).Scan(&b10, &b_b10) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, d10.Duration(), b10.Duration()) | ||
require.Equal(t, d10.Duration(), b_b10.Duration()) | ||
|
||
var b11 Duration | ||
var b_b11 Duration | ||
err = d.QueryRow("SELECT `ttl`, `b_ttl` FROM `users` WHERE id=?", 11).Scan(&b11, &b_b11) | ||
require.NoError(t, err) | ||
|
||
require.EqualValues(t, d11, b11) | ||
require.Empty(t, b_b11) | ||
|
||
var b12 Duration | ||
err = d.QueryRow("SELECT `ttl` FROM `users` WHERE id=?", 12).Scan(&b12) | ||
require.NoError(t, err) | ||
|
||
require.EqualValues(t, d12, b12) | ||
|
||
var b13 Duration | ||
err = d.QueryRow("SELECT `ttl` FROM `users` WHERE id=?", 13).Scan(&b13) | ||
require.NoError(t, err) | ||
|
||
require.EqualValues(t, d13, b13) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters