OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [database/] [sql/] [driver/] [driver.go] - Blame information for rev 774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// Package driver defines interfaces to be implemented by database
6
// drivers as used by package sql.
7
//
8
// Most code should use package sql.
9
//
10
// Drivers only need to be aware of a subset of Go's types.  The sql package
11
// will convert all types into one of the following:
12
//
13
//   int64
14
//   float64
15
//   bool
16
//   nil
17
//   []byte
18
//   string   [*] everywhere except from Rows.Next.
19
//   time.Time
20
//
21
package driver
22
 
23
import "errors"
24
 
25
// Driver is the interface that must be implemented by a database
26
// driver.
27
type Driver interface {
28
        // Open returns a new connection to the database.
29
        // The name is a string in a driver-specific format.
30
        //
31
        // Open may return a cached connection (one previously
32
        // closed), but doing so is unnecessary; the sql package
33
        // maintains a pool of idle connections for efficient re-use.
34
        //
35
        // The returned connection is only used by one goroutine at a
36
        // time.
37
        Open(name string) (Conn, error)
38
}
39
 
40
// ErrSkip may be returned by some optional interfaces' methods to
41
// indicate at runtime that the fast path is unavailable and the sql
42
// package should continue as if the optional interface was not
43
// implemented. ErrSkip is only supported where explicitly
44
// documented.
45
var ErrSkip = errors.New("driver: skip fast-path; continue as if unimplemented")
46
 
47
// Execer is an optional interface that may be implemented by a Conn.
48
//
49
// If a Conn does not implement Execer, the db package's DB.Exec will
50
// first prepare a query, execute the statement, and then close the
51
// statement.
52
//
53
// All arguments are of a subset type as defined in the package docs.
54
//
55
// Exec may return ErrSkip.
56
type Execer interface {
57
        Exec(query string, args []interface{}) (Result, error)
58
}
59
 
60
// Conn is a connection to a database. It is not used concurrently
61
// by multiple goroutines.
62
//
63
// Conn is assumed to be stateful.
64
type Conn interface {
65
        // Prepare returns a prepared statement, bound to this connection.
66
        Prepare(query string) (Stmt, error)
67
 
68
        // Close invalidates and potentially stops any current
69
        // prepared statements and transactions, marking this
70
        // connection as no longer in use.
71
        //
72
        // Because the sql package maintains a free pool of
73
        // connections and only calls Close when there's a surplus of
74
        // idle connections, it shouldn't be necessary for drivers to
75
        // do their own connection caching.
76
        Close() error
77
 
78
        // Begin starts and returns a new transaction.
79
        Begin() (Tx, error)
80
}
81
 
82
// Result is the result of a query execution.
83
type Result interface {
84
        // LastInsertId returns the database's auto-generated ID
85
        // after, for example, an INSERT into a table with primary
86
        // key.
87
        LastInsertId() (int64, error)
88
 
89
        // RowsAffected returns the number of rows affected by the
90
        // query.
91
        RowsAffected() (int64, error)
92
}
93
 
94
// Stmt is a prepared statement. It is bound to a Conn and not
95
// used by multiple goroutines concurrently.
96
type Stmt interface {
97
        // Close closes the statement.
98
        //
99
        // Closing a statement should not interrupt any outstanding
100
        // query created from that statement. That is, the following
101
        // order of operations is valid:
102
        //
103
        //  * create a driver statement
104
        //  * call Query on statement, returning Rows
105
        //  * close the statement
106
        //  * read from Rows
107
        //
108
        // If closing a statement invalidates currently-running
109
        // queries, the final step above will incorrectly fail.
110
        //
111
        // TODO(bradfitz): possibly remove the restriction above, if
112
        // enough driver authors object and find it complicates their
113
        // code too much. The sql package could be smarter about
114
        // refcounting the statement and closing it at the appropriate
115
        // time.
116
        Close() error
117
 
118
        // NumInput returns the number of placeholder parameters.
119
        //
120
        // If NumInput returns >= 0, the sql package will sanity check
121
        // argument counts from callers and return errors to the caller
122
        // before the statement's Exec or Query methods are called.
123
        //
124
        // NumInput may also return -1, if the driver doesn't know
125
        // its number of placeholders. In that case, the sql package
126
        // will not sanity check Exec or Query argument counts.
127
        NumInput() int
128
 
129
        // Exec executes a query that doesn't return rows, such
130
        // as an INSERT or UPDATE.  The args are all of a subset
131
        // type as defined above.
132
        Exec(args []interface{}) (Result, error)
133
 
134
        // Exec executes a query that may return rows, such as a
135
        // SELECT.  The args of all of a subset type as defined above.
136
        Query(args []interface{}) (Rows, error)
137
}
138
 
139
// ColumnConverter may be optionally implemented by Stmt if the
140
// the statement is aware of its own columns' types and can
141
// convert from any type to a driver subset type.
142
type ColumnConverter interface {
143
        // ColumnConverter returns a ValueConverter for the provided
144
        // column index.  If the type of a specific column isn't known
145
        // or shouldn't be handled specially, DefaultValueConverter
146
        // can be returned.
147
        ColumnConverter(idx int) ValueConverter
148
}
149
 
150
// Rows is an iterator over an executed query's results.
151
type Rows interface {
152
        // Columns returns the names of the columns. The number of
153
        // columns of the result is inferred from the length of the
154
        // slice.  If a particular column name isn't known, an empty
155
        // string should be returned for that entry.
156
        Columns() []string
157
 
158
        // Close closes the rows iterator.
159
        Close() error
160
 
161
        // Next is called to populate the next row of data into
162
        // the provided slice. The provided slice will be the same
163
        // size as the Columns() are wide.
164
        //
165
        // The dest slice may be populated with only with values
166
        // of subset types defined above, but excluding string.
167
        // All string values must be converted to []byte.
168
        //
169
        // Next should return io.EOF when there are no more rows.
170
        Next(dest []interface{}) error
171
}
172
 
173
// Tx is a transaction.
174
type Tx interface {
175
        Commit() error
176
        Rollback() error
177
}
178
 
179
// RowsAffected implements Result for an INSERT or UPDATE operation
180
// which mutates a number of rows.
181
type RowsAffected int64
182
 
183
var _ Result = RowsAffected(0)
184
 
185
func (RowsAffected) LastInsertId() (int64, error) {
186
        return 0, errors.New("no LastInsertId available")
187
}
188
 
189
func (v RowsAffected) RowsAffected() (int64, error) {
190
        return int64(v), nil
191
}
192
 
193
// DDLSuccess is a pre-defined Result for drivers to return when a DDL
194
// command succeeds.
195
var DDLSuccess ddlSuccess
196
 
197
type ddlSuccess struct{}
198
 
199
var _ Result = ddlSuccess{}
200
 
201
func (ddlSuccess) LastInsertId() (int64, error) {
202
        return 0, errors.New("no LastInsertId available after DDL statement")
203
}
204
 
205
func (ddlSuccess) RowsAffected() (int64, error) {
206
        return 0, errors.New("no RowsAffected available after DDL statement")
207
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.