Files
MES/core/db/Sql.php
T
2026-08-08 18:28:49 +08:00

57 lines
1.4 KiB
PHP

<?php
namespace core\db;
/**
* SQL 辅助类
*/
class Sql
{
// 构建 INSERT 语句
public static function insert($table, $data)
{
$fields = implode(', ', array_keys($data));
$placeholders = ':' . implode(', :', array_keys($data));
return 'INSERT INTO ' . $table . ' (' . $fields . ') VALUES (' . $placeholders . ')';
}
// 构建 UPDATE 语句
public static function update($table, $data, $where = '')
{
$set = array();
foreach ($data as $key => $value) {
$set[] = $key . ' = :' . $key;
}
$sql = 'UPDATE ' . $table . ' SET ' . implode(', ', $set);
if ($where) {
$sql .= ' WHERE ' . $where;
}
return $sql;
}
// 构建 SELECT 语句
public static function select($table, $fields = '*', $where = '', $order = '', $limit = '')
{
$sql = 'SELECT ' . $fields . ' FROM ' . $table;
if ($where) {
$sql .= ' WHERE ' . $where;
}
if ($order) {
$sql .= ' ORDER BY ' . $order;
}
if ($limit) {
$sql .= ' LIMIT ' . $limit;
}
return $sql;
}
// 构建 DELETE 语句
public static function delete($table, $where = '')
{
$sql = 'DELETE FROM ' . $table;
if ($where) {
$sql .= ' WHERE ' . $where;
}
return $sql;
}
}