123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- package dpsv1
- import (
- "errors"
- "git.sxidc.com/service-supports/dps-sdk/client"
- "git.sxidc.com/service-supports/dps-sdk/pb/v1"
- "git.sxidc.com/service-supports/dps-sdk/pb/v1/request"
- )
- type Transaction struct {
- stream v1.CommandService_TransactionClient
- client *Client
- }
- func (tx *Transaction) InsertTx(req *client.InsertRequest) (string, error) {
- if req.TableRow == nil {
- return "", nil
- }
- tx.client.transactionMutex.Lock()
- defer tx.client.transactionMutex.Unlock()
- if tx.client.conn == nil {
- return "", nil
- }
- var err error
- defer func() {
- if err != nil {
- innerErr := tx.stream.CloseSend()
- if innerErr != nil {
- panic(innerErr)
- }
- }
- }()
- reqTableRow, err := req.TableRow.ToDPSTableRow()
- if err != nil {
- return "", err
- }
- err = tx.stream.Send(&request.TransactionOperation{
- Request: &request.TransactionOperation_InsertTxRequest{
- InsertTxRequest: &request.InsertTxRequest{
- TablePrefixWithSchema: req.TablePrefixWithSchema,
- Version: req.Version,
- KeyColumns: req.KeyColumns,
- TableRow: reqTableRow,
- UserID: req.UserID,
- },
- }})
- if err != nil {
- return "", err
- }
- txResponse, err := tx.stream.Recv()
- if err != nil {
- return "", err
- }
- return txResponse.Statement, nil
- }
- func (tx *Transaction) InsertBatchTx(req *client.InsertBatchRequest) (string, error) {
- tx.client.transactionMutex.Lock()
- defer tx.client.transactionMutex.Unlock()
- if tx.client.conn == nil {
- return "", nil
- }
- tableRowItems := make([]*request.InsertTableRowItem, 0)
- for _, reqTableItem := range req.Items {
- tableRows := make([]*request.TableRow, 0)
- for _, reqTableRow := range reqTableItem.TableRows {
- if reqTableRow == nil {
- continue
- }
- tableRow, err := reqTableRow.ToDPSTableRow()
- if err != nil {
- return "", err
- }
- tableRows = append(tableRows, tableRow)
- }
- tableRowItems = append(tableRowItems, &request.InsertTableRowItem{
- TablePrefixWithSchema: reqTableItem.TablePrefixWithSchema,
- Version: reqTableItem.Version,
- KeyColumns: reqTableItem.KeyColumns,
- TableRows: tableRows,
- })
- }
- err := tx.stream.Send(&request.TransactionOperation{
- Request: &request.TransactionOperation_InsertBatchTxRequest{
- InsertBatchTxRequest: &request.InsertBatchTxRequest{
- Items: tableRowItems,
- UserID: req.UserID,
- },
- }})
- if err != nil {
- return "", err
- }
- txResponse, err := tx.stream.Recv()
- if err != nil {
- return "", err
- }
- return txResponse.Statement, nil
- }
- func (tx *Transaction) DeleteTx(req *client.DeleteRequest) (string, error) {
- tx.client.transactionMutex.Lock()
- defer tx.client.transactionMutex.Unlock()
- if tx.client.conn == nil {
- return "", nil
- }
- err := tx.stream.Send(&request.TransactionOperation{
- Request: &request.TransactionOperation_DeleteTxRequest{
- DeleteTxRequest: &request.DeleteTxRequest{
- TablePrefixWithSchema: req.TablePrefixWithSchema,
- Version: req.Version,
- KeyColumns: req.KeyValues.Columns(),
- KeyValues: req.KeyValues.Values(),
- UserID: req.UserID,
- },
- }})
- if err != nil {
- return "", err
- }
- txResponse, err := tx.stream.Recv()
- if err != nil {
- return "", err
- }
- return txResponse.Statement, nil
- }
- func (tx *Transaction) DeleteWhereTx(req *client.DeleteWhereRequest) (string, error) {
- if req.Where == nil {
- return "", errors.New("没有传递Where条件")
- }
- tx.client.transactionMutex.Lock()
- defer tx.client.transactionMutex.Unlock()
- if tx.client.conn == nil {
- return "", nil
- }
- whereJsonBytes, err := req.Where.ToJson()
- if err != nil {
- return "", err
- }
- err = tx.stream.Send(&request.TransactionOperation{
- Request: &request.TransactionOperation_DeleteWhereTxRequest{
- DeleteWhereTxRequest: &request.DeleteWhereTxRequest{
- TablePrefixWithSchema: req.TablePrefixWithSchema,
- Version: req.Version,
- KeyColumns: req.KeyColumns,
- Where: whereJsonBytes,
- UserID: req.UserID,
- },
- }})
- if err != nil {
- return "", err
- }
- txResponse, err := tx.stream.Recv()
- if err != nil {
- return "", err
- }
- return txResponse.Statement, nil
- }
- func (tx *Transaction) UpdateTx(req *client.UpdateRequest) (string, error) {
- if req.NewTableRow == nil {
- return "", nil
- }
- tx.client.transactionMutex.Lock()
- defer tx.client.transactionMutex.Unlock()
- if tx.client.conn == nil {
- return "", nil
- }
- reqNewTableRow, err := req.NewTableRow.ToDPSTableRow()
- if err != nil {
- return "", err
- }
- err = tx.stream.Send(&request.TransactionOperation{
- Request: &request.TransactionOperation_UpdateTxRequest{
- UpdateTxRequest: &request.UpdateTxRequest{
- TablePrefixWithSchema: req.TablePrefixWithSchema,
- Version: req.Version,
- KeyColumns: req.KeyValues.Columns(),
- KeyValues: req.KeyValues.Values(),
- NewTableRow: reqNewTableRow,
- UserID: req.UserID,
- },
- }})
- if err != nil {
- return "", err
- }
- txResponse, err := tx.stream.Recv()
- if err != nil {
- return "", err
- }
- return txResponse.Statement, nil
- }
- func (tx *Transaction) UpdateWhereTx(req *client.UpdateWhereRequest) (string, error) {
- if req.Where == nil {
- return "", errors.New("没有传递Where条件")
- }
- if req.NewTableRow == nil {
- return "", nil
- }
- tx.client.transactionMutex.Lock()
- defer tx.client.transactionMutex.Unlock()
- if tx.client.conn == nil {
- return "", nil
- }
- whereJsonBytes, err := req.Where.ToJson()
- if err != nil {
- return "", err
- }
- reqNewTableRow, err := req.NewTableRow.ToDPSTableRow()
- if err != nil {
- return "", err
- }
- err = tx.stream.Send(&request.TransactionOperation{
- Request: &request.TransactionOperation_UpdateWhereTxRequest{
- UpdateWhereTxRequest: &request.UpdateWhereTxRequest{
- TablePrefixWithSchema: req.TablePrefixWithSchema,
- Version: req.Version,
- KeyColumns: req.KeyColumns,
- Where: whereJsonBytes,
- NewTableRow: reqNewTableRow,
- UserID: req.UserID,
- },
- }})
- if err != nil {
- return "", err
- }
- txResponse, err := tx.stream.Recv()
- if err != nil {
- return "", err
- }
- return txResponse.Statement, nil
- }
|