1: <?php
2:
3: namespace PHPixie\Database\Type\SQL\Parser;
4:
5: abstract class Fragment
6: {
7: protected $quote;
8:
9: public function quote($str)
10: {
11: return $this->quote.$str.$this->quote;
12: }
13:
14: public function appendColumn($column, $expr)
15: {
16: if (is_object($column) && $column instanceof \PHPixie\Database\Type\SQL\Expression)
17: return $expr->append($column);
18:
19: if (strpos($column, '.')) {
20: $column = explode('.', $column);
21: $expr->sql.= $this->quote($column[0]).'.';
22: $column = $column[1];
23: }
24:
25: if ($column !== '*')
26: $column = $this->quote($column);
27:
28: $expr->sql.= $column;
29:
30: return $expr;
31: }
32:
33: public function appendTable($table, $expr, $alias = null)
34: {
35: if (is_string($table)) {
36: $table = explode('.', $table);
37: foreach($table as $key => $part) {
38: if($key !== 0) {
39: $expr->sql.= '.';
40: }
41: $expr->sql.= $this->quote($part);
42: }
43:
44: } elseif (
45: ($isQuery = $table instanceof \PHPixie\Database\Type\SQL\Query) ||
46: $table instanceof \PHPixie\Database\Type\SQL\Expression
47: ) {
48:
49: if ($isQuery)
50: $table = $table->parse();
51:
52: $expr->sql.= "( ";
53: $expr->append($table);
54: $expr->sql.= " )";
55: } else {
56: $class = get_class($table);
57: throw new \PHPixie\Database\Exception\Parser("Parameter type '$class' cannot be used as a table");
58: }
59:
60: if ($alias !== null)
61: $expr->sql.= " AS ".$this->quote($alias);
62:
63: return $expr;
64: }
65:
66: public function appendValue($value, $expr)
67: {
68: if ($value instanceof \PHPixie\Database\Type\SQL\Expression) {
69: $expr->append($value);
70: } elseif ($value instanceof \PHPixie\Database\Type\SQL\Query) {
71: $expr->sql.= "( ";
72: $expr->append($value-> parse());
73: $expr->sql.= " )";
74: } else {
75: $expr->sql.= '?';
76: $expr->params[]= $value;
77: }
78:
79: return $expr;
80: }
81: }
82: