1919import java .util .Comparator ;
2020import java .util .HashSet ;
2121import java .util .List ;
22+ import java .util .Objects ;
2223import java .util .Optional ;
2324import java .util .Set ;
2425
@@ -371,6 +372,8 @@ public static View updateViewContent(View view) {
371372 ViewColumn viewColumn = DatabaseFactory .eINSTANCE .createViewColumn ();
372373 viewColumn .setName (parsedColumn .getName ());
373374 viewColumn .setAlias (parsedColumn .getAlias ());
375+ view .getColumns ().add (viewColumn );
376+
374377 if (!StringUtils .isNullOrWhite (parsedColumn .getTable ())) {
375378 Optional <ViewTable > fromTableOpt = view .getTables ().stream ()
376379 .filter (t -> parsedColumn .getTable ().equals (t .getName ()) ||
@@ -379,8 +382,12 @@ public static View updateViewContent(View view) {
379382 if (fromTableOpt .isPresent ()) {
380383 viewColumn .setFrom (fromTableOpt .get ());
381384 }
385+ } else {
386+ ViewTable viewTable = computeViewTableFromViewColumn (viewColumn );
387+ if (viewTable != null ) {
388+ viewColumn .setFrom (viewTable );
389+ }
382390 }
383- view .getColumns ().add (viewColumn );
384391 }
385392 }
386393
@@ -389,6 +396,39 @@ public static View updateViewContent(View view) {
389396 return view ;
390397 }
391398
399+ private static ViewTable computeViewTableFromViewColumn (ViewColumn viewColumn ) {
400+
401+ View view = EObjectUtils .getContainer (viewColumn , View .class );
402+
403+ // First collect the view tables to look the column in
404+ List <ViewTable > viewTables = new ArrayList <>();
405+
406+ if (viewColumn .getFrom () != null ) {
407+ // The "from" table is specified then we must find the column in it
408+ viewTables .add (viewColumn .getFrom ());
409+ } else {
410+ // The "from" table is not specified then we must find the column in one of the ViewTables
411+ viewTables .addAll (view .getTables ());
412+ }
413+
414+ // Now that we know the tables where to look the column in, look for it
415+ Column column = viewTables .stream ()
416+ .map (DatabaseServices ::findTable )
417+ .filter (Objects ::nonNull )
418+ .flatMap (t -> t .getColumns ().stream ())
419+ .filter (c -> viewColumn .getName ().equals (c .getName ()))
420+ .findFirst ().orElse (null );
421+
422+ // And finally crawl to the View Table
423+ if (column != null ) {
424+ Table table = column .getOwner ();
425+ return view .getTables ().stream ()
426+ .filter (vt -> vt .getName ().equals (table .getName ())).findFirst ().orElse (null );
427+ }
428+
429+ return null ;
430+ }
431+
392432 private static void validateViewQuery (View view ) {
393433 List <DDiagramEditor > openDDiagramEditorsShowingView = EclipseUtils .getOpenDDiagramEditors ().stream ()
394434 .filter (dde -> dde .getRepresentation ().getOwnedRepresentationElements ()
@@ -426,34 +466,27 @@ public List<ViewElement> getViewElementsOrderedForDatabaseDiagram(View view) {
426466 return viewElements ;
427467 }
428468
429- private Table findTable (ViewTable viewTable ) {
469+ private static Table findTable (ViewTable viewTable ) {
430470 TableContainer tableContainer = EObjectUtils .getContainer (viewTable , TableContainer .class );
431471 return tableContainer .getTables ().stream ()
432472 .filter (Table .class ::isInstance ).map (Table .class ::cast )
433473 .filter (t -> viewTable .getName ().equals (t .getName ()))
434474 .findFirst ().orElse (null );
435475 }
436476
437- private Object findColumn (ViewColumn viewColumn ) {
438-
439- // First collect the view tables to look the column in
440- List <ViewTable > viewTables = new ArrayList <>();
441-
442- if (viewColumn .getFrom () != null ) {
443- // The "from" table is specified then we must find the column in it
444- viewTables .add (viewColumn .getFrom ());
445- } else {
446- // The "from" table is not specified then we must find the column in one of the ViewTables
447- viewTables .addAll (EObjectUtils .getContainer (viewColumn , View .class ).getTables ());
477+ private static Column findColumn (ViewColumn viewColumn ) {
478+ if (viewColumn .getFrom () == null ) {
479+ return null ;
480+ }
481+
482+ Table table = findTable (viewColumn .getFrom ());
483+ if (table == null ) {
484+ return null ;
448485 }
449-
450- // Now that we know the tables where to look the column in, look for it
451- return viewTables .stream ()
452- .map (this ::findTable )
453- .flatMap (t -> t .getColumns ().stream ())
454- .filter (c -> viewColumn .getName ().equals (c .getName ()))
455- .findFirst ().orElse (null );
456-
486+
487+ return table .getColumns ().stream ()
488+ .filter (c -> viewColumn .getName ().equals (c .getName ()))
489+ .findFirst ().orElse (null );
457490 }
458491
459492 public boolean isQueryValid (View view ) {
@@ -462,6 +495,10 @@ public boolean isQueryValid(View view) {
462495 return false ;
463496 }
464497
498+ if (view .getColumns ().stream ().anyMatch (c -> !c .getName ().equals ("*" ) && c .getFrom () == null )) {
499+ return false ;
500+ }
501+
465502 if (view .getColumns ().stream ().anyMatch (c -> !c .getName ().equals ("*" ) && findColumn (c ) == null )) {
466503 return false ;
467504 }
@@ -476,18 +513,19 @@ public String queryValidationMessage(View view) {
476513 .filter (t -> findTable (t ) == null )
477514 .collect (toList ());
478515 tablesNotFound .forEach (t -> message .append (String .format ("Table %s doesn't exist.\n " , t .getName ())));
479-
516+
517+ List <ViewColumn > columnsFromNoTable = view .getColumns ().stream ()
518+ .filter (c -> !c .getName ().equals ("*" ) && c .getFrom () == null )
519+ .collect (toList ());
520+ columnsFromNoTable .forEach (c -> message .append (String .format ("Column %s is from no existing table.\n " , c .getName ())));
521+
480522 view .getColumns ().stream ()
481- .filter (c -> !c .getName ().equals ("*" )
523+ .filter (c -> !c .getName ().equals ("*" )
524+ && !columnsFromNoTable .contains (c )
482525 && !tablesNotFound .contains (c .getFrom ())
483526 && findColumn (c ) == null )
484- .forEach (c -> {
485- if (c .getFrom () != null )
486- message .append (String .format ("Column %s of table %s doesn't exist.\n " , c .getName (), c .getFrom ().getName ()));
487- else
488- message .append (String .format ("Column %s doesn't exist in the 'from' tables.\n " , c .getName ()));
489- });
490-
527+ .forEach (c -> message .append (String .format ("Column %s of table %s doesn't exist.\n " , c .getName (), c .getFrom ().getName ())));
528+
491529 if (message .length () > 0 ) {
492530 message .deleteCharAt (message .length () - 1 );
493531 }
0 commit comments