class-clauses.php
2 years ago
class-database.php
2 years ago
class-escape.php
2 years ago
class-groupby.php
2 years ago
class-joins.php
2 years ago
class-orderby.php
2 years ago
class-query-builder.php
2 years ago
class-select.php
2 years ago
class-translate.php
2 years ago
class-where.php
2 years ago
index.php
2 years ago
class-select.php
132 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The select functions. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * @package MyThemeShop |
| 7 | * @subpackage MyThemeShop\Database |
| 8 | * @author MyThemeShop <admin@mythemeshop.com> |
| 9 | */ |
| 10 | |
| 11 | namespace MyThemeShop\Database; |
| 12 | |
| 13 | /** |
| 14 | * Select class. |
| 15 | */ |
| 16 | trait Select { |
| 17 | |
| 18 | /** |
| 19 | * Set the selected fields |
| 20 | * |
| 21 | * @param array $fields Fields to select. |
| 22 | * |
| 23 | * @return self The current query builder. |
| 24 | */ |
| 25 | public function select( $fields = '' ) { |
| 26 | if ( empty( $fields ) ) { |
| 27 | return $this; |
| 28 | } |
| 29 | |
| 30 | if ( is_string( $fields ) ) { |
| 31 | $this->add_sql_clause( 'select', $fields ); |
| 32 | return $this; |
| 33 | } |
| 34 | |
| 35 | foreach ( $fields as $key => $field ) { |
| 36 | $this->add_sql_clause( 'select', is_string( $key ) ? "$key AS $field" : $field ); |
| 37 | } |
| 38 | |
| 39 | return $this; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Shortcut to add a count function |
| 44 | * |
| 45 | * ->selectCount('id') |
| 46 | * ->selectCount('id', 'count') |
| 47 | * |
| 48 | * @param string $field Column name. |
| 49 | * @param string $alias (Optional) Column alias. |
| 50 | * |
| 51 | * @return self The current query builder. |
| 52 | */ |
| 53 | public function selectCount( $field = '*', $alias = null ) { // @codingStandardsIgnoreLine |
| 54 | return $this->selectFunc( 'count', $field, $alias ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Shortcut to add a sum function |
| 59 | * |
| 60 | * ->selectSum('id') |
| 61 | * ->selectSum('id', 'total') |
| 62 | * |
| 63 | * @param string $field Column name. |
| 64 | * @param string $alias (Optional) Column alias. |
| 65 | * |
| 66 | * @return self The current query builder. |
| 67 | */ |
| 68 | public function selectSum( $field, $alias = null ) { // @codingStandardsIgnoreLine |
| 69 | return $this->selectFunc( 'sum', $field, $alias ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Shortcut to add a avg function |
| 74 | * |
| 75 | * ->selectAvg('id') |
| 76 | * ->selectAvg('id', 'average') |
| 77 | * |
| 78 | * @param string $field Column name. |
| 79 | * @param string $alias (Optional) Column alias. |
| 80 | * |
| 81 | * @return self The current query builder. |
| 82 | */ |
| 83 | public function selectAvg( $field, $alias = null ) { // @codingStandardsIgnoreLine |
| 84 | return $this->selectFunc( 'avg', $field, $alias ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Shortcut to add a function |
| 89 | * |
| 90 | * @param string $func Function name. |
| 91 | * @param string $field Column name. |
| 92 | * @param string $alias (Optional) Column alias. |
| 93 | * |
| 94 | * @return self The current query builder. |
| 95 | */ |
| 96 | public function selectFunc( $func, $field, $alias = null ) { // @codingStandardsIgnoreLine |
| 97 | $func = \strtoupper( $func ); |
| 98 | $field = "$func({$field})"; |
| 99 | if ( ! is_null( $alias ) ) { |
| 100 | $field .= " AS {$alias}"; |
| 101 | } |
| 102 | |
| 103 | $this->add_sql_clause( 'select', $field ); |
| 104 | |
| 105 | return $this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Distinct select setter |
| 110 | * |
| 111 | * @param bool $distinct Is disticnt. |
| 112 | * |
| 113 | * @return self The current query builder. |
| 114 | */ |
| 115 | public function distinct( $distinct = true ) { |
| 116 | $this->distinct = $distinct; |
| 117 | return $this; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * SQL_CALC_FOUND_ROWS select setter |
| 122 | * |
| 123 | * @param bool $found_rows Should get found rows. |
| 124 | * |
| 125 | * @return self The current query builder. |
| 126 | */ |
| 127 | public function found_rows( $found_rows = true ) { |
| 128 | $this->found_rows = $found_rows; |
| 129 | return $this; |
| 130 | } |
| 131 | } |
| 132 |