Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Make execute more user-friendly #111

Closed
Ranxy opened this issue Jun 30, 2021 · 6 comments
Closed

proposal: Make execute more user-friendly #111

Ranxy opened this issue Jun 30, 2021 · 6 comments

Comments

@Ranxy
Copy link
Contributor

Ranxy commented Jun 30, 2021

What is the problem execute a stmt?

Now when we want to execute a stmt, We need to use string formatting to write an stmt like this

sess.Execute(fmt.Sprintf("INSERT VERTEX t2 (name, age) VALUES \"11\":(\"%s\", %d);", name, age))

or just splice string like

stmt := "INSERT VERTEX t2 (name, age) VALUES \"11\":(\""+name+"\","+strconv.Itoa(age)+");"
sess.Execute(stmt)

this is not a good way to deal with.

What is proposed to do?

  • decompose stmt from strings into templates and parameters.
    sess.Execute("INSERT VERTEX t2 (name, age) VALUES \"11\":({name},{age});",map[string]interface{"name":name,"age":age})
  • Adding definitions for data types in parameters.
     sess.Execute("INSERT VERTEX t2 (time) VALUES \"11\":({time});",map[string]interface{"time":TimeOf(t)})
    this will generate statements such as
    INSERT VERTEX t2 (time) VALUES "11":(time("17:53:59"));
@wey-gu
Copy link
Contributor

wey-gu commented Jun 30, 2021

Thank you @Ranxy!
What do you think of this please? @Aiee @laura-ding

@laura-ding
Copy link
Contributor

laura-ding commented Jun 30, 2021

@Ranxy Thanks for your question. 'execute' applies to all NGQL's, and INSERT NGQL is just one. execute is just a basic interface. The command splicing you want should be wrapped in another layer instead of being provided by execute. Welcome to contribute such wrapper layer.

@Ranxy
Copy link
Contributor Author

Ranxy commented Jun 30, 2021

@laura-ding Ok, I will then start the wrapped task.

@laura-ding
Copy link
Contributor

@laura-ding Ok, I will then start the wrapped task.

Thanks for your contribution.

@wey-gu
Copy link
Contributor

wey-gu commented Jul 1, 2021

@laura-ding Ok, I will then start the wrapped task.

Thank you @Ranxy!

@everywan
Copy link

everywan commented Jul 1, 2021

Hi everyone, I have write a orm library support nebula write by go, now it's feature like this

  1. support insert Vertex/Edge use map or struct
  2. parse execute result to struct or map.
  3. will support chainable in the future

Now i am test it in we production, and it will be a Open Source Project in one or two weeks later.

There are some example:

// init
func main() {
	dalector := norm.MustNewDialector(norm.DialectorConfig{
		Addresses: []string{"127.0.0.1:9669"},
		Timeout:   time.Second * 5,
	})
	db := norm.MustOpen(dalector, norm.Config{
		Space:    "test",
		Username: "test",
		Password: "test",
	})
	run(db)
}

func insertVertex(db *norm.DB) {
	user := &examples.User{
		VModel: norm.VModel{
			Vid: "user_101",
		},
		ID:      101,
		Created: 101,
	}
	err := db.Debug().InsertVertex(user)
	if err != nil {
		log.Errorf(context.TODO(), "insert %+v error: %v", user, err)
		panic(err)
	}
}

func insertEdge(db *norm.DB) {
	vote := &examples.AnswerVoteUp{
		EModel: norm.EModel{
			Src: "user_101",
			Dst: "answer_102",
		},
		VoteUpCnt: 101,
		Created:   100000,
	}
	err := db.Debug().InsertEdge(vote)
	if err != nil {
		log.Errorf(context.TODO(), "insert %+v error: %v", vote, err)
		panic(err)
	}
}

func matchSingle(db *norm.DB) {
	nsql := "match(v:user) where id(v)=='user_101' return v.id as id,v.created as created"
	user := examples.User{}
	err := db.Debug().ExecuteAndParse(nsql, &user)
	if err != nil {
		log.Errorf(context.TODO(), "exec %s error: %v", nsql, err)
		panic(err)
	}
	log.Infof(context.TODO(), "%+v", user)
}

(forgive my poor english

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants