Skip to content
Newer
Older
100644 77 lines (71 sloc) 2.71 KB
8fb69d2 @phf Extracted "connection" API to its own file.
authored
1 // Copyright 2009 Peter H. Froehlich. 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 db
6
7 import "os"
8
9 // The most basic type of database connection.
10 //
11 // The choice to separate Prepare() and Execute() is deliberate:
12 // It leaves the database driver the most flexibilty for achieving
13 // good performance without requiring additional caching schemes.
14 //
15 // Prepare() accepts a query language string and returns
16 // a precompiled statement that can be executed after any
17 // remaining parameters have been bound. The format of
18 // parameters in the query string is dependent on the
19 // database driver in question.
20 //
21 // Execute() accepts a precompiled statement, binds the
22 // given parameters, and then executes the statement.
23 // Execute() returns a channel of Result objects which
24 // can be examined one at a time (if the query produced
25 // results to begin with). Specific database drivers
26 // will return result objects conforming to one or more
27 // of the following interfaces which represent different
28 // levels of functionality.
29 //
30 // Close() ends the connection to the database system
31 // and frees up all internal resources associated with
32 // it. Note that you must close all objects created on
33 // the connection before closing the connection itself.
34 // After a connection has been closed, no further
35 // operations are allowed on it.
36 type Connection interface {
37 Prepare(query string) (Statement, os.Error);
3516f4f @phf Execute() returns ResultSet instead of channel.
authored
38 Execute(stat Statement, parameters ...) (ResultSet, os.Error);
8fb69d2 @phf Extracted "connection" API to its own file.
authored
39 Close() os.Error;
40 }
41
42 // InformativeConnections supply useful but optional information.
43 //
44 // Changes() returns the number of changes the last query made
45 // to the database. Note that the database driver has to explain
46 // what exactly constitutes a "change" for a given database system
47 // and query.
48 //
49 // LastId() returns the id of the last successful insertion into
50 // the database. The database driver has to explain the exact
51 // meaning of the id and the conditions under which it changes.
52 type InformativeConnection interface {
53 Connection;
54 Changes() (int, os.Error);
55 LastId() (int, os.Error);
56 }
57
58 // TransactionalConnections support transactions. Note that
59 // the database driver in question may be in "auto commit"
60 // mode by default. Once you call Begin(), "auto commit" will
61 // be disabled for that connection until you either Commit()
62 // or Rollback() successfully.
63 //
64 // Begin() starts a transaction.
65 //
66 // Commit() tries to push all changes made as part of the
67 // current transaction to the database.
68 //
69 // Rollback() tries to undo all changes made as part of the
70 // current transaction.
71 type TransactionalConnection interface {
72 Connection;
73 Begin() os.Error;
74 Commit() os.Error;
75 Rollback() os.Error;
76 }
Something went wrong with that request. Please try again.