Search in sources :

Example 1 with DasTable

use of com.intellij.database.model.DasTable in project yii2support by nvlad.

the class DatabaseUtils method getColumnsByTable.

public static ArrayList<String> getColumnsByTable(String table, Project project) {
    String prefixedTable = AddTablePrefix(table, true, project);
    DbPsiFacade facade = DbPsiFacade.getInstance(project);
    List<DbDataSource> dataSources = facade.getDataSources();
    ArrayList<String> list = new ArrayList<>();
    if (table == null)
        return list;
    table = ClassUtils.removeQuotes(prefixedTable);
    for (DbDataSource source : dataSources) {
        for (Object item : source.getModel().traverser().filter(DasTable.class)) {
            if (item instanceof DasTable && ((DasTable) item).getName().equals(prefixedTable)) {
                TableInfo tableInfo = new TableInfo((DasTable) item);
                for (DasColumn column : tableInfo.getColumns()) {
                    list.add(ClassUtils.removeQuotes(column.getName()));
                }
            }
        }
    }
    return list;
}
Also used : DasColumn(com.intellij.database.model.DasColumn) ArrayList(java.util.ArrayList) DasObject(com.intellij.database.model.DasObject) TableInfo(com.nvlad.yii2support.database.TableInfo) DasTable(com.intellij.database.model.DasTable)

Example 2 with DasTable

use of com.intellij.database.model.DasTable in project yii2support by nvlad.

the class DatabaseUtils method getNotDeclaredColumns.

public static ArrayList<VirtualProperty> getNotDeclaredColumns(String table, Collection<Field> fields, Project project) {
    DbPsiFacade facade = DbPsiFacade.getInstance(project);
    List<DbDataSource> dataSources = facade.getDataSources();
    final ArrayList<VirtualProperty> result = new ArrayList<>();
    if (table == null)
        return result;
    for (DbDataSource source : dataSources) {
        for (Object item : source.getModel().traverser().filter(DasTable.class)) {
            table = ClassUtils.removeQuotes(table);
            if (item instanceof DasTable && ((DasTable) item).getName().equals(table)) {
                TableInfo tableInfo = new TableInfo((DasTable) item);
                for (DasColumn column : tableInfo.getColumns()) {
                    boolean found = false;
                    PhpDocProperty prevProperty = null;
                    for (Field field : fields) {
                        if (field != null && field.getName().equals(column.getName())) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        VirtualProperty newItem = new VirtualProperty(column.getName(), column.getDataType().typeName, column.getDataType().toString(), column.getComment(), null);
                        result.add(newItem);
                    }
                }
            }
        }
    }
    return result;
}
Also used : DasColumn(com.intellij.database.model.DasColumn) ArrayList(java.util.ArrayList) PhpDocProperty(com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocProperty) DasTable(com.intellij.database.model.DasTable) DasObject(com.intellij.database.model.DasObject) TableInfo(com.nvlad.yii2support.database.TableInfo)

Example 3 with DasTable

use of com.intellij.database.model.DasTable in project yii2support by nvlad.

the class DatabaseUtils method getLookupItemsTables.

public static ArrayList<LookupElementBuilder> getLookupItemsTables(Project project, PhpExpression position) {
    DbPsiFacade facade = DbPsiFacade.getInstance(project);
    List<DbDataSource> dataSources = facade.getDataSources();
    // Code to test tests :)
    // dataSources.clear();
    // dataSources.add(new TestDataSource(project));
    ArrayList<LookupElementBuilder> list = new ArrayList<>();
    for (DbDataSource source : dataSources) {
        JBIterable<DasTable> filtered = source.getModel().traverser().filter(DasTable.class);
        for (Object item : filtered) {
            if (item instanceof DasTable) {
                list.add(DatabaseUtils.buildLookup(item, true, project));
            }
        }
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) DasObject(com.intellij.database.model.DasObject) DasTable(com.intellij.database.model.DasTable)

Example 4 with DasTable

use of com.intellij.database.model.DasTable in project yii2support by nvlad.

the class DatabaseUtils method getLookupItemsByTable.

@Nullable
public static ArrayList<LookupElementBuilder> getLookupItemsByTable(String table, Project project, PhpExpression position) {
    ArrayList<LookupElementBuilder> list = new ArrayList<>();
    if (table == null || table.isEmpty())
        return list;
    DbPsiFacade facade = DbPsiFacade.getInstance(project);
    List<DbDataSource> dataSources = facade.getDataSources();
    // Code to test tests :)
    // dataSources.clear();
    // dataSources.add(new TestDataSource(project));
    String prefixedTable = AddTablePrefix(table, true, project);
    for (DbDataSource source : dataSources) {
        for (Object item : source.getModel().traverser().filter(DasTable.class)) {
            if (item instanceof DasTable && (((DasTable) item).getName().equals(prefixedTable) || ((DasTable) item).getName().equals(table))) {
                TableInfo tableInfo = new TableInfo((DasTable) item);
                for (DasColumn column : tableInfo.getColumns()) {
                    list.add(DatabaseUtils.buildLookup(column, true, project));
                }
            }
        }
    }
    return list;
}
Also used : DasColumn(com.intellij.database.model.DasColumn) ArrayList(java.util.ArrayList) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) DasObject(com.intellij.database.model.DasObject) TableInfo(com.nvlad.yii2support.database.TableInfo) DasTable(com.intellij.database.model.DasTable) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with DasTable

use of com.intellij.database.model.DasTable in project yii2support by nvlad.

the class DatabaseUtils method isTableExists.

public static boolean isTableExists(String table, Project project) {
    if (table == null)
        return false;
    DbPsiFacade facade = DbPsiFacade.getInstance(project);
    List<DbDataSource> dataSources = facade.getDataSources();
    table = ClassUtils.removeQuotes(table);
    for (DbDataSource source : dataSources) {
        for (Object item : source.getModel().traverser().filter(DasTable.class)) {
            if (item instanceof DasTable && ((DasTable) item).getName().equals(table)) {
                return true;
            }
        }
    }
    return false;
}
Also used : DasObject(com.intellij.database.model.DasObject) DasTable(com.intellij.database.model.DasTable)

Aggregations

DasObject (com.intellij.database.model.DasObject)6 DasTable (com.intellij.database.model.DasTable)6 ArrayList (java.util.ArrayList)5 DasColumn (com.intellij.database.model.DasColumn)4 TableInfo (com.nvlad.yii2support.database.TableInfo)4 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)3 PhpDocProperty (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocProperty)2 Nullable (org.jetbrains.annotations.Nullable)2 com.intellij.database.psi (com.intellij.database.psi)1 TypePresentationService (com.intellij.ide.TypePresentationService)1 Document (com.intellij.openapi.editor.Document)1 Project (com.intellij.openapi.project.Project)1 PsiElement (com.intellij.psi.PsiElement)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 JBIterable (com.intellij.util.containers.JBIterable)1 PhpDocComment (com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment)1 PhpDocPropertyTag (com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocPropertyTag)1 com.jetbrains.php.lang.psi.elements (com.jetbrains.php.lang.psi.elements)1 ClassConstImpl (com.jetbrains.php.lang.psi.elements.impl.ClassConstImpl)1 StringLiteralExpressionImpl (com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl)1