Learn R Programming

⚠️There's a newer version (0.2.2) of this package.Take me there.

RSQL

Allows the user to generate and execute select, insert, update and delete ‘SQL’ queries the underlying database without having to explicitly write ‘SQL’ code.

ReleaseUsageDevelopment

How to get started

install.packages("RSQL")

How to get started (Development version)

Install the R package using the following commands on the R console:

devtools::install_github("rOpenStats/RSQL", build_opts = NULL)

A simple example

To get started execute the following commands:

# 0.  Load libraries
library(RSQL)

# 1.  RSQL
db.name <- getMtcarsdbPath()
rsql <- createRSQL(drv = RSQLite::SQLite(), dbname = db.name)
query_sql <- rsql$gen_select(
    select_fields = c("*"),
    table = "mtcars")

query_sql <- rsql$gen_select(
    select_fields = c("mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am"),
    table = "mtcars",
    where_fields = "gear",
    where_values = 4)
query_sql
#> [1] "select mpg, cyl, disp, hp, drat, wt, qsec, vs, am from mtcars where (gear) in  ( 4 )"

rsql$execute_select(query_sql)
#>     mpg cyl  disp  hp drat    wt  qsec vs am
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1
#> 4  24.4   4 146.7  62 3.69 3.190 20.00  1  0
#> 5  22.8   4 140.8  95 3.92 3.150 22.90  1  0
#> 6  19.2   6 167.6 123 3.92 3.440 18.30  1  0
#> 7  17.8   6 167.6 123 3.92 3.440 18.90  1  0
#> 8  32.4   4  78.7  66 4.08 2.200 19.47  1  1
#> 9  30.4   4  75.7  52 4.93 1.615 18.52  1  1
#> 10 33.9   4  71.1  65 4.22 1.835 19.90  1  1
#> 11 27.3   4  79.0  66 4.08 1.935 18.90  1  1
#> 12 21.4   4 121.0 109 4.11 2.780 18.60  1  1
update_sql <- rsql$gen_update(
    update_fields = c("vs"),
    values = 1,
    table = "mtcars",
    where_fields = "gear",
    where_values = 4)
update_sql
#> [1] "update mtcars set (vs)=('1') where (gear) in  ( 4 )"
rsql$execute_update(update_sql)
#> <SQLiteResult>
#>   SQL  update mtcars set (vs)=('1') where (gear) in  ( 4 )
#>   ROWS Fetched: 0 [complete]
#>        Changed: 12
rsql$execute_select(query_sql)
#> Warning: Closing open result set, pending rows
#>     mpg cyl  disp  hp drat    wt  qsec vs am
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  1  1
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  1  1
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1
#> 4  24.4   4 146.7  62 3.69 3.190 20.00  1  0
#> 5  22.8   4 140.8  95 3.92 3.150 22.90  1  0
#> 6  19.2   6 167.6 123 3.92 3.440 18.30  1  0
#> 7  17.8   6 167.6 123 3.92 3.440 18.90  1  0
#> 8  32.4   4  78.7  66 4.08 2.200 19.47  1  1
#> 9  30.4   4  75.7  52 4.93 1.615 18.52  1  1
#> 10 33.9   4  71.1  65 4.22 1.835 19.90  1  1
#> 11 27.3   4  79.0  66 4.08 1.935 18.90  1  1
#> 12 21.4   4 121.0 109 4.11 2.780 18.60  1  1
insert.df <- c(4, rep(99, 9))
names(insert.df) <- c("gear", "mpg", "cyl", "disp", "hp", "drat", "wt", "qsec", "vs", "am")
insert.df <- as.data.frame(t(insert.df))
insert_sql <- rsql$gen_insert(
    values = insert.df,
    table = "mtcars")
rsql$execute_insert(insert_sql)
#> <SQLiteResult>
#>   SQL  insert into mtcars(gear, mpg, cyl, disp, hp, drat, wt, qsec, vs, am) values   ( '4', '99', '99', '99', '99', '99', '99', '99', '99', '99' );
#>   ROWS Fetched: 0 [complete]
#>        Changed: 1
rsql$execute_select(query_sql)
#> Warning: Closing open result set, pending rows
#>     mpg cyl  disp  hp  drat     wt  qsec vs am
#> 1  21.0   6 160.0 110  3.90  2.620 16.46  1  1
#> 2  21.0   6 160.0 110  3.90  2.875 17.02  1  1
#> 3  22.8   4 108.0  93  3.85  2.320 18.61  1  1
#> 4  24.4   4 146.7  62  3.69  3.190 20.00  1  0
#> 5  22.8   4 140.8  95  3.92  3.150 22.90  1  0
#> 6  19.2   6 167.6 123  3.92  3.440 18.30  1  0
#> 7  17.8   6 167.6 123  3.92  3.440 18.90  1  0
#> 8  32.4   4  78.7  66  4.08  2.200 19.47  1  1
#> 9  30.4   4  75.7  52  4.93  1.615 18.52  1  1
#> 10 33.9   4  71.1  65  4.22  1.835 19.90  1  1
#> 11 27.3   4  79.0  66  4.08  1.935 18.90  1  1
#> 12 21.4   4 121.0 109  4.11  2.780 18.60  1  1
#> 13 99.0  99  99.0  99 99.00 99.000 99.00 99 99

Troubleshooting

Please note that the ‘RSQL’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('RSQL')

Monthly Downloads

265

Version

0.1.4

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Alejandro Baranek

Last Published

July 5th, 2020

Functions in RSQL (0.1.4)

parse_where_clause

Parses a where clause.
sql_gen_update

Generates an update statement
rename_col

renames a column on a data.frame
sql_gen_select

Generates a Select Statement
replaceNAwithNULL

Replace NA with NULL in sql statement
%IN%

Operator IN for multiple columns
sql_execute_delete

sql_execute_delete
cbind_coerced

TODO: WHAT DOES THIS DO AGAIN?
getPackageDir

Get package directory
createRSQL

Produces a RSQL object
stuff_quote

Stuff quote symbol from text
trim

Returns string w/o leading or trailing whitespace
rm_vector_quotes

Removes quotes from data.frame columns
sql_gen_insert

Generates an insert statement.
sql_gen_delete

Generates a Delete Statement
sql_gen_where

Generates a where statement to be used on a SQL statement.
sql_gen_where_list

Generates a where list statement to be used on a SQL statement.
sql_retrieve_insert

Retrieves or insert Statement
stuff_df_quoted

stuff quote characters in quoted or not quoted df for DSL or DML operations
re_quote

This functions remove original quotes and sets validated quotes for corresponding db. If it had no quotes, will only put corresponding quotes symbols
is_quoted

Determines if the string is quoted or not
needs_quotes

Determines string type which needs quotes in an SQL statement
sql_execute_insert

Executes a statement on the database.
sql_execute_update

Executes an update on the database
sql_execute_select

Executes a select on the database
sql_gen_where_or

Generates a where (or) statement to be used on a SQL statement.
sql_retrieve

Retrieves Statement
trim_leading

Returns string w/o leading whitespace
trim_trailing

Returns string w/o trailing whitespace
add_grep_exact_match

add_grep_exact_match
RSQL

rsql
getMtcarsdbPath

getCarsdbPath
execute_get_insert

Executes the insert statement
add_quotes

Adds quotes to a string
RSQL.class

The class that provides the SQL functionality.
dequote

Removes the quotes from the string
df_verify

Checks that the columns are in the data.frame
rm_quotes

Removes quotes from the String