adding quickfix
This commit is contained in:
226
quickfix/log/sql/sql_log.go
Normal file
226
quickfix/log/sql/sql_log.go
Normal file
@ -0,0 +1,226 @@
|
||||
// Copyright (c) quickfixengine.org All rights reserved.
|
||||
//
|
||||
// This file may be distributed under the terms of the quickfixengine.org
|
||||
// license as defined by quickfixengine.org and appearing in the file
|
||||
// LICENSE included in the packaging of this file.
|
||||
//
|
||||
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
|
||||
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
|
||||
// PARTICULAR PURPOSE.
|
||||
//
|
||||
// See http://www.quickfixengine.org/LICENSE for licensing information.
|
||||
//
|
||||
// Contact ask@quickfixengine.org if any conditions of this licensing
|
||||
// are not clear to you.
|
||||
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"quantex.com/qfixdpl/quickfix"
|
||||
"quantex.com/qfixdpl/quickfix/config"
|
||||
)
|
||||
|
||||
type sqlLogFactory struct {
|
||||
settings *quickfix.Settings
|
||||
}
|
||||
|
||||
type sqlLog struct {
|
||||
sessionID quickfix.SessionID
|
||||
sqlDriver string
|
||||
sqlDataSourceName string
|
||||
sqlConnMaxLifetime time.Duration
|
||||
db *sql.DB
|
||||
placeholder placeholderFunc
|
||||
}
|
||||
|
||||
type placeholderFunc func(int) string
|
||||
|
||||
var rePlaceholder = regexp.MustCompile(`\?`)
|
||||
|
||||
func sqlString(raw string, placeholder placeholderFunc) string {
|
||||
if placeholder == nil {
|
||||
return raw
|
||||
}
|
||||
idx := 0
|
||||
return rePlaceholder.ReplaceAllStringFunc(raw, func(_ string) string {
|
||||
p := placeholder(idx)
|
||||
idx++
|
||||
return p
|
||||
})
|
||||
}
|
||||
|
||||
func postgresPlaceholder(i int) string {
|
||||
return fmt.Sprintf("$%d", i+1)
|
||||
}
|
||||
|
||||
// NewLogFactory returns a sql-based implementation of LogFactory.
|
||||
func NewLogFactory(settings *quickfix.Settings) quickfix.LogFactory {
|
||||
return sqlLogFactory{settings: settings}
|
||||
}
|
||||
|
||||
// Create creates a new SQLLog implementation of the Log interface.
|
||||
func (f sqlLogFactory) Create() (log quickfix.Log, err error) {
|
||||
globalSettings := f.settings.GlobalSettings()
|
||||
|
||||
sqlDriver, err := globalSettings.Setting(config.SQLLogDriver)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlDataSourceName, err := globalSettings.Setting(config.SQLLogDataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlConnMaxLifetime := 0 * time.Second
|
||||
if globalSettings.HasSetting(config.SQLLogConnMaxLifetime) {
|
||||
sqlConnMaxLifetime, err = globalSettings.DurationSetting(config.SQLLogConnMaxLifetime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return newSQLLog(quickfix.SessionID{}, sqlDriver, sqlDataSourceName, sqlConnMaxLifetime)
|
||||
}
|
||||
|
||||
// CreateSessionLog creates a new SQLLog implementation of the Log interface.
|
||||
func (f sqlLogFactory) CreateSessionLog(sessionID quickfix.SessionID) (log quickfix.Log, err error) {
|
||||
globalSettings := f.settings.GlobalSettings()
|
||||
dynamicSessions, _ := globalSettings.BoolSetting(config.DynamicSessions)
|
||||
|
||||
sessionSettings, ok := f.settings.SessionSettings()[sessionID]
|
||||
if !ok {
|
||||
if dynamicSessions {
|
||||
sessionSettings = globalSettings
|
||||
} else {
|
||||
return nil, fmt.Errorf("unknown session: %v", sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
sqlDriver, err := sessionSettings.Setting(config.SQLLogDriver)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlDataSourceName, err := sessionSettings.Setting(config.SQLLogDataSourceName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlConnMaxLifetime := 0 * time.Second
|
||||
if sessionSettings.HasSetting(config.SQLLogConnMaxLifetime) {
|
||||
sqlConnMaxLifetime, err = sessionSettings.DurationSetting(config.SQLLogConnMaxLifetime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return newSQLLog(sessionID, sqlDriver, sqlDataSourceName, sqlConnMaxLifetime)
|
||||
}
|
||||
|
||||
func newSQLLog(sessionID quickfix.SessionID, driver string, dataSourceName string, connMaxLifetime time.Duration) (l *sqlLog, err error) {
|
||||
l = &sqlLog{
|
||||
sessionID: sessionID,
|
||||
sqlDriver: driver,
|
||||
sqlDataSourceName: dataSourceName,
|
||||
sqlConnMaxLifetime: connMaxLifetime,
|
||||
}
|
||||
|
||||
if l.sqlDriver == "postgres" || l.sqlDriver == "pgx" {
|
||||
l.placeholder = postgresPlaceholder
|
||||
}
|
||||
|
||||
if l.db, err = sql.Open(l.sqlDriver, l.sqlDataSourceName); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l.db.SetConnMaxLifetime(l.sqlConnMaxLifetime)
|
||||
|
||||
if err = l.db.Ping(); err != nil { // ensure immediate connection
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func (l sqlLog) OnIncoming(msg []byte) {
|
||||
l.insert("messages_log", string(msg))
|
||||
}
|
||||
|
||||
func (l sqlLog) OnOutgoing(msg []byte) {
|
||||
l.insert("messages_log", string(msg))
|
||||
}
|
||||
|
||||
func (l sqlLog) OnEvent(msg string) {
|
||||
l.insert("event_log", msg)
|
||||
}
|
||||
|
||||
func (l sqlLog) OnEventf(format string, v ...interface{}) {
|
||||
l.insert("event_log", fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func (l sqlLog) insert(table string, value string) {
|
||||
s := l.sessionID
|
||||
|
||||
_, err := l.db.Exec(sqlString(`INSERT INTO `+table+` (
|
||||
time,
|
||||
beginstring, session_qualifier,
|
||||
sendercompid, sendersubid, senderlocid,
|
||||
targetcompid, targetsubid, targetlocid,
|
||||
text)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, l.placeholder),
|
||||
time.Now(),
|
||||
s.BeginString, s.Qualifier,
|
||||
s.SenderCompID, s.SenderSubID, s.SenderLocationID,
|
||||
s.TargetCompID, s.TargetSubID, s.TargetLocationID,
|
||||
value,
|
||||
)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *sqlLog) iterate(table string, cb func(string) error) error {
|
||||
s := l.sessionID
|
||||
rows, err := l.db.Query(sqlString(`SELECT text FROM `+table+`
|
||||
WHERE beginstring=? AND session_qualifier=?
|
||||
AND sendercompid=? AND sendersubid=? AND senderlocid=?
|
||||
AND targetcompid=? AND targetsubid=? AND targetlocid=?`,
|
||||
l.placeholder),
|
||||
s.BeginString, s.Qualifier,
|
||||
s.SenderCompID, s.SenderSubID, s.SenderLocationID,
|
||||
s.TargetCompID, s.TargetSubID, s.TargetLocationID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
for rows.Next() {
|
||||
var txt string
|
||||
if err = rows.Scan(&txt); err != nil {
|
||||
return err
|
||||
} else if err = cb(txt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func (l *sqlLog) getEntries(table string) ([]string, error) {
|
||||
var msgs []string
|
||||
err := l.iterate(table, func(msg string) error {
|
||||
msgs = append(msgs, msg)
|
||||
return nil
|
||||
})
|
||||
return msgs, err
|
||||
}
|
||||
|
||||
// Close closes the log's database connection.
|
||||
func (l *sqlLog) close() error {
|
||||
if l.db != nil {
|
||||
l.db.Close()
|
||||
l.db = nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
139
quickfix/log/sql/sql_log_test.go
Normal file
139
quickfix/log/sql/sql_log_test.go
Normal file
@ -0,0 +1,139 @@
|
||||
// Copyright (c) quickfixengine.org All rights reserved.
|
||||
//
|
||||
// This file may be distributed under the terms of the quickfixengine.org
|
||||
// license as defined by quickfixengine.org and appearing in the file
|
||||
// LICENSE included in the packaging of this file.
|
||||
//
|
||||
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
|
||||
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
|
||||
// PARTICULAR PURPOSE.
|
||||
//
|
||||
// See http://www.quickfixengine.org/LICENSE for licensing information.
|
||||
//
|
||||
// Contact ask@quickfixengine.org if any conditions of this licensing
|
||||
// are not clear to you.
|
||||
|
||||
package sql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"quantex.com/qfixdpl/quickfix"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
)
|
||||
|
||||
// SQLLogTestSuite runs tests for the SQLLog impl of Log.
|
||||
type SQLLogTestSuite struct {
|
||||
suite.Suite
|
||||
sqlLogRootPath string
|
||||
log *sqlLog
|
||||
|
||||
settings *quickfix.Settings
|
||||
sessionID quickfix.SessionID
|
||||
}
|
||||
|
||||
func (suite *SQLLogTestSuite) SetupTest() {
|
||||
suite.sqlLogRootPath = path.Join(os.TempDir(), fmt.Sprintf("SQLLogTestSuite-%d", os.Getpid()))
|
||||
err := os.MkdirAll(suite.sqlLogRootPath, os.ModePerm)
|
||||
require.Nil(suite.T(), err)
|
||||
sqlDriver := "sqlite3"
|
||||
sqlDsn := path.Join(suite.sqlLogRootPath, fmt.Sprintf("%d.db", time.Now().UnixNano()))
|
||||
|
||||
// create tables
|
||||
db, err := sql.Open(sqlDriver, sqlDsn)
|
||||
require.Nil(suite.T(), err)
|
||||
ddlFnames, err := filepath.Glob(fmt.Sprintf("../../_sql/%s/*.sql", sqlDriver))
|
||||
require.Nil(suite.T(), err)
|
||||
for _, fname := range ddlFnames {
|
||||
sqlBytes, err := os.ReadFile(fname)
|
||||
require.Nil(suite.T(), err)
|
||||
_, err = db.Exec(string(sqlBytes))
|
||||
require.Nil(suite.T(), err)
|
||||
}
|
||||
|
||||
// create settings
|
||||
sessionID := quickfix.SessionID{BeginString: "FIX.4.4", SenderCompID: "SENDER", TargetCompID: "TARGET"}
|
||||
settings, err := quickfix.ParseSettings(strings.NewReader(fmt.Sprintf(`
|
||||
[DEFAULT]
|
||||
SQLLogDriver=%s
|
||||
SQLLogDataSourceName=%s
|
||||
SQLLogConnMaxLifetime=14400s
|
||||
|
||||
[SESSION]
|
||||
BeginString=%s
|
||||
SenderCompID=%s
|
||||
TargetCompID=%s`, sqlDriver, sqlDsn, sessionID.BeginString, sessionID.SenderCompID, sessionID.TargetCompID)))
|
||||
require.Nil(suite.T(), err)
|
||||
|
||||
suite.sessionID = sessionID
|
||||
suite.settings = settings
|
||||
}
|
||||
|
||||
func (suite *SQLLogTestSuite) TestSQLLogNoSession() {
|
||||
// create log
|
||||
log, err := NewLogFactory(suite.settings).Create()
|
||||
require.Nil(suite.T(), err)
|
||||
suite.log = log.(*sqlLog)
|
||||
|
||||
suite.log.OnIncoming([]byte("Cool1"))
|
||||
suite.log.OnOutgoing([]byte("Cool2"))
|
||||
entries, err := suite.log.getEntries("messages_log")
|
||||
require.Nil(suite.T(), err)
|
||||
require.Len(suite.T(), entries, 2)
|
||||
require.Equal(suite.T(), "Cool1", entries[0])
|
||||
require.Equal(suite.T(), "Cool2", entries[1])
|
||||
|
||||
suite.log.OnEvent("Cool3")
|
||||
suite.log.OnEvent("Cool4")
|
||||
entries, err = suite.log.getEntries("event_log")
|
||||
require.Nil(suite.T(), err)
|
||||
require.Len(suite.T(), entries, 2)
|
||||
require.Equal(suite.T(), "Cool3", entries[0])
|
||||
require.Equal(suite.T(), "Cool4", entries[1])
|
||||
}
|
||||
|
||||
func (suite *SQLLogTestSuite) TestSQLLog() {
|
||||
// create log
|
||||
log, err := NewLogFactory(suite.settings).CreateSessionLog(suite.sessionID)
|
||||
require.Nil(suite.T(), err)
|
||||
suite.log = log.(*sqlLog)
|
||||
|
||||
suite.log.OnIncoming([]byte("Cool1"))
|
||||
suite.log.OnOutgoing([]byte("Cool2"))
|
||||
entries, err := suite.log.getEntries("messages_log")
|
||||
require.Nil(suite.T(), err)
|
||||
require.Len(suite.T(), entries, 2)
|
||||
require.Equal(suite.T(), "Cool1", entries[0])
|
||||
require.Equal(suite.T(), "Cool2", entries[1])
|
||||
|
||||
suite.log.OnEvent("Cool3")
|
||||
suite.log.OnEvent("Cool4")
|
||||
entries, err = suite.log.getEntries("event_log")
|
||||
require.Nil(suite.T(), err)
|
||||
require.Len(suite.T(), entries, 2)
|
||||
require.Equal(suite.T(), "Cool3", entries[0])
|
||||
require.Equal(suite.T(), "Cool4", entries[1])
|
||||
}
|
||||
|
||||
func (suite *SQLLogTestSuite) TestSqlPlaceholderReplacement() {
|
||||
got := sqlString("A ? B ? C ?", postgresPlaceholder)
|
||||
suite.Equal("A $1 B $2 C $3", got)
|
||||
}
|
||||
|
||||
func (suite *SQLLogTestSuite) TearDownTest() {
|
||||
suite.log.close()
|
||||
os.RemoveAll(suite.sqlLogRootPath)
|
||||
}
|
||||
|
||||
func TestSQLLogTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(SQLLogTestSuite))
|
||||
}
|
||||
Reference in New Issue
Block a user