Initiate this container at global level. Methods in this class can help admin to manage general information of SPS. For now it stores some meta data, the encryption key pairs and the account info. You can use this database to store other useful things, like user password hash, IP, browsing info ...
A SQLite database by default is created inside config
directory.
If not, you
can use createDb
method to create one. On initiation, this class checks
if the default db is there and gives warnings if not.
One instance of this class is created by the spsAccount super class in global.R, normal users don't need to change anything.
new()
initialize a new class object
spsDb$new()
createDb()
Create a SPS database
spsDb$createDb(db_name = "config/sps.db")
db_name
database path, you need to manually create parent directory if not exists
queryValue()
Query database
spsDb$queryValue(table, SELECT = "*", WHERE = "1", db_name = "config/sps.db")
table
table name
SELECT
SQL select grammar
WHERE
SQL where grammar
db_name
database path
query result, usually a dataframe
queryValueDp()
Query database with dplyr grammar
Only supports simple selections, like comparison, %in%, between()
,
is.na()
, etc. Advanced selections like wildcard,
using outside dplyr functions like [stringr::str_detect()]
,
[base::grepl()]
are not supported.
spsDb$queryValueDp( table, dp_expr = "select(., everything())", db_name = "config/sps.db" )
table
table name
dp_expr
dplyr chained expression, must use '.' in first component of the chain expression
db_name
database path
query result, usually a tibble
queryUpdate()
update(modify) the value in db
spsDb$queryUpdate(table, value, col, WHERE = "1", db_name = "config/sps.db")
table
table name
value
new value
col
which column
WHERE
SQL where statement, conditions to select rows
db_name
database path
queryDel()
delete value in db
spsDb$queryDel(table, WHERE = "1", db_name = "config/sps.db")
table
table name
WHERE
SQL where statement, conditions to select rows
db_name
database path
queryInsert()
Insert value to db
spsDb$queryInsert(table, value, db_name = "config/sps.db")
table
table name
value
new values for the entire row, collect all values from all columns in a vector.
db_name
database path
clone()
The objects of this class are cloneable with this method.
spsDb$clone(deep = FALSE)
deep
Whether to make a deep clone.
#>#>#>mydb$createDb()#>#>#>#>#>#>#> [SPS-DANGER] 2021-09-18 02:42:28 Done, Db created at 'config/sps.db'. DO NOT share this file with others or upload to open access domains. #> [SPS-INFO] 2021-09-18 02:42:28 Key md5 e0451d6976e6f8e3430b6253cb7d97d9mydb$queryValue("sps_meta")#>#>#> info value #> 1 creation_date 20210918024228mydb$queryInsert("sps_meta", value = "'new1', '1'")#>#>mydb$queryValue("sps_meta")#>#>#> info value #> 1 creation_date 20210918024228 #> 2 new1 1#>#>mydb$queryValue("sps_meta")#>#>#> info value #> 1 creation_date 20210918024228 #> 2 new1 1 #> 3 new2 2mydb$queryUpdate("sps_meta", value = '234', col = "value", WHERE = "info = 'new1'")#>#>mydb$queryValue("sps_meta")#>#>#> info value #> 1 creation_date 20210918024228 #> 2 new1 234 #> 3 new2 2if (FALSE) { library(dplyr) mydb$queryValueDp( "sps_meta", dp_expr="filter(., info %in% c('new1', 'new2') %>% select(2)") } mydb$queryDel("sps_meta", WHERE = "value = '234'")#>#>