Dotclear

Changeset 3741:0ab84fb44787 for inc/core


Ignore:
Timestamp:
03/29/18 11:17:14 (7 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Add some helpers, cope with mixed (single or array) arg for clause fragment methods, add an optional parameter to reset first the corresponding element

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.selectstatement.php

    r3740 r3741  
    1717class dcSelectStatement 
    1818{ 
     19    protected $core; 
     20    protected $con; 
     21 
    1922    protected $columns; 
    2023    protected $from; 
    2124    protected $join; 
    2225    protected $where; 
    23     protected $and; 
     26    protected $cond; 
    2427    protected $sql; 
    2528    protected $having; 
     
    3336     * Class constructor 
    3437     * 
    35      * @param mixed $from   optional from clause(s) 
    36      */ 
    37     public function __construct($from = null) 
    38     { 
     38     * @param dcCore    $core   dcCore instance 
     39     * @param mixed     $from   optional from clause(s) 
     40     */ 
     41    public function __construct(&$core, $from = null) 
     42    { 
     43        $this->core = &$core; 
     44        $this->con  = &$core->con; 
     45 
    3946        $this->columns = 
    4047        $this->from    = 
     
    6168 
    6269    /** 
    63      * Adds a new column 
    64      * 
    65      * @param $c the column 
    66      * @return dcSelectStatement self instance, enabling to chain calls 
    67      */ 
    68     public function column($c) 
    69     { 
    70         array_push($this->columns, $c); 
    71         return $this; 
    72     } 
    73     /** 
    74      * adds a list of columns 
    75      * 
    76      * @param array $c the list of columns 
    77      * @return dcSelectStatement self instance, enabling to chain calls 
    78      */ 
    79     public function columns($c) 
    80     { 
    81         $this->columns = array_merge($this->columns, $c); 
    82         return $this; 
    83     } 
    84  
    85     /** 
    86      * Adds a FROM clause 
    87      * 
    88      * @param string $c the from clause 
    89      * @return dcSelectStatement self instance, enabling to chain calls 
    90      */ 
    91     public function from($c) 
    92     { 
    93         $c = trim(ltrim($c, ',')); // Cope with legacy code 
    94         array_push($this->from, $c); 
    95         return $this; 
    96     } 
    97  
    98     /** 
    99      * Adds a list of FROM clauses 
    100      * 
    101      * @param array $c the list of clauses 
    102      * @return dcSelectStatement self instance, enabling to chain calls 
    103      */ 
    104     public function froms($c) 
    105     { 
    106         $c          = array_map(trim(ltrim($c, ',')), $c); // Cope with legacy code 
    107         $this->from = array_merge($this->from, $c); 
    108         return $this; 
    109     } 
    110  
    111     /** 
    112      * Adds a JOIN clause (applied on first from item only) 
    113      * 
    114      * @param string $c the clause 
    115      * @return dcSelectStatement self instance, enabling to chain calls 
    116      */ 
    117     public function join($c) 
    118     { 
    119         array_push($this->join, $c); 
    120         return $this; 
    121     } 
    122  
    123     /** 
    124      * Adds a list of JOIN clauses (applied on first from item only) 
    125      * 
    126      * @param array $c the list of clauses 
    127      * @return dcSelectStatement self instance, enabling to chain calls 
    128      */ 
    129     public function joins($c) 
    130     { 
    131         $this->join = array_merge($this->join, $c); 
    132         return $this; 
    133     } 
    134  
    135     /** 
    136      * Adds a WHERE clause condition (each will be AND combined in statement) 
    137      * 
    138      * @param string $c the clause 
    139      * @return dcSelectStatement self instance, enabling to chain calls 
    140      */ 
    141     public function where($c) 
    142     { 
    143         array_push($this->where, $c); 
    144         return $this; 
    145     } 
    146  
    147     /** 
    148      * Adds a list of WHERE clauses (each will be AND combined in statement) 
    149      * 
    150      * @param array $c the list of clauses 
    151      * @return dcSelectStatement self instance, enabling to chain calls 
    152      */ 
    153     public function wheres($c) 
    154     { 
    155         $this->where = array_merge($this->where, $c); 
    156         return $this; 
    157     } 
    158  
    159     /** 
    160      * Adds a WHERE clause additional condition (including an operator at beginning) 
    161      * 
    162      * @param string $c the clause 
    163      * @return dcSelectStatement self instance, enabling to chain calls 
    164      */ 
    165     public function cond($c) { 
    166         array_push($this->cond, $c); 
    167         return $this; 
    168     } 
    169  
    170     /** 
    171      * Adds a list of WHERE clause additional conditions (each including an operator at beginning) 
    172      * 
    173      * @param array $c the list of clauses 
    174      * @return dcSelectStatement self instance, enabling to chain calls 
    175      */ 
    176     public function conds($c) 
    177     { 
    178         $this->cond = array_merge($this->cond, $c); 
    179         return $this; 
    180     } 
    181  
    182     /** 
    183      * Adds a generic clause 
    184      * 
    185      * @param string $c the clause 
    186      * @return dcSelectStatement self instance, enabling to chain calls 
    187      */ 
    188     public function sql($c) 
    189     { 
    190         array_push($this->sql, $c); 
    191         return $this; 
    192     } 
    193  
    194     /** 
    195      * Adds a list of generic clauses 
    196      * 
    197      * @param array $c the list of clauses 
    198      * @return dcSelectStatement self instance, enabling to chain calls 
    199      */ 
    200     public function sqls($c) 
    201     { 
    202         $this->sql = array_merge($this->sql, $c); 
    203         return $this; 
    204     } 
    205  
    206     /** 
    207      * Adds a HAVING clause 
    208      * 
    209      * @param string $c the clause 
    210      * @return dcSelectStatement self instance, enabling to chain calls 
    211      */ 
    212     public function having($c) 
    213     { 
    214         array_push($this->having, $c); 
    215         return $this; 
    216     } 
    217  
    218     /** 
    219      * Adds a list of HAVING clauses (will be AND combined in statement) 
    220      * 
    221      * @param array $c the list of clauses 
    222      * @return dcSelectStatement self instance, enabling to chain calls 
    223      */ 
    224     public function havings($c) 
    225     { 
    226         $this->having = array_merge($this->having, $c); 
    227         return $this; 
    228     } 
    229  
    230     /** 
    231      * Adds an ORDER BY clause 
    232      * 
    233      * @param string $c the clause 
    234      * @return dcSelectStatement self instance, enabling to chain calls 
    235      */ 
    236     public function order($c) 
    237     { 
    238         array_push($this->order, $c); 
    239         return $this; 
    240     } 
    241  
    242     /** 
    243      * Adds a list of ORDER BY clauses 
    244      * 
    245      * @param array $c the list of clauses 
    246      * @return dcSelectStatement self instance, enabling to chain calls 
    247      */ 
    248     public function orders($c) 
    249     { 
    250         $this->order = array_merge($this->order, $c); 
    251         return $this; 
    252     } 
    253  
    254     /** 
    255      * Adds an GROUP BY clause 
    256      * 
    257      * @param string $c the clause 
    258      * @return dcSelectStatement self instance, enabling to chain calls 
    259      */ 
    260     public function group($c) 
    261     { 
    262         array_push($this->group, $c); 
    263         return $this; 
    264     } 
    265  
    266     /** 
    267      * Adds a list of GROUP BY clauses 
    268      * 
    269      * @param array $c the list of clauses 
    270      * @return dcSelectStatement self instance, enabling to chain calls 
    271      */ 
    272     public function groups($c) 
    273     { 
    274         $this->order = array_merge($this->order, $c); 
     70     * Magic getter method 
     71     * 
     72     * @param      string  $property  The property 
     73     * 
     74     * @return     mixed   property value if property exists 
     75     */ 
     76    public function __get($property) 
     77    { 
     78        if (property_exists($this, $property)) { 
     79            return $this->$property; 
     80        } 
     81        trigger_error('Unknown property ' . $property, E_USER_ERROR); 
     82        return; 
     83    } 
     84 
     85    /** 
     86     * Magic setter method 
     87     * 
     88     * @param      string  $property  The property 
     89     * @param      mixed   $value     The value 
     90     * 
     91     * @return     self 
     92     */ 
     93    public function __set($property, $value) 
     94    { 
     95        if (property_exists($this, $property)) { 
     96            $this->$property = $value; 
     97        } else { 
     98            trigger_error('Unknown property ' . $property, E_USER_ERROR); 
     99        } 
     100        return $this; 
     101    } 
     102 
     103    /** 
     104     * Adds column(s) 
     105     * 
     106     * @param mixed     $c      the column(s) 
     107     * @param boolean   $reset  reset previous column(s) first 
     108     * 
     109     * @return dcSelectStatement self instance, enabling to chain calls 
     110     */ 
     111    public function columns($c, $reset = false) 
     112    { 
     113        if ($reset) { 
     114            $this->columns = array(); 
     115        } 
     116        if (is_array($c)) { 
     117            $this->columns = array_merge($this->columns, $c); 
     118        } else { 
     119            array_push($this->columns, $c); 
     120        } 
     121        return $this; 
     122    } 
     123 
     124    /** 
     125     * columns() alias 
     126     * 
     127     * @param      mixed    $c      the column(s) 
     128     * @param      boolean  $reset  reset previous column(s) first 
     129     * 
     130     * @return dcSelectStatement self instance, enabling to chain calls 
     131     */ 
     132    public function column($c, $reset = false) 
     133    { 
     134        return $this->columns($c, $reset); 
     135    } 
     136 
     137    /** 
     138     * Adds FROM clause(s) 
     139     * 
     140     * @param mixed     $c      the from clause(s) 
     141     * @param boolean   $reset  reset previous from(s) first 
     142     * 
     143     * @return dcSelectStatement self instance, enabling to chain calls 
     144     */ 
     145    public function from($c, $reset = false) 
     146    { 
     147        if ($reset) { 
     148            $this->from = array(); 
     149        } 
     150        if (is_array($c)) { 
     151            $c          = array_map(trim(ltrim($c, ',')), $c); // Cope with legacy code 
     152            $this->from = array_merge($this->from, $c); 
     153        } else { 
     154            $c = trim(ltrim($c, ',')); // Cope with legacy code 
     155            array_push($this->from, $c); 
     156        } 
     157        return $this; 
     158    } 
     159 
     160    /** 
     161     * Adds JOIN clause(s) (applied on first from item only) 
     162     * 
     163     * @param mixed     $c      the join clause(s) 
     164     * @param boolean   $reset  reset previous join(s) first 
     165     * 
     166     * @return dcSelectStatement self instance, enabling to chain calls 
     167     */ 
     168    public function join($c, $reset = false) 
     169    { 
     170        if ($reset) { 
     171            $this->join = array(); 
     172        } 
     173        if (is_array($c)) { 
     174            $this->join = array_merge($this->join, $c); 
     175        } else { 
     176            array_push($this->join, $c); 
     177        } 
     178        return $this; 
     179    } 
     180 
     181    /** 
     182     * Adds WHERE clause(s) condition (each will be AND combined in statement) 
     183     * 
     184     * @param mixed     $c      the clause(s) 
     185     * @param boolean   $reset  reset previous where(s) first 
     186     * 
     187     * @return dcSelectStatement self instance, enabling to chain calls 
     188     */ 
     189    public function where($c, $reset = false) 
     190    { 
     191        if ($reset) { 
     192            $this->where = array(); 
     193        } 
     194        if (is_array($c)) { 
     195            $this->where = array_merge($this->where, $c); 
     196        } else { 
     197            array_push($this->where, $c); 
     198        } 
     199        return $this; 
     200    } 
     201 
     202    /** 
     203     * Adds additional WHERE clause condition(s) (including an operator at beginning) 
     204     * 
     205     * @param mixed     $c      the clause(s) 
     206     * @param boolean   $reset  reset previous condition(s) first 
     207     * 
     208     * @return dcSelectStatement self instance, enabling to chain calls 
     209     */ 
     210    public function cond($c, $reset = false) 
     211    { 
     212        if ($reset) { 
     213            $this->cond = array(); 
     214        } 
     215        if (is_array($c)) { 
     216            $this->cond = array_merge($this->cond, $c); 
     217        } else { 
     218            array_push($this->cond, $c); 
     219        } 
     220        return $this; 
     221    } 
     222 
     223    /** 
     224     * Adds generic clause(s) 
     225     * 
     226     * @param mixed     $c      the clause(s) 
     227     * @param boolean   $reset  reset previous generic clause(s) first 
     228     * 
     229     * @return dcSelectStatement self instance, enabling to chain calls 
     230     */ 
     231    public function sql($c, $reset = false) 
     232    { 
     233        if ($reset) { 
     234            $this->sql = array(); 
     235        } 
     236        if (is_array($c)) { 
     237            $this->sql = array_merge($this->sql, $c); 
     238        } else { 
     239            array_push($this->sql, $c); 
     240        } 
     241        return $this; 
     242    } 
     243 
     244    /** 
     245     * Adds HAVING clause(s) 
     246     * 
     247     * @param mixed     $c      the clause(s) 
     248     * @param boolean   $reset  reset previous having(s) first 
     249     * 
     250     * @return dcSelectStatement self instance, enabling to chain calls 
     251     */ 
     252    public function having($c, $reset = false) 
     253    { 
     254        if ($reset) { 
     255            $this->having = array(); 
     256        } 
     257        if (is_array($c)) { 
     258            $this->having = array_merge($this->having, $c); 
     259        } else { 
     260            array_push($this->having, $c); 
     261        } 
     262        return $this; 
     263    } 
     264 
     265    /** 
     266     * Adds ORDER BY clause(s) 
     267     * 
     268     * @param mixed     $c      the clause(s) 
     269     * @param boolean   $reset  reset previous order(s) first 
     270     * 
     271     * @return dcSelectStatement self instance, enabling to chain calls 
     272     */ 
     273    public function order($c, $reset = false) 
     274    { 
     275        if ($reset) { 
     276            $this->order = array(); 
     277        } 
     278        if (is_array($c)) { 
     279            $this->order = array_merge($this->order, $c); 
     280        } else { 
     281            array_push($this->order, $c); 
     282        } 
     283        return $this; 
     284    } 
     285 
     286    /** 
     287     * Adds GROUP BY clause(s) 
     288     * 
     289     * @param mixed     $c      the clause(s) 
     290     * @param boolean   $reset  reset previous group(s) first 
     291     * 
     292     * @return dcSelectStatement self instance, enabling to chain calls 
     293     */ 
     294    public function group($c, $reset = false) 
     295    { 
     296        if ($reset) { 
     297            $this->group = array(); 
     298        } 
     299        if (is_array($c)) { 
     300            $this->group = array_merge($this->group, $c); 
     301        } else { 
     302            array_push($this->group, $c); 
     303        } 
    275304        return $this; 
    276305    } 
     
    328357    } 
    329358 
     359    // Helpers 
     360 
     361    /** 
     362     * Escape an identifier 
     363     * 
     364     * @param      string  $identifier  The identifier 
     365     * 
     366     * @return     string 
     367     */ 
     368    public function escape($identifier) 
     369    { 
     370        return $this->con->escape($identifier); 
     371    } 
     372 
     373    /** 
     374     * Return an SQL IN (…) fragment 
     375     * 
     376     * @param      mixed  $list   The list 
     377     * 
     378     * @return     string 
     379     */ 
     380    public function in($list) 
     381    { 
     382        return $this->con->in($list); 
     383    } 
     384 
     385    /** 
     386     * Return an SQL formatted date 
     387     * 
     388     * @param   string    $field     Field name 
     389     * @param   string    $pattern   Date format 
     390     * 
     391     * @return     string 
     392     */ 
     393    public function dateFormat($field, $pattern) 
     394    { 
     395        return $this->con->dateFormat($field, $pattern); 
     396    } 
     397 
    330398    /** 
    331399     * Returns the select statement 
Note: See TracChangeset for help on using the changeset viewer.

Sites map