/
sql_reader_postgresql_writer.go
57 lines (48 loc) · 2.12 KB
/
sql_reader_postgresql_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"
)
// SQLReaderPostgreSQLWriter performs both the job of a SQLReader and PostgreSQLWriter.
// This means it will run a SQL query, write the resulting data into a
// PostgreSQL database, and (if the write was successful) send the queried data
// to the next stage of processing.
//
// SQLReaderPostgreSQLWriter is composed of both a SQLReader and PostgreSQLWriter, so it
// supports all of the same properties and usage options (such as static
// versus dynamic SQL querying).
type SQLReaderPostgreSQLWriter struct {
SQLReader
PostgreSQLWriter
ConcurrencyLevel int // See ConcurrentDataProcessor
}
// NewSQLReaderPostgreSQLWriter returns a new SQLReaderPostgreSQLWriter ready for static querying.
func NewSQLReaderPostgreSQLWriter(readConn *sql.DB, writeConn *sql.DB, readQuery, writeTable string) *SQLReaderPostgreSQLWriter {
s := SQLReaderPostgreSQLWriter{}
s.SQLReader = *NewSQLReader(readConn, readQuery)
s.PostgreSQLWriter = *NewPostgreSQLWriter(writeConn, writeTable)
return &s
}
// NewDynamicSQLReaderPostgreSQLWriter returns a new SQLReaderPostgreSQLWriter ready for dynamic querying.
func NewDynamicSQLReaderPostgreSQLWriter(readConn *sql.DB, writeConn *sql.DB, sqlGenerator func(data.JSON) (string, error), writeTable string) *SQLReaderPostgreSQLWriter {
s := NewSQLReaderPostgreSQLWriter(readConn, writeConn, "", writeTable)
s.sqlGenerator = sqlGenerator
return s
}
// ProcessData uses SQLReader methods for processing data - this works via composition
func (s *SQLReaderPostgreSQLWriter) ProcessData(d data.JSON, outputChan chan data.JSON, killChan chan error) {
s.ForEachQueryData(d, killChan, func(d data.JSON) {
s.PostgreSQLWriter.ProcessData(d, outputChan, killChan)
outputChan <- d
})
}
// Finish - see interface for documentation.
func (s *SQLReaderPostgreSQLWriter) Finish(outputChan chan data.JSON, killChan chan error) {
}
func (s *SQLReaderPostgreSQLWriter) String() string {
return "SQLReaderPostgreSQLWriter"
}
// Concurrency defers to ConcurrentDataProcessor
func (s *SQLReaderPostgreSQLWriter) Concurrency() int {
return s.ConcurrencyLevel
}