forked from krasnopv/das
/
style.go
143 lines (135 loc) · 3.63 KB
/
style.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package referencedal
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"
)
const (
DAS_STYLE_TABLE = "DAS.STYLE"
)
// PostgresStyleRepository implements IStyleRepository and feeds data to
// businesslogic from a PostgreSQL database
type PostgresStyleRepository struct {
Database *sql.DB
SqlBuilder squirrel.StatementBuilderType
}
func (repo PostgresStyleRepository) CreateStyle(style *businesslogic.Style) error {
if repo.Database == nil {
return errors.New(dalutil.DataSourceNotSpecifiedError(repo))
}
stmt := repo.SqlBuilder.Insert("").Into(DAS_STYLE_TABLE).Columns(
common.COL_NAME,
common.COL_DESCRIPTION,
common.ColumnCreateUserID,
common.ColumnDateTimeCreated,
common.ColumnUpdateUserID,
common.ColumnDateTimeUpdated,
).Values(
style.Name,
style.Description,
style.CreateUserID,
style.DateTimeCreated,
style.UpdateUserID,
style.DateTimeUpdated,
).Suffix(
"RETURNING ID",
)
clause, args, err := stmt.ToSql()
if tx, txErr := repo.Database.Begin(); txErr != nil {
return txErr
} else {
row := repo.Database.QueryRow(clause, args...)
row.Scan(&style.ID)
tx.Commit()
}
return err
}
func (repo PostgresStyleRepository) DeleteStyle(style businesslogic.Style) error {
if repo.Database == nil {
return errors.New(dalutil.DataSourceNotSpecifiedError(repo))
}
stmt := repo.SqlBuilder.
Delete("").
From(DAS_STYLE_TABLE).
Where(squirrel.Eq{common.ColumnPrimaryKey: style.ID})
var err error
if tx, txErr := repo.Database.Begin(); txErr != nil {
return txErr
} else {
_, err = stmt.RunWith(repo.Database).Exec()
tx.Commit()
}
return err
}
func (repo PostgresStyleRepository) SearchStyle(criteria businesslogic.SearchStyleCriteria) ([]businesslogic.Style, 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_NAME,
common.COL_DESCRIPTION,
common.ColumnCreateUserID,
common.ColumnDateTimeCreated,
common.ColumnUpdateUserID,
common.ColumnDateTimeUpdated)).
From(DAS_STYLE_TABLE).
OrderBy(common.ColumnPrimaryKey)
if criteria.StyleID > 0 {
stmt = stmt.Where(squirrel.Eq{common.ColumnPrimaryKey: criteria.StyleID})
}
if len(criteria.Name) > 0 {
stmt = stmt.Where(squirrel.Eq{common.COL_NAME: criteria.Name})
}
rows, err := stmt.RunWith(repo.Database).Query()
styles := make([]businesslogic.Style, 0)
if err != nil {
return styles, err
}
for rows.Next() {
each := businesslogic.Style{}
rows.Scan(
&each.ID,
&each.Name,
&each.Description,
&each.CreateUserID,
&each.DateTimeCreated,
&each.UpdateUserID,
&each.DateTimeUpdated,
)
styles = append(styles, each)
}
rows.Close()
return styles, err
}
func (repo PostgresStyleRepository) UpdateStyle(style businesslogic.Style) error {
if repo.Database == nil {
return errors.New(dalutil.DataSourceNotSpecifiedError(repo))
}
stmt := repo.SqlBuilder.Update("").Table(DAS_STYLE_TABLE)
if style.ID > 0 {
stmt = stmt.Set(common.COL_NAME, style.Name).
Set(common.COL_DESCRIPTION, style.Description).
Set(common.ColumnUpdateUserID, style.UpdateUserID).
Set(common.ColumnDateTimeUpdated, style.DateTimeUpdated)
var err error
if tx, txErr := repo.Database.Begin(); txErr != nil {
return txErr
} else {
_, err = stmt.RunWith(repo.Database).Exec()
if err != nil {
tx.Rollback()
} else {
tx.Commit()
}
}
return err
} else {
return errors.New("style is not specified")
}
}