-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.goasdfas
117 lines (82 loc) · 1.93 KB
/
test.goasdfas
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
asdfasdf
import (
"bufio"
"context"
"database/sql"
"fmt"
"log"
"os"
"strings"
"time"
_ "github.com/lib/pq"
)
type App struct {
DB *sql.DB
}
func main() {
a := App{}
a.Initialize()
}
var projectName string
var err error
func (a *App) Initialize() {
dsn := fmt.Sprintf("host=localhost port=5432 sslmode=disable user=postgres password=changeme dbname=words")
a.DB, err = sql.Open("postgres", dsn)
if err != nil {
panic(err)
}
defer a.DB.Close()
err = a.DB.Ping()
if err != nil {
log.Printf("Error connecting to db: %v\n", err)
}
a.initializeDB()
}
func (a *App) initializeDB() {
var err error
words := []string{"noun", "adjective"}
ctx, cancelfunc := context.WithTimeout(context.Background(), 90*time.Second)
defer cancelfunc()
for _, word := range words {
checkRow := fmt.Sprintf("SELECT COUNT (DISTINCT word) FROM %s WHERE word IS NOT NULL", word)
table := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s ( id serial PRIMARY KEY, word text )", word)
insertRow := fmt.Sprintf("INSERT INTO %s ( word ) VALUES ($1)", word)
var fileLines []string
var rowCount int
file := "words/adjectives.text"
_, err = a.DB.ExecContext(ctx, table)
if err != nil {
panic(err)
}
if strings.Compare(word, "noun") == 0 {
file = "words/nouns.text"
}
numRows := a.DB.QueryRow(checkRow)
err = numRows.Scan(&rowCount)
if err != nil {
log.Fatal(err)
}
// if table has more than 10000 values break has it already has all words
if rowCount > 10000 {
continue
}
readFile, err := os.Open(file)
if err != nil {
fmt.Println(err)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
for fileScanner.Scan() {
fileLines = append(fileLines, fileScanner.Text())
}
readFile.Close()
for _, line := range fileLines {
_, err = a.DB.ExecContext(ctx, insertRow, line)
if err != nil {
panic(err)
}
}
readFile.Close()
defer a.DB.Close()
}
}