replace zxq.co/ripple/hanayo
This commit is contained in:
18
vendor/github.com/thehowl/qsql/LICENSE
generated
vendored
Normal file
18
vendor/github.com/thehowl/qsql/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
Copyright (c) 2016 Morgan Bazalgette
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the "Software"),
|
||||
to deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
sell copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||||
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
||||
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
3
vendor/github.com/thehowl/qsql/README.md
generated
vendored
Normal file
3
vendor/github.com/thehowl/qsql/README.md
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# qsql
|
||||
|
||||
**Q**uick **SQL** queries, with maps and slices. See [godoc](https://godoc.org/github.com/thehowl/qsql).
|
17
vendor/github.com/thehowl/qsql/new.go
generated
vendored
Normal file
17
vendor/github.com/thehowl/qsql/new.go
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
package qsql
|
||||
|
||||
import "database/sql"
|
||||
|
||||
// New creates a new *DB having an *sql.DB.
|
||||
func New(db *sql.DB) *DB {
|
||||
if db == nil {
|
||||
return nil
|
||||
}
|
||||
return &DB{*db}
|
||||
}
|
||||
|
||||
// Open behaves the same as sql.Open, but creates an *qsql.DB instead.
|
||||
func Open(driverName, dsn string) (*DB, error) {
|
||||
db, err := sql.Open(driverName, dsn)
|
||||
return New(db), err
|
||||
}
|
97
vendor/github.com/thehowl/qsql/qsql.go
generated
vendored
Normal file
97
vendor/github.com/thehowl/qsql/qsql.go
generated
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
// Package qsql implements SQL queries for the lazy, in the good ol' hashtable
|
||||
// or list of hashtable format.
|
||||
package qsql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// ErrDBIsNil is returned when the *sql.DB inside DB is nil.
|
||||
var ErrDBIsNil = errors.New("qsql: db is nil")
|
||||
|
||||
// DB wraps an sql.DB around a custom DB.
|
||||
//
|
||||
// If you're hardcore and want to create one without New(),
|
||||
// you should &qsql.DB{*db}.
|
||||
type DB struct {
|
||||
sql.DB
|
||||
}
|
||||
|
||||
// Exec behaves the same as sql.DB.Exec, however it does not wrap the last
|
||||
// insert ID and rows affected into an interface.
|
||||
func (d *DB) Exec(query string, params ...interface{}) (int, int, error) {
|
||||
res, err := d.DB.Exec(query, params...)
|
||||
lid, _ := res.LastInsertId()
|
||||
ra, _ := res.RowsAffected()
|
||||
return int(lid), int(ra), err
|
||||
}
|
||||
|
||||
// ExecNoRes returns sql.DB.Exec without Result.
|
||||
func (d *DB) ExecNoRes(query string, params ...interface{}) error {
|
||||
_, err := d.DB.Exec(query, params...)
|
||||
return err
|
||||
}
|
||||
|
||||
// Query queries the database for multiple rows. See sql.DB.Query.
|
||||
func (d *DB) Query(query string, params ...interface{}) ([]map[string]String, error) {
|
||||
return d.query(query, false, params...)
|
||||
}
|
||||
|
||||
// QueryRow queries the database for one row. See sql.DB.QueryRow.
|
||||
func (d *DB) QueryRow(query string, params ...interface{}) (map[string]String, error) {
|
||||
// sql.Row does not have .Columns(), so we can't really use db.QueryRow.
|
||||
// Instead, we use .query, telling it to return after the first row is extracted.
|
||||
m, err := d.query(query, true, params...)
|
||||
if len(m) > 0 {
|
||||
return m[0], err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (d *DB) query(query string, only1 bool, params ...interface{}) ([]map[string]String, error) {
|
||||
rows, err := d.DB.Query(query, params...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
cols, err := rows.Columns()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var returnSlice []map[string]String
|
||||
for rows.Next() {
|
||||
m, args := buildMapAndArgsSlice(cols)
|
||||
err := rows.Scan(args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
returnSlice = append(returnSlice, depointify(m))
|
||||
if only1 {
|
||||
return returnSlice, rows.Err()
|
||||
}
|
||||
}
|
||||
return returnSlice, rows.Err()
|
||||
}
|
||||
|
||||
func buildMapAndArgsSlice(cols []string) (map[string]*string, []interface{}) {
|
||||
m := make(map[string]*string, len(cols))
|
||||
sl := make([]interface{}, 0, len(cols))
|
||||
for _, col := range cols {
|
||||
var newS string
|
||||
m[col] = &newS
|
||||
sl = append(sl, &newS)
|
||||
}
|
||||
return m, sl
|
||||
}
|
||||
|
||||
func depointify(from map[string]*string) map[string]String {
|
||||
m := make(map[string]String, len(from))
|
||||
for k, v := range from {
|
||||
if v == nil {
|
||||
v = new(string)
|
||||
}
|
||||
m[k] = String(*v)
|
||||
}
|
||||
return m
|
||||
}
|
72
vendor/github.com/thehowl/qsql/qsql_test.go
generated
vendored
Normal file
72
vendor/github.com/thehowl/qsql/qsql_test.go
generated
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
package qsql_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/thehowl/qsql"
|
||||
)
|
||||
|
||||
/*
|
||||
database:
|
||||
|
||||
CREATE DATABASE qsql;
|
||||
USE qsql;
|
||||
CREATE TABLE qsql_test(
|
||||
id INT(11) NOT NULL AUTO_INCREMENT,
|
||||
potato VARCHAR(128) NOT NULL,
|
||||
apple VARCHAR(128) NOT NULL,
|
||||
PRIMARY KEY(id)
|
||||
);
|
||||
INSERT INTO qsql_test(potato, apple) VALUES ("test", "test");
|
||||
*/
|
||||
|
||||
func TestQuery(t *testing.T) {
|
||||
db, err := qsql.Open("mysql", "root@/qsql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
data, err := db.Query("SELECT * FROM qsql_test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, row := range data {
|
||||
if row["potato"] != "test" || row["apple"] != "test" {
|
||||
t.Fatal("Expected row to have potato=test and apple=test, got", row, "instead")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryRow(t *testing.T) {
|
||||
db, err := qsql.Open("mysql", "root@/qsql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
row, err := db.QueryRow("SELECT * FROM qsql_test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if row["potato"] != "test" || row["apple"] != "test" {
|
||||
t.Fatal("Expected row to have potato=test and apple=test, got", row, "instead")
|
||||
}
|
||||
}
|
||||
|
||||
func Example() {
|
||||
db, err := qsql.Open("mysql", "root@/")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer db.Close()
|
||||
row, err := db.QueryRow("SELECT 5 AS test, 1 AS test_bool, 13.37 AS test_float")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf(
|
||||
"test: %d | test_bool: %v | test_float: %.3f\n",
|
||||
row["test"].Int(), row["test_bool"].Bool(), row["test_float"].Float64(),
|
||||
)
|
||||
// Output: test: 5 | test_bool: true | test_float: 13.370
|
||||
}
|
78
vendor/github.com/thehowl/qsql/string.go
generated
vendored
Normal file
78
vendor/github.com/thehowl/qsql/string.go
generated
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
package qsql
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// String is just a string, but it implements numerous functions to convert it to various types.
|
||||
type String string
|
||||
|
||||
// String is a shorthand for string(s).
|
||||
func (s String) String() string {
|
||||
return string(s)
|
||||
}
|
||||
|
||||
// Int will convert s to an int. It will return 0 if conversion failed, with no error.
|
||||
func (s String) Int() int {
|
||||
r, _ := strconv.ParseInt(string(s), 10, 0)
|
||||
return int(r)
|
||||
}
|
||||
|
||||
// Uint will convert s to an uint. It will return 0 if conversion failed, with no error.
|
||||
func (s String) Uint() uint {
|
||||
r, _ := strconv.ParseUint(string(s), 10, 0)
|
||||
return uint(r)
|
||||
}
|
||||
|
||||
// Int64 will convert s to an int64. It will return 0 if conversion failed, with no error.
|
||||
func (s String) Int64() int64 {
|
||||
r, _ := strconv.ParseInt(string(s), 10, 64)
|
||||
return r
|
||||
}
|
||||
|
||||
// Uint64 will convert s to an uint64. It will return 0 if conversion failed, with no error.
|
||||
func (s String) Uint64() uint64 {
|
||||
r, _ := strconv.ParseUint(string(s), 10, 64)
|
||||
return r
|
||||
}
|
||||
|
||||
// Float64 will convert s to a float64. It will return 0 if conversion failed, with no error.
|
||||
func (s String) Float64() float64 {
|
||||
r, _ := strconv.ParseFloat(string(s), 64)
|
||||
return r
|
||||
}
|
||||
|
||||
// Float32 will convert s to a float32. It will return 0 if conversion failed, with no error.
|
||||
func (s String) Float32() float32 {
|
||||
r, _ := strconv.ParseFloat(string(s), 32)
|
||||
return float32(r)
|
||||
}
|
||||
|
||||
var truthyValues = [...]string{
|
||||
"1",
|
||||
"t",
|
||||
"true",
|
||||
"y",
|
||||
"yes",
|
||||
}
|
||||
|
||||
// Bool converts s to a bool.
|
||||
//
|
||||
// The following values are true:
|
||||
//
|
||||
// - 1
|
||||
// - t
|
||||
// - true
|
||||
// - y
|
||||
// - yes
|
||||
//
|
||||
// All other values are false.
|
||||
// Bool is not case sensitive.
|
||||
func (s String) Bool() bool {
|
||||
for _, el := range truthyValues {
|
||||
if string(s) == el {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
Reference in New Issue
Block a user