File tree Expand file tree Collapse file tree 2 files changed +92
-5
lines changed
Expand file tree Collapse file tree 2 files changed +92
-5
lines changed Original file line number Diff line number Diff line change 11var pg = require ( 'pg' ) ;
22
33var m_connection = Symbol ( 'connection' ) ;
4+ var m_transaction = Symbol ( 'transaction' ) ;
45
56class PostgreSQL {
67 constructor ( connection ) {
78 this [ m_connection ] = connection ;
9+ this [ m_transaction ] = false ;
810 }
911
1012 query ( sql ) {
@@ -38,6 +40,65 @@ class PostgreSQL {
3840 } ) ;
3941 } ) ;
4042 }
43+
44+ isTransactionSupported ( ) {
45+ return true ;
46+ }
47+
48+ inTransaction ( ) {
49+ return this [ m_transaction ] ;
50+ }
51+
52+ beginTransaction ( ) {
53+ var self = this ;
54+ if ( this . inTransaction ( ) == true ) {
55+ return Promise . resolve ( false ) ;
56+ }
57+ return new Promise ( ( resolve , reject ) => {
58+ this . execute ( 'BEGIN' )
59+ . then ( ( ) => {
60+ self [ m_transaction ] = true ;
61+ resolve ( true ) ;
62+ } )
63+ . catch ( error => {
64+ reject ( error ) ;
65+ } ) ;
66+ } ) ;
67+ }
68+
69+ commit ( ) {
70+ var self = this ;
71+ if ( this . inTransaction ( ) == false ) {
72+ return Promise . resolve ( false ) ;
73+ }
74+ return new Promise ( ( resolve , reject ) => {
75+ this . execute ( 'COMMIT' )
76+ . then ( ( ) => {
77+ self [ m_transaction ] = false ;
78+ resolve ( true ) ;
79+ } )
80+ . catch ( error => {
81+ reject ( error ) ;
82+ } )
83+ } ) ;
84+ }
85+
86+ rollback ( ) {
87+ var self = this ;
88+ if ( this . inTransaction ( ) == false ) {
89+ return Promise . resolve ( false ) ;
90+ }
91+ return new Promise ( ( resolve , reject ) => {
92+ this . execute ( 'ROLLBACK' )
93+ . then ( ( ) => {
94+ self [ m_transaction ] = false ;
95+ resolve ( true ) ;
96+ } )
97+ . catch ( error => {
98+ reject ( error ) ;
99+ } )
100+ } ) ;
101+ }
41102}
42103
43104function OpenConnection ( connection ) {
You can’t perform that action at this time.
0 commit comments