本文整理匯總了Golang中github.com/jmcvetta/neoism.Database.Begin方法的典型用法代碼示例。如果您正苦於以下問題:Golang Database.Begin方法的具體用法?Golang Database.Begin怎麽用?Golang Database.Begin使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/jmcvetta/neoism.Database
的用法示例。
在下文中一共展示了Database.Begin方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: transaction
func transaction(db *neoism.Database) {
qs := []*neoism.CypherQuery{
&neoism.CypherQuery{
Statement: `CREATE (n {name: "Scottie", shirt: "red"}) RETURN n`,
},
&neoism.CypherQuery{
Statement: `START n=node(*), m=node(*)
WHERE m.name = {name} AND n.shirt IN {colors}
CREATE (n)-[r:outranks]->(m)
RETURN n.name, type(r), m.name`,
Parameters: neoism.Props{"name": "Scottie", "colors": []string{"yellow", "blue"}},
Result: &[]struct {
N string `json:"n.name"` // `json` tag matches column name in query
Rel string `json:"type(r)"`
M string `json:"m.name"`
}{},
},
}
tx, _ := db.Begin(qs)
fmt.Println(qs[1].Result) // &[{Kirk outranks Scottie} {Spock outranks Scottie} {McCoy o...
tx.Commit()
}
示例2: CreateEquality
func CreateEquality(db *neoism.Database, w http.ResponseWriter, r *http.Request) {
source, err := helpers.ParseURL(r.FormValue("source"))
if err != nil {
http.Error(w, "source is invalid URL: "+r.FormValue("source"), 400)
return
}
target, err := helpers.ParseURL(r.FormValue("target"))
if err != nil {
http.Error(w, "target is invalid URL: "+r.FormValue("target"), 400)
return
}
sourceTitle, err := helpers.GetTitle(source)
if err != nil {
http.Error(w, "Couldn't fetch title for "+source.String(), 400)
return
}
targetTitle, err := helpers.GetTitle(target)
if err != nil {
http.Error(w, "Couldn't fetch title for "+target.String(), 400)
return
}
if helpers.EqualURLs(source, target) {
http.Error(w, "urls are equal.", 400)
return
}
// standardize urls
stdsource := helpers.GetStandardizedURL(source)
stdtarget := helpers.GetStandardizedURL(target)
// get user
user := "fiatjaf"
// user.GetKarma()
now := time.Now().UTC().Format("20060102150405")
var queries []*neoism.CypherQuery
res := []struct {
SN int
TN int
SU int
TU int
RELATIONSHIPS []int
}{}
cq := neoism.CypherQuery{
Statement: `
MERGE (su:URL {stdurl: {stdsource}})
ON CREATE SET su.title = {sourceTitle}
SET su.rawurl = {rawsource}
MERGE (tu:URL {stdurl: {stdtarget}})
ON CREATE SET tu.title = {targetTitle}
SET su.rawurl = {rawtarget}
WITH su, tu
OPTIONAL MATCH (su)<-[:INSTANCE]-(sn:Node)
OPTIONAL MATCH (tu)<-[:INSTANCE]-(tn:Node)
OPTIONAL MATCH (sn)-[r:RELATIONSHIP]-()
WITH sn, tn, su, tu, collect(r) AS rels
RETURN id(sn) AS sn,
id(tn) AS tn,
id(su) AS su,
id(tu) AS tu,
extract(rel IN rels | id(rel)) AS relationships
`,
Parameters: neoism.Props{
"stdsource": stdsource,
"stdtarget": stdtarget,
"rawsource": source.String(),
"rawtarget": target.String(),
"sourceTitle": sourceTitle,
"targetTitle": targetTitle,
},
Result: &res,
}
queries = append(queries, &cq)
txn, err := db.Begin(queries)
if err != nil {
pretty.Log(err)
http.Error(w, err.Error(), 500)
txn.Rollback()
return
}
row := res[0]
// do our checks to decide if we are going to create Nodes, mix them or what
queries = make([]*neoism.CypherQuery, 0)
if row.SN == row.TN && row.SN != 0 {
// they exist and are the same, so we do nothing
} else if row.SN != 0 && row.TN != 0 {
// both exists, transfer everything from the source to target and delete source
for _, r := range row.RELATIONSHIPS {
log.Print(row.SN, row.TN, r)
queries = append(queries, &neoism.CypherQuery{
Statement: `
MATCH (sn) WHERE id(sn) = {sn}
MATCH (tn) WHERE id(tn) = {tn}
//.........這裏部分代碼省略.........