當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Result_Row.Values方法代碼示例

本文整理匯總了Golang中github.com/cockroachdb/cockroach/sql/driver.Result_Row.Values方法的典型用法代碼示例。如果您正苦於以下問題:Golang Result_Row.Values方法的具體用法?Golang Result_Row.Values怎麽用?Golang Result_Row.Values使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/cockroachdb/cockroach/sql/driver.Result_Row的用法示例。


在下文中一共展示了Result_Row.Values方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: exec

// exec executes the request. Any error encountered is returned; it is
// the caller's responsibility to update the response.
func (s *Server) exec(req driver.Request) (driver.Response, error) {
	var resp driver.Response

	// Pick up current session state.
	planner := planner{db: s.db}
	if req.Session != nil {
		// TODO(tschottdorf) will have to validate the Session information (for
		// instance, whether access to the stored database is permitted).
		if err := gogoproto.Unmarshal(req.Session, &planner.session); err != nil {
			return resp, err
		}
	}
	stmts, err := parser.Parse(req.Sql)
	if err != nil {
		return resp, err
	}
	for _, stmt := range stmts {
		// Bind all the placeholder variables in the stmt to actual values.
		if err := parser.FillArgs(stmt, parameters(req.Params)); err != nil {
			return resp, err
		}
		var plan planNode
		if plan, err = planner.makePlan(stmt); err != nil {
			return resp, err
		}

		result := driver.Result{
			Columns: plan.Columns(),
		}
		for plan.Next() {
			values := plan.Values()
			row := driver.Result_Row{}
			row.Values = make([]driver.Datum, 0, len(values))
			for _, val := range values {
				switch vt := val.(type) {
				case parser.DBool:
					row.Values = append(row.Values, driver.Datum{BoolVal: (*bool)(&vt)})
				case parser.DInt:
					row.Values = append(row.Values, driver.Datum{IntVal: (*int64)(&vt)})
				case parser.DFloat:
					row.Values = append(row.Values, driver.Datum{FloatVal: (*float64)(&vt)})
				case parser.DString:
					row.Values = append(row.Values, driver.Datum{StringVal: (*string)(&vt)})
				case parser.DNull:
					row.Values = append(row.Values, driver.Datum{})
				default:
					return resp, util.Errorf("unsupported datum: %T", val)
				}
			}
			result.Rows = append(result.Rows, row)
		}
		resp.Results = append(resp.Results, result)
	}

	// Update session state.
	resp.Session, err = gogoproto.Marshal(&planner.session)
	return resp, err
}
開發者ID:arypurnomoz,項目名稱:cockroach,代碼行數:60,代碼來源:server.go

示例2: execStmt

func (s server) execStmt(stmt parser.Statement, params parameters, planMaker *planner) (driver.Result, error) {
	var result driver.Result
	if planMaker.txn == nil {
		if _, ok := stmt.(*parser.BeginTransaction); ok {
			// Start a transaction here and not in planMaker to prevent begin
			// transaction from being called within an auto-transaction below.
			planMaker.txn = client.NewTxn(s.db)
			planMaker.txn.SetDebugName("sql", 0)
		}
	} else if planMaker.txn.Proto.Status == proto.ABORTED {
		switch stmt.(type) {
		case *parser.CommitTransaction, *parser.RollbackTransaction:
			// Reset to allow starting a new transaction.
			planMaker.txn = nil
			return result, nil
		default:
			return result, errTransactionAborted
		}
	}
	// Bind all the placeholder variables in the stmt to actual values.
	if err := parser.FillArgs(stmt, params); err != nil {
		return result, err
	}
	var plan planNode
	// If there is a pending transaction.
	if planMaker.txn != nil {
		// Run in transaction planMaker.txn
		var err error
		if plan, err = planMaker.makePlan(stmt); err != nil {
			return result, err
		}
	} else {
		// No transaction. Run the command as a retryable block in an
		// auto-transaction.
		if err := s.db.Txn(func(txn *client.Txn) error {
			planMaker.txn = txn
			var err error
			plan, err = planMaker.makePlan(stmt)
			planMaker.txn = nil
			return err
		}); err != nil {
			return result, err
		}
	}
	result.Columns = plan.Columns()
	for plan.Next() {
		values := plan.Values()
		row := driver.Result_Row{Values: make([]driver.Datum, 0, len(values))}
		for _, val := range values {
			if val == parser.DNull {
				row.Values = append(row.Values, driver.Datum{})
			} else {
				switch vt := val.(type) {
				case parser.DBool:
					row.Values = append(row.Values, driver.Datum{BoolVal: (*bool)(&vt)})
				case parser.DInt:
					row.Values = append(row.Values, driver.Datum{IntVal: (*int64)(&vt)})
				case parser.DFloat:
					row.Values = append(row.Values, driver.Datum{FloatVal: (*float64)(&vt)})
				case parser.DString:
					row.Values = append(row.Values, driver.Datum{StringVal: (*string)(&vt)})
				case parser.DDate:
					s := vt.String()
					row.Values = append(row.Values, driver.Datum{StringVal: &s})
				case parser.DTimestamp:
					s := vt.String()
					row.Values = append(row.Values, driver.Datum{StringVal: &s})
				case parser.DInterval:
					s := vt.String()
					row.Values = append(row.Values, driver.Datum{StringVal: &s})
				default:
					return result, util.Errorf("unsupported datum: %T", val)
				}
			}
		}
		result.Rows = append(result.Rows, row)
	}
	if err := plan.Err(); err != nil {
		return result, err
	}
	return result, nil
}
開發者ID:slavau,項目名稱:cockroach,代碼行數:82,代碼來源:server.go

示例3: execStmt

func (e Executor) execStmt(stmt parser.Statement, params parameters, planMaker *planner) (driver.Result, error) {
	var result driver.Result
	switch stmt.(type) {
	case *parser.BeginTransaction:
		if planMaker.txn != nil {
			return result, errTransactionInProgress
		}
		// Start a transaction here and not in planMaker to prevent begin
		// transaction from being called within an auto-transaction below.
		planMaker.txn = client.NewTxn(e.db)
		planMaker.txn.SetDebugName("sql", 0)
	case *parser.CommitTransaction, *parser.RollbackTransaction:
		if planMaker.txn != nil {
			if planMaker.txn.Proto.Status == proto.ABORTED {
				// Reset to allow starting a new transaction.
				planMaker.txn = nil
				return result, nil
			}
		} else {
			return result, errNoTransactionInProgress
		}
	case *parser.SetTransaction:
		if planMaker.txn == nil {
			return result, errNoTransactionInProgress
		}
	default:
		if planMaker.txn != nil && planMaker.txn.Proto.Status == proto.ABORTED {
			return result, errTransactionAborted
		}
	}

	// Bind all the placeholder variables in the stmt to actual values.
	if err := parser.FillArgs(stmt, params); err != nil {
		return result, err
	}

	// Create a function which both makes and executes the plan, populating
	// result.
	//
	// TODO(pmattis): Should this be a separate function? Perhaps we should move
	// some of the common code back out into execStmts and have execStmt contain
	// only the body of this closure.
	f := func() error {
		plan, err := planMaker.makePlan(stmt)
		if err != nil {
			return err
		}

		result.Columns = plan.Columns()
		for plan.Next() {
			values := plan.Values()
			row := driver.Result_Row{Values: make([]driver.Datum, 0, len(values))}
			for _, val := range values {
				if val == parser.DNull {
					row.Values = append(row.Values, driver.Datum{})
				} else {
					switch vt := val.(type) {
					case parser.DBool:
						row.Values = append(row.Values, driver.Datum{
							Payload: &driver.Datum_BoolVal{BoolVal: bool(vt)},
						})
					case parser.DInt:
						row.Values = append(row.Values, driver.Datum{
							Payload: &driver.Datum_IntVal{IntVal: int64(vt)},
						})
					case parser.DFloat:
						row.Values = append(row.Values, driver.Datum{
							Payload: &driver.Datum_FloatVal{FloatVal: float64(vt)},
						})
					case parser.DBytes:
						row.Values = append(row.Values, driver.Datum{
							Payload: &driver.Datum_BytesVal{BytesVal: []byte(vt)},
						})
					case parser.DString:
						row.Values = append(row.Values, driver.Datum{
							Payload: &driver.Datum_StringVal{StringVal: string(vt)},
						})
					case parser.DDate:
						row.Values = append(row.Values, driver.Datum{
							Payload: &driver.Datum_TimeVal{
								TimeVal: &driver.Datum_Timestamp{
									Sec:  vt.Unix(),
									Nsec: uint32(vt.Nanosecond()),
								},
							},
						})
					case parser.DTimestamp:
						row.Values = append(row.Values, driver.Datum{
							Payload: &driver.Datum_TimeVal{
								TimeVal: &driver.Datum_Timestamp{
									Sec:  vt.Unix(),
									Nsec: uint32(vt.Nanosecond()),
								},
							},
						})
					case parser.DInterval:
						row.Values = append(row.Values, driver.Datum{
							Payload: &driver.Datum_StringVal{StringVal: vt.String()},
						})
					default:
//.........這裏部分代碼省略.........
開發者ID:harryyeh,項目名稱:cockroach,代碼行數:101,代碼來源:executor.go

示例4: exec

// exec executes the request. Any error encountered is returned; it is
// the caller's responsibility to update the response.
func (s *Server) exec(req driver.Request) (resp driver.Response, err error) {

	// Pick up current session state.
	// The request user is validated in ServeHTTP. Even in insecure mode,
	// it is guaranteed not to be empty.
	planMaker := planner{user: req.GetUser()}
	defer func() {
		// Update session state even if an error occurs.
		if bytes, err := gogoproto.Marshal(&planMaker.session); err != nil {
			// Marshaling a `Session` never errors (known from reading the code).
			panic(err)
		} else {
			resp.Session = bytes
		}
	}()

	if req.Session != nil {
		// TODO(tschottdorf) will have to validate the Session information (for
		// instance, whether access to the stored database is permitted).
		if err = gogoproto.Unmarshal(req.Session, &planMaker.session); err != nil {
			return
		}
	}
	var stmts parser.StatementList
	if stmts, err = parser.Parse(req.Sql); err != nil {
		return
	}
	for _, stmt := range stmts {
		// Bind all the placeholder variables in the stmt to actual values.
		if err = parser.FillArgs(stmt, parameters(req.Params)); err != nil {
			return
		}
		var plan planNode
		if err = s.db.Txn(func(txn *client.Txn) error {
			planMaker.txn = txn
			plan, err = planMaker.makePlan(stmt)
			planMaker.txn = nil
			return err
		}); err != nil {
			return
		}

		result := driver.Result{
			Columns: plan.Columns(),
		}
		for plan.Next() {
			values := plan.Values()
			row := driver.Result_Row{}
			row.Values = make([]driver.Datum, 0, len(values))
			for _, val := range values {
				if val == parser.DNull {
					row.Values = append(row.Values, driver.Datum{})
				} else {
					switch vt := val.(type) {
					case parser.DBool:
						row.Values = append(row.Values, driver.Datum{BoolVal: (*bool)(&vt)})
					case parser.DInt:
						row.Values = append(row.Values, driver.Datum{IntVal: (*int64)(&vt)})
					case parser.DFloat:
						row.Values = append(row.Values, driver.Datum{FloatVal: (*float64)(&vt)})
					case parser.DString:
						row.Values = append(row.Values, driver.Datum{StringVal: (*string)(&vt)})
					default:
						err = util.Errorf("unsupported datum: %T", val)
						return
					}
				}
			}
			result.Rows = append(result.Rows, row)
		}
		if err = plan.Err(); err != nil {
			return
		}

		resp.Results = append(resp.Results, result)
	}

	return
}
開發者ID:shitfSign,項目名稱:cockroach,代碼行數:81,代碼來源:server.go


注:本文中的github.com/cockroachdb/cockroach/sql/driver.Result_Row.Values方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。