1 /*
2 # PostgreSQL Database Modeler (pgModeler)
3 #
4 # Copyright 2006-2020 - Raphael Araújo e Silva <raphael@pgmodeler.io>
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation version 3.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # The complete text of GPLv3 is at LICENSE file on source code root directory.
16 # Also, you can get the complete GNU General Public License at <http://www.gnu.org/licenses/>
17 */
18 
19 /**
20 \ingroup pgmodeler-cli
21 \class PgModelerCliApp
22 \brief Implements the operations export models whitout use the graphical interface
23 */
24 
25 #ifndef PGMODELER_CLI_APP_H
26 #define PGMODELER_CLI_APP_H
27 
28 #include "application.h"
29 #include <QObject>
30 #include <QTextStream>
31 #include <QCoreApplication>
32 #include "exception.h"
33 #include "globalattributes.h"
34 #include "modelwidget.h"
35 #include "modelexporthelper.h"
36 #include "generalconfigwidget.h"
37 #include "connectionsconfigwidget.h"
38 #include "relationshipconfigwidget.h"
39 #include "generalconfigwidget.h"
40 #include "databaseimporthelper.h"
41 #include "modelsdiffhelper.h"
42 
43 class PgModelerCliApp: public Application {
44 	private:
45 		Q_OBJECT
46 
47 		XmlParser *xmlparser;
48 
49 		//! \brief Export helper object
50 		ModelExportHelper *export_hlp;
51 
52 		//! \brief Import helper object
53 		DatabaseImportHelper *import_hlp;
54 
55 		//! \brief Diff helper object
56 		ModelsDiffHelper *diff_hlp;
57 
58 		//! \brief Reference database model
59 		DatabaseModel *model;
60 
61 		//! \brief Graphical scene used to export the model to png
62 		ObjectsScene *scene;
63 
64 		//! \brief Stores the configured connection
65 		Connection connection,
66 
67 		//! \brief Stores the extra configured connection (only for diff)
68 		extra_connection;
69 
70 		//! \brief Loaded connections
71 		map<QString, Connection *> connections;
72 
73 		//! \brief Connection configuration widget used to load available connections from file
74 		ConnectionsConfigWidget *conn_conf;
75 
76 		//! \brief Relationship configuration widget used to load custom relationship settings
77 		RelationshipConfigWidget *rel_conf;
78 
79 		GeneralConfigWidget *general_conf;
80 
81 		//! \brief Creates an standard out to handles QStrings
82 		static QTextStream out;
83 
84 		//! \brief Stores the long option names. The boolean indicates if the option accepts a value
85 		static map<QString, bool> long_opts;
86 
87 		//! \brief Stores the short option names.
88 		static attribs_map short_opts;
89 
90 		//! \brief Stores the accepted options by the different operations
91 		static map<QString, QStringList> accepted_opts;
92 
93 		//! \brief Stores the parsed options names and values.
94 		attribs_map parsed_opts;
95 
96 		//! \brief Indicates if the cli must run in silent mode
97 		bool silent_mode;
98 
99 		//! \brief Stores the xml code for the objects being fixed
100 		QStringList objs_xml,
101 
102 		//! \brief Stores the object filters for reverse engineering
103 		obj_filters;
104 
105 		//! \brief Zoom to be applied onto the png export
106 		double zoom;
107 
108 		//! \brief Start date used for filter changelog of the input database model (partial diff)
109 		QDateTime start_date,
110 
111 		//! \brief End date used for filter changelog of the input database model (partial diff)
112 		end_date;
113 
114 		static const QRegExp PasswordRegExp;
115 		static const QString PasswordPlaceholder;
116 
117 		//! \brief Option names constants
118 		static const QString Input,
119 		Output,
120 		InputDb,
121 		ExportToFile,
122 		ExportToPng,
123 		ExportToSvg,
124 		ExportToDbms,
125 		ExportToDict,
126 		ImportDb,
127 		Diff,
128 		DropDatabase,
129 		DropObjects,
130 		PgSqlVer,
131 		Help,
132 		ShowGrid,
133 		ShowDelimiters,
134 		PageByPage,
135 		IgnoreDuplicates,
136 		IgnoreErrorCodes,
137 		ConnAlias,
138 		Host,
139 		Port,
140 		User,
141 		Passwd,
142 		InitialDb,
143 		Silent,
144 		ListConns,
145 		Simulate,
146 		FixModel,
147 		FixTries,
148 		ZoomFactor,
149 		UseTmpNames,
150 		DbmMimeType,
151 		Install,
152 		Uninstall,
153 		SystemWide,
154 		NoIndex,
155 		Splitted,
156 
157 		IgnoreImportErrors,
158 		ImportSystemObjs,
159 		ImportExtensionObjs,
160 		DebugMode,
161 		FilterObjects,
162 		OnlyMatching,
163 		MatchByName,
164 		ForceChildren,
165 		AllChildren,
166 
167 		PartialDiff,
168 		ForceDiff,
169 		StartDate,
170 		EndDate,
171 		CompareTo,
172 		SaveDiff,
173 		ApplyDiff,
174 		NoDiffPreview,
175 		DropClusterObjs,
176 		RevokePermissions,
177 		DropMissingObjs,
178 		ForceDropColsConstrs,
179 		RenameDb,
180 		TruncOnColsTypeChange,
181 		NoSequenceReuse,
182 		NoCascadeDropTrunc,
183 		ForceRecreateObjs,
184 		OnlyUnmodifiable,
185 
186 		CreateConfigs,
187 
188 		TagExpr,
189 		EndTagExpr,
190 		AttributeExpr,
191 
192 		MsgFileAssociated,
193 		MsgNoFileAssociation;
194 
195 		//! \brief Parsers the options and executes the action specified by them
196 		void parseOptions(attribs_map &parsed_opts);
197 
198 		//! \brief Shows the options menu
199 		void showMenu();
200 
201 		//! \brief Returns if the specified options exists on short options map
202 		bool isOptionRecognized(QString &op, bool &accepts_val);
203 
204 		/*! \brief Extracts the xml defintions from the input model and store them on obj_xml list
205 		in order to be parsed by the recreateObjects() method */
206 		void extractObjectXML();
207 
208 		//! \brief Recreates the objects from the obj_xml list fixing the creation order for them
209 		void recreateObjects();
210 
211 		//! \brief Fix some xml attributes and remove unused tags
212 		void fixObjectAttributes(QString &obj_xml);
213 
214 		/*! \brief Extracts the foreign key code for the specified table xml. The foreign keys
215 		are recreated after all the other objects */
216 		QStringList extractForeignKeys(QString &obj_xml);
217 
218 		//! \brief Returns if the specified string contains some of relationship attributes
219 		bool containsRelAttributes(const QString &str);
220 
221 		/*! \brief Install the .dbm file association in the mime database (default behaviour).
222 		The paramenter 'uninstall' is used to clean up any file association done previously. */
223 		void handleMimeDatabase(bool uninstall, bool system_wide);
224 
225 		/*! \brief Fixes the references to opertor classes and families by replacing tags like
226 		<opclass name="name"/> by <opclass signature="name USING index_method"/>. This method operates
227 		only over operator classes, indexes and constraints */
228 		void fixOpClassesFamiliesReferences(QString &obj_xml);
229 
230 		void fixModel();
231 		void exportModel();
232 		void importDatabase();
233 		void diffModelDatabase();
234 		void updateMimeType();
235 		void configureConnection(bool extra_conn);
236 		void importDatabase(DatabaseModel *model, Connection conn);
237 		void printMessage(const QString &msg);
238 		void handleLinuxMimeDatabase(bool uninstall, bool system_wide);
239 		void handleWindowsMimeDatabase(bool uninstall, bool system_wide);
240 		void createConfigurations();
241 		void listConnections();
242 
243 	public:
244 		PgModelerCliApp(int argc, char **argv);
245 		virtual ~PgModelerCliApp();
246 		int exec();
247 
248 	private slots:
249 		void handleObjectAddition(BaseObject *);
250 		void updateProgress(int progress, QString msg, ObjectType = ObjectType::BaseObject);
251 		void printIgnoredError(QString err_cod, QString err_msg, QString cmd);
252 		void handleObjectRemoval(BaseObject *object);
253 };
254 
255 #endif
256