@@ -114,4 +114,102 @@ public async ValueTask<T> ExecuteAsync<T>(Func<SQLiteDbContext, ValueTask<T>> fu
114114 }
115115 } , ( Connection : connection , Func : func ) , cancellationToken ) . ConfigureAwait ( false ) ;
116116 }
117+
118+ /// <inheritdoc/>
119+ public ValueTask ExecuteAsync < TState > ( Action < SQLiteDbContext , TState > action , TState state , CancellationToken cancellationToken = default )
120+ {
121+ ArgumentNullException . ThrowIfNull ( action ) ;
122+ cancellationToken . ThrowIfCancellationRequested ( ) ;
123+
124+ using var connection = _createConnection ( ) ;
125+ connection . AcquireLock ( static state =>
126+ {
127+ using var context = new SQLiteDbContext ( state . Connection ) ;
128+ try
129+ {
130+ state . Action ( context , state . State ) ;
131+ context . Commit ( ) ;
132+ }
133+ catch ( Exception ex )
134+ {
135+ context . TryRollback ( ex ) ;
136+ throw ;
137+ }
138+ } , ( Connection : connection , Action : action , State : state ) , cancellationToken ) ;
139+
140+ return default ;
141+ }
142+
143+ /// <inheritdoc/>
144+ public async ValueTask ExecuteAsync < TState > ( Func < SQLiteDbContext , TState , ValueTask > action , TState state , CancellationToken cancellationToken = default )
145+ {
146+ ArgumentNullException . ThrowIfNull ( action ) ;
147+ cancellationToken . ThrowIfCancellationRequested ( ) ;
148+
149+ using var connection = _createConnection ( ) ;
150+ await connection . AcquireLockAsync ( static async state =>
151+ {
152+ using var context = new SQLiteDbContext ( state . Connection ) ;
153+ try
154+ {
155+ await state . Action ( context , state . State ) . ConfigureAwait ( false ) ;
156+ context . Commit ( ) ;
157+ }
158+ catch ( Exception ex )
159+ {
160+ context . TryRollback ( ex ) ;
161+ throw ;
162+ }
163+ } , ( Connection : connection , Action : action , State : state ) , cancellationToken ) . ConfigureAwait ( false ) ;
164+ }
165+
166+ /// <inheritdoc/>
167+ public ValueTask < T > ExecuteAsync < TState , T > ( Func < SQLiteDbContext , TState , T > func , TState state , CancellationToken cancellationToken = default )
168+ {
169+ ArgumentNullException . ThrowIfNull ( func ) ;
170+ cancellationToken . ThrowIfCancellationRequested ( ) ;
171+
172+ using var connection = _createConnection ( ) ;
173+ T result = connection . AcquireLock ( static state =>
174+ {
175+ using var context = new SQLiteDbContext ( state . Connection ) ;
176+ try
177+ {
178+ T value = state . Func ( context , state . State ) ;
179+ context . Commit ( ) ;
180+ return value ;
181+ }
182+ catch ( Exception ex )
183+ {
184+ context . TryRollback ( ex ) ;
185+ throw ;
186+ }
187+ } , ( Connection : connection , Func : func , State : state ) , cancellationToken ) ;
188+
189+ return new ValueTask < T > ( result ) ;
190+ }
191+
192+ /// <inheritdoc/>
193+ public async ValueTask < T > ExecuteAsync < TState , T > ( Func < SQLiteDbContext , TState , ValueTask < T > > func , TState state , CancellationToken cancellationToken = default )
194+ {
195+ ArgumentNullException . ThrowIfNull ( func ) ;
196+ cancellationToken . ThrowIfCancellationRequested ( ) ;
197+
198+ using var connection = _createConnection ( ) ;
199+ return await connection . AcquireLockAsync ( static async state =>
200+ {
201+ using var context = new SQLiteDbContext ( state . Connection ) ;
202+ try
203+ {
204+ T result = await state . Func ( context , state . State ) . ConfigureAwait ( false ) ;
205+ context . Commit ( ) ;
206+ return result ;
207+ }
208+ catch ( Exception ex )
209+ {
210+ context . TryRollback ( ex ) ;
211+ throw ;
212+ }
213+ } , ( Connection : connection , Func : func , State : state ) , cancellationToken ) . ConfigureAwait ( false ) ;
214+ }
117215}
0 commit comments