123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- package xugu
- import (
- "database/sql/driver"
- "errors"
- )
- type xuguStmt struct {
-
- stmt_conn *xuguConn
-
- prepared bool
-
- prename []byte
-
- curopend bool
-
- curname []byte
-
- paramCount int
- mysql string
-
-
- }
- func (stmt *xuguStmt) Close() error {
-
- err := xuguUnPrepare(stmt.stmt_conn, stmt.stmt_conn.prepareName)
-
- return err
- }
- func (stmt *xuguStmt) NumInput() int {
- return assertParamCount(stmt.mysql)
-
- }
- func (stmt *xuguStmt) Exec(args []driver.Value) (driver.Result, error) {
-
- stmt.stmt_conn.mu.Lock()
- defer stmt.stmt_conn.mu.Unlock()
-
-
- if stmt.paramCount > 0 && len(args) > 0 {
- values := []xuguValue{}
- for _, param := range args {
- assertParamType(param, &values)
- }
- sockSendPutStatement(stmt.stmt_conn, stmt.prename, &values, stmt.paramCount)
- sockSendExecute(stmt.stmt_conn)
-
- } else {
- sockSendPutStatement(stmt.stmt_conn, []byte(stmt.mysql), nil, 0)
- sockSendExecute(stmt.stmt_conn)
- }
-
- aR, err := xuguSockRecvMsg(stmt.stmt_conn)
- if err != nil {
- return nil, err
- }
- switch aR.rt {
- case selectResult:
- return nil, errors.New("exec is Query error")
- case updateResult:
- return &xuguResult{affectedRows: int64(aR.u.UpdateNum), insertId: int64(0)}, nil
- case insertResult:
- return &xuguResult{affectedRows: int64(0), insertId: int64(aR.i.RowidLen)}, nil
- case errInfo:
- return nil, errors.New(string(aR.e.ErrStr))
- case warnInfo:
- return nil, errors.New(string(aR.w.WarnStr))
- default:
- return &xuguResult{
- affectedRows: int64(0),
- insertId: int64(0),
- }, nil
- }
- }
- func (stmt *xuguStmt) Query(args []driver.Value) (driver.Rows, error) {
-
- stmt.stmt_conn.mu.Lock()
- defer stmt.stmt_conn.mu.Unlock()
-
-
- if stmt.paramCount > 0 && len(args) > 0 {
- values := []xuguValue{}
- for _, param := range args {
- assertParamType(param, &values)
- }
- sockSendPutStatement(stmt.stmt_conn, stmt.prename, &values, stmt.paramCount)
- sockSendExecute(stmt.stmt_conn)
-
- } else {
- sockSendPutStatement(stmt.stmt_conn, []byte(stmt.mysql), nil, 0)
- sockSendExecute(stmt.stmt_conn)
- }
-
- aR, err := xuguSockRecvMsg(stmt.stmt_conn)
- if err != nil {
- return nil, err
- }
- switch aR.rt {
- case selectResult:
- rows := &xuguRows{
- rows_conn: stmt.stmt_conn,
- results: aR.s,
- colIdx: 0,
- prepared: false,
- }
- return rows, nil
- case errInfo:
- return nil, errors.New(string(aR.e.ErrStr))
- case warnInfo:
- return nil, errors.New(string(aR.w.WarnStr))
- default:
- }
- return nil, errors.New("xugu Query error")
- }
|