1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // ddTableFigure.h - Draw table figure of a model
9 //
10 ////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef DDTABLEFIGURE_H
13 #define DDTABLEFIGURE_H
14 #include "hotdraw/figures/hdCompositeFigure.h"
15 #include "hotdraw/figures/hdRectangleFigure.h"
16 #include "hotdraw/figures/hdSimpleTextFigure.h"
17 #include "dd/dditems/figures/ddTextTableItemFigure.h"
18 #include "dd/dditems/figures/ddColumnFigure.h"
19 #include "dd/ddmodel/ddDatabaseDesign.h"
20 
21 class ddScrollBarHandle;
22 class ddRelationshipFigure;
23 class hdDrawing;
24 
25 class ddTableFigure : public hdCompositeFigure
26 {
27 public:
28 	ddTableFigure(wxString tableName, int x, int y);
29 	ddTableFigure(wxString tableName, int posIdx, int x, int y);
30 	void InitTableValues(wxArrayString UniqueKeysName, wxString primaryKeyName, int bdc, int bdi, int maxcolsi, int minidxsi, int maxidxsi, int colsrs, int colsw, int idxsrs, int idxsw);
31 	void Init(wxString tableName, int x, int y);
32 	hdMultiPosRect &getBasicDisplayBox();
33 	~ddTableFigure();
34 
35 	//Diagrams related functions
36 	virtual void AddPosForNewDiagram();
37 	virtual void RemovePosOfDiagram(int posIdx);
38 
39 	//add remove items
40 	ddColumnFigure *getColByName(wxString name);
41 	void addColumn(int posIdx, ddColumnFigure *column);
42 	void addColumnFromStorage(ddColumnFigure *column);
43 	void syncInternalsPosAt(int posIdx, int x, int y);
44 	void syncInternalsPosAt(wxArrayInt &x, wxArrayInt &y);
45 	void removeColumn(int posIdx, ddColumnFigure *column);
46 
47 	//movement
manuallyNotifyChange(int posIdx)48 	void manuallyNotifyChange(int posIdx)
49 	{
50 		changed(posIdx);
51 	};
52 	virtual void basicMoveBy(int posIdx, int x, int y);
53 
54 	//show messages to set fk destination
55 	void setSelectFkDestMode(bool value);
56 
57 	//delete hack tables
58 	void processDeleteAlert(hdDrawing *drawing);
59 
60 	//columns scrolls
61 	void updateTableSize(bool notifyChange = true);
62 	void recalculateColsPos(int posIdx);
63 	void setColsRowsWindow(int num);
64 	hdMultiPosRect &getColsSpace();
65 	hdMultiPosRect &getFullSpace();
66 	hdMultiPosRect &getTitleRect();
67 	int getTotalColumns();
68 	int getColumnsWindow();
69 	int getTopColWindowIndex();
70 	void setColumnsWindow(int posIdx, int value, bool maximize = false);
71 	void columnsWindowUp(int posIdx);
72 	void columnsWindowDown(int posIdx);
73 	int getFiguresMaxWidth();
74 	int getColDefaultHeight(wxFont font);
75 
76 	//metadata
77 	wxString getTableName();
78 	wxString getShortTableName();
79 	wxString generateSQLCreate(wxString schemaName);
80 	wxString generateSQLAlterPks(wxString schemaName);
81 	wxString generateSQLAlterFks(wxString schemaName);
82 	wxString generateSQLAlterUks(wxString schemaName);
83 	wxString generateAltersTable(pgConn *connection, wxString schemaName, ddDatabaseDesign *design);
84 	wxArrayString getAllColumnsNames();
85 	wxArrayString getAllFkSourceColsNames(bool pk, int ukIndex = -1);
86 	ddColumnFigure *getColumnByName(wxString name);
87 	bool validateTable(wxString &errors);
88 
89 	//uk pk constraints
90 	void setPkConstraintName(wxString name);
91 	wxString getPkConstraintName();
92 	wxArrayString &getUkConstraintsNames();
93 	bool disablePrimaryKey();
94 	bool enablePrimaryKey();
95 
96 	//fk related
97 	void updateFkObservers();
98 	void updateSizeOfObservers();
99 	void prepareForDeleteFkColumn(ddColumnFigure *column);
100 
101 	//ScrollBar persistence related
getBeginDrawCols()102 	int getBeginDrawCols()
103 	{
104 		return beginDrawCols;
105 	};
getBeginDrawIdxs()106 	int getBeginDrawIdxs()
107 	{
108 		return beginDrawIdxs;
109 	};
getMaxColIndex()110 	int getMaxColIndex()
111 	{
112 		return maxColIndex;
113 	};
getMinIdxIndex()114 	int getMinIdxIndex()
115 	{
116 		return minIdxIndex;
117 	};
getMaxIdxIndex()118 	int getMaxIdxIndex()
119 	{
120 		return maxIdxIndex;
121 	};
getColsRowsSize()122 	int getColsRowsSize()
123 	{
124 		return colsRowsSize;
125 	};
getColsWindow()126 	int getColsWindow()
127 	{
128 		return colsWindow;
129 	};
getIdxsRowsSize()130 	int getIdxsRowsSize()
131 	{
132 		return idxsRowsSize;
133 	};
getIdxsWindow()134 	int getIdxsWindow()
135 	{
136 		return idxsWindow;
137 	};
138 
139 	//Temporary alter table instead of create Helper
getBelongsToSchema()140 	bool getBelongsToSchema()
141 	{
142 		return belongsToSchema;
143 	};
setBelongsToSchema(bool value)144 	void setBelongsToSchema(bool value)
145 	{
146 		belongsToSchema = value;
147 	};
148 
149 protected:
150 	//drawing
151 	virtual void basicDraw(wxBufferedDC &context, hdDrawingView *view);
152 	virtual void basicDrawSelected(wxBufferedDC &context, hdDrawingView *view);
153 
154 private:
155 	//Temporary alter table instead of create Helper
156 	bool belongsToSchema;
157 
158 	//Main Rectangle Sizes
159 	hdMultiPosRect fullSizeRect, titleRect, titleColsRect, colsRect, titleIndxsRect, indxsRect;
160 	hdMultiPosRect unScrolledColsRect, unScrolledFullSizeRect, unScrolledTitleRect;
161 
162 	//Rectangle item counters
163 	int colsRowsSize, colsWindow, idxsRowsSize, idxsWindow;
164 
165 	//vector indexes
166 	int maxColIndex, minIdxIndex, maxIdxIndex;
167 
168 	//position
169 	int beginDrawCols, beginDrawIdxs;
170 
171 	//Default Figures
172 	hdRectangleFigure *rectangleFigure;
173 	ddTextTableItemFigure *tableTitle;
174 
175 	//helper variables
176 	bool selectingFkDestination;
177 	int internalPadding, externalPadding;
178 	bool calcScrolled;
179 
180 	//specials handles
181 	ddScrollBarHandle *scrollbar;
182 
183 	//methods
184 	int getHeightFontMetric(wxString text, wxFont font);
185 	void calcInternalSubAreas(int posIdx);
186 
187 	//pk uk(s)
188 	wxString pkName;
189 	wxArrayString ukNames;
190 };
191 #endif
192