|
3 | 3 | * All right reserved. |
4 | 4 | * @author xiongfa.li |
5 | 5 | * @version V1.0 |
6 | | - * Description: |
| 6 | + * Description: |
7 | 7 | */ |
8 | 8 |
|
9 | 9 | package connection |
10 | 10 |
|
11 | 11 | import ( |
12 | | - "context" |
13 | | - "database/sql" |
14 | | - "github.com/xfali/gobatis/common" |
15 | | - "github.com/xfali/gobatis/errors" |
16 | | - "github.com/xfali/gobatis/reflection" |
17 | | - "github.com/xfali/gobatis/statement" |
18 | | - "github.com/xfali/gobatis/util" |
| 12 | + "context" |
| 13 | + "database/sql" |
| 14 | + "github.com/xfali/gobatis/common" |
| 15 | + "github.com/xfali/gobatis/errors" |
| 16 | + "github.com/xfali/gobatis/reflection" |
| 17 | + "github.com/xfali/gobatis/statement" |
| 18 | + "github.com/xfali/gobatis/util" |
19 | 19 | ) |
20 | 20 |
|
21 | 21 | type MysqlConnection sql.DB |
22 | 22 | type MysqlStatement sql.Stmt |
23 | 23 |
|
24 | 24 | func (c *MysqlConnection) Prepare(sqlStr string) (statement.Statement, error) { |
25 | | - db := (*sql.DB)(c) |
26 | | - s, err := db.Prepare(sqlStr) |
27 | | - if err != nil { |
28 | | - return nil, errors.CONNECTION_PREPARE_ERROR |
29 | | - } |
30 | | - return (*MysqlStatement)(s), nil |
| 25 | + db := (*sql.DB)(c) |
| 26 | + s, err := db.Prepare(sqlStr) |
| 27 | + if err != nil { |
| 28 | + return nil, errors.CONNECTION_PREPARE_ERROR |
| 29 | + } |
| 30 | + return (*MysqlStatement)(s), nil |
31 | 31 | } |
32 | 32 |
|
33 | 33 | func (c *MysqlConnection) Query(ctx context.Context, result reflection.Object, sqlStr string, params ...interface{}) error { |
34 | | - db := (*sql.DB)(c) |
35 | | - rows, err := db.QueryContext(ctx, sqlStr, params...) |
36 | | - if err != nil { |
37 | | - return errors.STATEMENT_QUERY_ERROR |
38 | | - } |
39 | | - defer rows.Close() |
| 34 | + db := (*sql.DB)(c) |
| 35 | + rows, err := db.QueryContext(ctx, sqlStr, params...) |
| 36 | + if err != nil { |
| 37 | + return errors.STATEMENT_QUERY_ERROR |
| 38 | + } |
| 39 | + defer rows.Close() |
40 | 40 |
|
41 | | - util.ScanRows(rows, result) |
42 | | - return nil |
| 41 | + util.ScanRows(rows, result) |
| 42 | + return nil |
43 | 43 | } |
44 | 44 |
|
45 | 45 | func (c *MysqlConnection) Exec(ctx context.Context, sqlStr string, params ...interface{}) (common.Result, error) { |
46 | | - db := (*sql.DB)(c) |
47 | | - return db.ExecContext(ctx, sqlStr, params...) |
| 46 | + db := (*sql.DB)(c) |
| 47 | + return db.ExecContext(ctx, sqlStr, params...) |
48 | 48 | } |
49 | 49 |
|
50 | 50 | func (s *MysqlStatement) Query(ctx context.Context, result reflection.Object, params ...interface{}) error { |
51 | | - stmt := (*sql.Stmt)(s) |
52 | | - rows, err := stmt.QueryContext(ctx, params...) |
53 | | - if err != nil { |
54 | | - return errors.STATEMENT_QUERY_ERROR |
55 | | - } |
56 | | - defer rows.Close() |
| 51 | + stmt := (*sql.Stmt)(s) |
| 52 | + rows, err := stmt.QueryContext(ctx, params...) |
| 53 | + if err != nil { |
| 54 | + return errors.STATEMENT_QUERY_ERROR |
| 55 | + } |
| 56 | + defer rows.Close() |
57 | 57 |
|
58 | | - util.ScanRows(rows, result) |
59 | | - return nil |
| 58 | + util.ScanRows(rows, result) |
| 59 | + return nil |
60 | 60 | } |
61 | 61 |
|
62 | 62 | func (s *MysqlStatement) Exec(ctx context.Context, params ...interface{}) (common.Result, error) { |
63 | | - stmt := (*sql.Stmt)(s) |
64 | | - return stmt.ExecContext(ctx, params...) |
| 63 | + stmt := (*sql.Stmt)(s) |
| 64 | + return stmt.ExecContext(ctx, params...) |
65 | 65 | } |
66 | 66 |
|
67 | 67 | func (s *MysqlStatement) Close() { |
68 | | - stmt := (*sql.Stmt)(s) |
69 | | - stmt.Close() |
| 68 | + stmt := (*sql.Stmt)(s) |
| 69 | + stmt.Close() |
70 | 70 | } |
0 commit comments