dbscript
[ class tree: dbscript ] [ index: dbscript ] [ all elements ]

Source for file postgresql.php

Documentation is available at postgresql.php

  1. <?php
  2.  
  3.   /** 
  4.    * dbscript for PHP 4 & 5 - restful crud framework
  5.    * @version 0.1.2 -- 19-Feb-2007
  6.    * @author Brian Hendrickson <brian@dbscript.net>
  7.    * @link http://dbscript.net/
  8.    * @copyright Copyright 2007 Brian Hendrickson
  9.    * @license http://www.opensource.org/licenses/mit-license.php MIT License
  10.    * @package dbscript
  11.    */
  12.  
  13.   /**
  14.    * PostgreSQL
  15.    * 
  16.    * adapter for the PostgreSQL database system
  17.    * 
  18.    * Usage:
  19.    * <code>
  20.    * $db = new PostgreSQL ( 'hostname', 'database_name', 'username', 'password' );
  21.    * </code>
  22.    * 
  23.    * More info...
  24.    * {@link http://dbscript.net/postgresql}
  25.    * 
  26.    * @package dbscript
  27.    * @author Brian Hendrickson <brian@dbscript.net>
  28.    * @access public
  29.    * @version 0.1.2
  30.    * @todo support array datatypes
  31.    */
  32.  
  33. class PostgreSQL extends Database {
  34.   var $connstr;
  35.   var $oid;
  36.   function PostgreSQL({
  37.     $this->db_open = false;
  38.     $this->models = array();
  39.     $this->recordsets = array();
  40.     $this->max_blob_length = 6144000;  // default max blob file size is 6MB
  41.     $this->max_string_length = 1024000;  // default max string length is 1MB
  42.     $this->datatype_map = array(
  43.       
  44.       'real' => 'float',
  45.       'double precision' => 'float',
  46.  
  47.       'int' => 'int',      
  48.       'integer' => 'int',
  49.       'smallint' => 'int',
  50.       'bigint' => 'int',
  51.       'serial' => 'int',
  52.       'serial primary key' => 'int',
  53.       'bigserial' => 'int',
  54.       'numeric' => 'int',
  55.  
  56.       'text' => 'text',
  57.  
  58.       'char' => 'char',
  59.       'varchar' => 'char',
  60.       'character' => 'char',
  61.       'character varying' => 'char',
  62.  
  63.       'timestamp' => 'time',
  64.       'timestamp without time zone' => 'time',
  65.       'timestamp with time zone' => 'time',
  66.       'time' => 'time',
  67.       'time without time zone' => 'time',
  68.       'time with time zone' => 'time',
  69.       
  70.       'date' => 'date',
  71.  
  72.       'boolean' => 'bool',
  73.       
  74.       'oid' => 'blob'
  75.  
  76.     );
  77.     $func_args func_get_args();
  78.     $argnames array('host','dbname','user','password','port');
  79.     $this->true_values = array('t','true','1',true)
  80.     $this->alias_array array();
  81.     for ($i 0$i count($func_args)$i++{
  82.       if (strlen($func_args[$i]0)
  83.         $this->connstr .= $argnames[$i'=' $func_args[$i' ';
  84.     }
  85.     $this->connect();
  86.   }
  87.   function connect(// establish a connection to the database
  88.     $this->conn = @pg_connect($this->connstr);
  89.     if (!$this->conn{
  90.       $this->db_open = false;
  91.       trigger_error("error in connect function of class PostgreSQL in postgresql.php".@pg_last_error($this->conn)E_USER_ERROR );
  92.     else {
  93.       $this->db_open = true;
  94.     }
  95.     return $this->db_open;
  96.   }
  97.   function escape_string($string{
  98.     if (!(strlen($string0)) return ""}
  99.     $result @pg_escape_string($string);
  100.     if (!$result && !(is_numeric($string)))
  101.       trigger_error("error in escape_string in postgresql.php".@pg_last_error($this->conn)E_USER_ERROR );
  102.     return $result;
  103.   }
  104.   function get_result$sql$returnfalse NULL /* run an SQL query */
  105.     $request =request_object();
  106.     if (isset($request->params)) {
  107.       $request->set_param'currentquery'$sql );
  108.       trigger_before'get_result'$request$this );
  109.     }
  110.     $result @pg_query$this->conn$sql );
  111.     if (!$result && $returnfalse === NULL)
  112.       trigger_error("error in get_result in postgresql.php".@pg_last_error($this->conn)." ".$sqlE_USER_ERROR );
  113.     elseif (!$result && $returnfalse)
  114.       return true;
  115.     else
  116.       return $result;
  117.   }
  118.   function next_primary_key$table$pkfield$sequence_name=NULL {
  119.     if ($sequence_name == NULL{
  120.       $sql "SELECT relname FROM pg_class WHERE relkind='S' and substr(relname,1,".strlen($table).")='$table'";
  121.       $result $this->get_result($sql);
  122.       if ($this->num_rows($result0{
  123.         $seq $this->result_value($result,0,"relname");
  124.       else {
  125.         return '';
  126.       }
  127.     else {
  128.       $seq $sequence_name;
  129.     }
  130.     $pk_result $this->get_result("SELECT nextval('$seq')");
  131.     if ($this->num_rows($result0)
  132.       $pkvalue $this->result_value$pk_result0"nextval" );
  133.     else
  134.       trigger_error("error selecting nexval in next_primary_key in postgresql.php".@pg_last_error($this->conn)E_USER_ERROR );
  135.     return $pkvalue;
  136.   }
  137.   function last_insert_id(&$result,$pkfield,$table// returns the id of the most recently modified record
  138.     $oid @pg_last_oid($result);
  139.     if (!$oid)
  140.       trigger_error(@pg_last_error($this->conn)E_USER_ERROR );
  141.     $sql "SELECT "$pkfield " FROM " $table " WHERE oid = " $oid;
  142.     $res $this->get_result($sql);
  143.     if (!$res)
  144.       trigger_error("error in last_insert_id in postgresql.php".@pg_last_error($this->conn)E_USER_ERROR );
  145.     else
  146.       return $this->result_value($res,0,$pkfield);
  147.   }
  148.   function result_value(&$result,$resultindex,$field// get a single value from a result set
  149.     $return pg_fetch_result($result,$resultindex,$field);
  150.     if (!$return && $return != 0)
  151.       trigger_error("error in result_value in postgresql.php".@pg_last_error($this->conn)E_USER_ERROR );
  152.     else
  153.       return $return;
  154.   }
  155.   function close({
  156.     $args func_get_args();
  157.     pg_close$this->conn );
  158.     if isset$args[0) ) {
  159.       if strlen($args[0]{
  160.         header"Location:" $args[0);
  161.         exit;
  162.       }
  163.     }
  164.   }
  165.   function &get_table($table{
  166.     if isset$this->models[$table) )
  167.       return $this->models[$table];
  168.     $data_model model_path(classify($table'.php';
  169.     if (file_exists($data_model))
  170.       require_once $data_model;
  171.     $this->models[$tablenew PostgreSQLTable$table$this );
  172.     return $this->models[$table];
  173.   }
  174.   function &model($model{
  175.     return $this->get_table(tableize($model));
  176.   }
  177.   function fetch_array&$result$row=NULL {
  178.     if (is_numeric($row))
  179.       $this->seek_row$result$row );
  180.     return pg_fetch_array$result$rowPGSQL_ASSOC );
  181.   }
  182.   function fetch_row&$result$row=NULL {
  183.     if ( ( is_numeric$row ) ) )
  184.       return pg_fetch_row$result$row );
  185.     return pg_fetch_row$result );
  186.   }
  187.   function seek_row(&$result,$row{
  188.     return true;
  189.   }
  190.   function query_limit($limit,$offset{
  191.     return " LIMIT " $limit " OFFSET " $offset;
  192.   }
  193.   function blob_value&$rec$field&$value {
  194.     return $value;
  195.   }
  196.   function sql_insert_for&$rec {
  197.     $sql "INSERT INTO " $rec->table " (";
  198.     $comma '';
  199.     $fields '';
  200.     $values '';
  201.     foreach (array_unique($rec->modified_fieldsAS $modified_field{
  202.       $datatype $this->get_mapped_datatype($this->models[$rec->table]->field_array[$modified_field]);
  203.       $this->pre_insert$rec$modified_field$datatype );
  204.       if !$datatype == 'blob' &&  !(strlen$rec->attributes[$modified_field) ) ) ) {
  205.         $fields .= $comma $modified_field;
  206.         $values .= $comma $this->quoted_insert_value$rec$modified_field );;
  207.         $comma ',';
  208.       }
  209.     }
  210.     $sql .= $fields ") VALUES (" $values ")";
  211.  
  212.     return $sql;
  213.   }
  214.   function sql_update_for&$rec {
  215.     $sql "UPDATE ";
  216.     $sql .= $rec->table ' SET ';
  217.     $comma '';
  218.     foreach (array_unique($rec->modified_fieldsAS $modified_field{
  219.       $datatype $this->get_mapped_datatype($this->models[$rec->table]->field_array[$modified_field]);
  220.       $this->pre_update$rec$modified_field$datatype );
  221.       if !$datatype == 'blob' &&  !(strlen$rec->attributes[$modified_field) ) ) ) {
  222.         $sql .= $comma $this->quoted_update_value$rec$modified_field );
  223.         $comma ',';
  224.       }
  225.     }
  226.     $sql .= " WHERE " $rec->primary_key "='" $rec->attributes[$rec->primary_key"'";
  227.     return $sql;
  228.   }
  229.   function sql_select_for&$rec$id {
  230.     return "SELECT ".$rec->selecttext." FROM ".$rec->table." WHERE ".$rec->primary_key." = '".$id."'";
  231.   }
  232.   function sql_delete_for&$rec {
  233.     $pkfield $rec->primary_key;
  234.     foreach ($rec->attributes as $key=>$value{
  235.       $datatype $this->get_mapped_datatype($this->models[$rec->table]->field_array[$key]);
  236.       if ($datatype == 'blob' && strlen($rec->attributes[$rec->primary_key]0{
  237.         $oid_result $this->get_result("select ".$key." from ".$rec->table." where ".$rec->primary_key." = '".$rec->attributes[$rec->primary_key]."'");
  238.         $prev_oid $this->fetch_array($oid_result,0,$key);
  239.         if (isset($prev_oid[0]&& $prev_oid[00)
  240.           $result $this->large_object_delete($prev_oid[0]);
  241.       }
  242.     }
  243.     $sql 'DELETE FROM ' $rec->table ' WHERE ' $pkfield ' = ' $rec->$pkfield;
  244.     return $sql;
  245.   }
  246.   function select_distinct$field$table$orderby {
  247.     return "SELECT DISTINCT $field$this->models[$table]->primary_key " FROM $table ORDER BY $orderby DESC";
  248.   }
  249.   function quoted_update_value&$rec$modified_field {
  250.     return $modified_field "='" $this->escape_string($rec->attributes[$modified_field]"'";
  251.   }
  252.   function quoted_insert_value&$rec$modified_field {
  253.     return "'" $this->escape_string($rec->attributes[$modified_field]"'";
  254.   }
  255.   function pre_insert&$rec$modified_field$datatype {
  256.     if (isset($this->models[$rec->table]->field_attrs[$modified_field]['required'])) {
  257.       if (!(strlen$rec->attributes[$modified_field0))
  258.         trigger_error"$modified_field is a required field"E_USER_ERROR );
  259.     }
  260.     if (isset($this->models[$rec->table]->field_attrs[$modified_field]['unique'])) {
  261.       $result $this->get_result("select ".$modified_field." from ".$rec->table." where ".$modified_field." = '".$rec->attributes[$modified_field]."'");
  262.       if ($this->num_rows($result0)
  263.         trigger_error"$modified_field must be unique!"E_USER_ERROR );
  264.     }
  265.     if ($datatype == 'time' && !(strlen($rec->attributes[$modified_field]0))
  266.       $rec->attributes[$modified_fielddate("Y-m-d H:i:s",strtotime("now"));
  267.     if ($datatype == 'blob' && strlen($rec->attributes[$modified_field]0{
  268.       $oid $this->large_object_create($rec->table,$rec->attributes[$modified_field]);
  269.       if ($oid 0)
  270.         $rec->attributes[$modified_field$oid;
  271.     }
  272.     if ($datatype == 'bool'{
  273.       if in_array$rec->attributes[$modified_field]$this->true_valuestrue ) )
  274.         $rec->attributes[$modified_field"true";
  275.       else
  276.         $rec->attributes[$modified_field"false";
  277.     }
  278.     if ($modified_field == $rec->primary_key{
  279.       if in_array$rec->attributes[$rec->primary_key]array''0'0' )true ))
  280.         $rec->attributes[$modified_field$this->next_primary_key$rec->table$modified_field);      
  281.     }
  282.   }
  283.   function pre_update&$rec$modified_field$datatype {
  284.     if (isset($this->models[$rec->table]->field_attrs[$modified_field]['required'])) {
  285.       if (!(strlen$rec->attributes[$modified_field0))
  286.         trigger_error"$modified_field is a required field"E_USER_ERROR );
  287.     }
  288.     if (isset($this->models[$rec->table]->field_attrs[$modified_field]['unique'])) {
  289.       $result $this->get_result("select ".$modified_field." from ".$rec->table." where ".$modified_field." = '".$rec->attributes[$modified_field]."' and ".$rec->primary_key." != '".$rec->attributes[$rec->primary_key]."'");
  290.       if ($this->num_rows($result0)
  291.         trigger_error"$modified_field must be unique!"E_USER_ERROR );
  292.     }
  293.     if ($datatype == 'blob' && (strlen$rec->attributes[$modified_field)) {
  294.       $oid_result $this->get_result("select ".$modified_field." from ".$rec->table." where ".$rec->primary_key." = '".$rec->attributes[$rec->primary_key]."'");
  295.       if ($this->num_rows($oid_result0{
  296.         $prev_oid $this->fetch_array($oid_result);
  297.         if (isset($prev_oid[0]&& $prev_oid[00)
  298.           $result $this->large_object_delete($prev_oid);
  299.       }
  300.       $oid $this->large_object_create($rec->table,$rec->attributes[$modified_field]);
  301.       if ($oid 0)
  302.         $rec->attributes[$modified_field$oid;
  303.     }
  304.   }
  305.   function post_insert&$rec&$result {
  306.     if (!$resulttrigger_error("Sorry, the record could not be saved due to a database error."E_USER_ERROR )}
  307.   }
  308.   function num_rows(&$result{
  309.     return @pg_num_rows($result);
  310.   }
  311.   function num_fields(&$result{
  312.     return @pg_num_fields($result);
  313.   }
  314.   function field_name(&$result$index{
  315.     return @pg_field_name($result$index);
  316.   }
  317.   function large_object_create($table,$file{
  318.     $return false;
  319.     $filename basename($file);
  320.     if (!$filenametrigger_error("Error determining base name of large object file $filename"E_USER_ERROR )}
  321.     $handle fopen($file,"r");
  322.     if (!$handletrigger_error("Error opening large object file $file"E_USER_ERROR )}
  323.     $buffer fread($handle,filesize($file));
  324.     if (!$buffertrigger_error("Error reading large object file $file"E_USER_ERROR )}
  325.     $result fclose($handle);
  326.     $result @pg_query($this->conn"BEGIN");
  327.     if (!$resulttrigger_error("error starting l_o_c transaction: ".@pg_last_error($this->conn)E_USER_ERROR )}
  328.     $oid @pg_lo_create($this->conn);
  329.     if (!$oidtrigger_error("error in pg_l_o_c: ".@pg_last_error($this->conn)E_USER_ERROR )}
  330.     #$result = pg_query($this->conn,"UPDATE $table SET $field = $oid WHERE $pkfield = '$pkvalue'");
  331.     #if (!$result) { trigger_error("Error updating file OID", E_USER_ERROR ); }
  332.     $handle @pg_lo_open($this->conn$oid"w");
  333.     if (!$handletrigger_error("error in pg_l_o_o: ".@pg_last_error($this->conn)E_USER_ERROR )}
  334.     $result @pg_lo_write($handle$buffer);
  335.     if (!$resulttrigger_error("error in l_o_w: ".@pg_last_error($this->conn)E_USER_ERROR )}
  336.     $result @pg_lo_close($handle);
  337.     if (!$resulttrigger_error("error in l_o_close: ".@pg_last_error($this->conn)E_USER_ERROR )}
  338.     $result @pg_query($this->conn"COMMIT");
  339.     if (!$resulttrigger_error("error committing l_o_c transaction: ".@pg_last_error($this->conn)E_USER_ERROR )}
  340.     else {
  341.       $return $oid;
  342.     }
  343.     return $return;
  344.   }
  345.   function large_object_fetch($oid$return false{
  346.     //$result = pg_query($this->conn,"SELECT $field FROM $table WHERE $");
  347.     //if (!$result) { trigger_error("Error in select file OID", E_USER_ERROR ); }
  348.     //$oid = pg_result($result,0,$fieldname);
  349.     //if (!$oid) { trigger_error("Error in file OID result", E_USER_ERROR ); }
  350.     $result @pg_query($this->conn,"BEGIN");
  351.     if (!$resulttrigger_error("error starting l_o_f transaction: ".@pg_last_error($this->conn)E_USER_ERROR )}
  352.     $handle @pg_lo_open($this->conn$oid"r");
  353.     if (!$handletrigger_error("error in l_o_f/l_o_o: ".@pg_last_error($this->conn)E_USER_ERROR )}
  354.     if ($return)
  355.       $return @pg_lo_read($handle,$this->max_blob_length);
  356.     else
  357.       @pg_lo_read_all($handle);
  358.     if (!$buffertrigger_error("error in l_o_read_all: ".@pg_last_error($this->conn)E_USER_ERROR )}
  359.     $result @pg_lo_close($handle);
  360.     if (!$resulttrigger_error("error in l_o_close: ".@pg_last_error($this->conn)E_USER_ERROR )}
  361.     $result @pg_query($this->conn,"COMMIT");
  362.     if (!$resulttrigger_error("error committing l_o_f transaction: ".@pg_last_error($this->conn)E_USER_ERROR )}
  363.     return $return;
  364.   }
  365.   function large_object_delete($oid{
  366.     $return false;
  367.     #$result = pg_query($this->conn,"SELECT $field FROM $table WHERE $pkfield = '$pkvalue'");
  368.     #if (!$result) { trigger_error("Error in select file OID", E_USER_ERROR ); }
  369.     #$oid = pg_result($result,0,$field);
  370.     #if (!$oid) { trigger_error("Error in file OID result", E_USER_ERROR ); }
  371.     $result @pg_query($this->conn,"BEGIN");
  372.     if (!$resulttrigger_error("error starting l_o_d transaction: ".@pg_last_error($this->conn)E_USER_ERROR )}
  373.     $result @pg_lo_unlink($this->conn$oid);
  374.     if (!$resulttrigger_error("error in l_o_unlink: ".@pg_last_error($this->conn)E_USER_ERROR )}
  375.     $result @pg_query($this->conn,"COMMIT");
  376.     if (!$resulttrigger_error("error committing l_o_d transaction: ".@pg_last_error($this->conn)E_USER_ERROR )}
  377.     #$result = pg_query($this->conn,"DELETE FROM $table WHERE lo_oid = $oid");
  378.     #if (!$result) { trigger_error("Error deleting file OID", E_USER_ERROR ); }
  379.     else {
  380.       $return true;
  381.     }
  382.     return $return;
  383.   }
  384.   function add_table$table$field_array {
  385.     if (!(count($field_array)>0)) trigger_error"Error creating table. No fields are defined. Use \$model->auto_field and \$model->text_field etc."E_USER_ERROR );
  386.     $sql "CREATE TABLE $table (";
  387.     $comma "";
  388.     foreach $field_array as $field => $data_type {
  389.       $sql .= "$comma $field $data_type";
  390.       $comma ",";
  391.     }
  392.     $sql .= ")";
  393.     $result $this->get_result($sql);
  394.   }
  395.   function add_field$table$field$data_type {
  396.     $sql "ALTER TABLE $table ADD COLUMN $field $data_type";
  397.     $result $this->get_result($sql);
  398.   }
  399.   function has_table($t{
  400.     return in_array$t$this->get_tables()true );
  401.   }
  402.   function get_tables({
  403.     $tables array();
  404.     #$sql  = "SELECT a.relname AS Name FROM pg_class a, pg_user b ";
  405.     #$sql .= "WHERE ( relkind = 'r') and relname !~ '^pg_' AND relname !~ '^sql_' ";
  406.     #$sql .= "AND relname !~ '^xin[vx][0-9]+' AND b.usesysid = a.relowner ";
  407.     #$sql .= "AND NOT (EXISTS (SELECT viewname FROM pg_views WHERE viewname=a.relname))";
  408.     $sql =  "SELECT tablename AS relname FROM pg_catalog.pg_tables";
  409.     $sql .= " WHERE schemaname NOT IN ('pg_catalog', 'information_schema',";
  410.     $sql .= " 'pg_toast') ORDER BY tablename";
  411.     $result $this->get_result($sql);
  412.     while ($arr $this->fetch_array($result)) {
  413.       foreach($arr as $key=>$value{
  414.         if (strtolower(str_replace'_'''$value)) != strtolower(classify($value)) )
  415.           $tables[$value;
  416.       }
  417.     }
  418.     return $tables;
  419.   }
  420.   function get_fields($table{
  421.     $datatypes array();
  422.     $fieldindex array();
  423.     $fieldindex["";
  424.     #$sql  = "SELECT column_name, data_type FROM information_schema.columns ";
  425.     #$sql .= "WHERE table_schema = 'public' AND table = '$table'";
  426.     $sql  "SELECT a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod)";
  427.     $sql .= " as type FROM pg_catalog.pg_attribute a LEFT JOIN";
  428.     $sql .= " pg_catalog.pg_attrdef adef ON a.attrelid=adef.adrelid AND";
  429.     $sql .= " a.attnum=adef.adnum LEFT JOIN pg_catalog.pg_type t ON";
  430.     $sql .= " a.atttypid=t.oid WHERE a.attrelid = (SELECT oid FROM";
  431.     $sql .= " pg_catalog.pg_class WHERE relname='$table')";
  432.     $sql .= " and a.attname != 'tableoid' and a.attname != 'oid'";
  433.     $sql .= " and a.attname != 'xmax' and a.attname != 'xmin'";
  434.     $sql .= " and a.attname != 'cmax' and a.attname != 'cmin'";
  435.     $sql .= " and a.attname != 'ctid' and a.attname != 'otre'";
  436.     $sql .= " and a.attname not ilike '%..%' order by a.attnum ASC";
  437.     $result $this->get_result($sql,true);
  438.     if (!$resultreturn $datatypes;
  439.     while ($arr $this->fetch_array($result)) {
  440.       foreach($arr as $key=>$value{
  441.         if ($key == "attname"{
  442.           $field $value;
  443.           $fieldindex[$value;
  444.         elseif ($key == "type"{
  445.           $type $value;
  446.         }
  447.       }
  448.       $datatypes[$field$type;
  449.     }
  450.     $sql "SELECT idx.indkey, idx.indisunique, idx.indisprimary";
  451.     $sql .= " FROM pg_catalog.pg_class c, pg_catalog.pg_class c2,";
  452.     $sql .= " pg_catalog.pg_index idx";
  453.     $sql .= " WHERE c.oid = idx.indrelid";
  454.     $sql .= " AND idx.indexrelid = c2.oid";
  455.     $sql .= " AND c.relname = '$table'";
  456.     #$sql .= " AND idx.isprimary = true";
  457.     $result $this->get_result($sql);
  458.     while ($row pg_fetch_row($result)) {
  459.       if (!(strstr($row[0]' '))) 
  460.         $datatypes[$table."_primary_key"$fieldindex[$row[0]];
  461.     }
  462.     return $datatypes;
  463.   }
  464. }
  465.  
  466.  
  467.   /**
  468.    * PostgreSQL Table
  469.    * 
  470.    * data model for a single PostgreSQL table
  471.    * 
  472.    * Usage:
  473.    * <code>
  474.    *   $people = $db->get_table( 'people' );
  475.    * </code>
  476.    * 
  477.    * More info...
  478.    * {@link http://dbscript.net/postgresqltable}
  479.    * 
  480.    * @package dbscript
  481.    * @author Brian Hendrickson <brian@dbscript.net>
  482.    * @access public
  483.    * @param string $table 
  484.    * @param object $db 
  485.    * @version 0.1.2
  486.    */
  487.    
  488. class PostgreSQLTable extends Model {
  489.   
  490.   function PostgreSQLTable$table&$db {
  491.  
  492.     $this->table = $table;
  493.     $db->models[$table=$this;
  494.     $this->params = array('resource'=>$table);
  495.     $this->access_list = array();
  496.     $this->relations = array();
  497.     $this->allowed_methods = array'get''post''put''delete' );
  498.     $this->field_array = $db->get_fields$this->table );
  499.     if count$this->field_array )
  500.       $this->exists = true;
  501.     else
  502.       $this->exists = false;
  503.     if (isset($this->field_array[$this->table."_primary_key"])) {
  504.       $this->set_primary_key$this->field_array[$this->table."_primary_key");
  505.       $this->field_array = drop_array_element($this->field_array,$this->table."_primary_key");
  506.     }
  507.     if class_existsclassify$table )))
  508.       $this->registerclassify$table ) );
  509.   }
  510.   
  511.   function auto_field$field {
  512.     $this->set_field$field"serial primary key" );
  513.     $this->set_primary_key$field );
  514.   }
  515.   
  516.   function enum_field$field$values {
  517.     $this->set_field$field$values );
  518.   }
  519.   
  520.   function float_field$field {
  521.     $this->set_field$field"double precision" );
  522.   }
  523.   
  524.   function bool_field$field {
  525.     $this->set_field$field"boolean" );
  526.   }
  527.   
  528.   function char_field$field {
  529.     $args func_get_args();
  530.     if (count($args)>1)
  531.       $len $args[1];
  532.     else
  533.       $len "255";
  534.     
  535.     $this->set_field$field"varchar($len));
  536.   }
  537.   
  538.   function date_field$field {
  539.     $this->set_field$field"date" );
  540.   }
  541.   
  542.   function file_field$field {
  543.     $this->set_field$field"oid" );
  544.   }
  545.   
  546.   function int_field$field {
  547.     $this->set_field$field"int" );
  548.   }
  549.   
  550.   function text_field$field {
  551.     $this->set_field$field"text" );
  552.   }
  553.   
  554.   function time_field$field {
  555.     $this->set_field$field"timestamp with time zone" );
  556.   }
  557.   
  558.   function get_query$id=NULL$find_by=NULL {
  559.     $db =db_object();
  560.     trigger_before'get_query'$this$db );
  561.     $pkfield $this->primary_key;
  562.     if ($find_by == NULL)
  563.       $find_by $this->primary_key;
  564.     $relfields array();
  565.     $relfields $this->relations;
  566.     $table $this->table;
  567.     $fieldstring '';
  568.     $sql "SELECT " "\n";
  569.     if (!array_key_exists($pkfield,$this->field_array))
  570.       $sql .= "$table.$pkfield as \"$table.$pkfield\", "\n";
  571.     foreach ($this->field_array as $fieldname=>$datatypename{
  572.       if (!(!(strpos($fieldname,"."=== false)))
  573.         $fieldname $table "." $fieldname;
  574.       $fieldstring .= "$fieldname as \"$fieldname\", "\n";
  575.     }
  576.     $leftsql "";
  577.     $first true;
  578.     if (count($relfields0{
  579.       foreach ($relfields as $key=>$val{
  580.         $spl split("\.",$val["fkey"]);
  581.         if (($val["type"!= 'child-many'&& isset($db->models[$spl[0]]))
  582.           $leftsql .= "(";
  583.       }
  584.       $skippedrel false;
  585.       foreach ($relfields as $key=>$val{
  586.         $spl split("\.",$val["fkey"]);
  587.         if (($val["type"== 'child-many'|| !(isset($db->models[$spl[0]]))) {
  588.           if ($first)
  589.             $skippedrel true;
  590.           continue;
  591.         }
  592.         foreach ($db->models[$spl[0]]->field_array as $fieldname=>$datatypename{
  593.           $fieldstring .= $spl[0].".".$fieldname." as \"".$spl[0].".".$fieldname."\", " "\n";
  594.         }
  595.         if ($first)
  596.           $leftsql .= $table;
  597.         $leftsql .= " left join " $spl[0" on ".$table.".".$val["col"]." = " $val["fkey"];
  598.         $leftsql .= ")";
  599.         $first false;
  600.       }
  601.     }
  602.     $fieldstring substr($fieldstring,0,-3" " "\n";
  603.     $sql .= $fieldstring;
  604.     $sql .= "FROM ";
  605.     
  606.     $sql .= $leftsql;
  607.     
  608.     if (!(strlen($leftsql1))
  609.       $sql .= $table;
  610.     
  611.     if ($id != NULL)
  612.       $sql .= " WHERE $table.$find_by = '$id";
  613.  
  614.     if (!(isset($this->orderby)))
  615.       $this->orderby = $table "." $pkfield;
  616.  
  617.     if (!(isset($this->order)))
  618.       $this->order = "DESC";
  619.     
  620.     if (!(isset($this->offset)))
  621.       $this->offset = 0;
  622.  
  623.     if (!(isset($this->limit)))
  624.       $this->limit = 20;
  625.  
  626.     $sql .= " ORDER BY " $this->orderby . " ";
  627.  
  628.     $sql .= $this->order . $db->query_limit($this->limit,$this->offset);
  629.     
  630.     if (class_exists(classify($table)))
  631.       $this->custom_class = classify($table);
  632.  
  633.     trigger_after'get_query'$this$db );
  634.  
  635.     return $sql;
  636.   
  637.   }
  638.  
  639. }
  640.  
  641. ?>

Documentation generated on Mon, 19 Feb 2007 10:25:00 -0800 by phpDocumentor 1.3.1