Skip to content

Commit

Permalink
open_db works now
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Dec 14, 2023
1 parent f9f83e2 commit c43003f
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

*/temp
*/*/temp
inst/doc
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ Description: What the package does (one paragraph).
License: MIT + file LICENSE
Imports:
methods,
magrittr,
dbplyr, dplyr,
magrittr, purrr,
dbplyr, dplyr,
DBI,
RMySQL,
crayon
Suggests:
knitr,
RMariaDB,
rmarkdown,
testthat (>= 3.0.0)
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
VignetteBuilder: knitr
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
# Generated by roxygen2: do not edit by hand

export(dbRemoveTables_like)
export(db_append)
export(db_info)
export(get_dbInfo)
export(import_table_large)
export(open_mariadb)
export(open_mysql)
export(tbl_copy)
import(DBI)
import(crayon)
import(dplyr)
import(magrittr)
importFrom(methods,new)
importFrom(purrr,`%||%`)
importFrom(utils,str)
39 changes: 36 additions & 3 deletions R/tool_dbase.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,33 @@ setMethod("dbSendQuery", c("MySQLConnection", "character"),
}
)

#' get_dbInfo
#'
#' Get DataBase info from `~/.db.yml`. You need to write config first, see
#' `vignette("database_config")` for details.
#'
#' @importFrom purrr `%||%`
#' @export
get_dbInfo <- function(name = NULL) {
config = yaml::read_yaml("~/.db.yml")
name = name %||% names(config)[1]
if(name == "all") config else config[[name]]
}

#' Con database connection
#'
#' @examples
#' \dontrun{
#' dbinfo = get_dbInfo() # see which db to read
#' open_mysql()
#' open_mariadb()
#' }
#'
#' @export
#' @import DBI
open_mysql <- function(dbinfo, dbname=1) {
open_mysql <- function(dbname=1, dbinfo=NULL) {
dbinfo = dbinfo %||% get_dbInfo()

if (is.numeric(dbname)) dbname = dbinfo$dbname[dbname]
bold(Ipaper::ok(sprintf("[info] opening db: %s", dbname)))

Expand All @@ -39,7 +64,11 @@ open_mysql <- function(dbinfo, dbname=1) {
return(con)
}

open_mariadb <- function(dbinfo, dbname=1) {
#' @rdname open_mysql
#' @export
open_mariadb <- function(dbname=1, dbinfo=NULL) {
dbinfo = dbinfo %||% get_dbInfo()

if (is.numeric(dbname)) dbname = dbinfo$dbname[dbname]
bold(ok(sprintf("[info] opening db: %s", dbname)))

Expand All @@ -61,13 +90,14 @@ open_mariadb <- function(dbinfo, dbname=1) {
return(con)
}


#' @export
db_info <- function(con) {
str(dbGetInfo(con))
}

# copy_to(con, st_met2481)
# `copy_to` not work for mysql
#' @export
tbl_copy <- function(con, tbl, tbl_name = NULL, overwrite = TRUE, row.names=FALSE, ...) {
if (is.null(tbl_name)) tbl_name = deparse(substitute(tbl))
t = system.time({
Expand All @@ -81,11 +111,13 @@ tbl_copy <- function(con, tbl, tbl_name = NULL, overwrite = TRUE, row.names=FALS
DBI::dbExecute(con, cmd_encode)
}

#' @export
db_append <- function(con, tbl, values) {
dbWriteTable(con, tbl, values, append = TRUE)
}


#' @export
dbRemoveTables_like <- function(con, pattern="dbplyr", del=TRUE) {
tbls_bad = dbListTables(con) %>% .[grep(pattern, .)]
# print(tbls_bad)
Expand All @@ -96,6 +128,7 @@ dbRemoveTables_like <- function(con, pattern="dbplyr", del=TRUE) {
}


#' @export
import_table_large <- function(con, df, table, chunksize=1e6) {
n = nrow(df)
# chunksize = 1e6
Expand Down
12 changes: 12 additions & 0 deletions man/get_dbInfo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions man/open_mysql.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vignettes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.html
*.R
60 changes: 60 additions & 0 deletions vignettes/database_config.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: "database_config"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{database_config}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```

## 如何配置数据库

```{r setup}
# library(tidydb2)
usethis::edit_file("~/.db.yml")
```

按照如下格式填写,可以填写多个配置。
在不指定`name``dbname`的情况下,默认读取第一个。

```
kong-nas:
host: kong-nas
user: CUG
pwd: passwd
port: 3306
dbname:
- ChinaMet
remote-nas:
host: xxxhydro.top
user: CUG
pwd: pwd
port: 2306
dbname:
- ChinaMet
default: kong-nas
```

## 测试连接

> 这里有改进空间
```{r, eval=FALSE}
# source('scripts/main_pkgs.R')
config = yaml::read_yaml("~/.db.yml")
dbinfo = get_dbInfo() # see which db to read
con_mysql = open_mysql()
con_mariadb = open_mariadb(1, dbinfo)
```


0 comments on commit c43003f

Please sign in to comment.