@@ -71,6 +71,9 @@ use std::time::Duration;
71
71
#[ cfg( feature = "unix_socket" ) ]
72
72
use std:: path:: PathBuf ;
73
73
74
+ // FIXME remove in 0.12
75
+ pub use transaction:: Transaction ;
76
+
74
77
use error:: { Error , ConnectError , SqlState , DbError } ;
75
78
use io:: { StreamWrapper , NegotiateSsl } ;
76
79
use message:: BackendMessage :: * ;
@@ -93,10 +96,11 @@ mod url;
93
96
mod util;
94
97
pub mod error;
95
98
pub mod io;
99
+ pub mod notification;
96
100
pub mod rows;
97
101
pub mod stmt;
102
+ pub mod transaction;
98
103
pub mod types;
99
- pub mod notification;
100
104
101
105
const TYPEINFO_QUERY : & ' static str = "__typeinfo" ;
102
106
const TYPEINFO_ENUM_QUERY : & ' static str = "__typeinfo_enum" ;
@@ -1172,12 +1176,7 @@ impl Connection {
1172
1176
"`transaction` must be called on the active transaction" ) ;
1173
1177
try!( conn. quick_query ( "BEGIN" ) ) ;
1174
1178
conn. trans_depth += 1 ;
1175
- Ok ( Transaction {
1176
- conn : self ,
1177
- commit : Cell :: new ( false ) ,
1178
- depth : 1 ,
1179
- finished : false ,
1180
- } )
1179
+ Ok ( Transaction :: new ( self , 1 ) )
1181
1180
}
1182
1181
1183
1182
/// Creates a new prepared statement.
@@ -1343,136 +1342,6 @@ impl Connection {
1343
1342
}
1344
1343
}
1345
1344
1346
- /// Represents a transaction on a database connection.
1347
- ///
1348
- /// The transaction will roll back by default.
1349
- pub struct Transaction < ' conn > {
1350
- conn : & ' conn Connection ,
1351
- depth : u32 ,
1352
- commit : Cell < bool > ,
1353
- finished : bool ,
1354
- }
1355
-
1356
- impl < ' a > fmt:: Debug for Transaction < ' a > {
1357
- fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
1358
- fmt. debug_struct ( "Transaction" )
1359
- . field ( "commit" , & self . commit . get ( ) )
1360
- . field ( "depth" , & self . depth )
1361
- . finish ( )
1362
- }
1363
- }
1364
-
1365
- impl < ' conn > Drop for Transaction < ' conn > {
1366
- fn drop ( & mut self ) {
1367
- if !self . finished {
1368
- let _ = self . finish_inner ( ) ;
1369
- }
1370
- }
1371
- }
1372
-
1373
- impl < ' conn > Transaction < ' conn > {
1374
- fn finish_inner ( & mut self ) -> Result < ( ) > {
1375
- let mut conn = self . conn . conn . borrow_mut ( ) ;
1376
- debug_assert ! ( self . depth == conn. trans_depth) ;
1377
- let query = match ( self . commit . get ( ) , self . depth != 1 ) {
1378
- ( false , true ) => "ROLLBACK TO sp" ,
1379
- ( false , false ) => "ROLLBACK" ,
1380
- ( true , true ) => "RELEASE sp" ,
1381
- ( true , false ) => "COMMIT" ,
1382
- } ;
1383
- conn. trans_depth -= 1 ;
1384
- conn. quick_query ( query) . map ( |_| ( ) )
1385
- }
1386
-
1387
- /// Like `Connection::prepare`.
1388
- pub fn prepare ( & self , query : & str ) -> Result < Statement < ' conn > > {
1389
- self . conn . prepare ( query)
1390
- }
1391
-
1392
- /// Like `Connection::prepare_cached`.
1393
- ///
1394
- /// Note that the statement will be cached for the duration of the
1395
- /// connection, not just the duration of this transaction.
1396
- pub fn prepare_cached ( & self , query : & str ) -> Result < Statement < ' conn > > {
1397
- self . conn . prepare_cached ( query)
1398
- }
1399
-
1400
- /// Like `Connection::execute`.
1401
- pub fn execute ( & self , query : & str , params : & [ & ToSql ] ) -> Result < u64 > {
1402
- self . conn . execute ( query, params)
1403
- }
1404
-
1405
- /// Like `Connection::query`.
1406
- pub fn query < ' a > ( & ' a self , query : & str , params : & [ & ToSql ] ) -> Result < Rows < ' a > > {
1407
- self . conn . query ( query, params)
1408
- }
1409
-
1410
- /// Like `Connection::batch_execute`.
1411
- pub fn batch_execute ( & self , query : & str ) -> Result < ( ) > {
1412
- self . conn . batch_execute ( query)
1413
- }
1414
-
1415
- /// Like `Connection::transaction`.
1416
- ///
1417
- /// # Panics
1418
- ///
1419
- /// Panics if there is an active nested transaction.
1420
- pub fn transaction < ' a > ( & ' a self ) -> Result < Transaction < ' a > > {
1421
- let mut conn = self . conn . conn . borrow_mut ( ) ;
1422
- check_desync ! ( conn) ;
1423
- assert ! ( conn. trans_depth == self . depth,
1424
- "`transaction` may only be called on the active transaction" ) ;
1425
- try!( conn. quick_query ( "SAVEPOINT sp" ) ) ;
1426
- conn. trans_depth += 1 ;
1427
- Ok ( Transaction {
1428
- conn : self . conn ,
1429
- commit : Cell :: new ( false ) ,
1430
- depth : self . depth + 1 ,
1431
- finished : false ,
1432
- } )
1433
- }
1434
-
1435
- /// Returns a reference to the `Transaction`'s `Connection`.
1436
- pub fn connection ( & self ) -> & ' conn Connection {
1437
- self . conn
1438
- }
1439
-
1440
- /// Like `Connection::is_active`.
1441
- pub fn is_active ( & self ) -> bool {
1442
- self . conn . conn . borrow ( ) . trans_depth == self . depth
1443
- }
1444
-
1445
- /// Determines if the transaction is currently set to commit or roll back.
1446
- pub fn will_commit ( & self ) -> bool {
1447
- self . commit . get ( )
1448
- }
1449
-
1450
- /// Sets the transaction to commit at its completion.
1451
- pub fn set_commit ( & self ) {
1452
- self . commit . set ( true ) ;
1453
- }
1454
-
1455
- /// Sets the transaction to roll back at its completion.
1456
- pub fn set_rollback ( & self ) {
1457
- self . commit . set ( false ) ;
1458
- }
1459
-
1460
- /// A convenience method which consumes and commits a transaction.
1461
- pub fn commit ( self ) -> Result < ( ) > {
1462
- self . set_commit ( ) ;
1463
- self . finish ( )
1464
- }
1465
-
1466
- /// Consumes the transaction, commiting or rolling it back as appropriate.
1467
- ///
1468
- /// Functionally equivalent to the `Drop` implementation of `Transaction`
1469
- /// except that it returns any error to the caller.
1470
- pub fn finish ( mut self ) -> Result < ( ) > {
1471
- self . finished = true ;
1472
- self . finish_inner ( )
1473
- }
1474
- }
1475
-
1476
1345
/// A trait allowing abstraction over connections and transactions
1477
1346
pub trait GenericConnection {
1478
1347
/// Like `Connection::execute`.
@@ -1614,3 +1483,11 @@ trait WrongTypeNew {
1614
1483
trait FieldNew {
1615
1484
fn new ( name : String , type_ : Type ) -> Field ;
1616
1485
}
1486
+
1487
+ trait TransactionInternals < ' conn > {
1488
+ fn new ( conn : & ' conn Connection , depth : u32 ) -> Transaction < ' conn > ;
1489
+
1490
+ fn conn ( & self ) -> & ' conn Connection ;
1491
+
1492
+ fn depth ( & self ) -> u32 ;
1493
+ }
0 commit comments