vesoft-inc / nebula-go

Nebula client in Golang
Apache License 2.0
134 stars 71 forks source link

proposal: Make execute more user-friendly #111

Closed Ranxy closed 3 years ago

Ranxy commented 3 years ago

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?

wey-gu commented 3 years ago

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

laura-ding commented 3 years ago

@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 commented 3 years ago

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

laura-ding commented 3 years ago

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

Thanks for your contribution.

wey-gu commented 3 years ago

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

Thank you @Ranxy!

everywan commented 3 years ago

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