sql.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * Parse SQL CREATE TABLE. Simple initial version for community to improve.
  3. */
  4. Draw.loadPlugin(function(ui)
  5. {
  6. // LATER: REFERENCES and PRIMARY KEY
  7. var div = document.createElement('div');
  8. div.style.userSelect = 'none';
  9. div.style.overflow = 'hidden';
  10. div.style.padding = '10px';
  11. div.style.height = '100%';
  12. var graph = ui.editor.graph;
  13. var sqlInput = document.createElement('textarea');
  14. sqlInput.style.height = '200px';
  15. sqlInput.style.width = '100%';
  16. sqlInput.value = 'CREATE TABLE Persons\n(\nPersonID int,\nLastName varchar(255),\n' +
  17. 'FirstName varchar(255),\nAddress varchar(255),\nCity varchar(255)\n);';
  18. mxUtils.br(div);
  19. div.appendChild(sqlInput);
  20. var graph = ui.editor.graph;
  21. var wnd = new mxWindow(mxResources.get('insertTable'), div, document.body.offsetWidth - 480, 140,
  22. 320, 300, true, true);
  23. wnd.destroyOnClose = false;
  24. wnd.setMaximizable(false);
  25. wnd.setResizable(false);
  26. wnd.setClosable(true);
  27. function parseSql(text)
  28. {
  29. var lines = text.split('\n');
  30. var tableCell = null;
  31. var rows = null;
  32. var cells = [];
  33. var dx = 0;
  34. for (var i = 0; i < lines.length; i++)
  35. {
  36. var tmp = mxUtils.trim(lines[i]);
  37. if (tmp.substring(0, 12).toLowerCase() == 'create table')
  38. {
  39. var name = mxUtils.trim(tmp.substring(12));
  40. if (name.charAt(name.length - 1) == '(')
  41. {
  42. name = name.substring(0, name.lastIndexOf(' '));
  43. }
  44. tableCell = new mxCell(name, new mxGeometry(dx, 0, 160, 26),
  45. 'swimlane;fontStyle=0;childLayout=stackLayout;horizontal=1;startSize=26;fillColor=#e0e0e0;horizontalStack=0;resizeParent=1;resizeLast=0;collapsible=1;marginBottom=0;swimlaneFillColor=#ffffff;align=center;');
  46. tableCell.vertex = true;
  47. cells.push(tableCell);
  48. var size = ui.editor.graph.getPreferredSizeForCell(rowCell);
  49. if (size != null)
  50. {
  51. tableCell.geometry.width = size.width + 10;
  52. }
  53. // For primary key lookups
  54. rows = {};
  55. }
  56. else if (tableCell != null && tmp.charAt(0) == ')')
  57. {
  58. dx += tableCell.geometry.width + 40;
  59. tableCell = null;
  60. }
  61. else if (tmp != '(' && tableCell != null)
  62. {
  63. var name = tmp.substring(0, (tmp.charAt(tmp.length - 1) == ',') ? tmp.length - 1 : tmp.length);
  64. if (name.substring(0, 11).toLowerCase() != 'primary key')
  65. {
  66. var rowCell = new mxCell(name, new mxGeometry(0, 0, 90, 26),
  67. 'shape=partialRectangle;top=0;left=0;right=0;bottom=0;align=left;verticalAlign=top;spacingTop=-2;fillColor=none;spacingLeft=34;spacingRight=4;overflow=hidden;rotatable=0;points=[[0,0.5],[1,0.5]];portConstraint=eastwest;dropTarget=0;');
  68. rowCell.vertex = true;
  69. var left = sb.cloneCell(rowCell, '' /* eg. PK */);
  70. left.connectable = false;
  71. left.style = 'shape=partialRectangle;top=0;left=0;bottom=0;fillColor=none;align=left;verticalAlign=middle;spacingLeft=4;spacingRight=4;overflow=hidden;rotatable=0;points=[];portConstraint=eastwest;part=1;'
  72. left.geometry.width = 30;
  73. left.geometry.height = 26;
  74. rowCell.insert(left);
  75. var size = ui.editor.graph.getPreferredSizeForCell(rowCell);
  76. if (size != null && tableCell.geometry.width < size.width + 10)
  77. {
  78. tableCell.geometry.width = size.width + 10;
  79. }
  80. tableCell.insert(rowCell);
  81. tableCell.geometry.height += 26;
  82. rows[rowCell.value] = rowCell;
  83. }
  84. }
  85. }
  86. if (cells.length > 0)
  87. {
  88. var graph = ui.editor.graph;
  89. var view = graph.view;
  90. var bds = graph.getGraphBounds();
  91. // Computes unscaled, untranslated graph bounds
  92. var x = Math.ceil(Math.max(0, bds.x / view.scale - view.translate.x) + 4 * graph.gridSize);
  93. var y = Math.ceil(Math.max(0, (bds.y + bds.height) / view.scale - view.translate.y) + 4 * graph.gridSize);
  94. graph.setSelectionCells(graph.importCells(cells, x, y));
  95. graph.scrollCellToVisible(graph.getSelectionCell());
  96. }
  97. wnd.setVisible(false);
  98. };
  99. mxUtils.br(div);
  100. var resetBtn = mxUtils.button(mxResources.get('reset'), function()
  101. {
  102. sqlInput.value = '';
  103. });
  104. resetBtn.style.marginTop = '8px';
  105. resetBtn.style.marginRight = '4px';
  106. resetBtn.style.padding = '4px';
  107. div.appendChild(resetBtn);
  108. var btn = mxUtils.button(mxResources.get('close'), function()
  109. {
  110. wnd.setVisible(false);
  111. });
  112. btn.style.marginTop = '8px';
  113. btn.style.marginRight = '4px';
  114. btn.style.padding = '4px';
  115. div.appendChild(btn);
  116. var btn = mxUtils.button(mxResources.get('insert'), function()
  117. {
  118. parseSql(sqlInput.value);
  119. });
  120. btn.style.marginTop = '8px';
  121. btn.style.padding = '4px';
  122. div.appendChild(btn);
  123. // Extends Extras menu
  124. mxResources.parse('fromSql=From SQL');
  125. // Adds action
  126. ui.actions.addAction('fromSql', function()
  127. {
  128. wnd.setVisible(!wnd.isVisible());
  129. if (wnd.isVisible())
  130. {
  131. sqlInput.focus();
  132. }
  133. });
  134. var theMenu = ui.menus.get('insert');
  135. var oldMenu = theMenu.funct;
  136. theMenu.funct = function(menu, parent)
  137. {
  138. oldMenu.apply(this, arguments);
  139. ui.menus.addMenuItems(menu, ['fromSql'], parent);
  140. };
  141. });