Skip to content

Instantly share code, notes, and snippets.

@Mawulijo
Last active December 13, 2018 11:25
Show Gist options
  • Save Mawulijo/a29f42681c853f199b9757ed4da252d0 to your computer and use it in GitHub Desktop.
Save Mawulijo/a29f42681c853f199b9757ed4da252d0 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"os"
"fmt"
"bufio"
"strings"
)
func createQueryFile(){
// pQueryString holds the beginning part of the query
pQueryString := "SELECT [circuit_id],[unique_id],[unique_number],
[created_user],[created_date],[last_edited_user],
[last_edited_date],[unique_id_hidden],[object_id]
FROM [<DB_NAME>].[dbo].[<TABLE_NAME>]
WHERE unique_id="
// creating a file to house the sql query
_, err := os.Create("queryFile.sql")
if err != nil {
log.Fatalln("error creating file", err)
}
// opening the file in append and read only mode
q, err := os.OpenFile("queryFile.sql", os.O_APPEND|os.O_WRONLY,0644)
if err != nil {
fmt.Println(err)
return
}
q.WriteString(pQueryString)
// opening the unique IDs file in read and write mode
IDsFile, err := os.OpenFile("uniqueIds.txt", os.O_RDWR,0644)
defer func(){
if err = IDsFile.Close(); err != nil{
log.Fatal(err)
}
}()
// creating a scanner on the unique IDs file
s := bufio.NewScanner(IDsFile)
// get the first unique ID in file and append it to query string
if s.Scan(){
q.WriteString("'"+strings.TrimSpace(s.Text())+"'")
}
// append the remaining unique IDS
for s.Scan() {
fmt.Fprintln(q, "OR unique_id=" + "'"+strings.TrimSpace(s.Text())+"'" )
}
err = s.Err()
if err != nil {
log.Fatal(err)
}
q.WriteString(";")
}
func main() {
createQueryFile()
fmt.Println("Query File Successfully Created")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment