Search in sources :

Example 1 with IType

use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.

the class QueryHelper method generateSQL.

public static String generateSQL(ExecutionContext executionContext, final ISessionFactory sessionFactory, final String query, final IService service) {
    // $NON-NLS-1$
    if (StringHelper.isEmpty(query))
        return "";
    String result = (String) executionContext.execute(new ExecutionContext.Command() {

        public Object execute() {
            try {
                StringBuffer str = new StringBuffer(256);
                IHQLQueryPlan plan = service.newHQLQueryPlan(query, false, sessionFactory);
                IQueryTranslator[] translators = plan.getTranslators();
                for (int i = 0; i < translators.length; i++) {
                    IQueryTranslator translator = translators[i];
                    if (translator.isManipulationStatement()) {
                        // $NON-NLS-1$
                        str.append(ConsoleMessages.DynamicSQLPreviewView_manipulation_of + i + ":");
                        Iterator<?> iterator = translator.getQuerySpaces().iterator();
                        while (iterator.hasNext()) {
                            Object qspace = iterator.next();
                            str.append(qspace);
                            // $NON-NLS-1$
                            if (iterator.hasNext()) {
                                str.append(", ");
                            }
                        }
                    } else {
                        IType[] returnTypes = translator.getReturnTypes();
                        // $NON-NLS-1$
                        str.append(i + ": ");
                        for (int j = 0; j < returnTypes.length; j++) {
                            IType returnType = returnTypes[j];
                            str.append(returnType.getName());
                            // $NON-NLS-1$
                            if (j < returnTypes.length - 1) {
                                str.append(", ");
                            }
                        }
                    }
                    // $NON-NLS-1$
                    str.append("\n-----------------\n");
                    Iterator<?> sqls = translator.collectSqlStrings().iterator();
                    while (sqls.hasNext()) {
                        String sql = (String) sqls.next();
                        str.append(QLFormatHelper.formatForScreen(sql));
                        // $NON-NLS-1$
                        str.append("\n\n");
                    }
                }
                return str.toString();
            } catch (Throwable t) {
                StringBuffer msgs = new StringBuffer();
                Throwable cause = t;
                while (cause != null) {
                    msgs.append(t);
                    if (cause.getCause() == cause) {
                        cause = null;
                    } else {
                        cause = cause.getCause();
                        if (cause != null)
                            msgs.append(ConsoleMessages.DynamicSQLPreviewView_caused_by);
                    }
                }
                return msgs.toString();
            }
        }
    });
    return result;
}
Also used : IHQLQueryPlan(org.jboss.tools.hibernate.runtime.spi.IHQLQueryPlan) IQueryTranslator(org.jboss.tools.hibernate.runtime.spi.IQueryTranslator) IType(org.jboss.tools.hibernate.runtime.spi.IType)

Example 2 with IType

use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.

the class ElementsFactory method processExpand.

protected void processExpand(ExpandableShape shape) {
    Object element = shape.getOrmElement();
    if (!(element instanceof IProperty)) {
        return;
    }
    OrmShape s = null;
    IProperty property = (IProperty) element;
    if (!property.isComposite()) {
        final IConfiguration config = getConfig();
        // 
        IValue v = property.getValue();
        IType type = UtilTypeExtract.getTypeUsingExecContext(v, getConsoleConfig());
        if (type != null && type.isEntityType()) {
            Object clazz = config != null ? config.getClassMapping(type.getAssociatedEntityName()) : null;
            if (clazz instanceof IPersistentClass && ((IPersistentClass) clazz).isInstanceOfRootClass()) {
                IPersistentClass rootClass = (IPersistentClass) clazz;
                s = getOrCreatePersistentClass(rootClass, null);
                if (shouldCreateConnection(shape, s)) {
                    connections.add(new Connection(shape, s));
                }
            } else if (clazz instanceof IPersistentClass && ((IPersistentClass) clazz).isInstanceOfSubclass()) {
                s = getOrCreatePersistentClass(((IPersistentClass) clazz).getRootClass(), null);
            }
        }
    } else {
        s = getOrCreatePersistentClass(getService().newSpecialRootClass(property), null);
        if (shouldCreateConnection(shape, s)) {
            connections.add(new Connection(shape, s));
        }
        createConnections(s, getOrCreateDatabaseTable(property.getValue().getTable()));
    }
}
Also used : IValue(org.jboss.tools.hibernate.runtime.spi.IValue) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) IType(org.jboss.tools.hibernate.runtime.spi.IType)

Example 3 with IType

use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.

the class OrmShape method initModel.

/**
 * creates children of the shape,
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void initModel() {
    Object ormElement = getOrmElement();
    if (ormElement instanceof IPersistentClass && ((IPersistentClass) ormElement).isInstanceOfRootClass()) {
        IPersistentClass rootClass = (IPersistentClass) ormElement;
        IProperty identifierProperty = rootClass.getIdentifierProperty();
        if (identifierProperty != null) {
            addChild(new Shape(identifierProperty, getConsoleConfigName()));
        }
        IValue identifier = rootClass.getIdentifier();
        if (identifier != null && identifier.isComponent()) {
            IValue component = identifier;
            if (component.isEmbedded()) {
                Iterator<IProperty> iterator = identifier.getPropertyIterator();
                while (iterator.hasNext()) {
                    IProperty property = iterator.next();
                    addChild(new Shape(property, getConsoleConfigName()));
                }
            }
        }
        Iterator<IProperty> iterator = rootClass.getPropertyIterator();
        while (iterator.hasNext()) {
            IProperty field = iterator.next();
            if (!field.isBackRef()) {
                if (!field.isComposite()) {
                    final IValue val = field.getValue();
                    Shape bodyOrmShape = null;
                    if (val.isSimpleValue() && val.isTypeSpecified()) {
                        bodyOrmShape = new Shape(field, getConsoleConfigName());
                    } else {
                        if (val.isCollection()) {
                            bodyOrmShape = new ComponentShape(field, getConsoleConfigName());
                        } else {
                            IType type = getTypeUsingExecContext(val);
                            if (type != null && type.isEntityType()) {
                                bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
                            } else {
                                bodyOrmShape = new Shape(field, getConsoleConfigName());
                            }
                        }
                    }
                    addChild(bodyOrmShape);
                } else {
                    Shape bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
                    addChild(bodyOrmShape);
                }
            }
        }
    } else if (ormElement instanceof IPersistentClass && ((IPersistentClass) ormElement).isInstanceOfSubclass()) {
        IPersistentClass rootClass = ((IPersistentClass) ormElement).getRootClass();
        IProperty identifierProperty = rootClass.getIdentifierProperty();
        if (identifierProperty != null) {
            addChild(new Shape(identifierProperty, getConsoleConfigName()));
        }
        IValue identifier = rootClass.getIdentifier();
        if (identifier.isComponent()) {
            Iterator<IProperty> iterator = identifier.getPropertyIterator();
            while (iterator.hasNext()) {
                IProperty property = iterator.next();
                addChild(new Shape(property, getConsoleConfigName()));
            }
        }
        Iterator<IProperty> iterator = rootClass.getPropertyIterator();
        while (iterator.hasNext()) {
            IProperty field = iterator.next();
            if (!field.isBackRef()) {
                if (!field.isComposite()) {
                    IValue fieldValue = field.getValue();
                    boolean typeIsAccessible = true;
                    if (fieldValue.isSimpleValue() && fieldValue.isTypeSpecified()) {
                        try {
                            field.getValue().getType();
                        } catch (Exception e) {
                            typeIsAccessible = false;
                        }
                    }
                    Shape bodyOrmShape = null;
                    if (typeIsAccessible && field.getValue().isSimpleValue()) {
                        bodyOrmShape = new Shape(field, getConsoleConfigName());
                    } else if (typeIsAccessible && field.getValue().getType().isEntityType()) {
                        bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
                    } else if (typeIsAccessible && field.getValue().getType().isCollectionType()) {
                        bodyOrmShape = new ComponentShape(field, getConsoleConfigName());
                    } else {
                        bodyOrmShape = new Shape(field, getConsoleConfigName());
                    }
                    addChild(bodyOrmShape);
                } else {
                    Shape bodyOrmShape = new ExpandableShape(field, getConsoleConfigName());
                    addChild(bodyOrmShape);
                }
            }
        }
        Iterator<IProperty> iter = ((IPersistentClass) ormElement).getPropertyIterator();
        while (iter.hasNext()) {
            IProperty property = iter.next();
            if (!property.isBackRef()) {
                if (!property.isComposite()) {
                    boolean typeIsAccessible = true;
                    IValue propertyValue = property.getValue();
                    if (propertyValue.isSimpleValue() && propertyValue.isTypeSpecified()) {
                        try {
                            property.getValue().getType();
                        } catch (Exception e) {
                            typeIsAccessible = false;
                        }
                    }
                    Shape bodyOrmShape = null;
                    if (typeIsAccessible && property.getValue().getType().isEntityType()) {
                        bodyOrmShape = new ExpandableShape(property, getConsoleConfigName());
                    } else if (typeIsAccessible && property.getValue().getType().isCollectionType()) {
                        bodyOrmShape = new ComponentShape(property, getConsoleConfigName());
                    } else {
                        bodyOrmShape = new Shape(property, getConsoleConfigName());
                    }
                    addChild(bodyOrmShape);
                } else {
                    Shape bodyOrmShape = new ExpandableShape(property, getConsoleConfigName());
                    addChild(bodyOrmShape);
                }
            }
        }
    } else if (ormElement instanceof ITable) {
        Iterator iterator = ((ITable) getOrmElement()).getColumnIterator();
        while (iterator.hasNext()) {
            IColumn column = (IColumn) iterator.next();
            Shape bodyOrmShape = new Shape(column, getConsoleConfigName());
            addChild(bodyOrmShape);
        }
    }
}
Also used : IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) IType(org.jboss.tools.hibernate.runtime.spi.IType) IValue(org.jboss.tools.hibernate.runtime.spi.IValue) IProperty(org.jboss.tools.hibernate.runtime.spi.IProperty) IColumn(org.jboss.tools.hibernate.runtime.spi.IColumn) Iterator(java.util.Iterator) ITable(org.jboss.tools.hibernate.runtime.spi.ITable)

Example 4 with IType

use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.

the class OrmImageMap method getImageName.

/**
 * the image name for hierarchy:
 * Property
 * @param field
 * @return
 */
public static String getImageName(IProperty field, final ConsoleConfiguration cfg) {
    // $NON-NLS-1$
    String str = "Image_PersistentFieldSimple";
    if (field == null) {
        return str;
    }
    final IPersistentClass persistentClass = field.getPersistentClass();
    if (persistentClass != null && persistentClass.getVersion() == field) {
        // $NON-NLS-1$
        str = "Image_PersistentFieldSimple_version";
    } else if (persistentClass != null && persistentClass.getIdentifierProperty() == field) {
        // $NON-NLS-1$
        str = "Image_PersistentFieldSimple_id";
    } else if (field.getValue() != null) {
        final IValue value = field.getValue();
        if (value.isOneToMany()) {
            // $NON-NLS-1$
            str = "Image_PersistentFieldOne-to-many";
        } else if (value.isOneToOne()) {
            // $NON-NLS-1$
            str = "Image_PersistentFieldOne-to-one";
        } else if (value.isManyToOne()) {
            // $NON-NLS-1$
            str = "Image_PersistentFieldMany-to-one";
        } else if (value.isAny()) {
            // $NON-NLS-1$
            str = "Image_PersistentFieldAny";
        } else {
            IType type = UtilTypeExtract.getTypeUsingExecContext(value, cfg);
            if (type != null && type.isCollectionType()) {
                if (value.isPrimitiveArray()) {
                    // $NON-NLS-1$
                    str = "Image_Collection_primitive_array";
                } else if (value.isArray()) {
                    // $NON-NLS-1$
                    str = "Image_Collection_array";
                } else if (value.isList()) {
                    // $NON-NLS-1$
                    str = "Image_Collection_list";
                } else if (value.isSet()) {
                    // $NON-NLS-1$
                    str = "Image_Collection_set";
                } else if (value.isMap()) {
                    // $NON-NLS-1$
                    str = "Image_Collection_map";
                } else if (value.isBag()) {
                    // $NON-NLS-1$
                    str = "Image_Collection_bag";
                } else if (value.isIdentifierBag()) {
                    // $NON-NLS-1$
                    str = "Image_Collection_idbag";
                } else {
                    // $NON-NLS-1$
                    str = "Image_Collection";
                }
            }
        }
    } else if ("parent".equals(field.getName())) {
        // $NON-NLS-1$
        // $NON-NLS-1$
        str = "Image_PersistentFieldParent";
    }
    return str;
}
Also used : IValue(org.jboss.tools.hibernate.runtime.spi.IValue) IPersistentClass(org.jboss.tools.hibernate.runtime.spi.IPersistentClass) IType(org.jboss.tools.hibernate.runtime.spi.IType)

Example 5 with IType

use of org.jboss.tools.hibernate.runtime.spi.IType in project jbosstools-hibernate by jbosstools.

the class QueryFacadeTest method testSetParameter.

@Test
public void testSetParameter() {
    Type typeProxy = (Type) Proxy.newProxyInstance(FACADE_FACTORY.getClassLoader(), new Class[] { Type.class }, new TypeInvocationHandler());
    IType typeFacade = FACADE_FACTORY.createType(typeProxy);
    Object object = new Object();
    query.setParameter(Integer.MAX_VALUE, object, typeFacade);
    Assert.assertEquals("setParameter", methodName);
    Assert.assertArrayEquals(new Object[] { Integer.MAX_VALUE, object, typeProxy }, arguments);
    methodName = null;
    arguments = null;
    query.setParameter("foobar", object, typeFacade);
    Assert.assertEquals("setParameter", methodName);
    Assert.assertArrayEquals(new Object[] { "foobar", object, typeProxy }, arguments);
}
Also used : IType(org.jboss.tools.hibernate.runtime.spi.IType) Type(org.hibernate.type.Type) IType(org.jboss.tools.hibernate.runtime.spi.IType) Test(org.junit.Test)

Aggregations

IType (org.jboss.tools.hibernate.runtime.spi.IType)372 Test (org.junit.Test)360 Type (org.hibernate.type.Type)208 ClassType (org.hibernate.type.ClassType)120 ArrayType (org.hibernate.type.ArrayType)56 EntityType (org.hibernate.type.EntityType)24 IntegerType (org.hibernate.type.IntegerType)24 ManyToOneType (org.hibernate.type.ManyToOneType)24 RootClass (org.hibernate.mapping.RootClass)16 IPersistentClass (org.jboss.tools.hibernate.runtime.spi.IPersistentClass)12 Component (org.hibernate.mapping.Component)8 PersistentClass (org.hibernate.mapping.PersistentClass)8 SimpleValue (org.hibernate.mapping.SimpleValue)8 POJOClass (org.hibernate.tool.hbm2x.pojo.POJOClass)8 ComponentMetamodel (org.hibernate.tuple.component.ComponentMetamodel)8 AnyType (org.hibernate.type.AnyType)8 BagType (org.hibernate.type.BagType)8 ComponentType (org.hibernate.type.ComponentType)8 OneToOneType (org.hibernate.type.OneToOneType)8 StringType (org.hibernate.type.StringType)8