Beispiel #1
0
 /**
  * Override {@link epDbPort::checkIndex()}
  * @param epClassMap $cm
  * @param epDb $db
  * @return false|array
  */
 public function checkIndex($cm, $db)
 {
     // reset counter and return value
     $ret = array(array(), array());
     // get all columns in the pg_attribute and pg_class table
     $sql = 'SELECT a.attname, a.attnum ' . ' FROM pg_attribute a, pg_class c ' . ' WHERE c.relname = ' . $db->quote($cm->getTable()) . ' AND a.attrelid = c.oid AND a.attnum > 0' . ' ORDER BY a.attnum';
     // execute the query
     if (!$db->execute($sql)) {
         return false;
     }
     // array to collect all columns
     $columns = array();
     // go through reach record
     $okay = $this->db->rsRestart();
     while ($okay) {
         $num = $db->rsGetCol('attnum');
         $name = $db->rsGetCol('attname');
         $columns[$num] = $name;
         $okay = $this->db->rsNext();
     }
     // get all the indexes in the table (indkey has a list, space separated)
     $sql = 'SELECT c2.relname AS indexname, i.indisprimary, i.indisunique, i.indkey AS indkey' . ' FROM pg_class c, pg_class c2, pg_index i' . ' WHERE c.relname = ' . $db->quote($cm->getTable()) . ' AND c.oid = i.indrelid AND i.indexrelid = c2.oid';
     // execute above query
     if (!$db->execute($sql)) {
         return false;
     }
     // go through reach record
     $okay = $db->rsRestart();
     while ($okay) {
         // skip the primary index
         if ($this->db->rsGetCol('indisprimary') == 't') {
             // next row
             $okay = $this->db->rsNext();
             continue;
         }
         // get index name
         $name = $this->db->rsGetCol('indexname');
         $unique = $this->db->rsGetCol('indisunique');
         // $unique is t if unique
         // $unique is f if index
         $unique = $unique == 't' ? 0 : 1;
         $indexes = explode(' ', $this->db->rsGetCol('indkey'));
         foreach ($indexes as $index) {
             $ret[$unique][$name][] = $columns[$index];
         }
         // next row
         $okay = $db->rsNext();
     }
     return $ret;
 }
Beispiel #2
0
 /**
  * Override {@link epDbPort::checkIndex()}
  * @param epClassMap $cm
  * @param epDb $db
  * @return false|array
  */
 public function checkIndex($cm, $db)
 {
     // get index list
     $sql = 'PRAGMA index_list(' . $db->quoteId($cm->getTable()) . ')';
     if (!$db->execute($sql)) {
         return false;
     }
     // reset counter and return value
     $ret = array(array(), array());
     $indexes = array();
     $uniques = array();
     // go through reach record
     $okay = $db->rsRestart();
     while ($okay) {
         // get index name
         $name = $db->rsGetCol('name');
         $unique = $db->rsGetCol('unique');
         // $unique is 1 if unique
         // $unique is 0 if index
         $unique = !$unique;
         // ???
         // store the index name for further information
         $indexes[] = $name;
         $uniques[$name] = $unique;
         $ret[$unique][$name] = array();
         // next row
         $okay = $db->rsNext();
     }
     // go through each index
     foreach ($indexes as $index) {
         $sql = 'PRAGMA index_info(' . $db->quoteId($index) . ')';
         if (!$db->execute($sql)) {
             return false;
         }
         // go through reach record
         $okay = $db->rsRestart();
         while ($okay) {
             // get index name
             $column = $db->rsGetCol('name');
             $ret[$uniques[$index]][$index][] = $column;
             // next row
             $okay = $db->rsNext();
         }
     }
     return $ret;
 }
 /**
  * Converts the last record set into uoids
  * @param epClassMap $cm the class map for the conversion
  * @param array (of integers) object ids to be excluded
  * @return false|array (of uoids)
  * @throws epExceptionDbObject
  */
 protected function _rs2uoid($cm, $oids_ex = null)
 {
     // !!!important!!! with a large db, the list of oid to be excluded
     // $oids_ex can grown really large and can significantly slow down
     // queries. so it is suppressed in the select statement and moved
     // to this method to process.
     // get the class name
     $class = $cm->getName();
     // reset counter and return value
     $ret = array();
     // go through reach record
     $okay = $this->db->rsRestart();
     while ($okay) {
         // get oid column
         $oid = $this->db->rsGetCol($cn = $cm->getOidColumn(), $class . '.' . $cn);
         // exclude it?
         if ($oids_ex && in_array($oid, $oids_ex)) {
             // next row
             $okay = $this->db->rsNext();
             // exclude it
             continue;
         }
         // get class_b
         $class_b = $this->db->rsGetCol('class_b', $class . '.' . 'class_b');
         // get oid_b
         $oid_b = $this->db->rsGetCol('oid_b', $class . '.' . 'oid_b');
         // collect return result
         $ret[] = $class_b . ':' . $oid_b;
         // next row
         $okay = $this->db->rsNext();
     }
     return $ret;
 }
Beispiel #4
0
 /**
  * Checks index in database table
  * @param epClassMap $cm
  * @param epDb $db
  * @return false|array
  */
 public function checkIndex($cm, $db)
 {
     // show all keys in the table
     $sql = 'SHOW KEYS FROM ' . $db->quoteId($cm->getTable());
     if (!$db->execute($sql)) {
         return false;
     }
     // reset return array
     $ret = array(array(), array());
     // go through reach record
     $okay = $db->rsRestart();
     while ($okay) {
         // get index name
         $name = $db->rsGetCol('Key_name');
         // skip the primary key
         if ($name == 'PRIMARY') {
             // next row
             $okay = $db->rsNext();
             continue;
         }
         // get the column name
         $column = $db->rsGetCol('Column_name');
         // get if it is unique or not
         $unique = $db->rsGetCol('Non_unique');
         // $unique is 1 if an index
         // $unique is 0 if unique
         // collect return result
         $ret[$unique][$name][] = $column;
         // next row
         $okay = $db->rsNext();
     }
     return $ret;
 }
 /**
  * Converts the last record set into epObject object(s) with class map
  * @param epClassMap $cm the class map for the conversion
  * @param array (of integers) object ids to be excluded
  * @return false|array (of epObject)
  * @throws epExceptionDbObject
  */
 protected function &_rs2obj($cm, $ex = null)
 {
     // !!!important!!! with a large db, the list of oid to be excluded
     // $ex can grown really large and can significantly slow down
     // queries. so it is suppressed in the select statement and moved
     // to this method to process.
     // get epManager instance and cache it
     if (!$this->ep_m) {
         $this->ep_m =& epManager::instance();
     }
     // get the class name
     $class = $cm->getName();
     // get all mapped vars
     if (!($fms = $cm->getAllFields())) {
         return self::$false;
     }
     // reset counter and return value
     $ret = array();
     // go through reach record
     $okay = $this->db->rsRestart();
     while ($okay) {
         // get oid column
         $oid = $this->db->rsGetCol($cn = $cm->getOidColumn(), $class . '.' . $cn);
         // exclude it?
         if ($ex && in_array($oid, $ex)) {
             // next row
             $okay = $this->db->rsNext();
             // exclude it
             continue;
         }
         // call epManager to create an instance (false: no caching; false: no event dispatching)
         if (!($o =& $this->ep_m->_create($class, false, false))) {
             // next row
             $okay = $this->db->rsNext();
             continue;
         }
         // go through each field
         foreach ($fms as $fname => $fm) {
             // skip non-primivite field
             if (!$fm->isPrimitive()) {
                 continue;
             }
             // get var value and set to object
             $val = $this->db->rsGetCol($cn = $fm->getColumnName(), $class . '.' . $cn);
             // set value to var (true: no dirty flag change)
             $o->epSet($fm->getName(), $this->_castType($val, $fm->getType()), true);
         }
         // set oid
         $o->epSetObjectId($oid);
         // collect return result
         $ret[] = $o;
         // next row
         $okay = $this->db->rsNext();
     }
     return $ret;
 }