-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9241391
commit 97ccb25
Showing
10 changed files
with
824 additions
and
11 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,253 @@ | ||
------------------- | ||
var | ||
------------------- | ||
const ( | ||
UNKNOWN = iota | ||
QUESTION | ||
DOLLAR | ||
NAMED | ||
AT | ||
) | ||
|
||
var NameMapper = strings.ToLower | ||
|
||
------------------- | ||
type | ||
------------------- | ||
# type ColScanner interface { | ||
Columns() ([]string, error) | ||
Scan(dest ...interface{}) error | ||
Err() error | ||
} | ||
|
||
# type Conn struct { | ||
*sql.Conn | ||
Mapper *reflectx.Mapper | ||
// contains filtered or unexported fields | ||
} | ||
|
||
func (c *Conn) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) | ||
func (c *Conn) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error | ||
func (c *Conn) PreparexContext(ctx context.Context, query string) (*Stmt, error) | ||
func (c *Conn) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row | ||
func (c *Conn) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) | ||
func (c *Conn) Rebind(query string) string | ||
func (c *Conn) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error | ||
|
||
# type DB struct { | ||
*sql.DB | ||
Mapper *reflectx.Mapper | ||
// contains filtered or unexported fields | ||
} | ||
|
||
* 本身就继承了 sql.DB | ||
|
||
func Connect(driverName, dataSourceName string) (*DB, error) | ||
func ConnectContext(ctx context.Context, driverName, dataSourceName string) (*DB, error) | ||
func MustConnect(driverName, dataSourceName string) *DB | ||
* 创建新的数据库,并且立即创建连接 | ||
|
||
func MustOpen(driverName, dataSourceName string) *DB | ||
func Open(driverName, dataSourceName string) (*DB, error) | ||
* 创建新得数据库,不立即创建连接 | ||
|
||
func NewDb(db *sql.DB, driverName string) *DB | ||
* 使用sql.DB创建数据库 | ||
|
||
func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) | ||
func (db *DB) Beginx() (*Tx, error) | ||
func (db *DB) BindNamed(query string, arg interface{}) (string, []interface{}, error) | ||
func (db *DB) Connx(ctx context.Context) (*Conn, error) | ||
func (db *DB) DriverName() string | ||
|
||
func (db *DB) MapperFunc(mf func(string) string) | ||
|
||
func (db *DB) MustBegin() *Tx | ||
func (db *DB) MustBeginTx(ctx context.Context, opts *sql.TxOptions) *Tx | ||
|
||
func (db *DB) MustExec(query string, args ...interface{}) sql.Result | ||
func (db *DB) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result | ||
* 执行SQL更新语句,如果出现异常则panic | ||
* 如果需要自己控制err,则可以使用父类的Exec方法 | ||
|
||
func (db *DB) NamedExec(query string, arg interface{}) (sql.Result, error) | ||
func (db *DB) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) | ||
func (db *DB) NamedQuery(query string, arg interface{}) (*Rows, error) | ||
func (db *DB) NamedQueryContext(ctx context.Context, query string, arg interface{}) (*Rows, error) | ||
|
||
func (db *DB) PrepareNamed(query string) (*NamedStmt, error) | ||
func (db *DB) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error) | ||
func (db *DB) Preparex(query string) (*Stmt, error) | ||
func (db *DB) PreparexContext(ctx context.Context, query string) (*Stmt, error) | ||
|
||
func (db *DB) QueryRowx(query string, args ...interface{}) *Row | ||
func (db *DB) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row | ||
* 查询单行结果,异常发生到Row绑定的时候 | ||
|
||
func (db *DB) Queryx(query string, args ...interface{}) (*Rows, error) | ||
func (db *DB) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) | ||
* 查询多行结果,异常发生在查询执行的时候 | ||
|
||
func (db *DB) Rebind(query string) string | ||
|
||
func (db *DB) Get(dest interface{}, query string, args ...interface{}) error | ||
func (db *DB) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error | ||
|
||
func (db *DB) Select(dest interface{}, query string, args ...interface{}) error | ||
func (db *DB) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error | ||
|
||
func (db *DB) Unsafe() *DB | ||
|
||
# type Execer interface { | ||
Exec(query string, args ...interface{}) (sql.Result, error) | ||
} | ||
|
||
# type ExecerContext interface { | ||
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) | ||
} | ||
# type Ext interface { | ||
Queryer | ||
Execer | ||
} | ||
# type ExtContext interface { | ||
QueryerContext | ||
ExecerContext | ||
} | ||
# type NamedStmt struct { | ||
Params []string | ||
QueryString string | ||
Stmt *Stmt | ||
} | ||
func (n *NamedStmt) Close() error | ||
func (n *NamedStmt) Exec(arg interface{}) (sql.Result, error) | ||
func (n *NamedStmt) ExecContext(ctx context.Context, arg interface{}) (sql.Result, error) | ||
func (n *NamedStmt) Get(dest interface{}, arg interface{}) error | ||
func (n *NamedStmt) GetContext(ctx context.Context, dest interface{}, arg interface{}) error | ||
func (n *NamedStmt) MustExec(arg interface{}) sql.Result | ||
func (n *NamedStmt) MustExecContext(ctx context.Context, arg interface{}) sql.Result | ||
func (n *NamedStmt) Query(arg interface{}) (*sql.Rows, error) | ||
func (n *NamedStmt) QueryContext(ctx context.Context, arg interface{}) (*sql.Rows, error) | ||
func (n *NamedStmt) QueryRow(arg interface{}) *Row | ||
func (n *NamedStmt) QueryRowContext(ctx context.Context, arg interface{}) *Row | ||
func (n *NamedStmt) QueryRowx(arg interface{}) *Row | ||
func (n *NamedStmt) QueryRowxContext(ctx context.Context, arg interface{}) *Row | ||
func (n *NamedStmt) Queryx(arg interface{}) (*Rows, error) | ||
func (n *NamedStmt) QueryxContext(ctx context.Context, arg interface{}) (*Rows, error) | ||
func (n *NamedStmt) Select(dest interface{}, arg interface{}) error | ||
func (n *NamedStmt) SelectContext(ctx context.Context, dest interface{}, arg interface{}) error | ||
func (n *NamedStmt) Unsafe() *NamedStmt | ||
|
||
# type Preparer interface { | ||
Prepare(query string) (*sql.Stmt, error) | ||
} | ||
# type PreparerContext interface { | ||
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) | ||
} | ||
# type Queryer interface { | ||
Query(query string, args ...interface{}) (*sql.Rows, error) | ||
Queryx(query string, args ...interface{}) (*Rows, error) | ||
QueryRowx(query string, args ...interface{}) *Row | ||
} | ||
# type QueryerContext interface { | ||
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) | ||
QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) | ||
QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row | ||
} | ||
# type Row struct { | ||
Mapper *reflectx.Mapper | ||
} | ||
func (r *Row) ColumnTypes() ([]*sql.ColumnType, error) | ||
func (r *Row) Columns() ([]string, error) | ||
func (r *Row) Err() error | ||
func (r *Row) MapScan(dest map[string]interface{}) error | ||
func (r *Row) Scan(dest ...interface{}) error | ||
func (r *Row) SliceScan() ([]interface{}, error) | ||
func (r *Row) StructScan(dest interface{}) error | ||
|
||
* 单列结果集 | ||
|
||
# type Rows struct { | ||
*sql.Rows | ||
Mapper *reflectx.Mapper | ||
} | ||
func NamedQuery(e Ext, query string, arg interface{}) (*Rows, error) | ||
func NamedQueryContext(ctx context.Context, e ExtContext, query string, arg interface{}) (*Rows, error) | ||
func (r *Rows) MapScan(dest map[string]interface{}) error | ||
* 绑定结果集到MAP | ||
func (r *Rows) SliceScan() ([]interface{}, error) | ||
* 绑定结果集到切片 | ||
func (r *Rows) StructScan(dest interface{}) error | ||
* 绑定结果集到对象 | ||
|
||
# type Stmt struct { | ||
*sql.Stmt | ||
Mapper *reflectx.Mapper | ||
} | ||
func Preparex(p Preparer, query string) (*Stmt, error) | ||
func PreparexContext(ctx context.Context, p PreparerContext, query string) (*Stmt, error) | ||
func (s *Stmt) Get(dest interface{}, args ...interface{}) error | ||
func (s *Stmt) GetContext(ctx context.Context, dest interface{}, args ...interface{}) error | ||
func (s *Stmt) MustExec(args ...interface{}) sql.Result | ||
func (s *Stmt) MustExecContext(ctx context.Context, args ...interface{}) sql.Result | ||
func (s *Stmt) QueryRowx(args ...interface{}) *Row | ||
func (s *Stmt) QueryRowxContext(ctx context.Context, args ...interface{}) *Row | ||
func (s *Stmt) Queryx(args ...interface{}) (*Rows, error) | ||
func (s *Stmt) QueryxContext(ctx context.Context, args ...interface{}) (*Rows, error) | ||
func (s *Stmt) Select(dest interface{}, args ...interface{}) error | ||
func (s *Stmt) SelectContext(ctx context.Context, dest interface{}, args ...interface{}) error | ||
func (s *Stmt) Unsafe() *Stmt | ||
|
||
# type Tx struct { | ||
*sql.Tx | ||
Mapper *reflectx.Mapper | ||
} | ||
func (tx *Tx) BindNamed(query string, arg interface{}) (string, []interface{}, error) | ||
func (tx *Tx) DriverName() string | ||
func (tx *Tx) Get(dest interface{}, query string, args ...interface{}) error | ||
func (tx *Tx) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error | ||
func (tx *Tx) MustExec(query string, args ...interface{}) sql.Result | ||
func (tx *Tx) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result | ||
func (tx *Tx) NamedExec(query string, arg interface{}) (sql.Result, error) | ||
func (tx *Tx) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) | ||
func (tx *Tx) NamedQuery(query string, arg interface{}) (*Rows, error) | ||
func (tx *Tx) NamedStmt(stmt *NamedStmt) *NamedStmt | ||
func (tx *Tx) NamedStmtContext(ctx context.Context, stmt *NamedStmt) *NamedStmt | ||
func (tx *Tx) PrepareNamed(query string) (*NamedStmt, error) | ||
func (tx *Tx) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error) | ||
func (tx *Tx) Preparex(query string) (*Stmt, error) | ||
func (tx *Tx) PreparexContext(ctx context.Context, query string) (*Stmt, error) | ||
func (tx *Tx) QueryRowx(query string, args ...interface{}) *Row | ||
func (tx *Tx) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *Row | ||
func (tx *Tx) Queryx(query string, args ...interface{}) (*Rows, error) | ||
func (tx *Tx) QueryxContext(ctx context.Context, query string, args ...interface{}) (*Rows, error) | ||
func (tx *Tx) Rebind(query string) string | ||
func (tx *Tx) Select(dest interface{}, query string, args ...interface{}) error | ||
func (tx *Tx) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error | ||
func (tx *Tx) Stmtx(stmt interface{}) *Stmt | ||
func (tx *Tx) StmtxContext(ctx context.Context, stmt interface{}) *Stmt | ||
func (tx *Tx) Unsafe() *Tx | ||
|
||
|
||
|
||
------------------- | ||
func | ||
------------------- | ||
func BindDriver(driverName string, bindType int) | ||
func BindNamed(bindType int, query string, arg interface{}) (string, []interface{}, error) | ||
func BindType(driverName string) int | ||
func Get(q Queryer, dest interface{}, query string, args ...interface{}) error | ||
func GetContext(ctx context.Context, q QueryerContext, dest interface{}, query string, ...) error | ||
func In(query string, args ...interface{}) (string, []interface{}, error) | ||
func LoadFile(e Execer, path string) (*sql.Result, error) | ||
func LoadFileContext(ctx context.Context, e ExecerContext, path string) (*sql.Result, error) | ||
func MapScan(r ColScanner, dest map[string]interface{}) error | ||
func MustExec(e Execer, query string, args ...interface{}) sql.Result | ||
func MustExecContext(ctx context.Context, e ExecerContext, query string, args ...interface{}) sql.Result | ||
func Named(query string, arg interface{}) (string, []interface{}, error) | ||
func NamedExec(e Ext, query string, arg interface{}) (sql.Result, error) | ||
func NamedExecContext(ctx context.Context, e ExtContext, query string, arg interface{}) (sql.Result, error) | ||
func Rebind(bindType int, query string) string | ||
func Select(q Queryer, dest interface{}, query string, args ...interface{}) error | ||
func SelectContext(ctx context.Context, q QueryerContext, dest interface{}, query string, ...) error | ||
func SliceScan(r ColScanner) ([]interface{}, error) | ||
func StructScan(rows rowsi, dest interface{}) error |
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,4 @@ | ||
-------------------------- | ||
╡Ия╞ | ||
-------------------------- | ||
# ╣╔ап╡Ия╞ |
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,39 @@ | ||
-------------------------- | ||
sqlx | ||
-------------------------- | ||
# SQL | ||
sqlx是一个go语言包,在内置database/sql包之上增加了很多扩展,简化数据库操作代码的书写。 | ||
|
||
# 地址 | ||
http://jmoiron.github.io/sqlx/ | ||
https://github.com/jmoiron/sqlx | ||
|
||
|
||
# 数据库的初始化 | ||
func Connect(driverName, dataSourceName string) (*DB, error) | ||
func ConnectContext(ctx context.Context, driverName, dataSourceName string) (*DB, error) | ||
func MustConnect(driverName, dataSourceName string) *DB | ||
* 创建新的数据库,并且立即创建连接 | ||
|
||
func MustOpen(driverName, dataSourceName string) *DB | ||
func Open(driverName, dataSourceName string) (*DB, error) | ||
* 创建新得数据库,不立即创建连接 | ||
|
||
func NewDb(db *sql.DB, driverName string) *DB | ||
* 使用sql.DB创建数据库 | ||
|
||
* Demo | ||
db, err := sqlx.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", "root", "root1", "localhost", 3306, "demo")) | ||
if err != nil { | ||
log.Fatalf("创建数据库异常:err=%s\n", err.Error()) | ||
} | ||
if err := db.Ping(); err != nil { | ||
log.Fatalf("获取数据库连接异常:err=%s\n", err.Error()) | ||
} | ||
|
||
-------------------------- | ||
sqlx - 更新 | ||
-------------------------- | ||
|
||
func (db *DB) MustExec(query string, args ...interface{}) sql.Result | ||
func (db *DB) MustExecContext(ctx context.Context, query string, args ...interface{}) sql.Result |
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,83 @@ | ||
import java.io.BufferedReader; | ||
import java.io.BufferedWriter; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.UUID; | ||
|
||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
|
||
public class MainTest { | ||
|
||
public static void main(String[] args) throws Exception { | ||
write(); | ||
read(); | ||
} | ||
|
||
public static void write() throws Exception { | ||
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("D:\\sobig.json"), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) { | ||
|
||
try (JsonWriter jsonWriter = new JsonWriter(writer)) { | ||
|
||
// 设置缩进,提高可读性 | ||
jsonWriter.setIndent(" "); | ||
|
||
jsonWriter.beginObject(); // 输出:{ | ||
jsonWriter.name("users"); // 输出:"name" | ||
|
||
jsonWriter.beginArray(); // 输出:[ | ||
for (int i = 0; i < 10000; i++) { | ||
jsonWriter.beginObject(); // 输出:{ | ||
jsonWriter.name("name"); // 输出:"name" | ||
jsonWriter.value(UUID.randomUUID().toString()); // 输出:"{uuid}" | ||
jsonWriter.endObject(); // 输出:} | ||
} | ||
|
||
jsonWriter.endArray(); // 输出:] | ||
|
||
jsonWriter.endObject(); // 输出:} | ||
} | ||
} | ||
} | ||
|
||
public static void read() throws Exception { | ||
try (BufferedReader reader = Files.newBufferedReader(Paths.get("D:\\sobig.json"))) { | ||
try (JsonReader jsonReader = new JsonReader(reader)) { | ||
while (true) { | ||
JsonToken jsonToken = jsonReader.peek(); | ||
if (jsonToken == JsonToken.BEGIN_OBJECT) { | ||
System.out.println("开始解析对象"); | ||
jsonReader.beginObject(); | ||
} else if (jsonToken == JsonToken.END_OBJECT) { | ||
System.out.println("对象解析完毕"); | ||
jsonReader.endObject(); | ||
} else if (jsonToken == JsonToken.BEGIN_ARRAY) { | ||
System.out.println("开始解析数组"); | ||
jsonReader.beginArray(); | ||
} else if (jsonToken == JsonToken.END_ARRAY) { | ||
System.out.println("数组解析完毕"); | ||
jsonReader.endArray(); | ||
} else if (jsonToken == JsonToken.NAME) { | ||
System.out.println("解析到key:" + jsonReader.nextName()); | ||
} else if (jsonToken == JsonToken.STRING) { | ||
System.out.println("解析到string value:" + jsonReader.nextString()); | ||
} else if (jsonToken == JsonToken.NUMBER) { | ||
// jsonReader.nextDouble(); | ||
// jsonReader.nextLong(); | ||
System.out.println("解析到number vallue:" + jsonReader.nextInt()); | ||
} else if (jsonToken == JsonToken.BOOLEAN) { | ||
System.out.println("解析到bool value:" + jsonReader.nextBoolean()); | ||
} else if (jsonToken == JsonToken.NULL) { | ||
jsonReader.nextNull(); // void | ||
System.out.println("解析到null value: null"); | ||
} else if (jsonToken == JsonToken.END_DOCUMENT) { | ||
System.out.println("JSON解析完毕"); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.