dev-main 2021-04-04 19:17 UTC

This package is auto-updated.

Last update: 2024-09-05 03:28:21 UTC


README

一个类型安全的查询构建器,旨在减少拼写错误。

安装

请注意,需要PHP 8.0或更高版本。

通过Composer

$ composer require falgunphp/typo

限制

  • 仅支持MySQL/MariaDB。
  • 目前仅提供SQL特性的非常小的子集。
  • 不适用于实际使用。

使用方法

在开始使用此库之前,请注意

  • Typo试图成为一个 数据库优先 的ORM/查询构建器。您不能用它设计数据库模式。与模式相关的元数据将来自现有数据库。
  • 它试图尽可能接近SQL语法结构。因此,代码看起来可能会很冗长。

要为您的数据库生成元数据,运行(安装后)

vendor/bin/generate-typo-meta

上述脚本将要求输入数据库凭证和元数据目录(它将在此处存储文件)。目前,元数据类已默认命名空间定义为 App\DB。您可以更改它以满足您的需求,但请注意,重新运行生成器脚本将 覆盖 您的更改。

一旦生成了元数据,我们就可以开始使用Typo来构建查询。

<?php
use Falgun\Kuery\Kuery;
use App\DB\Metas\UsersMeta;
use Falgun\Typo\Query\Builder;
use Falgun\Kuery\Configuration;
use Falgun\Kuery\Connection\MySqlConnection;

$confArray = [
      'host' => 'localhost',
      'user' => 'username',
      'password' => 'password',
      'database' => 'falgun'
  ];

// build configuration class
$configuration = Configuration::fromArray($confArray);

// build connection class
$connection = new MySqlConnection($configuration);

// attemp to connect
$connection->connect();

//create the query builder
$builder = new Builder(new Kuery($connection));

//load user meta, this is auto generated class
$userMeta = UsersMeta::new();

/**************
* Select Query
**************/
$users = $builder
    ->select(
        $userMeta->id(),
        $userMeta->name()->as('full_name'),
        $userMeta->username(),
    )
    ->from($userMeta->table())
    ->where($userMeta->id()->eq(12))
    ->orWhere($userMeta->id()->eq(13))
    ->orderBy($userMeta->id())
    ->limit(0, 100)
    ->fetch();

// This one will run below SQL
SELECT users.id, users.name as full_name, users.username
FROM users
WHERE users.id = ? OR users.id = ?
ORDER BY users.id ASC
LIMIT 0, 100

/**************
* Insert Query
**************/
$userID = $builder->insertInto(
    $userMeta->table(),
    $userMeta->name(),
    $userMeta->username()
    )
    ->values('New User', 'NewUser')
    ->execute();

// It will run below SQL
INSERT INTO users (users.name, users.username) VALUES (?, ?)

/**************
* Update Query
**************/
$builder->update($userMeta->table())
    ->set($userMeta->name(), 'Updated Name')
    ->set($userMeta->username(), 'UpdatedName')
    ->where($userMeta->id()->eq(10))
    ->execute();

// It will run below SQL
UPDATE users
SET users.name = ?, users.username = ?
WHERE users.id = ?

/**************
* Delete Query
**************/
$qb->delete($userMeta->table())
    ->where($userMeta->id()->eq(5))
    ->execute();

// It will run below SQL
DELETE FROM users
WHERE users.id = ?

许可证

本软件根据 LGPL 3.0 或更高版本许可证进行分发。请阅读 LICENSE 了解软件的可用性和分发信息。