diff --git a/cmd/template/dbdriver/files/service/mysql.tmpl b/cmd/template/dbdriver/files/service/mysql.tmpl index 93ee4895..c42b9306 100644 --- a/cmd/template/dbdriver/files/service/mysql.tmpl +++ b/cmd/template/dbdriver/files/service/mysql.tmpl @@ -13,8 +13,15 @@ import ( _ "github.com/joho/godotenv/autoload" ) +// Service represents a service that interacts with a database. type Service interface { + // Health returns a map of health status information. + // The keys and values in the map are service-specific. Health() map[string]string + + // Close terminates the database connection. + // It returns an error if the connection cannot be closed. + Close() error } type service struct { @@ -102,3 +109,12 @@ func (s *service) Health() map[string]string { return stats } + +// Close closes the database connection. +// It logs a message indicating the disconnection from the specific database. +// If the connection is successfully closed, it returns nil. +// If an error occurs while closing the connection, it returns the error. +func (s *service) Close() error { + log.Printf("Disconnected from database: %s", dbname) + return s.db.Close() +} diff --git a/cmd/template/dbdriver/files/service/postgres.tmpl b/cmd/template/dbdriver/files/service/postgres.tmpl index 95f34f27..d3f5ba6d 100644 --- a/cmd/template/dbdriver/files/service/postgres.tmpl +++ b/cmd/template/dbdriver/files/service/postgres.tmpl @@ -13,8 +13,15 @@ import ( _ "github.com/joho/godotenv/autoload" ) +// Service represents a service that interacts with a database. type Service interface { + // Health returns a map of health status information. + // The keys and values in the map are service-specific. Health() map[string]string + + // Close terminates the database connection. + // It returns an error if the connection cannot be closed. + Close() error } type service struct { @@ -96,3 +103,12 @@ func (s *service) Health() map[string]string { return stats } + +// Close closes the database connection. +// It logs a message indicating the disconnection from the specific database. +// If the connection is successfully closed, it returns nil. +// If an error occurs while closing the connection, it returns the error. +func (s *service) Close() error { + log.Printf("Disconnected from database: %s", database) + return s.db.Close() +} diff --git a/cmd/template/dbdriver/files/service/sqlite.tmpl b/cmd/template/dbdriver/files/service/sqlite.tmpl index 4959f539..d9e8315e 100644 --- a/cmd/template/dbdriver/files/service/sqlite.tmpl +++ b/cmd/template/dbdriver/files/service/sqlite.tmpl @@ -13,8 +13,15 @@ import ( _ "github.com/joho/godotenv/autoload" ) +// Service represents a service that interacts with a database. type Service interface { + // Health returns a map of health status information. + // The keys and values in the map are service-specific. Health() map[string]string + + // Close terminates the database connection. + // It returns an error if the connection cannot be closed. + Close() error } type service struct { @@ -95,3 +102,12 @@ func (s *service) Health() map[string]string { return stats } + +// Close closes the database connection. +// It logs a message indicating the disconnection from the specific database. +// If the connection is successfully closed, it returns nil. +// If an error occurs while closing the connection, it returns the error. +func (s *service) Close() error { + log.Printf("Disconnected from database: %s", dburl) + return s.db.Close() +}