good morning!!!!

Skip to content
Snippets Groups Projects
Commit b22bcb38 authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

Documentation: reviewing error.go, wrapper.go y main_test.go.

parent bdd1debc
Branches
Tags
No related merge requests found
/* // Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam
Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam //
// Permission is hereby granted, free of charge, to any person obtaining
Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the
a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including
"Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish,
without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to
distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to
permit persons to whom the Software is furnished to do so, subject to // the following conditions:
the following conditions: //
// The above copyright notice and this permission notice shall be
The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software.
included in all copies or substantial portions of the Software. //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package db package db
...@@ -27,7 +25,7 @@ import ( ...@@ -27,7 +25,7 @@ import (
"errors" "errors"
) )
// Application error messages. // Shared error messages.
var ( var (
ErrExpectingPointer = errors.New(`Argument must be an address.`) ErrExpectingPointer = errors.New(`Argument must be an address.`)
ErrExpectingSlicePointer = errors.New(`Argument must be a slice address.`) ErrExpectingSlicePointer = errors.New(`Argument must be a slice address.`)
...@@ -46,7 +44,7 @@ var ( ...@@ -46,7 +44,7 @@ var (
ErrUnsupported = errors.New(`This action is currently unsupported on this database.`) ErrUnsupported = errors.New(`This action is currently unsupported on this database.`)
ErrQueryIsPending = errors.New(`Can't execute this instruction while the result set is still open.`) ErrQueryIsPending = errors.New(`Can't execute this instruction while the result set is still open.`)
ErrUnsupportedDestination = errors.New(`Unsupported destination type.`) ErrUnsupportedDestination = errors.New(`Unsupported destination type.`)
)
// Deprecated but kept for backwards compatibility. See: https://github.com/upper/db/issues/18 // Deprecated but kept for backwards compatibility. See: https://github.com/upper/db/issues/18
ErrCollectionDoesNotExists = ErrCollectionDoesNotExist var ErrCollectionDoesNotExists = ErrCollectionDoesNotExist
)
// Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package db_test package db_test
import ( import (
......
/* // Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam
Copyright (c) 2012-2014 José Carlos Nieto, https://menteslibres.net/xiam //
// Permission is hereby granted, free of charge, to any person obtaining
Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the
a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including
"Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish,
without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to
distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to
permit persons to whom the Software is furnished to do so, subject to // the following conditions:
the following conditions: //
// The above copyright notice and this permission notice shall be
The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software.
included in all copies or substantial portions of the Software. //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package db package db
...@@ -28,34 +26,35 @@ import ( ...@@ -28,34 +26,35 @@ import (
"reflect" "reflect"
) )
// Registered wrappers. // This map holds a copy of all registered adapters.
var wrappers = make(map[string]Database) var wrappers = make(map[string]Database)
// Registers a database wrapper with a unique name. // The db.Register() function is provided for database adapters. Using
func Register(name string, driver Database) { // db.Register() an adapter can make itself available by the provided name.
// The adapter name must not be an empty string and the driver must not be nil,
// otherwise db.Register() will panic.
func Register(name string, adapter Database) {
if name == "" { if name == `` {
panic("Missing wrapper name.") panic(`Missing adapter name.`)
} }
if _, ok := wrappers[name]; ok != false { if _, ok := wrappers[name]; ok != false {
panic("Register called twice for driver " + name) panic(`db.Register() called twice for adapter: ` + name)
} }
wrappers[name] = driver wrappers[name] = adapter
} }
// Configures a connection to a database using the named adapter and the given // Configures a database sessions using the given adapter and the given
// settings. // settings.
func Open(name string, settings Settings) (Database, error) { func Open(name string, settings Settings) (Database, error) {
driver, ok := wrappers[name] driver, ok := wrappers[name]
if ok == false { if ok == false {
// Using panic instead of returning error because attemping to use an // Using panic instead of returning error because attemping to use an
// nonexistent adapter will never result in a successful connection, // nonexistent adapter will never result in a successful connection.
// therefore should be considered a developer's mistake and must be catched panic(fmt.Sprintf(`Open: Unknown adapter %s. (see: https://upper.io/db#database-adapters)`, name))
// at compilation time.
panic(fmt.Sprintf("Open: Unknown adapter %s. (see: https://upper.io/db#database-adapters)", name))
} }
// Creating a new connection everytime Open() is called. // Creating a new connection everytime Open() is called.
...@@ -63,8 +62,7 @@ func Open(name string, settings Settings) (Database, error) { ...@@ -63,8 +62,7 @@ func Open(name string, settings Settings) (Database, error) {
newAdapter := reflect.New(driverType).Interface().(Database) newAdapter := reflect.New(driverType).Interface().(Database)
// Setting up the connection. // Setting up the connection.
err := newAdapter.Setup(settings) if err := newAdapter.Setup(settings); err != nil {
if err != nil {
return nil, err return nil, err
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment