-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First aproach to new driver... reusing code... #21
Conversation
dbTypeOracle = backends.DbTypeOracle | ||
dbTypeCsvq = backends.DbTypeCsvq |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New constants for the new drivers..
cmd/main.go
Outdated
|
||
// TODO - Adding drivers... | ||
_ "github.com/mithrandie/csvq-driver" // sql.Open("csvq", "/path/to/data/directory") | ||
_ "github.com/sijms/go-ora/v2" // sql.Open("oracle", "oracle://OT:yourpassword@localhost/XE") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here add new drivers... only imports...
case dbTypeCsvq: | ||
backend, errI = backends.NewCsvqBackend(conn, opt, sLog) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because with this driver... the code change a few... decide implement new backend .. this aproach (not use ifs with typo) could help later to load this code dinamically
backends/backends.go
Outdated
@@ -22,4 +24,50 @@ type ResultSet interface { | |||
WriteRow([]interface{}) error | |||
Flush() error | |||
Close() error | |||
CreateTableSchema(cols []string, colTypes []*sql.ColumnType) insertSchema |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This methods it a TODO may could be private as earlier...
type SqlDB struct { | ||
db *sql.DB | ||
opt Opt | ||
logger *log.Logger | ||
|
||
// The result schemas (CREATE TABLE ...) are dynamically | ||
// generated everytime queries are executed based on their result columns. | ||
// They're cached here so as to avoid repetetive generation. | ||
resTableSchemas map[string]insertSchema | ||
schemaMutex sync.RWMutex | ||
} | ||
|
||
type Writer struct { | ||
jobID string | ||
taskName string | ||
colsWritten bool | ||
cols []string | ||
rows [][]byte | ||
tx *sql.Tx | ||
tbl string | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move generic struct to interface... and other related things...
// They're cached here so as to avoid repetetive generation. | ||
resTableSchemas map[string]insertSchema | ||
schemaMutex sync.RWMutex | ||
SqlDB |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the generic type extacted to interface...
Writer | ||
SqlDB |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar than before .. but less verbose :)
resTableSchemas: make(map[string]insertSchema), | ||
schemaMutex: sync.RWMutex{}, | ||
logger: l, | ||
SqlDB{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use generic type...
backend: s, | ||
tbl: fmt.Sprintf(s.opt.ResultsTable, jobID), | ||
tx: tx, | ||
Writer{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use generic type...
w.schemaMutex.RLock() | ||
_, ok := w.resTableSchemas[w.taskName] | ||
w.schemaMutex.RUnlock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we dont nedd backend.. it's an aggreation..
type csvqDBWriter struct { | ||
sqlDBWriter | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it's the tricky part ... this new backend implements the actual... but overwrite the only needed functionality...
return nil, err | ||
} | ||
|
||
return &csvqDBWriter{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return it's own type of backend...
backends/csvq.go
Outdated
ins := fmt.Sprintf(`INSERT INTO "%%s" (%s) `, strings.Join(colNameHolder, ",")) | ||
ins += fmt.Sprintf("VALUES (%s)", strings.Join(colValHolder, ",")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The insert command change in csvq driver... and better than ifs... (as comented in other place) use a new backend...
backends/csvq.go
Outdated
// TODO - This should configurable... maybe in some case not supported or need incremental update.. | ||
// if _, err := tx.Exec(fmt.Sprintf(rSchema.dropTable, w.tbl)); err != nil { | ||
// return err | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This driver don't need delete the table...
backends/csvq.go
Outdated
return insertSchema{ | ||
// TODO - In some dialects... | ||
dropTable: `DROP TABLE IF EXISTS "%s";`, | ||
createTable: fmt.Sprintf(`CREATE %s TABLE %%s (%s);`, unlogged, strings.Join(fields, ",")), | ||
insertRow: fmt.Sprintf(`INSERT INTO %%s (%s) VALUES (%s)`, strings.Join(colNameHolder, ","), strings.Join(colValHolder, ",")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the sql change a little...
Hi @josejuanmontiel. Thanks for the inline comments on the PR, but could you please explain here in a few bullet points, what exactly is changing, why, and how it helps. The overall architecture. |
Hi!
As bonus, need some extra configuration too... i include the comented option to run the jobber_test.go with testcontainer. TODO:
|
Thanks for the update. Will try to review this some time soon. |
Please see this #23 (comment) |
The idea... include new driver reusing actual code... for later dinamically load by configuration.
With the two (three) actual driver ... use ifs inside actual backend... because new one of new drivers change the code more drastically, decide to implements new backend.
And to reuse actual code in backend that don't change... in this backend include the "parent" of sql backend (the actual one) and only reimplement those methods that didn't change.
The trick its done here
type csvqDBWriter struct {
sqlDBWriter
}
reusing the actual implementation ... and only change the method ... that realy change...