Drop sequence and triggers if exists, autoincrement objects.

@param  string $table
@return null
544194
public function dropAutoIncrementObjects($table)
    {
        // drop sequence and trigger object
        $prefix = $this->connection->getTablePrefix();
        // get the actual primary column name from table
        $col = $this->getPrimaryKey($prefix . $table);
        // if primary key col is set, drop auto increment objects
        if (isset($col) && ! empty($col)) {
            // drop sequence for auto increment
            $sequenceName = $this->createObjectName($prefix, $table, $col, 'seq');
            $this->sequence->drop($sequenceName);

            // drop trigger for auto increment work around
            $triggerName = $this->createObjectName($prefix, $table, $col, 'trg');
            $this->trigger->drop($triggerName);
        }
    }
217087
public function from($table)
    {
        $this->fromTable = (is_null($table)) ? get_class($this) : $table;

        return $this;
    }
1284103
public function truncateTable( $table )
    {
        $query = "DELETE FROM $table";

        $result = $this->execute( $query );

        if ( $result > 0 )
        {
            $this->execute( 'VACUUM' );

            return TRUE;
        }

        return FALSE;
    }
28696
private function isEmpty($table)
    {
        $table = $this->db->quoteName($table);

        /** @noinspection SqlDialectInspection */
        return $this->db->scalar("SELECT COUNT(*) FROM {$table}") == 0;
    }
627226
public function getAutoIncrementValue(string $table, string $column = 'id')
    {
        return "nextval('" . $this->connection->getDatabasePlatform()->getIdentitySequenceName($table, $column) . "')";
    }
603801
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 = $this->getQuotedName($table . '_id_seq');
        $lastSequence = null;

        try {
            $lastSequence = $this->getDbh()->lastInsertId($sequenceName);
        } catch (\PDOException $e) {
            // in this case, the sequence name might be combined with the primary key name
        }

        // here we check if for instance, it's something like table_primary_key_seq instead of table_id_seq
        // this could occur when you use some kind of import tool like pgloader
        if (!$lastSequence) {
            $primaryKeys = $this->getPrimaryKey($table);
            $pkName = array_shift($primaryKeys);
            $lastSequence = $this->getDbh()->lastInsertId($this->getQuotedName($table . '_' . $pkName . '_seq'));
        }

        return $lastSequence;
    }
1289471
public function delete($table)
    {
        $this->type  = 'DELETE';
        $this->table = $this->quoteName($table);
        $this->fromRaw($this->quoteName($table));
        return $this;
    }
396776
public function dropTable($table)
    {
        $changeSetElt = $this->createChangeSet('drop-table-' . $table->getName());
        $dropTableElt = $this->document->createElement('dropTable');

        $tableName = QualifiedName::fromAsset($table);

        if ($tableName->getNamespaceName()) {
            $dropTableElt->setAttribute('schemaName', $tableName->getNamespaceName());
        }
        $dropTableElt->setAttribute('tableName', $tableName->getName());
        // Should we add cascadeConstraints attribute ?
        // $dropTableElt->setAttribute('cascadeConstraints', 'false');

        $changeSetElt->appendChild($dropTableElt);
        $this->root->appendChild($changeSetElt);
    }
1213406
protected function findExistingCreateMigrationFile($table)
    {
        $directory = database_path('migrations');
        $files     = scandir($directory);
        $basePath  = $this->getMigrationBasePath();

        foreach ($files as $file) {
            if (strpos($file, 'create_'.$table->getName().'_table') !== false) {
                return $basePath.DIRECTORY_SEPARATOR.$file;
            }
        }

        return null;
    }
1037992
public function switchTable($table=null)
	{
		static $tTable;
		if(null===$table)
		{
			if(null!==$tTable)
			{
				$this->table=$tTable;
				$tTable=null;
			}
		}
		else
		{
			$tTable=$this->table;
			$this->table=$table;
		}
		$this->loadFields($this->fields, $this->fieldNames, $this->pk, $this->bitFields);
	}
693400
public function assertTableExists(string $table): void
    {
        if ($this->conn->getSchemaManager()->tablesExist([$table]) === false) {
            throw new NeuralyzerException("Table $table does not exist");
        }
    }
958269
public function drop(string $table)
    {
        $compiler = $this->connection->schemaCompiler();

        $result = $compiler->drop($table);

        $this->connection->command($result['sql'], $result['params']);

        //clear table list
        $this->tableList = null;
        unset($this->columns[strtolower($table)]);
    }
260740
public function dropPrimaryKey($table)
    {
        $table = $this->prefixTable($table);

        return $this->container->make('Piwik\Updater\Migration\Db\DropPrimaryKey', array(
            'table' => $table
        ));
    }
796066
public function getPrimaryKeyDDL(Table $table)
    {
        if ($table->hasPrimaryKey()) {

            $keys = $table->getPrimaryKey();

            //MySQL throws an 'Incorrect table definition; there can be only one auto column and it must be defined as a key'
            //if the primary key consists of multiple columns and if the first is not the autoIncrement one. So
            //this push the autoIncrement column to the first position if its not already.
            $autoIncrement = $table->getAutoIncrementPrimaryKey();
            if ($autoIncrement && $keys[0] != $autoIncrement) {
                $idx = array_search($autoIncrement, $keys);
                if ($idx !== false) {
                    unset($keys[$idx]);
                    array_unshift($keys, $autoIncrement);
                }
            }

            return 'PRIMARY KEY (' . $this->getColumnListDDL($keys) . ')';
        }
    }
179743
protected function nullifyEmptyNullables($table)
    {
        $columns = scheme()->columns($table);

        foreach ($this->data as $key => &$value) {
            if (!array_key_exists($key, $columns)) {
                continue;
            }

            if (!$columns[$key]->getNotnull() && empty($value)) {
                $value = null;
            }
        }
    }
227259
public function addPrimaryAutoIncrementIdFieldToTable($table): bool
    {
        // Drop primary key - suspend error because we can have no primary index
        @$this->sql_query('ALTER IGNORE TABLE `' . $table . '` DROP PRIMARY KEY');
        // Create field and primary index
        $this->sql_query('ALTER IGNORE TABLE  `' . $table . '` ADD  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST');

        return true;
    }
1216499
public function cleanup($table)
    {
        $this->checkTable($table);

        if (!$this->param('dry-run')) {
            $okayToDestroy = $this->in(__('Are you sure? This will irreversibly delete files'), ['y', 'n'], 'n');
            if ($okayToDestroy === 'N') {
                $this->out(__('Aborted, no files deleted.'));
                $this->_stop();
            }
        } else {
            $this->out(__('<info>Performing dry run cleanup.</info>'));
            $this->out($this->nl(0));
        }

        $config = $this->Table->behaviors()->Proffer->config();

        // Get the root upload folder for this table
        $uploadFieldFolders = glob(WWW_ROOT . 'files' . DS . strtolower($table) . DS . '*');

        // Loop through each upload field configured for this table (field)
        foreach ($uploadFieldFolders as $fieldFolder) {
            // Loop through each instance of an upload for this field (seed)
            $pathFieldName = pathinfo($fieldFolder, PATHINFO_BASENAME);
            $uploadFolders = glob($fieldFolder . DS . '*');
            foreach ($uploadFolders as $seedFolder) {
                // Does the seed exist in the db?
                $seed = pathinfo($seedFolder, PATHINFO_BASENAME);

                foreach ($config as $field => $settings) {
                    if ($pathFieldName != $field) {
                        continue;
                    }

                    $targets = [];

                    $record = $this->{$this->Table->alias()}->find()
                        ->select([
                            $field,
                            $settings['dir']
                        ])
                        ->where([
                            $settings['dir'] => $seed
                        ])
                        ->first();

                    if ($record) {
                        $record = $record->toArray();
                    } else {
                        $record = [];
                    }

                    if (!in_array($seed, $record)) {
                        // No it doesn't - remove the folder and it's contents - probably with a user prompt
                        if ($this->param('dry-run')) {
                            if ($this->param('verbose')) {
                                $this->out(__("Would remove folder `$seedFolder`"));
                            } else {
                                $this->out(__("Would remove folder `$seed`"));
                            }
                        } else {
                            array_map('unlink', glob($seedFolder . DS . '*'));
                            rmdir($seedFolder);

                            if ($this->param('verbose')) {
                                $this->out(__("Remove `$seedFolder` folder and contents"));
                            } else {
                                $this->out(__("Removed `$seed` folder and contents"));
                            }
                        }
                    } else {
                        $files = glob($seedFolder . DS . '*');

                        $filenames = array_map(function ($p) {
                            return pathinfo($p, PATHINFO_BASENAME);
                        }, $files);

                        $targets[] = $record[$field];
                        if (!empty($settings['thumbnailSizes'])) {
                            foreach ($settings['thumbnailSizes'] as $prefix => $dimensions) {
                                $targets[] = $prefix . '_' . $record[$field];
                            }
                        }

                        $filesToRemove = array_diff($filenames, $targets);

                        foreach ($filesToRemove as $file) {
                            if ($this->param('dry-run') && $this->param('verbose')) {
                                $this->out(__("Would delete `$seedFolder" . DS . "$file`"));
                            } elseif ($this->param('dry-run')) {
                                $this->out(__("Would delete `$file`"));
                            } else {
                                unlink($seedFolder . DS . $file);
                                if ($this->param('verbose')) {
                                    $this->out(__("Deleted `$seedFolder" . DS . "$file`"));
                                } else {
                                    $this->out(__("Deleted `$file`"));
                                }
                            }
                        }
                    }
                }
            }
        }

        $this->out($this->nl(0));
        $this->out(__('<info>Completed</info>'));
    }