@@ -18,11 +18,15 @@ var (
1818 pinnerIdx atomic.Int64
1919)
2020
21- // Release releases (unpins) a previously pinned message.
21+ // Release releases (unpins) a previously pinned message. Pinners are created and
22+ // returned for each function call in this library, and it is the responsibility of
23+ // the caller to call Release when it is done with the data that was returned.
24+ // Note that 'done' can also mean 'the data has been copied into memory that is
25+ // managed by the caller'.
2226//
2327//export Release
24- func Release (ptr int64 ) int32 {
25- val , ok := pinners .LoadAndDelete (ptr )
28+ func Release (pinnerId int64 ) int32 {
29+ val , ok := pinners .LoadAndDelete (pinnerId )
2630 if ! ok {
2731 return 1
2832 }
@@ -31,6 +35,10 @@ func Release(ptr int64) int32 {
3135 return 0
3236}
3337
38+ // pin pins the memory location pointed to by the result of the given message.
39+ // This prevents the Go runtime from moving or garbage collecting this memory.
40+ // The returned pinner ID must be used to call Release when the caller is done
41+ // with the message.
3442func pin (msg * exported.Message ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
3543 pinner := & runtime.Pinner {}
3644 if msg .Res != nil {
@@ -41,18 +49,28 @@ func pin(msg *exported.Message) (int64, int32, int64, int32, unsafe.Pointer) {
4149 return idx , msg .Code , msg .ObjectId , msg .Length (), msg .ResPointer ()
4250}
4351
52+ // CreatePool creates a pool of database connections. A Pool is equivalent to a *sql.DB.
53+ //
4454//export CreatePool
4555func CreatePool (dsn string ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
4656 msg := exported .CreatePool (dsn )
4757 return pin (msg )
4858}
4959
60+ // ClosePool closes a previously opened Pool.
61+ //
5062//export ClosePool
5163func ClosePool (id int64 ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
5264 msg := exported .ClosePool (id )
5365 return pin (msg )
5466}
5567
68+ // CreateConnection creates or borrows a connection from a previously created pool.
69+ // Note that as Spanner does not really use a 'connection-based' API, creating a
70+ // connection is a relatively cheap operation. It does not physically create a new
71+ // gRPC channel or any other physical connection to Spanner, and it also does not
72+ // create a server-side session. Instead, all session state is stored in the client.
73+ //
5674//export CreateConnection
5775func CreateConnection (poolId int64 ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
5876 msg := exported .CreateConnection (poolId )
@@ -65,60 +83,96 @@ func CloseConnection(poolId, connId int64) (int64, int32, int64, int32, unsafe.P
6583 return pin (msg )
6684}
6785
86+ // Execute executes a SQL statement on the given connection.
87+ // The return type is an identifier for a Rows object. This identifier can be used to
88+ // call the functions Metadata and Next to get respectively the metadata of the result
89+ // and the next row of results.
90+ //
6891//export Execute
6992func Execute (poolId , connectionId int64 , statement []byte ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
7093 msg := exported .Execute (poolId , connectionId , statement )
7194 return pin (msg )
7295}
7396
97+ // ExecuteTransaction executes a statement using a specific transaction.
98+ //
7499//export ExecuteTransaction
75100func ExecuteTransaction (poolId , connectionId , txId int64 , statement []byte ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
76101 msg := exported .ExecuteTransaction (poolId , connectionId , txId , statement )
77102 return pin (msg )
78103}
79104
105+ // ExecuteBatchDml executes a batch of DML statements on the given connection.
106+ //
80107//export ExecuteBatchDml
81108func ExecuteBatchDml (poolId , connectionId int64 , statements []byte ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
82109 msg := exported .ExecuteBatchDml (poolId , connectionId , statements )
83110 return pin (msg )
84111}
85112
113+ // Metadata returns the metadata of a Rows object.
114+ //
86115//export Metadata
87116func Metadata (poolId , connId , rowsId int64 ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
88117 msg := exported .Metadata (poolId , connId , rowsId )
89118 return pin (msg )
90119}
91120
121+ // UpdateCount returns the number of rows that was affected by a DML statement that was
122+ // executed.
123+ //
92124//export UpdateCount
93125func UpdateCount (poolId , connId , rowsId int64 ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
94126 msg := exported .UpdateCount (poolId , connId , rowsId )
95127 return pin (msg )
96128}
97129
130+ // Next returns the next row in a Rows object. The returned message contains a protobuf
131+ // ListValue that contains all the columns of the row. The message is empty if there are
132+ // no more rows in the Rows object.
133+ //
98134//export Next
99135func Next (poolId , connId , rowsId int64 ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
100136 msg := exported .Next (poolId , connId , rowsId )
101137 return pin (msg )
102138}
103139
140+ // CloseRows closes and cleans up all memory held by a Rows object. This must be called
141+ // when the application is done with the Rows object.
142+ //
104143//export CloseRows
105144func CloseRows (poolId , connId , rowsId int64 ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
106145 msg := exported .CloseRows (poolId , connId , rowsId )
107146 return pin (msg )
108147}
109148
149+ // BeginTransaction begins a new transaction on the given connection.
150+ // The txOpts byte slice contains a serialized protobuf TransactionOptions object.
151+ //
110152//export BeginTransaction
111153func BeginTransaction (poolId , connectionId int64 , txOpts []byte ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
112154 msg := exported .BeginTransaction (poolId , connectionId , txOpts )
113155 return pin (msg )
114156}
115157
158+ // Commit commits a previously started transaction. All transactions must be either
159+ // committed or rolled back, including read-only transactions. This to ensure that
160+ // all resources that are held by a transaction are cleaned up.
161+ //
116162//export Commit
117163func Commit (poolId , connectionId , txId int64 ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
118164 msg := exported .Commit (poolId , connectionId , txId )
119165 return pin (msg )
120166}
121167
168+ // Rollback rolls back a previously started transaction. All transactions must be either
169+ // committed or rolled back, including read-only transactions. This to ensure that
170+ // all resources that are held by a transaction are cleaned up.
171+ //
172+ // Spanner does not require read-only transactions to be committed or rolled back, but
173+ // this library requires that all transactions are committed or rolled back to clean up
174+ // all resources. Commit and Rollback are semantically the same for read-only transactions.
175+ //
122176//export Rollback
123177func Rollback (poolId , connectionId , txId int64 ) (int64 , int32 , int64 , int32 , unsafe.Pointer ) {
124178 msg := exported .Rollback (poolId , connectionId , txId )
0 commit comments