bulton-fr /
bfw-sql
| 1 | <?php |
||
| 2 | |||
| 3 | namespace BfwSql\Queries\Parts; |
||
| 4 | |||
| 5 | class Column |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var \BfwSql\Queries\Parts\Table $table The table where is the column |
||
| 9 | */ |
||
| 10 | protected $table; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var string $name The column name |
||
| 14 | */ |
||
| 15 | protected $name; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string|null $shortcut The column shortcut |
||
| 19 | */ |
||
| 20 | protected $shortcut; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var mixed $value The column shortcut |
||
| 24 | */ |
||
| 25 | protected $value; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Construct |
||
| 29 | * |
||
| 30 | * @param \BfwSql\Queries\Parts\Table $table The table where is the column |
||
| 31 | * @param string $name The column name |
||
| 32 | * @param string $shortcut The column shortcut |
||
| 33 | * @param mixed $value The column value |
||
| 34 | */ |
||
| 35 | public function __construct( |
||
| 36 | Table $table, |
||
| 37 | string $name, |
||
| 38 | string $shortcut = null, |
||
| 39 | $value = null |
||
| 40 | ) { |
||
| 41 | $this->table = $table; |
||
| 42 | $this->name = $name; |
||
| 43 | $this->shortcut = $shortcut; |
||
| 44 | $this->value = $value; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Getter accessor to property table |
||
| 49 | * |
||
| 50 | * @return \BfwSql\Queries\Parts\Table |
||
| 51 | */ |
||
| 52 | public function getTable(): Table |
||
| 53 | { |
||
| 54 | return $this->table; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Getter accessor to property name |
||
| 59 | * |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | public function getName(): string |
||
| 63 | { |
||
| 64 | return $this->name; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Getter accessor to property shortcut |
||
| 69 | * |
||
| 70 | * @return string|null |
||
| 71 | */ |
||
| 72 | public function getShortcut() |
||
| 73 | { |
||
| 74 | return $this->shortcut; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Getter accessor to property value |
||
| 79 | * |
||
| 80 | * @return mixed |
||
| 81 | */ |
||
| 82 | public function getValue() |
||
| 83 | { |
||
| 84 | return $this->value; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Generate the sql query for to this column |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function generate(): string |
||
| 93 | { |
||
| 94 | $partQuery = $this->obtainName(); |
||
| 95 | if ($this->shortcut !== null) { |
||
| 96 | $partQuery .= ' AS `'.$this->shortcut.'`'; |
||
| 97 | } |
||
| 98 | |||
| 99 | return $partQuery; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Obtain the column name to use into the sql query |
||
| 104 | * Use shortcut if defined |
||
| 105 | * Use backtick if needed (not a function or a keyword) |
||
| 106 | * Add the table name before the column name |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | */ |
||
| 110 | public function obtainName(): string |
||
| 111 | { |
||
| 112 | $columnName = $this->name; |
||
| 113 | $tableName = $this->table->getName(); |
||
| 114 | |||
| 115 | if ($this->table->getShortcut() !== null) { |
||
| 116 | $tableName = $this->table->getShortcut(); |
||
| 117 | } |
||
| 118 | |||
| 119 | $isFunction = false; |
||
| 120 | if ( |
||
| 121 | strpos($columnName, ' ') !== false || |
||
| 122 | strpos($columnName, '(') !== false |
||
| 123 | ) { |
||
| 124 | $isFunction = true; |
||
| 125 | } |
||
| 126 | |||
| 127 | $isJoker = ($columnName === '*') ? true : false; |
||
| 128 | |||
| 129 | return $this->table |
||
| 130 | ->getQuerySystem() |
||
| 131 | ->getQuerySgbd() |
||
| 132 | ->columnName($columnName, $tableName, $isFunction, $isJoker) |
||
| 133 | ; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Obtain the value to use into sql query |
||
| 138 | * Return the string null if the column have the value null in php |
||
| 139 | * Use quoting system declared into the used query system |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function obtainValue() |
||
| 144 | { |
||
| 145 | if ($this->value === null) { |
||
| 146 | return 'null'; |
||
| 147 | } |
||
| 148 | |||
| 149 | return $this->table |
||
| 150 | ->getQuerySystem() |
||
| 151 | ->getQuoting() |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 152 | ->quoteValue($this->name, $this->value) |
||
| 153 | ; |
||
| 154 | } |
||
| 155 | } |
||
| 156 |