/
ds.go
79 lines (68 loc) · 1.84 KB
/
ds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package morm
import (
"encoding/csv"
"io"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MgoDS interface {
Exec(exec func(i interface{}) error) error
ExportCSV(w io.Writer, title []string, exec func(writer *csv.Writer, i interface{}) error) error
}
func (mm *mgoModelImpl) NewFindMgoDS(d DocInter, q bson.M, opts ...*options.FindOptions) MgoDS {
return &findDsImpl{
MgoDBModel: mm,
d: d,
q: q,
opts: opts,
}
}
type findDsImpl struct {
MgoDBModel
d DocInter
q bson.M
opts []*options.FindOptions
}
func (mm *findDsImpl) Exec(exec func(i interface{}) error) error {
return mm.FindAndExec(mm.d, mm.q, exec, mm.opts...)
}
func (mm *findDsImpl) ExportCSV(w io.Writer, title []string, exec func(writer *csv.Writer, i interface{}) error) error {
csvWriter := csv.NewWriter(w)
err := csvWriter.Write(title)
defer csvWriter.Flush()
if err != nil {
return err
}
return mm.FindAndExec(mm.d, mm.q, func(i interface{}) error {
return exec(csvWriter, i)
}, mm.opts...)
}
func (mm *mgoModelImpl) NewPipeFindMgoDS(d MgoAggregate, q bson.M, opts ...*options.AggregateOptions) MgoDS {
return &pipeFindDsImpl{
MgoDBModel: mm,
d: d,
q: q,
opts: opts,
}
}
type pipeFindDsImpl struct {
MgoDBModel
d MgoAggregate
q bson.M
opts []*options.AggregateOptions
}
func (mm *pipeFindDsImpl) Exec(exec func(i interface{}) error) error {
return mm.PipeFindAndExec(mm.d, mm.q, exec, mm.opts...)
}
func (mm *pipeFindDsImpl) ExportCSV(w io.Writer, title []string, exec func(writer *csv.Writer, i interface{}) error) error {
csvWriter := csv.NewWriter(w)
err := csvWriter.Write(title)
defer csvWriter.Flush()
if err != nil {
return err
}
return mm.PipeFindAndExec(mm.d, mm.q, func(i interface{}) error {
err = exec(csvWriter, i)
return err
}, mm.opts...)
}