﻿// Copyright 2005, Bensoft Inc.
// Distributed under the terms of the GNU PUBLIC LICENSE
// Contact info@bensoft.com for other licensing terms
function jsqueryResultSetMetaData() {
 //props
 this.tableName=new String();
 this.colNames=new Array();
 this.colTypes=new Array();
 //eval setters
 this.setTableName=function(name){this.tableName=name};
 this.setNames=function(names){this.colNames=names};
 this.setTypes=function(types){this.colTypes=types};
 //getters
 this.getColumnCount=function(){return(this.colNames.length)};
 this.getColumnName=function(c){return(this.colNames[c-1])};
 this.getColumnType=function(t){return(this.colTypes[t-1])};
 this.getTableName=function(){return(this.tableName)};
 this.findColumn=function(nm){
  cc=this.colNames.length;
  for(var i=0;i<cc;i++){
   if(this.colNames[i].toLowerCase()==nm.toLowerCase()){return(i+1)} //1-based
  }
  return(0);
 }
}



function jsqueryResultSet() {
 //props
 this.row=-1;//0-based
 this.md=new jsqueryResultSetMetaData();
 this.rows=new Array();
 // error handler
 this.error="";
 this.setError=function(e){this.error=e;}
 this.getError=function(){return(this.error)};
 //eval loader
 this.add=function(ary){this.rows.push(ary)};
 this.remove=function(row)//add by hzf
 {
 	var temp=new Array();
 	var i;
 	for(i=0;i<this.rows.length;i++)
 		if(i!=row) temp.push(this.rows[i]);
 	this.rows=temp;
 }
 //MetaData
 this.getMetaData=function(){return(this.md)};
 //Navigation
 this.onRow=function(){r=this.row;return(r>=0&&r<this.rows.length?true:false)};
 this.absolute=function(r){
   if(r>0){this.row=r<=this.rows.length+1?r-1:this.rows.length}
   else {this.row=this.rows.length+r>=-1?this.rows.length+r:-1}
   return(this.onRow());
 }
 this.relative=function(r){return(this.absolute(this.row+r+1))};
 this.next=function(){return(this.relative(1))};
 this.previous=function(){return(this.relative(-1))};
 this.first=function(){this.row=0;return(this.onRow())};
 this.isFirst=function(){return(this.row==0?true:false)};
 this.last=function(){this.row=this.rows.length-1;return(this.onRow())};
 this.isLast=function(){return(this.row==this.rows.length-1?true:false)};
 this.beforeFirst=function(){this.row=-1};
 this.isBeforeFirst=function(){return(this.row==-1?true:false)};
 this.afterLast=function(){this.row=this.rows.length};
 this.isAfterLast=function(){return(this.row==this.rows.length?true:false)};
 this.getRow=function(){return(this.row+1)};
 this.getRecordCount=function(){return(this.rows.length)};
 //Rows
 this.getRowArray=function(){//getRowArray
  var r=this.row;
  r=r>=0&&r<=this.rows.length?r:-1;
  if(r>-1){var ra=this.rows;return(ra[r])}
  return(undefined);
 }
 //Columns
 this.findColumn=function(nm){return(this.md.findColumn(nm))};
 // Data functions
 this.getBoolean=function(i){var ra=this.getRowArray();return(ra!=undefined?new Boolean(ra[i-1]):ra)};
 this.getNumber=function(i){var ra=this.getRowArray();return(ra!=undefined?new Number(ra[i-1]):ra)};
 this.getDate=function(i){var ra=this.getRowArray();return(ra!=undefined?new Date(ra[i-1]):ra)};
 this.getStringByColumnId=function(i){
 	  var ra=this.getRowArray();
 	  var re=ra!=undefined?new String(ra[i-1]):ra;
 	  if(re=="null") re="";
 	  return(re);
 };
 this.getString=function(columnname){//add by hzf
 	var i,n;
 	n=parseInt(columnname);
 	if(isNaN(n))
 	{ 
 	  i=this.findColumn(columnname);
 	}
 	else
 	{
 		i=n;
 	}
 	var ra=this.getRowArray();
 	var re=ra!=undefined?new String(ra[i-1]):ra;
 	if(re=="null") re="";
 	return(re);
 	};//add by hzf
 // close
 this.close=function()
 {
 	this.row=-1;
 	this.md=new jsqueryResultSetMetaData();
 	//delete this.rows;
 	this.rows=new Array();
 	this.error="";
 	//delete this.fields;
 	this.fields=new Array();
  this.drawCheckBox=false;
  this.fieldCount=0; 	
 };
 this.clear=function()
 {
 	this.row=-1;
 	//delete this.rows;
 	this.rows=new Array();
 	this.error="";
 	//delete this.fields;
 	this.fields=new Array();
  this.drawCheckBox=false;
  this.fieldCount=0; 	
 };
 
 //////////////////////////生成表格///////////////////////////////////////////////////////////////////
 this.drawCheckBox=false;
 this.fieldCount=0;
 this.fields=new Array();
 this.clearFields=function()
 {
 	 this.fields=new Array();
 	 this.fieldCount=0;
 };
 
 this.addField=function(fn,fc,st,git)
 {
 	  this.fieldCount++;
    var qField=new queryField(this.fieldCount-1,fn,fc,st,git);
    this.fields[this.fieldCount-1]=qField;
    return qField;
 };
 this.setDisplayAllField=function()
 {
 	 this.fields=new Array();
 	 this.fieldCount=0;
 	 var i=0;

 	 for(i=1;i<=this.getMetaData().getColumnCount();i++)
 	 {
 	 	 this.addField(this.getMetaData().getColumnName(i),this.getMetaData().getColumnName(i),this.getMetaData().getColumnType(i),'label');
 	 }
 };
 
 this.setCheckBoxDraw=function(b)
 {
 	this.drawCheckBox=b;
 };
 this.getHtmlByTitle_Tr=function()
 {
 	  if(this.fieldCount==0) return("");
 	  var html="";
    html+="<tr class='ctr_title'> ";
    if (this.drawCheckBox==true)  
    {
      html+="<td class='ctd_title'>选择</td>";
    }
    for (var i=0;i<this.fieldCount;i++) 
    {   
      html+="<td class='ctd_title'";
      if (this.fields[i].gridInputType=="nodisplay")
        html+=" style='display: none'";
      html+=">";
      html+=this.fields[i].fieldCaption;
      html+="</td>";
    }
    html+="</tr>";       
    return html;
 };
 
 this.getHtmlByData_Tr=function(tb,shooseColor)
 {
 	 if(this.fieldCount==0) return("");
 	 
   var html="";
   html+="<input type='hidden' id='currval_";
   html+=tb;
   html+="'>";
   html+="<tr class='ctr_data'> ";
   var tri=0;  //编号 从 1开始
   try
   {
      var n=this.fieldCount;
      this.beforeFirst();
      while (this.next())
      {
        if (!this.drawCheckBox) 
          tri = tri+1; 
        else 
          tri=this.row+1;
            
        html+="<tr id='"+tb+"_tr"+tri+"' class=ctr_data bgcolor='#ffffff' ";
        html+=" style='cursor:hand;'";          
        html+=" onclick=\"javascript:chooserow('"+tb+"',"+tri+" ,'"+shooseColor+"')\"> ";
        if (this.drawCheckBox)  
        {
              html+="<td class=ctr_data ";
              html+="/><input type=\"checkbox\" id='";
              html+=tb;
              html+="_ck";
              html+=tri-1;
              html+="' name='";
              html+=tb;
              html+="_ck";
              html+=tri-1; 
              html+="'></td>";


        }
        for (var i=0;i<n;i++) //显示数据
        {
          	
                  html+="<td class=ctr_data id='";
                  html+=tb;
                  html+="_td";
                  html+=tri;
                  html+="_";
                  html+=this.fields[i].fieldName.toLowerCase();
                  html+="'";
                  if (this.fields[i].gridInputType=="nodisplay")
                    html+=" style='display: none'";
                  html+=" >";
               
                  html+=this.getString(this.fields[i].fieldName);
                  html+="</td>";
                  
         }
        html+="</tr>";
    } 
  }
  catch(E)
  {
    html+="显示记录数据时出错，错误信息如下："+E;
  }
  return html; 	
 };
 this.getHtmlByTable=function(tb,shooseColor)
 {

 	   if(this.fieldCount==0) return("");
     var Html="<table width='100%' cellspacing=1 cellpadding=5 class='tab_simple'>" ;
     Html+=this.getHtmlByTitle_Tr();
     Html+=this.getHtmlByData_Tr(tb,shooseColor);
     Html+="</table>";
     return Html; 	
 };
///////////////////////////////////////////////////////////////////////////////////////////////////// 
 
//////////////////////////////////////用于构建临时表////////////////////////////////////////////////////
 this.setTableName=function(str)
 {
 	 this.md.setTableName(str);
 };
 this.setField=function(field_array)
 {
   this.md.setNames(field_array);
 };
 this.setFieldType=function(type_array)
 {
 	 if(this.md.colNames.length!=type_array.length)
 	 {
 	 	alert("数据类型的个数和列个数不一致");
 	 	return;
 	 }
 	 else
     this.md.setTypes(type_array);
 }; 
 this.addRow=function(row_array)
 {
   if(this.md.colNames.length!=row_array.length)
 	 {
 	 	alert("数据个数和列个数不一致");
 	 	return;
 	 }
 	 else 	 
 	   this.add(row_array);
 };
 this.setValue=function(fieldname,value)
 {
	var colN=this.findColumn(fieldname);
	var curr=this.getRowArray();
	curr[colN-1]=value;
 }
//////////////////////////////////////////////////////////////////////////////////////////////////////
}
