Search in sources :

Example 1 with StringMapping

use of org.datanucleus.store.rdbms.mapping.java.StringMapping in project datanucleus-rdbms by datanucleus.

the class QueryToSQLMapper method processVariableExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processVariableExpression(org.datanucleus.query.expression.VariableExpression)
     */
@Override
protected Object processVariableExpression(VariableExpression expr) {
    String varName = expr.getId();
    Symbol varSym = expr.getSymbol();
    if (varSym != null) {
        // Use name from symbol if possible
        varName = varSym.getQualifiedName();
    }
    if (hasSQLTableMappingForAlias(varName)) {
        // Variable already found
        SQLTableMapping tblMapping = getSQLTableMappingForAlias(varName);
        SQLExpression sqlExpr = exprFactory.newExpression(tblMapping.table.getSQLStatement(), tblMapping.table, tblMapping.mapping);
        stack.push(sqlExpr);
        return sqlExpr;
    } else if (compilation.getCompilationForSubquery(varName) != null) {
        // Subquery variable
        QueryCompilation subCompilation = compilation.getCompilationForSubquery(varName);
        AbstractClassMetaData subCmd = ec.getMetaDataManager().getMetaDataForClass(subCompilation.getCandidateClass(), ec.getClassLoaderResolver());
        // Create subquery statement, using any provided alias if possible
        String subAlias = null;
        if (subCompilation.getCandidateAlias() != null && !subCompilation.getCandidateAlias().equals(candidateAlias)) {
            subAlias = subCompilation.getCandidateAlias();
        }
        StatementResultMapping subqueryResultMapping = new StatementResultMapping();
        // TODO Fix "avg(something)" arg - not essential but is a hack right now
        SQLStatement subStmt = RDBMSQueryUtils.getStatementForCandidates(storeMgr, stmt, subCmd, null, ec, subCompilation.getCandidateClass(), true, "avg(something)", subAlias, null, null);
        QueryToSQLMapper sqlMapper = new QueryToSQLMapper(subStmt, subCompilation, parameters, null, subqueryResultMapping, subCmd, true, fetchPlan, ec, importsDefinition, options, extensionsByName);
        sqlMapper.setDefaultJoinType(defaultJoinType);
        sqlMapper.setDefaultJoinTypeFilter(defaultJoinTypeFilter);
        sqlMapper.setParentMapper(this);
        sqlMapper.compile();
        if (subqueryResultMapping.getNumberOfResultExpressions() > 1) {
            throw new NucleusUserException("Number of result expressions in subquery should be 1");
        }
        SQLExpression subExpr = null;
        // TODO Cater for subquery select of its own candidate
        if (subqueryResultMapping.getNumberOfResultExpressions() == 0) {
            subExpr = new org.datanucleus.store.rdbms.sql.expression.SubqueryExpression(stmt, subStmt);
        } else {
            JavaTypeMapping subMapping = ((StatementMappingIndex) subqueryResultMapping.getMappingForResultExpression(0)).getMapping();
            if (subMapping instanceof TemporalMapping) {
                subExpr = new TemporalSubqueryExpression(stmt, subStmt);
            } else if (subMapping instanceof StringMapping) {
                subExpr = new StringSubqueryExpression(stmt, subStmt);
            } else {
                subExpr = new NumericSubqueryExpression(stmt, subStmt);
            }
            if (subExpr.getJavaTypeMapping() == null) {
                subExpr.setJavaTypeMapping(subMapping);
            }
        }
        stack.push(subExpr);
        return subExpr;
    } else if (stmt.getParentStatement() != null && parentMapper != null && parentMapper.candidateAlias != null && parentMapper.candidateAlias.equals(varName)) {
        // Variable in subquery linking back to parent query
        SQLExpression varExpr = exprFactory.newExpression(stmt.getParentStatement(), stmt.getParentStatement().getPrimaryTable(), stmt.getParentStatement().getPrimaryTable().getTable().getIdMapping());
        stack.push(varExpr);
        return varExpr;
    } else {
        // Variable never met before, so return as UnboundExpression - process later if needing binding
        NucleusLogger.QUERY.debug("QueryToSQL.processVariable (unbound) variable=" + varName + " is not yet bound so returning UnboundExpression");
        UnboundExpression unbExpr = new UnboundExpression(stmt, varName);
        stack.push(unbExpr);
        return unbExpr;
    }
}
Also used : SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) Symbol(org.datanucleus.query.compiler.Symbol) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) UnboundExpression(org.datanucleus.store.rdbms.sql.expression.UnboundExpression) StringMapping(org.datanucleus.store.rdbms.mapping.java.StringMapping) SQLStatement(org.datanucleus.store.rdbms.sql.SQLStatement) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) BooleanSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.BooleanSubqueryExpression) NumericSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.NumericSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) SubqueryExpression(org.datanucleus.query.expression.SubqueryExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) TemporalSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.TemporalSubqueryExpression) StringSubqueryExpression(org.datanucleus.store.rdbms.sql.expression.StringSubqueryExpression) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation) TemporalMapping(org.datanucleus.store.rdbms.mapping.java.TemporalMapping)

Example 2 with StringMapping

use of org.datanucleus.store.rdbms.mapping.java.StringMapping in project datanucleus-rdbms by datanucleus.

the class ClassTable method initialize.

/**
 * Method to initialise the table.
 * This adds the columns based on the MetaData representation for the class being represented by this table.
 * @param clr The ClassLoaderResolver
 */
public void initialize(ClassLoaderResolver clr) {
    // if already initialized, we have nothing further to do here
    if (isInitialized()) {
        return;
    }
    // we may inherit from that table are initialized at the point at which we may need them
    if (supertable != null) {
        supertable.initialize(clr);
    }
    // Add the fields for this class (and any other superclasses that we need to manage the
    // fields for (inheritance-strategy="subclass-table" in the superclass)
    initializeForClass(cmd, clr);
    MappingManager mapMgr = storeMgr.getMappingManager();
    // Add Version where specified in MetaData
    // TODO If there is a superclass table that has a version we should omit from here even if in MetaData
    // See "getTableWithDiscriminator()" for the logic
    versionMetaData = cmd.getVersionMetaDataForTable();
    if (versionMetaData != null && versionMetaData.getFieldName() == null) {
        if (versionMetaData.getVersionStrategy() == VersionStrategy.NONE || versionMetaData.getVersionStrategy() == VersionStrategy.VERSION_NUMBER) {
            // No optimistic locking but the idiot wants a column for that :-)
            versionMapping = new VersionMapping.VersionLongMapping(this, mapMgr.getMapping(Long.class));
        } else if (versionMetaData.getVersionStrategy() == VersionStrategy.DATE_TIME) {
            if (!dba.supportsOption(DatastoreAdapter.DATETIME_STORES_MILLISECS)) {
                // TODO Localise this
                throw new NucleusException("Class " + cmd.getFullClassName() + " is defined " + "to use date-time versioning, yet this datastore doesnt support storing " + "milliseconds in DATETIME/TIMESTAMP columns. Use version-number");
            }
            versionMapping = new VersionMapping.VersionTimestampMapping(this, mapMgr.getMapping(Timestamp.class));
        }
        if (versionMapping != null) {
            logMapping("VERSION", versionMapping);
        }
    }
    // Add Discriminator where specified in MetaData
    DiscriminatorMetaData dismd = cmd.getDiscriminatorMetaDataForTable();
    if (dismd != null) {
        discriminatorMetaData = dismd;
        if (storeMgr.getBooleanProperty(RDBMSPropertyNames.PROPERTY_RDBMS_DISCRIM_PER_SUBCLASS_TABLE)) {
            // Backwards compatibility only. Creates discriminator in all subclass tables even though not needed
            // TODO Remove this in the future
            discriminatorMapping = DiscriminatorMapping.createDiscriminatorMapping(this, dismd);
        } else {
            // Create discriminator column only in top most table that needs it
            ClassTable tableWithDiscrim = getTableWithDiscriminator();
            if (tableWithDiscrim == this) {
                // No superclass with a discriminator so add it in this table
                discriminatorMapping = DiscriminatorMapping.createDiscriminatorMapping(this, dismd);
            }
        }
        if (discriminatorMapping != null) {
            logMapping("DISCRIMINATOR", discriminatorMapping);
        }
    }
    // TODO Only put on root table (i.e "if (supertable != null)" then omit)
    if (storeMgr.getNucleusContext().isClassMultiTenant(cmd)) {
        ColumnMetaData colmd = new ColumnMetaData();
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_COLUMN_NAME)) {
            colmd.setName(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_COLUMN_NAME));
        }
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_JDBC_TYPE)) {
            colmd.setJdbcType(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_JDBC_TYPE));
        }
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_COLUMN_LENGTH)) {
            colmd.setLength(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_MULTITENANCY_COLUMN_LENGTH));
        }
        String colName = (colmd.getName() != null) ? colmd.getName() : "TENANT_ID";
        String typeName = (colmd.getJdbcType() == JdbcType.INTEGER) ? Integer.class.getName() : String.class.getName();
        multitenancyMapping = (typeName.equals(Integer.class.getName())) ? new IntegerMapping() : new StringMapping();
        multitenancyMapping.setTable(this);
        multitenancyMapping.initialize(storeMgr, typeName);
        Column tenantColumn = addColumn(typeName, storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colName), multitenancyMapping, colmd);
        storeMgr.getMappingManager().createDatastoreMapping(multitenancyMapping, tenantColumn, typeName);
        logMapping("MULTITENANCY", multitenancyMapping);
    }
    if (cmd.hasExtension(MetaData.EXTENSION_CLASS_SOFTDELETE)) {
        // SoftDelete flag column
        ColumnMetaData colmd = new ColumnMetaData();
        if (cmd.hasExtension(MetaData.EXTENSION_CLASS_SOFTDELETE_COLUMN_NAME)) {
            colmd.setName(cmd.getValueForExtension(MetaData.EXTENSION_CLASS_SOFTDELETE_COLUMN_NAME));
        }
        String colName = (colmd.getName() != null) ? colmd.getName() : "DELETED";
        // TODO Allow integer?
        String typeName = Boolean.class.getName();
        softDeleteMapping = new BooleanMapping();
        softDeleteMapping.setTable(this);
        softDeleteMapping.initialize(storeMgr, typeName);
        Column tenantColumn = addColumn(typeName, storeMgr.getIdentifierFactory().newIdentifier(IdentifierType.COLUMN, colName), softDeleteMapping, colmd);
        storeMgr.getMappingManager().createDatastoreMapping(softDeleteMapping, tenantColumn, typeName);
        logMapping("SOFTDELETE", softDeleteMapping);
    }
    // Initialise any SecondaryTables
    if (secondaryTables != null) {
        Iterator<Map.Entry<String, SecondaryTable>> secondaryTableEntryIter = secondaryTables.entrySet().iterator();
        while (secondaryTableEntryIter.hasNext()) {
            Map.Entry<String, SecondaryTable> secondaryTableEntry = secondaryTableEntryIter.next();
            SecondaryTable second = secondaryTableEntry.getValue();
            if (!second.isInitialized()) {
                second.initialize(clr);
            }
        }
    }
    if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
        NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("057023", this));
    }
    storeMgr.registerTableInitialized(this);
    state = TABLE_STATE_INITIALIZED;
}
Also used : VersionMapping(org.datanucleus.store.rdbms.mapping.java.VersionMapping) MacroString(org.datanucleus.util.MacroString) IntegerMapping(org.datanucleus.store.rdbms.mapping.java.IntegerMapping) StringMapping(org.datanucleus.store.rdbms.mapping.java.StringMapping) BooleanMapping(org.datanucleus.store.rdbms.mapping.java.BooleanMapping) Timestamp(java.sql.Timestamp) DiscriminatorMetaData(org.datanucleus.metadata.DiscriminatorMetaData) MappingManager(org.datanucleus.store.rdbms.mapping.MappingManager) NucleusException(org.datanucleus.exceptions.NucleusException) ColumnMetaData(org.datanucleus.metadata.ColumnMetaData) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with StringMapping

use of org.datanucleus.store.rdbms.mapping.java.StringMapping in project datanucleus-rdbms by datanucleus.

the class OptionalOrElseMethod method getExpression.

/* (non-Javadoc)
     * @see org.datanucleus.store.rdbms.sql.method.SQLMethod#getExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression, java.util.List)
     */
public SQLExpression getExpression(SQLStatement stmt, SQLExpression expr, List<SQLExpression> args) {
    if (args == null || args.size() != 1) {
        throw new NucleusException("Optional.orElse should be passed 1 argument");
    }
    SQLExpression elseExpr = args.get(0);
    SQLExpressionFactory exprFactory = stmt.getSQLExpressionFactory();
    OptionalMapping opMapping = (OptionalMapping) ((OptionalExpression) expr).getJavaTypeMapping();
    JavaTypeMapping javaMapping = opMapping.getWrappedMapping();
    SQLExpression getExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), javaMapping);
    SQLExpression isNotNullExpr = exprFactory.newExpression(stmt, expr.getSQLTable(), javaMapping).ne(new NullLiteral(stmt, javaMapping, null, null));
    if (javaMapping instanceof StringMapping) {
        return new CaseStringExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
    } else if (javaMapping instanceof IntegerMapping || javaMapping instanceof LongMapping || javaMapping instanceof ShortMapping || javaMapping instanceof FloatMapping || javaMapping instanceof DoubleMapping || javaMapping instanceof BigIntegerMapping || javaMapping instanceof BigDecimalMapping) // TODO Maybe use javaMapping.getJavaType compared to Number to avoid the check above
    {
        return new CaseNumericExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
    } else if (javaMapping instanceof BooleanMapping) {
        return new CaseBooleanExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
    }
    return new CaseExpression(new SQLExpression[] { isNotNullExpr }, new SQLExpression[] { getExpr }, elseExpr);
}
Also used : SQLExpressionFactory(org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory) SQLExpression(org.datanucleus.store.rdbms.sql.expression.SQLExpression) BigDecimalMapping(org.datanucleus.store.rdbms.mapping.java.BigDecimalMapping) JavaTypeMapping(org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping) CaseNumericExpression(org.datanucleus.store.rdbms.sql.expression.CaseNumericExpression) StringMapping(org.datanucleus.store.rdbms.mapping.java.StringMapping) BigIntegerMapping(org.datanucleus.store.rdbms.mapping.java.BigIntegerMapping) IntegerMapping(org.datanucleus.store.rdbms.mapping.java.IntegerMapping) BooleanMapping(org.datanucleus.store.rdbms.mapping.java.BooleanMapping) DoubleMapping(org.datanucleus.store.rdbms.mapping.java.DoubleMapping) OptionalMapping(org.datanucleus.store.rdbms.mapping.java.OptionalMapping) ShortMapping(org.datanucleus.store.rdbms.mapping.java.ShortMapping) CaseExpression(org.datanucleus.store.rdbms.sql.expression.CaseExpression) CaseStringExpression(org.datanucleus.store.rdbms.sql.expression.CaseStringExpression) LongMapping(org.datanucleus.store.rdbms.mapping.java.LongMapping) BigIntegerMapping(org.datanucleus.store.rdbms.mapping.java.BigIntegerMapping) FloatMapping(org.datanucleus.store.rdbms.mapping.java.FloatMapping) CaseBooleanExpression(org.datanucleus.store.rdbms.sql.expression.CaseBooleanExpression) NucleusException(org.datanucleus.exceptions.NucleusException) NullLiteral(org.datanucleus.store.rdbms.sql.expression.NullLiteral)

Aggregations

StringMapping (org.datanucleus.store.rdbms.mapping.java.StringMapping)3 NucleusException (org.datanucleus.exceptions.NucleusException)2 BooleanMapping (org.datanucleus.store.rdbms.mapping.java.BooleanMapping)2 IntegerMapping (org.datanucleus.store.rdbms.mapping.java.IntegerMapping)2 JavaTypeMapping (org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping)2 SQLExpression (org.datanucleus.store.rdbms.sql.expression.SQLExpression)2 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)1 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)1 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)1 DiscriminatorMetaData (org.datanucleus.metadata.DiscriminatorMetaData)1 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)1 Symbol (org.datanucleus.query.compiler.Symbol)1 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)1 MappingManager (org.datanucleus.store.rdbms.mapping.MappingManager)1 BigDecimalMapping (org.datanucleus.store.rdbms.mapping.java.BigDecimalMapping)1 BigIntegerMapping (org.datanucleus.store.rdbms.mapping.java.BigIntegerMapping)1 DoubleMapping (org.datanucleus.store.rdbms.mapping.java.DoubleMapping)1