Initial commit

This commit is contained in:
Kevin Cotugno 2020-05-16 13:46:30 -07:00
commit ce7ae3be1a
4 changed files with 107 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
hex-decode-fix

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module git.cotugno.family/kevin/hex-decode-fix
go 1.14
require rsc.io/sqlite v1.0.0

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
rsc.io/sqlite v1.0.0 h1:zUGL/JDeFfblrma2rToXG+fUsGZe+CShUHI3KlUNwQc=
rsc.io/sqlite v1.0.0/go.mod h1:bRoHdqsJCgrcQDvBeCS454l4kLoFAQKHdwfwpoweIOo=

99
main.go Normal file
View File

@ -0,0 +1,99 @@
package main
import (
"database/sql"
"encoding/hex"
"fmt"
"log"
"os"
"strings"
_ "rsc.io/sqlite"
)
const prefix = "\\x"
var file string
var table string
var idColumn string
var hexColumn string
func init() {
if len(os.Args) < 5 {
log.Fatal("Requires args: file, table, id column, and hex column")
}
file = os.Args[1]
table = os.Args[2]
idColumn = os.Args[3]
hexColumn = os.Args[4]
}
func main() {
db, err := sql.Open("sqlite3", file)
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
rows, err := tx.Query(fmt.Sprintf("SELECT %s, %s FROM %s", idColumn, hexColumn, table))
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var updated int
var skipped int
var failed int
for rows.Next() {
var id int
var raw string
if err = rows.Scan(&id, &raw); err != nil {
log.Print(err)
failed++
continue
}
if !strings.HasPrefix(raw, prefix) {
skipped++
continue
}
decoded, err := hex.DecodeString(strings.TrimPrefix(raw, prefix))
if err != nil {
log.Print(err)
failed++
continue
}
_, err = tx.Exec(fmt.Sprintf("UPDATE %s SET %s = $1 WHERE %s = $2", table, hexColumn, idColumn), decoded, id)
if err == nil {
updated++
} else {
log.Print(err)
failed++
}
}
log.Print("Skipped: ", skipped)
log.Print("Failed: ", failed)
log.Print("Updated: ", updated)
err = rows.Err()
if err != nil {
log.Fatal(err)
}
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
}