1package pgx
2
3import (
4	"database/sql/driver"
5	"reflect"
6)
7
8// This file contains code copied from the Go standard library due to the
9// required function not being public.
10
11// Copyright (c) 2009 The Go Authors. All rights reserved.
12
13// Redistribution and use in source and binary forms, with or without
14// modification, are permitted provided that the following conditions are
15// met:
16
17//    * Redistributions of source code must retain the above copyright
18// notice, this list of conditions and the following disclaimer.
19//    * Redistributions in binary form must reproduce the above
20// copyright notice, this list of conditions and the following disclaimer
21// in the documentation and/or other materials provided with the
22// distribution.
23//    * Neither the name of Google Inc. nor the names of its
24// contributors may be used to endorse or promote products derived from
25// this software without specific prior written permission.
26
27// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39// From database/sql/convert.go
40
41var valuerReflectType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
42
43// callValuerValue returns vr.Value(), with one exception:
44// If vr.Value is an auto-generated method on a pointer type and the
45// pointer is nil, it would panic at runtime in the panicwrap
46// method. Treat it like nil instead.
47// Issue 8415.
48//
49// This is so people can implement driver.Value on value types and
50// still use nil pointers to those types to mean nil/NULL, just like
51// string/*string.
52//
53// This function is mirrored in the database/sql/driver package.
54func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
55	if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Ptr &&
56		rv.IsNil() &&
57		rv.Type().Elem().Implements(valuerReflectType) {
58		return nil, nil
59	}
60	return vr.Value()
61}
62