y0lk/sqldumper

SQL dump utility

0.2.2 2023-08-23 16:03 UTC

This package is auto-updated.

Last update: 2024-10-23 18:37:57 UTC


README

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality License Total Downloads

SQLDumper is a small library designed to easily create customized SQL dumps.

Installation

Via Composer

$ composer require y0lk/sqldumper

Usage

The concept is that you can select tables and set certain "dump parameters" for each table, made so you can chain a bunch of calls to quickly create a customized dump.

use Y0lk\SQLDumper\SQLDumper;

//Init the dumper with your DB info
$dumper = new SQLDumper('localhost', 'dbname', 'root', '');

//Set all tables to dump without data and without DROP statement
$dumper->allTables()
	->withData(false)
	->withDrop(false);

//Set table1 to dump with data
$dumper->table('table1')
	->withData(true);

//Set table2 and table3 to dump without structure (data only), and table3 with where condition
$dumper->listTables([
		'table2',
		'table3'
	])
	->withStructure(false)
	->table('table3')
		->where('id=2 OR foo="bar"');

//This will group DROP statements and put them at the beginning of the dump
$dumper->groupDrops(true);

//This will group INSERT statements and put them at the end of the dump
$dumper->groupInserts(true);

$dumper->save('dump.sql');

Dumper options

groupDrops(bool $group)

When set to TRUE, this will group DROP statements and put them at the beginning of the dump

groupInserts(bool $group)

When set to TRUE, this will group INSERT statements and put them at the end of the dump

Table selection

There are 3 basic methods to select tables. When a table or a list of tables are selected, they are returned as TableDumper objects on which you can set options for the dump.

allTables()

Selects all the tables in the DB

table(string|Table $table)

Select by table's name (string) or a Table object

listTables(array $listTables)

Select by a list of table names (string) or Table object

Table options

This is the list of methods available on each each TableDumper

withStructure(bool $withStructure)

Whether to dump the table CREATE structure

withData(bool $withData)

Whether to dump table data (INSERT statement)

withDrop(bool $withDrop)

Wheter to include the DROP statement (before CREATE)

where(string $where_string)

WHERE query string as regular SQL

Output

dump(resource $stream)

This the main dump function, outputs the dump to a stream.

save(string $filename)

Saves the dump to a file

License

The MIT License (MIT). Please see License File for more information.