Skip to content

Commit

Permalink
update package with new functionality for in statements
Browse files Browse the repository at this point in the history
  • Loading branch information
vikram-rawat committed Mar 20, 2024
1 parent 0db0526 commit c527da1
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 38 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: read.sql
Title: Reads .SQL Files And Execute Them
Version: 0.3.1
Version: 0.3.2
Authors@R:
person(
given = "Vikram",
Expand Down
22 changes: 20 additions & 2 deletions R/read_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ print.sql_query <- function(
#'
#' @param sql_query a sql_query object that will be used for sqlinterpolation
#' @param meta_query_params A list of values for interpolation in the SQL file
#' it can also deal with parameter where the we need to build an IN value in sql
#'
#' @return query object
#'
Expand All @@ -78,14 +79,31 @@ print.sql_query <- function(
meta_sql_interpolate <- function(sql_query, meta_query_params) {
# Loop over each item in the query_params list
for (param in names(meta_query_params)) {
# Replace the placeholder in the query with the parameter value
# Check if the parameter value is a vector
if (length(meta_query_params[[param]]) > 1) {
# Create a string of values, with numeric values not in
# parentheses and non-numeric values in parentheses
values <- sapply(meta_query_params[[param]], function(x) {
if (is.numeric(x)) {
return(as.character(x))
} else {
return(paste0("'", as.character(x), "'"))
}
})
# Join the values with commas
replacement <- paste(values, collapse = ", ")
} else {
# If not a vector, use the parameter value as is
replacement <- meta_query_params[[param]]
}
# Replace the placeholder in the query with the replacement string
sql_query <- stringi::stri_replace_all_fixed(
sql_query,
stringi::stri_sprintf(
format = "{%s}",
param
),
meta_query_params[[param]],
replacement,
vectorize_all = FALSE
)
}
Expand Down
38 changes: 28 additions & 10 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -153,33 +153,43 @@ query_params is an optional argument. You would only need it when you want to in
sometimes you will feel like you want to dynamically update the table name or column name in a query. This functionality is not available in `DBI::sqlInterpolate` function. Hence this has to be dealt with seperately. So there is an optional parameter called `meta_query_params` in the function `rs_interpolate` which will simply replace anything inside the `{}` curly brackets by matching it with named list. liket this

```{sql eval=FALSE}
select
*
from
{main_table}
where
{column1} >= ?mincol1
and
and
{column2} >= ?mincol2
and
{column3} in ({species_value})
and
`Petal.Width` in ({width_value})
```

to run a query like this we need to pass 2 values like this.

```{r eval=FALSE}
query_obj <- read.sql::rs_interpolate(
sql_query = sql_query_object, # object created from rs_read_query function
sql_conn = conn,
meta_query_params = list(
main_table = "iris",
column1 = "`Sepal.Length`",
column2 = "`Petal.Length`"
column2 = "`Petal.Length`",
column3 = "`Species`",
species_value = c("Setosa", "versicolor"),
width_value = seq(1.0, 1.4, 0.1)
),
query_params = list(
mincol1 = 5,
mincol2 = 5
mincol1 = 4,
mincol2 = 4
)
)
```


Expand All @@ -188,10 +198,14 @@ query_obj <- read.sql::rs_interpolate(

most common function you will use most of the time is this. This will execute the query that is read from the file or modified by function rs_interpolate.

query_obj |>
read.sql::rs_execute(
sql_conn = conn
)
```{r eval=FALSE}
query_obj |>
read.sql::rs_execute(
sql_conn = conn
)
```


The only thing different about it is that it has a method argument where if you need results from the DB you should use `get` all lower case. If you want to execute a delete, or update statement you should use `post` all lower case.

Expand All @@ -200,8 +214,12 @@ The only thing different about it is that it has a method argument where if you
This package also has a migration function. That runs files saved in the folder `sql/migrate/up` or `sql/migrate/down` depending on the boolean value of up arguement.


conn |>
rs_migrate()
```{r eval=FALSE}
conn |>
rs_migrate()
```


This will help you set up a DB again and again anytime you need it.

Expand Down
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,39 @@ inside the `{}` curly brackets by matching it with named list. liket
this

``` sql

select
*
from
{main_table}
where
{column1} >= ?mincol1
and
and
{column2} >= ?mincol2
and
{column3} in ({species_value})
and
`Petal.Width` in ({width_value})
```

to run a query like this we need to pass 2 values like this.

``` r

query_obj <- read.sql::rs_interpolate(
sql_query = sql_query_object, # object created from rs_read_query function
sql_conn = conn,
meta_query_params = list(
main_table = "iris",
column1 = "`Sepal.Length`",
column2 = "`Petal.Length`"
column2 = "`Petal.Length`",
column3 = "`Species`",
species_value = c("Setosa", "versicolor"),
width_value = seq(1.0, 1.4, 0.1)
),
query_params = list(
mincol1 = 5,
mincol2 = 5
mincol1 = 4,
mincol2 = 4
)
)
```
Expand All @@ -225,7 +234,12 @@ most common function you will use most of the time is this. This will
execute the query that is read from the file or modified by function
rs_interpolate.

query_obj \|\> read.sql::rs_execute( sql_conn = conn )
``` r
query_obj |>
read.sql::rs_execute(
sql_conn = conn
)
```

The only thing different about it is that it has a method argument where
if you need results from the DB you should use `get` all lower case. If
Expand All @@ -238,8 +252,10 @@ This package also has a migration function. That runs files saved in the
folder `sql/migrate/up` or `sql/migrate/down` depending on the boolean
value of up arguement.

conn |>
rs_migrate()
``` r
conn |>
rs_migrate()
```

This will help you set up a DB again and again anytime you need it.

Expand Down
6 changes: 5 additions & 1 deletion tests/sql/simplq_sql_interpolation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ from
{main_table}
where
{column1} >= ?mincol1
and
and
{column2} >= ?mincol2
and
{column3} in ({species_value})
and
`Petal.Width` in ({width_value})
23 changes: 6 additions & 17 deletions tests/testthat/test-db_func.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,18 @@ query_obj <- read.sql::rs_interpolate(
meta_query_params = list(
main_table = "iris",
column1 = "`Sepal.Length`",
column2 = "`Petal.Length`"
column2 = "`Petal.Length`",
column3 = "`Species`",
species_value = c("Setosa", "versicolor"),
width_value = seq(1.0, 1.4, 0.1)
),
query_params = list(
mincol1 = 5,
mincol2 = 5
mincol1 = 4,
mincol2 = 4
)
)

query_obj |>
read.sql::rs_execute(
sql_conn = conn
)

dbGetQuery(
conn,
"
select
*
from
'iris'
where
'sepal_length' >= 5
and
'petal_length' >= 5
"
)

0 comments on commit c527da1

Please sign in to comment.