File size: 1,394 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package swoinfo

import (
	"fmt"
	"strings"

	"github.com/target/goalert/swo/swodb"
	"github.com/target/goalert/util/sqlutil"
)

// Table represents a table in the database.
type Table struct {
	name string
	deps map[string]struct{}
	cols []column
	id   column
}
type column swodb.TableColumnsRow

// Name returns the name of the table.
func (t Table) Name() string { return t.name }

// IDType returns the type of the ID column.
func (t Table) IDType() string { return t.id.ColDataType }

// Columns returns the names of the columns in the table.
func (t Table) Columns() []string {
	var cols []string
	for _, c := range t.cols {
		cols = append(cols, c.ColColumnName)
	}
	return cols
}

// InsesrtJSONRowsQuery returns a query that can be used to insert or upsert rows from the given JSON data.
func (t Table) InsertJSONRowsQuery(upsert bool) string {
	query := fmt.Sprintf("insert into %s select * from json_populate_recordset(null::%s, $1)", sqlutil.QuoteID(t.Name()), sqlutil.QuoteID(t.Name()))
	if !upsert {
		return query
	}

	sets := make([]string, 0, len(t.cols))
	for _, col := range t.Columns() {
		if col == "id" {
			continue
		}
		sets = append(sets, fmt.Sprintf("%s = excluded.%s", sqlutil.QuoteID(col), sqlutil.QuoteID(col)))
	}

	return fmt.Sprintf("%s on conflict (id) do update set %s where %s.id = excluded.id", query, strings.Join(sets, ", "), sqlutil.QuoteID(t.Name()))
}