Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * See the COPYRIGHT.txt file distributed with this work for information
   * regarding copyright ownership.  Some portions may be licensed
   * to Red Hat, Inc. under one or more contributor license agreements.
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  * 02110-1301 USA.
  */
 package org.teiid.olingo;
 
 import java.util.List;
 import java.util.Map;
 
 import  org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType;
 
 public class LocalClient implements Client {
     private static final String BATCH_SIZE = "batch-size"//$NON-NLS-1$
     private static final String SKIPTOKEN_TIME = "skiptoken-cache-time"//$NON-NLS-1$
     static final String INVALID_CHARACTER_REPLACEMENT = "invalid-xml10-character-replacement"//$NON-NLS-1$
 
     private volatile VDBMetaData vdb;
     private final String vdbName;
     private final int vdbVersion;
     private final int batchSize;
     private final long cacheTime;
     private final String connectionString;
     private final Properties connectionProperties = new Properties();
     private final Properties initProperties;
     private TeiidDriver driver = TeiidDriver.getInstance();
 
     public LocalClient(String vdbNameint vdbVersionProperties props) {
         this. = vdbName;
         this. = vdbVersion;
         this. = PropertiesUtils.getIntProperty(props,
                 .);
         this. = PropertiesUtils.getLongProperty(props,
                 300000L);
         StringBuilder sb = new StringBuilder();
         sb.append("jdbc:teiid:").append(this.).append(".").append(this.).append(";"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
         this. = props;
         .put(
                 .."true"); //$NON-NLS-1$
                 props.getProperty(."odata")); //$NON-NLS-1$
         .put(."0"); //$NON-NLS-1$
         this. = sb.toString();
     }
 
     @Override
     public VDBMetaData getVDB() {
         ConnectionImpl connection = null;
         if (this. == null) {
             try {
                 connection = getConnection();
                 LocalServerConnection lsc = (LocalServerConnectionconnection
                         .getServerConnection();
                 VDBMetaData vdb = lsc.getWorkContext().getVDB();
                 if (vdb == null) {
                     throw new TeiidRuntimeException(..gs(
                            ..this.,
                            this.));
                }
                this. = vdb;
            } catch (SQLException e) {
                throw new TeiidRuntimeException(e);
            } finally {
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                    }
                }
            }
        }
        return this.;
    }
    public void setDriver(TeiidDriver driver) {
        this. = driver;
    }
        ConnectionImpl connection = .connect(this.,
                );
        ODBCServerRemoteImpl.setConnectionProperties(connection);
        ODBCServerRemoteImpl
                .setConnectionProperties(connection);
        return connection;
    }
    @Override
    public BaseResponse executeCall(String sqlList<SQLParamparameters,
            SingletonPrimitiveType returnType) {
        ConnectionImpl connection = null;
        /*
         * try { LogManager.logDetail(LogConstants.CTX_ODATA,
         * "Teiid-Query:",sql); //$NON-NLS-1$ connection = getConnection();
         * final CallableStatementImpl stmt = connection.prepareCall(sql);
         * 
         * int i = 1; if (returnType != null && returnType.isSimple()) {
         * stmt.registerOutParameter(i++,
         * JDBCSQLTypeInfo.getSQLType(ODataTypeManager
         * .teiidType(returnType.getFullyQualifiedName()))); }
         * 
         * if (!parameters.isEmpty()) { for (SQLParam param:parameters) {
         * stmt.setObject(i++, param.value, param.sqlType); } }
         * 
         * boolean results = stmt.execute(); if (results) { final ResultSet rs =
         * stmt.getResultSet(); OCollection.Builder resultRows =
         * OCollections.newBuilder
         * ((EdmComplexType)((EdmCollectionType)returnType).getItemType());
         * while (rs.next()) { int idx = 1; List<OProperty<?>> row = new
         * ArrayList<OProperty<?>>(); Iterator<EdmProperty> props =
         * ((EdmComplexType
         * )((EdmCollectionType)returnType).getItemType()).getProperties
         * ().iterator(); while (props.hasNext()) { EdmProperty prop =
         * props.next(); row.add(buildPropery(prop.getName(), prop.getType(),
         * rs.getObject(idx++), invalidCharacterReplacement)); } OComplexObject
         * erow =
         * OComplexObjects.create((EdmComplexType)((EdmCollectionType)returnType
         * ).getItemType(), row); resultRows.add(erow); } String collectionName
         * = returnType.getFullyQualifiedTypeName(); collectionName =
         * collectionName.replace("(", "_"); //$NON-NLS-1$ //$NON-NLS-2$
         * collectionName = collectionName.replace(")", "_"); //$NON-NLS-1$
         * //$NON-NLS-2$ return Responses.collection(resultRows.build(), null,
         * null, null, collectionName); }
         * 
         * if (returnType != null) { Object result = stmt.getObject(1);
         * OProperty prop = buildPropery("return", returnType, result,
         * invalidCharacterReplacement); //$NON-NLS-1$ return
         * Responses.property(prop); } return
         * Responses.simple(EdmSimpleType.INT32, 1); } catch (Exception e) {
         * throw new ServerErrorException(e.getMessage(), e); } finally { if
         * (connection != null) { try { connection.close(); } catch
         * (SQLException e) { } } }
         */
        return null;
    }
    @Override
    public MetadataStore getMetadataStore() {
        return getVDB().getAttachment(TransformationMetadata.class)
                .getMetadataStore();
    }
    @Override
    public void executeSQL(Query queryList<SQLParamparameters,
            boolean countQueryInteger skipOptionInteger topOption,
            final QueryResponse respose) {
        Connection connection = null;
        try {
            boolean cache = this. > 0;
            if (cache) {
                CacheHint hint = new CacheHint();
                hint.setTtl(this.);
                hint.setScope(..);
                query.setCacheHint(hint);
            }
            String sql = query.toString();
            LogManager.logDetail(."Teiid-Query:"sql); //$NON-NLS-1$
            connection = getConnection();
            final PreparedStatement stmt = connection.prepareStatement(sql,
                    cache ? .
                            : .,
                    .);
            if (parameters != null && !parameters.isEmpty()) {
                for (int i = 0; i < parameters.size(); i++) {
                    stmt.setObject(i + 1, parameters.get(i).,
                            parameters.get(i).);
                }
            }
            final ResultSet rs = stmt.executeQuery();
            // skip to the initial position
            int count = 0;
            int skipSize = 0;
            // skip based upon the skip value
            if (countQuery && skipOption != null) {
                skipSize = skipOption;
            }
            // skip based upon the skipToken
            if (skipOption != null) {
                skipSize += skipOption;
            }
            if (skipSize > 0) {
                count += skip(cachersskipSize);
            }
            // determine the number of records to return
            int size = ;
            int top = .;
            if (countQuery && topOption != null) {
                top = topOption;
                size = top;
                if ( > 0) {
                    size = Math.min(size);
                }
            } else if (size < 1) {
                size = .;
            }
            // build the results
            for (int i = 0; i < sizei++) {
                if (!rs.next()) {
                    break;
                }
                count++;
                respose.addRow(rs);
            }
            // set the count
            if (countQuery) {
                if (!cache) {
                    while (rs.next()) {
                        count++;
                    }
                } else {
                    rs.last();
                    count = rs.getRow();
                }
            }
            respose.setCount(count);
            // set the skipToken if needed
            if (cache && respose.size() == this.) {
                long end = skipSize + respose.size();
                if (countQuery) {
                    if (end < Math.min(topcount)) {
                        respose.setNext(end);
                    }
                } else if (rs.next()) {
                    respose.setNext(end);
                    // will force the entry to cache or is effectively a no-op
                    // when already cached
                    rs.last();
                }
            }
        } catch (Exception e) {
            throw new TeiidRuntimeException(e);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                }
            }
        }
    }
    private int skip(boolean cachefinal ResultSet rsint skipSize)
            throws SQLException {
        int skipped = 0;
        if (!cache) {
            for (int i = 0; i < skipSizei++) {
                skipped++;
                if (!rs.next()) {
                    break;
                }
            }
        } else {
            rs.absolute(skipSize);
        }
        return skipped;
    }
    @Override
    public CountResponse executeCount(Query queryList<SQLParamparameters) {
        ConnectionImpl connection = null;
        try {
            String sql = query.toString();
            LogManager.logDetail(."Teiid-Query:"sql); //$NON-NLS-1$
            connection = getConnection();
            final PreparedStatementImpl stmt = connection.prepareStatement(sql);
            if (!parameters.isEmpty()) {
                for (int i = 0; i < parameters.size(); i++) {
                    stmt.setObject(i + 1, parameters.get(i).,
                            parameters.get(i).);
                }
            }
            ResultSet rs = stmt.executeQuery();
            rs.next();
            final int count = rs.getInt(1);
            rs.close();
            stmt.close();
            return new CountResponse() {
                @Override
                public long getCount() {
                    return count;
                }
            };
        } catch (Exception e) {
            throw new TeiidRuntimeException(e);
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }
    }
    @Override
    public UpdateResponse executeUpdate(Command queryList<SQLParamparameters) {
        ConnectionImpl connection = null;
        try {
            String sql = query.toString();
            LogManager.logDetail(."Teiid-Query:"sql); //$NON-NLS-1$
            connection = getConnection();
            final PreparedStatementImpl stmt = connection.prepareStatement(sql,
                    ..,
                    .,
                    .);
            if (!parameters.isEmpty()) {
                for (int i = 0; i < parameters.size(); i++) {
                    stmt.setObject(i + 1, parameters.get(i).,
                            parameters.get(i).);
                }
            }
            final int count = stmt.executeUpdate();
            final Map<StringObjectkeys = getGeneratedKeys(stmt
                    .getGeneratedKeys());
            stmt.close();
            return new UpdateResponse() {
                @Override
                public Map<StringObjectgetGeneratedKeys() {
                    return keys;
                }
                @Override
                public int getUpdateCount() {
                    return count;
                }
            };
        } catch (Exception e) {
            throw new TeiidRuntimeException(e);
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
            }
        }
    }
    private Map<StringObjectgetGeneratedKeys(ResultSet result)
            throws SQLException {
        if (result == null) {
            return null;
        }
        HashMap<StringObjectkeys = new HashMap<StringObject>();
        ResultSetMetaData metadata = result.getMetaData();
        // now read the values
        while (result.next()) {
            for (int i = 0; i < metadata.getColumnCount(); i++) {
                String label = metadata.getColumnLabel(i + 1);
                keys.put(labelresult.getObject(i + 1));
            }
        }
        return keys;
    }
    @Override
    public String getProperty(String name) {
        return this..getProperty(name);
    }
New to GrepCode? Check out our FAQ X