1818import org .pgcodekeeper .cli .exception .LibraryObjectDuplicationException ;
1919import org .pgcodekeeper .cli .localizations .Messages ;
2020import org .pgcodekeeper .core .PgCodekeeperException ;
21- import org .pgcodekeeper .core .PgDiff ;
2221import org .pgcodekeeper .core .api .DatabaseFactory ;
2322import org .pgcodekeeper .core .api .PgCodeKeeperApi ;
2423import org .pgcodekeeper .core .loader .FullAnalyze ;
2726import org .pgcodekeeper .core .schema .AbstractDatabase ;
2827import org .pgcodekeeper .core .schema .PgOverride ;
2928import org .pgcodekeeper .core .xmlstore .DependenciesXmlStore ;
29+ import org .slf4j .Logger ;
30+ import org .slf4j .LoggerFactory ;
3031
3132import java .io .IOException ;
3233import java .nio .file .Paths ;
34+ import java .util .ArrayList ;
3335import java .util .Collection ;
3436import java .util .List ;
3537
36- public final class PgDiffCli extends PgDiff {
37-
38+ public final class PgDiffCli {
39+ private static final Logger LOG = LoggerFactory .getLogger (PgDiffCli .class );
40+ private final List <Object > errors = new ArrayList <>();
3841 private final CliArgs arguments ;
3942
4043 public PgDiffCli (CliArgs arguments ) {
41- super (arguments );
4244 this .arguments = arguments ;
4345 }
4446
@@ -47,25 +49,25 @@ public void updateProject()
4749 AbstractDatabase oldDatabase = loadOldDatabaseWithLibraries ();
4850 AbstractDatabase newDatabase = loadNewDatabaseWithLibraries ();
4951
50- PgCodeKeeperApi .update (settings , oldDatabase , newDatabase ,
52+ PgCodeKeeperApi .update (arguments , oldDatabase , newDatabase ,
5153 arguments .getOutputTarget (), arguments .getIgnoreLists (), null );
5254 }
5355
5456 public void exportProject () throws IOException , InterruptedException , PgCodekeeperException {
55- AbstractDatabase newDb = loadNewDatabase ( );
56- PgCodeKeeperApi .export (settings , newDb , arguments .getOutputTarget (), arguments .getIgnoreLists (), null );
57+ AbstractDatabase newDb = loadDatabaseSchema ( arguments . getNewSrc () );
58+ PgCodeKeeperApi .export (arguments , newDb , arguments .getOutputTarget (), arguments .getIgnoreLists (), null );
5759 }
5860
5961 public String createDiff () throws InterruptedException , IOException , PgCodekeeperException {
6062 AbstractDatabase oldDatabase = loadOldDatabaseWithLibraries ();
6163 AbstractDatabase newDatabase = loadNewDatabaseWithLibraries ();
62- return PgCodeKeeperApi .diff (settings , oldDatabase , newDatabase , arguments .getIgnoreLists ());
64+ return PgCodeKeeperApi .diff (arguments , oldDatabase , newDatabase , arguments .getIgnoreLists ());
6365 }
6466
6567 public AbstractDatabase loadNewDatabaseWithLibraries ()
6668 throws IOException , InterruptedException , PgCodekeeperException {
6769 LOG .info (Messages .PgDiffCli_log_load_new_db );
68- AbstractDatabase newDatabase = loadNewDatabase ( );
70+ AbstractDatabase newDatabase = loadDatabaseSchema ( arguments . getNewSrc () );
6971 LOG .info (Messages .PgDiffCli_log_load_new_db_lib );
7072 loadLibraries (newDatabase , arguments .getTargetLibXmls (), arguments .getTargetLibs (),
7173 arguments .getTargetLibsWithoutPriv ());
@@ -78,7 +80,7 @@ public AbstractDatabase loadNewDatabaseWithLibraries()
7880
7981 // read additional privileges from special folder
8082 LOG .info (Messages .PgDiffCli_log_load_new_db_overrides );
81- loadOverrides (newDatabase , arguments .getNewSrcFormat (), arguments . getNewSrc ());
83+ loadOverrides (newDatabase , arguments .getNewSrc ());
8284
8385 LOG .info (Messages .PgDiffCli_log_start_db_analyze );
8486 analyzeDatabase (newDatabase );
@@ -89,7 +91,7 @@ public AbstractDatabase loadNewDatabaseWithLibraries()
8991 public AbstractDatabase loadOldDatabaseWithLibraries ()
9092 throws IOException , InterruptedException , PgCodekeeperException {
9193 LOG .info (Messages .PgDiffCli_log_load_old_db );
92- AbstractDatabase oldDatabase = loadOldDatabase ( );
94+ AbstractDatabase oldDatabase = loadDatabaseSchema ( arguments . getOldSrc () );
9395
9496 LOG .info (Messages .PgDiffCli_log_load_old_db_lib );
9597 loadLibraries (oldDatabase , arguments .getSourceLibXmls (), arguments .getSourceLibs (),
@@ -102,7 +104,7 @@ public AbstractDatabase loadOldDatabaseWithLibraries()
102104 }
103105 LOG .info (Messages .PgDiffCli_log_load_old_db_overrides );
104106 // read additional privileges from special folder
105- loadOverrides (oldDatabase , arguments .getOldSrcFormat (), arguments . getOldSrc ());
107+ loadOverrides (oldDatabase , arguments .getOldSrc ());
106108
107109 LOG .info (Messages .PgDiffCli_log_start_db_analyze );
108110 analyzeDatabase (oldDatabase );
@@ -116,53 +118,41 @@ private void analyzeDatabase(AbstractDatabase db)
116118 assertErrors ();
117119 }
118120
119- private void loadOverrides (AbstractDatabase db , SourceFormat format , String source )
121+ private void loadOverrides (AbstractDatabase db , String source )
120122 throws InterruptedException , IOException , PgCodekeeperException {
121- if (SourceFormat .PARSED != format ) {
123+ if (SourceFormat .PARSED != SourceFormat . parsePath ( source ) ) {
122124 return ;
123125 }
124126
125- new ProjectLoader (source , settings , errors ).loadOverrides (db );
127+ new ProjectLoader (source , arguments , errors ).loadOverrides (db );
126128 assertErrors ();
127129 }
128130
129131 private void loadLibraries (AbstractDatabase db , Collection <String > libXmls , Collection <String > libs ,
130- Collection <String > libsWithoutPriv )
132+ Collection <String > libsWithoutPrivileges )
131133 throws InterruptedException , IOException , PgCodekeeperException {
132134 LibraryLoader ll = new LibraryLoader (db , Utils .getMetaPath (), errors );
133135
134136 for (String xml : libXmls ) {
135- ll .loadXml (new DependenciesXmlStore (Paths .get (xml )), settings );
137+ ll .loadXml (new DependenciesXmlStore (Paths .get (xml )), arguments );
136138 }
137139
138- ll .loadLibraries (settings , false , libs );
139- ll .loadLibraries (settings , true , libsWithoutPriv );
140+ ll .loadLibraries (arguments , false , libs );
141+ ll .loadLibraries (arguments , true , libsWithoutPrivileges );
140142 assertErrors ();
141143 }
142144
143- private AbstractDatabase loadNewDatabase ()
144- throws IOException , InterruptedException , PgCodekeeperException {
145- return loadDatabaseSchema (arguments .getNewSrcFormat (), arguments .getNewSrc ());
146- }
147-
148- private AbstractDatabase loadOldDatabase ()
149- throws IOException , InterruptedException , PgCodekeeperException {
150- return loadDatabaseSchema (arguments .getOldSrcFormat (), arguments .getOldSrc ());
151- }
152-
153145 /**
154146 * Loads database schema choosing the provided method.
155147 *
156- * @param format format of the database source, must be "dump", "parsed" or
157- * "db" otherwise exception is thrown
158148 * @param srcPath path to the database source to load
159149 *
160150 * @return the loaded database
161151 */
162- private AbstractDatabase loadDatabaseSchema (SourceFormat format , String srcPath )
152+ private AbstractDatabase loadDatabaseSchema (String srcPath )
163153 throws InterruptedException , IOException , PgCodekeeperException {
164- var factory = new DatabaseFactory (settings , arguments .isIgnoreErrors (), false );
165- return switch (format ) {
154+ var factory = new DatabaseFactory (arguments , arguments .isIgnoreErrors (), false );
155+ return switch (SourceFormat . parsePath ( srcPath ) ) {
166156 case DB -> factory .loadFromJdbc (srcPath , arguments .getIgnoreSchemaList ());
167157 case DUMP -> factory .loadFromDump (srcPath );
168158 case PARSED -> factory .loadFromProject (srcPath , arguments .getIgnoreSchemaList ());
@@ -174,4 +164,8 @@ private void assertErrors() throws PgCodekeeperException {
174164 throw new PgCodekeeperException (Messages .PgDiffCli_error_while_load_database );
175165 }
176166 }
167+
168+ public List <Object > getErrors () {
169+ return errors ;
170+ }
177171}
0 commit comments