<commit_before><?php
namespace Codeception\Util\Driver;

class PostgreSql extends Db
{
    protected $putline = false;

    protected $connection = null;

    public function cleanup()
    {
        $drops = $this->dbh->query("select 'drop table if exists \"' || tablename || '\" cascade;' from pg_tables where schemaname = 'public' ;")->fetchAll();

        if (!$drops) return;
        foreach ($drops as $drop) {
            $this->dbh->exec($drop[0]);
        }
    }

    public function sqlLine($sql)
    {
        if (!$this->putline) return parent::sqlLine($sql);

        if ($sql == '\.') {
            $this->putline = false;
            pg_put_line($this->connection, $sql."\n");
            pg_end_copy($this->connection);
            pg_close($this->connection);
        } else {
            pg_put_line($this->connection, $sql."\n");
        }
        return true;
    }

    public function sqlQuery($query)
    {
        if (strpos(trim($query), 'COPY ') === 0) {
            if (!extension_loaded('pgsql')) throw new \Codeception\Exception\Module('\Codeception\Module\Db', "To run 'COPY' commands 'pgsql' extension should be installed");
            $constring = str_replace(';',' ',substr($this->dsn,6));
            $constring .= ' user='.$this->user;
            $constring .= ' password='.$this->password;
            $this->connection = pg_connect($constring);
            pg_query($this->connection, $query);
            $this->putline = true;
        } else {
            $this->dbh->exec($query);
        }
    }

    public function select($column, $table, array $criteria) {
        $where = $criteria ? "where %s" : '';
        $query = 'select %s from "%s" '.$where;
        $params = array();
        foreach ($criteria as $k => $v) {
            if($v === NULL) {
                $params[] = "$k IS NULL ";
                unset($criteria[$k]);
            } else {
                $params[] = "$k = ? ";
            }
        }
        $params = implode('AND ', $params);

        return sprintf($query, $column, $table, $params);
    }

    public function lastInsertId($table) {
      /*We make an assumption that the sequence name for this table is based on how postgres names sequences for SERIAL columns */
      $sequenceName = $table.'_id_seq';
      return $this->getDbh()->lastInsertId($sequenceName);
    }
}
<commit_msg>Fix delete sequences in PostgreSQL friver<commit_after><?php
namespace Codeception\Util\Driver;

class PostgreSql extends Db
{
    protected $putline = false;

    protected $connection = null;

    public function cleanup()
    {
        $tables = $this->dbh->query("SELECT 'DROP TABLE IF EXISTS \"' || tablename || '\" cascade;' FROM pg_tables WHERE schemaname = 'public';")->fetchAll();
        $sequences = $this->dbh->query("SELECT 'DROP SEQUENCE IF EXISTS \"' || relname || '\" cascade;' FROM pg_class WHERE relkind = 'S';")->fetchAll();

        $drops = array_merge($tables, $sequences);
        if ($drops) {
            foreach ($drops as $drop) {
                $this->dbh->exec($drop[0]);
            }
        }
    }

    public function sqlLine($sql)
    {
        if (!$this->putline) return parent::sqlLine($sql);

        if ($sql == '\.') {
            $this->putline = false;
            pg_put_line($this->connection, $sql."\n");
            pg_end_copy($this->connection);
            pg_close($this->connection);
        } else {
            pg_put_line($this->connection, $sql."\n");
        }
        return true;
    }

    public function sqlQuery($query)
    {
        if (strpos(trim($query), 'COPY ') === 0) {
            if (!extension_loaded('pgsql')) throw new \Codeception\Exception\Module('\Codeception\Module\Db', "To run 'COPY' commands 'pgsql' extension should be installed");
            $constring = str_replace(';',' ',substr($this->dsn,6));
            $constring .= ' user='.$this->user;
            $constring .= ' password='.$this->password;
            $this->connection = pg_connect($constring);
            pg_query($this->connection, $query);
            $this->putline = true;
        } else {
            $this->dbh->exec($query);
        }
    }

    public function select($column, $table, array $criteria) {
        $where = $criteria ? "where %s" : '';
        $query = 'select %s from "%s" '.$where;
        $params = array();
        foreach ($criteria as $k => $v) {
            if($v === NULL) {
                $params[] = "$k IS NULL ";
                unset($criteria[$k]);
            } else {
                $params[] = "$k = ? ";
            }
        }
        $params = implode('AND ', $params);

        return sprintf($query, $column, $table, $params);
    }

    public function lastInsertId($table) {
      /*We make an assumption that the sequence name for this table is based on how postgres names sequences for SERIAL columns */
      $sequenceName = $table.'_id_seq';
      return $this->getDbh()->lastInsertId($sequenceName);
    }
}

{'fraction_non_alphanumeric': 0.164991961414791, 'fraction_numerical': 0.0012057877813504824, 'mean_word_length': 2.106741573033708, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1203538', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1450, 'n_words': 345}