@@ -13,6 +13,7 @@ package attachment
1313
1414import (
1515 "database/sql"
16+ "fmt"
1617 "strings"
1718 "time"
1819
@@ -36,8 +37,8 @@ func (s Store) Add(ctx domain.RequestContext, a attachment.Attachment) (err erro
3637 bits := strings .Split (a .Filename , "." )
3738 a .Extension = bits [len (bits )- 1 ]
3839
39- _ , err = ctx .Transaction .Exec (s .Bind ("INSERT INTO dmz_doc_attachment (c_refid, c_orgid, c_docid, c_job, c_fileid, c_filename, c_data, c_extension, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ),
40- a .RefID , a .OrgID , a .DocumentID , a .Job , a .FileID , a .Filename , a .Data , a .Extension , a .Created , a .Revised )
40+ _ , err = ctx .Transaction .Exec (s .Bind ("INSERT INTO dmz_doc_attachment (c_refid, c_orgid, c_docid, c_sectionid, c_job, c_fileid, c_filename, c_data, c_extension, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ),
41+ a .RefID , a .OrgID , a .DocumentID , a .SectionID , a . Job , a .FileID , a .Filename , a .Data , a .Extension , a .Created , a .Revised )
4142
4243 if err != nil {
4344 err = errors .Wrap (err , "execute insert attachment" )
@@ -50,7 +51,7 @@ func (s Store) Add(ctx domain.RequestContext, a attachment.Attachment) (err erro
5051func (s Store ) GetAttachment (ctx domain.RequestContext , orgID , attachmentID string ) (a attachment.Attachment , err error ) {
5152 err = s .Runtime .Db .Get (& a , s .Bind (`
5253 SELECT id, c_refid AS refid,
53- c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
54+ c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
5455 c_filename AS filename, c_data AS data, c_extension AS extension,
5556 c_created AS created, c_revised AS revised
5657 FROM dmz_doc_attachment
@@ -68,7 +69,7 @@ func (s Store) GetAttachment(ctx domain.RequestContext, orgID, attachmentID stri
6869func (s Store ) GetAttachments (ctx domain.RequestContext , docID string ) (a []attachment.Attachment , err error ) {
6970 err = s .Runtime .Db .Select (& a , s .Bind (`
7071 SELECT id, c_refid AS refid,
71- c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
72+ c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
7273 c_filename AS filename, c_extension AS extension,
7374 c_created AS created, c_revised AS revised
7475 FROM dmz_doc_attachment
@@ -88,11 +89,36 @@ func (s Store) GetAttachments(ctx domain.RequestContext, docID string) (a []atta
8889 return
8990}
9091
92+ // GetSectionAttachments returns a slice containing the attachment records
93+ // with file data for specified document section.
94+ func (s Store ) GetSectionAttachments (ctx domain.RequestContext , sectionID string ) (a []attachment.Attachment , err error ) {
95+ err = s .Runtime .Db .Select (& a , s .Bind (`
96+ SELECT id, c_refid AS refid,
97+ c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
98+ c_filename AS filename, c_data AS data, c_extension AS extension,
99+ c_created AS created, c_revised AS revised
100+ FROM dmz_doc_attachment
101+ WHERE c_orgid=? AND c_sectionid=?
102+ ORDER BY c_filename` ),
103+ ctx .OrgID , sectionID )
104+
105+ if err == sql .ErrNoRows {
106+ err = nil
107+ a = []attachment.Attachment {}
108+ }
109+ if err != nil {
110+ err = errors .Wrap (err , "execute select section attachments" )
111+ return
112+ }
113+
114+ return
115+ }
116+
91117// GetAttachmentsWithData returns a slice containing the attachment records (including their data) for document docID, ordered by filename.
92118func (s Store ) GetAttachmentsWithData (ctx domain.RequestContext , docID string ) (a []attachment.Attachment , err error ) {
93119 err = s .Runtime .Db .Select (& a , s .Bind (`
94120 SELECT id, c_refid AS refid,
95- c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
121+ c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
96122 c_filename AS filename, c_data AS data, c_extension AS extension,
97123 c_created AS created, c_revised AS revised
98124 FROM dmz_doc_attachment
@@ -116,3 +142,11 @@ func (s Store) GetAttachmentsWithData(ctx domain.RequestContext, docID string) (
116142func (s Store ) Delete (ctx domain.RequestContext , id string ) (rows int64 , err error ) {
117143 return s .DeleteConstrained (ctx .Transaction , "dmz_doc_attachment" , ctx .OrgID , id )
118144}
145+
146+ // DeleteSection removes all attachments agasinst a section.
147+ func (s Store ) DeleteSection (ctx domain.RequestContext , sectionID string ) (rows int64 , err error ) {
148+ rows , err = s .DeleteWhere (ctx .Transaction , fmt .Sprintf ("DELETE FROM dmz_doc_attachment WHERE c_orgid='%s' AND c_sectionid='%s'" ,
149+ ctx .OrgID , sectionID ))
150+
151+ return
152+ }
0 commit comments