Browse Source

Añadida función para crear una copia de seguridad de la base de datos.

main
Costa 2 years ago
parent
commit
d1f684a4f3
4 changed files with 145 additions and 29 deletions
  1. +42
    -0
      Docs/sqlFunctions-doc.Rmd
  2. +88
    -28
      Docs/sqlFunctions-doc.html
  3. +11
    -0
      sqlFunctions.R
  4. +4
    -1
      workflow.R

+ 42
- 0
Docs/sqlFunctions-doc.Rmd

@ -87,6 +87,48 @@ sqlInitialize<-function(){
--- ---
## sqlBackUp
### Description
Creates a Back Up copy of the database.
### Usage
sqlBackUp(dbfile=file,bu.dir="BU_OVARIO")
### Arguments
Argument|Description
---|---
dbfile| Database File location.
bu.dir| Directory under the DB file where the back up will be placed.
### Details
Creates a Back Up copy of the database. It adds the date in front of the back up file.
### Value
Invisibly for success (and failures cause errors).
### Examples
```r
sqlInitialize()
sqlBackUp()
```
### Function
```r
sqlBackUp<-function(dbfile=file,bu.dir="BU_OVARIO"){
db=strsplit(dbfile, "/")[[1]]%>% tail(n=1)
bu_path<-gsub(db,bu.dir,dbfile)
if (!dir.exists(bu_path)){
dir.create(bu_path)
print(paste0("Back Up directory ", bu_path, " created"))
}
cp_bu<-paste0(bu_path, "/", format(Sys.time(), format="%Y%m%d"),"-",db)
file.copy(dbfile, cp_bu)
}
```
---
## sqlShowSamples ## sqlShowSamples
### Description ### Description

+ 88
- 28
Docs/sqlFunctions-doc.html

@ -171,6 +171,7 @@ pre code {
<ul> <ul>
<li><a href="#sqldroplast">sqlDropLast</a></li> <li><a href="#sqldroplast">sqlDropLast</a></li>
<li><a href="#sqlinitizalize">sqlInitizalize</a></li> <li><a href="#sqlinitizalize">sqlInitizalize</a></li>
<li><a href="#sqlbackup">sqlBackUp</a></li>
<li><a href="#sqlshowsamples">sqlShowSamples</a></li> <li><a href="#sqlshowsamples">sqlShowSamples</a></li>
<li><a href="#sqlgenovid">sqlGenOVID</a></li> <li><a href="#sqlgenovid">sqlGenOVID</a></li>
<li><a href="#sqlwritetemp">sqlWriteTemp</a></li> <li><a href="#sqlwritetemp">sqlWriteTemp</a></li>
@ -289,17 +290,76 @@ sqlDropLast(dta, "TableTest")
<hr /> <hr />
</div> </div>
</div> </div>
<div id="sqlbackup" class="section level2">
<h2>sqlBackUp</h2>
<div id="description-2" class="section level3">
<h3>Description</h3>
<p>Creates a Back Up copy of the database.</p>
</div>
<div id="usage-2" class="section level3">
<h3>Usage</h3>
<p>sqlBackUp(dbfile=file,bu.dir=“BU_OVARIO”)</p>
</div>
<div id="arguments-2" class="section level3">
<h3>Arguments</h3>
<table>
<thead>
<tr class="header">
<th>Argument</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>dbfile</td>
<td>Database File location.</td>
</tr>
<tr class="even">
<td>bu.dir</td>
<td>Directory under the DB file where the back up will be placed.</td>
</tr>
</tbody>
</table>
</div>
<div id="details-2" class="section level3">
<h3>Details</h3>
<p>Creates a Back Up copy of the database. It adds the date in front of the back up file.</p>
</div>
<div id="value-2" class="section level3">
<h3>Value</h3>
<p>Invisibly for success (and failures cause errors).</p>
</div>
<div id="examples-2" class="section level3">
<h3>Examples</h3>
<pre class="r"><code>sqlInitialize()
sqlBackUp()</code></pre>
</div>
<div id="function-2" class="section level3">
<h3>Function</h3>
<pre class="r"><code>sqlBackUp&lt;-function(dbfile=file,bu.dir=&quot;BU_OVARIO&quot;){
db=strsplit(dbfile, &quot;/&quot;)[[1]]%&gt;% tail(n=1)
bu_path&lt;-gsub(db,bu.dir,dbfile)
if (!dir.exists(bu_path)){
dir.create(bu_path)
print(paste0(&quot;Back Up directory &quot;, bu_path, &quot; created&quot;))
}
cp_bu&lt;-paste0(bu_path, &quot;/&quot;, format(Sys.time(), format=&quot;%Y%m%d&quot;),&quot;-&quot;,db)
file.copy(dbfile, cp_bu)
}</code></pre>
<hr />
</div>
</div>
<div id="sqlshowsamples" class="section level2"> <div id="sqlshowsamples" class="section level2">
<h2>sqlShowSamples</h2> <h2>sqlShowSamples</h2>
<div id="description-2" class="section level3">
<div id="description-3" class="section level3">
<h3>Description</h3> <h3>Description</h3>
<p>Shows if there are already samples from the specified NHCs.</p> <p>Shows if there are already samples from the specified NHCs.</p>
</div> </div>
<div id="usage-2" class="section level3">
<div id="usage-3" class="section level3">
<h3>Usage</h3> <h3>Usage</h3>
<p>sqlShowSamples(conn=dta, nhcs=nhc.test, verb=F)</p> <p>sqlShowSamples(conn=dta, nhcs=nhc.test, verb=F)</p>
</div> </div>
<div id="arguments-2" class="section level3">
<div id="arguments-3" class="section level3">
<h3>Arguments</h3> <h3>Arguments</h3>
<table> <table>
<thead> <thead>
@ -324,21 +384,21 @@ sqlDropLast(dta, "TableTest")
</tbody> </tbody>
</table> </table>
</div> </div>
<div id="details-2" class="section level3">
<div id="details-3" class="section level3">
<h3>Details</h3> <h3>Details</h3>
<p>Takes the NHCs listed in the nhcs vector and checks if there are already samples from those patients.</p> <p>Takes the NHCs listed in the nhcs vector and checks if there are already samples from those patients.</p>
</div> </div>
<div id="value-2" class="section level3">
<div id="value-3" class="section level3">
<h3>Value</h3> <h3>Value</h3>
<p>A data.frame with information about the patients.</p> <p>A data.frame with information about the patients.</p>
</div> </div>
<div id="examples-2" class="section level3">
<div id="examples-3" class="section level3">
<h3>Examples</h3> <h3>Examples</h3>
<pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;) <pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;)
nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;) nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;)
sqlShowSamples()</code></pre> sqlShowSamples()</code></pre>
</div> </div>
<div id="function-2" class="section level3">
<div id="function-3" class="section level3">
<h3>Function</h3> <h3>Function</h3>
<pre class="r"><code>sqlShowSamples&lt;-function(conn=dta, nhcs=nhc.test, verb=F){ <pre class="r"><code>sqlShowSamples&lt;-function(conn=dta, nhcs=nhc.test, verb=F){
if (isFALSE(verb)){ if (isFALSE(verb)){
@ -359,15 +419,15 @@ sqlShowSamples()
</div> </div>
<div id="sqlgenovid" class="section level2"> <div id="sqlgenovid" class="section level2">
<h2>sqlGenOVID</h2> <h2>sqlGenOVID</h2>
<div id="description-3" class="section level3">
<div id="description-4" class="section level3">
<h3>Description</h3> <h3>Description</h3>
<p>Generates new consecutive OVID code for the patients that are not found in the DB.</p> <p>Generates new consecutive OVID code for the patients that are not found in the DB.</p>
</div> </div>
<div id="usage-3" class="section level3">
<div id="usage-4" class="section level3">
<h3>Usage</h3> <h3>Usage</h3>
<p>sqlGenOVID(conn=dta, nhcs=nhc.test, verb=T, sinc=F)</p> <p>sqlGenOVID(conn=dta, nhcs=nhc.test, verb=T, sinc=F)</p>
</div> </div>
<div id="arguments-3" class="section level3">
<div id="arguments-4" class="section level3">
<h3>Arguments</h3> <h3>Arguments</h3>
<table> <table>
<colgroup> <colgroup>
@ -400,21 +460,21 @@ sqlShowSamples()
</tbody> </tbody>
</table> </table>
</div> </div>
<div id="details-3" class="section level3">
<div id="details-4" class="section level3">
<h3>Details</h3> <h3>Details</h3>
<p>Generates new consecutive OVID code for the patients that are not found in the DB.</p> <p>Generates new consecutive OVID code for the patients that are not found in the DB.</p>
</div> </div>
<div id="value-3" class="section level3">
<div id="value-4" class="section level3">
<h3>Value</h3> <h3>Value</h3>
<p>If verb is TRUE, it returns a data.frame.</p> <p>If verb is TRUE, it returns a data.frame.</p>
</div> </div>
<div id="examples-3" class="section level3">
<div id="examples-4" class="section level3">
<h3>Examples</h3> <h3>Examples</h3>
<pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;) <pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;)
nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;) nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;)
sqlGenOVID(sinc=T)</code></pre> sqlGenOVID(sinc=T)</code></pre>
</div> </div>
<div id="function-3" class="section level3">
<div id="function-4" class="section level3">
<h3>Function</h3> <h3>Function</h3>
<pre class="r"><code>sqlGenOVID&lt;-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F){ <pre class="r"><code>sqlGenOVID&lt;-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F){
ovid&lt;-sqlFetch(conn,&quot;OVID&quot;) ovid&lt;-sqlFetch(conn,&quot;OVID&quot;)
@ -440,15 +500,15 @@ sqlGenOVID(sinc=T)
</div> </div>
<div id="sqlwritetemp" class="section level2"> <div id="sqlwritetemp" class="section level2">
<h2>sqlWriteTemp</h2> <h2>sqlWriteTemp</h2>
<div id="description-4" class="section level3">
<div id="description-5" class="section level3">
<h3>Description</h3> <h3>Description</h3>
<p>Fills the Query Template file with the OVID and OV newly generated codes.</p> <p>Fills the Query Template file with the OVID and OV newly generated codes.</p>
</div> </div>
<div id="usage-4" class="section level3">
<div id="usage-5" class="section level3">
<h3>Usage</h3> <h3>Usage</h3>
<p>sqlWriteTemp(conn=dta, nhcs=nhc.test, file=“queryOV.xlsx”, samples.mod=T, clinics.mod=T)</p> <p>sqlWriteTemp(conn=dta, nhcs=nhc.test, file=“queryOV.xlsx”, samples.mod=T, clinics.mod=T)</p>
</div> </div>
<div id="arguments-4" class="section level3">
<div id="arguments-5" class="section level3">
<h3>Arguments</h3> <h3>Arguments</h3>
<table> <table>
<thead> <thead>
@ -481,22 +541,22 @@ sqlGenOVID(sinc=T)
</tbody> </tbody>
</table> </table>
</div> </div>
<div id="details-4" class="section level3">
<div id="details-5" class="section level3">
<h3>Details</h3> <h3>Details</h3>
<p>Fills the Query Template file with the OVID and OV newly generated codes. It is required that the DB has been updated with the sqlGenOVID function. It replaces previous content in the template file sheets that are filled. In the case of “CLINICS” table, if there were already an entry in the DB for that OVID code, the template file is filled with that information.</p> <p>Fills the Query Template file with the OVID and OV newly generated codes. It is required that the DB has been updated with the sqlGenOVID function. It replaces previous content in the template file sheets that are filled. In the case of “CLINICS” table, if there were already an entry in the DB for that OVID code, the template file is filled with that information.</p>
</div> </div>
<div id="value-4" class="section level3">
<div id="value-5" class="section level3">
<h3>Value</h3> <h3>Value</h3>
<p>Invisibly for success (and failures cause errors).</p> <p>Invisibly for success (and failures cause errors).</p>
</div> </div>
<div id="examples-4" class="section level3">
<div id="examples-5" class="section level3">
<h3>Examples</h3> <h3>Examples</h3>
<pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;) <pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;)
nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;) nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;)
sqlGenOVID(sinc=T) sqlGenOVID(sinc=T)
sqlWriteTemp()</code></pre> sqlWriteTemp()</code></pre>
</div> </div>
<div id="function-4" class="section level3">
<div id="function-5" class="section level3">
<h3>Function</h3> <h3>Function</h3>
<pre class="r"><code>sqlWriteTemp&lt;-function(conn=dta, nhcs=nhc.test, file=&quot;queryOV.xlsx&quot;, samples.mod=T, clinics.mod=T){ <pre class="r"><code>sqlWriteTemp&lt;-function(conn=dta, nhcs=nhc.test, file=&quot;queryOV.xlsx&quot;, samples.mod=T, clinics.mod=T){
upd.ovid&lt;-sqlFetch(conn, &quot;OVID&quot;) %&gt;% filter(NHC %in% nhcs) upd.ovid&lt;-sqlFetch(conn, &quot;OVID&quot;) %&gt;% filter(NHC %in% nhcs)
@ -535,15 +595,15 @@ sqlWriteTemp()
</div> </div>
<div id="sqlsincbd" class="section level2"> <div id="sqlsincbd" class="section level2">
<h2>sqlSincBD</h2> <h2>sqlSincBD</h2>
<div id="description-5" class="section level3">
<div id="description-6" class="section level3">
<h3>Description</h3> <h3>Description</h3>
<p>Updates the DB with the information filled in the template file.</p> <p>Updates the DB with the information filled in the template file.</p>
</div> </div>
<div id="usage-5" class="section level3">
<div id="usage-6" class="section level3">
<h3>Usage</h3> <h3>Usage</h3>
<p>sqlSincBD(conn=dta, filetemp=“QueryOV.xlsx”, sinc.samples=F, sinc.clinics=F)</p> <p>sqlSincBD(conn=dta, filetemp=“QueryOV.xlsx”, sinc.samples=F, sinc.clinics=F)</p>
</div> </div>
<div id="arguments-5" class="section level3">
<div id="arguments-6" class="section level3">
<h3>Arguments</h3> <h3>Arguments</h3>
<table> <table>
<colgroup> <colgroup>
@ -576,15 +636,15 @@ sqlWriteTemp()
</tbody> </tbody>
</table> </table>
</div> </div>
<div id="details-5" class="section level3">
<div id="details-6" class="section level3">
<h3>Details</h3> <h3>Details</h3>
<p>Updates the DB with the information filled in the template file. All the “samples” entries are added as new rows (as all samples are new even if the patient was already in the DB). The new patients included in the “CLINICS” sheet are introduced in the DB as new rows and the ones that were already there are modified in its previous row location.</p> <p>Updates the DB with the information filled in the template file. All the “samples” entries are added as new rows (as all samples are new even if the patient was already in the DB). The new patients included in the “CLINICS” sheet are introduced in the DB as new rows and the ones that were already there are modified in its previous row location.</p>
</div> </div>
<div id="value-5" class="section level3">
<div id="value-6" class="section level3">
<h3>Value</h3> <h3>Value</h3>
<p>Invisibly for success (and failures cause errors).</p> <p>Invisibly for success (and failures cause errors).</p>
</div> </div>
<div id="examples-5" class="section level3">
<div id="examples-6" class="section level3">
<h3>Examples</h3> <h3>Examples</h3>
<pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;) <pre class="r"><code>dta&lt;-odbcConnect(&quot;test&quot;)
nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;) nhc.test&lt;-c(&quot;XXXXXXXX&quot;,&quot;XXXXXXX&quot;)
@ -592,7 +652,7 @@ sqlGenOVID(sinc=T)
sqlWriteTemp() sqlWriteTemp()
sqlSincBD(sinc.samples=T, sinc.clinics=T)</code></pre> sqlSincBD(sinc.samples=T, sinc.clinics=T)</code></pre>
</div> </div>
<div id="function-5" class="section level3">
<div id="function-6" class="section level3">
<h3>Function</h3> <h3>Function</h3>
<pre class="r"><code>sqlSincBD&lt;-function(conn=dta, filetemp=&quot;QueryOV.xlsx&quot;, sinc.samples=F, sinc.clinics=F){ <pre class="r"><code>sqlSincBD&lt;-function(conn=dta, filetemp=&quot;QueryOV.xlsx&quot;, sinc.samples=F, sinc.clinics=F){
## Añadir código de muestra nueva a la base de datos ## Añadir código de muestra nueva a la base de datos

+ 11
- 0
sqlFunctions.R

@ -15,6 +15,17 @@ sqlInitialize<-function(){
source("ruta_database.R", encoding = "UTF-8") source("ruta_database.R", encoding = "UTF-8")
} }
sqlBackUp<-function(dbfile=file,bu.dir="BU_OVARIO"){
db=strsplit(dbfile, "/")[[1]]%>% tail(n=1)
bu_path<-gsub(db,bu.dir,dbfile)
if (!dir.exists(bu_path)){
dir.create(bu_path)
print(paste0("Back Up directory ", bu_path, " created"))
}
cp_bu<-paste0(bu_path, "/", format(Sys.time(), format="%Y%m%d"),"-",db)
file.copy(dbfile, cp_bu)
}
sqlShowSamples<-function(conn=dta, nhcs=nhc.test, verb=F){ sqlShowSamples<-function(conn=dta, nhcs=nhc.test, verb=F){
if (isFALSE(verb)){ if (isFALSE(verb)){
sqlQuery(conn, "SELECT O.NHC,S.* sqlQuery(conn, "SELECT O.NHC,S.*

+ 4
- 1
workflow.R

@ -1,5 +1,8 @@
source("sqlFunctions.R") source("sqlFunctions.R")
# Crear copia de seguridad
sqlBackUp()
# Cargar dependencias # Cargar dependencias
sqlInitialize() sqlInitialize()
@ -21,4 +24,4 @@ sqlWriteTemp()
#### ####
# Sincronizar lo rellenado en las pestañas samples y clinics # Sincronizar lo rellenado en las pestañas samples y clinics
sqlSincBD(sinc.samples = T, sinc.clinics = T)
sqlSincBD(sinc.samples = T, sinc.clinics = T)

Loading…
Cancel
Save