本文整理汇总了Golang中github.com/youtube/vitess/go/vt/vtgate/vtgateconn.VTGateConn.Begin方法的典型用法代码示例。如果您正苦于以下问题:Golang VTGateConn.Begin方法的具体用法?Golang VTGateConn.Begin怎么用?Golang VTGateConn.Begin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/youtube/vitess/go/vt/vtgate/vtgateconn.VTGateConn
的用法示例。
在下文中一共展示了VTGateConn.Begin方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: testTxPassNotInTransaction
func testTxPassNotInTransaction(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := context.Background()
execCase := execMap["txRequestNIT"]
tx, err := conn.Begin(ctx)
if err != nil {
t.Error(err)
}
_, err = tx.Execute(ctx, execCase.execQuery.Sql, execCase.execQuery.BindVariables, execCase.execQuery.TabletType, true)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteShard(ctx, execCase.shardQuery.Sql, execCase.shardQuery.Keyspace, execCase.shardQuery.Shards, execCase.shardQuery.BindVariables, execCase.shardQuery.TabletType, true)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteKeyspaceIds(ctx, execCase.keyspaceIdQuery.Sql, execCase.keyspaceIdQuery.Keyspace, execCase.keyspaceIdQuery.KeyspaceIds, execCase.keyspaceIdQuery.BindVariables, execCase.keyspaceIdQuery.TabletType, true)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.Sql, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType, true)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteEntityIds(ctx, execCase.entityIdsQuery.Sql, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType, true)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteBatchShard(ctx, execCase.batchQueryShard.Queries, execCase.batchQueryShard.Keyspace, execCase.batchQueryShard.Shards, execCase.batchQueryShard.TabletType, true)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteBatchKeyspaceIds(ctx, execCase.keyspaceIdBatchQuery.Queries, execCase.keyspaceIdBatchQuery.Keyspace, execCase.keyspaceIdBatchQuery.KeyspaceIds, execCase.keyspaceIdBatchQuery.TabletType, true)
if err != nil {
t.Error(err)
}
// no rollback necessary
}
示例2: checkTransactionExecuteErrors
func checkTransactionExecuteErrors(t *testing.T, conn *vtgateconn.VTGateConn, execute func(tx *vtgateconn.VTGateTx, query string) error) {
ctx := context.Background()
for errStr, errCode := range executeErrors {
query := errorPrefix + errStr
tx, err := conn.Begin(ctx)
if err != nil {
t.Errorf("[%v] Begin error: %v", query, err)
}
checkError(t, execute(tx, query), query, errStr, errCode)
// Partial error where server doesn't close the session.
query = partialErrorPrefix + errStr
tx, err = conn.Begin(ctx)
if err != nil {
t.Errorf("[%v] Begin error: %v", query, err)
}
checkError(t, execute(tx, query), query, errStr, errCode)
// The transaction should still be usable now.
if err := tx.Rollback(ctx); err != nil {
t.Errorf("[%v] Rollback error: %v", query, err)
}
// Partial error where server closes the session.
tx, err = conn.Begin(ctx)
if err != nil {
t.Errorf("[%v] Begin error: %v", query, err)
}
query = partialErrorPrefix + errStr + "/close transaction"
checkError(t, execute(tx, query), query, errStr, errCode)
// The transaction should be unusable now.
if tx.Rollback(ctx) == nil {
t.Errorf("[%v] expected Rollback error, got nil", query)
}
}
}
示例3: testEchoTransactionExecute
func testEchoTransactionExecute(t *testing.T, conn *vtgateconn.VTGateConn) {
var qr *mproto.QueryResult
var err error
ctx := callerid.NewContext(context.Background(), callerID, nil)
tx, err := conn.Begin(ctx)
if err != nil {
t.Fatalf("Begin error: %v", err)
}
qr, err = tx.Execute(ctx, echoPrefix+query, bindVars, tabletType, true)
checkEcho(t, "Execute", qr, err, map[string]string{
"callerId": callerIDEcho,
"query": echoPrefix + query,
"bindVars": bindVarsEcho,
"tabletType": tabletTypeEcho,
"session": sessionEcho,
"notInTransaction": "true",
})
qr, err = tx.ExecuteShards(ctx, echoPrefix+query, keyspace, shards, bindVars, tabletType, true)
checkEcho(t, "ExecuteShards", qr, err, map[string]string{
"callerId": callerIDEcho,
"query": echoPrefix + query,
"keyspace": keyspace,
"shards": shardsEcho,
"bindVars": bindVarsEcho,
"tabletType": tabletTypeEcho,
"session": sessionEcho,
"notInTransaction": "true",
})
qr, err = tx.ExecuteKeyspaceIds(ctx, echoPrefix+query, keyspace, keyspaceIDs, bindVars, tabletType, true)
checkEcho(t, "ExecuteKeyspaceIds", qr, err, map[string]string{
"callerId": callerIDEcho,
"query": echoPrefix + query,
"keyspace": keyspace,
"keyspaceIds": keyspaceIDsEcho,
"bindVars": bindVarsEcho,
"tabletType": tabletTypeEcho,
"session": sessionEcho,
"notInTransaction": "true",
})
qr, err = tx.ExecuteKeyRanges(ctx, echoPrefix+query, keyspace, keyRanges, bindVars, tabletType, true)
checkEcho(t, "ExecuteKeyRanges", qr, err, map[string]string{
"callerId": callerIDEcho,
"query": echoPrefix + query,
"keyspace": keyspace,
"keyRanges": keyRangesEcho,
"bindVars": bindVarsEcho,
"tabletType": tabletTypeEcho,
"session": sessionEcho,
"notInTransaction": "true",
})
qr, err = tx.ExecuteEntityIds(ctx, echoPrefix+query, keyspace, "column1", entityKeyspaceIDs, bindVars, tabletType, true)
checkEcho(t, "ExecuteEntityIds", qr, err, map[string]string{
"callerId": callerIDEcho,
"query": echoPrefix + query,
"keyspace": keyspace,
"entityColumnName": "column1",
"entityIds": entityKeyspaceIDsEcho,
"bindVars": bindVarsEcho,
"tabletType": tabletTypeEcho,
"session": sessionEcho,
"notInTransaction": "true",
})
if err := tx.Rollback(ctx); err != nil {
t.Fatalf("Rollback error: %v", err)
}
tx, err = conn.Begin(ctx)
if err != nil {
t.Fatalf("Begin (again) error: %v", err)
}
var qrs []mproto.QueryResult
qrs, err = tx.ExecuteBatchShards(ctx, []gproto.BoundShardQuery{
gproto.BoundShardQuery{
Sql: echoPrefix + query,
Keyspace: keyspace,
Shards: shards,
BindVariables: bindVars,
},
}, tabletType, true)
checkEcho(t, "ExecuteBatchShards", &qrs[0], err, map[string]string{
"callerId": callerIDEcho,
"query": echoPrefix + query,
"keyspace": keyspace,
"shards": shardsEcho,
"bindVars": bindVarsEcho,
"tabletType": tabletTypeEcho,
"session": sessionEcho,
"asTransaction": "true",
})
qrs, err = tx.ExecuteBatchKeyspaceIds(ctx, []gproto.BoundKeyspaceIdQuery{
//.........这里部分代码省略.........
示例4: testTxFail
func testTxFail(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := context.Background()
tx, err := conn.Begin(ctx)
if err != nil {
t.Error(err)
}
err = tx.Commit(ctx)
want := "commit: session mismatch"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("Commit: %v, want %v", err, want)
}
_, err = tx.Execute(ctx, "", nil, "", false)
want = "execute: not in transaction"
if err == nil || err.Error() != want {
t.Errorf("Execute: %v, want %v", err, want)
}
_, err = tx.ExecuteShard(ctx, "", "", nil, nil, "", false)
want = "executeShard: not in transaction"
if err == nil || err.Error() != want {
t.Errorf("ExecuteShard: %v, want %v", err, want)
}
_, err = tx.ExecuteKeyspaceIds(ctx, "", "", nil, nil, "", false)
want = "executeKeyspaceIds: not in transaction"
if err == nil || err.Error() != want {
t.Errorf("ExecuteShard: %v, want %v", err, want)
}
_, err = tx.ExecuteKeyRanges(ctx, "", "", nil, nil, "", false)
want = "executeKeyRanges: not in transaction"
if err == nil || err.Error() != want {
t.Errorf("ExecuteShard: %v, want %v", err, want)
}
_, err = tx.ExecuteEntityIds(ctx, "", "", "", nil, nil, "", false)
want = "executeEntityIds: not in transaction"
if err == nil || err.Error() != want {
t.Errorf("ExecuteShard: %v, want %v", err, want)
}
_, err = tx.ExecuteBatchShard(ctx, nil, "", nil, "", false)
want = "executeBatchShard: not in transaction"
if err == nil || err.Error() != want {
t.Errorf("ExecuteShard: %v, want %v", err, want)
}
_, err = tx.ExecuteBatchKeyspaceIds(ctx, nil, "", nil, "", false)
want = "executeBatchKeyspaceIds: not in transaction"
if err == nil || err.Error() != want {
t.Errorf("ExecuteShard: %v, want %v", err, want)
}
err = tx.Commit(ctx)
want = "commit: not in transaction"
if err == nil || err.Error() != want {
t.Errorf("Commit: %v, want %v", err, want)
}
err = tx.Rollback(ctx)
if err != nil {
t.Error(err)
}
tx, err = conn.Begin(ctx)
if err != nil {
t.Error(err)
}
err = tx.Rollback(ctx)
want = "rollback: session mismatch"
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("Rollback: %v, want %v", err, want)
}
}
示例5: testBeginPanic
func testBeginPanic(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := context.Background()
_, err := conn.Begin(ctx)
expectPanic(t, err)
}
示例6: testTxPass
func testTxPass(t *testing.T, conn *vtgateconn.VTGateConn) {
ctx := context.Background()
execCase := execMap["txRequest"]
// Execute
tx, err := conn.Begin(ctx)
if err != nil {
t.Error(err)
}
_, err = tx.Execute(ctx, execCase.execQuery.Sql, execCase.execQuery.BindVariables, execCase.execQuery.TabletType, false)
if err != nil {
t.Error(err)
}
err = tx.Commit(ctx)
if err != nil {
t.Error(err)
}
// ExecuteShard
tx, err = conn.Begin(ctx)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteShard(ctx, execCase.shardQuery.Sql, execCase.shardQuery.Keyspace, execCase.shardQuery.Shards, execCase.shardQuery.BindVariables, execCase.shardQuery.TabletType, false)
if err != nil {
t.Error(err)
}
err = tx.Rollback(ctx)
if err != nil {
t.Error(err)
}
// ExecuteKeyspaceIds
tx, err = conn.Begin(ctx)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteKeyspaceIds(ctx, execCase.keyspaceIdQuery.Sql, execCase.keyspaceIdQuery.Keyspace, execCase.keyspaceIdQuery.KeyspaceIds, execCase.keyspaceIdQuery.BindVariables, execCase.keyspaceIdQuery.TabletType, false)
if err != nil {
t.Error(err)
}
err = tx.Rollback(ctx)
if err != nil {
t.Error(err)
}
// ExecuteKeyRanges
tx, err = conn.Begin(ctx)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteKeyRanges(ctx, execCase.keyRangeQuery.Sql, execCase.keyRangeQuery.Keyspace, execCase.keyRangeQuery.KeyRanges, execCase.keyRangeQuery.BindVariables, execCase.keyRangeQuery.TabletType, false)
if err != nil {
t.Error(err)
}
err = tx.Rollback(ctx)
if err != nil {
t.Error(err)
}
// ExecuteEntityIds
tx, err = conn.Begin(ctx)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteEntityIds(ctx, execCase.entityIdsQuery.Sql, execCase.entityIdsQuery.Keyspace, execCase.entityIdsQuery.EntityColumnName, execCase.entityIdsQuery.EntityKeyspaceIDs, execCase.entityIdsQuery.BindVariables, execCase.entityIdsQuery.TabletType, false)
if err != nil {
t.Error(err)
}
err = tx.Rollback(ctx)
if err != nil {
t.Error(err)
}
// ExecuteBatchShard
tx, err = conn.Begin(ctx)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteBatchShard(ctx, execCase.batchQueryShard.Queries, execCase.batchQueryShard.Keyspace, execCase.batchQueryShard.Shards, execCase.batchQueryShard.TabletType, false)
if err != nil {
t.Error(err)
}
err = tx.Rollback(ctx)
if err != nil {
t.Error(err)
}
// ExecuteBatchKeyspaceIds
tx, err = conn.Begin(ctx)
if err != nil {
t.Error(err)
}
_, err = tx.ExecuteBatchKeyspaceIds(ctx, execCase.keyspaceIdBatchQuery.Queries, execCase.keyspaceIdBatchQuery.Keyspace, execCase.keyspaceIdBatchQuery.KeyspaceIds, execCase.keyspaceIdBatchQuery.TabletType, false)
if err != nil {
t.Error(err)
}
err = tx.Rollback(ctx)
if err != nil {
t.Error(err)
//.........这里部分代码省略.........