-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Two new options are provided -csv1.sort and -csv2.sort, which if either are set, will sort the corresponding data by the -key columns before diffing with the other source. This uses SQLite behind the scenes which requires CGO. This release drops Windows binaries for the time being until an alternate implementation is considered. Signed-off-by: Byron Ruth <[email protected]>
- Loading branch information
Showing
10 changed files
with
259 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
FROM alpine:3.6 | ||
FROM golang:1.9 AS build-env | ||
WORKDIR /go/src/github.com/chop-dbhi/diff-table | ||
RUN make dist-build-linux | ||
|
||
FROM alpine:3.6 | ||
RUN apk add --update ca-certificates | ||
|
||
COPY ./dist/linux-amd64/diff-table / | ||
|
||
COPY --from=build-env /go/src/github.com/chop-dbhi/diff-table/dist/linux-amd64/diff-table / | ||
ENTRYPOINT ["/diff-table"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM golang:1.9 | ||
ADD build.sh / | ||
|
||
ENV CGO_ENABLED=1 | ||
ENV ROOT=/go/src/github.com/chop-dbhi/diff-table | ||
|
||
ENTRYPOINT ["/build.sh"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
# Gopkg.toml example | ||
# | ||
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md | ||
# for detailed Gopkg.toml documentation. | ||
# | ||
# required = ["github.com/user/thing/cmd/thing"] | ||
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project" | ||
# version = "1.0.0" | ||
# | ||
# [[constraint]] | ||
# name = "github.com/user/project2" | ||
# branch = "dev" | ||
# source = "github.com/myfork/project2" | ||
# | ||
# [[override]] | ||
# name = "github.com/x/y" | ||
# version = "2.4.0" | ||
|
||
|
||
[[constraint]] | ||
branch = "master" | ||
name = "github.com/lib/pq" | ||
|
||
[[constraint]] | ||
name = "github.com/mattn/go-sqlite3" | ||
version = "1.3.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
|
||
cd $ROOT | ||
make dist-build-linux |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package difftable | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/csv" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
) | ||
|
||
const ( | ||
indexName = "results_key_index" | ||
) | ||
|
||
func newDb(cr *csv.Reader, table string, head, key []string) (*sql.DB, error) { | ||
db, err := sql.Open("sqlite3", ":memory:") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cols := make([]string, len(head)) | ||
|
||
for i, col := range head { | ||
cols[i] = fmt.Sprintf("`%s` TEXT", col) | ||
} | ||
|
||
keyCols := make([]string, len(key)) | ||
for i, col := range key { | ||
keyCols[i] = fmt.Sprintf("`%s`", col) | ||
} | ||
|
||
stmts := []string{ | ||
fmt.Sprintf(`CREATE TABLE %s (%s)`, table, strings.Join(cols, ",\n")), | ||
fmt.Sprintf(`CREATE INDEX %s ON %s (%s)`, indexName, table, strings.Join(keyCols, ",")), | ||
} | ||
|
||
for _, stmt := range stmts { | ||
if _, err = db.Exec(stmt); err != nil { | ||
db.Close() | ||
return nil, err | ||
} | ||
} | ||
|
||
return db, nil | ||
} | ||
|
||
func insertStmt(table string, head []string) string { | ||
header := make([]string, len(head)) | ||
for i, c := range head { | ||
header[i] = fmt.Sprintf("`%s`", c) | ||
} | ||
|
||
params := make([]string, len(head)) | ||
|
||
for i, _ := range params { | ||
params[i] = "?" | ||
} | ||
|
||
return fmt.Sprintf(` | ||
INSERT INTO %s (%s) | ||
VALUES (%s) | ||
`, table, strings.Join(header, ","), strings.Join(params, ",")) | ||
} | ||
|
||
func CsvDB(cr *csv.Reader, table string, key []string) (*sql.DB, error) { | ||
head, err := cr.Read() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
db, err := newDb(cr, table, head, key) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tx, err := db.Begin() | ||
if err != nil { | ||
db.Close() | ||
return nil, err | ||
} | ||
defer tx.Rollback() | ||
|
||
sql := insertStmt(table, head) | ||
stmt, err := tx.Prepare(sql) | ||
if err != nil { | ||
db.Close() | ||
return nil, err | ||
} | ||
|
||
vals := make([]interface{}, len(head)) | ||
|
||
for { | ||
row, err := cr.Read() | ||
if err != nil { | ||
if err == io.EOF { | ||
break | ||
} | ||
|
||
stmt.Close() | ||
tx.Rollback() | ||
db.Close() | ||
return nil, err | ||
} | ||
|
||
for i, s := range row { | ||
vals[i] = s | ||
} | ||
|
||
if _, err := stmt.Exec(vals...); err != nil { | ||
stmt.Close() | ||
tx.Rollback() | ||
db.Close() | ||
return nil, err | ||
} | ||
} | ||
|
||
stmt.Close() | ||
|
||
if err := tx.Commit(); err != nil { | ||
db.Close() | ||
return nil, err | ||
} | ||
|
||
return db, nil | ||
} |