check_column()
云策文档标注
概述
check_column() 函数用于检查数据库表列是否符合指定条件,通过 SQL DESC 语句获取列信息并进行比对。它允许开发者验证表列的类型、是否允许 NULL、键信息、默认值和额外属性。
关键要点
- 函数使用 SQL DESC 语句检索表列信息,包括 Field、Type、Null、Key、Default 和 Extra。
- 参数包括表名、列名、列类型等,传递 null 可跳过对应条件的检查。
- 返回布尔值:true 表示匹配,false 表示不匹配。
- 列名区分大小写,需与 DESC 返回的 Field 值精确匹配。
代码示例
function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) {
global $wpdb;
$diffs = 0;
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Cannot be prepared. Fetches columns for table names.
$results = $wpdb->get_results( "DESC $table_name" );
foreach ( $results as $row ) {
if ( $row->Field === $col_name ) {
// Got our column, check the params.
if ( ( null !== $col_type ) && ( $row->Type !== $col_type ) ) {
++$diffs;
}
if ( ( null !== $is_null ) && ( $row->Null !== $is_null ) ) {
++$diffs;
}
if ( ( null !== $key ) && ( $row->Key !== $key ) ) {
++$diffs;
}
if ( ( null !== $default_value ) && ( $row->Default !== $default_value ) ) {
++$diffs;
}
if ( ( null !== $extra ) && ( $row->Extra !== $extra ) ) {
++$diffs;
}
if ( $diffs > 0 ) {
return false;
}
return true;
} // End if found our column.
}
return false;
}注意事项
- 使用 wpdb::get_results() 执行 SQL 查询,需注意 SQL 注入风险,但此处因表名不可准备而忽略。
- 函数从 WordPress 1.0.0 版本引入,相关功能可参考 wpdb::get_results()。
原文内容
Checks that database table column matches the criteria.
Description
Uses the SQL DESC for retrieving the table info for the column. It will help understand the parameters, if you do more research on what column information is returned by the SQL statement. Pass in null to skip checking that criteria.
Column names returned from DESC table are case sensitive and are as listed:
- Field
- Type
- Null
- Key
- Default
- Extra
Parameters
$table_namestringrequired-
Database table name.
$col_namestringrequired-
Table column name.
$col_typestringrequired-
Table column type.
$is_nullbooloptional-
Check is null.
Default:
null $keymixedoptional-
Key info.
Default:
null $default_valuemixedoptional-
Default value.
Default:
null $extramixedoptional-
Extra value.
Default:
null
Source
function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default_value = null, $extra = null ) {
global $wpdb;
$diffs = 0;
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Cannot be prepared. Fetches columns for table names.
$results = $wpdb->get_results( "DESC $table_name" );
foreach ( $results as $row ) {
if ( $row->Field === $col_name ) {
// Got our column, check the params.
if ( ( null !== $col_type ) && ( $row->Type !== $col_type ) ) {
++$diffs;
}
if ( ( null !== $is_null ) && ( $row->Null !== $is_null ) ) {
++$diffs;
}
if ( ( null !== $key ) && ( $row->Key !== $key ) ) {
++$diffs;
}
if ( ( null !== $default_value ) && ( $row->Default !== $default_value ) ) {
++$diffs;
}
if ( ( null !== $extra ) && ( $row->Extra !== $extra ) ) {
++$diffs;
}
if ( $diffs > 0 ) {
return false;
}
return true;
} // End if found our column.
}
return false;
}
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |