File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1
1
package pq
2
2
3
3
import (
4
+ "context"
4
5
"database/sql/driver"
5
6
"encoding/binary"
6
7
"errors"
@@ -273,6 +274,38 @@ func (ci *copyin) Exec(v []driver.Value) (r driver.Result, err error) {
273
274
return driver .RowsAffected (0 ), nil
274
275
}
275
276
277
+ // CopyData executes a raw CopyData command using the PostgreSQL Frontend/Backend
278
+ // protocol. Use Exec(nil) to finish the command.
279
+ func (ci * copyin ) CopyData (ctx context.Context , line string ) (r driver.Result , err error ) {
280
+ if ci .closed {
281
+ return nil , errCopyInClosed
282
+ }
283
+
284
+ if finish := ci .cn .watchCancel (ctx ); finish != nil {
285
+ defer finish ()
286
+ }
287
+
288
+ if err := ci .getBad (); err != nil {
289
+ return nil , err
290
+ }
291
+ defer ci .cn .errRecover (& err )
292
+
293
+ if err := ci .err (); err != nil {
294
+ return nil , err
295
+ }
296
+
297
+ ci .buffer = append (ci .buffer , []byte (line )... )
298
+ ci .buffer = append (ci .buffer , '\n' )
299
+
300
+ if len (ci .buffer ) > ciBufferFlushSize {
301
+ ci .flush (ci .buffer )
302
+ // reset buffer, keep bytes for message identifier and length
303
+ ci .buffer = ci .buffer [:5 ]
304
+ }
305
+
306
+ return driver .RowsAffected (0 ), nil
307
+ }
308
+
276
309
func (ci * copyin ) Close () (err error ) {
277
310
if ci .closed { // Don't do anything, we're already closed
278
311
return nil
You can’t perform that action at this time.
0 commit comments