1+ /* eslint-disable */
12import { addHiddenProperty , hasProp , transformToJSON } from './util' ;
23import analyzeSQL , { abstractSqlAstJSON } from './sql/analyze' ;
34import normalizeSQL from './sql/normalize' ;
@@ -362,14 +363,14 @@ export default class Event {
362363
363364 get identityHash ( ) {
364365 if ( ! this . $hidden . identityHash ) {
365- this . $hidden . identityHash = this . buildIdentityHash ( this ) . digest ( ) ;
366+ this . $hidden . identityHash = this . buildIdentityHash ( ) . digest ( ) ;
366367 }
367368 return this . $hidden . identityHash ;
368369 }
369370
370371 get hash ( ) {
371372 if ( ! this . $hidden . hash ) {
372- this . $hidden . hash = this . buildStableHash ( this ) . digest ( ) ;
373+ this . $hidden . hash = this . buildStableHash ( ) . digest ( ) ;
373374 }
374375 return this . $hidden . hash ;
375376 }
@@ -381,6 +382,13 @@ export default class Event {
381382 return this . $hidden . stableProperties ;
382383 }
383384
385+ getHashWithSqlCache ( parsedSqlCache ) {
386+ if ( ! this . $hidden . hash ) {
387+ this . $hidden . hash = this . buildStableHash ( parsedSqlCache ) . digest ( ) ;
388+ }
389+ return this . $hidden . hash ;
390+ }
391+
384392 callStack ( ) {
385393 const stack = this . ancestors ( ) . reverse ( ) ;
386394 stack . push ( this . callEvent ) ;
@@ -501,7 +509,7 @@ export default class Event {
501509
502510 // Collects properties of an event which are not dependent on the specifics
503511 // of invocation.
504- gatherStableProperties ( ) {
512+ gatherStableProperties ( parsedSqlCache ) {
505513 const { sqlQuery } = this ;
506514
507515 // Convert null and undefined values to empty strings
@@ -526,10 +534,17 @@ export default class Event {
526534
527535 let properties ;
528536 if ( sqlQuery ) {
529- const sqlNormalized = abstractSqlAstJSON ( sqlQuery , this . sql . database_type )
530- // Collapse repeated variable literals and parameter tokens (e.g. '?, ?' in an IN clause)
531- . split ( / { " t y p e " : " v a r i a b l e " } (?: , { " t y p e " : " v a r i a b l e " } ) * / g)
532- . join ( `{"type":"variable"}` ) ;
537+ let sqlNormalized ;
538+ const cacheKey = `${ this . sql . database_type } :${ sqlQuery } ` ;
539+ if ( parsedSqlCache ) sqlNormalized = parsedSqlCache . get ( cacheKey ) ;
540+ if ( ! sqlNormalized ) {
541+ sqlNormalized = abstractSqlAstJSON ( sqlQuery , this . sql . database_type )
542+ // Collapse repeated variable literals and parameter tokens (e.g. '?, ?' in an IN clause)
543+ . split ( / { " t y p e " : " v a r i a b l e " } (?: , { " t y p e " : " v a r i a b l e " } ) * / g)
544+ . join ( `{"type":"variable"}` ) ;
545+ parsedSqlCache . set ( cacheKey , sqlNormalized ) ;
546+ }
547+
533548 properties = {
534549 event_type : 'sql' ,
535550 sql_normalized : sqlNormalized ,
@@ -554,7 +569,10 @@ export default class Event {
554569 return HashBuilder . buildHash ( 'event-identity-v2' , this . gatherIdentityProperties ( ) ) ;
555570 }
556571
557- buildStableHash ( ) {
558- return HashBuilder . buildHash ( 'event-stable-properties-v2' , this . gatherStableProperties ( ) ) ;
572+ buildStableHash ( parsedSqlCache ) {
573+ return HashBuilder . buildHash (
574+ 'event-stable-properties-v2' ,
575+ this . gatherStableProperties ( parsedSqlCache )
576+ ) ;
559577 }
560578}
0 commit comments