/** * 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; }
/** * 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; }
/** * Executes multiple db queries * @param string|array $sql either a single sql statement or an array of statemetns * @return mixed */ protected function _execute($sql) { // make sql into an array if (!is_array($sql)) { $sql = array($sql); } // execute sql stmt one by one foreach ($sql as $sql_) { $r = $this->db->execute($sql_); } return $r; }
/** * 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; }
/** * Executes multiple db queries * @param string|array $sql either a single sql statement or an array of statemetns * @return mixed */ protected function _execute($sql) { // make sql into an array if (!is_array($sql)) { $sql = array($sql); } $r = true; // execute sql stmt one by one foreach ($sql as $sql_) { // skip empty queries if (!$sql_) { continue; } $r = $this->db->execute($sql_); } return $r; }