Search in sources :

Example 11 with QueryImpl

use of lucee.runtime.type.QueryImpl in project Lucee by lucee.

the class Directory method actionList.

/**
 * list all files and directories inside a directory
 * @throws PageException
 */
public static Object actionList(PageContext pageContext, Resource directory, String serverPassword, int type, ResourceFilter filter, int listInfo, boolean recurse, String sort) throws PageException {
    // check directory
    SecurityManager securityManager = pageContext.getConfig().getSecurityManager();
    securityManager.checkFileLocation(pageContext.getConfig(), directory, serverPassword);
    if (type != TYPE_ALL) {
        ResourceFilter typeFilter = (type == TYPE_DIR) ? DIRECTORY_FILTER : FILE_FILTER;
        if (filter == null)
            filter = typeFilter;
        else
            filter = new AndResourceFilter(new ResourceFilter[] { typeFilter, filter });
    }
    // create query Object
    String[] names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory" };
    String[] types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR" };
    boolean hasMeta = directory instanceof ResourceMetaData;
    if (hasMeta) {
        names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory", "meta" };
        types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR", "OBJECT" };
    }
    Array array = null;
    Query query = null;
    Object rtn;
    if (listInfo == LIST_INFO_QUERY_ALL || listInfo == LIST_INFO_QUERY_NAME) {
        boolean listOnlyNames = listInfo == LIST_INFO_QUERY_NAME;
        rtn = query = new QueryImpl(listOnlyNames ? new String[] { "name" } : names, listOnlyNames ? new String[] { "VARCHAR" } : types, 0, "query");
    } else
        rtn = array = new ArrayImpl();
    if (!directory.exists()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("directory [" + directory.toString() + "] doesn't exist");
    }
    if (!directory.isDirectory()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("file [" + directory.toString() + "] exists, but isn't a directory");
    }
    if (!directory.isReadable()) {
        if (directory instanceof FileResource)
            return rtn;
        throw new ApplicationException("no access to read directory [" + directory.toString() + "]");
    }
    long startNS = System.nanoTime();
    try {
        // Query All
        if (listInfo == LIST_INFO_QUERY_ALL)
            _fillQueryAll(query, directory, filter, 0, hasMeta, recurse);
        else // Query Name
        if (listInfo == LIST_INFO_QUERY_NAME) {
            if (recurse || type != TYPE_ALL)
                _fillQueryNamesRec("", query, directory, filter, 0, recurse);
            else
                _fillQueryNames(query, directory, filter, 0);
        } else // Array Name/Path
        if (listInfo == LIST_INFO_ARRAY_NAME || listInfo == LIST_INFO_ARRAY_PATH) {
            boolean onlyName = listInfo == LIST_INFO_ARRAY_NAME;
            if (// QueryNamesRec("",query, directory, filter, 0,recurse);
            !onlyName || recurse || type != TYPE_ALL)
                // QueryNamesRec("",query, directory, filter, 0,recurse);
                _fillArrayPathOrName(array, directory, filter, 0, recurse, onlyName);
            else
                _fillArrayName(array, directory, filter, 0);
        }
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
    // sort
    if (sort != null && query != null) {
        String[] arr = sort.toLowerCase().split(",");
        for (int i = arr.length - 1; i >= 0; i--) {
            try {
                String[] col = arr[i].trim().split("\\s+");
                if (col.length == 1)
                    query.sort(col[0].trim());
                else if (col.length == 2) {
                    String order = col[1].toLowerCase().trim();
                    if (order.equals("asc"))
                        query.sort(col[0], lucee.runtime.type.Query.ORDER_ASC);
                    else if (order.equals("desc"))
                        query.sort(col[0], lucee.runtime.type.Query.ORDER_DESC);
                    else
                        throw new ApplicationException("invalid order type [" + col[1] + "]");
                }
            } catch (Throwable t) {
                ExceptionUtil.rethrowIfNecessary(t);
            }
        }
    }
    if (query != null)
        query.setExecutionTime(System.nanoTime() - startNS);
    return rtn;
}
Also used : SecurityManager(lucee.runtime.security.SecurityManager) Query(lucee.runtime.type.Query) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) ArrayImpl(lucee.runtime.type.ArrayImpl) FileResource(lucee.commons.io.res.type.file.FileResource) IOException(java.io.IOException) Array(lucee.runtime.type.Array) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) FileResourceFilter(lucee.commons.io.res.filter.FileResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) QueryImpl(lucee.runtime.type.QueryImpl) ApplicationException(lucee.runtime.exp.ApplicationException) ResourceMetaData(lucee.commons.io.res.ResourceMetaData)

Example 12 with QueryImpl

use of lucee.runtime.type.QueryImpl in project Lucee by lucee.

the class Ftp method toQuery.

public static lucee.runtime.type.Query toQuery(FTPFile[] files, String prefix, String directory, String hostName) throws PageException {
    String[] cols = new String[] { "name", "isdirectory", "lastmodified", "length", "mode", "path", "url", "type", "raw", "attributes" };
    String[] types = new String[] { "VARCHAR", "BOOLEAN", "DATE", "DOUBLE", "VARCHAR", "VARCHAR", "VARCHAR", "VARCHAR", "VARCHAR", "VARCHAR" };
    lucee.runtime.type.Query query = new QueryImpl(cols, types, 0, "query");
    // translate directory path for display
    if (directory.length() == 0)
        directory = "/";
    else if (directory.startsWith("./"))
        directory = directory.substring(1);
    else if (directory.charAt(0) != '/')
        directory = '/' + directory;
    if (directory.charAt(directory.length() - 1) != '/')
        directory = directory + '/';
    int row;
    for (int i = 0; i < files.length; i++) {
        FTPFile file = files[i];
        if (file.getName().equals(".") || file.getName().equals(".."))
            continue;
        row = query.addRow();
        query.setAt("attributes", row, "");
        query.setAt("isdirectory", row, Caster.toBoolean(file.isDirectory()));
        query.setAt("lastmodified", row, new DateTimeImpl(file.getTimestamp()));
        query.setAt("length", row, Caster.toDouble(file.getSize()));
        query.setAt("mode", row, FTPConstant.getPermissionASInteger(file));
        query.setAt("type", row, FTPConstant.getTypeAsString(file.getType()));
        // query.setAt("permission",row,FTPConstant.getPermissionASInteger(file));
        query.setAt("raw", row, file.getRawListing());
        query.setAt("name", row, file.getName());
        query.setAt("path", row, directory + file.getName());
        query.setAt("url", row, prefix + "://" + hostName + "" + directory + file.getName());
    }
    return query;
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) FTPFile(org.apache.commons.net.ftp.FTPFile)

Example 13 with QueryImpl

use of lucee.runtime.type.QueryImpl in project Lucee by lucee.

the class Update method doEndTag.

@Override
public int doEndTag() throws PageException {
    Object ds = DBInfo.getDatasource(pageContext, datasource);
    DataSourceManager manager = pageContext.getDataSourceManager();
    DatasourceConnection dc = ds instanceof DataSource ? manager.getConnection(pageContext, (DataSource) ds, username, password) : manager.getConnection(pageContext, Caster.toString(ds), username, password);
    try {
        Struct meta = null;
        try {
            meta = Insert.getMeta(dc, tablequalifier, tableowner, tablename);
        } catch (SQLException se) {
            meta = new StructImpl();
        }
        String[] pKeys = getPrimaryKeys(dc);
        SQL sql = createSQL(dc, pKeys, meta);
        if (sql != null) {
            lucee.runtime.type.Query query = new QueryImpl(pageContext, dc, sql, -1, -1, null, "query");
            if (pageContext.getConfig().debug()) {
                String dsn = ds instanceof DataSource ? ((DataSource) ds).getName() : Caster.toString(ds);
                boolean logdb = ((ConfigImpl) pageContext.getConfig()).hasDebugOptions(ConfigImpl.DEBUG_DATABASE);
                if (logdb) {
                    boolean debugUsage = DebuggerUtil.debugQueryUsage(pageContext, query);
                    pageContext.getDebugger().addQuery(debugUsage ? query : null, dsn, "", sql, query.getRecordcount(), pageContext.getCurrentPageSource(), query.getExecutionTime());
                }
            }
            // log
            Log log = pageContext.getConfig().getLog("datasource");
            if (log.getLogLevel() >= Log.LEVEL_INFO) {
                log.info("update tag", "executed [" + sql.toString().trim() + "] in " + DecimalFormat.call(pageContext, query.getExecutionTime() / 1000000D) + " ms");
            }
        }
        return EVAL_PAGE;
    } catch (PageException pe) {
        pageContext.getConfig().getLog("datasource").error("update tag", pe);
        throw pe;
    } finally {
        manager.releaseConnection(pageContext, dc);
    }
}
Also used : PageException(lucee.runtime.exp.PageException) DatasourceConnection(lucee.runtime.db.DatasourceConnection) SQLException(java.sql.SQLException) Log(lucee.commons.io.log.Log) DataSourceManager(lucee.runtime.db.DataSourceManager) DataSource(lucee.runtime.db.DataSource) Struct(lucee.runtime.type.Struct) SQL(lucee.runtime.db.SQL) QueryImpl(lucee.runtime.type.QueryImpl) StructImpl(lucee.runtime.type.StructImpl) ConfigImpl(lucee.runtime.config.ConfigImpl)

Example 14 with QueryImpl

use of lucee.runtime.type.QueryImpl in project Lucee by lucee.

the class Zip method actionList.

private void actionList() throws PageException, IOException {
    required("file", file, true);
    required("name", name);
    lucee.runtime.type.Query query = new QueryImpl(new String[] { "name", "size", "type", "dateLastModified", "directory", "crc", "compressedSize", "comment" }, 0, "query");
    pageContext.setVariable(name, query);
    ZipFile zip = getZip(file);
    Enumeration entries = zip.entries();
    try {
        String path, name, dir;
        ZipEntry ze;
        int row = 0, index;
        while (entries.hasMoreElements()) {
            ze = (ZipEntry) entries.nextElement();
            if (!showDirectory && ze.isDirectory())
                continue;
            path = ze.getName().replace('\\', '/');
            index = path.lastIndexOf('/');
            if (!recurse && index > 0)
                continue;
            dir = index == -1 ? "" : path.substring(0, index);
            name = path.substring(index + 1);
            if (filter != null && !filter.accept(file.getRealResource(name)))
                continue;
            if (!entryPathMatch(dir))
                continue;
            // if(entryPath!=null && !(dir.equalsIgnoreCase(entryPath) || StringUtil.startsWithIgnoreCase(dir,entryPath+"/"))) ;///continue;
            row++;
            query.addRow();
            query.setAt("name", row, path);
            query.setAt("size", row, Caster.toDouble(ze.getSize()));
            query.setAt("type", row, ze.isDirectory() ? "Directory" : "File");
            query.setAt("dateLastModified", row, new DateTimeImpl(pageContext, ze.getTime(), false));
            query.setAt("crc", row, Caster.toDouble(ze.getCrc()));
            query.setAt("compressedSize", row, Caster.toDouble(ze.getCompressedSize()));
            query.setAt("comment", row, ze.getComment());
            query.setAt("directory", row, dir);
        // zis.closeEntry();
        }
    } finally {
        IOUtil.closeEL(zip);
    }
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) Enumeration(java.util.Enumeration) ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl)

Example 15 with QueryImpl

use of lucee.runtime.type.QueryImpl in project Lucee by lucee.

the class Admin method doGetRHExtensionProviders.

private void doGetRHExtensionProviders() throws PageException {
    RHExtensionProvider[] providers = config.getRHExtensionProviders();
    lucee.runtime.type.Query qry = new QueryImpl(new Key[] { KeyConstants._url, KeyConstants._readonly }, providers.length, "query");
    RHExtensionProvider provider;
    for (int i = 0; i < providers.length; i++) {
        provider = providers[i];
        int row = i + 1;
        qry.setAt(KeyConstants._url, row, provider.getURL().toExternalForm());
        qry.setAt(KeyConstants._readonly, row, provider.isReadonly());
    }
    pageContext.setVariable(getString("admin", action, "returnVariable"), qry);
}
Also used : QueryImpl(lucee.runtime.type.QueryImpl) Query(lucee.runtime.type.Query) RHExtensionProvider(lucee.runtime.extension.RHExtensionProvider)

Aggregations

QueryImpl (lucee.runtime.type.QueryImpl)82 Query (lucee.runtime.type.Query)65 Collection (lucee.runtime.type.Collection)17 Struct (lucee.runtime.type.Struct)16 StructImpl (lucee.runtime.type.StructImpl)13 PageException (lucee.runtime.exp.PageException)12 Key (lucee.runtime.type.Collection.Key)12 Iterator (java.util.Iterator)11 Map (java.util.Map)10 ApplicationException (lucee.runtime.exp.ApplicationException)10 Array (lucee.runtime.type.Array)10 DatabaseException (lucee.runtime.exp.DatabaseException)9 Stopwatch (lucee.runtime.timer.Stopwatch)9 HashMap (java.util.HashMap)8 Resource (lucee.commons.io.res.Resource)7 BundleCollection (lucee.loader.osgi.BundleCollection)7 Entry (java.util.Map.Entry)6 IOException (java.io.IOException)5 ResultSet (java.sql.ResultSet)5 ArrayImpl (lucee.runtime.type.ArrayImpl)5