CFLib.org – Common Function Library Project

QuerySim(queryData)

Last updated December 18, 2007

Version: 2 | Requires: ColdFusion 5 | Library: DataManipulationLib

 
Rated 6 time(s). Average Rating: 4.8

Description:
Accepts a specifically formatted chunk of text, and returns it as a query object. Based on QuerySim.cfm by hal.helms@TeamAllaire.com. Pass in a block of text where the first line is a comma separated column list, and subequent lines represent records, with '|' delimited data cells. Note that version 2 no longer accepts the queryname as the first line.

Return Values:
Returns a query object.

Example:

view plain print about
<cfscript>
people = querySim('
id , name , mail
1 | weed | weed@theflowerpot.not
2 | bill | bill@theflowerpot.not
3 | ben | ben@theflowerpot.not
');
</cfscript>

People
<cfdump var="#people#">

Parameters:

Name Description Required
queryData Specifically format chunk of text to convert to a query. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Accepts a specifically formatted chunk of text, and returns it as a query object.
 * v2 rewrite by Jamie Jackson
 * 
 * @param queryData      Specifically format chunk of text to convert to a query. (Required)
 * @return Returns a query object. 
 * @author Bert Dawson (bert@redbanner.com) 
 * @version 2, December 18, 2007 
 */

function querySim(queryData) {
    var fieldsDelimiter="|";
    var colnamesDelimiter=",";
    var listOfColumns="";
    var tmpQuery="";
    var numLines="";
    var cellValue="";
    var cellValues="";
    var colName="";
    var lineDelimiter=chr(10) & chr(13);
    var lineNum=0;
    var colPosition=0;

    // the first line is the column list, eg "column1,column2,column3"
    listOfColumns = Trim(ListGetAt(queryData, 1, lineDelimiter));
    
    // create a temporary Query
    tmpQuery = QueryNew(listOfColumns);

    // the number of lines in the queryData
    numLines = ListLen(queryData, lineDelimiter);
    
    // loop though the queryData starting at the second line
    for(lineNum=2;  lineNum LTE numLines;  lineNum = lineNum + 1) {
        cellValues = ListGetAt(queryData, lineNum, lineDelimiter);

        if (ListLen(cellValues, fieldsDelimiter) IS ListLen(listOfColumns,",")) {
            QueryAddRow(tmpQuery);
            for (colPosition=1; colPosition LTE ListLen(listOfColumns); colPosition = colPosition + 1){
                cellValue = Trim(ListGetAt(cellValues, colPosition, fieldsDelimiter));
                colName   = Trim(ListGetAt(listOfColumns,colPosition));
                QuerySetCell(tmpQuery, colName, cellValue);
            }
        } 
    }
    
    return( tmpQuery );
    
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson