func (c *Conn) dispatch(data []byte) error { cmd := data[0] data = data[1:] switch cmd { case COM_QUERY: return c.handleQuery(hack.String(data)) case COM_PING: return c.writeOK(nil) case COM_INIT_DB: if err := c.useDB(hack.String(data)); err != nil { return err } else { return c.writeOK(nil) } // case COM_STMT_PREPARE: // return c.handleStmtPrepare(data) // case COM_STMT_EXECUTE: // return c.handleStmtExecute(data) // case COM_STMT_CLOSE: // return c.handleStmtClose(data) // case COM_STMT_SEND_LONG_DATA: // return c.handleStmtSendLongData(data) // case COM_STMT_RESET: // return c.handleStmtReset(data) default: msg := fmt.Sprintf("command %d not supported now", cmd) return NewError(ER_UNKNOWN_ERROR, msg) } return nil }
func (c *Conn) limitSelectResult(r *Resultset, stmt *sqlparser.Select) error { if stmt.Limit == nil { return nil } var offset, count int64 var err error if stmt.Limit.Offset == nil { offset = 0 } else { if o, ok := stmt.Limit.Offset.(sqlparser.NumVal); !ok { return fmt.Errorf("invalid select limit %s", nstring(stmt.Limit)) } else { if offset, err = strconv.ParseInt(hack.String([]byte(o)), 10, 64); err != nil { return err } } } if o, ok := stmt.Limit.Rowcount.(sqlparser.NumVal); !ok { return fmt.Errorf("invalid limit %s", nstring(stmt.Limit)) } else { if count, err = strconv.ParseInt(hack.String([]byte(o)), 10, 64); err != nil { return err } else if count < 0 { return fmt.Errorf("invalid limit %s", nstring(stmt.Limit)) } } if offset+count > int64(len(r.Values)) { count = int64(len(r.Values)) - offset } r.Values = r.Values[offset : offset+count] r.RowDatas = r.RowDatas[offset : offset+count] return nil }
func EncodeValue(value interface{}) string { switch val := value.(type) { case int: return Uint64Key(val).String() case uint64: return Uint64Key(val).String() case int64: return Uint64Key(val).String() case string: return val case []byte: return hack.String(val) } panic(NewKeyError("Unexpected key variable type %T", value)) }
func (r *Resultset) GetString(row, column int) (string, error) { d, err := r.GetValue(row, column) if err != nil { return "", err } switch v := d.(type) { case string: return v, nil case []byte: return hack.String(v), nil case int64: return strconv.FormatInt(v, 10), nil case uint64: return strconv.FormatUint(v, 10), nil case float64: return strconv.FormatFloat(v, 'f', -1, 64), nil case nil: return "", nil default: return "", fmt.Errorf("data type is %T", v) } }
func NumValue(value interface{}) int64 { switch val := value.(type) { case int: return int64(val) case uint64: return int64(val) case int64: return int64(val) case string: if v, err := strconv.ParseInt(val, 10, 64); err != nil { panic(NewKeyError("invalid num format %s", v)) } else { return v } case []byte: if v, err := strconv.ParseInt(hack.String(val), 10, 64); err != nil { panic(NewKeyError("invalid num format %s", v)) } else { return v } } panic(NewKeyError("Unexpected key variable type %T", value)) }
// String returns the raw value as a string func (v Value) String() string { if v.Inner == nil { return "" } return hack.String(v.Inner.raw()) }