| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace suda\database\statement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Countable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use IteratorAggregate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use suda\database\Binder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use suda\database\exception\SQLException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Trait PrepareTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package suda\database\statement | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | trait PrepareTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     use WherePrepareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * 准备选择列 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param string|array $reads | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param string $table | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected function prepareReadFields($reads, string $table = ''):string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if (is_string($reads)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             $fields = $reads; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $field = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $prefix = strlen($table) ?"`{$table}`." :''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             foreach ($reads as $want) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 $field[] = $prefix."`$want`"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $fields = implode(',', $field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * 准备更新 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     protected function prepareUpdateSet(array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $binders = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $sets = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         foreach ($data as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $_name = Binder::index($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $binders[] = new Binder($_name, $value, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $sets[] = "`{$name}`=:{$_name}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return [implode(',', $sets), $binders]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * 合并绑定工具 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param Binder[] $binderArray | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param array $parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @return Binder[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function mergeBinder(array $binderArray, array $parameter) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         foreach ($parameter as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             if (! ($value instanceof Binder)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $value = new Binder($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             if (!in_array($value, $binderArray)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $binderArray[] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return $binderArray; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  |