Exemple #1
0
 public function __construct(Eden_Sql_Select $parentQuery, $select = '*')
 {
     //Argument 2 must be a string
     Eden_Mysql_Error::i()->argument(2, 'string');
     $this->setParentQuery($parentQuery);
     $this->_select = is_array($select) ? implode(', ', $select) : $select;
 }
Exemple #2
0
 /**
  * Sets the name of the table you wish to create
  *
  * @param string name
  * @return this
  */
 public function setName($name)
 {
     //Argument 1 must be a string
     Eden_Mysql_Error::i()->argument(1, 'string');
     $this->_name = $name;
     return $this;
 }
Exemple #3
0
 /**
  * Returns the whole enitre schema and rows
  * of the current table
  *
  * @return string
  */
 public function getTableSchema($table)
 {
     //Argument 1 must be a string
     Eden_Mysql_Error::i()->argument(1, 'string');
     $backup = array();
     //get the schema
     $schema = $this->getColumns($table);
     if (count($schema)) {
         //lets rebuild this schema
         $query = $this->create()->setName($table);
         foreach ($schema as $field) {
             //first try to parse what we can from each field
             $fieldTypeArray = explode(' ', $field['Type']);
             $typeArray = explode('(', $fieldTypeArray[0]);
             $type = $typeArray[0];
             $length = str_replace(')', '', $typeArray[1]);
             $attribute = isset($fieldTypeArray[1]) ? $fieldTypeArray[1] : NULL;
             $null = strtolower($field['Null']) == 'no' ? false : true;
             $increment = strtolower($field['Extra']) == 'auto_increment' ? true : false;
             //lets now add a field to our schema class
             $q->addField($field['Field'], array('type' => $type, 'length' => $length, 'attribute' => $attribute, 'null' => $null, 'default' => $field['Default'], 'auto_increment' => $increment));
             //set keys where found
             switch ($field['Key']) {
                 case 'PRI':
                     $query->addPrimaryKey($field['Field']);
                     break;
                 case 'UNI':
                     $query->addUniqueKey($field['Field'], array($field['Field']));
                     break;
                 case 'MUL':
                     $query->addKey($field['Field'], array($field['Field']));
                     break;
             }
         }
         //store the query but dont run it
         $backup[] = $query;
     }
     //get the rows
     $rows = $this->query($this->select->from($table)->getQuery());
     if (count($rows)) {
         //lets build an insert query
         $query = $this->insert($table);
         foreach ($rows as $index => $row) {
             foreach ($row as $key => $value) {
                 $query->set($key, $this->getBinds($value), $index);
             }
         }
         //store the query but dont run it
         $backup[] = $query->getQuery(true);
     }
     return implode("\n\n", $backup);
 }
Exemple #4
0
 /**
  * Query for truncating a table
  *
  * @param string the name of the table
  * @return this
  */
 public function truncate($table)
 {
     //Argument 1 must be a string
     Eden_Mysql_Error::i()->argument(1, 'string');
     $this->_query = 'TRUNCATE `' . $table . '`';
     return $this;
 }
Exemple #5
0
if(!class_exists('Eden_Mysql')){class Eden_Mysql extends Eden_Sql_Database{protected $_host='localhost';protected $_name=NULL;protected $_user=NULL;protected $_pass=NULL;protected $_model=self::MODEL;protected $_collection=self::COLLECTION;public static function i(){return self::_getMultiple(__CLASS__);}public function __construct($host,$name,$user,$pass=NULL,$port=NULL){Eden_Mysql_Error::i()->argument(1,'string','null')->argument(2,'string')->argument(3,'string')->argument(4,'string','null')->argument(5,'numeric','null');$this->_host=$host;$this->_name=$name;$this->_user=$user;$this->_pass=$pass;$this->_port=$port;}public function alter($name=NULL){Eden_Mysql_Error::i()->argument(1,'string','null');return Eden_Mysql_Alter::i($name);}public function create($name=NULL){Eden_Mysql_Error::i()->argument(1,'string','null');return Eden_Mysql_Create::i($name);}public function connect(array $options=array()){$host=$port=NULL;if(!is_null($this->_host)){$host='host='.$this->_host.';';if(!is_null($this->_port)){$port='port='.$this->_port.';';}}$connection='mysql:'.$host.$port.'dbname='.$this->_name;$this->_connection=new PDO($connection,$this->_user,$this->_pass,$options);$this->trigger();return $this;}public function subselect($parentQuery,$select='*'){Eden_Mysql_Error::i()->argument(2,'string');return Eden_Mysql_Subselect::i($parentQuery,$select);}public function utility(){return Eden_Mysql_Utility::i();}public function getColumns($table,$filters=NULL){Eden_Mysql_Error::i()->argument(1,'string');$query=$this->utility();if(is_array($filters)){foreach($filters as $i=>$filter){$format=array_shift($filter);$filter=$this->bind($filter);$filters[$i]=vsprintf($format,$filter);}}$query->showColumns($table,$filters);return $this->query($query,$this->getBinds());}public function getPrimaryKey($table){Eden_Mysql_Error::i()->argument(1,'string');$query=$this->utility();$results=$this->getColumns($table,"`Key`='PRI'");return isset($results[0]['Field']) ? $results[0]['Field'] : NULL;}public function getSchema(){$backup=array();$tables=$this->getTables();foreach($tables as $table){$backup[]=$this->getBackup();}return implode("\n\n",$backup);}public function getTables($like=NULL){Eden_Mysql_Error::i()->argument(1,'string','null');$query=$this->utility();$like=$like ? $this->bind($like) : NULL;$results=$this->query($query->showTables($like),$q->getBinds());$newResults=array();foreach($results as $result){foreach($result as $key=>$value){$newResults[]=$value;break;}}return $newResults;}public function getTableSchema($table){Eden_Mysql_Error::i()->argument(1,'string');$backup=array();$schema=$this->getColumns($table);if(count($schema)){$query=$this->create()->setName($table);foreach($schema as $field){$fieldTypeArray=explode(' ',$field['Type']);$typeArray=explode('(',$fieldTypeArray[0]);$type=$typeArray[0];$length=str_replace(')','',$typeArray[1]);$attribute=isset($fieldTypeArray[1]) ? $fieldTypeArray[1] : NULL;$null=strtolower($field['Null'])=='no' ? false : true;$increment=strtolower($field['Extra'])=='auto_increment' ? true : false;$q->addField($field['Field'],array( 'type'=>$type,'length'=>$length,'attribute'=>$attribute,'null'=>$null,'default'=>$field['Default'],'auto_increment'=>$increment));switch($field['Key']){case 'PRI': $query->addPrimaryKey($field['Field']);break;case 'UNI': $query->addUniqueKey($field['Field'],array($field['Field']));break;case 'MUL': $query->addKey($field['Field'],array($field['Field']));break;}}$backup[]=$query;}$rows=$this->query($this->select->from($table)->getQuery());if(count($rows)){$query=$this->insert($table);foreach($rows as $index=>$row){foreach($row as $key=>$value){$query->set($key,$this->getBinds($value),$index);}}$backup[]=$query->getQuery(true);}return implode("\n\n",$backup);}}}
Exemple #6
0
 public function withOids($oids)
 {
     //Argument 1 must be a boolean
     Eden_Mysql_Error::i()->argument(1, 'bool');
     $this->_oids = $oids;
     return $this;
 }