<?php
/*
 *
 * Class: iapiQueryData
 * 
 * Osnovna klasa sa metodama za obradu podataka u upitu (parametara i rezultata).
 * Ova klasa se ne upotrebljava direktno vec preko naslednika iapiQueryParams.   
 *  
 */

include_once ('iapi_query_class.php');
include_once ('iapi_table_references_class.php');

abstract class iapiQueryData extends iapiQuery {

    public $TableReferences;
    public $database_format = "PLAIN_OBJ";
    public $DataBaseAttachment = '';
    
    public function getClassName() {
        return "TAPIQueryData";
    }
    
    public function Prepare() {
        $this->CreateDatabase();
    }        
 
    public function AddTableReference ($pTableName, $pTableData){
        if (! isset ($this->TableReferences)) {
          $this->TableReferences = new iapiTablereferences ();
        }
        $this->TableReferences->Add (new iapiTablereference ($pTableName, $pTableData));
       
    }
    
    private function CreateDatabase() {
        if ($this->database_format == "PLAIN_OBJ") {
            $this->TablesToObjects();            
        } else {
            $this->TablesToSQLite();
        }
    }
    
    private function TablesToObjects()
    {
        unset ($this->DataBaseAttachment);
        
        if (isset ($this->TableReferences )) {
        
            foreach ($this->TableReferences->items as $mKey => $mItem) {
                
                if (is_resource($mItem->table_data)) {
                    $mDataType = get_resource_type ($mItem->table_data);
                } else {
                    if (is_object($mItem->table_data)) {
                        $mDataType = get_class ($mItem->table_data);
                    }
                }
                
                switch ($mDataType) {
                  case 'PDOStatement':
                    $mTable = $this->PDOStatementToObject($mItem->table_data);
                    $mTable['table_name'] = $mItem->table_name; 
                    $this->DataBaseAttachment['items'][] = $mTable; 
                    break; 
                  default:
                    throw new Exception ('DataType "' . $mDataType . '" not supporrted!');
                }
                
            }

            $this->DataBaseAttachment = json_encode ($this->DataBaseAttachment);
        }   
    }
    
    private function PDOStatementToObject($pData) {

        $mrowcount = 0;

        while (($mRow = $pData->fetch(PDO::FETCH_ASSOC)) !== false) {
          
            if ($mrowcount == 0) {
              
              foreach(range(0, $pData->columnCount() - 1) as $mColumnIndex) {
                $mColumn = $pData->getColumnMeta($mColumnIndex);
                $mColumns[$mColumn['name']] = $mColumn;
              }              
              
                foreach ($mRow as $mKey => $mItem) {
 
                    $mFieldDef['name'] = $mKey;
                    
                    $mFieldType = $mColumns[$mKey]['native_type'];
                    
                    switch ($mFieldType) {

                        case "boolean":
                            $mFieldDef['type'] = 'L';
                            break;
                        case "integer":
                            $mFieldDef['type'] = 'I';
                            break;
                                                
                        case "double":
                            $mFieldDef['type'] = 'N';
                            break;
                            
                        case "string":
                            $mFieldDef['type'] = 'C';
                            break;
                            
                        case "array":
                        case "object":
                        case "resource":
                        case "NULL":
                        case "unknown type":
                            $mFieldDef['type'] = 'U';
                        
                        
                    }
                    $mResult['fields']['items'][] = $mFieldDef;
 
                }
            }
            foreach ($mRow as $mKey => $mItem) {
              $mType = $mColumns[$mKey]['native_type'];
              settype ($mItem, $mType);
              $mDestRow[$mKey] = $mItem;
            }
            $mResult['rows']['items'][] = $mDestRow;
            $mrowcount++; 
        }
        
        return $mResult;
                
    }    

    private function TablesToSQLite()
    {
        throw new Exception ('TablesToSQLite() not implemented!');        
    }
    
}

?>