11import { ByteDatabase } from '../index' ;
2- import { sanitize } from '../functions/sanitize' ;
32import { DEFAULT_TABLE } from '../util/constants' ;
43
54export class RowManager {
6- _database : ByteDatabase ;
5+ private readonly _database : ByteDatabase ;
76
87 constructor ( db : ByteDatabase ) {
98 this . _database = db ;
@@ -15,7 +14,7 @@ export class RowManager {
1514 table : string = DEFAULT_TABLE ,
1615 ) {
1716 value = value !== 'string' ? JSON . stringify ( value ) : value ;
18- table = this . _database . options . sanitize ? sanitize ( table ) : table ;
17+ table = this . trySanitize ( table )
1918 return this . _database . _raw
2019 . prepare ( `INSERT INTO ${ table } (ID,Json) VALUES (?,?)` )
2120 . run ( key , value ) ;
@@ -27,22 +26,26 @@ export class RowManager {
2726 table : string = DEFAULT_TABLE ,
2827 ) {
2928 value = value !== 'string' ? JSON . stringify ( value ) : value ;
30- table = this . _database . options . sanitize ? sanitize ( table ) : table ;
29+ table = this . trySanitize ( table )
3130 return this . _database . _raw
3231 . prepare ( `UPDATE ${ table } SET Json = (?) WHERE ID = (?)` )
3332 . run ( key , value ) ;
3433 }
3534
35+ private trySanitize ( ...args : Parameters < ByteDatabase [ "trySanitize" ] > ) {
36+ return this . _database [ "trySanitize" ] ( ...args )
37+ }
38+
3639 findRowByKey ( key : string , table : string = DEFAULT_TABLE ) {
37- table = this . _database . options . sanitize ? sanitize ( table ) : table ;
40+ table = this . trySanitize ( table )
3841 const val = this . _database . _raw
3942 . prepare ( `SELECT Json FROM ${ table } WHERE ID = @key` )
4043 . get ( { key } ) ;
4144 return val != null ? JSON . parse ( val . Json ) : null ;
4245 }
4346
4447 findAllRows ( table : string = DEFAULT_TABLE ) {
45- table = this . _database . options . sanitize ? sanitize ( table ) : table ;
48+ table = this . trySanitize ( table )
4649 const val = this . _database . _raw . prepare ( `SELECT * FROM ${ table } ` ) . iterate ( ) ;
4750 const data : object [ ] = [ ] ;
4851 for ( const i of val ) {
@@ -57,12 +60,12 @@ export class RowManager {
5760 }
5861
5962 deleteAllRows ( table : string = DEFAULT_TABLE ) {
60- table = this . _database . options . sanitize ? sanitize ( table ) : table ;
63+ table = this . trySanitize ( table )
6164 return this . _database . _raw . prepare ( `DELETE FROM ${ table } ` ) . run ( ) ;
6265 }
6366
6467 deleteRowByKey ( key : string , table : string = DEFAULT_TABLE ) {
65- table = this . _database . options . sanitize ? sanitize ( table ) : table ;
68+ table = this . trySanitize ( table )
6669 return this . _database . _raw
6770 . prepare ( `DELETE FROM ${ table } WHERE ID = @key` )
6871 . run ( { key } ) ;
0 commit comments