当前位置: 首页>>代码示例>>Golang>>正文


Golang Database.Begin方法代码示例

本文整理汇总了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()
}
开发者ID:zombor,项目名称:neoism,代码行数:22,代码来源:presentation.go

示例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}
//.........这里部分代码省略.........
开发者ID:fiatjaf,项目名称:nodes,代码行数:101,代码来源:create-equality.go


注:本文中的github.com/jmcvetta/neoism.Database.Begin方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。