forked from taggledevel2/ratchet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_reader_writer.go
57 lines (48 loc) · 1.9 KB
/
sql_reader_writer.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
package processors
import (
"database/sql"
"github.com/dailyburn/ratchet/data"
)
// SQLReaderWriter performs both the job of a SQLReader and SQLWriter.
// This means it will run a SQL query, write the resulting data into a
// SQL database, and (if the write was successful) send the queried data
// to the next stage of processing.
//
// SQLReaderWriter is composed of both a SQLReader and SQLWriter, so it
// supports all of the same properties and usage options (such as static
// versus dynamic SQL querying).
type SQLReaderWriter struct {
SQLReader
SQLWriter
ConcurrencyLevel int // See ConcurrentDataProcessor
}
// NewSQLReaderWriter returns a new SQLReaderWriter ready for static querying.
func NewSQLReaderWriter(readConn *sql.DB, writeConn *sql.DB, readQuery, writeTable string) *SQLReaderWriter {
s := SQLReaderWriter{}
s.SQLReader = *NewSQLReader(readConn, readQuery)
s.SQLWriter = *NewSQLWriter(writeConn, writeTable)
return &s
}
// NewDynamicSQLReaderWriter returns a new SQLReaderWriter ready for dynamic querying.
func NewDynamicSQLReaderWriter(readConn *sql.DB, writeConn *sql.DB, sqlGenerator func(data.JSON) (string, error), writeTable string) *SQLReaderWriter {
s := NewSQLReaderWriter(readConn, writeConn, "", writeTable)
s.sqlGenerator = sqlGenerator
return s
}
// ProcessData uses SQLReader methods for processing data - this works via composition
func (s *SQLReaderWriter) ProcessData(d data.JSON, outputChan chan data.JSON, killChan chan error) {
s.ForEachQueryData(d, killChan, func(d data.JSON) {
s.SQLWriter.ProcessData(d, outputChan, killChan)
outputChan <- d
})
}
// Finish - see interface for documentation.
func (s *SQLReaderWriter) Finish(outputChan chan data.JSON, killChan chan error) {
}
func (s *SQLReaderWriter) String() string {
return "SQLReaderWriter"
}
// Concurrency defers to ConcurrentDataProcessor
func (s *SQLReaderWriter) Concurrency() int {
return s.ConcurrencyLevel
}