001    package railo.runtime.functions.query;
002    
003    import java.util.Iterator;
004    
005    import railo.commons.lang.StringUtil;
006    import railo.runtime.PageContext;
007    import railo.runtime.exp.DatabaseException;
008    import railo.runtime.exp.FunctionException;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.ext.function.Function;
011    import railo.runtime.op.Caster;
012    import railo.runtime.op.Decision;
013    import railo.runtime.type.Array;
014    import railo.runtime.type.Collection.Key;
015    import railo.runtime.type.List;
016    import railo.runtime.type.Query;
017    import railo.runtime.type.QueryImpl;
018    import railo.runtime.type.Struct;
019    import railo.runtime.type.util.QueryUtil;
020    
021    /**
022     * Implements the Cold Fusion Function querynew
023     */
024    public final class QueryNew implements Function {
025            public static railo.runtime.type.Query call(PageContext pc , String columnList) throws DatabaseException {
026                return new QueryImpl(List.listToArrayTrim(columnList,","),0,"query");
027            }
028            public static railo.runtime.type.Query call(PageContext pc , String columnList, String columnTypeList) throws PageException {
029                    if(StringUtil.isEmpty(columnTypeList)) return call(pc, columnList);
030                    return new QueryImpl(List.listToArrayTrim(columnList,","),List.listToArrayTrim(columnTypeList,","),0,"query");
031            }
032            
033            public static railo.runtime.type.Query call(PageContext pc , String strColumnList, String strColumnTypeList, Object data) throws PageException {
034                    Array columnList = List.listToArrayTrim(strColumnList,",");
035                    railo.runtime.type.Query qry;
036                    if(StringUtil.isEmpty(strColumnTypeList))
037                            qry= new QueryImpl(columnList,0,"query");
038                    else
039                            qry= new QueryImpl(columnList,List.listToArrayTrim(strColumnTypeList,","),0,"query");
040                    
041                    if(data==null) return qry;
042                    return populate(pc, qry, data); 
043            }
044            
045            private static Query populate(PageContext pc, Query qry,Object data) throws PageException {
046                    if(Decision.isArray(data))
047                            return _populate(pc,qry,Caster.toArray(data));
048                    else if(Decision.isStruct(data))
049                            return _populate(pc,qry,Caster.toStruct(data));
050                    else 
051                            throw new FunctionException(pc, "QueryNew", 3, "data", "the date must be defined as array of structs , array of arrays or struct of arrays");
052            }
053            
054            private static Query _populate(PageContext pc, Query qry,Struct data) throws PageException {
055                    Key[] keys = data.keys();
056                    for(int i=0;i<keys.length;i++){
057                            if(qry.getColumn(keys[i],null)!=null) 
058                                     populateColumn(qry,keys[i],Caster.toArray(data.get(keys[i])));
059                    }
060                    return qry; 
061            }
062            
063            private static void populateColumn(Query qry, Key column, Array data) throws PageException {
064                    Iterator<?> it = data.valueIterator();
065                    int row=0;
066                    while(it.hasNext()){
067                            row++;
068                            if(row>qry.getRecordcount()) qry.addRow();
069                            qry.setAt(column, row, it.next());
070                    }
071            }
072            private static Query _populate(PageContext pc, Query qry,Array data) throws PageException {
073                    Iterator<?> it = data.valueIterator();
074                    Object o;
075                    while(it.hasNext()){
076                            o=it.next();
077                            qry.addRow();
078                            if(Decision.isStruct(o))populateRow(qry,Caster.toStruct(o));
079                            else if(Decision.isArray(o))populateRow(qry,Caster.toArray(o));
080                            else
081                                    throw new FunctionException(pc, "QueryNew", 3, "data", "the date must be defined as array of structs , array of arrays or struct of arrays");
082                    }
083                    return qry;
084            }
085            
086            public static void populateRow(Query qry, Struct data) throws PageException {
087                    Key[] columns = QueryUtil.getColumnNames(qry);
088                    int row=qry.getRecordcount();
089                    Object value;
090                    for(int i=0;i<columns.length;i++){
091                            value=data.get(columns[i],null);
092                            if(value!=null) qry.setAt(columns[i], row, value);
093                    }
094                    
095            }
096            protected static void populateRow(Query qry, Array data) throws PageException {
097                    Iterator<?> it = data.valueIterator();
098                    Key[] columns = QueryUtil.getColumnNames(qry);
099                    int row=qry.getRecordcount();
100                    int index=-1;
101                    while(it.hasNext()){
102                            index++;
103                            if(index>=columns.length) break;
104                            qry.setAt(columns[index], row, it.next());
105                    }
106            }
107    }