Exemplo n.º 1
0
 /**
  * Retorna o ultimo ID da tabela para campos auto-increment
  * @author Hugo Ferreira da Silva
  * @link http://www.hufersil.com.br/
  * @param string $campo Nome do campo da tabela de auto-increment
  * @return int Valor da ultima insercao
  */
 public function getLastId($campo)
 {
     $obj = Lumine::factory($this->getTablename());
     $field = $obj->metadata()->getFieldByColumn($campo);
     $obj->destroy();
     // se nao tiver sequence
     if (empty($field['options']['sequence'])) {
         // verificacao de existencia de esquema no nome da tabela.
         // sugestao de Tiago Hiller - 05/10/2011
         $tablename = $this->getTablename();
         $hasSchema = preg_match('@\\b(?P<schema>\\w+)\\.(?P<tablename>\\w+)\\b@', $this->getTablename(), $res);
         if ($hasSchema) {
             $tablename = $res['tablename'];
         }
         $sql = "SELECT currval( s2.nspname || '.' || t2.relname ) AS id\n\t\t\t\t\tFROM pg_depend AS d\n\t\t\t\t\tJOIN pg_class AS t1 ON t1.oid = d.refobjid\n\t\t\t\t\tJOIN pg_class AS t2 ON t2.oid = d.objid\n\t\t\t\t\tJOIN pg_namespace AS s1 ON s1.oid = t1.relnamespace\n\t\t\t\t\tJOIN pg_namespace AS s2 ON s2.oid = t2.relnamespace\n\t\t\t\t\tJOIN pg_attribute AS a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid\n\t\t\t\t\tWHERE t1.relkind = 'r'\n\t\t\t\t\tAND t2.relkind = 'S'\n\t\t\t\t\tAND t1.relname = '" . $tablename . "'\n\t\t\t\t\tAND attname = '" . $campo . "'";
         if ($hasSchema) {
             $sql .= " AND s2.nspname = '{$res['schema']}'";
         }
     } else {
         $sql = "SELECT currval('" . $field['options']['sequence'] . "') as id";
     }
     $cn = $this->getConnection();
     $rs = $cn->executeSQL($sql);
     if (pg_num_rows($rs) > 0) {
         $line = pg_fetch_row($rs);
         pg_free_result($rs);
         return $line[0];
     }
     pg_free_result($rs);
     return 0;
 }
Exemplo n.º 2
0
 /**
  * Retorna o ultimo ID da tabela para campos auto-increment
  * @author Hugo Ferreira da Silva
  * @link http://www.hufersil.com.br/
  * @param string $campo Nome do campo da tabela de auto-increment
  * @return int Valor da ultima insercao
  */
 public function getLastId($campo)
 {
     $obj = Lumine::factory($this->getTablename());
     $field = $obj->_getFieldByColumn($campo);
     $obj->destroy();
     // se nao tiver sequence
     if (empty($field['options']['sequence'])) {
         $sql = "SELECT currval( s2.nspname || '.' || t2.relname ) AS id\n\t\t\t\t\tFROM pg_depend AS d\n\t\t\t\t\tJOIN pg_class AS t1 ON t1.oid = d.refobjid\n\t\t\t\t\tJOIN pg_class AS t2 ON t2.oid = d.objid\n\t\t\t\t\tJOIN pg_namespace AS s1 ON s1.oid = t1.relnamespace\n\t\t\t\t\tJOIN pg_namespace AS s2 ON s2.oid = t2.relnamespace\n\t\t\t\t\tJOIN pg_attribute AS a ON a.attrelid = d.refobjid AND a.attnum = d.refobjsubid\n\t\t\t\t\tWHERE t1.relkind = 'r'\n\t\t\t\t\tAND t2.relkind = 'S'\n\t\t\t\t\tAND t1.relname = '" . $this->getTablename() . "'\n\t\t\t\t\tAND attname = '" . $campo . "'";
     } else {
         $sql = "SELECT currval('" . $field['options']['sequence'] . "') as id";
     }
     $cn = $this->getConnection();
     $rs = $cn->executeSQL($sql);
     if (pg_num_rows($rs) > 0) {
         $line = pg_fetch_row($rs);
         pg_free_result($rs);
         return $line[0];
     }
     pg_free_result($rs);
     return 0;
 }