22using System . Text . Json ;
33using Npgsql ;
44using NpgsqlTypes ;
5- using Ctx = NpgsqlDataSourceExtensions . NpgsqlCommandContext ;
5+ using Ctx = Wololo . PgKeyValueDB . NpgsqlDataSourceExtensions . NpgsqlCommandContext ;
66
77namespace Wololo . PgKeyValueDB ;
88
@@ -20,7 +20,7 @@ public PgKeyValueDB(NpgsqlDataSource dataSource, string schemaName, string table
2020 this . dataSource = dataSource ;
2121 this . schemaName = schemaName ;
2222 this . tableName = tableName ;
23- this . tableRef = $ "{ schemaName } .{ tableName } ";
23+ tableRef = $ "{ schemaName } .{ tableName } ";
2424 JsonSerializerOptions = jsonSerializerOptions ;
2525 Init ( ) ;
2626 }
@@ -42,15 +42,15 @@ primary key (pid, id)
4242 dataSource . Execute ( new Ctx ( $ "create index if not exists idx_{ schemaName } _{ tableName } _expires on { tableRef } (expires) where expires is not null", Prepare : false ) ) ;
4343 }
4444
45- static IEnumerable < NpgsqlParameter > CreateParams ( string pid , string ? id = null )
45+ static List < NpgsqlParameter > CreateParams ( string pid , string ? id = null )
4646 {
4747 var baseParams = new List < NpgsqlParameter > { new ( ) { ParameterName = "pid" , Value = pid } } ;
4848 if ( id != null )
4949 baseParams . Add ( new ( ) { ParameterName = "id" , Value = id } ) ;
5050 return baseParams ;
5151 }
5252
53- static IEnumerable < NpgsqlParameter > CreateParams < T > ( string pid , string ? id , T ? value , DateTimeOffset ? expires )
53+ static List < NpgsqlParameter > CreateParams < T > ( string pid , string ? id , T ? value , DateTimeOffset ? expires )
5454 {
5555 var baseParams = new List < NpgsqlParameter > { new ( ) { ParameterName = "pid" , Value = pid } } ;
5656 if ( id != null )
@@ -76,52 +76,52 @@ static IEnumerable<NpgsqlParameter> CreateParams<T>(string pid, string? id, T? v
7676
7777 public bool Create < T > ( string id , T value , string pid = DEFAULT_PID , DateTimeOffset ? expires = null ) =>
7878 dataSource . Execute ( new Ctx ( CreateCreateSql , CreateParams ( pid , id , value , expires ) ) ) > 0 ;
79- public async Task < bool > CreateAsync < T > ( string id , T value , string pid = DEFAULT_PID , DateTimeOffset ? expires = null ) =>
80- await dataSource . ExecuteAsync ( new Ctx ( CreateCreateSql , CreateParams ( pid , id , value , expires ) ) ) > 0 ;
79+ public async Task < bool > CreateAsync < T > ( string id , T value , string pid = DEFAULT_PID , DateTimeOffset ? expires = null , CancellationToken token = default ) =>
80+ await dataSource . ExecuteAsync ( new Ctx ( CreateCreateSql , CreateParams ( pid , id , value , expires ) ) , token ) > 0 ;
8181 public bool Update < T > ( string id , T value , string pid = DEFAULT_PID , DateTimeOffset ? expires = null ) =>
8282 dataSource . Execute ( new Ctx ( UpdateSql , CreateParams ( pid , id , value , expires ) ) ) > 0 ;
83- public async Task < bool > UpdateAsync < T > ( string id , T value , string pid = DEFAULT_PID , DateTimeOffset ? expires = null ) =>
84- await dataSource . ExecuteAsync ( new Ctx ( UpdateSql , CreateParams ( pid , id , value , expires ) ) ) > 0 ;
83+ public async Task < bool > UpdateAsync < T > ( string id , T value , string pid = DEFAULT_PID , DateTimeOffset ? expires = null , CancellationToken token = default ) =>
84+ await dataSource . ExecuteAsync ( new Ctx ( UpdateSql , CreateParams ( pid , id , value , expires ) ) , token ) > 0 ;
8585 public bool Upsert < T > ( string id , T value , string pid = DEFAULT_PID , DateTimeOffset ? expires = null ) =>
8686 dataSource . Execute ( new Ctx ( UpsertSql , CreateParams ( pid , id , value , expires ) ) ) > 0 ;
87- public async Task < bool > UpsertAsync < T > ( string id , T value , string pid = DEFAULT_PID , DateTimeOffset ? expires = null ) =>
88- await dataSource . ExecuteAsync ( new Ctx ( UpsertSql , CreateParams ( pid , id , value , expires ) ) ) > 0 ;
87+ public async Task < bool > UpsertAsync < T > ( string id , T value , string pid = DEFAULT_PID , DateTimeOffset ? expires = null , CancellationToken token = default ) =>
88+ await dataSource . ExecuteAsync ( new Ctx ( UpsertSql , CreateParams ( pid , id , value , expires ) ) , token ) > 0 ;
8989 public bool Remove ( string id , string pid = DEFAULT_PID ) =>
9090 dataSource . Execute ( new Ctx ( DeleteSql , CreateParams ( pid , id ) ) ) > 0 ;
91- public async Task < bool > RemoveAsync ( string id , string pid = DEFAULT_PID ) =>
92- await dataSource . ExecuteAsync ( new Ctx ( DeleteSql , CreateParams ( pid , id ) ) ) > 0 ;
91+ public async Task < bool > RemoveAsync ( string id , string pid = DEFAULT_PID , CancellationToken token = default ) =>
92+ await dataSource . ExecuteAsync ( new Ctx ( DeleteSql , CreateParams ( pid , id ) ) , token ) > 0 ;
9393 public int RemoveAll ( string pid = DEFAULT_PID ) =>
9494 dataSource . Execute ( new Ctx ( DeleteAllSql , CreateParams ( pid ) ) ) ;
9595 public int RemoveAll < T > ( string pid = DEFAULT_PID , Expression < Func < T , bool > > ? where = null ) =>
9696 dataSource . Execute ( BuildCommandParams ( DeleteAllSql , pid , where ) ) ;
97- public async Task < int > RemoveAllAsync ( string pid = DEFAULT_PID ) =>
98- await dataSource . ExecuteAsync ( new Ctx ( DeleteAllSql , CreateParams ( pid ) ) ) ;
99- public async Task < int > RemoveAllAsync < T > ( string pid = DEFAULT_PID , Expression < Func < T , bool > > ? where = null ) =>
100- await dataSource . ExecuteAsync ( BuildCommandParams ( DeleteAllSql , pid , where ) ) ;
97+ public async Task < int > RemoveAllAsync ( string pid = DEFAULT_PID , CancellationToken token = default ) =>
98+ await dataSource . ExecuteAsync ( new Ctx ( DeleteAllSql , CreateParams ( pid ) ) , token ) ;
99+ public async Task < int > RemoveAllAsync < T > ( string pid = DEFAULT_PID , Expression < Func < T , bool > > ? where = null , CancellationToken token = default ) =>
100+ await dataSource . ExecuteAsync ( BuildCommandParams ( DeleteAllSql , pid , where ) , token ) ;
101101 public int RemoveAllExpired ( string pid = DEFAULT_PID ) =>
102102 dataSource . Execute ( new Ctx ( DeleteAllExpiredSql , CreateParams ( pid ) ) ) ;
103- public async Task < int > RemoveAllExpiredAsync ( string pid = DEFAULT_PID ) =>
104- await dataSource . ExecuteAsync ( new Ctx ( DeleteAllExpiredSql , CreateParams ( pid ) ) ) ;
103+ public async Task < int > RemoveAllExpiredAsync ( string pid = DEFAULT_PID , CancellationToken token = default ) =>
104+ await dataSource . ExecuteAsync ( new Ctx ( DeleteAllExpiredSql , CreateParams ( pid ) ) , token ) ;
105105 public int RemoveAllExpiredGlobal ( ) =>
106106 dataSource . Execute ( new Ctx ( DeleteAllExpiredGlobalSql ) ) ;
107- public async Task < int > RemoveAllExpiredGlobalAsync ( ) =>
108- await dataSource . ExecuteAsync ( new Ctx ( DeleteAllExpiredGlobalSql ) ) ;
107+ public async Task < int > RemoveAllExpiredGlobalAsync ( CancellationToken token = default ) =>
108+ await dataSource . ExecuteAsync ( new Ctx ( DeleteAllExpiredGlobalSql ) , token ) ;
109109 public T ? Get < T > ( string id , string pid = DEFAULT_PID ) =>
110110 dataSource . Execute < T > ( new Ctx ( SelectSql , CreateParams ( pid , id ) ) ) ;
111- public async Task < T ? > GetAsync < T > ( string id , string pid = DEFAULT_PID ) =>
112- await dataSource . ExecuteAsync < T > ( new Ctx ( SelectSql , CreateParams ( pid , id ) ) ) ;
111+ public async Task < T ? > GetAsync < T > ( string id , string pid = DEFAULT_PID , CancellationToken token = default ) =>
112+ await dataSource . ExecuteAsync < T > ( new Ctx ( SelectSql , CreateParams ( pid , id ) ) , token ) ;
113113 public bool Exists ( string id , string pid = DEFAULT_PID ) =>
114114 dataSource . Execute < bool > ( new Ctx ( ExistsSql , CreateParams ( pid , id ) ) ) ;
115- public async Task < bool > ExistsAsync ( string id , string pid = DEFAULT_PID ) =>
116- await dataSource . ExecuteAsync < bool > ( new Ctx ( ExistsSql , CreateParams ( pid , id ) ) ) ;
115+ public async Task < bool > ExistsAsync ( string id , string pid = DEFAULT_PID , CancellationToken token = default ) =>
116+ await dataSource . ExecuteAsync < bool > ( new Ctx ( ExistsSql , CreateParams ( pid , id ) ) , token ) ;
117117 public long Count ( string pid = DEFAULT_PID ) =>
118118 dataSource . Execute < long > ( new Ctx ( CountSql , CreateParams ( pid ) ) ) ;
119119 public long Count < T > ( string pid = DEFAULT_PID , Expression < Func < T , bool > > ? where = null ) =>
120120 dataSource . Execute < long > ( BuildCommandParams ( CountSql , pid , where ) ) ;
121- public async Task < long > CountAsync ( string pid = DEFAULT_PID ) =>
122- await dataSource . ExecuteAsync < long > ( new Ctx ( CountSql , CreateParams ( pid ) ) ) ;
123- public async Task < long > CountAsync < T > ( string pid = DEFAULT_PID , Expression < Func < T , bool > > ? where = null ) =>
124- await dataSource . ExecuteAsync < long > ( BuildCommandParams ( CountSql , pid , where ) ) ;
121+ public async Task < long > CountAsync ( string pid = DEFAULT_PID , CancellationToken token = default ) =>
122+ await dataSource . ExecuteAsync < long > ( new Ctx ( CountSql , CreateParams ( pid ) ) , token ) ;
123+ public async Task < long > CountAsync < T > ( string pid = DEFAULT_PID , Expression < Func < T , bool > > ? where = null , CancellationToken token = default ) =>
124+ await dataSource . ExecuteAsync < long > ( BuildCommandParams ( CountSql , pid , where ) , token ) ;
125125
126126 private Ctx BuildCommandParams < T > ( string sql , string pid = DEFAULT_PID , Expression < Func < T , bool > > ? where = null )
127127 {
@@ -136,7 +136,7 @@ private Ctx BuildCommandParams<T>(string sql, string pid = DEFAULT_PID, Expressi
136136 return new Ctx ( sql , baseParams ) ;
137137 }
138138
139- public IAsyncEnumerable < T > GetListAsync < T > ( string pid = DEFAULT_PID , Expression < Func < T , bool > > ? where = null , long ? limit = null , long ? offset = null )
139+ public IAsyncEnumerable < T > GetListAsync < T > ( string pid = DEFAULT_PID , Expression < Func < T , bool > > ? where = null , long ? limit = null , long ? offset = null , CancellationToken token = default )
140140 {
141141 var sql = $ "select value from { tableRef } where pid = @pid and (expires is null or now() < expires)";
142142 var baseParams = new List < NpgsqlParameter >
@@ -156,6 +156,6 @@ public IAsyncEnumerable<T> GetListAsync<T>(string pid = DEFAULT_PID, Expression<
156156 {
157157 sql += " limit @limit offset @offset" ;
158158 }
159- return dataSource . ExecuteListAsync < T > ( new Ctx ( sql , baseParams ) ) ;
159+ return dataSource . ExecuteListAsync < T > ( new Ctx ( sql , baseParams ) , token ) ;
160160 }
161161}
0 commit comments