| Title: | Database Agnostic Package to Generate and Process 'SQL' Queries in R |
|---|---|
| Description: | Allows the user to generate and execute select, insert, update and delete 'SQL' queries the underlying database without having to explicitly write 'SQL' code. |
| Authors: | Alejandro Baranek [cre, aut], Leonardo Belen [aut] |
| Maintainer: | Alejandro Baranek <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.2 |
| Built: | 2026-05-31 08:10:14 UTC |
| Source: | https://github.com/ropenstats/rsql |
Operator IN for multiple columns
x %IN% yx %IN% y
x |
vector x |
y |
vector y |
ken4rab
add_grep_exact_match
add_grep_exact_match(text)add_grep_exact_match(text)
text |
TEST |
ken4rab
Adds quotes to a string
add_quotes(text)add_quotes(text)
text |
The string to quote |
ken4rab
This function prepares data frame to be inserted in a db
assessRSqlDf(values.df)assessRSqlDf(values.df)
values.df |
Data Frame with corresponding values and fields as colnames |
ken4rab
cbind_coerced
cbind_coerced(...)cbind_coerced(...)
... |
The parameters |
ken4rab
Check fields and values are sound
check_fields_values(fields, values, min.length = 0)check_fields_values(fields, values, min.length = 0)
fields |
Fields names to check |
values |
values to check |
min.length |
for vectors |
ken4rab
Produces a RSQL object
createRSQL(drv, dbname, user = NULL, password = NULL, host = NULL, port = NULL)createRSQL(drv, dbname, user = NULL, password = NULL, host = NULL, port = NULL)
drv |
Driver name |
dbname |
Database name |
user |
Database user name |
password |
Database password |
host |
Database host |
port |
Database port |
ken4rab
Removes the quotes from the string
dequote(text)dequote(text)
text |
The string to remove the quotes from. |
ken4rab
Checks that the columns are in the data.frame
df_verify(dataframe, columns)df_verify(dataframe, columns)
dataframe |
The data.frame |
columns |
The columns to check |
ken4rab
genLogger
genLogger(r6.object)genLogger(r6.object)
r6.object |
a R6 object with a logger member |
ken4rab
getLogger
getLogger(r6.object)getLogger(r6.object)
r6.object |
a R6 object with a logger member |
ken4rab
getCarsdbPath
getMtcarsdbPath(copy = TRUE)getMtcarsdbPath(copy = TRUE)
copy |
a boolean that states whether it should be copied to the home directory or not. |
ken4rab
Gets the path of package data.
getPackageDir()getPackageDir()
ken4rab
Determines if the string is quoted or not
is_quoted(text, quotes_symbols = "'")is_quoted(text, quotes_symbols = "'")
text |
The text to test |
quotes_symbols |
The quotation characters |
ken4rab
This function returns true if x is a POSIXct object type
is.POSIXct(x)is.POSIXct(x)
x |
value to be checked as a POSIXct |
ken4rab
loggerSetupFile
loggerSetupFile(log.file, default.threshold = "info")loggerSetupFile(log.file, default.threshold = "info")
log.file |
filename to log on |
default.threshold |
default threshold for logger |
kenarab
Determines string type which needs quotes in an SQL statement
needs_quotes(text)needs_quotes(text)
text |
The text to test |
ken4rab
Parses a where clause.
parse_where_clause(where_clause_list = c())parse_where_clause(where_clause_list = c())
where_clause_list |
The list of params |
ken4rab
This functions remove original quotes and sets validated quotes for corresponding db. If it had no quotes, will only put corresponding quotes symbols
re_quote(text, quotes = "'")re_quote(text, quotes = "'")
text |
The string |
quotes |
The quotes |
ken4rab
This functions remove original quotes and sets validated quotes for corresponding db. If it had no quotes, will only put corresponding quotes symbols
re_quote_alt(text, quotes = "'")re_quote_alt(text, quotes = "'")
text |
The string |
quotes |
The quotes |
ken4rab
renames a column on a data.frame
rename_col(df, name, replace_name)rename_col(df, name, replace_name)
df |
The date.frame |
name |
The name of the column |
replace_name |
The new name of the column |
ken4rab
Replace NA with NULL in sql statement
replaceNAwithNULL(sql.code)replaceNAwithNULL(sql.code)
sql.code |
code to replace NA with NULL |
ken4rab
Removes quotes from the String
rm_quotes(text, quotes = "'")rm_quotes(text, quotes = "'")
text |
The string to remove quotes from |
quotes |
Quote characters |
ken4rab
Removes quotes from data.frame columns
rm_vector_quotes(text.vector)rm_vector_quotes(text.vector)
text.vector |
The text vector to remove quotes from. |
ken4rab
A package to work with SQL datasources in a simple manner
.onLoad(libname, pkgname).onLoad(libname, pkgname)
libname |
Library name |
pkgname |
Package name |
Alejandro Baranek <[email protected]>, Leonardo Javier Belen <[email protected]> Executes code while loading the package.
library(RSQL) library(RSQLite) db.name <- getMtcarsdbPath(copy = TRUE) rsql <- createRSQL(drv = RSQLite::SQLite(), dbname = db.name) select_sql <- rsql$gen_select( select_fields = "*", # c("wt", "qsec"), table = "mtcars", where_values = data.frame(carb = 8) ) mtcars.observed <- rsql$execute_select(select_sql) mtcars.observedlibrary(RSQL) library(RSQLite) db.name <- getMtcarsdbPath(copy = TRUE) rsql <- createRSQL(drv = RSQLite::SQLite(), dbname = db.name) select_sql <- rsql$gen_select( select_fields = "*", # c("wt", "qsec"), table = "mtcars", where_values = data.frame(carb = 8) ) mtcars.observed <- rsql$execute_select(select_sql) mtcars.observed
This class is intended to simplify SQL commands.
driverdriver name
db.namedatabase name
userdb user
passworddb password
hostdb host
portdb port
available.functionsfor generating select expressions
entity.field.regexpfor scrape a field or table expression
entity.select.regexpfor scrape a select expressions expression
connThe connection handler
valid.connChecks if connection is valid
last.queryThe last query
last.rsThe last resultset
results.classExpected class for db results for running dbClearResult
select.counterAn instance select counter
insert.counterAn instance insert counter
update.counterAn instance update counter
delete.counterAn instance delete counter
command.counterAn instance command counter
clear.rs.counterAn instance clear.rs.counter
tzlocal timezone
loggeris conigured logger for current class
new()
Initializes a connection
RSQL.class$new( drv, dbname, user = NULL, password = NULL, host = NULL, port = NULL, tz = Sys.timezone() )
drvdriver name
dbnamedatabase name
useruser name
passwordpassword
hosthost name
portport number
tzactual time zone
checkConnection()
Function which check if db connection is valid
RSQL.class$checkConnection()
conn object
connect()
Function which connects to database
RSQL.class$connect()
conn object
setupResultClassFromDriver()
Infer ResultsClass from corresponding driver. Implemented for SQLiteDriver & PqConnection
RSQL.class$setupResultClassFromDriver()
RSQL object
setupRegexp()
initialize regexp for scraping entities
RSQL.class$setupRegexp(force = FALSE)
forceforce setup?
regexp for scraping select expressions
finalize()
Class destructor
RSQL.class$finalize()
checkEntitiesNames()
Checks if an entity exists
RSQL.class$checkEntitiesNames(entities, entity.type)
entitiesentities to check
entity.typeentity type to check against
gen_select()
Generates a select
RSQL.class$gen_select( select_fields, table, where_fields = names(where_values), where_values = NULL, group_by = c(), order_by = c(), top = 0, distinct = FALSE )
select_fieldsfields to be selected
tabletable to select from
where_fieldsfields in the where clause
where_valuesvalues to the fields on the where clause
group_byfields to group by
order_byfields to order by
topwhere does the resultset starts?
distinctprovides a way to select distinct rows
gen_insert()
Generate insert statement
RSQL.class$gen_insert(table, values_df, insert_fields = names(values_df))
tableThe table to insert into
values_dfThe values to insert. Must be defined as data.frame of values
insert_fieldsthe fields to insert into
gen_update()
Generate insert statement
RSQL.class$gen_update( table, update_fields = names(values), values, where_fields = names(where_values), where_values = NULL )
tablethe table to insert into
update_fieldsthe fields to update
valuesthe values to update
where_fieldsa where clause to the insert
where_valuesthe values to add to the where clause
gen_delete()
Generate a delete statement
RSQL.class$gen_delete( table, where_fields = names(where_values), where_values = NULL )
tablethe table to insert into
where_fieldsa where clause to the insert
where_valuesthe fields to add to the where clause
execute_select()
Performs an execution on the database
RSQL.class$execute_select(sql_select)
sql_selectthe sql select statement to perform
execute_update()
Performs an update on the database
RSQL.class$execute_update(sql_update)
sql_updatethe sql update statement to perform
execute_insert()
Performs an insert on the database
RSQL.class$execute_insert(sql_insert)
sql_insertthe sql insert statement to perform
execute_get_insert()
Performs a select statement, if not exists, executes insert statement
RSQL.class$execute_get_insert(sql_select, sql_insert)
sql_selectthe sql select statement to perform
sql_insertthe sql insert statement to perform
execute_command()
Performs a command on the database
RSQL.class$execute_command(sql_command)
sql_commandthe sql statement to perform
execute_delete()
Performs an deletion on the database
RSQL.class$execute_delete(sql_delete)
sql_deletethe sql delete statement to perform
retrieve()
Performs an insert on the database. This is a composite function
RSQL.class$retrieve( table, fields_uk = names(values_uk), values_uk, fields = names(values), values = NULL, field_id = "id" )
tableThe table
fields_ukThe fields unique key
values_ukThe values unique key
fieldsThe fields (Not used. Included for compatibility)
valuesThe values (Not used. Included for compatibility)
field_idThe field of the serial id
retrieve_insert()
Obtain id if object exists on the database. Insert object if not.
RSQL.class$retrieve_insert( table, fields_uk = names(values_uk), values_uk, fields = names(values), values = data.frame(), field_id = "id" )
tableThe table
fields_ukThe fields unique key
values_ukThe values unique key
fieldsThe fields
valuesThe values
field_idThe field of the serial id
clearLastResult()
clearLast Result for avoiding nasty warning getSummary
RSQL.class$clearLastResult()
getSummary()
get RSQL summary string
RSQL.class$getSummary()
disconnect()
Disconnects the instance from the database
RSQL.class$disconnect()
clone()
The objects of this class are cloneable with this method.
RSQL.class$clone(deep = FALSE)
deepWhether to make a deep clone.
ken4rab
library(RSQL) library(RSQLite) db.name <- getMtcarsdbPath(copy = TRUE) rsql <- createRSQL(drv = RSQLite::SQLite(), dbname = db.name) select_sql <- rsql$gen_select( select_fields = "*", # c("wt", "qsec"), table = "mtcars", where_values = data.frame(carb = 8) ) mtcars.observed <- rsql$execute_select(select_sql) mtcars.observed mtcars.new <- mtcars.observed mtcars.new$carb <- 9 insert_sql <- rsql$gen_insert(table = "mtcars", values_df = mtcars.new) rsql$execute_insert(sql_insert = insert_sql) where_values_df <- data.frame(carb = 9) select_sql <- rsql$gen_select( select_fields = "*", # c("wt", "qsec"), table = "mtcars", where_values = data.frame(carb = 8) ) mtcars.observed <- rsql$execute_select(select_sql) mtcars.observedlibrary(RSQL) library(RSQLite) db.name <- getMtcarsdbPath(copy = TRUE) rsql <- createRSQL(drv = RSQLite::SQLite(), dbname = db.name) select_sql <- rsql$gen_select( select_fields = "*", # c("wt", "qsec"), table = "mtcars", where_values = data.frame(carb = 8) ) mtcars.observed <- rsql$execute_select(select_sql) mtcars.observed mtcars.new <- mtcars.observed mtcars.new$carb <- 9 insert_sql <- rsql$gen_insert(table = "mtcars", values_df = mtcars.new) rsql$execute_insert(sql_insert = insert_sql) where_values_df <- data.frame(carb = 9) select_sql <- rsql$gen_select( select_fields = "*", # c("wt", "qsec"), table = "mtcars", where_values = data.frame(carb = 8) ) mtcars.observed <- rsql$execute_select(select_sql) mtcars.observed
Executes a delete on the Database
sql_execute_delete(sql_delete, dbconn = NULL)sql_execute_delete(sql_delete, dbconn = NULL)
sql_delete |
The delete SQL |
dbconn |
The Database Connection to run the query against |
ken4rab
Executes the insert statement
sql_execute_get_insert(dbconn, sql_select, sql_insert, ...)sql_execute_get_insert(dbconn, sql_select, sql_insert, ...)
dbconn |
The db connection |
sql_select |
The SQL select query |
sql_insert |
The SQL insert query |
... |
other variables to considered. |
ken4rab
Executes a statement on the database.
sql_execute_insert(sql_insert, dbconn)sql_execute_insert(sql_insert, dbconn)
sql_insert |
The SQL String |
dbconn |
The Database Connection to run the query against |
ken4rab
Executes a select on the database
sql_execute_select(sql_select, dbconn = NULL)sql_execute_select(sql_select, dbconn = NULL)
sql_select |
The delete SQL |
dbconn |
The Database Connection to run the query against |
ken4rab
Executes an update on the database
sql_execute_update(sql_update, dbconn = NULL)sql_execute_update(sql_update, dbconn = NULL)
sql_update |
The update SQL |
dbconn |
The Database Connection to run the query against |
ken4rab
Generates a Delete Statement
sql_gen_delete(table, where_fields = names(where_values), where_values = NULL)sql_gen_delete(table, where_fields = names(where_values), where_values = NULL)
table |
The table from which the delete statement will be generated |
where_fields |
The fields used in the where section |
where_values |
The values used in the where section |
ken4rab
Generates an insert statement.
sql_gen_insert(table, values_df, insert_fields = names(values_df))sql_gen_insert(table, values_df, insert_fields = names(values_df))
table |
The table to be affected |
values_df |
The values to insert. Must be defined as data.frame of values |
insert_fields |
The fields to insert |
ken4rab
Generates a Select Statement
sql_gen_select( select_fields, table, where_fields = names(where_values), where_values = NULL, group_by = c(), order_by = c(), top = 0, distinct = FALSE )sql_gen_select( select_fields, table, where_fields = names(where_values), where_values = NULL, group_by = c(), order_by = c(), top = 0, distinct = FALSE )
select_fields |
The fields to be selected |
table |
The table to be used in the select |
where_fields |
The fields used in the where section |
where_values |
The values used in the where section |
group_by |
Group by fields |
order_by |
Order by fields |
top |
Retrieve top records |
distinct |
it adds a distinct clause to the query. |
ken4rab
Generates an update statement
sql_gen_update( table, update_fields = names(values), values, where_fields = names(where_values), where_values )sql_gen_update( table, update_fields = names(values), values, where_fields = names(where_values), where_values )
table |
The table to update |
update_fields |
The fields to update |
values |
The values to update |
where_fields |
The fields for where statement |
where_values |
The values for where statement |
ken4rab
Generates a where statement to be used on a SQL statement.
sql_gen_where(where_fields = names(where_values), where_values)sql_gen_where(where_fields = names(where_values), where_values)
where_fields |
The fields used in the where section |
where_values |
The values used in the where section |
ken4rab
Generates a where list statement to be used on a SQL statement.
sql_gen_where_list(where_fields, where_values)sql_gen_where_list(where_fields, where_values)
where_fields |
The fields used in the where section |
where_values |
The values used in the where section |
ken4rab
Generates a where (or) statement to be used on a SQL statement.
sql_gen_where_or(where_fields = names(where_values), where_values)sql_gen_where_or(where_fields = names(where_values), where_values)
where_fields |
The fields used in the where section |
where_values |
The values used in the where section |
ken4rab
Retrieves Statement
sql_retrieve( table, fields_uk = names(values_uk), values_uk, fields = names(values), values = NULL, field_id = "id", dbconn = NULL )sql_retrieve( table, fields_uk = names(values_uk), values_uk, fields = names(values), values = NULL, field_id = "id", dbconn = NULL )
table |
The table |
fields_uk |
The fields unique key |
values_uk |
The values unique key |
fields |
The fields (Not used. Included for compatibility) |
values |
The values (Not used. Included for compatibility) |
field_id |
The field of the serial id |
dbconn |
The database connection |
ken4rab
Retrieves or insert Statement
sql_retrieve_insert( table, fields_uk = names(values_uk), values_uk, fields = names(values), values = NULL, field_id = "id", dbconn = NULL )sql_retrieve_insert( table, fields_uk = names(values_uk), values_uk, fields = names(values), values = NULL, field_id = "id", dbconn = NULL )
table |
The table |
fields_uk |
The fields unique key |
values_uk |
The values unique key |
fields |
The fields |
values |
The values |
field_id |
The field of the serial id |
dbconn |
The database connection |
ken4rab
stuff quote characters in quoted or not quoted df for DSL or DML operations
stuff_df_quoted(text.df)stuff_df_quoted(text.df)
text.df |
Data Frame with corresponding values and fields as colnames |
ken4rab
Stuff quote symbol from text
stuff_quote(unquoted.text, quote = "'")stuff_quote(unquoted.text, quote = "'")
unquoted.text |
The unquoted string to stuff quotes from. |
quote |
The quoting symbol. Default is ' |
ken4rab
This function converts POSIXct columns to UTC for inserting in a database
times_to_utc(values.df)times_to_utc(values.df)
values.df |
Data Frame with corresponding values and fields as colnames |
ken4rab
Returns string w/o leading or trailing whitespace
trim(x)trim(x)
x |
The string |
ken4rab
Returns string w/o leading whitespace
trim_leading(x)trim_leading(x)
x |
The string |
ken4rab
Returns string w/o trailing whitespace
trim_trailing(x)trim_trailing(x)
x |
The string |
ken4rab