forked from pgadmin-org/pgadmin4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
814 lines (726 loc) · 24.4 KB
/
__init__.py
File metadata and controls
814 lines (726 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
##########################################################################
#
# pgAdmin 4 - PostgreSQL Tools
#
# Copyright (C) 2013 - 2026, The pgAdmin Development Team
# This software is released under the PostgreSQL Licence
#
##########################################################################
"""A blueprint module implementing the erd tool."""
import json
from flask import request, Response, session
from flask import render_template, current_app as app
from flask_security import permissions_required
from pgadmin.user_login_check import pga_login_required
from flask_babel import gettext
from werkzeug.user_agent import UserAgent
from pgadmin.utils import PgAdminModule, \
SHORTCUT_FIELDS as shortcut_fields
from pgadmin.utils.ajax import make_json_response, internal_server_error
from pgadmin.model import Server
from config import PG_DEFAULT_DRIVER, ALLOW_SAVE_PASSWORD
from pgadmin.utils.driver import get_driver
from pgadmin.browser.utils import underscore_unescape
from pgadmin.browser.server_groups.servers.databases.schemas.utils \
import get_schemas
from pgadmin.browser.server_groups.servers.databases.schemas.tables. \
constraints.foreign_key import utils as fkey_utils
from pgadmin.utils.constants import PREF_LABEL_KEYBOARD_SHORTCUTS, \
PREF_LABEL_OPTIONS
from .utils import ERDHelper
from pgadmin.utils.exception import ConnectionLost
from pgadmin.authenticate import socket_login_required
from pgadmin.tools.user_management.PgAdminPermissions import AllPermissionTypes
from ... import socketio
MODULE_NAME = 'erd'
SOCKETIO_NAMESPACE = '/{0}'.format(MODULE_NAME)
class ERDModule(PgAdminModule):
"""
class ERDModule(PgAdminModule)
A module class for ERD derived from PgAdminModule.
"""
LABEL = gettext("ERD tool")
def get_own_menuitems(self):
return {}
def get_exposed_url_endpoints(self):
"""
Returns:
list: URL endpoints
"""
return [
'erd.panel',
'erd.initialize',
'erd.prequisite',
'erd.sql',
'erd.close'
]
def register_preferences(self):
self.preference.register(
'keyboard_shortcuts',
'open_project',
gettext('Open project'),
'keyboardshortcut',
{
'alt': False,
'shift': False,
'control': True,
'ctrl_is_meta': True,
'key': {
'key_code': 79,
'char': 'o'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'save_project',
gettext('Save project'),
'keyboardshortcut',
{
'alt': False,
'shift': False,
'control': True,
'ctrl_is_meta': True,
'key': {
'key_code': 83,
'char': 's'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'save_project_as',
gettext('Save project as'),
'keyboardshortcut',
{
'alt': False,
'shift': True,
'control': True,
'key': {
'key_code': 83,
'char': 's'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'generate_sql',
gettext('Generate SQL'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 83,
'char': 's'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'download_image',
gettext('Download image'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 73,
'char': 'i'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'search_table',
gettext('Search table'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 70,
'char': 'f'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'add_table',
gettext('Add table'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 65,
'char': 'a'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'edit_table',
gettext('Edit table'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 69,
'char': 'e'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'clone_table',
gettext('Clone table'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 67,
'char': 'c'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'drop_table',
gettext('Drop table'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 68,
'char': 'd'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'add_edit_note',
gettext('Add/Edit note'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 78,
'char': 'n'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'one_to_one',
gettext('One to one link'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 66,
'char': 'b'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'one_to_many',
gettext('One to many link'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 79,
'char': 'o'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'many_to_many',
gettext('Many to many link'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 77,
'char': 'm'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'auto_align',
gettext('Auto align'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 76,
'char': 'l'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'show_details',
gettext('Show more/fewer details'),
'keyboardshortcut',
{
'alt': True,
'shift': False,
'control': True,
'key': {
'key_code': 84,
'char': 't'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'zoom_to_fit',
gettext('Zoom to fit'),
'keyboardshortcut',
{
'alt': True,
'shift': True,
'control': False,
'key': {
'key_code': 70,
'char': 'f'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'zoom_in',
gettext('Zoom in'),
'keyboardshortcut',
{
'alt': True,
'shift': True,
'control': False,
'key': {
'key_code': 187,
'char': '+'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'keyboard_shortcuts',
'zoom_out',
gettext('Zoom out'),
'keyboardshortcut',
{
'alt': True,
'shift': True,
'control': False,
'key': {
'key_code': 189,
'char': '-'
}
},
category_label=PREF_LABEL_KEYBOARD_SHORTCUTS,
fields=shortcut_fields
)
self.preference.register(
'options',
'sql_with_drop',
gettext('SQL With DROP Table'),
'boolean',
False,
category_label=PREF_LABEL_OPTIONS,
help_str=gettext(
'If enabled, the SQL generated by the ERD Tool will add '
'DROP table DDL before each CREATE table DDL.'
)
)
self.preference.register(
'options',
'table_relation_depth',
gettext('Table Relation Depth'),
'integer',
-1,
category_label=PREF_LABEL_OPTIONS,
help_str=gettext(
'The maximum depth pgAdmin should traverse to find '
'related tables when generating an ERD for a table. '
'Use -1 for no limit.'
)
)
self.preference.register(
'options',
'insert_table_with_relations',
gettext('Insert Table With Relations'),
'boolean',
False,
category_label=PREF_LABEL_OPTIONS,
help_str=gettext(
'Whether inserting a table via drag and drop should '
'also insert its relations to the existing tables in '
'the diagram.'
)
)
self.preference.register(
'options', 'cardinality_notation',
gettext('Cardinality Notation'), 'radioModern', 'crows',
category_label=PREF_LABEL_OPTIONS, options=[
{'label': gettext('Crow\'s foot'), 'value': 'crows'},
{'label': gettext('Chen'), 'value': 'chen'},
],
help_str=gettext(
'Notation to be used to present cardinality.'
)
)
self.preference.register(
'options', 'image_pixel_ratio',
gettext('Image Download Resolution'), 'radioModern', '1',
category_label=PREF_LABEL_OPTIONS, options=[
{'label': gettext('Good'), 'value': '1'},
{'label': gettext('High'), 'value': '3'},
{'label': gettext('Very High'), 'value': '5'},
],
help_str=gettext(
'Higher values will use higher memory and slower rendering.'
),
)
self.preference.register(
'options',
'sql_with_drop',
gettext('SQL With DROP Table'),
'boolean',
False,
category_label=PREF_LABEL_OPTIONS,
help_str=gettext(
'If enabled, the SQL generated by the ERD Tool will add '
'DROP table DDL before each CREATE table DDL.'
)
)
self.preference.register(
'options',
'format_pgerd',
gettext('Format ERD Project File?'),
'boolean',
False,
category_label=PREF_LABEL_OPTIONS,
help_str=gettext(
'If enabled, the .pgerd project file of the ERD tool will '
'be formatted before saving.'
)
)
blueprint = ERDModule(MODULE_NAME, __name__, static_url_path='/static')
@blueprint.route(
'/panel/<int:trans_id>',
methods=["POST"],
endpoint='panel'
)
@permissions_required(AllPermissionTypes.tools_erd_tool)
@pga_login_required
def panel(trans_id):
"""
This method calls index.html to render the erd tool.
Args:
panel_title: Title of the panel
"""
params = {'trans_id': trans_id, }
if request.form:
for key, val in request.form.items():
params[key] = val
if request.args:
params.update({k: v for k, v in request.args.items()})
if 'gen' in params:
params['gen'] = True if params['gen'] == 'true' else False
# We need client OS information to render correct Keyboard shortcuts
user_agent = UserAgent(request.headers.get('User-Agent'))
"""
Animations and transitions are not automatically GPU accelerated and by
default use browser's slow rendering engine. We need to set 'translate3d'
value of '-webkit-transform' property in order to use GPU. After applying
this property under linux, Webkit calculates wrong position of the
elements so panel contents are not visible. To make it work, we need to
explicitly set '-webkit-transform' property to 'none' for .ajs-notifier,
.ajs-message, .ajs-modal classes.
This issue is only with linux runtime application and observed in Query
tool and debugger. When we open 'Open File' dialog then whole Query tool
panel content is not visible though it contains HTML element in back end.
The port number should have already been set by the runtime if we're
running in desktop mode.
"""
is_linux_platform = False
from sys import platform as _platform
if "linux" in _platform:
is_linux_platform = True
s = Server.query.filter_by(id=int(params['sid'])).first()
if s:
params.update({
'bgcolor': s.bgcolor,
'fgcolor': s.fgcolor,
'client_platform': user_agent.platform,
'is_desktop_mode': app.PGADMIN_RUNTIME,
'is_linux': is_linux_platform
})
return render_template(
"erd/index.html",
connectionTitle=underscore_unescape(params['connectionTitle']),
params=json.dumps(params),
)
else:
params['error'] = 'Server did not find.'
return render_template(
"erd/index.html",
title=None,
params=json.dumps(params))
@blueprint.route(
'/initialize/<int:trans_id>/<int:sgid>/<int:sid>/<int:did>',
methods=["POST"], endpoint='initialize'
)
@pga_login_required
def initialize_erd(trans_id, sgid, sid, did):
"""
This method is responsible for instantiating and initializing
the erd tool object. It will also create a unique
transaction id and store the information into session variable.
Args:
sgid: Server group Id
sid: Server Id
did: Database Id
"""
# Read the data if present. Skipping read may cause connection
# reset error if data is sent from the client
data = {}
if request.data:
data = json.loads(request.data)
try:
conn = _get_connection(sid, did, trans_id, data.get('db_name', None))
except ConnectionLost as e:
return make_json_response(
success=0,
status=428,
result={"server_label": data.get('server_name', None),
"username": data.get('user', None),
"server_type":data.get('server_type', None),
"errmsg": str(e),
"prompt_password": True,
"allow_save_password": True
if ALLOW_SAVE_PASSWORD and
session.get('allow_save_password', None) else False,
}
)
return make_json_response(
data={
'connId': str(trans_id),
'database': conn.db,
'serverVersion': conn.manager.version,
}
)
def _get_connection(sid, did, trans_id, db_name=None):
"""
Get the connection object of ERD.
:param sid:
:param did:
:param trans_id:
:return:
"""
manager = get_driver(PG_DEFAULT_DRIVER).connection_manager(sid)
try:
conn = manager.connection(conn_id=trans_id,
auto_reconnect=True,
use_binary_placeholder=True,
**({"database": db_name}
if db_name is not None
else {"did": did})
)
status, msg = conn.connect()
if not status:
app.logger.error(msg)
raise ConnectionLost(sid, conn.db, trans_id)
return conn
except Exception as e:
app.logger.error(e)
raise
@blueprint.route('/prequisite/<int:trans_id>/<int:sgid>/<int:sid>/<int:did>',
methods=["GET"],
endpoint='prequisite')
@pga_login_required
def prequisite(trans_id, sgid, sid, did):
conn = _get_connection(sid, did, trans_id)
helper = ERDHelper(trans_id, sid, did)
status, col_types = helper.get_types()
if not status:
return internal_server_error(errormsg=col_types)
status, schemas = get_schemas(conn, show_system_objects=False)
if not status:
return internal_server_error(errormsg=schemas)
status, types = helper.get_geometry_types()
if not status:
return internal_server_error(errormsg=types)
return make_json_response(
data={
'col_types': col_types,
'schemas': schemas['rows'],
'geometry_types': types
},
status=200
)
def translate_foreign_keys(tab_fks, tab_data, all_nodes):
"""
This function will take the from table foreign keys and translate
it into non oid based format. It will allow creating FK sql even
if table is not already created.
:param tab_fks: Table foreign keyss
:param tab_data: Table data
:param all_nodes: All the nodes info from ERD
:return: Translated foreign key data
"""
for tab_fk in tab_fks:
if 'columns' not in tab_fk:
continue
try:
remote_table = all_nodes[tab_fk['columns'][0]['references']]
except KeyError:
continue
tab_fk['schema'] = tab_data['schema']
tab_fk['table'] = tab_data['name']
tab_fk['remote_schema'] = remote_table['schema']
tab_fk['remote_table'] = remote_table['name']
new_column = {
'local_column': tab_fk['columns'][0]['local_column'],
'referenced': tab_fk['columns'][0]['referenced']
}
tab_fk['columns'][0] = new_column
return tab_fks
@blueprint.route('/sql/<int:trans_id>/<int:sgid>/<int:sid>/<int:did>',
methods=["POST"],
endpoint='sql')
@pga_login_required
def sql(trans_id, sgid, sid, did):
data = json.loads(request.data)
with_drop = False
if request.args and 'with_drop' in request.args:
with_drop = True if request.args.get('with_drop') == 'true' else False
helper = ERDHelper(trans_id, sid, did)
conn = _get_connection(sid, did, trans_id)
sql = ''
tab_foreign_keys = []
all_nodes = data.get('nodes', {})
table_sql = ''
for tab_key, tab_data in all_nodes.items():
tab_fks = tab_data.pop('foreign_key', [])
tab_foreign_keys.extend(translate_foreign_keys(tab_fks, tab_data,
all_nodes))
table_sql += '\n\n' + helper.get_table_sql(tab_data,
with_drop=with_drop)
if with_drop:
for tab_fk in tab_foreign_keys:
fk_sql = fkey_utils.get_delete_sql(conn, tab_fk)
sql += '\n\n' + fk_sql
if sql != '':
sql += '\n\n'
sql += table_sql
for tab_fk in tab_foreign_keys:
fk_sql, _ = fkey_utils.get_sql(conn, tab_fk, None)
sql += '\n\n' + fk_sql
return make_json_response(
data=sql,
status=200
)
@socketio.on('connect', namespace=SOCKETIO_NAMESPACE)
def connect():
"""
Connect to the server through socket.
:return:
:rtype:
"""
socketio.emit('connected', {'sid': request.sid},
namespace=SOCKETIO_NAMESPACE,
to=request.sid)
@socketio.on('tables', namespace=SOCKETIO_NAMESPACE)
@socket_login_required
def tables(params):
try:
helper = ERDHelper(params['trans_id'], params['sid'], params['did'])
_get_connection(params['sid'], params['did'], params['trans_id'])
status, tables = helper.get_all_tables(params.get('scid', None),
params.get('tid', None))
if not status:
tables = tables.json if isinstance(tables, Response) else tables
socketio.emit('tables_failed', tables,
namespace=SOCKETIO_NAMESPACE,
to=request.sid)
return
socketio.emit('tables_success', tables, namespace=SOCKETIO_NAMESPACE,
to=request.sid)
except Exception as e:
socketio.emit('tables_failed', str(e), namespace=SOCKETIO_NAMESPACE,
to=request.sid)
@blueprint.route('/close/<int:trans_id>/<int:sgid>/<int:sid>/<int:did>',
methods=["DELETE"],
endpoint='close')
@pga_login_required
def close(trans_id, sgid, sid, did):
manager = get_driver(
PG_DEFAULT_DRIVER).connection_manager(sid)
if manager is not None:
conn = manager.connection(did=did, conn_id=trans_id)
# Release the connection
if conn.connected():
conn.cancel_transaction(trans_id, did=did)
manager.release(did=did, conn_id=trans_id)
return make_json_response(data={'status': True})