001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019/** 020 * Implements the CFML Function querysetcell 021 */ 022package lucee.runtime.functions.query; 023 024import lucee.commons.lang.StringUtil; 025import lucee.runtime.PageContext; 026import lucee.runtime.exp.DatabaseException; 027import lucee.runtime.exp.PageException; 028import lucee.runtime.functions.BIF; 029import lucee.runtime.op.Caster; 030import lucee.runtime.type.KeyImpl; 031import lucee.runtime.type.Query; 032import lucee.runtime.type.util.ListUtil; 033 034public final class QuerySort extends BIF { 035 036 private static final long serialVersionUID = -6566120440638749819L; 037 038 public static boolean call(PageContext pc , Query query, String columnName) throws PageException { 039 return call(pc,query,columnName,null); 040 } 041 public static boolean call(PageContext pc , Query query, String columnNames, String directions) throws PageException { 042 // column names 043 String[] arrColumnNames = ListUtil.trimItems(ListUtil.listToStringArray(columnNames, ',')); 044 int[] dirs = new int[arrColumnNames.length]; 045 046 // directions 047 if(!StringUtil.isEmpty(directions)) { 048 String[] arrDirections = ListUtil.trimItems(ListUtil.listToStringArray(directions, ',')); 049 if(arrColumnNames.length!=arrDirections.length)throw new DatabaseException("column names and directions has not the same count",null,null,null); 050 051 String direction; 052 for(int i=0;i<dirs.length;i++){ 053 direction=arrDirections[i].toLowerCase(); 054 dirs[i]=0; 055 if(direction.equals("asc"))dirs[i]=Query.ORDER_ASC; 056 else if(direction.equals("desc"))dirs[i]=Query.ORDER_DESC; 057 else { 058 throw new DatabaseException("argument direction of function querySort must be \"asc\" or \"desc\", now \""+direction+"\"",null,null,null); 059 } 060 } 061 } 062 else { 063 for(int i=0;i<dirs.length;i++){ 064 dirs[i]=Query.ORDER_ASC; 065 } 066 } 067 068 069 for(int i=arrColumnNames.length-1;i>=0;i--) 070 query.sort(KeyImpl.init(arrColumnNames[i]),dirs[i]); 071 072 073 074 return true; 075 } 076 077 @Override 078 public Object invoke(PageContext pc, Object[] args) throws PageException { 079 if(args.length==2)return call(pc,Caster.toQuery(args[0]),Caster.toString(args[1])); 080 return call(pc,Caster.toQuery(args[0]),Caster.toString(args[1]),Caster.toString(args[2])); 081 } 082}