influxdb-orm v1.1.0 發佈,時序數據庫 InfluxDB 的 ORM

介紹

一個用於 InfluxDB 時序數據庫的 ORM,終結沒有 InfluxDB ORM 的時代。php

經常使用操做一把梭,支持 php-fpm、Swoole 環境,一鍵輕鬆切換。git

能夠用於全部傳統框架、全部 Swoole 框架中!github

碼雲:https://gitee.com/yurunsoft/influxdb-orm數據庫

Github:https://github.com/Yurunsoft/influxdb-ormjson

更新日誌

  • 新增測試用例
  • 新增 travis 自動化測試
  • 完善註釋和參數返回值類型
  • 修復 ResultSet->getScalar() 默認值錯誤
  • 查詢構建器查詢後時區復原
  • 新增模型測試和查詢器測試
  • 修復 or 條件問題
  • QueryBuilder 支持設定 timezone()
  • 新增支持日期時間格式化
  • 模型新增 toArray() 方法
  • 爲 @Value 註解增長字段類型屬性

Composer

本項目能夠使用composer安裝,遵循psr-4自動加載規則,在你的 composer.json 中加入下面的內容:數組

{
    "require": {
        "yurunsoft/influxdb-orm": "^1.1.0"
    }
}

而後執行 composer update 安裝。composer

使用

Swoole 支持

WorkerStart 事件中執行:框架

\Yurun\Util\YurunHttp::setDefaultHandler(\Yurun\Util\YurunHttp\Handler\Swoole::class);

定義模型

具體可參考 example/test.php
<?php
namespace Yurun\InfluxDB\ORM\Example\Model;

use Yurun\InfluxDB\ORM\BaseModel;
use Yurun\InfluxDB\ORM\Annotation\Tag;
use Yurun\InfluxDB\ORM\Annotation\Field;
use Yurun\InfluxDB\ORM\Annotation\Value;
use Yurun\InfluxDB\ORM\Annotation\Timestamp;
use Yurun\InfluxDB\ORM\Annotation\Measurement;

/**
 * @Measurement(name="aaa")
 */
class A extends BaseModel
{
    /**
     * @Tag(name="id", type="int")
     *
     * @var int
     */
    private $id;

    /**
     * @Field(name="name", type="string")
     *
     * @var string
     */
    private $name;

    /**
     * @Timestamp(precision="s")
     *
     * @var int|string
     */
    private $time;

    /**
     * @Value
     *
     * @var int
     */
    private $value;

    public static function create($id, $name, $time, $value)
    {
        return new static(compact('id', 'name', 'time', 'value'));
    }

    /**
     * Get the value of time
     *
     * @return int|string
     */ 
    public function getTime()
    {
        return $this->time;
    }

    /**
     * Set the value of time
     *
     * @param int|string $time
     *
     * @return self
     */ 
    public function setTime($time)
    {
        $this->time = $time;

        return $this;
    }

    /**
     * Get the value of id
     *
     * @return int
     */ 
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set the value of id
     *
     * @param int $id
     *
     * @return self
     */ 
    public function setId(int $id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get the value of name
     *
     * @return string
     */ 
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set the value of name
     *
     * @param string $name
     *
     * @return self
     */ 
    public function setName(string $name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get the value of value
     *
     * @return int
     */ 
    public function getValue()
    {
        return $this->value;
    }

    /**
     * Set the value of value
     *
     * @param int $value
     *
     * @return self
     */ 
    public function setValue(int $value)
    {
        $this->value = $value;

        return $this;
    }
}

數據寫入

use Yurun\InfluxDB\ORM\InfluxDBManager;

// 設置客戶端名稱爲test,默認數據庫爲db_test
InfluxDBManager::setClientConfig('test', '127.0.0.1', 8086, '', '', false, false, 0, 0, 'db_test');
// 設置默認數據庫爲test
InfluxDBManager::setDefaultClientName('test');

// 寫入數據,支持對象和數組
$r = A::write([
    A::create(mt_rand(1, 999999), time(), time(), mt_rand(1, 100)),
    ['id'=>1, 'name'=>'aaa', 'time'=>time(), 'value'=>mt_rand(1, 100)],
]);

var_dump($r);

數據查詢

// 獲取查詢器
$query = A::query();

// 常見用法,反正就那一套,很少說了
$query->field('id,name')
      ->from('table')
      ->where([
          'id'    =>  1
      ])->where('id', '=', 1)
      ->orWhere('id', '=', 1)
      ->order('time', 'desc')
      ->group('id')
      ->limit(0, 10);

// 查詢結果,與 InfluxDB 官方客戶端同樣用法
$resultSet = $query->select();

// 查詢結果轉模型,適合用於查詢記錄而不是統計數據
$model = $resultSet->getModel(A::class);

// 查詢結果轉模型列表,適合用於查詢記錄而不是統計數據
$list = $resultSet->getModelList(A::class);

模型快捷查詢

適合用於查詢記錄而不是統計數據php-fpm

use Yurun\InfluxDB\ORM\Query\QueryBuilder;

// 查詢結果轉模型,適合用於查詢記錄而不是統計數據
$model = A::find(function(QueryBuilder $query){
    $query->where('id', '=', 1)->limit(1);
});

// 查詢結果轉模型列表,適合用於查詢記錄而不是統計數據
$list = A::select(function(QueryBuilder $query){
    $query->where('id', '=', 1)->limit(2);
});

獲取單個字段值

$count = A::query()->field('count(value)')->select()->getScalar();
相關文章
相關標籤/搜索