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. // Extends Extras menu
  22. mxResources.parse('fromSql=From SQL');
  23. var wnd = new mxWindow(mxResources.get('fromSql'), div, document.body.offsetWidth - 480, 140,
  24. 320, 300, true, true);
  25. wnd.destroyOnClose = false;
  26. wnd.setMaximizable(false);
  27. wnd.setResizable(false);
  28. wnd.setClosable(true);
  29. function parseSql(text)
  30. {
  31. var lines = text.split('\n');
  32. var tableCell = null;
  33. var rows = null;
  34. var cells = [];
  35. var dx = 0;
  36. for (var i = 0; i < lines.length; i++)
  37. {
  38. var tmp = mxUtils.trim(lines[i]);
  39. if (tmp.substring(0, 12).toLowerCase() == 'create table')
  40. {
  41. var name = mxUtils.trim(tmp.substring(12));
  42. if (name.charAt(name.length - 1) == '(')
  43. {
  44. name = name.substring(0, name.lastIndexOf(' '));
  45. }
  46. tableCell = new mxCell(name, new mxGeometry(dx, 0, 160, 26),
  47. '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;');
  48. tableCell.vertex = true;
  49. cells.push(tableCell);
  50. var size = ui.editor.graph.getPreferredSizeForCell(rowCell);
  51. if (size != null)
  52. {
  53. tableCell.geometry.width = size.width + 10;
  54. }
  55. // For primary key lookups
  56. rows = {};
  57. }
  58. else if (tableCell != null && tmp.charAt(0) == ')')
  59. {
  60. dx += tableCell.geometry.width + 40;
  61. tableCell = null;
  62. }
  63. else if (tmp != '(' && tableCell != null)
  64. {
  65. var name = tmp.substring(0, (tmp.charAt(tmp.length - 1) == ',') ? tmp.length - 1 : tmp.length);
  66. if (name.substring(0, 11).toLowerCase() != 'primary key')
  67. {
  68. var rowCell = new mxCell(name, new mxGeometry(0, 0, 90, 26),
  69. '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;');
  70. rowCell.vertex = true;
  71. var left = sb.cloneCell(rowCell, '' /* eg. PK */);
  72. left.connectable = false;
  73. 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;'
  74. left.geometry.width = 30;
  75. left.geometry.height = 26;
  76. rowCell.insert(left);
  77. var size = ui.editor.graph.getPreferredSizeForCell(rowCell);
  78. if (size != null && tableCell.geometry.width < size.width + 10)
  79. {
  80. tableCell.geometry.width = size.width + 10;
  81. }
  82. tableCell.insert(rowCell);
  83. tableCell.geometry.height += 26;
  84. rows[rowCell.value] = rowCell;
  85. }
  86. }
  87. }
  88. if (cells.length > 0)
  89. {
  90. var graph = ui.editor.graph;
  91. var view = graph.view;
  92. var bds = graph.getGraphBounds();
  93. // Computes unscaled, untranslated graph bounds
  94. var x = Math.ceil(Math.max(0, bds.x / view.scale - view.translate.x) + 4 * graph.gridSize);
  95. var y = Math.ceil(Math.max(0, (bds.y + bds.height) / view.scale - view.translate.y) + 4 * graph.gridSize);
  96. graph.setSelectionCells(graph.importCells(cells, x, y));
  97. graph.scrollCellToVisible(graph.getSelectionCell());
  98. }
  99. wnd.setVisible(false);
  100. };
  101. mxUtils.br(div);
  102. var resetBtn = mxUtils.button(mxResources.get('reset'), function()
  103. {
  104. sqlInput.value = '';
  105. });
  106. resetBtn.style.marginTop = '8px';
  107. resetBtn.style.marginRight = '4px';
  108. resetBtn.style.padding = '4px';
  109. div.appendChild(resetBtn);
  110. var btn = mxUtils.button(mxResources.get('cancel'), function()
  111. {
  112. wnd.setVisible(false);
  113. });
  114. btn.style.marginTop = '8px';
  115. btn.style.marginRight = '4px';
  116. btn.style.padding = '4px';
  117. div.appendChild(btn);
  118. var btn = mxUtils.button(mxResources.get('insert'), function()
  119. {
  120. parseSql(sqlInput.value);
  121. });
  122. btn.style.marginTop = '8px';
  123. btn.style.padding = '4px';
  124. div.appendChild(btn);
  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. });