@@ -55,7 +55,35 @@ type conn struct {
5555 dataOpts []options.ExecuteDataQueryOption
5656 scanOpts []options.ExecuteScanQueryOption
5757
58- currentTx * tx
58+ transaction table.Transaction
59+ }
60+
61+ func (c * conn ) Commit () error {
62+ if c .isClosed () {
63+ return errClosedConn
64+ }
65+ defer func () {
66+ c .transaction = nil
67+ }()
68+ _ , err := c .transaction .CommitTx (context .Background ())
69+ if err != nil {
70+ return c .checkClosed (err )
71+ }
72+ return nil
73+ }
74+
75+ func (c * conn ) Rollback () error {
76+ if c .isClosed () {
77+ return errClosedConn
78+ }
79+ defer func () {
80+ c .transaction = nil
81+ }()
82+ err := c .transaction .Rollback (context .Background ())
83+ if err != nil {
84+ return c .checkClosed (err )
85+ }
86+ return err
5987}
6088
6189var (
@@ -119,7 +147,6 @@ func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, e
119147 }
120148 return & stmt {
121149 conn : c ,
122- tx : c .currentTx ,
123150 params : internal .Params (s ),
124151 query : query ,
125152 }, nil
@@ -129,38 +156,38 @@ func (c *conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (driver.
129156 if c .isClosed () {
130157 return nil , errClosedConn
131158 }
132- if c .currentTx != nil {
159+ if c .transaction != nil {
133160 return nil , xerrors .WithStackTrace (
134- fmt .Errorf ("conn already have an opened tx: %s" , c .currentTx . transaction .ID ()),
161+ fmt .Errorf ("conn already have an opened tx: %s" , c .transaction .ID ()),
135162 )
136163 }
137164 txSettings , err := isolation .ToYDB (txOptions )
138165 if err != nil {
139166 return nil , xerrors .WithStackTrace (err )
140167 }
141- t , err : = c .session .BeginTransaction (ctx , txSettings )
168+ c . transaction , err = c .session .BeginTransaction (ctx , txSettings )
142169 if err != nil {
143170 return nil , c .checkClosed (err )
144171 }
145- c .currentTx = & tx {
146- conn : c ,
147- transaction : t ,
148- }
149- return c .currentTx , nil
172+ return c , nil
150173}
151174
152175func (c * conn ) ExecContext (ctx context.Context , query string , args []driver.NamedValue ) (driver.Result , error ) {
153176 if c .isClosed () {
154177 return nil , errClosedConn
155178 }
156179 m := queryModeFromContext (ctx , c .defaultQueryMode )
157- if c .currentTx != nil {
180+ if c .transaction != nil {
158181 if m != DataQueryMode {
159182 return nil , xerrors .WithStackTrace (
160183 fmt .Errorf ("query mode `%s` not supported with transaction" , m .String ()),
161184 )
162185 }
163- return c .currentTx .ExecContext (ctx , query , args )
186+ _ , err := c .transaction .Execute (ctx , query , toQueryParams (args ), dataQueryOptions (ctx )... )
187+ if err != nil {
188+ return nil , c .checkClosed (err )
189+ }
190+ return c , nil
164191 }
165192 switch m {
166193 case DataQueryMode :
@@ -199,13 +226,22 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
199226 return nil , errClosedConn
200227 }
201228 m := queryModeFromContext (ctx , c .defaultQueryMode )
202- if c .currentTx != nil {
229+ if c .transaction != nil {
203230 if m != DataQueryMode {
204231 return nil , xerrors .WithStackTrace (
205232 fmt .Errorf ("query mode `%s` not supported with transaction" , m .String ()),
206233 )
207234 }
208- return c .currentTx .QueryContext (ctx , query , args )
235+ res , err := c .transaction .Execute (ctx , query , toQueryParams (args ), dataQueryOptions (ctx )... )
236+ if err != nil {
237+ return nil , c .checkClosed (err )
238+ }
239+ if err = res .Err (); err != nil {
240+ return nil , c .checkClosed (err )
241+ }
242+ return & rows {
243+ result : res ,
244+ }, nil
209245 }
210246 switch m {
211247 case DataQueryMode :
0 commit comments