123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package com.rthoni.intellij.codefromds.DataAccess;
-
- import com.intellij.database.model.DasArgument;
- import com.intellij.database.psi.*;
- import com.intellij.database.util.DasUtil;
- import com.rthoni.intellij.codefromds.business.Helper;
- import com.rthoni.intellij.codefromds.dbo.template.*;
-
- import java.util.List;
- import java.util.stream.Collectors;
-
- /**
- * Created by robin on 4/13/17.
- */
- public class DataSourcesDataAccess {
-
- public static List<DataSourceDbo> getDataSourcesDbo()
- {
- return Helper.getDataSources().stream()
- .map(DataSourcesDataAccess::dataSourceToDbo)
- .collect(Collectors.toList());
- }
-
- public static DataSourceDbo getDataSourceDbo(String name)
- {
- return Helper.getDataSources().stream()
- .filter(dataSource -> dataSource.getName().equals(name))
- .map(DataSourcesDataAccess::dataSourceToDbo)
- .findFirst().orElse(null);
- }
-
- private static DataSourceDbo dataSourceToDbo(DbDataSource dataSource)
- {
- DataSourceDbo dataSourceDbo = new DataSourceDbo();
- dataSourceDbo.setName(dataSource.getName());
-
- final DataHolder<TableDataSourceDbo> tableDataSourceDbo = new DataHolder<>();
- final DataHolder<ColumnDataSourceDbo> columnDataSourceDbo = new DataHolder<>();
-
- dataSource.getModel().traverser().forEach(dasObject ->
- {
- if (dasObject instanceof DbTable) {
- DbTable table = (DbTable) dasObject;
- tableDataSourceDbo.data = new TableDataSourceDbo();
- tableDataSourceDbo.data.setName(table.getName());
- dataSourceDbo.addTable(tableDataSourceDbo.data);
- }
- else if (dasObject instanceof DbColumn) {
- DbColumn column = (DbColumn) dasObject;
- columnDataSourceDbo.data = new ColumnDataSourceDbo();
- columnDataSourceDbo.data.setName(column.getName());
- columnDataSourceDbo.data.setNotNull(column.isNotNull());
- SqlTypeDbo type = new SqlTypeDbo();
- type.setType(column.getDataType().typeName);
- type.setVagueArg(column.getDataType().vagueArg);
- columnDataSourceDbo.data.setSqlType(type);
- columnDataSourceDbo.data.setDefaultValue(column.getDefault());
- columnDataSourceDbo.data.setPrimary(DasUtil.isPrimary(column));
- tableDataSourceDbo.data.addColumn(columnDataSourceDbo.data);
- }
- });
-
- dataSource.getModel().traverser().forEach(dasObject ->
- {
- if (dasObject instanceof DbTable) {
- DbTable table = (DbTable) dasObject;
- tableDataSourceDbo.data = dataSourceDbo.findTable(table.getName());
- DasUtil.getForeignKeys(table).forEach(fk ->
- {
- if (fk.getRefTable() == null) {
- return;
- }
-
- TableDataSourceDbo srcTable = dataSourceDbo.findTable(fk.getTable().getName());
- TableDataSourceDbo targetTable = dataSourceDbo.findTable(fk.getRefTable().getName());
-
- ForeignKeyDbo fkDbo = new ForeignKeyDbo();
- fkDbo.setName(fk.getName());
- fkDbo.setSourceTable(srcTable);
- fkDbo.setSourceForeignKeyName(fkDbo.getName());//TODO find if it already exists
- fkDbo.setTargetTable(targetTable);
- fkDbo.setTargetForeignKeyName(fkDbo.getName());//TODO find if it already exists
-
- fk.getColumnsRef().names().forEach(s -> fkDbo.addSourceColumn(srcTable.findColumn(s)));
- fk.getRefColumns().names().forEach(s -> fkDbo.addTargetColumn(targetTable.findColumn(s)));
-
- srcTable.addSourceForeignKey(fkDbo);
- targetTable.addTargetForeignKey(fkDbo);
-
- });
- }
- else if (dasObject instanceof DbColumn) {
- DbColumn column = (DbColumn) dasObject;
- columnDataSourceDbo.data = tableDataSourceDbo.data.findColumn(column.getName());
- }
- else if (dasObject instanceof DbIndex) {
- DbIndex index = (DbIndex) dasObject;
- }
- else if (dasObject instanceof DbConstraint) {
- DbConstraint constraint = (DbConstraint) dasObject;
- }
- else if (dasObject instanceof DbRoutine) {
- DbRoutine sp = (DbRoutine) dasObject;
- StoredProcedureDbo spDbo = new StoredProcedureDbo();
- spDbo.setSelected(false);
- spDbo.setName(sp.getName());
- spDbo.setFullName(sp.getText());
- dataSourceDbo.addStoredProcedure(spDbo);
- DataHolder<Integer> position = new DataHolder<>();
- position.data = 0;
- TableDataSourceDbo returnType = new TableDataSourceDbo();
- returnType.setName(spDbo.getName());
- sp.getArguments().forEach(o ->
- {
- SqlTypeDbo sqlTypeDbo = new SqlTypeDbo();
- sqlTypeDbo.setType(o.getDataType().typeName);
- sqlTypeDbo.setVagueArg(o.getDataType().vagueArg);
-
- DasArgument.Direction dir = o.getArgumentDirection();
-
- if (dir.isReturnOrResult()) {
- spDbo.setSqlType(sqlTypeDbo);
- }
- if (dir.isIn()) {
- StoredProcedureArgDbo argDbo = new StoredProcedureArgDbo();
- argDbo.setName(o.getName().isEmpty() ? "arg" + position.data : o.getName());
- argDbo.setSqlType(sqlTypeDbo);
- spDbo.addArgument(argDbo);
- ++position.data;
- }
- if (dir.isOut()) {
- ColumnDataSourceDbo field = new ColumnDataSourceDbo();
- field.setSelected(true);
- field.setPrimary(false);
- field.setDefaultValue(null);
- field.setNotNull(false);
- field.setName(o.getName());
- field.setSqlType(sqlTypeDbo);
- returnType.addColumn(field);
- }
- });
- if (returnType.getColumns().size() > 1) {
- boolean isSetOf = sp.getReturnArgument().getDataType().typeName.startsWith("setof ");
- SqlTypeDbo sqlTypeDbo = new SqlTypeDbo();
- sqlTypeDbo.setType((isSetOf ? "setof " : "" ) + returnType.getName());
- sqlTypeDbo.setVagueArg(null);
- spDbo.setSqlType(sqlTypeDbo);
- dataSourceDbo.addType(returnType);
- }
- else {
- DasArgument returnArg = sp.getReturnArgument();
- if (returnArg != null) {
- }
- }
- }
- });
- return dataSourceDbo;
- }
- }
|