Login   Register  
PHP Classes
elePHPant
Icontem

File: sql2txt.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Pablo DallOglio  >  SQL2TXT  >  sql2txt.php  >  Download  
File: sql2txt.php
Role: ???
Content type: text/plain
Description: class file
Class: SQL2TXT
Author: By
Last change:
Date: 2001-05-10 20:42
Size: 3,186 bytes
 

Contents

Class file image Download
<?
// This is a class that converts a SQL instructions into TXT file follow SDF format
// Author : Pablo Dall'Oglio (pablo@univates.br)
// Date :  Thursday, 10, May, 2001

class sql2txt
{
  var $nome_arq;
  var $myfile;
  var $LoginDB;
  var $LoginUID;
  var $LoginPWD;
  var $port;
  var $id_connection;
  var $sql;
  var $result;
  var $row;
  var $arg;
  var $brancos =   "                                                  "; //  50


  // This function open Connection with PostgreSQL DataBase
  // returns 1 if open if success otherwise, returns 0
  function openSQL($LoginDB, $port, $LoginUID, $LoginPWD)
  {
    $this->LoginDB = $LoginDB;
    $this->port = $port;
    $this->LoginUID = $LoginUID;
    $this->LoginPWD = $LoginPWD;
    $this->arg = "dbname=$this->LoginDB port=$this->port user=$this->LoginUID password=$this->LoginPWD";

    $this->id_connection = pg_Connect($this->arg);

    return empty($this->id_connection) ? 0 : $this->id_connection;
  }

  // This function open the TXT file for write
  // returns 1 if open with success otherwise, returns 0
  function opentxt($filename)
  {
    $this->nome_arq = $filename;

    $this->myfile = @fopen($this->nome_arq, "w");
    if (!$this->myfile)
    {
      return(0);
    }
    return(1);
  }

  // This function close both TXT file and PostgreSQL Database
  function Closeall()
  {
    if ($id_connection)
    {
      pg_close($this->id_connection);
    }
    fclose($this->myfile);
  }

  // This function get the columns from SQL instructions and
  // whrite it into a TXT file
  function InsertColumnsIntoTXT($sql, $myarray, $header, $trailer)
  {
    $this->sql = $sql;

    //Open query
    $this->result = pg_exec($this->id_connection,$this->sql);
    $this->row    = -1;

    if ($this->result != null)
    {
      if ($header)
      { fputs($this->myfile, "$header" . chr(13) . chr(10)); } // header

      $total = count($myarray);
      while ( $this->row + 1 < pg_numrows($this->result) )
      {
        $this->row++;

        // loop the database
        for($index=1; $index <= $total; $index ++)
        {
          list ( $tamanho, $alinhamento ) = split("-",$myarray[$index-1],2);

          // coluna comeca em 0
          $conteudo = pg_result($this->result,$this->row,$index-1);

          $conteudo = trim($conteudo);
          if ($alinhamento=="right")
          {
            $conteudo = substr($conteudo,0,$tamanho);
            $conteudo = substr($this->brancos,0,$tamanho-strlen($conteudo)) . $conteudo;

          }
          else
          {
            $conteudo = $conteudo . $this->brancos;
            $conteudo = substr($conteudo,0,$tamanho);
          }
          $linha_mens = $conteudo;

          fputs($this->myfile, "$linha_mens");
        }
        fputs($this->myfile, chr(13) . chr(10)); // retorno de carro
      }
      if ($trailer)
      { fputs($this->myfile, "$trailer" . chr(13) . chr(10)); } // trailer

    }
    // Close query
    if ( $this->result != null )
    {
      pg_freeresult($this->result);
      $this->result = null;
    }
  }
}
?>