Skip to content

Commit

Permalink
proper support for inner join query creation
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeljesus committed Feb 23, 2018
1 parent c29b843 commit 4aec9c7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type (
// Table represents a klepto table definition
Table struct {
Name string
PrimaryKey string
IgnoreData bool
Filter Filter
Anonymise map[string]string
Expand All @@ -28,9 +29,10 @@ type (

// Relationship represents a relationship definition
Relationship struct {
Table string
ForeignKey string
ReferencedTable string
ReferencedKey string
ForeignKey string
}
)

Expand Down
2 changes: 2 additions & 0 deletions pkg/dumper/generic/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (p *sqlDumper) readAndDumpTables(done chan<- struct{}, configTables config.
}

opts = reader.ReadTableOpt{
PrimaryKey: tableConfig.PrimaryKey,
Match: tableConfig.Filter.Match,
Limit: tableConfig.Filter.Limit,
Relationships: p.relationshipConfigToOptions(tableConfig.Relationships),
Expand Down Expand Up @@ -144,6 +145,7 @@ func (p *sqlDumper) relationshipConfigToOptions(relationshipsConfig []*config.Re

for _, r := range relationshipsConfig {
opts = append(opts, &reader.RelationshipOpt{
Table: r.Table,
ReferencedTable: r.ReferencedTable,
ReferencedKey: r.ReferencedKey,
ForeignKey: r.ForeignKey,
Expand Down
13 changes: 9 additions & 4 deletions pkg/reader/generic/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,25 @@ func (s *SqlReader) buildQuery(tableName string, opts reader.ReadTableOpt) (sq.S
var query sq.SelectBuilder

query = sq.Select(opts.Columns...).From(s.QuoteIdentifier(tableName))

for _, r := range opts.Relationships {
if r.Table == "" {
r.Table = tableName
}
query = query.Join(fmt.Sprintf(
"%s ON %s.%s = %s.%s",
r.ReferencedTable,
tableName,
r.ForeignKey,
r.ReferencedTable,
r.ReferencedKey,
r.Table,
r.ForeignKey,
))
}

if len(opts.Relationships) > 0 {
query = query.GroupBy(fmt.Sprintf("%s.id", tableName))
if opts.PrimaryKey == "" {
opts.PrimaryKey = "id"
}
query = query.GroupBy(fmt.Sprintf("%s.%s", tableName, opts.PrimaryKey))
}

if opts.Match != "" {
Expand Down
5 changes: 4 additions & 1 deletion pkg/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type (

// ReadTableOpt represents the read table options
ReadTableOpt struct {
// PrimaryKey is the primary key
PrimaryKey string
// Columns contains the (quoted) column of the table
Columns []string
// Match is a condition field to dump only certain amount data
Expand All @@ -43,9 +45,10 @@ type (

// RelationshipOpt represents the relationships options
RelationshipOpt struct {
Table string
ForeignKey string
ReferencedTable string
ReferencedKey string
ForeignKey string
}

// ConnOpts are the options to create a connection
Expand Down

0 comments on commit 4aec9c7

Please sign in to comment.