|
@@ -70,14 +70,6 @@ func (xgConn *xuguConn) Close() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-func (xgConn *xuguConn) Exec(sql string,
|
|
|
- args []driver.Value) (driver.Result, error) {
|
|
|
- gt("xuguConn.Exec")
|
|
|
- defer gt("xuguConn.Exec end")
|
|
|
-
|
|
|
- return nil, nil
|
|
|
-}
|
|
|
-
|
|
|
func (xgConn *xuguConn) Query(sql string,
|
|
|
args []driver.Value) (driver.Rows, error) {
|
|
|
gt("xuguConn.Query")
|
|
@@ -85,6 +77,7 @@ func (xgConn *xuguConn) Query(sql string,
|
|
|
|
|
|
xgConn.mu.Lock()
|
|
|
defer xgConn.mu.Unlock()
|
|
|
+
|
|
|
// 检测sql语句不是查询则报错
|
|
|
if switchSQLType(sql) != SQL_SELECT {
|
|
|
return nil, errors.New("The executed SQL statement is not a SELECT")
|
|
@@ -129,6 +122,7 @@ func (xgConn *xuguConn) Query(sql string,
|
|
|
|
|
|
switch aR.rt {
|
|
|
case selectResult:
|
|
|
+ fmt.Printf("selectResult %#v\n", aR.s)
|
|
|
rows := &xuguRows{
|
|
|
rows_conn: xgConn,
|
|
|
results: aR.s,
|
|
@@ -155,6 +149,7 @@ func (xgConn *xuguConn) Ping(ctx context.Context) error {
|
|
|
|
|
|
//send
|
|
|
xgConn.mu.Lock()
|
|
|
+ defer xgConn.mu.Unlock()
|
|
|
sockSendPutStatement(xgConn, []byte("select count(*) from dual;"), nil, 0)
|
|
|
sockSendExecute(xgConn)
|
|
|
|
|
@@ -164,7 +159,7 @@ func (xgConn *xuguConn) Ping(ctx context.Context) error {
|
|
|
return err
|
|
|
}
|
|
|
xgConn.readBuff.reset()
|
|
|
- xgConn.mu.Unlock()
|
|
|
+
|
|
|
return nil
|
|
|
}
|
|
|
|
|
@@ -201,7 +196,9 @@ func (xgConn *xuguConn) Prepare(sql string) (driver.Stmt, error) {
|
|
|
paramCount: count,
|
|
|
mysql: sql,
|
|
|
}
|
|
|
- stmt.prename = []byte(xgConn.prepareName)
|
|
|
+ stmt.prename = []byte(fmt.Sprintf("? %s", xgConn.prepareName))
|
|
|
+
|
|
|
+ fmt.Println("stmt.prename ", stmt.prename, string(stmt.prename))
|
|
|
return stmt, nil
|
|
|
}
|
|
|
|
|
@@ -211,6 +208,7 @@ func xuguPrepare(pConn *xuguConn, cmd_sql string, prepareName string) error {
|
|
|
prepareName = fmt.Sprintf("%s%d", prepareName, pConn.prepareNo)
|
|
|
sqlRet := fmt.Sprintf("PREPARE %s AS %s", prepareName, cmd_sql)
|
|
|
pConn.prepareName = prepareName
|
|
|
+ fmt.Println("pConn.prepareName ", pConn.prepareName)
|
|
|
pConn.prepareNo++
|
|
|
//send msg
|
|
|
sockSendPutStatement(pConn, []byte(sqlRet), nil, 0)
|
|
@@ -229,3 +227,128 @@ func xuguPrepare(pConn *xuguConn, cmd_sql string, prepareName string) error {
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+func (xgConn *xuguConn) Exec(sql string,
|
|
|
+ args []driver.Value) (driver.Result, error) {
|
|
|
+ gt("xuguConn.Exec")
|
|
|
+ defer gt("xuguConn.Exec end")
|
|
|
+ xgConn.mu.Lock()
|
|
|
+ defer xgConn.mu.Unlock()
|
|
|
+
|
|
|
+ // 检测sql语句是查询则报错
|
|
|
+ if switchSQLType(sql) == SQL_SELECT {
|
|
|
+ return nil, errors.New("The executed SQL statement is a SELECT")
|
|
|
+ }
|
|
|
+
|
|
|
+ // 有传进来的参数
|
|
|
+ if len(args) != 0 {
|
|
|
+ values := []xuguValue{}
|
|
|
+ //判断类型
|
|
|
+ for _, param := range args {
|
|
|
+ err := assertParamType(param, &values)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //send msg
|
|
|
+ if err := sockSendPutStatement(xgConn, []byte(sql), &values, len(args)); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := sockSendExecute(xgConn); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ //send msg
|
|
|
+ if err := sockSendPutStatement(xgConn, []byte(sql), nil, len(args)); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := sockSendExecute(xgConn); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ //recv msg
|
|
|
+ aR, err := xuguSockRecvMsg(xgConn)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ switch aR.rt {
|
|
|
+ case selectResult:
|
|
|
+
|
|
|
+ return nil, errors.New("exec is Query error")
|
|
|
+ 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 (xgConn *xuguConn) exec(sql string, args []driver.Value) (driver.Result, error) {
|
|
|
+ // 有传进来的参数
|
|
|
+ if len(args) != 0 {
|
|
|
+ values := []xuguValue{}
|
|
|
+ //判断类型
|
|
|
+ for _, param := range args {
|
|
|
+ err := assertParamType(param, &values)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //send msg
|
|
|
+ if err := sockSendPutStatement(xgConn, []byte(sql), &values, len(args)); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := sockSendExecute(xgConn); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+
|
|
|
+ //send msg
|
|
|
+ if err := sockSendPutStatement(xgConn, []byte(sql), nil, len(args)); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := sockSendExecute(xgConn); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //recv msg
|
|
|
+ aR, err := xuguSockRecvMsg(xgConn)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ switch aR.rt {
|
|
|
+ case selectResult:
|
|
|
+
|
|
|
+ return nil, errors.New("exec is Query error")
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+}
|