err=Db.Select(&pages,"SELECT a.id, a.refid, a.orgid, a.documentid, a.userid, a.contenttype, a.level, a.sequence, a.title, a.body, a.revisions, a.created, a.revised FROM page a WHERE a.orgid=? AND a.documentid=? ORDER BY a.sequence",p.Context.OrgID,documentID)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to execute select pages for org %s and document %s",p.Context.OrgID,documentID),err)
return
}
return
}
// GetPagesWhereIn returns a slice, in presentation sequence, containing those page records for a given documentID
// where their refid is in the comma-separated list passed as inPages.
sql:="SELECT a.id, a.refid, a.orgid, a.documentid, a.userid, a.contenttype, a.level, a.sequence, a.title, a.body, a.revisions, a.created, a.revised FROM page a WHERE a.orgid=? AND a.documentid=? AND a.refid IN (?"+strings.Repeat(",?",len(tempValues)-1)+") ORDER BY sequence"
inValues:=make([]interface{},len(tempValues))
fori,v:=rangetempValues{
inValues[i]=interface{}(v)
}
args=append(args,inValues...)
stmt,err:=Db.Preparex(sql)
deferutility.Close(stmt)
iferr!=nil{
log.Error(fmt.Sprintf("Failed to prepare select pages for org %s and document %s where in %s",p.Context.OrgID,documentID,inPages),err)
return
}
rows,err:=stmt.Queryx(args...)
iferr!=nil{
log.Error(fmt.Sprintf("Failed to execute select pages for org %s and document %s where in %s",p.Context.OrgID,documentID,inPages),err)
return
}
deferutility.Close(rows)
forrows.Next(){
page:=entity.Page{}
err=rows.StructScan(&page)
iferr!=nil{
log.Error(fmt.Sprintf("Failed to scan row: select pages for org %s and document %s where in %s",p.Context.OrgID,documentID,inPages),err)
return
}
pages=append(pages,page)
}
iferr!=nil{
log.Error(fmt.Sprintf("Failed to execute select pages for org %s and document %s where in %s",p.Context.OrgID,documentID,inPages),err)
return
}
return
}
// GetPagesWithoutContent returns a slice containing all the page records for a given documentID, in presentation sequence,
// but without the body field (which holds the HTML content).
err=Db.Select(&pages,"SELECT id, refid, orgid, documentid, userid, contenttype, sequence, level, title, revisions, created, revised FROM page WHERE orgid=? AND documentid=? ORDER BY sequence",p.Context.OrgID,documentID)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to execute select pages for org %s and document %s",p.Context.OrgID,documentID),err)
return
}
return
}
// UpdatePage saves changes to the database and handles recording of revisions.
// Not all updates result in a revision being recorded hence the parameter.
stmt,err=p.Context.Transaction.Preparex("INSERT INTO revision (refid, orgid, documentid, ownerid, pageid, userid, contenttype, title, body, rawbody, config, created, revised) SELECT ? as refid, a.orgid, a.documentid, a.userid as ownerid, a.refid as pageid, ? as userid, a.contenttype, a.title, a.body, b.rawbody, b.config, ? as created, ? as revised FROM page a, pagemeta b WHERE a.refid=? AND a.refid=b.pageid")
deferutility.Close(stmt)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to prepare insert for page revision %s",page.RefID),err)
log.Error(fmt.Sprintf("Unable to execute insert for page revision %s",page.RefID),err)
returnerr
}
}
// Update page
varstmt2*sqlx.NamedStmt
stmt2,err=p.Context.Transaction.PrepareNamed("UPDATE page SET documentid=:documentid, level=:level, title=:title, body=:body, revisions=:revisions, sequence=:sequence, revised=:revised WHERE orgid=:orgid AND refid=:refid")
deferutility.Close(stmt2)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to prepare update for page %s",page.RefID),err)
return
}
_,err=stmt2.Exec(&page)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to execute update for page %s",page.RefID),err)
stmt,err=p.Context.Transaction.PrepareNamed("UPDATE pagemeta SET userid=:userid, documentid=:documentid, rawbody=:rawbody, config=:config, externalsource=:externalsource, revised=:revised WHERE orgid=:orgid AND pageid=:pageid")
deferutility.Close(stmt)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to prepare update for page meta %s",meta.PageID),err)
return
}
_,err=stmt.Exec(&meta)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to execute update for page meta %s",meta.PageID),err)
return
}
return
}
// UpdatePageSequence changes the presentation sequence of the pageID page in the document.
// It then propagates that change into the search table and audits that it has occurred.
stmt,err:=Db.Preparex("SELECT id, pageid, orgid, userid, documentid, rawbody, coalesce(config,JSON_UNQUOTE('{}')) as config, externalsource, created, revised FROM pagemeta WHERE orgid=? AND pageid=?")
deferutility.Close(stmt)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to prepare select for pagemeta %s",pageID),err)
return
}
err=stmt.Get(&meta,p.Context.OrgID,pageID)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to execute select for pagemeta %s",pageID),err)
return
}
return
}
// GetDocumentPageMeta returns the meta information associated with a document.
err=Db.Select(&meta,"SELECT id, pageid, orgid, userid, documentid, rawbody, coalesce(config,JSON_UNQUOTE('{}')) as config, externalsource, created, revised FROM pagemeta WHERE orgid=? AND documentid=?"+filter,p.Context.OrgID,documentID)
iferr!=nil{
log.Error(fmt.Sprintf("Unable to execute select document page meta for org %s and document %s",p.Context.OrgID,documentID),err)