PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/berry/lib/berry/sql/sql.php

http://goodgirl.googlecode.com/
PHP | 243 lines | 153 code | 66 blank | 24 comment | 34 complexity | 299c283de5960e56f72dd6eff4b7a90e MD5 | raw file
  1. <?php /* `,
  2. ,\, #
  3. B E R R Y |/ ?
  4. <http://goodgirl.ru/berry> | ~ )\
  5. <http://goodgirl.ru/berry/license> /__/\ \____
  6. / \_/ \
  7. ???? zloy ? ???????? <http://lexa.cutenews.ru> / <_ ____,_-/\ __
  8. ---------------------------------------------------------/___/_____ \--'\|/----
  9. \/|*/
  10. class SQL extends SQL_Control {
  11. ////////////////////////////////////////////////////////////////////////////////
  12. function __construct($id = 0, $class = ''){ $class = strtolower($class ? $class : get_class($this));
  13. $this->alias = (substr($class, -4) == '_sql' ? substr($class, 0, -4) : $class);
  14. if (!$this->table){
  15. $this->table = self::$connection['database'];
  16. $this->table .= '.'.self::$connection['prefix'];
  17. $this->table .= inflector::tableize($this->alias);
  18. } elseif (!strpos($this->table, '.')){
  19. $this->table = self::$connection['database'].'.'.$this->table; }
  20. $this->table = trim($this->table, '.');
  21. $this->from[] = $this->table.' as '.$this->alias;
  22. $this->relations = self::deep_throat($this->alias);
  23. if ($id){ if (is_array($id))
  24. list($this->primary_key, $id) = array(key($id), reset($id));
  25. self::where($this->primary_key.' = ?', ($this->id = $id));
  26. }
  27. }
  28. ////////////////////////////////////////////////////////////////////////////////
  29. function into(){
  30. if (!$this->into)
  31. $this->into = func_get_args();
  32. return $this;
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////
  35. function values(){
  36. $args = func_get_args();
  37. if ($this->where){ foreach ($args[0] as $k => $v)
  38. $this[$k] = $v;
  39. return $this; }
  40. if (!is_array($args[0])){
  41. $this->values[] = $args;
  42. return $this;
  43. }
  44. if (!$this->into)
  45. $this->into = array_keys($args[0]);
  46. $this->values[] = array_values($args[0]);
  47. return $this;
  48. }
  49. ////////////////////////////////////////////////////////////////////////////////
  50. function select(){ foreach (func_get_args() as $arg)
  51. if ($arg instanceof SQL_Raw){
  52. $this->select[] = '?';
  53. $this->placeholders[] = $arg;
  54. } elseif ($arg instanceof SQL or $arg instanceof SQL_Query){
  55. $this->select[] = $this->build('subquery', 'select', $arg);
  56. } elseif ($arg == '*'){ $this->select = array_merge($this->select, array_keys(self::schema($this->alias)));
  57. } elseif (substr($arg, -2) == '.*'){ $arg = substr($arg, 0, -2);
  58. foreach (array_keys(self::schema($arg)) as $field)
  59. $this->select[] = $arg.'.'.$field.' as '.$arg.'.'.$field;
  60. } else {
  61. $this->select[] = $arg;
  62. }
  63. return $this;
  64. }
  65. ////////////////////////////////////////////////////////////////////////////////
  66. function from($table){ if (!$this instanceof self and $table)
  67. return self::table($table);
  68. foreach (func_get_args() as $arg) if ($pos = stripos($arg, ' as ')){
  69. $this->from[] = self::table(trim(substr($arg, 0, $pos)))->table.substr($arg, $pos);
  70. } else {
  71. $this->from[] = self::table($arg)->table; }
  72. return $this;
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////
  75. function join(){ foreach (func_get_args() as $arg){ if ($arg instanceof SQL){ $this->join[] = $this->build('subquery', 'join', $arg); continue;
  76. } elseif ($arg instanceof SQL_Query){ $this->join[] = $arg->build('subquery', 'join', $this);
  77. continue; } elseif (is_array($arg)){ list($arg, $vars) = array($this->alias, $arg); $this->relations = array_merge($this->relations, self::deep_throat(array($arg => $vars))); }
  78. if ($pos = stripos($arg, ' as ')){
  79. $alias = trim(substr($arg, ($pos + 4)));
  80. $arg = trim(substr($arg, 0, $pos));
  81. $relation = $this->relations[strtolower($arg)];
  82. if ($relation['foreign']['alias2'])
  83. $relation['foreign']['alias2'] = $alias;
  84. else
  85. $relation['foreign']['alias'] = $alias;
  86. } else { $relation = $this->relations[strtolower($arg)];
  87. }
  88. $this->join = array_merge($this->join, $this->build('join', $relation));
  89. if (substr($relation['type'], -4) == 'many')
  90. $this->multiple[] = ($relation['foreign']['alias2'] ? $relation['foreign']['alias2'] : $relation['foreign']['alias']);
  91. }
  92. return $this;
  93. }
  94. ////////////////////////////////////////////////////////////////////////////////
  95. function where(){
  96. $args = func_get_args();
  97. $this->where[] = join(' or ', (array)array_shift($args));
  98. foreach ($args as $arg){
  99. if ($arg instanceof SQL or $arg instanceof SQL_Query){
  100. $arg->alias = 'subquery_'.$arg->alias;
  101. $arg->from[0] = $arg->table.' as '.$arg->alias;
  102. $query = $arg->query($arg->build('select'))->build('select');
  103. $this->placeholders[] = self::raw($query);
  104. } else {
  105. $this->placeholders[] = $arg;
  106. }
  107. }
  108. return $this;
  109. }
  110. ////////////////////////////////////////////////////////////////////////////////
  111. function group_by(){
  112. $args = func_get_args();
  113. $this->group_by = array_merge($this->group_by, $args);
  114. return $this;
  115. }
  116. ////////////////////////////////////////////////////////////////////////////////
  117. function having(){
  118. foreach (func_get_args() as $arg)
  119. $this->having[] = (is_array($arg) ? join(' or ', $arg) : $arg);
  120. return $this;
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////
  123. function order_by(){
  124. foreach (func_get_args() as $arg)
  125. $this->order_by[] = ($arg[0] == '-' ? substr($arg, 1).' desc' : $arg);
  126. return $this;
  127. }
  128. ////////////////////////////////////////////////////////////////////////////////
  129. function limit($limit){ $this->limit = (is_numeric($limit) ? $limit : self::$connection->quote($limit));
  130. return $this;
  131. }
  132. ////////////////////////////////////////////////////////////////////////////////
  133. function offset($offset){ if ($offset > 0)
  134. $this->offset = (is_numeric($offset) ? $offset : self::$connection->quote($offset));
  135. return $this;
  136. }
  137. ////////////////////////////////////////////////////////////////////////////////
  138. function page($page){ if ($page > 0)
  139. $this->offset($page * $this->limit - $this->limit);
  140. return $this;
  141. }
  142. ////////////////////////////////////////////////////////////////////////////////
  143. static function union(){ $class = new self;
  144. $class->table = $class->alias = '';
  145. foreach (func_get_args() as $arg)
  146. if ($arg instanceof SQL or $arg instanceof SQL_Query){
  147. if (!$arg->select)
  148. $arg->select[] = '*';
  149. $class->union[] = $arg->build('select');
  150. $class->placeholders = array_merge($class->placeholders, $arg->placeholders);
  151. }
  152. return $class;
  153. }
  154. ////////////////////////////////////////////////////////////////////////////////
  155. function __call($method, $params){
  156. $method = strtolower($method);
  157. if (substr($method, 0, 3) == 'by_' and $params){
  158. $method = substr($method, 3);
  159. if (!strpos($method, '_')){
  160. $select[] = $method;
  161. } else { $pos = strrpos($method, '_');
  162. $select[] = str_replace('_', '.', substr($method, 0, $pos));
  163. $select[] = substr($method, ($pos + 1));
  164. }
  165. foreach ($params as $param)
  166. self::where(join('.', $select).' = ?'.(is_array($param) ? 'a' : ''), $param);
  167. return $this;
  168. }
  169. if (!$scope = $this->scope[$method])
  170. trigger_error(sprintf('Call to undefined method %s::%s()', get_class($this), $method), E_USER_ERROR);
  171. foreach ($scope as $k => $v)
  172. if (call_user_func_array(array($this, $k), array_merge((array)$v)))
  173. $this->placeholders = array_merge($this->placeholders, $params);
  174. return $this;
  175. }
  176. ////////////////////////////////////////////////////////////////////////////////
  177. }