Parcourir la Source

initial commit

bmallred 9 ans auparavant
Parent
Commettre
f088f26caf
2 fichiers modifiés avec 68 ajouts et 1 suppressions
  1. 2 1
      .gitignore
  2. 66 0
      main.go

+ 2 - 1
.gitignore

@ -19,4 +19,5 @@ _cgo_export.*
19 19
20 20
_testmain.go
21 21
22
*.exe
22
*.exe
23
sqlwrap

+ 66 - 0
main.go

@ -0,0 +1,66 @@
1
package main
2
3
import (
4
	"bufio"
5
	"fmt"
6
	"log"
7
	"os"
8
	"strings"
9
)
10
11
func main() {
12
	a := len(os.Args)
13
	if a == 1 {
14
		log.Fatal("Must specific a file")
15
	}
16
17
	for i := 1; i < a; i++ {
18
		path := os.Args[i]
19
20
		// Open the file initially for read only
21
		fi, err := os.OpenFile(path, os.O_RDONLY, 0755)
22
		if err != nil {
23
			log.Fatal(err)
24
		}
25
		defer fi.Close()
26
27
		// Loop through all the lines of the file look for INSERT statements to wrap
28
		lines := []string{}
29
		scanner := bufio.NewScanner(fi)
30
		for scanner.Scan() {
31
			line := scanner.Text()
32
			idx := strings.Index(line, "INSERT")
33
			if idx == 0 || len(lines) == 0 {
34
				lines = append(lines, line)
35
			} else {
36
				previous := len(lines) - 1
37
				lines[previous] = fmt.Sprintf("%s' + CHAR(13) + N'%s", lines[previous], line)
38
			}
39
		}
40
41
		// Check for errors in the scanner
42
		if err := scanner.Err(); err != nil {
43
			log.Fatal(err)
44
		}
45
46
		// Close for sanity's sake
47
		err = fi.Close()
48
		if err != nil {
49
			log.Fatal(err)
50
		}
51
52
		// Truncate the file and open for write
53
		fi, err = os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0755)
54
		if err != nil {
55
			log.Fatal(err)
56
		}
57
		defer fi.Close()
58
59
		// Write each of the new lines to the file
60
		writer := bufio.NewWriter(fi)
61
		for _, line := range lines {
62
			fmt.Fprintln(writer, line)
63
		}
64
		writer.Flush()
65
	}
66
}