示例#1
0
func main() {
	conn, err := net.Dial(_NET, _ADDRESS)
	if err != nil {
		panic(err)
	}

	tr := transporter.NewHexTransporter(conn)
	pr := transporter.NewProtocoler()
	pk := transporter.NewPackager(tr, pr)
	defer pk.Close()

	termReader := bufio.NewReader(os.Stdin)
	for {
		fmt.Print(":> ")
		line, err := termReader.ReadBytes('\n')
		stat := line[:len(line)-1]
		if err != nil {
			fmt.Println(err)
		}
		if string(stat) == "clear" {
			for i := 0; i < 80; i++ {
				fmt.Println()
			}
			continue
		}
		if string(stat) == "exit" || string(stat) == "quit" {
			break
		}

		pkg := transporter.NewPackage([]byte(stat), nil)

		err = pk.Send(pkg)
		if err != nil {
			utils.Info(err)
			break
		}

		rpkg, err := pk.Receive()
		if err != nil {
			utils.Info(err)
			break
		}

		if rpkg.Err() != nil {
			fmt.Println(rpkg.Err())
		} else {
			fmt.Println(string(rpkg.Data()))
		}
	}
}
示例#2
0
func (e *executor) Execute(sql []byte) ([]byte, error) {
	utils.Info("Execute: ", string(sql))

	stat, err := parser.Parse(sql)
	if err != nil {
		return nil, err
	}

	var result []byte
	switch st := stat.(type) {
	case *statement.Begin:
		if e.xid != 0 {
			return nil, ErrNoNestedTransaction
		}
		e.xid, result = e.tbm.Begin(st)
		return result, nil
	case *statement.Commit:
		if e.xid == 0 {
			return nil, ErrNotInAnyTransaction
		}
		result, err = e.tbm.Commit(e.xid)
		if err != nil {
			return nil, err
		}
		e.xid = 0
		return result, nil
	case *statement.Abort:
		if e.xid == 0 {
			return nil, ErrNotInAnyTransaction
		}
		result = e.tbm.Abort(e.xid)
		e.xid = 0
		return result, nil
	default:
		return e.execute2(st)
	}
}
示例#3
0
func (e *executor) Close() {
	if e.xid != 0 {
		utils.Info("Abnormal Abort: ", e.xid)
		e.tbm.Abort(e.xid)
	}
}