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

Source for file Table.php

Documentation is available at Table.php

  1. <?php
  2.  
  3. /**
  4. * Error code at instantiation time when the first parameter to the
  5. * constructor is not a PEAR DB object.
  6. */
  7. define('DB_TABLE_ERR_NOT_DB_OBJECT',    -1);
  8.  
  9. /**
  10. * Error code at instantiation time when the PEAR DB $phptype is not
  11. * supported by DB_Table.
  12. */
  13. define('DB_TABLE_ERR_PHPTYPE',          -2);
  14.  
  15. /**
  16. * Error code when you call select() or selectResult() and the first
  17. * parameter does not match any of the $this->sql keys.
  18. */
  19. define('DB_TABLE_ERR_SQL_UNDEF',        -3);
  20.  
  21. /**
  22. * Error code when you try to insert data to a column that is not in the
  23. * $this->col array.
  24. */
  25. define('DB_TABLE_ERR_INS_COL_NOMAP',    -4);
  26.  
  27. /**
  28. * Error code when you try to insert data, and that data does not have a
  29. * column marked as 'require' in the $this->col array.
  30. */
  31. define('DB_TABLE_ERR_INS_COL_REQUIRED'-5);
  32.  
  33. /**
  34. * Error code when auto-validation fails on data to be inserted.
  35. */
  36. define('DB_TABLE_ERR_INS_DATA_INVALID'-6);
  37.  
  38. /**
  39. * Error code when you try to update data to a column that is not in the
  40. * $this->col array.
  41. */
  42. define('DB_TABLE_ERR_UPD_COL_NOMAP',    -7);
  43.  
  44. /**
  45. * Error code when you try to update data, and that data does not have a
  46. * column marked as 'require' in the $this->col array.
  47. */
  48. define('DB_TABLE_ERR_UPD_COL_REQUIRED'-8);
  49.  
  50. /**
  51. * Error code when auto-validation fails on update data.
  52. */
  53. define('DB_TABLE_ERR_UPD_DATA_INVALID'-9);
  54.  
  55. /**
  56. * Error code when you use a create() flag that is not recognized (must
  57. * be 'safe', 'drop', or boolean false.
  58. */
  59. define('DB_TABLE_ERR_CREATE_FLAG',      -10);
  60.  
  61. /**
  62. * Error code at create() time when you define an index in $this->idx
  63. * that has no columns.
  64. */
  65. define('DB_TABLE_ERR_IDX_NO_COLS',      -11);
  66.  
  67. /**
  68. * Error code at create() time when you define an index in $this->idx
  69. * that refers to a column that does not exist in the $this->col array.
  70. */
  71. define('DB_TABLE_ERR_IDX_COL_UNDEF',    -12);
  72.  
  73. /**
  74. * Error code at create() time when you define a $this->idx index type
  75. * that is not recognized (must be 'normal' or 'unique').
  76. */
  77. define('DB_TABLE_ERR_IDX_TYPE',         -13);
  78.  
  79. /**
  80. * Error code at create() time when you have an error in a 'char' or
  81. * 'varchar' definition in $this->col (usually because 'size' is wrong).
  82. */
  83. define('DB_TABLE_ERR_DECLARE_STRING',   -14);
  84.  
  85. /**
  86. * Error code at create() time when you have an error in a 'decimal'
  87. * definition (usually becuase the 'size' or 'scope' are wrong).
  88. */
  89. define('DB_TABLE_ERR_DECLARE_DECIMAL',  -15);
  90.  
  91. /**
  92. * Error code at create() time when you define a column in $this->col
  93. * with an unrecognized 'type'.
  94. */
  95. define('DB_TABLE_ERR_DECLARE_TYPE',     -16);
  96.  
  97. /**
  98. * Error code at validation time when a column in $this->col has an
  99. * unrecognized 'type'.
  100. */
  101. define('DB_TABLE_ERR_VALIDATE_TYPE',    -17);
  102.  
  103. /**
  104. * Error code at create() time when you define a column in $this->col
  105. * with an invalid column name (usually because it's a reserved keyword).
  106. */
  107. define('DB_TABLE_ERR_DECLARE_COLNAME',  -18);
  108.  
  109. /**
  110. * Error code at create() time when you define an index in $this->idx
  111. * with an invalid index name (usually because it's a reserved keyword).
  112. */
  113. define('DB_TABLE_ERR_DECLARE_IDXNAME',  -19);
  114.  
  115.  
  116. /**
  117. * The PEAR class for errors
  118. */
  119. require_once 'PEAR.php';
  120.  
  121. /**
  122. * The Date class for recasting date and time values
  123. */
  124. require_once 'Date.php';
  125.  
  126.  
  127. /**
  128. * DB_Table supports these RDBMS engines and their various native data
  129. * types; we need these here instead of in Manager.php becuase the
  130. * initial array key tells us what databases are supported.
  131. */
  132. $GLOBALS['_DB_TABLE']['type'= array(
  133.     'fbsql' => array(
  134.         'boolean'   => 'DECIMAL(1,0)',
  135.         'char'      => 'CHAR',
  136.         'varchar'   => 'VARCHAR',
  137.         'smallint'  => 'SMALLINT',
  138.         'integer'   => 'INTEGER',
  139.         'bigint'    => 'LONGINT',
  140.         'decimal'   => 'DECIMAL',
  141.         'single'    => 'REAL',
  142.         'double'    => 'DOUBLE PRECISION',
  143.         'clob'      => 'CLOB',
  144.         'date'      => 'CHAR(10)',
  145.         'time'      => 'CHAR(8)',
  146.         'timestamp' => 'CHAR(19)'
  147.     ),
  148.     'mssql' => array(
  149.         'boolean'   => 'DECIMAL(1,0)',
  150.         'char'      => 'CHAR',
  151.         'varchar'   => 'VARCHAR',
  152.         'smallint'  => 'SMALLINT',
  153.         'integer'   => 'INTEGER',
  154.         'bigint'    => 'BIGINT',
  155.         'decimal'   => 'DECIMAL',
  156.         'single'    => 'REAL',
  157.         'double'    => 'FLOAT',
  158.         'clob'      => 'TEXT',
  159.         'date'      => 'CHAR(10)',
  160.         'time'      => 'CHAR(8)',
  161.         'timestamp' => 'CHAR(19)'
  162.     ),
  163.     'mysql' => array(
  164.         'boolean'   => 'DECIMAL(1,0)',
  165.         'char'      => 'CHAR',
  166.         'varchar'   => 'VARCHAR',
  167.         'smallint'  => 'SMALLINT',
  168.         'integer'   => 'INTEGER',
  169.         'bigint'    => 'BIGINT',
  170.         'decimal'   => 'DECIMAL',
  171.         'single'    => 'FLOAT',
  172.         'double'    => 'DOUBLE',
  173.         'clob'      => 'LONGTEXT',
  174.         'date'      => 'CHAR(10)',
  175.         'time'      => 'CHAR(8)',
  176.         'timestamp' => 'CHAR(19)'
  177.     ),
  178.     'oci8' => array(
  179.         'boolean'   => 'NUMBER(1)',
  180.         'char'      => 'CHAR',
  181.         'varchar'   => 'VARCHAR2',
  182.         'smallint'  => 'NUMBER(6)',
  183.         'integer'   => 'NUMBER(11)',
  184.         'bigint'    => 'NUMBER(19)',
  185.         'decimal'   => 'NUMBER',
  186.         'single'    => 'REAL',
  187.         'double'    => 'DOUBLE PRECISION',
  188.         'clob'      => 'CLOB',
  189.         'date'      => 'CHAR(10)',
  190.         'time'      => 'CHAR(8)',
  191.         'timestamp' => 'CHAR(19)'
  192.     ),
  193.     'pgsql' => array(
  194.         'boolean'   => 'DECIMAL(1,0)',
  195.         'char'      => 'CHAR',
  196.         'varchar'   => 'VARCHAR',
  197.         'smallint'  => 'SMALLINT',
  198.         'integer'   => 'INTEGER',
  199.         'bigint'    => 'BIGINT',
  200.         'decimal'   => 'DECIMAL',
  201.         'single'    => 'REAL',
  202.         'double'    => 'DOUBLE PRECISION',
  203.         'clob'      => 'TEXT',
  204.         'date'      => 'CHAR(10)',
  205.         'time'      => 'CHAR(8)',
  206.         'timestamp' => 'CHAR(19)'
  207.     ),
  208.     'sqlite' => array(
  209.         'boolean'   => 'BOOLEAN',
  210.         'char'      => 'CHAR',
  211.         'varchar'   => 'VARCHAR',
  212.         'smallint'  => 'SMALLINT',
  213.         'integer'   => 'INTEGER',
  214.         'bigint'    => 'BIGINT',
  215.         'decimal'   => 'NUMERIC',
  216.         'single'    => 'FLOAT',
  217.         'double'    => 'DOUBLE',
  218.         'clob'      => 'CLOB',
  219.         'date'      => 'DATE',
  220.         'time'      => 'TIME',
  221.         'timestamp' => 'TIMESTAMP'
  222.     )
  223. );
  224.  
  225.  
  226. /**
  227. * US-English error messages.  DB_Table has no other embedded strings, so
  228. * if you want to internationalize, you can modify these for your
  229. * language; just set them before or after including DB_Table.
  230. */
  231. if (isset($GLOBALS['_DB_TABLE']['error'])) {
  232.     $GLOBALS['_DB_TABLE']['error'= array(
  233.         DB_TABLE_ERR_NOT_DB_OBJECT       => 'First parameter must be a DB object',
  234.         DB_TABLE_ERR_PHPTYPE             => 'DB phptype not supported',
  235.         DB_TABLE_ERR_SQL_UNDEF           => 'Select key not in map',
  236.         DB_TABLE_ERR_INS_COL_NOMAP       => 'Insert column not in map',
  237.         DB_TABLE_ERR_INS_COL_REQUIRED    => 'Insert data must be set and non-null for column',
  238.         DB_TABLE_ERR_INS_DATA_INVALID    => 'Insert data not valid for column',
  239.         DB_TABLE_ERR_UPD_COL_NOMAP       => 'Update column not in map',
  240.         DB_TABLE_ERR_UPD_COL_REQUIRED    => 'Update column must be set and non-null',
  241.         DB_TABLE_ERR_UPD_DATA_INVALID    => 'Update data not valid for column',
  242.         DB_TABLE_ERR_CREATE_FLAG         => 'Create flag not valid',
  243.         DB_TABLE_ERR_IDX_NO_COLS         => 'No columns for index',
  244.         DB_TABLE_ERR_IDX_COL_UNDEF       => 'Column not in map for index',
  245.         DB_TABLE_ERR_IDX_TYPE            => 'Type not valid for index',
  246.         DB_TABLE_ERR_DECLARE_STRING      => 'String column declaration not valid',
  247.         DB_TABLE_ERR_DECLARE_DECIMAL     => 'Decimal column declaration not valid',
  248.         DB_TABLE_ERR_DECLARE_TYPE        => 'Column type not valid',
  249.         DB_TABLE_ERR_VALIDATE_TYPE       => 'Cannot validate for unknown type on column',
  250.         DB_TABLE_ERR_DECLARE_COLNAME     => 'Column name not valid',
  251.         DB_TABLE_ERR_DECLARE_IDXNAME     => 'Index name not valid',
  252.         DB_TABLE_ERR_DECLARE_TYPE        => 'Column type not valid'
  253.     );
  254. }
  255.  
  256.  
  257. /**
  258. * DB_Table is a database API and data type SQL abstraction class.
  259. * DB_Table provides database API abstraction, data type abstraction,
  260. * automated SELECT, INSERT, and UPDATE queries, automated table
  261. * creation, automated validation of inserted/updated column values,
  262. * and automated creation of QuickForm elemnts based on the column
  263. * definitions.
  264. * $Id: Table.php,v 1.42 2004/04/23 15:23:44 pmjones Exp $
  265. *
  266. @author Paul M. Jones <pmjones@ciaweb.net>
  267. @version 0.18 alpha
  268. *
  269. @package DB_Table
  270. */
  271.  
  272. class DB_Table {
  273.     
  274.     
  275.     /**
  276.     * 
  277.     * The PEAR DB object that connects to the database.
  278.     * 
  279.     * @access public
  280.     * 
  281.     * @var object 
  282.     * 
  283.     */
  284.     
  285.     var $db = null;
  286.     
  287.     
  288.     /**
  289.     * 
  290.     * The table or view in the database to which this object binds.
  291.     * 
  292.     * @access public
  293.     * 
  294.     * @var string 
  295.     * 
  296.     */
  297.     
  298.     var $table = null;
  299.     
  300.     
  301.     /**
  302.     * 
  303.     * Associative array of column definitions.
  304.     * 
  305.     * @access public
  306.     * 
  307.     * @var array 
  308.     * 
  309.     */
  310.     
  311.     var $col = array();
  312.     
  313.     
  314.     /**
  315.     * 
  316.     * Associative array of index definitions.
  317.     * 
  318.     * @access public
  319.     * 
  320.     * @var array 
  321.     * 
  322.     */
  323.     
  324.     var $idx = array();
  325.     
  326.     
  327.     /**
  328.     * 
  329.     * Baseline SQL SELECT mappings for select() and selectResult().
  330.     * 
  331.     * @access public
  332.     * 
  333.     * @var array 
  334.     * 
  335.     */
  336.     
  337.     var $sql = array();
  338.     
  339.     
  340.     /**
  341.     * 
  342.     * Whether or not to automatically validate data at insert-time.
  343.     * 
  344.     * @access private
  345.     * 
  346.     * @var bool 
  347.     * 
  348.     */
  349.     
  350.     var $_valid_insert = true;
  351.     
  352.     
  353.     /**
  354.     * 
  355.     * Whether or not to automatically validate data at update-time.
  356.     * 
  357.     * @access private
  358.     * 
  359.     * @var bool 
  360.     * 
  361.     */
  362.     
  363.     var $_valid_update = true;
  364.     
  365.     
  366.     /**
  367.     * 
  368.     * When calling select() and selectResult(), use this fetch mode (usually
  369.     * a DB_FETCHMODE_* constant).  If null, uses whatever is set in the $db
  370.     * PEAR DB object.
  371.     * 
  372.     * @access public
  373.     * 
  374.     * @var int 
  375.     * 
  376.     */
  377.     
  378.     var $fetchmode = null;
  379.     
  380.     
  381.     /**
  382.     * 
  383.     * When fetchmode is DB_FETCHMODE_OBJECT, use this class for each
  384.     * returned row.  If null, uses whatever is set in the $db
  385.     * PEAR DB object.
  386.     * 
  387.     * @access public
  388.     * 
  389.     * @var string 
  390.     * 
  391.     */
  392.     
  393.     var $fetchmode_object_class = null;
  394.     
  395.     
  396.     /**
  397.     * 
  398.     * If there is an error on instantiation, this captures that error.
  399.     *
  400.     * This property is used only for errors encountered in the constructor
  401.     * at instantiation time.  To check if there was an instantiation error...
  402.     *
  403.     * <code>
  404.     * $obj =& new DB_Table();
  405.     * if ($obj->error) {
  406.     *     // ... error handling code here ...
  407.     * }
  408.     * </code>
  409.     * 
  410.     * @var object PEAR_Error 
  411.     * 
  412.     */
  413.     
  414.     var $error = null;
  415.     
  416.     
  417.     /**
  418.     * 
  419.     * Specialized version of throwError() modeled on PEAR_Error.
  420.     * 
  421.     * Throws a PEAR_Error with a DB_Table error message based on a
  422.     * DB_Table constant error code.
  423.     * 
  424.     * @static
  425.     * 
  426.     * @access public
  427.     * 
  428.     * @param string $code A DB_Table error code constant.
  429.     * 
  430.     * @param string $extra Extra text for the error (in addition to the
  431.     *  regular error message).
  432.     * 
  433.     * @return object PEAR_Error 
  434.     * 
  435.     */
  436.     
  437.     function &throwError($code$extra = null)
  438.     {
  439.         // get the error message text based on the error code
  440.         $text $GLOBALS['_DB_TABLE']['error'][$code];
  441.         
  442.         // add any additional error text
  443.         if ($extra{
  444.             $text .= ' ' $extra;
  445.         }
  446.         
  447.         // done!
  448.         return PEAR::throwError($text$code);
  449.     }
  450.     
  451.     
  452.     /**
  453.     * 
  454.     * Constructor.
  455.     * 
  456.     * If there is an error on instantiation, $this->error will be
  457.     * populated with the PEAR_Error.
  458.     * 
  459.     * @access public
  460.     * 
  461.     * @param object &$db A PEAR DB object.
  462.     * 
  463.     * @param string $table The table name to connect to in the database.
  464.     * 
  465.     * @param mixed $create The automatic table creation mode to pursue:
  466.     *  boolean false to not attempt creation, 'safe' to
  467.     *  create the table only if it does not exist, or
  468.     *  'drop' to drop any existing table with the same name
  469.     *  and re-create it.
  470.     * 
  471.     * @return object DB_Table 
  472.     * 
  473.     */
  474.     
  475.     function DB_Table(&$db$table$create = false)
  476.     {
  477.         // is the first argument a DB object?
  478.         if (is_subclass_of($db'db_common')) {
  479.             $this->error =DB_Table::throwError(DB_TABLE_ERR_NOT_DB_OBJECT);
  480.             return;
  481.         }
  482.         
  483.         // is the RDBMS supported?
  484.         if (DB_Table::supported($db->phptype)) {
  485.             $this->error =DB_Table::throwError(
  486.                 DB_TABLE_ERR_PHPTYPE,
  487.                 "({$db->phptype})"
  488.             );
  489.             return;
  490.         }
  491.         
  492.         // set the class properties
  493.         $this->db =$db;
  494.         $this->table = $table;
  495.         
  496.         // should we attempt table creation?
  497.         if ($create{
  498.             // yes, attempt to create the table with the appropriate
  499.             // flag.
  500.             $result $this->create($create);
  501.             if (PEAR::isError($result)) {
  502.                 // problem creating the table
  503.                 $this->error =$result;
  504.                 return;
  505.             }
  506.         }
  507.     }
  508.     
  509.     
  510.     /**
  511.     * 
  512.     * Is a particular RDBMS supported by DB_Table?
  513.     * 
  514.     * @static
  515.     * 
  516.     * @access public
  517.     * 
  518.     * @param string $phptype The RDBMS type for PHP.
  519.     * 
  520.     * @return bool True if supported, false if not.
  521.     * 
  522.     */
  523.     
  524.     function supported($phptype)
  525.     {
  526.         $supported array_keys($GLOBALS['_DB_TABLE']['type']);
  527.         return in_array(strtolower($phptype)$supported);
  528.     }
  529.     
  530.     
  531.     
  532.     /**
  533.     * 
  534.     * Returns all or part of the $this->col property array.
  535.     * 
  536.     * @access public
  537.     * 
  538.     * @param mixed $col If null, returns the $this->col property array
  539.     *  as it is.  If string, returns that column name from the $this->col
  540.     *  array. If an array, returns those columns named as the array
  541.     *  values from the $this->col array as an array.
  542.     *
  543.     * @return mixed All or part of the $this->col property array, or
  544.     *  boolean false if no matching column names are found.
  545.     * 
  546.     */
  547.     
  548.     function getColumns($col = null)
  549.     {
  550.         // by default, return all column definitions
  551.         if (is_null($col)) {
  552.             return $this->col;
  553.         }
  554.         
  555.         // if the param is a string, only return the column definition
  556.         // named by the that string
  557.         if (is_string($col)) {
  558.             if (isset($this->col[$col])) {
  559.                 return $this->col[$col];
  560.             else {
  561.                 return false;
  562.             }
  563.         }
  564.         
  565.         // if the param is a sequential array of column names,
  566.         // return only those columns named in that array
  567.         if (is_array($col)) {
  568.             $set = array();
  569.             foreach ($col as $name{
  570.                 $set[$name$this->getColumns($name);
  571.             }
  572.             
  573.             if (count($set== 0{
  574.                 return false;
  575.             else {
  576.                 return $set;
  577.             }
  578.         }
  579.         
  580.         // param was not null, string, or array
  581.         return false;
  582.     }
  583.     
  584.     
  585.     /**
  586.     * 
  587.     * Returns all or part of the $this->idx property array.
  588.     * 
  589.     * @access public
  590.     * 
  591.     * @param string $col If specified, returns only this index key
  592.     *  from the $this->col property array.
  593.     * 
  594.     * @return array All or part of the $this->idx property array.
  595.     * 
  596.     */
  597.     
  598.     function getIndexes($idx = null)
  599.     {
  600.         // by default, return all index definitions
  601.         if (is_null($idx)) {
  602.             return $this->idx;
  603.         }
  604.         
  605.         // if the param is a string, only return the index definition
  606.         // named by the that string
  607.         if (is_string($idx)) {
  608.             if (isset($this->idx[$idx])) {
  609.                 return $this->idx[$idx];
  610.             else {
  611.                 return false;
  612.             }
  613.         }
  614.         
  615.         // if the param is a sequential array of index names,
  616.         // return only those indexes named in that array
  617.         if (is_array($idx)) {
  618.             $set = array();
  619.             foreach ($idx as $name{
  620.                 $set[$name$this->getIndexes($name);
  621.             }
  622.             
  623.             if (count($set== 0{
  624.                 return false;
  625.             else {
  626.                 return $set;
  627.             }
  628.         }
  629.         
  630.         // param was not null, string, or array
  631.         return false;
  632.     }
  633.     
  634.     
  635.     /**
  636.     *
  637.     * Select rows from the table using one of the 'DB::get*()' methods.
  638.     * 
  639.     * @access public
  640.     * 
  641.     * @param string $sqlkey The name of the SQL SELECT to use from the
  642.     *  $this->sql property array.
  643.     * 
  644.     * @param string $filter Ad-hoc SQL snippet to AND with the default
  645.     *  SELECT WHERE clause.
  646.     * 
  647.     * @param string $order Ad-hoc SQL snippet to override the default
  648.     *  SELECT ORDER BY clause.
  649.     * 
  650.     * @param int $start The row number to start listing from in the
  651.     *  result set.
  652.     * 
  653.     * @param int $count The number of rows to list in the result set.
  654.     * 
  655.     * @return mixed An array of records from the table (if anything but
  656.     *  'getOne'), a single value (if 'getOne'), or a PEAR_Error object.
  657.     *
  658.     * @see DB::getAll()
  659.     *
  660.     * @see DB::getAssoc()
  661.     *
  662.     * @see DB::getCol()
  663.     *
  664.     * @see DB::getOne()
  665.     *
  666.     * @see DB::getRow()
  667.     *
  668.     * @see DB_Table::_swapModes()
  669.     *
  670.     */
  671.     
  672.     function select($sqlkey$filter = null$order = null,
  673.         $start = null$count = null)
  674.     {
  675.         // build the base command
  676.         $sql $this->buildSQL($sqlkey$filter$order$start$count);
  677.         
  678.         // set the get*() method name
  679.         if (isset($this->sql[$sqlkey]['get'])) {
  680.             $method ucwords(strtolower(trim($this->sql[$sqlkey]['get'])));
  681.             $method = "get$method";
  682.         else {
  683.             $method 'getAll';
  684.         }
  685.         
  686.         // DB_Table assumes you are using a shared PEAR DB object.  Other
  687.         // scripts using the same object probably expect its fetchmode
  688.         // not to change, unless they change it themselves.  Thus, to
  689.         // provide friendly mode-swapping, we will restore these modes
  690.         // afterwards.
  691.         $restore_mode $this->db->fetchmode;
  692.         $restore_class $this->db->fetchmode_object_class;
  693.         
  694.         // swap modes
  695.         $this->_swapModes($this->fetchmode$this->fetchmode_object_class);
  696.         
  697.         // get the result
  698.         $result $this->db->$method($sql);
  699.             
  700.         // swap modes back
  701.         $this->_swapModes($restore_mode$restore_class);
  702.         
  703.         // return the result
  704.         return $result;
  705.     }
  706.     
  707.     
  708.     /**
  709.     *
  710.     * Select rows from the table as a DB_Result object.
  711.     * 
  712.     * @access public
  713.     * 
  714.     * @param string $sqlkey The name of the SQL SELECT to use from the
  715.     *  $this->sql property array.
  716.     * 
  717.     * @param string $filter Ad-hoc SQL snippet to add to the default
  718.     *  SELECT WHERE clause.
  719.     * 
  720.     * @param string $order Ad-hoc SQL snippet to override the default
  721.     *  SELECT ORDER BY clause.
  722.     * 
  723.     * @param int $start The record number to start listing from in the
  724.     *  result set.
  725.     * 
  726.     * @param int $count The number of records to list in the result set.
  727.     * 
  728.     * @return mixed A PEAR_Error on failure, or a DB_Result object on
  729.     *  success.
  730.     *
  731.     * @see DB_Table::_swapModes()
  732.     *
  733.     */
  734.     
  735.     function selectResult($sqlkey$filter = null$order = null
  736.         $start = null$count = null)
  737.     {
  738.         // build the base command
  739.         $sql $this->buildSQL($sqlkey$filter$order$start$count);
  740.         
  741.         // DB_Table assumes you are using a shared PEAR DB object.  Other
  742.         // scripts using the same object probably expect its fetchmode
  743.         // not to change, unless they change it themselves.  Thus, to
  744.         // provide friendly mode-swapping, we will restore these modes
  745.         // afterwards.
  746.         $restore_mode $this->db->fetchmode;
  747.         $restore_class $this->db->fetchmode_object_class;
  748.         
  749.         // swap modes
  750.         $this->_swapModes($this->fetchmode$this->fetchmode_object_class);
  751.         
  752.         // get the result
  753.         $result =$this->db->query($sql);
  754.         
  755.         // swap modes back
  756.         $this->_swapModes($restore_mode$restore_class);
  757.         
  758.         // return the result
  759.         return $result;
  760.     }
  761.     
  762.     
  763.     /**
  764.     * 
  765.     * Change the $this->db PEAR DB object fetchmode and
  766.     * fetchmode_object_class.
  767.     * 
  768.     * Becase DB_Table objects tend to use the same PEAR DB object, it
  769.     * may sometimes be useful to have one object return results in one
  770.     * mode, and have another object return results in a different mode.
  771.     * This method allows us to switch DB fetch modes on the fly.
  772.     * 
  773.     * @access private
  774.     * 
  775.     * @param string $new_mode A DB_FETCHMODE_* constant.  If null,
  776.     *  defaults to whatever the DB object is currently using.
  777.     * 
  778.     * @param string $new_class The object class to use for results when
  779.     *  the $db object is in DB_FETCHMODE_OBJECT fetch mode.  If null,
  780.     *  defaults to whatever the the DB object is currently using.
  781.     * 
  782.     * @return void 
  783.     * 
  784.     */
  785.     
  786.     function _swapModes($new_mode$new_class)
  787.     {
  788.         // get the old (current) mode and class
  789.         $old_mode $this->db->fetchmode;
  790.         $old_class $this->db->fetchmode_object_class;
  791.         
  792.         // don't need to swap anything if the new modes are both
  793.         // null or if the old and new modes already match.
  794.         if ((is_null($new_mode&& is_null($new_class)) ||
  795.             ($old_mode == $new_mode && $old_class == $new_class)) {
  796.             return;
  797.         }
  798.         
  799.         // set the default new mode
  800.         if (is_null($new_mode)) {
  801.             $new_mode $old_mode;
  802.         }
  803.         
  804.         // set the default new class
  805.         if (is_null($new_class)) {
  806.             $new_class $old_class;
  807.         }
  808.         
  809.         // swap modes
  810.         $this->db->setFetchMode($new_mode$new_class);
  811.     }
  812.     
  813.     
  814.     /**
  815.     * 
  816.     * Build the SQL command from a specified $this->sql element.
  817.     * 
  818.     * @access public
  819.     * 
  820.     * @param string $sqlkey The $this->sql key to use as the basis for the
  821.     *  SQL query string.
  822.     * 
  823.     * @param string $filter A filter to add to the WHERE clause of the
  824.     *  defined SELECT in $this->sql.
  825.     * 
  826.     * @param string $order An ORDER clause to override the defined order
  827.     *  in $this->sql.
  828.     * 
  829.     * @param int $start The row number to start listing from in the
  830.     *  result set.
  831.     * 
  832.     * @param int $count The number of rows to list in the result set.
  833.     * 
  834.     * @return mixed A PEAR_Error on failure, or an SQL command string on
  835.     *  success.
  836.     * 
  837.     */
  838.     
  839.     function buildSQL($sqlkey$filter = null$order = null,
  840.         $start = null$count = null)
  841.     {
  842.         // does the SQL SELECT key exist?
  843.         $tmp array_keys($this->sql);
  844.         if (in_array($sqlkey$tmp)) {
  845.             return $this->throwError(
  846.                 DB_TABLE_ERR_SQL_UNDEF,
  847.                 "('$sqlkey')"
  848.             );
  849.         }
  850.         
  851.         // the SQL clause parts and their default values
  852.         $part = array(
  853.             'select' => '*',
  854.             'from'   => $this->table,
  855.             'join'   => null,
  856.             'where'  => null,
  857.             'group'  => null,
  858.             'having' => null,
  859.             'order'  => null
  860.         );
  861.         
  862.         // loop through each possible clause
  863.         foreach ($part as $key => $val{
  864.             if (isset($this->sql[$sqlkey][$key])) {
  865.                 continue;
  866.             else {
  867.                 $part[$key$this->sql[$sqlkey][$key];
  868.             }
  869.         }
  870.         
  871.         // add the filter to the WHERE part
  872.         if ($filter{
  873.             if ($part['where']{
  874.                 $part['where'.= $filter;
  875.             else {
  876.                 $part['where'.= " AND ($filter)";
  877.             }
  878.         }
  879.         
  880.         // override the ORDER part
  881.         if ($order{
  882.             $part['order'$order;
  883.         }
  884.         
  885.         // build up the command string form the parts
  886.         $cmd '';
  887.         foreach ($part as $key => $val{
  888.             
  889.             // if the part value has not been set, skip it
  890.             if ($val{
  891.                 continue;
  892.             }
  893.             
  894.             switch ($key{
  895.             
  896.             case 'join':
  897.                 $cmd .= strtoupper($key. " $val\n";
  898.                 break;
  899.                 
  900.             case 'group':
  901.             case 'order':
  902.                 $cmd .= strtoupper($key. " BY $val\n";
  903.                 break;
  904.                 
  905.             default:
  906.                 $cmd .= strtoupper($key. " $val\n";
  907.                 break;
  908.             
  909.             }
  910.         }
  911.         
  912.         // add LIMIT if requested
  913.         if (is_null($start&& is_null($count)) {
  914.             $cmd $this->db->modifyLimitQuery(
  915.                 $cmd$start$count);
  916.         }
  917.         
  918.         return $cmd;
  919.     }
  920.     
  921.     
  922.     /**
  923.     *
  924.     * Insert a single table row after validating through validInsert().
  925.     * 
  926.     * @access public
  927.     * 
  928.     * @param array $data An associative array of key-value pairs where
  929.     *  the key is the column name and the value is the column value.  This
  930.     *  is the data that will be inserted into the table.  Data is checked
  931.     *  against the column data type for validity.
  932.     * 
  933.     * @return mixed Void on success, a PEAR_Error object on failure.
  934.     *
  935.     * @see validInsert()
  936.     * 
  937.     * @see DB::autoExecute()
  938.     * 
  939.     */
  940.         
  941.     function insert($data)
  942.     {
  943.         // validate the data if auto-validation is turned on
  944.         if ($this->_valid_insert{
  945.             $result $this->validInsert($data);
  946.             if (PEAR::isError($result)) {
  947.                 return $result;
  948.             }
  949.         }
  950.         
  951.         return $this->db->autoExecute($this->table$data,
  952.             DB_AUTOQUERY_INSERT);
  953.     }
  954.     
  955.     
  956.     /**
  957.     * 
  958.     * Turn on (or off) automatic validation of inserted data.
  959.     * 
  960.     * @access public
  961.     * 
  962.     * @param bool $flag True to turn on auto-validation, false to turn it off.
  963.     * 
  964.     * @return void 
  965.     * 
  966.     */
  967.     
  968.     function autoValidInsert($flag = true)
  969.     {
  970.         if ($flag === true || $flag === false{
  971.             $this->_valid_insert $flag;
  972.         elseif ($flag{
  973.             $this->_valid_insert = true;
  974.         else {
  975.             $this->_valid_insert = false;
  976.         }
  977.     }
  978.     
  979.     
  980.     /**
  981.     *
  982.     * Validate an array for insertion into the table.
  983.     * 
  984.     * @access public
  985.     * 
  986.     * @param array $data An associative array of key-value pairs where
  987.     *  the key is the column name and the value is the column value.  This
  988.     *  is the data that will be inserted into the table.  Data is checked
  989.     *  against the column data type for validity.
  990.     * 
  991.     * @return mixed Boolean true on success, a PEAR_Error object on
  992.     *  failure.
  993.     *
  994.     * @see insert()
  995.     * 
  996.     */
  997.         
  998.     function validInsert(&$data)
  999.     {
  1000.         // loop through the data, and disallow insertion of unmapped
  1001.         // columns
  1002.         foreach ($data as $col => $val{
  1003.             if (isset($this->col[$col])) {
  1004.                 return $this->throwError(
  1005.                     DB_TABLE_ERR_INS_COL_NOMAP,
  1006.                     "('$col')"
  1007.                 );
  1008.             }
  1009.         }
  1010.         
  1011.         // loop through each column mapping, and check the data to be
  1012.         // inserted into it against the column data type. we loop through
  1013.         // column mappings instead of the insert data to make sure that
  1014.         // all necessary columns are being inserted.
  1015.         foreach ($this->col as $col => $val{
  1016.             
  1017.             // is the value allowed to be null?
  1018.             if (isset($val['require']&&
  1019.                 $val['require'== true &&
  1020.                 (isset($data[$col]|| is_null($data[$col]))) {
  1021.                 return $this->throwError(
  1022.                     DB_TABLE_ERR_INS_COL_REQUIRED,
  1023.                     "'$col'"
  1024.                 );
  1025.             }
  1026.             
  1027.             // does the value to be inserted match the column data type?
  1028.             if (isset($data[$col]&&
  1029.                 $this->isValid($data[$col]$col)) {
  1030.                 return $this->throwError(
  1031.                     DB_TABLE_ERR_INS_DATA_INVALID,
  1032.                     "'$col' ('$data[$col]')"
  1033.                 );
  1034.             }
  1035.         }
  1036.         
  1037.         return true;
  1038.     }
  1039.     
  1040.     
  1041.     /**
  1042.     *
  1043.     * Update table row(s) matching a custom WHERE clause, after checking
  1044.     * against validUpdate().
  1045.     * 
  1046.     * @access public
  1047.     * 
  1048.     * @param array $data An associative array of key-value pairs where
  1049.     *  the key is the column name and the value is the column value.  These
  1050.     *  are the columns that will be updated with new values.
  1051.     * 
  1052.     * @param string $where An SQL WHERE clause limiting which records
  1053.     *  are to be updated.
  1054.     * 
  1055.     * @return mixed Void on success, a PEAR_Error object on failure.
  1056.     *
  1057.     * @see validUpdate()
  1058.     *
  1059.     * @see DB::autoExecute()
  1060.     * 
  1061.     */
  1062.     
  1063.     function update($data$where)
  1064.     {
  1065.         // validate the data if auto-validation is turned on
  1066.         if ($this->_valid_update{
  1067.             $result $this->validUpdate($data);
  1068.             if (PEAR::isError($result)) {
  1069.                 return $result;
  1070.             }
  1071.         }
  1072.         
  1073.         return $this->db->autoExecute($this->table$data,
  1074.             DB_AUTOQUERY_UPDATE$where);
  1075.     }
  1076.     
  1077.     
  1078.     /**
  1079.     * 
  1080.     * Turn on (or off) automatic validation of updated data.
  1081.     * 
  1082.     * @access public
  1083.     * 
  1084.     * @param bool $flag True to turn on auto-validation, false to turn it off.
  1085.     * 
  1086.     * @return void 
  1087.     * 
  1088.     */
  1089.     
  1090.     function autoValidUpdate($flag = true)
  1091.     {
  1092.         if ($flag === true || $flag === false{
  1093.             $this->_valid_update $flag;
  1094.         elseif ($flag{
  1095.             $this->_valid_update = true;
  1096.         else {
  1097.             $this->_valid_update = false;
  1098.         }
  1099.     }
  1100.     
  1101.     
  1102.     /**
  1103.     *
  1104.     * Validate an array for updating the table.
  1105.     * 
  1106.     * @access public
  1107.     * 
  1108.     * @param array $data An associative array of key-value pairs where
  1109.     *  the key is the column name and the value is the column value.  This
  1110.     *  is the data that will be inserted into the table.  Data is checked
  1111.     *  against the column data type for validity.
  1112.     * 
  1113.     * @return mixed Boolean true on success, a PEAR_Error object on
  1114.     *  failure.
  1115.     *
  1116.     * @see update()
  1117.     * 
  1118.     */
  1119.         
  1120.     function validUpdate(&$data)
  1121.     {
  1122.         // loop through each data element, and check the
  1123.         // data to be updated against the column data type.
  1124.         foreach ($data as $col => $val{
  1125.             
  1126.             // does the column exist?
  1127.             if (isset($this->col[$col])) {
  1128.                 return $this->throwError(
  1129.                     DB_TABLE_ERR_UPD_COL_NOMAP,
  1130.                     "('$col')"
  1131.                 );
  1132.             }
  1133.             
  1134.             // the column definition
  1135.             $defn $this->col[$col];
  1136.             
  1137.             // is it allowed to be null?
  1138.             if (isset($defn['require']&&
  1139.                 $defn['require'== true &&
  1140.                 isset($data[$col]&&
  1141.                 is_null($data[$col])) {
  1142.                 return $this->throwError(
  1143.                     DB_TABLE_ERR_UPD_COL_REQUIRED,
  1144.                     $col
  1145.                 );
  1146.             }
  1147.             
  1148.             // does the value to be inserted match the column data type?
  1149.             if ($this->isValid($data[$col]$col)) {
  1150.                 return $this->throwError(
  1151.                     DB_TABLE_ERR_UPD_DATA_INVALID,
  1152.                     "$col ('$data[$col]')"
  1153.                 );
  1154.             }
  1155.         }
  1156.         
  1157.         return true;
  1158.     }
  1159.     
  1160.     
  1161.     /**
  1162.     *
  1163.     * Delete table rows matching a custom WHERE clause.
  1164.     * 
  1165.     * @access public
  1166.     * 
  1167.     * @param string $where The WHERE clause for the delete command.
  1168.     *
  1169.     * @return mixed Void on success or a PEAR_Error object on failure.
  1170.     *
  1171.     * @see DB::query()
  1172.     * 
  1173.     */
  1174.     
  1175.     function delete($where)
  1176.     {
  1177.         return $this->db->query("DELETE FROM $this->table WHERE $where");
  1178.     }
  1179.     
  1180.     
  1181.     /**
  1182.     *
  1183.     * Generate a sequence value; sequence name defaults to the table name.
  1184.     * 
  1185.     * @access public
  1186.     * 
  1187.     * @param string $seq_name The sequence name; defaults to _table_id.
  1188.     * 
  1189.     * @return integer The next value in the sequence.
  1190.     *
  1191.     * @see DB::nextID()
  1192.     *
  1193.     */
  1194.     
  1195.     function nextID($seq_name = null)
  1196.     {
  1197.         if (is_null($seq_name)) {
  1198.             $seq_name = "_{$this->table}_id";
  1199.         } else {
  1200.             $seq_name = "_{$this->table}_{$seq_name}";
  1201.         }
  1202.         
  1203.         return $this->db->nextId($seq_name);
  1204.     }
  1205.     
  1206.     
  1207.     /**
  1208.     * 
  1209.     * Escape and enquote a value for use in an SQL query.
  1210.     * 
  1211.     * Helps makes user input safe against SQL injection attack.
  1212.     * 
  1213.     * @access public
  1214.     * 
  1215.     * @return string The value with quotes escaped, and inside single quotes.
  1216.     * 
  1217.     * @see DB_Common::quoteSmart()
  1218.     * 
  1219.     */
  1220.     
  1221.     function quote($val)
  1222.     {
  1223.         return $this->db->quoteSmart($val);
  1224.     }
  1225.     
  1226.     
  1227.     /**
  1228.     * 
  1229.     * Return a blank row array based on the column map.
  1230.     * 
  1231.     * The array keys are the column names, and all values are set to null.
  1232.     * 
  1233.     * @access public
  1234.     * 
  1235.     * @return array An associative array where the key is column name
  1236.     * and the value is null.
  1237.     * 
  1238.     */
  1239.     
  1240.     function getBlankRow()
  1241.     {
  1242.         $row = array();
  1243.         
  1244.         foreach ($this->col as $key => $val{
  1245.             $row[$key] = null;
  1246.         }
  1247.         
  1248.         $this->recast($row);
  1249.         
  1250.         return $row;
  1251.     }
  1252.     
  1253.     
  1254.     /**
  1255.     * 
  1256.     * Force array elements to the proper types for their columns.
  1257.     * 
  1258.     * This will not valiate the data, and will forcibly change the data
  1259.     * to match the recast-type.
  1260.     * 
  1261.     * The date, time, and timestamp recasting has special logic for
  1262.     * arrays coming from an HTML_QuickForm object so that the arrays
  1263.     * are converted into properly-formatted strings.
  1264.     * 
  1265.     * @todo If a column key holds an array of values (say from a multiple
  1266.     * select) then this method will not work properly; it will recast the
  1267.     * value to the string 'Array'.  Is this bad?
  1268.     * 
  1269.     * @access public
  1270.     * 
  1271.     * @param array &$data The data array to re-cast.
  1272.     *
  1273.     * @return void
  1274.     * 
  1275.     */
  1276.     
  1277.     function recast(&$data)
  1278.     {
  1279.         $keys = array_keys($data);
  1280.         
  1281.         foreach ($keys as $key) {
  1282.         
  1283.             if (! isset($this->col[$key])) {
  1284.                 continue;
  1285.             }
  1286.             
  1287.             unset($val);
  1288.             $val =& $data[$key];
  1289.             
  1290.             switch ($this->col[$key]['type']{
  1291.             
  1292.             case 'boolean':
  1293.                 $val = ($val) ? 1 : 0;
  1294.                 break;
  1295.                 
  1296.             case 'char':
  1297.             case 'varchar':
  1298.             case 'clob':
  1299.                 settype($val, 'string');
  1300.                 break;
  1301.                 
  1302.             case 'date':
  1303.             
  1304.                 if (is_array($val) &&
  1305.                     isset($val['Y']) &&
  1306.                     isset($val['m']) &&
  1307.                     isset($val['d'])) {
  1308.                     
  1309.                     // the date is in HTML_QuickForm format,
  1310.                     // convert into a string
  1311.                     $y = $val['Y'];
  1312.                     
  1313.                     $m = ($val['m'] < 10)
  1314.                         ? '0'.$val['m'] : $val['m'];
  1315.                         
  1316.                     $d = ($val['d'] < 10)
  1317.                         ? '0'.$val['d'] : $val['d'];
  1318.                         
  1319.                     $val = "$y-$m-$d";
  1320.                     
  1321.                 } else {
  1322.                 
  1323.                     // convert using the Date class
  1324.                     $tmp =& new Date($val);
  1325.                     $val = $tmp->format('%Y-%m-%d');
  1326.                     
  1327.                 }
  1328.                 
  1329.                 break;
  1330.             
  1331.             case 'time':
  1332.             
  1333.                 if (is_array($val) &&
  1334.                     isset($val['H']) &&
  1335.                     isset($val['i']) &&
  1336.                     isset($val['s'])) {
  1337.                     
  1338.                     // the time is in HTML_QuickForm format,
  1339.                     // convert into a string
  1340.                     $h = ($val['H'] < 10)
  1341.                         ? '0' . $val['H'] : $val['H'];
  1342.                     
  1343.                     $i = ($val['i'] < 10)
  1344.                         ? '0' . $val['i'] : $val['i'];
  1345.                         
  1346.                     $s = ($val['s'] < 10)
  1347.                         ? '0' . $val['s'] : $val['s'];
  1348.                         
  1349.                     $val = "$h:$i:$s";
  1350.                     
  1351.                 } else {
  1352.                     // date does not matter in this case, so
  1353.                     // pre 1970 and post 2040 are not an issue.
  1354.                     $tmp = strtotime(date('Y-m-d') . " $val");
  1355.                     $val = date('H:i:s', $tmp);
  1356.                 }
  1357.                 
  1358.                 break;
  1359.                 
  1360.             case 'timestamp':
  1361.                 if (is_array($val) &&
  1362.                     isset($val['Y']) &&
  1363.                     isset($val['m']) &&
  1364.                     isset($val['d']) &&
  1365.                     isset($val['H']) &&
  1366.                     isset($val['i']) &&
  1367.                     isset($val['s'])) {
  1368.                     
  1369.                     // timestamp is in HTML_QuickForm format,
  1370.                     // convert to a string
  1371.                     $y = $val['Y'];
  1372.                     
  1373.                     $m = ($val['m'] < 10)
  1374.                         ? '0'.$val['m'] : $val['m'];
  1375.                         
  1376.                     $d = ($val['d'] < 10)
  1377.                         ? '0'.$val['d'] : $val['d'];
  1378.                         
  1379.                     $h = ($val['H'] < 10)
  1380.                         ? '0' . $val['H'] : $val['H'];
  1381.                     
  1382.                     $i = ($val['i'] < 10)
  1383.                         ? '0' . $val['i'] : $val['i'];
  1384.                         
  1385.                     $s = ($val['s'] < 10)
  1386.                         ? '0' . $val['s'] : $val['s'];
  1387.                         
  1388.                     $val = "$y-$m-$d $h:$i:$s";
  1389.                     
  1390.                 } else {
  1391.                     // convert using the Date class
  1392.                     $tmp =& new Date($val);
  1393.                     $val = $tmp->format('%Y-%m-%d %H:&i:&s');
  1394.                 }
  1395.                 
  1396.                 break;
  1397.             
  1398.             case 'smallint':
  1399.             case 'integer':
  1400.             case 'bigint':
  1401.                 settype($val, 'integer');
  1402.                 break;
  1403.             
  1404.             case 'decimal':
  1405.             case 'single':
  1406.             case 'double':
  1407.                 settype($val, 'float');
  1408.                 break;
  1409.             }
  1410.         }
  1411.     }
  1412.     
  1413.     
  1414.     /**
  1415.     * 
  1416.     * Create the table based on $this->col and $this->idx.
  1417.     * 
  1418.     * @access public
  1419.     * 
  1420.     * @param mixed $flag Boolean false to abort the create attempt from
  1421.     * the start, 'drop' to drop the existing table and
  1422.     * re-create it, or 'safe' to only create the table if it
  1423.     * does not exist in the database.
  1424.     * 
  1425.     * @return mixed Boolean false if there was no attempt to create the
  1426.     * table, boolean true if the attempt succeeded, or a PEAR_Error if
  1427.     * the attempt failed.
  1428.     *
  1429.     * @see DB_Table_Manager::create()
  1430.     * 
  1431.     */
  1432.     
  1433.     function create($flag)
  1434.     {
  1435.         // are we OK to create the table?
  1436.         $ok = false;
  1437.         
  1438.         // check the create-flag
  1439.         switch ($flag) {
  1440.         
  1441.         case 'drop':
  1442.             // forcibly drop an existing table
  1443.             $this->db->query("DROP TABLE {$this->table}");
  1444.             $ok = true;
  1445.             break;
  1446.         
  1447.         case 'safe':
  1448.             // create only if table does not exist
  1449.             $list = $this->db->getListOf('tables');
  1450.             // ok to create only if table does not exist
  1451.             $ok (in_array($this->table$list));
  1452.             break;
  1453.             
  1454.         default:
  1455.             // unknown flag
  1456.             return $this->throwError(
  1457.                 DB_TABLE_ERR_CREATE_FLAG,
  1458.                 "('$flag')"
  1459.             );
  1460.         }
  1461.         
  1462.         // are we going to create the table?
  1463.         if (! $ok) {
  1464.             return false;
  1465.         } else {
  1466.             include_once 'DB/Table/Manager.php';
  1467.             return DB_Table_Manager::create(
  1468.                 $this->db$this->table$this->col$this->idx$flag
  1469.             );
  1470.         }
  1471.     }
  1472.     
  1473.     
  1474.     /**
  1475.     * 
  1476.     * Check if a value validates against the DB_Table data type for a
  1477.     * given column. This only checks that it matches the data type; it
  1478.     * does not do extended validation.
  1479.     * 
  1480.     * @access public
  1481.     * 
  1482.     * @param array $val A value to check against the column's DB_Table
  1483.     * data type.
  1484.     * 
  1485.     * @param array $col A column name from $this->col.
  1486.     * 
  1487.     * @return boolean True if the value validates (matches the
  1488.     * data type), false if not.
  1489.     * 
  1490.     * @see DB_Table_Valid
  1491.     * 
  1492.     */
  1493.     
  1494.     function isValid($val, $col)
  1495.     {
  1496.         // is the value null?
  1497.         if (is_null($val)) {
  1498.             // is the column required?
  1499.             if ($this->isRequired($col)) {
  1500.                 // yes, so not valid
  1501.                 return false;
  1502.             } else {
  1503.                 // not required, so it's valid
  1504.                 return true;
  1505.             }
  1506.         }
  1507.         
  1508.         // make sure we have the validation class
  1509.         include_once 'DB/Table/Valid.php';
  1510.         
  1511.         // validate values per the column type
  1512.         $map = array_keys($GLOBALS['_DB_TABLE']['type']['sqlite']);
  1513.         
  1514.         // is the column type on the map?
  1515.         if (! in_array($this->col[$col]['type']$map)) {
  1516.             return $this->throwError(
  1517.                 DB_TABLE_ERR_VALIDATE_TYPE,
  1518.                 "'$col' ('{$this->col[$col]['type']}')"
  1519.             );
  1520.         }
  1521.         
  1522.         // validate for the type
  1523.         switch ($this->col[$col]['type']{
  1524.         
  1525.         case 'char':
  1526.         case 'varchar':
  1527.             $result = DB_Table_Valid::isChar(
  1528.                 $val,
  1529.                 $this->col[$col]['size']
  1530.             );
  1531.             break;
  1532.         
  1533.         case 'decimal':
  1534.             $result = DB_Table_Valid::isDecimal(
  1535.                 $val,
  1536.                 $this->col[$col]['size'],
  1537.                 $this->col[$col]['scope']
  1538.             );
  1539.             break;
  1540.             
  1541.         default:
  1542.             $result = call_user_func(
  1543.                 array(
  1544.                     'DB_Table_Valid',
  1545.                     'is' . ucwords($this->col[$col]['type'])
  1546.                 ),
  1547.                 $val
  1548.             );
  1549.             break;
  1550.         }
  1551.         
  1552.         // have we passed the check so far, and should we
  1553.         // also check for allowed values?
  1554.         if ($result && isset($this->col[$col]['qf_vals'])) {
  1555.             $result = in_array(
  1556.                 $val,
  1557.                 array_keys($this->col[$col]['qf_vals'])
  1558.             );
  1559.         }
  1560.         
  1561.         return $result;
  1562.     }
  1563.     
  1564.     
  1565.     /**
  1566.     * 
  1567.     * Is a specific column required to be set and non-null?
  1568.     * 
  1569.     * @access public
  1570.     * 
  1571.     * @param mixed $column The column to check against.
  1572.     * 
  1573.     * @return boolean True if required, false if not.
  1574.     * 
  1575.     */
  1576.     
  1577.     function isRequired($column)
  1578.     {
  1579.         if (isset($this->col[$column]['require']&&
  1580.             $this->col[$column]['require'== true{
  1581.             return true;
  1582.         } else {
  1583.             return false;
  1584.         }
  1585.     }
  1586.     
  1587.     
  1588.     /**
  1589.     * 
  1590.     * Create and return a QuickForm object based on table columns.
  1591.     *
  1592.     * @access public
  1593.     *
  1594.     * @param array $columns A sequential array of column names to use in
  1595.     * the form; if null, uses all columns.
  1596.     *
  1597.     * @param string $array_name By default, the form will use the names
  1598.     * of the columns as the names of the form elements.  If you pass
  1599.     * $array_name, the column names will become keys in an array named
  1600.     * for this parameter.
  1601.     * 
  1602.     * @param array $args An associative array of optional arguments to
  1603.     * pass to the QuickForm object.  The keys are...
  1604.     *
  1605.     * 'formName' : String, name of the form; defaults to the name of this
  1606.     * table.
  1607.     * 
  1608.     * 'method' : String, form method; defaults to 'post'.
  1609.     * 
  1610.     * 'action' : String, form action; defaults to
  1611.     * $_SERVER['REQUEST_URI'].
  1612.     * 
  1613.     * 'target' : String, form target target; defaults to '_self'
  1614.     * 
  1615.     * 'attributes' : Associative array, extra attributes for <form>
  1616.     * tag; the key is the attribute name and the value is attribute
  1617.     * value.
  1618.     * 
  1619.     * 'trackSubmit' : Boolean, whether to track if the form was
  1620.     * submitted by adding a special hidden field
  1621.     * 
  1622.     * @return object HTML_QuickForm
  1623.     * 
  1624.     * @see HTML_QuickForm
  1625.     * 
  1626.     * @see DB_Table_QuickForm
  1627.     * 
  1628.     */
  1629.     
  1630.     function &getForm($columns = null, $array_name = null, $args = array())
  1631.     {
  1632.         include_once 'DB/Table/QuickForm.php';
  1633.         $coldefs = $this->_getFormColDefs($columns);
  1634.         return DB_Table_QuickForm::getForm($coldefs$array_name$args);
  1635.     }
  1636.     
  1637.     
  1638.     /**
  1639.     * 
  1640.     * Adds elements and rules to a pre-existing HTML_QuickForm object.
  1641.     * 
  1642.     * @access public
  1643.     * 
  1644.     * @param object &$form An HTML_QuickForm object.
  1645.     * 
  1646.     * @param array $columns A sequential array of column names to use in
  1647.     * the form; if null, uses all columns.
  1648.     *
  1649.     * @param string $array_name By default, the form will use the names
  1650.     * of the columns as the names of the form elements.  If you pass
  1651.     * $array_name, the column names will become keys in an array named
  1652.     * for this parameter.
  1653.     * 
  1654.     * @return void
  1655.     * 
  1656.     * @see HTML_QuickForm
  1657.     * 
  1658.     * @see DB_Table_QuickForm
  1659.     * 
  1660.     */
  1661.     
  1662.     function addFormElements(&$form, $columns = null, $array_name = null)
  1663.     {
  1664.         include_once 'DB/Table/QuickForm.php';
  1665.         $coldefs = $this->_getFormColDefs($columns);
  1666.         DB_Table_QuickForm::addElements($form$coldefs$array_name);
  1667.         DB_Table_QuickForm::addRules($form$coldefs$array_name);
  1668.     }
  1669.     
  1670.     
  1671.     /**
  1672.     * 
  1673.     * Creates and returns an array of QuickForm elements based on an
  1674.     * array of DB_Table column names.
  1675.     * 
  1676.     * @access public
  1677.     * 
  1678.     * @param array $columns A sequential array of column names to use in
  1679.     * the form; if null, uses all columns.
  1680.     * 
  1681.     * @param string $array_name By default, the form will use the names
  1682.     * of the columns as the names of the form elements.  If you pass
  1683.     * $array_name, the column names will become keys in an array named
  1684.     * for this parameter.
  1685.     * 
  1686.     * @return array An array of HTML_QuickForm_Element objects.
  1687.     * 
  1688.     * @see HTML_QuickForm
  1689.     * 
  1690.     * @see DB_Table_QuickForm
  1691.     * 
  1692.     */
  1693.     
  1694.     function &getFormGroup($columns = null, $array_name = null)
  1695.     {
  1696.         include_once 'DB/Table/QuickForm.php';
  1697.         $coldefs = $this->_getFormColDefs($columns);
  1698.         return DB_Table_QuickForm::getGroup($coldefs$array_name);
  1699.     }
  1700.     
  1701.     
  1702.     /**
  1703.     * 
  1704.     * Creates and returns a single QuickForm element based on a DB_Table
  1705.     * column name.
  1706.     * 
  1707.     * @access public
  1708.     * 
  1709.     * @param string $column A DB_Table column name.
  1710.     * 
  1711.     * @param string $elemname The name to use for the generated QuickForm
  1712.     * element.
  1713.     * 
  1714.     * @return object HTML_QuickForm_Element
  1715.     * 
  1716.     * @see HTML_QuickForm
  1717.     * 
  1718.     * @see DB_Table_QuickForm
  1719.     * 
  1720.     */
  1721.     
  1722.     function &getFormElement($column, $elemname)
  1723.     {
  1724.         include_once 'DB/Table/QuickForm.php';
  1725.         $coldef = $this->_getFormColDefs($column);
  1726.         return DB_Table_QuickForm::getElement($coldef$elemname);
  1727.     }
  1728.     
  1729.     
  1730.     /**
  1731.     * 
  1732.     * Creates a column definition array suitable for DB_Table_QuickForm.
  1733.     * 
  1734.     * @access public
  1735.     * 
  1736.     * @param string|array $column_set A string column name, a sequential
  1737.     * array of columns names, or an associative array where the key is a
  1738.     * column name and the value is the default value for the generated
  1739.     * form element.  If null, uses all columns for this class.
  1740.     * 
  1741.     * @return array An array of columne defintions suitable for passing
  1742.     * to DB_Table_QuickForm.
  1743.     * 
  1744.     */
  1745.     
  1746.     function _getFormColDefs($column_set = null)
  1747.     {
  1748.         if (is_null($column_set)) {
  1749.             // no columns or columns+values; just return the $this->col
  1750.             // array.
  1751.             return $this->getColumns($column_set);
  1752.         }
  1753.         
  1754.         // check to see if the keys are sequential integers.  if so,
  1755.         // the $column_set is just a list of columns.
  1756.         settype($column_set, 'array');
  1757.         $keys = array_keys($column_set);
  1758.         $all_integer = true;
  1759.         foreach ($keys as $val) {
  1760.             if (! is_integer($val)) {
  1761.                 $all_integer = false;
  1762.                 break;
  1763.             }
  1764.         }
  1765.         
  1766.         if ($all_integer) {
  1767.         
  1768.             // the column_set is just a list of columns; get back the $this->col
  1769.             // array elements matching this list.
  1770.             $coldefs = $this->getColumns($column_set);
  1771.             
  1772.         } else {
  1773.             
  1774.             // the columns_set is an associative array where the key is a
  1775.             // column name and the value is the form element value.
  1776.             $coldefs = $this->getColumns($keys);
  1777.             foreach ($coldefs as $key => $val{
  1778.                 $coldefs[$key]['qf_setvalue'] = $column_set[$key];
  1779.             }
  1780.             
  1781.         }
  1782.         
  1783.         return $coldefs;
  1784.     }
  1785.     
  1786.  
  1787. }
  1788.  

Documentation generated on Mon, 11 Mar 2019 10:15:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.