brownag / gpkg

Utilities for the Open Geospatial Consortium (OGC) 'GeoPackage' Format in R
http://humus.rocks/gpkg/
Creative Commons Zero v1.0 Universal
18 stars 0 forks source link

in-place modification for `gpkg_connect()` #8

Closed brownag closed 1 year ago

brownag commented 1 year ago

Often there isn't a need to manually connect a geopackage object because connections are created and destroyed on the fly. However, when performing many database operations it may be beneficial to make use of a persistent connection to avoid the extra overhead of repeatedly closing and re-opening.

Currently, you need to overwrite the object to connect to the database and have that connection "stick".

library(gpkg)

g <- geopackage()
g
#> <geopackage>
#> --------------------------------------------------------------------------------
#> # of Tables: 0
#>  
#>  
#> --------------------------------------------------------------------------------

# doesnt work
gpkg_connect(g)
#> <geopackage>
#> --------------------------------------------------------------------------------
#> # of Tables: 0
#>  
#>  
#> --------------------------------------------------------------------------------
#> <SQLiteConnection>
#>   Path: /tmp/RtmpWPANIS/Rgpkg277b54d212bf9.gpkg
#>   Extensions: TRUE
g
#> <geopackage>
#> --------------------------------------------------------------------------------
#> # of Tables: 0
#>  
#>  
#> --------------------------------------------------------------------------------

# works
g <- gpkg_connect(g)
g
#> <geopackage>
#> --------------------------------------------------------------------------------
#> # of Tables: 0
#>  
#>  
#> --------------------------------------------------------------------------------
#> <SQLiteConnection>
#>   Path: /tmp/RtmpWPANIS/Rgpkg277b54d212bf9.gpkg
#>   Extensions: TRUE

# works
gpkg_disconnect(g)
g
#> <geopackage>
#> Error: Invalid or closed connection

Connecting to a database and not saving the result prevents from being able to reference the pointer in the future to be disconnected (which causes warnings: Warning message: call dbDisconnect() when finished working with a connection).

It would be nice to come up with a way that in-place modification of a geopackage object could be used to update the connection field.

brownag commented 1 year ago

Now w/ https://github.com/brownag/gpkg/commit/dfd755f5830bdbb49bccfaad1b0162131986b401 I get:

library(gpkg)

g <- geopackage()
g
#> <geopackage>
#> --------------------------------------------------------------------------------
#> # of Tables: 0
#>  
#>  
#> --------------------------------------------------------------------------------

# works
gpkg_connect(g)
#> <geopackage>
#> --------------------------------------------------------------------------------
#> # of Tables: 0
#>  
#>  
#> --------------------------------------------------------------------------------
#> <SQLiteConnection>
#>   Path: /tmp/RtmpKe9spX/Rgpkg2ea944417fc85.gpkg
#>   Extensions: TRUE
g
#> <geopackage>
#> --------------------------------------------------------------------------------
#> # of Tables: 0
#>  
#>  
#> --------------------------------------------------------------------------------
#> <SQLiteConnection>
#>   Path: /tmp/RtmpKe9spX/Rgpkg2ea944417fc85.gpkg
#>   Extensions: TRUE

# works
g <- gpkg_connect(g)
g
#> <geopackage>
#> --------------------------------------------------------------------------------
#> # of Tables: 0
#>  
#>  
#> --------------------------------------------------------------------------------
#> <SQLiteConnection>
#>   Path: /tmp/RtmpKe9spX/Rgpkg2ea944417fc85.gpkg
#>   Extensions: TRUE

# works
gpkg_disconnect(g)
g
#> <geopackage>
#> --------------------------------------------------------------------------------
#> # of Tables: 0
#>  
#>  
#> --------------------------------------------------------------------------------
#> <SQLiteConnection>
#>   DISCONNECTED
brownag commented 1 year ago

Reopening, may be closed by #11