Guest User

Untitled

a guest
Nov 9th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "flag"
  5. "fmt"
  6. "log"
  7. "os"
  8. "strings"
  9.  
  10. "github.com/neo4j/neo4j-go-driver/neo4j"
  11. )
  12.  
  13. var (
  14. uri string
  15. username string
  16. password string
  17. query string
  18. )
  19.  
  20. // Simple header printing logic, open to improvements
  21. func processHeaders(result neo4j.Result) {
  22. if keys, err := result.Keys(); err == nil {
  23. for index, key := range keys {
  24. if index > 0 {
  25. fmt.Print("\t")
  26. }
  27. fmt.Printf("%-10s", key)
  28. }
  29. fmt.Print("\n")
  30.  
  31. for index := range keys {
  32. if index > 0 {
  33. fmt.Print("\t")
  34. }
  35. fmt.Print(strings.Repeat("=", 10))
  36. }
  37. fmt.Print("\n")
  38. }
  39. }
  40.  
  41. // Simple record values printing logic, open to improvements
  42. func processRecord(record neo4j.Record) {
  43. for index, value := range record.Values() {
  44. if index > 0 {
  45. fmt.Print("\t")
  46. }
  47. fmt.Printf("%-10v", value)
  48. }
  49. fmt.Print("\n")
  50. }
  51.  
  52. // Transaction function
  53. func executeQuery(tx neo4j.Transaction) (interface{}, error) {
  54. var (
  55. counter int
  56. result neo4j.Result
  57. err error
  58. )
  59.  
  60. // Execute the query on the provided transaction
  61. if result, err = tx.Run(query, nil); err != nil {
  62. return nil, err
  63. }
  64.  
  65. // Print headers
  66. processHeaders(result)
  67.  
  68. // Loop through record stream until EOF or error
  69. for result.Next() {
  70. processRecord(result.Record())
  71. counter++
  72. }
  73. // Check if we encountered any error during record streaming
  74. if err = result.Err(); err != nil {
  75. return nil, err
  76. }
  77.  
  78. // Return counter
  79. return counter, nil
  80. }
  81.  
  82. func run() error {
  83. var (
  84. driver neo4j.Driver
  85. session neo4j.Session
  86. recordsProcessed interface{}
  87. err error
  88. )
  89.  
  90. // Construct a new driver
  91. if driver, err = neo4j.NewDriver(uri, neo4j.BasicAuth(username, password, ""), func(config *neo4j.Config) {
  92. config.Log = neo4j.ConsoleLogger(neo4j.ERROR)
  93. }); err != nil {
  94. return err
  95. }
  96. defer driver.Close()
  97.  
  98. // Acquire a session
  99. if session, err = driver.Session(neo4j.AccessModeRead); err != nil {
  100. return err
  101. }
  102. defer session.Close()
  103.  
  104. // Execute the transaction function
  105. if recordsProcessed, err = session.ReadTransaction(executeQuery); err != nil {
  106. return err
  107. }
  108.  
  109. fmt.Printf("\n%d records processed\n", recordsProcessed)
  110.  
  111. return nil
  112. }
  113.  
  114. func parseAndVerifyFlags() bool {
  115. flag.Parse()
  116.  
  117. if uri == "" || username == "" || password == "" || query == "" {
  118. flag.Usage()
  119. return false
  120. }
  121.  
  122. return true
  123. }
  124.  
  125. func main() {
  126. if !parseAndVerifyFlags() {
  127. os.Exit(-1)
  128. }
  129.  
  130. if err := run(); err != nil {
  131. log.Fatal(err)
  132. os.Exit(1)
  133. }
  134.  
  135. os.Exit(0)
  136. }
  137.  
  138. func init() {
  139. flag.StringVar(&uri, "uri", "bolt://localhost", "the bolt uri to connect to")
  140. flag.StringVar(&username, "username", "neo4j", "the database username")
  141. flag.StringVar(&password, "password", "", "the database password")
  142. flag.StringVar(&query, "query", "", "the query to execute")
  143. }
Add Comment
Please, Sign In to add comment