首页/应用软件/内容

如何写一个属于自己的数据库封装(3)

应用软件2022-10-23 阅读()
SQL是Structured Query Language(结构化查询语言)的缩写。SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言。在使用它时,只需要发出“做什么”的命令,“怎么做”是不用使用者考虑的。SQL功能强大、简单易学、使用方便,已经成为了数据库操作的基础,并且现在几乎所有的数据库均支持SQL。

本期要点

深入了解php函数的各种辅助函数 PHP核心语法:函数

理解什么是可变参数函数, ...$var, PHP5.6新特性介绍

compact函数的用法 PHP: compact - Manual

list函数的用法 PHP: list - Manual

PHP魔术方法

开始之前

本期说的是SQL中的查询语句, 由于复杂程度和多个类之间的关联性不是Connector篇那么简单, 故此, 这一回需要一口气讲解Builder类, Grammar类, 还有Model类, 当然, 只是查询部分


Builder.php

<?php
/**
* 请求构建器
*/
class Builder {
    // 连接数据库, Connector类
    protected $connector;

    // 生成SQL语法,Grammar类
    protected $grammar;

    // 连接的Model, Model类
    protected $model;

    // SQL查询语句中条件的值
    // 虽然列出了全部, 但本教程只实现了where,其余的因为懒(理直气壮),请自行实现, 逻辑和where函数大致
    protected $bindings = [
        'select' => [],
        'join'   => [],
        'where'  => [],
        'having' => [],
        'order'  => [],
        'union'  => [],
    ];

    // select 语法想要查看的字段
    public $columns;

    // 过滤重复值
    public $distinct = false;

    // 需要查询的表
    public $from;

    // 所有join 语法
    public $joins;

    // 所有where 语法
    public $wheres;

    // group 语法
    public $groups;

    // having 语法
    public $havings;

    // order by 语法
    public $orders;

    // 限制数据库返回的数据量, limit语法
    public $limit;

    // 需要略过的数据量, offset语法
    public $offset;

    // 数据写保护, 开启后该条数据无法删除或改写
    public $writeLock = false;

接下来是函数

where()有两种不同的调用方式
接下来多个函数都有两种调用方式, 请从之后的代码逻辑中自行领悟
以参数的方式, 默认为'='

Actor::select('first_name', 'last_name')
  ->where('first_name', 'NICK')
  ->where('last_name','!=', 'WAHLBERG')
  ->first()

以数组的方式, 这方式有局限性, 仅能匹配 字段等于值 的情况

Actor::select('first_name', 'last_name')
  ->where(['first_name'=> 'NICK', 'last_name'=> 'WAHLBERG'])
  ->first()

接下来看代码

public function where($column, $operator = null, $value = null, $boolean = 'and') {
        // 判定$column是否为数组
        if (is_array($column)) {
            // 如果是数组, 循环再调用本函数,where()
            foreach ($column as $key => $value) 
                $this->where($key, "=", $value, $boolean);
        }else {
            // 反之, 判定参数数量和$value是否为空, 如果为真,这意味着用户省略了'=',自动添加
            if(func_num_args() == 2      is_null($value)) list($operator, $value) = ['=', $operator];

            // 最简单原始的条件查询, 所以$type值为Basic
            $type = "Basic";
            // 将处理过的条件存入$wheres
            $this->wheres[] = compact('type', 'column', 'operator', 'value', 'boolean');
            // 将字段需要匹配的值存入$bindings中的where
            $this->addBinding($value, 'where');
        }

        // 返回Builder实例
        return $this;
    }

Grammar.php

<?php
/**
* 数据库语法生成    
*/
class Grammar {
    // 构建查询语句所可能出现的各种SQL语法
    // 注意, 它们的顺序是对应着各自在SQL语句中合法的位置
    // sqlsrv略微不同
    protected $selectComponents = [
        'distinct',
        'columns',
        'from',
        'joins',
        'wheres',
        'groups',
        'orders',
        'limit',
        'offset',
    ];

Model.php

<?php
/**
* 入口文件, 数据库表的父类
*/
class Model {
    // SQL命令构建器, Builder类
    protected $builder;

    // 数据库返回的数据存在这里
    protected $data;

    // 数据库表名, 选填, 默认为类名
    protected $table;

    // 主键, 二选一($unique)
    protected $identity;

    // unique key, 二选一($identity)
    protected $unique;


本期疑问

1.) 缺少的Builder函数如果有人愿意提供例子就好了, 进阶复杂的语句那就更好了, 直接写然后再分享给我那就最好了
2.) 有些函数或结构可能没有效率或者白白添加服务器压力, 但我写的顺了可能没看见, 请指出
3.) 有人能告诉我laravel是怎么解决pdo 2100个最大参数(bind)的问题吗? 源代码把我看蒙了

以上就是如何写一个属于自己的数据库封装(3)的详细内容,更多请关注php中文网其它相关文章!


学习教程快速掌握从入门到精通的SQL知识。

……

相关阅读