forked from krasnopv/das
/
eventdance.go
112 lines (108 loc) · 3.11 KB
/
eventdance.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
package eventdal
import (
"database/sql"
"errors"
"fmt"
"github.com/ProximaB/das/businesslogic"
"github.com/ProximaB/das/dataaccess/common"
"github.com/ProximaB/das/dataaccess/util"
"github.com/Masterminds/squirrel"
"log"
)
// PostgresEventDanceRepository implements IEventDanceRepository
type PostgresEventDanceRepository struct {
Database *sql.DB
SqlBuilder squirrel.StatementBuilderType
}
const (
DAS_EVENT_DANCES_TABLE = "DAS.EVENT_DANCES"
)
func (repo PostgresEventDanceRepository) SearchEventDance(criteria businesslogic.SearchEventDanceCriteria) ([]businesslogic.EventDance, error) {
if repo.Database == nil {
return nil, errors.New(dalutil.DataSourceNotSpecifiedError(repo))
}
stmt := repo.SqlBuilder.Select(
fmt.Sprintf("%s, %s, %s, %s, %s, %s, %s",
common.ColumnPrimaryKey,
common.COL_EVENT_ID,
common.COL_DANCE_ID,
common.ColumnCreateUserID,
common.ColumnDateTimeCreated,
common.ColumnUpdateUserID,
common.ColumnDateTimeUpdated),
).From(DAS_EVENT_DANCES_TABLE).
OrderBy(common.ColumnPrimaryKey)
if criteria.EventID > 0 {
stmt = stmt.Where(squirrel.Eq{common.COL_EVENT_ID: criteria.EventID})
}
rows, err := stmt.RunWith(repo.Database).Query()
output := make([]businesslogic.EventDance, 0)
if err != nil {
log.Printf("[error] querying EventDances with criteria %#v: %v", criteria, err)
return output, err
}
for rows.Next() {
each := businesslogic.EventDance{}
scanErr := rows.Scan(
&each.ID,
&each.EventID,
&each.DanceID,
&each.CreateUserID,
&each.DateTimeCreated,
&each.UpdateUserID,
&each.DateTimeUpdated,
)
if scanErr != nil {
log.Printf("[error] scanning EventDance with %#v: %v", criteria, scanErr)
return output, scanErr
}
output = append(output, each)
}
closeErr := rows.Close()
return output, closeErr
}
func (repo PostgresEventDanceRepository) CreateEventDance(eventDance *businesslogic.EventDance) error {
if repo.Database == nil {
return errors.New(dalutil.DataSourceNotSpecifiedError(repo))
}
stmt := repo.SqlBuilder.Insert("").
Into(DAS_EVENT_DANCES_TABLE).
Columns(
common.COL_EVENT_ID,
common.COL_DANCE_ID,
common.ColumnCreateUserID,
common.ColumnDateTimeCreated,
common.ColumnUpdateUserID,
common.ColumnDateTimeUpdated).
Values(
eventDance.EventID,
eventDance.DanceID,
eventDance.CreateUserID,
eventDance.DateTimeCreated,
eventDance.CreateUserID,
eventDance.DateTimeUpdated,
).Suffix("RETURNING ID")
clause, args, err := stmt.ToSql()
if err != nil {
return err
}
if tx, txErr := repo.Database.Begin(); txErr != nil {
return txErr
} else {
tx.QueryRow(clause, args...).Scan(&eventDance.ID)
tx.Commit()
return err
}
}
func (repo PostgresEventDanceRepository) DeleteEventDance(eventDance businesslogic.EventDance) error {
if repo.Database == nil {
return errors.New(dalutil.DataSourceNotSpecifiedError(repo))
}
return errors.New("not implemented")
}
func (repo PostgresEventDanceRepository) UpdateEventDance(eventDance businesslogic.EventDance) error {
if repo.Database == nil {
return errors.New(dalutil.DataSourceNotSpecifiedError(repo))
}
return errors.New("not implemented")
}