maehara-n commented on issue #1455:
URL: https://github.com/apache/age/issues/1455#issuecomment-1926282265

   @jrgemignani The code for "myapp" is just importing and using 
"github.com/apache/age/drivers/golang/age".
   The driver is referencing the parser.
   
   "myapp" project is structured as follows:
   ```
   myapp
     age_wrapper_sample.go
     main.go
     sql_api_sample.go
     go.mod
     go.sum
   ```
     main.go
   ```
   package main
   
   import (
        "fmt"
   
        _ "github.com/lib/pq"
   )
   
   // var dsn string = "host={host} port={port} dbname={dbname} user={username} 
password={password} sslmode=disable"
   var dsn string = "host=127.0.0.1 port=5432 dbname=postgres user=postgres 
password=agens sslmode=disable"
   
   // var graphName string = "{graph_path}"
   var graphName string = "testGraph"
   
   func main() {
   
        // Do cypher query to AGE with database/sql Tx API transaction control
        fmt.Println("# Do cypher query with SQL API")
        doWithSqlAPI(dsn, graphName)
   
        // Do cypher query to AGE with Age API
        fmt.Println("# Do cypher query with Age API")
        doWithAgeWrapper(dsn, graphName)
   }
   ```
   sql_api_sample.go
   ```
   package main
   
   import (
        "database/sql"
        "fmt"
   
        "github.com/apache/age/drivers/golang/age"
   )
   
   // Do cypher query to AGE with database/sql Tx API transaction control
   func doWithSqlAPI(dsn string, graphName string) {
   
        // Connect to PostgreSQL
        db, err := sql.Open("postgres", dsn)
        if err != nil {
                panic(err)
        }
   
        // Confirm graph_path created
        _, err = age.GetReady(db, graphName)
        if err != nil {
                panic(err)
        }
   
        // Tx begin for execute create vertex
        tx, err := db.Begin()
        if err != nil {
                panic(err)
        }
   
        // Create vertices with Cypher
        _, err = age.ExecCypher(tx, graphName, 0, "CREATE (n:Person {name: 
'%s', weight:%f})", "Joe", 67.3)
        if err != nil {
                panic(err)
        }
   
        _, err = age.ExecCypher(tx, graphName, 0, "CREATE (n:Person {name: 
'%s', weight:77.3, roles:['Dev','marketing']})", "Jack")
        if err != nil {
                panic(err)
        }
   
        _, err = age.ExecCypher(tx, graphName, 0, "CREATE (n:Person {name: 
'%s', weight:%d})", "Andy", 59)
        if err != nil {
                panic(err)
        }
   
        // Commit Tx
        tx.Commit()
   
        // Tx begin for queries
        tx, err = db.Begin()
        if err != nil {
                panic(err)
        }
        // Query cypher
        cypherCursor, err := age.ExecCypher(tx, graphName, 1, "MATCH (n:Person) 
RETURN n")
        if err != nil {
                panic(err)
        }
        // Unmarshal result data to Vertex row by row
        for cypherCursor.Next() {
                row, err := cypherCursor.GetRow()
                if err != nil {
                        panic(err)
                }
                vertex := row[0].(*age.Vertex)
                fmt.Println(vertex.Id(), vertex.Label(), vertex.Props())
        }
   
        // Create Paths (Edges)
        _, err = age.ExecCypher(tx, graphName, 0, "MATCH (a:Person), (b:Person) 
WHERE a.name='%s' AND b.name='%s' CREATE (a)-[r:workWith {weight: %d}]->(b)", 
"Jack", "Joe", 3)
        if err != nil {
                panic(err)
        }
   
        _, err = age.ExecCypher(tx, graphName, 0, "MATCH (a:Person {name: 
'%s'}), (b:Person {name: '%s'}) CREATE (a)-[r:workWith {weight: %d}]->(b)", 
"Joe", "Andy", 7)
        if err != nil {
                panic(err)
        }
   
        tx.Commit()
   
        tx, err = db.Begin()
        if err != nil {
                panic(err)
        }
        // Query Paths with Cypher
        cypherCursor, err = age.ExecCypher(tx, graphName, 1, "MATCH 
p=()-[:workWith]-() RETURN p")
        if err != nil {
                panic(err)
        }
   
        for cypherCursor.Next() {
                row, err := cypherCursor.GetRow()
                if err != nil {
                        panic(err)
                }
   
                path := row[0].(*age.Path)
                vertexStart := path.GetAsVertex(0)
                edge := path.GetAsEdge(1)
                vertexEnd := path.GetAsVertex(2)
   
                fmt.Println(vertexStart, edge, vertexEnd)
        }
   
        // Query with return many columns
        cursor, err := age.ExecCypher(tx, graphName, 3, "MATCH 
(a:Person)-[l:workWith]-(b:Person) RETURN a, l, b")
        if err != nil {
                panic(err)
        }
   
        count := 0
        for cursor.Next() {
                row, err := cursor.GetRow()
                if err != nil {
                        panic(err)
                }
                count++
                v1 := row[0].(*age.Vertex)
                edge := row[1].(*age.Edge)
                v2 := row[2].(*age.Vertex)
                fmt.Println("ROW ", count, ">>", "\n\t", v1, "\n\t", edge, 
"\n\t", v2)
        }
   
        // Delete Vertices
        _, err = age.ExecCypher(tx, graphName, 0, "MATCH (n:Person) DETACH 
DELETE n RETURN *")
        if err != nil {
                panic(err)
        }
        tx.Commit()
   }
   ```
   age_wrapper_sample.go
   ```
   package main
   
   import (
        "fmt"
   
        "github.com/apache/age/drivers/golang/age"
   )
   
   // Do cypher query to AGE with Age API
   func doWithAgeWrapper(dsn string, graphName string) {
   
        ag, err := age.ConnectAge(graphName, dsn)
   
        if err != nil {
                panic(err)
        }
   
        tx, err := ag.Begin()
        if err != nil {
                panic(err)
        }
   
        _, err = tx.ExecCypher(0, "CREATE (n:Person {name: '%s'})", "Joe")
        if err != nil {
                panic(err)
        }
   
        _, err = tx.ExecCypher(0, "CREATE (n:Person {name: '%s', age: %d})", 
"Smith", 10)
        if err != nil {
                panic(err)
        }
   
        _, err = tx.ExecCypher(0, "CREATE (n:Person {name: '%s', weight:%f})", 
"Jack", 70.3)
        if err != nil {
                panic(err)
        }
   
        tx.Commit()
   
        tx, err = ag.Begin()
        if err != nil {
                panic(err)
        }
   
        cursor, err := tx.ExecCypher(1, "MATCH (n:Person) RETURN n")
        if err != nil {
                panic(err)
        }
   
        count := 0
        for cursor.Next() {
                entities, err := cursor.GetRow()
                if err != nil {
                        panic(err)
                }
                count++
                vertex := entities[0].(*age.Vertex)
                fmt.Println(count, "]", vertex.Id(), vertex.Label(), 
vertex.Props())
        }
   
        fmt.Println("Vertex Count:", count)
   
        _, err = tx.ExecCypher(0, "MATCH (a:Person), (b:Person) WHERE 
a.name='%s' AND b.name='%s' CREATE (a)-[r:workWith {weight: %d}]->(b)",
                "Jack", "Joe", 3)
        if err != nil {
                panic(err)
        }
   
        _, err = tx.ExecCypher(0, "MATCH (a:Person {name: '%s'}), (b:Person 
{name: '%s'}) CREATE (a)-[r:workWith {weight: %d}]->(b)",
                "Joe", "Smith", 7)
        if err != nil {
                panic(err)
        }
   
        tx.Commit()
   
        tx, err = ag.Begin()
        if err != nil {
                panic(err)
        }
   
        cursor, err = tx.ExecCypher(1, "MATCH p=()-[:workWith]-() RETURN p")
        if err != nil {
                panic(err)
        }
   
        count = 0
        for cursor.Next() {
                entities, err := cursor.GetRow()
                if err != nil {
                        panic(err)
                }
                count++
   
                path := entities[0].(*age.Path)
   
                vertexStart := path.GetAsVertex(0)
                edge := path.GetAsEdge(1)
                vertexEnd := path.GetAsVertex(2)
   
                fmt.Println(count, "]", vertexStart, edge.Props(), vertexEnd)
        }
   
        // Query with return many columns
        cursor, err = tx.ExecCypher(3, "MATCH 
(a:Person)-[l:workWith]-(b:Person) RETURN a, l, b")
        if err != nil {
                panic(err)
        }
   
        count = 0
        for cursor.Next() {
                row, err := cursor.GetRow()
                if err != nil {
                        panic(err)
                }
   
                count++
   
                v1 := row[0].(*age.Vertex)
                edge := row[1].(*age.Edge)
                v2 := row[2].(*age.Vertex)
   
                fmt.Println("ROW ", count, ">>", "\n\t", v1, "\n\t", edge, 
"\n\t", v2)
        }
   
        _, err = tx.ExecCypher(0, "MATCH (n:Person) DETACH DELETE n RETURN *")
        if err != nil {
                panic(err)
        }
        tx.Commit()
   }
   ```
   If I describe go.mod as follows, the build will fail.
   ```
   module myapp
   
   go 1.21.5
   
   require (
        github.com/apache/age/drivers/golang v0.0.0-20240131202942-03a22b5269ec
        github.com/lib/pq v1.10.9
   )
   
   require (
        github.com/antlr/antlr4/runtime/Go/antlr/v4 
v4.0.0-20230321174746-8dcc6526cfb1 // indirect
        golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
   )
   ```
   If I describe go.mod as follows, the build will succeed.
   ```
   module myapp
   
   go 1.21.5
   
   require (
        github.com/apache/age/drivers/golang v0.0.0-20230125234102-2a233e3e2da9
        github.com/lib/pq v1.10.9
   )
   
   require github.com/antlr/antlr4/runtime/Go/antlr 
v0.0.0-20210521184019-c5ad59b459ec // indirect
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@age.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to