-
Notifications
You must be signed in to change notification settings - Fork 3
/
database.go
104 lines (89 loc) · 2.65 KB
/
database.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
package papergres
import (
"database/sql"
"fmt"
)
// Database contains all required database attributes
type Database struct {
// conn is all information needed to connect to the database.
conn *Connection
// connString referes to the DSN string for the current DB.
connString string
}
// Connection returns the connection information for a database
func (db *Database) Connection() Connection {
return *db.conn
}
// ConnectionString returns the DSN(Data Source Name) connection string for the
// current DB connection.
func (db *Database) ConnectionString() string {
if db.connString != "" {
return db.connString
}
db.connString = db.conn.String()
return db.connString
}
// CreateDatabase creates a default database
// Good for use during testing and local dev
func (db *Database) CreateDatabase() *Result {
// if Ping works then DB already exists
err := db.Ping()
if err == nil {
return NewResult()
}
sql := fmt.Sprintf(`
CREATE DATABASE %s
WITH
OWNER = postgres
ENCODING = 'UTF8'
LC_COLLATE = 'English_United States.1252'
LC_CTYPE = 'English_United States.1252'
TABLESPACE = pg_default
CONNECTION LIMIT = -1;`, db.Connection().Database)
conn := db.Connection()
conn.Database = ""
db.conn = &conn
db.connString = ""
return db.Query(sql).ExecNonQuery()
}
// GenerateInsert generates an insert query for the given object
func (db *Database) GenerateInsert(obj interface{}) *Query {
return db.Schema("public").GenerateInsert(obj)
}
// Insert inserts the passed in object
func (db *Database) Insert(obj interface{}) *Result {
return db.Schema("public").Insert(obj)
}
// InsertAll inserts a slice of objects concurrently.
// objs must be a slice with items in it.
// the Result slice will be in the same order as objs
// so a simple loop will set all the primary keys if needed:
// for i, r := range results {
// objs[i].Id = r.LastInsertId.ID
// }
func (db *Database) InsertAll(objs interface{}) ([]*Result, error) {
return db.Schema("public").InsertAll(objs)
}
// Ping tests the database connection
func (db *Database) Ping() error {
return open(db.ConnectionString()).Ping()
}
// Query creates a base new query object that can be used for all database operations
func (db *Database) Query(sql string, args ...interface{}) *Query {
return &Query{
SQL: sql,
Database: db,
Args: args,
}
}
// Stats returns DBStats. Right now this only returns OpenConnections
func (db *Database) Stats() sql.DBStats {
return open(db.ConnectionString()).Stats()
}
// Schema allows for certain operations that require a specific schema
func (db *Database) Schema(name string) *Schema {
return &Schema{
Name: name,
Database: db,
}
}