| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Maphper\DataSource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | class GeneralEditDatabase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |     private $pdo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     private $dataTypes = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |         'datetime' => 'DATETIME', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |         'int' => 'INT(11)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |         'decimal' => 'DECIMAL', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |         'short_string' => 'VARCHAR', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |         'long_string' => 'LONGBLOG', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |         'short_string_max_len' => 255, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         'other' => 'VARCHAR(255)', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         'pk_default' => 'INT(11) NOT NULL AUTO_INCREMENT' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public function __construct(\PDO $pdo, array $dataTypes) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->pdo = $pdo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->dataTypes = array_merge($this->dataTypes, $dataTypes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function quote($str) { | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | 		return '`' . str_replace('.', '`.`', trim($str, '`')) . '`'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function getType($val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		if ($val instanceof \DateTime) return $this->dataTypes['datetime']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		else if ($result = $this->doNumberTypes($val)) return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		else if ($result = $this->doStringTypes($val)) return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		else return $this->dataTypes['other']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private function doNumberTypes($val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if (is_int($val)) return $this->dataTypes['int']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		else if (is_double($val)) return $this->dataTypes['decimal'] . '(9,' . strlen($val) - strrpos($val, '.') - 1 . ')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         else return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     private function doStringTypes($val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if (!is_string($val)) return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if (strlen($val) <= $this->dataTypes['short_string_max_len']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             return $this->dataTypes['short_string'] . '(' . $this->dataTypes['short_string_max_len'] . ')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		else return $this->dataTypes['long_string']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function isNotSavableType($value, $key, $primaryKey) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         return is_array($value) || (is_object($value) && !($value instanceof \DateTime)) || | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 in_array($key, $primaryKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     //Alter the database so that it can store $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function createTable($table, array $primaryKey, $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$parts = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		foreach ($primaryKey as $key) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			$pk = $data->$key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			if ($pk == null) $parts[] = $key . ' ' . $this->dataTypes['pk_default']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			else $parts[] = $key . ' ' . $this->getType($pk) . ' NOT NULL'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$pkField = implode(', ', $parts) . ', PRIMARY KEY(' . implode(', ', $primaryKey) . ')'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$this->pdo->query('CREATE TABLE IF NOT EXISTS ' . $table . ' (' . $pkField . ')'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 64 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |  |