Package org.hibernate.engine.query.spi

Examples of org.hibernate.engine.query.spi.ParameterMetadata


public class Neo4jParameterMetadataBuilderTest {

  @Test
  public void shouldDetectParameterNames() {
    ParameterMetadataBuilder builder = new Neo4jParameterMetadataBuilder();
    ParameterMetadata metadata = builder.buildParameterMetadata(
        "MATCH ( n:Poem { name: {name}, author:{author} } ) RETURN n"
    );

    assertThat( metadata.getNamedParameterNames() ).containsOnly( "name", "author" );
  }
View Full Code Here


  }

  @Test
  public void shouldIgnoreParametersInQuotes() {
    ParameterMetadataBuilder builder = new Neo4jParameterMetadataBuilder();
    ParameterMetadata metadata = builder.buildParameterMetadata(
        "MATCH ( n:Poem { name: {name}, author:{author}, desc:'{desc}' } ) RETURN n"
    );

    assertThat( metadata.getNamedParameterNames() ).containsOnly( "name", "author" );
  }
View Full Code Here

  }

  @Test
  public void shouldIgnoreParametersInEscapedName() {
    ParameterMetadataBuilder builder = new Neo4jParameterMetadataBuilder();
    ParameterMetadata metadata = builder.buildParameterMetadata(
        "MATCH ( n:Poem { `{nameNode}`: {name}, author:{author} } ) RETURN n"
    );

    assertThat( metadata.getNamedParameterNames() ).containsOnly( "name", "author" );
  }
View Full Code Here

  }

  @Test
  public void shouldAllowSameParameterTwice() {
    ParameterMetadataBuilder builder = new Neo4jParameterMetadataBuilder();
    ParameterMetadata metadata = builder.buildParameterMetadata(
        "MATCH ( n:Poem { name: {name}, author:{name} } ) RETURN n"
    );

    assertThat( metadata.getNamedParameterNames() ).containsOnly( "name" );
  }
View Full Code Here

  }

  @Test
  public void shouldAllowWhitespaceInParameterName() {
    ParameterMetadataBuilder builder = new Neo4jParameterMetadataBuilder();
    ParameterMetadata metadata = builder.buildParameterMetadata(
        "MATCH ( n:Poem { name: { name } } ) RETURN n"
    );

    assertThat( metadata.getNamedParameterNames() ).containsOnly( "name" );
  }
View Full Code Here

              description.isJpaStyle()
          )
      );
    }

    return new ParameterMetadata( ordinalDescriptors, namedParamDescriptorMap );
  }
View Full Code Here

  private void extractParameterInfo(Map<String,Class> namedParameterTypeRedefinition) {
    if ( ! org.hibernate.internal.AbstractQueryImpl.class.isInstance( query ) ) {
      throw new IllegalStateException( "Unknown query type for parameter extraction" );
    }

    final ParameterMetadata parameterMetadata = org.hibernate.internal.AbstractQueryImpl.class.cast( query ).getParameterMetadata();

    // extract named params
    for ( String name : (Set<String>) parameterMetadata.getNamedParameterNames() ) {
      final NamedParameterDescriptor descriptor = parameterMetadata.getNamedParameterDescriptor( name );
      Class javaType = namedParameterTypeRedefinition.get( name );
      if ( javaType != null && mightNeedRedefinition( javaType ) ) {
        descriptor.resetExpectedType(
            sfi().getTypeResolver().heuristicType( javaType.getName() )
        );
      }
      else if ( descriptor.getExpectedType() != null ) {
        javaType = descriptor.getExpectedType().getReturnedClass();
      }
      registerParameter( new ParameterRegistrationImpl( query, name, javaType ) );
      if ( descriptor.isJpaStyle() ) {
        if ( jpaPositionalIndices == null ) {
          jpaPositionalIndices = new HashSet<Integer>();
        }
        jpaPositionalIndices.add( Integer.valueOf( name ) );
      }
    }

    // extract positional parameters
    for ( int i = 0, max = parameterMetadata.getOrdinalParameterCount(); i < max; i++ ) {
      final OrdinalParameterDescriptor descriptor = parameterMetadata.getOrdinalParameterDescriptor( i + 1 );
      Class javaType = descriptor.getExpectedType() == null ? null : descriptor.getExpectedType().getReturnedClass();
      registerParameter( new ParameterRegistrationImpl( query, i+1, javaType ) );
      Integer position = descriptor.getOrdinalPosition();
            if ( jpaPositionalIndices != null && jpaPositionalIndices.contains(position) ) {
        LOG.parameterPositionOccurredAsBothJpaAndHibernatePositionalParameter(position);
View Full Code Here

    return query;
  }

  @Override
  public SQLQuery createSQLQuery(NamedSQLQueryDefinition namedQueryDefinition) {
    final ParameterMetadata parameterMetadata = factory.getQueryPlanCache().getSQLParameterMetadata( namedQueryDefinition.getQueryString() );
    final SQLQuery query = new SQLQueryImpl(
        namedQueryDefinition,
        this,
        parameterMetadata
    );
View Full Code Here

  private final MongoDBDatastoreProvider provider;
  private final Class<?> entityType;
  private final DBObject projections;

  public MongoDBQueryImpl(Class<?> entityType, DBObject query, DBObject projections, OgmSession session) {
    super( query.toString(), null, session, new ParameterMetadata( null, null ) );
    this.query = query;
    this.entityType = entityType;
    this.projections = projections;
    this.provider = (MongoDBDatastoreProvider) session.getSessionFactory().getServiceRegistry().getService( DatastoreProvider.class );
  }
View Full Code Here

      throw new IllegalStateException( "Unknown query type for parameter extraction" );
    }

    boolean hadJpaPositionalParameters = false;

    final ParameterMetadata parameterMetadata = org.hibernate.internal.AbstractQueryImpl.class.cast( query ).getParameterMetadata();

    // extract named params
    for ( String name : (Set<String>) parameterMetadata.getNamedParameterNames() ) {
      final NamedParameterDescriptor descriptor = parameterMetadata.getNamedParameterDescriptor( name );
      Class javaType = namedParameterTypeRedefinition.get( name );
      if ( javaType != null && mightNeedRedefinition( javaType, descriptor.getExpectedType() ) ) {
        descriptor.resetExpectedType(
            sfi().getTypeResolver().heuristicType( javaType.getName() )
        );
      }
      else if ( descriptor.getExpectedType() != null ) {
        javaType = descriptor.getExpectedType().getReturnedClass();
      }

      if ( descriptor.isJpaStyle() ) {
        hadJpaPositionalParameters = true;
        final Integer position = Integer.valueOf( name );
        registerParameter( new JpaPositionalParameterRegistrationImpl( this, query, position, javaType ) );
      }
      else {
        registerParameter( new ParameterRegistrationImpl( this, query, name, javaType ) );
      }
    }

    if ( hadJpaPositionalParameters ) {
      if ( parameterMetadata.getOrdinalParameterCount() > 0 ) {
        throw new IllegalArgumentException(
            "Cannot mix JPA positional parameters and native Hibernate positional/ordinal parameters"
        );
      }
    }

    // extract Hibernate native positional parameters
    for ( int i = 0, max = parameterMetadata.getOrdinalParameterCount(); i < max; i++ ) {
      final OrdinalParameterDescriptor descriptor = parameterMetadata.getOrdinalParameterDescriptor( i + 1 );
      Class javaType = descriptor.getExpectedType() == null ? null : descriptor.getExpectedType().getReturnedClass();
      registerParameter( new ParameterRegistrationImpl( this, query, i+1, javaType ) );
    }
  }
View Full Code Here

TOP

Related Classes of org.hibernate.engine.query.spi.ParameterMetadata

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.