tyty16 / sqldeadcolumnfinder
查找空或仅有一个唯一值的列
Requires
- php: >=5.1.0
This package is not auto-updated.
Last update: 2024-09-14 18:58:31 UTC
README
一个php类,用于在SQL数据库中查找“死”列(空或只有一个唯一值)。
##安装
此库需要PHP 5.1或更高版本,但建议使用PHP的最新版本。它没有其他依赖项。
它可以通过Composer自动加载并安装tyty16/sqldeadcolumnfinder,或者可以单独下载。
##入门
###实例化
使用pdo连接和要检查的数据库名称实例化SQLDeadColumnFinder类。可选参数包括
-$all (布尔值,表示是否检查所有表,或者仅检查具有created_at列的表) 默认:false -$months (整数,表示在created_at日期之前多少个月进行检查。当all设置为false时使用) 默认:6 -$file (字符串,表示期望的输出文件名) 默认:‘dead-columns’
####示例
<?php $db = new PDO('mysql:host=localhost;dbname=test', $user, $pass); $sqlDeadColumnFinder = new SQLDeadColumnFinder($db, 'test', false, 6, 'path/to/dir/deadColumns'); ?>
###查找死列
调用find()方法,该方法将调用单个方法来收集要检查的列,检查它们,然后将数据导出到csv文件中,文件名可以是默认文件名,也可以是在实例化时提供的文件路径。
####示例
<?php $sqlDeadColumnFinder->find(); ?>
##方法
可以单独调用方法,但必须遵循输入参数的预期数组格式。
###getTablesToCheck()
从INFORMATION_SCHEMA收集要检查的表名。如果all设置为true,则将仅包括具有“created_at”列的表。
####示例
<?php $tables = array(); $tables = $sqlDeadColumnFinder->getTablesToCheck(); ?>
####返回数组格式
array(2) {
[0]=>
array(2) {
["TABLE_NAME"]=>
string(3) "foo"
["TABLE_SCHEMA"]=>
string(4) "test"
}
[1]=>
array(2) {
["TABLE_NAME"]=>
string(3) "bar"
["TABLE_SCHEMA"]=>
string(4) "test"
}
}
###getColumnsToCheck($tables)
使用$tables数组参数从INFORMATION_SCHEMA收集要检查的列名。$tables必须与getTablesToCheck()返回的数组格式相同。此方法还会增加$numColumns字段。
####示例
<?php $columns = $sqlDeadColumnFinder->getColumnsToCheck($tables); ?>
####返回数组格式
array(3) {
[0]=>
array(3) {
["TABLE_SCHEMA"]=>
string(4) "test"
["TABLE_NAME"]=>
string(3) "foo"
["COLUMN_NAME"]=>
string(2) "id"
}
[1]=>
array(2) {
["TABLE_SCHEMA"]=>
string(4) "test"
["TABLE_NAME"]=>
string(3) "foo"
["COLUMN_NAME"]=>
string(2) "name"
}
[2]=>
array(2) {
["TABLE_SCHEMA"]=>
string(4) "test"
["TABLE_NAME"]=>
string(3) "bar"
["COLUMN_NAME"]=>
string(2) "address"
}
}
###formatTablesWithColumns($columnsByTable)
以在findDeadColumns()中使用嵌套for循环的方式格式化列和表名。$columnsByTable的格式应与getColumnsToCheck()返回的数组格式相同。
####示例
<?php $formattedTablesWithColumns = sqlDeadColumnFinder->formatTablesWithColumns($unformattedColumns); ?>
####返回数组格式
array(1) {
["test"]=>
array(2) {
["foo"]=>
array(2) {
[0]=>
string(2) "id"
[1]=>
string(4) "name"
}
["bar"]=>
array(1) {
[0]=>
string(7) "address"
}
}
}
###findDeadColumns($dbWithTablesWithColumns)
搜索给定的列列表参数,查找具有一个唯一值或完全为null的列。如果all设置为false,则仅包括最近几个月内的记录。$dbWithTablesWithColumns必须与formatTablesWithColumns()返回的数组格式相同。将返回一个数组,其中每个列的值如下
-distinct (该列的唯一值数量) -value (唯一值本身) -is_null (列是否完全为null)
####示例
<?php $deadColumns = sqlDeadColumnFinder->formatTablesWithColumns($unformattedColumns); ?>
####返回数组格式
array(1) {
["test"]=>
array(1) {
["foo"]=>
array(2) {
["id"]=>
array(3) {
["distinct"]=>
int(1)
["value"]=>
string(1) "1"
["is_null"]=>
int(0)
}
["name"]=>
array(3) {
["distinct"]=>
int(0)
["value"]=>
string(4) "NULL"
["is_null"]=>
int(1)
}
}
}
}
###outputToFile($deadColumns)
接受死列的列表,对其进行格式化,并将它们输出到.csv文件。$deadColumns的格式必须与findDeadColumns()返回的数组格式相同。文件将保存在默认文件路径“dead-columns.csv”或指定的文件路径。.csv中的列如下
-Database:包含死列的数据库的名称 -Table:包含死列的表的名称 -Column:死列的名称 -Distinct Values:死列的唯一值数量 -Value:唯一值本身 -在过去的x个月中:记录文件中的$months值
####示例
<?php sqlDeadColumnFinder->outputToFile($deadColumns); ?>