@@ -361,6 +361,23 @@ public function getLastSyncTime(): ?string
361361 return strtotime ($ result ->fetch (PDO ::FETCH_ASSOC )['start_time ' ]);
362362 }
363363
364+ /**
365+ *
366+ */
367+ public function getMapping ($ name )
368+ {
369+ return $ this ->mappings [$ name ] ?? NULL ;
370+ }
371+
372+
373+ /**
374+ *
375+ */
376+ public function getMappings (): array
377+ {
378+ return $ this ->mappings ;
379+ }
380+
364381
365382 /**
366383 *
@@ -441,7 +458,7 @@ public function schedule(array $mappings = array(), $scheduled_by = NULL): array
441458 /**
442459 *
443460 */
444- public function run (array $ mappings = array (), $ force_update = FALSE ): array
461+ public function run (array $ mappings = array (), $ ids = array (), $ force_update = FALSE ): array
445462 {
446463 $ this ->stat ();
447464
@@ -479,7 +496,7 @@ public function run(array $mappings = array(), $force_update = FALSE): array
479496
480497 foreach ($ mappings as $ mapping ) {
481498 try {
482- $ this ->syncMapping ($ mapping , $ force_update );
499+ $ this ->syncMapping ($ mapping , $ ids [ $ mapping ] ?? [], $ force_update );
483500
484501 } catch (\Exception $ e ) {
485502 $ this ->log ($ e ->getMessage ());
@@ -848,7 +865,7 @@ protected function log($message)
848865 /**
849866 *
850867 */
851- protected function syncMapping ($ name , $ force_update )
868+ protected function syncMapping ($ name , $ ids , $ force_update )
852869 {
853870 if (!isset ($ this ->mappings [$ name ])) {
854871 throw new RuntimeException (sprintf (
@@ -875,7 +892,7 @@ protected function syncMapping($name, $force_update)
875892 //
876893
877894 foreach ($ mapping ->getDependencies () as $ dependency ) {
878- $ this ->syncMapping ($ dependency , $ force_update );
895+ $ this ->syncMapping ($ dependency , [], $ force_update );
879896 }
880897
881898 if ($ this ->strictTime ) {
@@ -889,17 +906,17 @@ protected function syncMapping($name, $force_update)
889906 $ this ->log (sprintf ('Syncing %s ' , $ name ));
890907
891908 $ this ->createTemporaryTable ($ mapping );
892- $ this ->syncMappingTemporary ($ mapping );
909+ $ this ->syncMappingTemporary ($ mapping, $ ids );
893910
894911 $ start_sync_time = date ('Y-m-d H:i:s ' );
895912
896- if ($ this ->truncate [$ mapping ->getDestination ()]) {
913+ if ($ this ->truncate [$ mapping ->getDestination ()] && empty ( $ ids ) ) {
897914 $ this ->truncateTable ($ mapping );
898915 $ this ->syncMappingInserts ($ mapping );
899916
900917 } else {
901918 if ($ mapping ->canDelete ()) {
902- $ this ->syncMappingDeletes ($ mapping );
919+ $ this ->syncMappingDeletes ($ mapping, $ ids );
903920 $ this ->log ('...completed deletions ' );
904921 }
905922
@@ -920,20 +937,32 @@ protected function syncMapping($name, $force_update)
920937 $ this ->updateSet ($ name , $ start_sync_time );
921938
922939 $ this ->synced [array_pop ($ this ->stack )] = TRUE ;
940+
941+ if ($ ids ) {
942+ foreach ($ mapping ->getAdjuncts () as $ adjunct => $ config ) {
943+ $ adjunct = $ this ->mappings [$ adjunct ];
944+ $ key_query = $ mapping ->composeSourceAdjunctKeyQuery ($ adjunct , $ ids );
945+ $ keys = $ this ->source ->query ($ key_query )->fetchAll ();
946+
947+ $ keys = $ this ->filterKeys ($ adjunct , $ keys , 'select ' );
948+
949+ $ this ->syncMapping ($ adjunct ->getDestination (), $ keys , $ force_update );
950+ }
951+ }
923952 }
924953
925954
926955 /**
927956 *
928957 */
929- protected function syncMappingDeletes (Mapping $ mapping )
958+ protected function syncMappingDeletes (Mapping $ mapping, $ ids = array () )
930959 {
931960 if (!$ mapping ->canDelete ()) {
932961 return ;
933962 }
934963
935964 try {
936- $ delete_select_query = $ mapping ->composeSourceDeleteSelectQuery ();
965+ $ delete_select_query = $ mapping ->composeSourceDeleteSelectQuery ($ ids );
937966 $ delete_results = $ this ->destination ->query ($ delete_select_query )->fetchAll ();
938967 } catch (\Exception $ e ) {
939968 $ this ->log (sprintf (
@@ -1044,9 +1073,9 @@ protected function syncMappingInserts(Mapping $mapping)
10441073 /**
10451074 *
10461075 */
1047- protected function syncMappingTemporary (Mapping $ mapping )
1076+ protected function syncMappingTemporary (Mapping $ mapping, $ ids = array () )
10481077 {
1049- $ source_select_query = $ mapping ->composeSourceSelectQuery ();
1078+ $ source_select_query = $ mapping ->composeSourceSelectQuery ($ ids );
10501079 try {
10511080 $ source_results = $ this ->source ->query ($ source_select_query , PDO ::FETCH_ASSOC )->fetchAll ();
10521081 $ generated = array_keys ($ mapping ->getGenerators ());
0 commit comments