Hàm lấy danh sách Distinct toàn bộ bản ghi theo điều kiện

@author: 713uk13m <dev@nguyenanhung.com>
@time  : 2019-04-07 04:21

@param string $selectField
@param array  $whereValue

@return \Illuminate\Support\Collection
169075
public function getDistinctResultByColumn($selectField = '', $whereValue = [])
    {
        if (!is_array($selectField)) {
            $selectField = [$selectField];
        }
        $this->connection();
        $db = DB::table($this->table);
        if (is_array($whereValue) && count($whereValue) > 0) {
            foreach ($whereValue as $column => $column_value) {
                if (is_array($column_value)) {
                    $db->whereIn($column, $column_value);
                } else {
                    $db->where($column, self::OPERATOR_EQUAL_TO, $column_value);
                }
            }
        } else {
            if (!empty($whereValue)) {
                if (is_array($whereValue)) {
                    $db->whereIn($selectField, $whereValue);
                } else {
                    $db->where($selectField, self::OPERATOR_EQUAL_TO, $whereValue);
                }
            }
        }
        $db->distinct();
        $this->debug->debug(__FUNCTION__, 'SQL Queries: ' . $db->toSql());
        $result = $db->get($selectField);
        $this->debug->debug(__FUNCTION__, 'Result from DB => ' . json_encode($result));

        return $result;
    }
601826
public function row($fields = null, $where = null, $orderBy = null)
    {
        // 查询数据
        $row = parent::row($fields, $where, $orderBy);

        // 如果没有查到
        if (!$row) {
            $row = [];
        }
        // 构造 为行对象
        return new Row($this->tableName, $row);
    }
43956
public function resolveField(FieldValue $fieldValue): FieldValue
    {
        /** @var \Illuminate\Database\Eloquent\Model $model */
        $model = $this->getModelClass();

        return $fieldValue->setResolver(
            function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($model) {
                $results = $resolveInfo
                    ->builder
                    ->addScopes(
                        $this->directiveArgValue('scopes', [])
                    )
                    ->apply(
                        $model::query(),
                        $args
                    )
                    ->get();

                if ($results->count() > 1) {
                    throw new Error('The query returned more than one result.');
                }

                return $results->first();
            }
        );
    }
1161776
public function find(array $where = [], array $field = [])
    {
        return $this
            ->createQuery(
                $this->getQueryBuilder()
                    ->select(array() === $field ? $this->getAlias() : $field)
                    ->from($this->getTable())
                    ->where($where)
            )
            ->execute()
            ->getOne()
            ;
    }
338856
protected function _where(Kwf_Db_Table_Select $select, $where)
    {
        $where = (array) $where;

        foreach ($where as $key => $val) {
            // is $key an int?
            if (is_int($key)) {
                // $val is the full condition
                $select->where($val);
            } else {
                // $key is the condition with placeholder,
                // and $val is quoted into the condition
                $select->where($key, $val);
            }
        }

        return $select;
    }
1359398
protected function parseWhere(Query $query, $where)
    {
        $options  = $query->getOptions();
        $whereStr = $this->buildWhere($query, $where);

        if (!empty($options['soft_delete'])) {
            // 附加软删除条件
            list($field, $condition) = $options['soft_delete'];

            $binds    = $this->connection->getFieldsBind($options['table']);
            $whereStr = $whereStr ? '( ' . $whereStr . ' ) AND ' : '';
            $whereStr = $whereStr . $this->parseWhereItem($query, $field, $condition, '', $binds);
        }

        return empty($whereStr) ? '' : ' WHERE ' . $whereStr;
    }
761341
public function get($fields, $where = null, $orderBy = null)
    {
        // 按SELECT进行查询
        $table = self::selectArray($fields, $where, $orderBy, 1);

        // 如果没有查到
        if (!$table or !count($table)) {
            return null;
        }

        // 取第一行
        $row = $table[0];

        // 取所有的键名
        $keys = array_keys($row);

        // 取第一列的值
        return $row[$keys[0]];
    }
1192491
public function select($table, $where = [], array $bind = [])
    {
        $table = $this->quote()->identifier($table);
        $sql   = "SELECT * FROM $table";
        if (is_array($where)) {
            $where = $this->createWhereExpression($where);
        }
        $sql .= (($where) ? " WHERE {$where}" : '');

        return $this->query($sql, $bind);
    }
1238965
protected function parseItem($field, $where = [])
    {
        $op        = $where[0];
        $condition = isset($where[1]) ? $where[1] : null;

        if (is_array($op)) {
            // 同一字段多条件查询
            array_unshift($where, $field);
        } elseif (is_null($condition)) {
            if (in_array(strtoupper($op), ['NULL', 'NOTNULL', 'NOT NULL'], true)) {
                // null查询
                $where = [$field, $op, ''];
            } elseif (in_array($op, ['=', 'eq', 'EQ', null], true)) {
                $where = [$field, 'NULL', ''];
            } elseif (in_array($op, ['<>', 'neq', 'NEQ'], true)) {
                $where = [$field, 'NOTNULL', ''];
            } else {
                // 字段相等查询
                $where = [$field, '=', $op];
            }
        } else {
            $where = [$field, $op, $condition];
        }

        return $where;
    }
706309
private function select2($fields = false, $where = null, $orderBy = null, $limit = null): array
    {
        //合并请求参数
        $statement = $this->statement;
        $statement->select($fields, $where, $orderBy, $limit);

        // 执行前置 钩子
        $ret = $this->before('Select', $statement);
        if ($ret === self::INTERRUPT) {
            return [];
        }

        //修正查询参数
        list($this->statement) = $ret;

        // 查询结果
        $ret = self::query2();

        // 清除Statement缓存
        $this->clear();

        // 执行后置钩子
        return $this->after('Select', $ret);
    }
1253809
public function delete($table, $where, $whereValue)
    {
        $query = $this->provider->buildDeleteQuery($table, $where, $whereValue);

        return $this->provider->query($query, null);
    }
828753
public function whereNotExists(SelectQuery $sql)
    {
        $instance = $this->whereStrategyInstance();

        $instance->whereStrategy()->whereNotExists($sql);

        return $instance;
    }
1333109
public function selectArray($fields = null, $where = null, $orderBy = null, $limit = null): array
    {
        return self::select2($fields, $where, $orderBy, $limit);
    }
345386
public function from($table, $selectFields = array('*'))
    {
    	list($tableName, $tableAlias) = $this->tableProcess($table);
        $this->fromTable[$tableAlias] = $tableName;
        if (empty($selectFields)) {
            return $this;
        }
        if ($this->queryType == self::QUERYTYPE_SELECT) {
            $this->fields[$tableAlias] = $this->fieldProcess($tableAlias, $selectFields);
        } else {
            $this->fields[$tableAlias] = $selectFields;
        }
        return $this;
    }
242546
public function col($fields, $where = null, $orderBy = null, $limit = null): array
    {
        // 按Select进行查询
        $table = self::selectArray($fields, $where, $orderBy, $limit);

        // 如果没有查到
        if (!$table) {
            return [];
        }

        // 保存返回值
        $ret = [];

        // 取所有的列名
        $keys = array_keys($table[0]);

        // 取第一个列名
        $key = $keys[0];

        // 逐个循环,取出一列的值
        foreach ($table as $row) {
            $ret[] = $row[$key];
        }

        // 返回一列
        return $ret;
    }
972595
public function fieldSelectAll($table, $fields, $where, $orderBy = null)
    {
        $table = $this->getTableName($table);

        $query = "SELECT ";

        $query .= $this->selectFields($fields);

        $query .= " FROM " . $table . " ";

        $params = null;
        if ($where !== self::NO_WHERE_CLAUSE) {
            $query .= "WHERE " . $this->where($where, $params);
        }

        if ($orderBy) {
            $query .= $this->orderBy($orderBy) . " ";
        }

        return $this->query($query, $params);
    }
952664
public function buildDeleteQuery($table, $whereKey, $whereValue)
    {
        $sql = "DELETE FROM " . $this->quoteSpecialName($table) . " WHERE " . $this->quoteSpecialName($whereKey) . " = " . $this->escapeItem($whereValue) . ';';


        return $sql;
    }