From 836bfda35d8a78b186c69fa6ecff72f52cc04eae Mon Sep 17 00:00:00 2001 From: jun chen Date: Fri, 4 Mar 2022 21:05:42 -0800 Subject: [PATCH 1/4] log query --- delete.go | 20 ++++++++++++-------- gogm.go | 5 +++-- save.go | 37 ++++++++++++++++++++++--------------- session.go | 4 ++-- sessionv2.go | 10 ++++++++-- 5 files changed, 47 insertions(+), 29 deletions(-) diff --git a/delete.go b/delete.go index f6bbaeb..e51aea1 100644 --- a/delete.go +++ b/delete.go @@ -27,7 +27,7 @@ import ( ) // deleteNode is used to remove nodes from the database -func deleteNode(deleteObj interface{}) (neo4j.TransactionWork, error) { +func deleteNode(gogm *Gogm, deleteObj interface{}) (neo4j.TransactionWork, error) { rawType := reflect.TypeOf(deleteObj) if rawType.Kind() != reflect.Ptr && rawType.Kind() != reflect.Slice { @@ -72,11 +72,11 @@ func deleteNode(deleteObj interface{}) (neo4j.TransactionWork, error) { } } - return deleteByIds(ids...), nil + return deleteByIds(gogm, ids...), nil } // deleteByIds deletes node by graph ids -func deleteByIds(ids ...int64) neo4j.TransactionWork { +func deleteByIds(gogm *Gogm, ids ...int64) neo4j.TransactionWork { return func(tx neo4j.Transaction) (interface{}, error) { cyp, err := dsl.QB(). Cypher("UNWIND $rows as row"). @@ -93,9 +93,11 @@ func deleteByIds(ids ...int64) neo4j.TransactionWork { return nil, err } - _, err = tx.Run(cyp, map[string]interface{}{ + xp := map[string]interface{}{ "rows": ids, - }) + } + gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + _, err = tx.Run(cyp, xp) if err != nil { return nil, err } @@ -105,7 +107,7 @@ func deleteByIds(ids ...int64) neo4j.TransactionWork { } // deleteByUuids deletes nodes by uuids -func deleteByUuids(ids ...string) neo4j.TransactionWork { +func deleteByUuids(gogm *Gogm, ids ...string) neo4j.TransactionWork { return func(tx neo4j.Transaction) (interface{}, error) { cyp, err := dsl.QB(). Cypher("UNWIND {rows} as row"). @@ -121,9 +123,11 @@ func deleteByUuids(ids ...string) neo4j.TransactionWork { if err != nil { return nil, err } - _, err = tx.Run(cyp, map[string]interface{}{ + xp := map[string]interface{}{ "rows": ids, - }) + } + gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + _, err = tx.Run(cyp, xp) if err != nil { return nil, err } diff --git a/gogm.go b/gogm.go index f9273d6..2954eaf 100644 --- a/gogm.go +++ b/gogm.go @@ -251,8 +251,9 @@ func (g *Gogm) initDriverRoutine(neoConfig func(neoConf *neo4j.Config), doneChan AccessMode: neo4j.AccessModeRead, // DatabaseName: "neo4j", }) - - res, err := sess.Run("return 1", nil) + cyp := "return 1" + g.logger.Debugf("cypher - %v - {%v}", cyp, nil) + res, err := sess.Run(cyp, nil) if err != nil { doneChan <- err return diff --git a/save.go b/save.go index 8779813..043475a 100644 --- a/save.go +++ b/save.go @@ -101,7 +101,7 @@ func saveDepth(gogm *Gogm, obj interface{}, depth int) neo4j.TransactionWork { return nil, fmt.Errorf("failed to parse struct, %w", err) } // save/update nodes - err = createNodes(tx, nodes, nodeRef, nodeIdRef) + err = createNodes(gogm, tx, nodes, nodeRef, nodeIdRef) if err != nil { return nil, fmt.Errorf("failed to create nodes, %w", err) } @@ -137,14 +137,14 @@ func saveDepth(gogm *Gogm, obj interface{}, depth int) neo4j.TransactionWork { } if len(dels) != 0 { - err := removeRelations(tx, dels) + err := removeRelations(gogm, tx, dels) if err != nil { return nil, err } } if len(relations) != 0 { - err := relateNodes(tx, relations, nodeIdRef) + err := relateNodes(gogm, tx, relations, nodeIdRef) if err != nil { return nil, err } @@ -155,7 +155,7 @@ func saveDepth(gogm *Gogm, obj interface{}, depth int) neo4j.TransactionWork { } // relateNodes connects nodes together using edge config -func relateNodes(transaction neo4j.Transaction, relations map[string][]*relCreate, lookup map[uintptr]int64) error { +func relateNodes(gogm *Gogm, transaction neo4j.Transaction, relations map[string][]*relCreate, lookup map[uintptr]int64) error { if len(relations) == 0 { return errors.New("relations can not be nil or empty") } @@ -244,10 +244,11 @@ func relateNodes(transaction neo4j.Transaction, relations map[string][]*relCreat if err != nil { return fmt.Errorf("failed to build query, %w", err) } - - res, err := transaction.Run(cyp, map[string]interface{}{ + xp := map[string]interface{}{ "rows": params, - }) + } + gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + res, err := transaction.Run(cyp, xp) if err != nil { return fmt.Errorf("failed to relate nodes, %w", err) } else if err = res.Err(); err != nil { @@ -259,7 +260,7 @@ func relateNodes(transaction neo4j.Transaction, relations map[string][]*relCreat } // removes relationships between specified nodes -func removeRelations(transaction neo4j.Transaction, dels map[int64][]int64) error { +func removeRelations(gogm *Gogm, transaction neo4j.Transaction, dels map[int64][]int64) error { if len(dels) == 0 { return nil } @@ -290,9 +291,11 @@ func removeRelations(transaction neo4j.Transaction, dels map[int64][]int64) erro return err } - res, err := transaction.Run(cyq, map[string]interface{}{ + xp := map[string]interface{}{ "rows": params, - }) + } + gogm.logger.Debugf("cypher - %v - {%v}", cyq, xp) + res, err := transaction.Run(cyq, xp) if err != nil { return fmt.Errorf("%s: %w", err.Error(), ErrInternal) } else if err = res.Err(); err != nil { @@ -503,7 +506,7 @@ func generateCurRels(gogm *Gogm, parentPtr uintptr, current *reflect.Value, curr } // createNodes updates existing nodes and creates new nodes while also making a lookup table for ptr -> neoid -func createNodes(transaction neo4j.Transaction, crNodes map[string]map[uintptr]*nodeCreate, nodeRef map[uintptr]*reflect.Value, nodeIdRef map[uintptr]int64) error { +func createNodes(gogm *Gogm, transaction neo4j.Transaction, crNodes map[string]map[uintptr]*nodeCreate, nodeRef map[uintptr]*reflect.Value, nodeIdRef map[uintptr]int64) error { for label, nodes := range crNodes { var updateRows, newRows []interface{} for ptr, config := range nodes { @@ -541,9 +544,11 @@ func createNodes(transaction neo4j.Transaction, crNodes map[string]map[uintptr]* return fmt.Errorf("failed to build query, %w", err) } - res, err := transaction.Run(cyp, map[string]interface{}{ + xp := map[string]interface{}{ "rows": newRows, - }) + } + gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + res, err := transaction.Run(cyp, xp) if err != nil { return fmt.Errorf("failed to execute new node query, %w", err) } else if res.Err() != nil { @@ -607,9 +612,11 @@ func createNodes(transaction neo4j.Transaction, crNodes map[string]map[uintptr]* return fmt.Errorf("failed to build query, %w", err) } - res, err := transaction.Run(cyp, map[string]interface{}{ + xp := map[string]interface{}{ "rows": updateRows, - }) + } + gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + res, err := transaction.Run(cyp, xp) if err != nil { return fmt.Errorf("failed to run update query, %w", err) } else if res.Err() != nil { diff --git a/session.go b/session.go index 5209739..68bef01 100644 --- a/session.go +++ b/session.go @@ -447,7 +447,7 @@ func (s *Session) Delete(deleteObj interface{}) error { } // handle if in transaction - workFunc, err := deleteNode(deleteObj) + workFunc, err := deleteNode(s.gogm, deleteObj) if err != nil { return fmt.Errorf("failed to generate work func for delete, %w", err) } @@ -461,7 +461,7 @@ func (s *Session) DeleteUUID(uuid string) error { } // handle if in transaction - return s.runWrite(deleteByUuids(uuid)) + return s.runWrite(deleteByUuids(s.gogm, uuid)) } func (s *Session) runWrite(work neo4j.TransactionWork) error { diff --git a/sessionv2.go b/sessionv2.go index f2a191b..6345442 100644 --- a/sessionv2.go +++ b/sessionv2.go @@ -423,6 +423,7 @@ func (s *SessionV2Impl) runReadOnly(ctx context.Context, cyp string, params map[ if span != nil { span.LogKV("info", "running in existing transaction") } + s.gogm.logger.Debugf("cypher - %v - {%v}", cyp, params) result, err := s.tx.Run(cyp, params) if err != nil { return err @@ -435,6 +436,7 @@ func (s *SessionV2Impl) runReadOnly(ctx context.Context, cyp string, params map[ span.LogKV("info", "running in driver managed transaction") } _, err := s.neoSess.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) { + s.gogm.logger.Debugf("cypher - %v - {%v}", cyp, params) res, err := tx.Run(cyp, params) if err != nil { return nil, err @@ -495,7 +497,7 @@ func (s *SessionV2Impl) Delete(ctx context.Context, deleteObj interface{}) error } // handle if in transaction - workFunc, err := deleteNode(deleteObj) + workFunc, err := deleteNode(s.gogm, deleteObj) if err != nil { return fmt.Errorf("failed to generate work func for delete, %w", err) } @@ -517,7 +519,7 @@ func (s *SessionV2Impl) DeleteUUID(ctx context.Context, uuid string) error { } // handle if in transaction - return s.runWrite(ctx, deleteByUuids(uuid)) + return s.runWrite(ctx, deleteByUuids(s.gogm, uuid)) } func (s *SessionV2Impl) runWrite(ctx context.Context, work neo4j.TransactionWork) error { @@ -574,6 +576,7 @@ func (s *SessionV2Impl) Query(ctx context.Context, query string, properties map[ } return s.runWrite(ctx, func(tx neo4j.Transaction) (interface{}, error) { + s.gogm.logger.Debugf("cypher - %v - {%v}", query, properties) res, err := tx.Run(query, properties) if err != nil { return nil, err @@ -589,6 +592,7 @@ func (s *SessionV2Impl) QueryRaw(ctx context.Context, query string, properties m } var err error if s.tx != nil { + s.gogm.logger.Debugf("cypher - %v - {%v}", query, properties) res, err := s.tx.Run(query, properties) if err != nil { return nil, nil, fmt.Errorf("failed to execute query, %w", err) @@ -607,6 +611,7 @@ func (s *SessionV2Impl) QueryRaw(ctx context.Context, query string, properties m var sum neo4j.ResultSummary if s.conf.AccessMode == AccessModeRead { ires, err = s.neoSess.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) { + s.gogm.logger.Debugf("cypher - %v - {%v}", query, properties) res, err := tx.Run(query, properties) if err != nil { return nil, err @@ -623,6 +628,7 @@ func (s *SessionV2Impl) QueryRaw(ctx context.Context, query string, properties m }) } else { ires, err = s.neoSess.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) { + s.gogm.logger.Debugf("cypher - %v - {%v}", query, properties) res, err := tx.Run(query, properties) if err != nil { return nil, err From 20100176c592f30ee5dbfc005f1dd7a22dcb98d0 Mon Sep 17 00:00:00 2001 From: jun chen Date: Fri, 11 Mar 2022 21:06:53 -0800 Subject: [PATCH 2/4] optionally log query parameter --- delete.go | 4 ++-- gogm.go | 10 +++++++++- save.go | 8 ++++---- sessionv2.go | 12 ++++++------ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/delete.go b/delete.go index e51aea1..a925bfb 100644 --- a/delete.go +++ b/delete.go @@ -96,7 +96,7 @@ func deleteByIds(gogm *Gogm, ids ...int64) neo4j.TransactionWork { xp := map[string]interface{}{ "rows": ids, } - gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + gogm.LogQuery(cyp, xp) _, err = tx.Run(cyp, xp) if err != nil { return nil, err @@ -126,7 +126,7 @@ func deleteByUuids(gogm *Gogm, ids ...string) neo4j.TransactionWork { xp := map[string]interface{}{ "rows": ids, } - gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + gogm.LogQuery(cyp, xp) _, err = tx.Run(cyp, xp) if err != nil { return nil, err diff --git a/gogm.go b/gogm.go index 2954eaf..9406b04 100644 --- a/gogm.go +++ b/gogm.go @@ -252,7 +252,7 @@ func (g *Gogm) initDriverRoutine(neoConfig func(neoConf *neo4j.Config), doneChan // DatabaseName: "neo4j", }) cyp := "return 1" - g.logger.Debugf("cypher - %v - {%v}", cyp, nil) + g.LogQuery(cyp, nil) res, err := sess.Run(cyp, nil) if err != nil { doneChan <- err @@ -346,3 +346,11 @@ func (g *Gogm) NewSessionV2(conf SessionConfig) (SessionV2, error) { return newSessionWithConfigV2(g, conf) } + +func (g *Gogm) LogQuery(query string, params map[string]interface{}) { + if g.config.EnableLogParams { + g.logger.Debugf("cypher - %v - {%v}", query, params) + } else { + g.logger.Debugf("cypher - %v", query) + } +} diff --git a/save.go b/save.go index 043475a..62e06c2 100644 --- a/save.go +++ b/save.go @@ -247,7 +247,7 @@ func relateNodes(gogm *Gogm, transaction neo4j.Transaction, relations map[string xp := map[string]interface{}{ "rows": params, } - gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + gogm.LogQuery(cyp, xp) res, err := transaction.Run(cyp, xp) if err != nil { return fmt.Errorf("failed to relate nodes, %w", err) @@ -294,7 +294,7 @@ func removeRelations(gogm *Gogm, transaction neo4j.Transaction, dels map[int64][ xp := map[string]interface{}{ "rows": params, } - gogm.logger.Debugf("cypher - %v - {%v}", cyq, xp) + gogm.LogQuery(cyq, xp) res, err := transaction.Run(cyq, xp) if err != nil { return fmt.Errorf("%s: %w", err.Error(), ErrInternal) @@ -547,7 +547,7 @@ func createNodes(gogm *Gogm, transaction neo4j.Transaction, crNodes map[string]m xp := map[string]interface{}{ "rows": newRows, } - gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + gogm.LogQuery(cyp, xp) res, err := transaction.Run(cyp, xp) if err != nil { return fmt.Errorf("failed to execute new node query, %w", err) @@ -615,7 +615,7 @@ func createNodes(gogm *Gogm, transaction neo4j.Transaction, crNodes map[string]m xp := map[string]interface{}{ "rows": updateRows, } - gogm.logger.Debugf("cypher - %v - {%v}", cyp, xp) + gogm.LogQuery(cyp, xp) res, err := transaction.Run(cyp, xp) if err != nil { return fmt.Errorf("failed to run update query, %w", err) diff --git a/sessionv2.go b/sessionv2.go index 6345442..abb6252 100644 --- a/sessionv2.go +++ b/sessionv2.go @@ -423,7 +423,7 @@ func (s *SessionV2Impl) runReadOnly(ctx context.Context, cyp string, params map[ if span != nil { span.LogKV("info", "running in existing transaction") } - s.gogm.logger.Debugf("cypher - %v - {%v}", cyp, params) + s.gogm.LogQuery(cyp, params) result, err := s.tx.Run(cyp, params) if err != nil { return err @@ -436,7 +436,7 @@ func (s *SessionV2Impl) runReadOnly(ctx context.Context, cyp string, params map[ span.LogKV("info", "running in driver managed transaction") } _, err := s.neoSess.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) { - s.gogm.logger.Debugf("cypher - %v - {%v}", cyp, params) + s.gogm.LogQuery(cyp, params) res, err := tx.Run(cyp, params) if err != nil { return nil, err @@ -576,7 +576,7 @@ func (s *SessionV2Impl) Query(ctx context.Context, query string, properties map[ } return s.runWrite(ctx, func(tx neo4j.Transaction) (interface{}, error) { - s.gogm.logger.Debugf("cypher - %v - {%v}", query, properties) + s.gogm.LogQuery(query, properties) res, err := tx.Run(query, properties) if err != nil { return nil, err @@ -592,7 +592,7 @@ func (s *SessionV2Impl) QueryRaw(ctx context.Context, query string, properties m } var err error if s.tx != nil { - s.gogm.logger.Debugf("cypher - %v - {%v}", query, properties) + s.gogm.LogQuery(query, properties) res, err := s.tx.Run(query, properties) if err != nil { return nil, nil, fmt.Errorf("failed to execute query, %w", err) @@ -611,7 +611,7 @@ func (s *SessionV2Impl) QueryRaw(ctx context.Context, query string, properties m var sum neo4j.ResultSummary if s.conf.AccessMode == AccessModeRead { ires, err = s.neoSess.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) { - s.gogm.logger.Debugf("cypher - %v - {%v}", query, properties) + s.gogm.LogQuery(query, properties) res, err := tx.Run(query, properties) if err != nil { return nil, err @@ -628,7 +628,7 @@ func (s *SessionV2Impl) QueryRaw(ctx context.Context, query string, properties m }) } else { ires, err = s.neoSess.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) { - s.gogm.logger.Debugf("cypher - %v - {%v}", query, properties) + s.gogm.LogQuery(query, properties) res, err := tx.Run(query, properties) if err != nil { return nil, err From d62f5a660910c77bf82e00d51903f05fdace8533 Mon Sep 17 00:00:00 2001 From: jun chen Date: Sat, 12 Mar 2022 08:39:26 -0800 Subject: [PATCH 3/4] use log wrapper around transaction and session --- delete.go | 20 +++++++---------- gogm.go | 13 ++--------- save.go | 37 +++++++++++++------------------ session.go | 4 ++-- sessionv2.go | 61 ++++++++++++++++++++++++++++++++++++++++++---------- 5 files changed, 77 insertions(+), 58 deletions(-) diff --git a/delete.go b/delete.go index a925bfb..f6bbaeb 100644 --- a/delete.go +++ b/delete.go @@ -27,7 +27,7 @@ import ( ) // deleteNode is used to remove nodes from the database -func deleteNode(gogm *Gogm, deleteObj interface{}) (neo4j.TransactionWork, error) { +func deleteNode(deleteObj interface{}) (neo4j.TransactionWork, error) { rawType := reflect.TypeOf(deleteObj) if rawType.Kind() != reflect.Ptr && rawType.Kind() != reflect.Slice { @@ -72,11 +72,11 @@ func deleteNode(gogm *Gogm, deleteObj interface{}) (neo4j.TransactionWork, error } } - return deleteByIds(gogm, ids...), nil + return deleteByIds(ids...), nil } // deleteByIds deletes node by graph ids -func deleteByIds(gogm *Gogm, ids ...int64) neo4j.TransactionWork { +func deleteByIds(ids ...int64) neo4j.TransactionWork { return func(tx neo4j.Transaction) (interface{}, error) { cyp, err := dsl.QB(). Cypher("UNWIND $rows as row"). @@ -93,11 +93,9 @@ func deleteByIds(gogm *Gogm, ids ...int64) neo4j.TransactionWork { return nil, err } - xp := map[string]interface{}{ + _, err = tx.Run(cyp, map[string]interface{}{ "rows": ids, - } - gogm.LogQuery(cyp, xp) - _, err = tx.Run(cyp, xp) + }) if err != nil { return nil, err } @@ -107,7 +105,7 @@ func deleteByIds(gogm *Gogm, ids ...int64) neo4j.TransactionWork { } // deleteByUuids deletes nodes by uuids -func deleteByUuids(gogm *Gogm, ids ...string) neo4j.TransactionWork { +func deleteByUuids(ids ...string) neo4j.TransactionWork { return func(tx neo4j.Transaction) (interface{}, error) { cyp, err := dsl.QB(). Cypher("UNWIND {rows} as row"). @@ -123,11 +121,9 @@ func deleteByUuids(gogm *Gogm, ids ...string) neo4j.TransactionWork { if err != nil { return nil, err } - xp := map[string]interface{}{ + _, err = tx.Run(cyp, map[string]interface{}{ "rows": ids, - } - gogm.LogQuery(cyp, xp) - _, err = tx.Run(cyp, xp) + }) if err != nil { return nil, err } diff --git a/gogm.go b/gogm.go index 9406b04..f9273d6 100644 --- a/gogm.go +++ b/gogm.go @@ -251,9 +251,8 @@ func (g *Gogm) initDriverRoutine(neoConfig func(neoConf *neo4j.Config), doneChan AccessMode: neo4j.AccessModeRead, // DatabaseName: "neo4j", }) - cyp := "return 1" - g.LogQuery(cyp, nil) - res, err := sess.Run(cyp, nil) + + res, err := sess.Run("return 1", nil) if err != nil { doneChan <- err return @@ -346,11 +345,3 @@ func (g *Gogm) NewSessionV2(conf SessionConfig) (SessionV2, error) { return newSessionWithConfigV2(g, conf) } - -func (g *Gogm) LogQuery(query string, params map[string]interface{}) { - if g.config.EnableLogParams { - g.logger.Debugf("cypher - %v - {%v}", query, params) - } else { - g.logger.Debugf("cypher - %v", query) - } -} diff --git a/save.go b/save.go index 62e06c2..8779813 100644 --- a/save.go +++ b/save.go @@ -101,7 +101,7 @@ func saveDepth(gogm *Gogm, obj interface{}, depth int) neo4j.TransactionWork { return nil, fmt.Errorf("failed to parse struct, %w", err) } // save/update nodes - err = createNodes(gogm, tx, nodes, nodeRef, nodeIdRef) + err = createNodes(tx, nodes, nodeRef, nodeIdRef) if err != nil { return nil, fmt.Errorf("failed to create nodes, %w", err) } @@ -137,14 +137,14 @@ func saveDepth(gogm *Gogm, obj interface{}, depth int) neo4j.TransactionWork { } if len(dels) != 0 { - err := removeRelations(gogm, tx, dels) + err := removeRelations(tx, dels) if err != nil { return nil, err } } if len(relations) != 0 { - err := relateNodes(gogm, tx, relations, nodeIdRef) + err := relateNodes(tx, relations, nodeIdRef) if err != nil { return nil, err } @@ -155,7 +155,7 @@ func saveDepth(gogm *Gogm, obj interface{}, depth int) neo4j.TransactionWork { } // relateNodes connects nodes together using edge config -func relateNodes(gogm *Gogm, transaction neo4j.Transaction, relations map[string][]*relCreate, lookup map[uintptr]int64) error { +func relateNodes(transaction neo4j.Transaction, relations map[string][]*relCreate, lookup map[uintptr]int64) error { if len(relations) == 0 { return errors.New("relations can not be nil or empty") } @@ -244,11 +244,10 @@ func relateNodes(gogm *Gogm, transaction neo4j.Transaction, relations map[string if err != nil { return fmt.Errorf("failed to build query, %w", err) } - xp := map[string]interface{}{ + + res, err := transaction.Run(cyp, map[string]interface{}{ "rows": params, - } - gogm.LogQuery(cyp, xp) - res, err := transaction.Run(cyp, xp) + }) if err != nil { return fmt.Errorf("failed to relate nodes, %w", err) } else if err = res.Err(); err != nil { @@ -260,7 +259,7 @@ func relateNodes(gogm *Gogm, transaction neo4j.Transaction, relations map[string } // removes relationships between specified nodes -func removeRelations(gogm *Gogm, transaction neo4j.Transaction, dels map[int64][]int64) error { +func removeRelations(transaction neo4j.Transaction, dels map[int64][]int64) error { if len(dels) == 0 { return nil } @@ -291,11 +290,9 @@ func removeRelations(gogm *Gogm, transaction neo4j.Transaction, dels map[int64][ return err } - xp := map[string]interface{}{ + res, err := transaction.Run(cyq, map[string]interface{}{ "rows": params, - } - gogm.LogQuery(cyq, xp) - res, err := transaction.Run(cyq, xp) + }) if err != nil { return fmt.Errorf("%s: %w", err.Error(), ErrInternal) } else if err = res.Err(); err != nil { @@ -506,7 +503,7 @@ func generateCurRels(gogm *Gogm, parentPtr uintptr, current *reflect.Value, curr } // createNodes updates existing nodes and creates new nodes while also making a lookup table for ptr -> neoid -func createNodes(gogm *Gogm, transaction neo4j.Transaction, crNodes map[string]map[uintptr]*nodeCreate, nodeRef map[uintptr]*reflect.Value, nodeIdRef map[uintptr]int64) error { +func createNodes(transaction neo4j.Transaction, crNodes map[string]map[uintptr]*nodeCreate, nodeRef map[uintptr]*reflect.Value, nodeIdRef map[uintptr]int64) error { for label, nodes := range crNodes { var updateRows, newRows []interface{} for ptr, config := range nodes { @@ -544,11 +541,9 @@ func createNodes(gogm *Gogm, transaction neo4j.Transaction, crNodes map[string]m return fmt.Errorf("failed to build query, %w", err) } - xp := map[string]interface{}{ + res, err := transaction.Run(cyp, map[string]interface{}{ "rows": newRows, - } - gogm.LogQuery(cyp, xp) - res, err := transaction.Run(cyp, xp) + }) if err != nil { return fmt.Errorf("failed to execute new node query, %w", err) } else if res.Err() != nil { @@ -612,11 +607,9 @@ func createNodes(gogm *Gogm, transaction neo4j.Transaction, crNodes map[string]m return fmt.Errorf("failed to build query, %w", err) } - xp := map[string]interface{}{ + res, err := transaction.Run(cyp, map[string]interface{}{ "rows": updateRows, - } - gogm.LogQuery(cyp, xp) - res, err := transaction.Run(cyp, xp) + }) if err != nil { return fmt.Errorf("failed to run update query, %w", err) } else if res.Err() != nil { diff --git a/session.go b/session.go index 68bef01..5209739 100644 --- a/session.go +++ b/session.go @@ -447,7 +447,7 @@ func (s *Session) Delete(deleteObj interface{}) error { } // handle if in transaction - workFunc, err := deleteNode(s.gogm, deleteObj) + workFunc, err := deleteNode(deleteObj) if err != nil { return fmt.Errorf("failed to generate work func for delete, %w", err) } @@ -461,7 +461,7 @@ func (s *Session) DeleteUUID(uuid string) error { } // handle if in transaction - return s.runWrite(deleteByUuids(s.gogm, uuid)) + return s.runWrite(deleteByUuids(uuid)) } func (s *Session) runWrite(work neo4j.TransactionWork) error { diff --git a/sessionv2.go b/sessionv2.go index abb6252..bf0bc0c 100644 --- a/sessionv2.go +++ b/sessionv2.go @@ -42,6 +42,18 @@ type SessionV2Impl struct { lastBookmark string } +type cypherLogger func(cypher string, params map[string]interface{}) + +type transactionWithLog struct { + neo4j.Transaction + log cypherLogger +} + +type sessionWithLog struct { + neo4j.Session + log cypherLogger +} + func newSessionWithConfigV2(gogm *Gogm, conf SessionConfig) (*SessionV2Impl, error) { if gogm == nil { return nil, errors.New("gogm instance can not be nil") @@ -55,12 +67,12 @@ func newSessionWithConfigV2(gogm *Gogm, conf SessionConfig) (*SessionV2Impl, err return nil, errors.New("gogm driver not initialized") } - neoSess := gogm.driver.NewSession(neo4j.SessionConfig{ + neoSess := &sessionWithLog{gogm.driver.NewSession(neo4j.SessionConfig{ AccessMode: conf.AccessMode, Bookmarks: conf.Bookmarks, DatabaseName: conf.DatabaseName, FetchSize: neo4j.FetchDefault, - }) + }), newCypherLogger(gogm)} return &SessionV2Impl{ neoSess: neoSess, @@ -91,7 +103,7 @@ func (s *SessionV2Impl) Begin(ctx context.Context) error { if err != nil { return err } - + s.tx = &transactionWithLog{s.tx, newCypherLogger(s.gogm)} return nil } @@ -423,7 +435,6 @@ func (s *SessionV2Impl) runReadOnly(ctx context.Context, cyp string, params map[ if span != nil { span.LogKV("info", "running in existing transaction") } - s.gogm.LogQuery(cyp, params) result, err := s.tx.Run(cyp, params) if err != nil { return err @@ -436,7 +447,6 @@ func (s *SessionV2Impl) runReadOnly(ctx context.Context, cyp string, params map[ span.LogKV("info", "running in driver managed transaction") } _, err := s.neoSess.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) { - s.gogm.LogQuery(cyp, params) res, err := tx.Run(cyp, params) if err != nil { return nil, err @@ -497,7 +507,7 @@ func (s *SessionV2Impl) Delete(ctx context.Context, deleteObj interface{}) error } // handle if in transaction - workFunc, err := deleteNode(s.gogm, deleteObj) + workFunc, err := deleteNode(deleteObj) if err != nil { return fmt.Errorf("failed to generate work func for delete, %w", err) } @@ -519,7 +529,7 @@ func (s *SessionV2Impl) DeleteUUID(ctx context.Context, uuid string) error { } // handle if in transaction - return s.runWrite(ctx, deleteByUuids(s.gogm, uuid)) + return s.runWrite(ctx, deleteByUuids(uuid)) } func (s *SessionV2Impl) runWrite(ctx context.Context, work neo4j.TransactionWork) error { @@ -576,7 +586,6 @@ func (s *SessionV2Impl) Query(ctx context.Context, query string, properties map[ } return s.runWrite(ctx, func(tx neo4j.Transaction) (interface{}, error) { - s.gogm.LogQuery(query, properties) res, err := tx.Run(query, properties) if err != nil { return nil, err @@ -592,7 +601,6 @@ func (s *SessionV2Impl) QueryRaw(ctx context.Context, query string, properties m } var err error if s.tx != nil { - s.gogm.LogQuery(query, properties) res, err := s.tx.Run(query, properties) if err != nil { return nil, nil, fmt.Errorf("failed to execute query, %w", err) @@ -611,7 +619,6 @@ func (s *SessionV2Impl) QueryRaw(ctx context.Context, query string, properties m var sum neo4j.ResultSummary if s.conf.AccessMode == AccessModeRead { ires, err = s.neoSess.ReadTransaction(func(tx neo4j.Transaction) (interface{}, error) { - s.gogm.LogQuery(query, properties) res, err := tx.Run(query, properties) if err != nil { return nil, err @@ -628,7 +635,6 @@ func (s *SessionV2Impl) QueryRaw(ctx context.Context, query string, properties m }) } else { ires, err = s.neoSess.WriteTransaction(func(tx neo4j.Transaction) (interface{}, error) { - s.gogm.LogQuery(query, properties) res, err := tx.Run(query, properties) if err != nil { return nil, err @@ -796,3 +802,36 @@ func (s *SessionV2Impl) Close() error { return s.neoSess.Close() } + +func (w *transactionWithLog) Run(cypher string, params map[string]interface{}) (neo4j.Result, error) { + w.log(cypher, params) + return w.Transaction.Run(cypher, params) +} + +func (s *sessionWithLog) ReadTransaction(work neo4j.TransactionWork, configurers ...func(*neo4j.TransactionConfig)) (interface{}, error) { + return s.Session.ReadTransaction(transactionWorkWithLog(work, s.log), configurers...) +} + +func (s *sessionWithLog) WriteTransaction(work neo4j.TransactionWork, configurers ...func(*neo4j.TransactionConfig)) (interface{}, error) { + return s.Session.WriteTransaction(transactionWorkWithLog(work, s.log), configurers...) +} + +func (s *sessionWithLog) Run(cypher string, params map[string]interface{}, configurers ...func(*neo4j.TransactionConfig)) (neo4j.Result, error) { + return s.Session.Run(cypher, params, configurers...) +} + +func transactionWorkWithLog(work neo4j.TransactionWork, log cypherLogger) neo4j.TransactionWork { + return func(tx neo4j.Transaction) (interface{}, error) { + return work(&transactionWithLog{tx, log}) + } +} + +func newCypherLogger(gogm *Gogm) cypherLogger { + return func(query string, params map[string]interface{}) { + if gogm.config.EnableLogParams { + gogm.logger.Debugf("cypher - %v - {%v}", query, params) + } else { + gogm.logger.Debugf("cypher - %v", query) + } + } +} From 5262f0c3acd64724f658216a6ea71f3fec6c25e4 Mon Sep 17 00:00:00 2001 From: jun chen Date: Sat, 12 Mar 2022 11:43:58 -0800 Subject: [PATCH 4/4] cleanup --- sessionv2.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sessionv2.go b/sessionv2.go index bf0bc0c..91f15b1 100644 --- a/sessionv2.go +++ b/sessionv2.go @@ -67,12 +67,12 @@ func newSessionWithConfigV2(gogm *Gogm, conf SessionConfig) (*SessionV2Impl, err return nil, errors.New("gogm driver not initialized") } - neoSess := &sessionWithLog{gogm.driver.NewSession(neo4j.SessionConfig{ + neoSess := newNeo4jSession(gogm, neo4j.SessionConfig{ AccessMode: conf.AccessMode, Bookmarks: conf.Bookmarks, DatabaseName: conf.DatabaseName, FetchSize: neo4j.FetchDefault, - }), newCypherLogger(gogm)} + }) return &SessionV2Impl{ neoSess: neoSess, @@ -103,7 +103,7 @@ func (s *SessionV2Impl) Begin(ctx context.Context) error { if err != nil { return err } - s.tx = &transactionWithLog{s.tx, newCypherLogger(s.gogm)} + s.tx = &transactionWithLog{s.tx, getCypherLogger(s.gogm)} return nil } @@ -707,7 +707,7 @@ func (s *SessionV2Impl) reset() error { s.neoSess = nil } - s.neoSess = s.gogm.driver.NewSession(neo4j.SessionConfig{ + s.neoSess = newNeo4jSession(s.gogm, neo4j.SessionConfig{ AccessMode: s.conf.AccessMode, Bookmarks: s.conf.Bookmarks, DatabaseName: s.conf.DatabaseName, @@ -817,6 +817,7 @@ func (s *sessionWithLog) WriteTransaction(work neo4j.TransactionWork, configurer } func (s *sessionWithLog) Run(cypher string, params map[string]interface{}, configurers ...func(*neo4j.TransactionConfig)) (neo4j.Result, error) { + s.log(cypher, params) return s.Session.Run(cypher, params, configurers...) } @@ -826,7 +827,11 @@ func transactionWorkWithLog(work neo4j.TransactionWork, log cypherLogger) neo4j. } } -func newCypherLogger(gogm *Gogm) cypherLogger { +func newNeo4jSession(gogm *Gogm, config neo4j.SessionConfig) neo4j.Session { + return &sessionWithLog{gogm.driver.NewSession(config), getCypherLogger(gogm)} +} + +func getCypherLogger(gogm *Gogm) cypherLogger { return func(query string, params map[string]interface{}) { if gogm.config.EnableLogParams { gogm.logger.Debugf("cypher - %v - {%v}", query, params)