PageRenderTime 62ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/LP/Data.php

https://github.com/radicaldesigns/jaguar
PHP | 136 lines | 109 code | 25 blank | 2 comment | 22 complexity | c04756eb2947f882e043c58df2ae3cf9 MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. <?php
  2. class Data {
  3. protected $dbcon;
  4. protected $datatable;
  5. protected $source;
  6. protected $sql_criteria = array();
  7. protected $_sql_select = array();
  8. protected $_nativeColumns = array( );
  9. protected $id_field = "id";
  10. protected $name_field = "name";
  11. protected $errors = array();
  12. function __construct( &$dbcon ) {
  13. $this->dbcon = &$dbcon;
  14. $this->setSource( $this->datatable );
  15. if (method_exists( $this, '_register_criteria_dynamic' )) {
  16. $this->_register_criteria_dynamic();
  17. }
  18. }
  19. function setSource( $sourcename = null ) {
  20. if (!isset($sourcename)) return false;
  21. if (!$cols = $this->_getColumnNames( $sourcename )) return false;
  22. $this->datatable = $sourcename;
  23. $this->_nativeColumns = $cols;
  24. }
  25. function setSelect( $expression_set ) {
  26. if (!is_array($expression_set)) return false;
  27. $this->_sql_select = array();
  28. foreach ($expression_set as $exp) {
  29. $this->addSelectExpression( $exp );
  30. }
  31. }
  32. function setCriteria( $expression_set ) {
  33. if (!is_array($expression_set)) return false;
  34. $this->sql_criteria = array();
  35. foreach ($expression_set as $exp) {
  36. $this->addCriteria ( $exp );
  37. }
  38. }
  39. function getCriteria () {
  40. if (empty($this->sql_criteria)) return false;
  41. return $this->sql_criteria;
  42. }
  43. function addColumn ( $name ) {
  44. if (!isColumn( $name )) return false;
  45. return $this->addSelectExpression( $name );
  46. }
  47. function isColumn( $exp ) {
  48. if ( empty( $this->_nativeColumns ) && !isset( $this->datatable )) return false;
  49. if ( empty( $this->_nativeColumns )) {
  50. $this->setSource( $this->datatable );
  51. if ( empty( $this->_nativeColumns )) return false;
  52. }
  53. if (array_search( $exp, $this->_nativeColumns ) === FALSE) return false;
  54. return true;
  55. }
  56. function addSelectExpression( $exp ) {
  57. if (!is_string($exp)) return false;
  58. if (array_search( $exp, $this->_sql_select )!==FALSE) return true;
  59. $this->_sql_select[] = $exp;
  60. return true;
  61. }
  62. function addCriteria( $exp ) {
  63. if (!is_string($exp)) return false;
  64. if (array_search( $exp, $this->sql_criteria )!==FALSE) return true;
  65. $this->sql_criteria[] = $exp;
  66. return true;
  67. }
  68. function hasData() {
  69. if (empty($this->source)) return false;
  70. if (method_exists($this->source, "RecordCount")) return $this->source->RecordCount();
  71. return true;
  72. }
  73. function _assembleSQL( $criteria = null ) {
  74. $sql = $this->_makeSelect();
  75. $sql .= $this->_makeSource();
  76. $sql .= isset( $criteria ) ?
  77. ' WHERE ' . $criteria
  78. : $this->_makeCriteria();
  79. return $sql;
  80. }
  81. function _makeSelect( ) {
  82. $output = "Select ";
  83. if (empty($this->_sql_select)) return $output . "*";
  84. return $output . join(", ", $this->_sql_select);
  85. }
  86. function _makeSource( ) {
  87. if ($this->datatable) return " FROM " . $this->datatable;
  88. trigger_error ("No datatable set in ". get_class($this));
  89. return false;
  90. }
  91. function _makeCriteria() {
  92. if (empty($this->sql_criteria)) return false;
  93. return ' WHERE ' . join( " AND ", $this->sql_criteria );
  94. }
  95. function _getColumnNames( $sourceDef ) {
  96. return AMP::get_column_names( $this->dbcon, $sourceDef );
  97. }
  98. # sets a minimum id for the next database insert
  99. function _setSourceIncrement( $new_value ){
  100. if ( $lowest_id = lowerlimitInsertID($this->datatable, $new_value)) {
  101. $this->dbcon->Execute( "ALTER TABLE ".$this->datatable." AUTO_INCREMENT = ".$lowest_id);
  102. }
  103. }
  104. # validation error handling code
  105. function addError( $error ) {
  106. $this->errors[] = $error;
  107. }
  108. function getErrors() {
  109. return join("<BR>" , $this->errors);
  110. }
  111. }