-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
153 lines (124 loc) · 3.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// A simple example of quickly converting SQL result into an Excel file.
package main
import (
"database/sql"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"time"
_ "github.com/denisenkom/go-mssqldb"
"github.com/tealeg/xlsx"
)
// Variables used with command line arguments
var (
host string
user string
pass string
sqlf string
outf string
)
// Parse command line arguments
func init() {
flag.StringVar(&host, "h", "", "SQL Server hostname or IP")
flag.StringVar(&user, "u", "", "User ID")
flag.StringVar(&pass, "p", "", "Password")
flag.StringVar(&sqlf, "s", "", "SQL Query filename")
flag.StringVar(&outf, "o", "", "Output filename")
if len(os.Args) < 5 {
flag.Usage()
os.Exit(1)
}
flag.Parse()
}
func main() {
// Connect to SQL Server
dsn := fmt.Sprintf("server=%s;user id=%s;password=%s;encrypt=disable", host, user, pass)
conn, err := sql.Open("mssql", dsn)
if err != nil {
log.Fatalf("error opening connection to server, %s\n", err)
}
defer conn.Close()
// Verify connection to SQL Server
err = conn.Ping()
if err != nil {
log.Fatalf("error verifying connection to server, %s\n", err)
return
}
// Load SQL Query file
query, err := ioutil.ReadFile(sqlf)
if err != nil {
log.Fatalf("error opening SQL Query file %s, %s\n", sqlf, err)
}
// Query the database
rows, err := conn.Query(string(query))
if err != nil {
log.Fatalf("error running query, %s\n", err)
}
defer rows.Close()
err = generateXLSXFromRows(rows, outf)
if err != nil {
log.Fatal(err)
}
}
func generateXLSXFromRows(rows *sql.Rows, outf string) error {
var err error
// Get column names from query result
colNames, err := rows.Columns()
if err != nil {
return fmt.Errorf("error fetching column names, %s\n", err)
}
length := len(colNames)
// Create a interface slice filled with pointers to interface{}'s
pointers := make([]interface{}, length)
container := make([]interface{}, length)
for i := range pointers {
pointers[i] = &container[i]
}
// Create output xlsx workbook
xfile := xlsx.NewFile()
xsheet, err := xfile.AddSheet("Sheet1")
if err != nil {
return fmt.Errorf("error adding sheet to xlsx file, %s\n", err)
}
// Write Headers to 1st row
xrow := xsheet.AddRow()
xrow.WriteSlice(&colNames, -1)
// Process sql rows
for rows.Next() {
// Scan the sql rows into the interface{} slice
err = rows.Scan(pointers...)
if err != nil {
return fmt.Errorf("error scanning sql row, %s\n", err)
}
xrow = xsheet.AddRow()
// Here we range over our container and look at each column
// and set some different options depending on the column type.
for _, v := range container {
xcell := xrow.AddCell()
switch v := v.(type) {
case string:
xcell.SetString(v)
case []byte:
xcell.SetString(string(v))
case int64:
xcell.SetInt64(v)
case float64:
xcell.SetFloat(v)
case bool:
xcell.SetBool(v)
case time.Time:
xcell.SetDateTime(v)
default:
xcell.SetValue(v)
}
}
}
// Save the excel file to the provided output file
err = xfile.Save(outf)
if err != nil {
return fmt.Errorf("error writing to output file %s, %s\n", outf, err)
}
return nil
}