Package org.pentaho.platform.plugin.services.connections.sql

Examples of org.pentaho.platform.plugin.services.connections.sql.SQLConnection


    return result;
  }

  @Override
  protected boolean runQuery( final String rawQuery, final boolean live ) {
    SQLConnection conn = (SQLConnection) connection;
    return runSqlQuery( conn, rawQuery, live );
  }
View Full Code Here


    }
  }

  protected SQLConnection getConnection( final String jndiName, final String driver, final String userId,
                                         final String password, final String connectionInfo ) {
    SQLConnection connection = null;
    try {
      if ( jndiName != null ) {
        connection =
          (SQLConnection) PentahoConnectionFactory.getConnection( IPentahoConnection.SQL_DATASOURCE, jndiName,
            getSession(), this );
View Full Code Here

        error( Messages.getInstance().getErrorString( "SQLBaseComponent.ERROR_0007_NO_CONNECTION" ) ); //$NON-NLS-1$
        return false;
      }

      String query = applyInputsToFormat( rawQuery );
      SQLConnection sqlConnection = null;
      if ( ( connection instanceof SQLConnection ) ) {
        sqlConnection = (SQLConnection) connection;
      }
      // Some of the following Added by Arijit Chatterjee passing the timeout value to SQLConnection class
      if ( sqlConnection != null ) {
        if ( this.getQueryTimeout() >= 0 ) {
          sqlConnection.setQueryTimeout( this.getQueryTimeout() );
        }
        if ( this.getMaxRows() >= 0 ) {
          sqlConnection.setMaxRows( this.getMaxRows() );
        }
        if ( this.getReadOnly() ) {
          sqlConnection.setReadOnly( true );
        }
      }

      AbstractRelationalDbAction relationalDbAction = (AbstractRelationalDbAction) getActionDefinition();

      IPentahoResultSet resultSet = null;
      boolean isForwardOnly = relationalDbAction.getUseForwardOnlyResultSet().getBooleanValue( false );

      resultSet = doQuery( sqlConnection, query, isForwardOnly );

      if ( sqlConnection.isForcedForwardOnly() ) {
        isForwardOnly = true;
        live = false;
        warn( Messages.getInstance().getString( "SQLBaseComponent.WARN_FALL_BACK_TO_NONSCROLLABLE" ) ); //$NON-NLS-1$
      }
View Full Code Here

  @Test
  public void testRunQuery() {

    java.sql.Connection connection = new MockNativeConnection();
    SQLConnection sqlConnection = new MockSQLConnection( connection );
    MockSQLBaseComponent component = new MockSQLBaseComponent( sqlConnection );

    Assert.assertTrue( component.runQuery() );
  }
View Full Code Here

    DatabaseMeta databaseMeta = ThinModelConverter.convertToLegacy( sqlModel.getId(), sqlModel.getDatasource() );
    // this connection needs closed
    boolean closeConnection = true;

    DatabaseMeta activeDatabaseMeta = getActiveDatabaseMeta( databaseMeta );
    SQLConnection sqlConnection = getConnection( activeDatabaseMeta );
    String sql = null;
    try {
      if ( ( sqlConnection == null ) || !sqlConnection.initialized() ) {
        logger.error( Messages.getInstance().getErrorString( "SQLBaseComponent.ERROR_0007_NO_CONNECTION" ) ); //$NON-NLS-1$
        // TODO: throw an exception up the stack.
        return null;
      }

      // Make sure all parameters are of the correct type.
      // Fix for PDB-1753
      for ( Parameter param : queryObject.getParameters() ) {
        String pName = param.getName();
        if ( parameters.containsKey( pName ) && !parameters.get( pName ).getClass().isArray() ) {
          parameters.put( pName, this.convertParameterValue( param, parameters.get( pName ) ) );
        }
      }

      MappedQuery mappedQuery = null;
      try {
        SqlGenerator sqlGenerator = createSqlGenerator();
        mappedQuery =
            sqlGenerator.generateSql( queryObject, LocaleHelper.getLocale().toString(), getMetadataDomainRepository(),
              activeDatabaseMeta, parameters, true );
      } catch ( Exception e ) {
        throw new RuntimeException( e.getLocalizedMessage(), e );
      }

      Integer timeout = getTimeout();
      if ( timeout != null && timeout >= 0 ) {
        sqlConnection.setQueryTimeout( timeout );
      }

      Integer maxRows = getMaxRows();
      if ( maxRows != null && maxRows >= 0 ) {
        sqlConnection.setMaxRows( maxRows );
      }

      Boolean readOnly = isReadOnly();
      if ( readOnly != null && readOnly.booleanValue() ) {
        sqlConnection.setReadOnly( true );
      }

      IPentahoResultSet localResultSet = null;
      sql = mappedQuery.getQuery();
      if ( logger.isDebugEnabled() ) {
        logger.debug( "SQL: " + sql ); //$NON-NLS-1$
      }
      if ( getDoQueryLog() ) {
        logger.info( "SQL: " + sql ); //$NON-NLS-1$
      }

      // populate prepared sql params
      List<Object> sqlParams = null;
      if ( mappedQuery.getParamList() != null ) {
        sqlParams = new ArrayList<Object>();
        for ( String param : mappedQuery.getParamList() ) {
          Object sqlParam = parameters.get( param );
          // lets see if the parameter is a multi valued param
          if ( sqlParam instanceof Object[] ) {
            Object[] multivaluedParamValues = (Object[]) sqlParam;
            for ( Object p : multivaluedParamValues ) {
              sqlParams.add( p );
            }
          } else {
            sqlParams.add( sqlParam );
          }
        }
      }

      try {
        if ( !isForwardOnly() ) {
          if ( sqlParams != null ) {
            localResultSet = sqlConnection.prepareAndExecuteQuery( sql, sqlParams );
          } else {
            localResultSet = sqlConnection.executeQuery( sql );
          }
        } else {
          if ( sqlParams != null ) {
            localResultSet =
                sqlConnection.prepareAndExecuteQuery( sql, sqlParams, SQLConnection.RESULTSET_FORWARDONLY,
                    SQLConnection.CONCUR_READONLY );
          } else {
            localResultSet =
                sqlConnection.executeQuery( sql, SQLConnection.RESULTSET_FORWARDONLY, SQLConnection.CONCUR_READONLY );
          }
        }
        IPentahoMetaData metadata = mappedQuery.generateMetadata( localResultSet.getMetaData() );
        ( (SQLResultSet) localResultSet ).setMetaData( metadata );
        closeConnection = false;

      } catch ( Exception e ) {
        logger.error( Messages.getInstance().getErrorString(
          "SqlMetadataQueryExec.ERROR_0002_ERROR_EXECUTING_QUERY", e.getLocalizedMessage(), sql ) ); //$NON-NLS-1$
        logger.debug( "error", e ); //$NON-NLS-1$
        return null;
      }

      return localResultSet;
    } finally {
      if ( closeConnection && sqlConnection != null ) {
        sqlConnection.close();
      }
    }

  }
View Full Code Here

      return databaseMeta;
    }

    // retrieve a temporary connection to determine if a dialect change is necessary
    // for generating the MQL Query.
    SQLConnection tempConnection = getConnection( databaseMeta );
    try {

      // if the connection type is not of the current dialect, regenerate the query
      DatabaseInterface di = getDatabaseInterface( tempConnection );

      if ( ( di != null ) && ( !databaseMeta.getPluginId().equals( di.getPluginId() ) ) ) {
        // we need to reinitialize our mqlQuery object and reset the query.
        // note that using this di object wipes out connection info
        DatabaseMeta meta = (DatabaseMeta) databaseMeta.clone();
        DatabaseInterface di2 = (DatabaseInterface) di.clone();
        di2.setAccessType( databaseMeta.getAccessType() );
        di2.setDatabaseName( databaseMeta.getDatabaseName() );
        di2.setAttributes( databaseMeta.getAttributes() );
        di2.setUsername( databaseMeta.getUsername() );
        di2.setPassword( databaseMeta.getPassword() );
        di2.setHostname( databaseMeta.getHostname() );
        meta.setDatabaseInterface( di2 );
        return meta;
      } else {
        return databaseMeta;
      }
    } finally {
      if ( tempConnection != null ) {
        tempConnection.close();
      }
    }

  }
View Full Code Here

  }

  protected SQLConnection getConnection( DatabaseMeta databaseMeta ) {
    // use the connection specified in the query
    SQLConnection localConnection = null;
    try {
      IPentahoSession session = PentahoSessionHolder.getSession();
      if ( databaseMeta.getAccessType() == DatabaseMeta.TYPE_ACCESS_JNDI ) {
        String jndiName = databaseMeta.getDatabaseName();
        if ( jndiName != null ) {
View Full Code Here

TOP

Related Classes of org.pentaho.platform.plugin.services.connections.sql.SQLConnection

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.