Search in sources :

Example 1 with ProcMetaCollection

use of lucee.runtime.db.ProcMetaCollection in project Lucee by lucee.

the class StoredProc method returnValue.

private void returnValue(DatasourceConnection dc) throws PageException {
    Connection conn = dc.getConnection();
    if (SQLUtil.isOracle(conn)) {
        String name = this.procedure.toUpperCase().trim();
        // split procedure definition
        String catalog = null, scheme = null;
        {
            int index = name.lastIndexOf('.');
            if (index != -1) {
                catalog = name.substring(0, index).trim();
                name = name.substring(index + 1).trim();
                index = catalog.lastIndexOf('.');
                if (index != -1) {
                    scheme = catalog.substring(0, index).trim();
                    catalog = catalog.substring(index + 1).trim();
                // scheme=catalog.substring(index+1);
                // catalog=catalog.substring(0,index);
                }
            }
            if (StringUtil.isEmpty(scheme))
                scheme = null;
            if (StringUtil.isEmpty(catalog))
                catalog = null;
        }
        try {
            // if(procedureColumnCache==null)procedureColumnCache=new ReferenceMap();
            // ProcMetaCollection coll=procedureColumnCache.get(procedure);
            DataSourceSupport d = ((DataSourceSupport) dc.getDatasource());
            long cacheTimeout = d.getMetaCacheTimeout();
            Map<String, ProcMetaCollection> procedureColumnCache = d.getProcedureColumnCache();
            String id = procedure.toLowerCase();
            ProcMetaCollection coll = procedureColumnCache.get(id);
            if (coll == null || (cacheTimeout >= 0 && (coll.created + cacheTimeout) < System.currentTimeMillis())) {
                DatabaseMetaData md = conn.getMetaData();
                String _catalog = null, _scheme = null, _name = null;
                boolean available = false;
                /*
					 * print.e("pro:"+procedure); print.e("cat:"+catalog); print.e("sch:"+scheme); print.e("nam:"+name);
					 */
                ResultSet proc = md.getProcedures(null, null, name);
                try {
                    while (proc.next()) {
                        _catalog = proc.getString(1);
                        _scheme = proc.getString(2);
                        _name = proc.getString(3);
                        if (_name.equalsIgnoreCase(name) && // second option is very unlikely to ever been the case, but does not hurt to test
                        (catalog == null || _catalog == null || catalog.equalsIgnoreCase(_catalog)) && // second option is very unlikely to ever been the case, but does not hurt to test
                        (scheme == null || _scheme == null || scheme.equalsIgnoreCase(_scheme))) {
                            available = true;
                            break;
                        } else {
                            Log log = pageContext.getConfig().getLog("datasource");
                            if (// log entry added to troubleshoot LDEV-1147
                            log.getLogLevel() >= Log.LEVEL_DEBUG)
                                log.debug("LDEV1147", String.format("name=[%s] scheme=[%s] catalog=[%s] _name=[%s] _scheme=[%s] _catalog=[%s]", name, scheme, catalog, _name, _scheme, _catalog));
                        }
                    }
                } finally {
                    IOUtil.closeEL(proc);
                }
                if (available) {
                    /*
						 * print.e("---------------"); print.e("_pro:"+procedure); print.e("_cat:"+_catalog); print.e("_sch:"+_scheme); print.e("_nam:"+_name);
						 */
                    ResultSet res = md.getProcedureColumns(_catalog, _scheme, _name, "%");
                    coll = createProcMetaCollection(res);
                    procedureColumnCache.put(id, coll);
                }
            }
            int index = -1;
            int ct;
            if (coll != null) {
                Iterator<ProcMeta> it = coll.metas.iterator();
                ProcMeta pm;
                while (it.hasNext()) {
                    index++;
                    pm = it.next();
                    ct = pm.columnType;
                    // Return
                    if (ct == DatabaseMetaData.procedureColumnReturn) {
                        index--;
                        ProcResultBean result = getFirstResult();
                        ProcParamBean param = new ProcParamBean();
                        param.setType(pm.dataType);
                        param.setDirection(ProcParamBean.DIRECTION_OUT);
                        if (result != null)
                            param.setVariable(result.getName());
                        returnValue = param;
                    } else if (ct == DatabaseMetaData.procedureColumnOut || ct == DatabaseMetaData.procedureColumnInOut) {
                        // review of the code: seems to add an addional column in this case
                        if (pm.dataType == CFTypes.CURSOR) {
                            ProcResultBean result = getFirstResult();
                            ProcParamBean param = new ProcParamBean();
                            param.setType(pm.dataType);
                            param.setDirection(ProcParamBean.DIRECTION_OUT);
                            if (result != null)
                                param.setVariable(result.getName());
                            if (params.size() < index)
                                throw new DatabaseException("you have only defined [" + params.size() + "] procparam tags, but the procedure/function called is expecting more", null, null, dc);
                            else if (params.size() == index)
                                params.add(param);
                            else
                                params.add(index, param);
                        } else {
                            ProcParamBean param = null;
                            if (params.size() > index)
                                param = params.get(index);
                            if (param != null && pm.dataType != Types.OTHER && pm.dataType != param.getType()) {
                                param.setType(pm.dataType);
                            }
                        }
                    } else if (ct == DatabaseMetaData.procedureColumnIn) {
                        ProcParamBean param = get(params, index);
                        if (param != null && pm.dataType != Types.OTHER && pm.dataType != param.getType()) {
                            param.setType(pm.dataType);
                        }
                    }
                }
            }
            contractTo(params, index + 1);
        // if(res!=null)print.out(new QueryImpl(res,"columns").toString());
        } catch (SQLException e) {
            throw new DatabaseException(e, dc);
        }
    }
    // return code
    if (returncode) {
        returnValue = STATUS_CODE;
    }
}
Also used : DataSourceSupport(lucee.runtime.db.DataSourceSupport) Log(lucee.commons.io.log.Log) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatasourceConnection(lucee.runtime.db.DatasourceConnection) DatabaseMetaData(java.sql.DatabaseMetaData) ProcMetaCollection(lucee.runtime.db.ProcMetaCollection) ResultSet(java.sql.ResultSet) DatabaseException(lucee.runtime.exp.DatabaseException) ProcMeta(lucee.runtime.db.ProcMeta)

Aggregations

Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Log (lucee.commons.io.log.Log)1 DataSourceSupport (lucee.runtime.db.DataSourceSupport)1 DatasourceConnection (lucee.runtime.db.DatasourceConnection)1 ProcMeta (lucee.runtime.db.ProcMeta)1 ProcMetaCollection (lucee.runtime.db.ProcMetaCollection)1 DatabaseException (lucee.runtime.exp.DatabaseException)1