本文整理汇总了Golang中github.com/cockroachdb/cockroach/sql/driver.Timestamp函数的典型用法代码示例。如果您正苦于以下问题:Golang Timestamp函数的具体用法?Golang Timestamp怎么用?Golang Timestamp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Timestamp函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ExecuteStatements
// ExecuteStatements executes the given statement(s) and returns a response.
// On error, the returned integer is an HTTP error code.
func (e *Executor) ExecuteStatements(user string, session Session, stmts string, params []parser.Datum) (Response, int, error) {
planMaker := plannerPool.Get().(*planner)
defer plannerPool.Put(planMaker)
*planMaker = planner{
user: user,
evalCtx: parser.EvalContext{
NodeID: e.nodeID,
ReCache: e.reCache,
// Copy existing GetLocation closure. See plannerPool.New() for the
// initial setting.
GetLocation: planMaker.evalCtx.GetLocation,
},
leaseMgr: e.leaseMgr,
systemConfig: e.getSystemConfig(),
session: session,
}
// Resume a pending transaction if present.
if planMaker.session.Txn != nil {
txn := client.NewTxn(e.db)
txn.Proto = planMaker.session.Txn.Txn
txn.UserPriority = planMaker.session.Txn.UserPriority
if planMaker.session.MutatesSystemConfig {
txn.SetSystemConfigTrigger()
}
planMaker.setTxn(txn, planMaker.session.Txn.Timestamp.GoTime())
}
// Send the Request for SQL execution and set the application-level error
// for each result in the reply.
planMaker.params = parameters(params)
reply := e.execStmts(stmts, planMaker)
// Send back the session state even if there were application-level errors.
// Add transaction to session state.
if planMaker.txn != nil {
// TODO(pmattis): Need to record the leases used by a transaction within
// the transaction state and restore it when the transaction is restored.
planMaker.releaseLeases(e.db)
planMaker.session.Txn = &Session_Transaction{
Txn: planMaker.txn.Proto,
Timestamp: driver.Timestamp(planMaker.evalCtx.TxnTimestamp.Time),
UserPriority: planMaker.txn.UserPriority,
}
planMaker.session.MutatesSystemConfig = planMaker.txn.SystemConfigTrigger()
} else {
planMaker.session.Txn = nil
planMaker.session.MutatesSystemConfig = false
}
bytes, err := proto.Marshal(&planMaker.session)
if err != nil {
return Response{}, http.StatusInternalServerError, err
}
reply.Session = bytes
return reply, 0, nil
}
示例2: Execute
// Execute the statement(s) in the given request and return a response.
// On error, the returned integer is an HTTP error code.
func (e *Executor) Execute(args driver.Request) (driver.Response, int, error) {
planMaker := plannerPool.Get().(*planner)
defer plannerPool.Put(planMaker)
*planMaker = planner{
user: args.GetUser(),
evalCtx: parser.EvalContext{
NodeID: e.nodeID,
ReCache: e.reCache,
// Copy existing GetLocation closure. See plannerPool.New() for the
// initial setting.
GetLocation: planMaker.evalCtx.GetLocation,
},
leaseMgr: e.leaseMgr,
systemConfig: e.getSystemConfig(),
}
// Pick up current session state.
if err := proto.Unmarshal(args.Session, &planMaker.session); err != nil {
return args.CreateReply(), http.StatusBadRequest, err
}
// Resume a pending transaction if present.
if planMaker.session.Txn != nil {
txn := client.NewTxn(e.db)
txn.Proto = planMaker.session.Txn.Txn
if planMaker.session.MutatesSystemDB {
txn.SetSystemDBTrigger()
}
planMaker.setTxn(txn, planMaker.session.Txn.Timestamp.GoTime())
}
// Send the Request for SQL execution and set the application-level error
// for each result in the reply.
planMaker.params = parameters(args.Params)
reply := e.execStmts(args.Sql, planMaker)
// Send back the session state even if there were application-level errors.
// Add transaction to session state.
if planMaker.txn != nil {
planMaker.session.Txn = &Session_Transaction{Txn: planMaker.txn.Proto, Timestamp: driver.Timestamp(planMaker.evalCtx.TxnTimestamp.Time)}
planMaker.session.MutatesSystemDB = planMaker.txn.SystemDBTrigger()
} else {
planMaker.session.Txn = nil
planMaker.session.MutatesSystemDB = false
}
bytes, err := proto.Marshal(&planMaker.session)
if err != nil {
return args.CreateReply(), http.StatusInternalServerError, err
}
reply.Session = bytes
return reply, 0, nil
}
示例3: protoFromDatum
func protoFromDatum(datum parser.Datum) driver.Datum {
if datum == parser.DNull {
return driver.Datum{}
}
switch vt := datum.(type) {
case parser.DBool:
return driver.Datum{
Payload: &driver.Datum_BoolVal{BoolVal: bool(vt)},
}
case parser.DInt:
return driver.Datum{
Payload: &driver.Datum_IntVal{IntVal: int64(vt)},
}
case parser.DFloat:
return driver.Datum{
Payload: &driver.Datum_FloatVal{FloatVal: float64(vt)},
}
case *parser.DDecimal:
return driver.Datum{
Payload: &driver.Datum_DecimalVal{DecimalVal: vt.Dec.String()},
}
case parser.DBytes:
return driver.Datum{
Payload: &driver.Datum_BytesVal{BytesVal: []byte(vt)},
}
case parser.DString:
return driver.Datum{
Payload: &driver.Datum_StringVal{StringVal: string(vt)},
}
case parser.DDate:
return driver.Datum{
Payload: &driver.Datum_DateVal{DateVal: int64(vt)},
}
case parser.DTimestamp:
wireTimestamp := driver.Timestamp(vt.Time)
return driver.Datum{
Payload: &driver.Datum_TimeVal{
TimeVal: &wireTimestamp,
},
}
case parser.DInterval:
return driver.Datum{
Payload: &driver.Datum_IntervalVal{IntervalVal: vt.Nanoseconds()},
}
default:
panic(util.Errorf("unsupported result type: %s", datum.Type()))
}
}
示例4: makeDriverDatum
func makeDriverDatum(datum parser.Datum) (driver.Datum, error) {
if datum == parser.DNull {
return driver.Datum{}, nil
}
switch vt := datum.(type) {
case parser.DBool:
return driver.Datum{
Payload: &driver.Datum_BoolVal{BoolVal: bool(vt)},
}, nil
case parser.DInt:
return driver.Datum{
Payload: &driver.Datum_IntVal{IntVal: int64(vt)},
}, nil
case parser.DFloat:
return driver.Datum{
Payload: &driver.Datum_FloatVal{FloatVal: float64(vt)},
}, nil
case parser.DBytes:
return driver.Datum{
Payload: &driver.Datum_BytesVal{BytesVal: []byte(vt)},
}, nil
case parser.DString:
return driver.Datum{
Payload: &driver.Datum_StringVal{StringVal: string(vt)},
}, nil
case parser.DDate:
return driver.Datum{
Payload: &driver.Datum_DateVal{DateVal: int64(vt)},
}, nil
case parser.DTimestamp:
wireTimestamp := driver.Timestamp(vt.Time)
return driver.Datum{
Payload: &driver.Datum_TimeVal{
TimeVal: &wireTimestamp,
},
}, nil
case parser.DInterval:
return driver.Datum{
Payload: &driver.Datum_IntervalVal{IntervalVal: vt.Nanoseconds()},
}, nil
default:
return driver.Datum{}, fmt.Errorf("unsupported result type: %s", datum.Type())
}
}
示例5: execStmt
func (e *Executor) execStmt(stmt parser.Statement, params parameters, planMaker *planner) (driver.Response_Result, error) {
var result driver.Response_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.setTxn(client.NewTxn(e.db), time.Now())
planMaker.txn.SetDebugName("sql", 0)
case *parser.CommitTransaction, *parser.RollbackTransaction:
if planMaker.txn == nil {
return result, errNoTransactionInProgress
} else if planMaker.txn.Proto.Status == roachpb.ABORTED {
// Reset to allow starting a new transaction.
planMaker.resetTxn()
return result, nil
}
case *parser.SetTransaction:
if planMaker.txn == nil {
return result, errNoTransactionInProgress
}
default:
if planMaker.txn != nil && planMaker.txn.Proto.Status == roachpb.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(timestamp time.Time) error {
planMaker.evalCtx.StmtTimestamp = parser.DTimestamp{Time: timestamp}
plan, err := planMaker.makePlan(stmt)
if err != nil {
return err
}
switch stmt.StatementType() {
case parser.DDL:
result.Union = &driver.Response_Result_DDL_{DDL: &driver.Response_Result_DDL{}}
case parser.RowsAffected:
resultRowsAffected := driver.Response_Result_RowsAffected{}
result.Union = &resultRowsAffected
for plan.Next() {
resultRowsAffected.RowsAffected++
}
case parser.Rows:
resultRows := &driver.Response_Result_Rows{
Columns: plan.Columns(),
}
result.Union = &driver.Response_Result_Rows_{
Rows: resultRows,
}
for plan.Next() {
values := plan.Values()
row := driver.Response_Result_Rows_Row{Values: make([]driver.Datum, 0, len(values))}
for _, val := range values {
if val == parser.DNull {
row.Values = append(row.Values, driver.Datum{})
continue
}
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_DateVal{DateVal: int64(vt)},
})
case parser.DTimestamp:
//.........这里部分代码省略.........
示例6: execStmt
func (e *Executor) execStmt(stmt parser.Statement, planMaker *planner) (driver.Response_Result, error) {
var result driver.Response_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.setTxn(client.NewTxn(e.db), time.Now())
planMaker.txn.SetDebugName("sql", 0)
case *parser.CommitTransaction, *parser.RollbackTransaction:
if planMaker.txn == nil {
return result, errNoTransactionInProgress
} else if planMaker.txn.Proto.Status == roachpb.ABORTED {
// Reset to allow starting a new transaction.
planMaker.resetTxn()
return result, nil
}
case *parser.SetTransaction:
if planMaker.txn == nil {
return result, errNoTransactionInProgress
}
default:
if planMaker.txn != nil && planMaker.txn.Proto.Status == roachpb.ABORTED {
return result, errTransactionAborted
}
}
// Bind all the placeholder variables in the stmt to actual values.
if err := parser.FillArgs(stmt, &planMaker.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(timestamp time.Time) error {
planMaker.evalCtx.StmtTimestamp = parser.DTimestamp{Time: timestamp}
plan, err := planMaker.makePlan(stmt)
if err != nil {
return err
}
switch stmt.StatementType() {
case parser.DDL:
result.Union = &driver.Response_Result_DDL_{DDL: &driver.Response_Result_DDL{}}
case parser.RowsAffected:
resultRowsAffected := driver.Response_Result_RowsAffected{}
result.Union = &resultRowsAffected
for plan.Next() {
resultRowsAffected.RowsAffected++
}
case parser.Rows:
resultRows := &driver.Response_Result_Rows{
Columns: plan.Columns(),
}
result.Union = &driver.Response_Result_Rows_{
Rows: resultRows,
}
for plan.Next() {
values := plan.Values()
row := driver.Response_Result_Rows_Row{Values: make([]driver.Datum, 0, len(values))}
for _, val := range values {
if val == parser.DNull {
row.Values = append(row.Values, driver.Datum{})
continue
}
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_DateVal{DateVal: int64(vt)},
})
case parser.DTimestamp:
//.........这里部分代码省略.........