core/SqlSelector.php
public function createSql($type = 0)
    {

        if (!empty($this->sqlTemplate)) {
            $runtimeDir = Config::get('sdopx.runtime_dir', 'runtime');
            $runtimeDir = Utils::trimPath($runtimeDir);
            $runtimeDir = Utils::path(ROOT_DIR, $runtimeDir);
            if ($type == 2) {
                $order = $this->orderItem;
                $limit = $this->limit;
                $this->orderItem = null;
                $this->limit = null;
                $segment = $this->getSegment();
                $this->orderItem = $order;
                $this->limit = $limit;
                $sql = Sdopx::fetchSQL($this->sqlTemplate, $segment, $runtimeDir);
                return ['sql' => 'select count(1) from (' . $sql . ') countTempTable', 'args' => []];
            } else {
                $segment = $this->getSegment();
                $sql = Sdopx::fetchSQL($this->sqlTemplate, $segment, $runtimeDir);
                return ['sql' => $sql, 'args' => []];
            }
        }
        $sqlItems = [];
        $argItems = [];
        //获取查询数量的sql语句
        if ($type == 2) {
            if ($this->groupItem != null || $this->unionItem != null) {
                $order = $this->orderItem;
                $limit = $this->limit;
                $this->orderItem = null;
                $this->limit = null;
                $temp = $this->createSql(0);
                $temp['sql'] = 'select count(1) from (' . $temp['sql'] . ') countTempTable';
                $this->orderItem = $order;
                $this->limit = $limit;
                return $temp;
            } else {
                $sqlItems[] = 'select count(1) from ' . $this->table;
                if (!empty($this->alias)) {
                    $sqlItems[] = ' ' . $this->alias;
                }
            }
        } //优化查询,只适合简单单表sql
        elseif ($type == 1) {
            $alias = 'Zt';
            $findSql = $alias . '.*';
            if ($this->fieldItem !== null) {
                $findSql = $this->fieldItem->sql;
                if ($this->fieldItem->args !== null) {
                    $argItems = array_merge($argItems, $this->fieldItem->args);
                }
            }
            $sqlItems[] = "select {$findSql} from `{$this->table}` {$alias},(select id from `{$this->table}`";
            if (!empty($this->alias)) {
                $sqlItems[] = ' ' . $this->alias;
            }
        } else {
            $findSql = '*';
            if ($this->fieldItem !== null) {
                $findSql = $this->fieldItem->sql;
                if ($this->fieldItem->args !== null) {
                    $argItems = array_merge($argItems, $this->fieldItem->args);
                }
            }
            $sqlItems[] = "select {$findSql} from `{$this->table}`";
            if (!empty($this->alias)) {
                $sqlItems[] = ' ' . $this->alias;
            }
        }
        //JSON
        if ($type == 2 || $type == 0) {
            if ($this->joinItem !== null) {
                $joinSql = $this->joinItem->sql;
                if ($this->joinItem->args !== null) {
                    $argItems = array_merge($argItems, $this->joinItem->args);
                }
                if (!empty($joinSql)) {
                    $sqlItems[] = ' ' . $joinSql;
                }
            }
        }
        //WHERE
        $frame = $this->condition->getFrame();
        if (!empty($frame['sql'])) {
            if (preg_match('@^(AND|OR)\s+@i', $frame['sql'])) {
                $sqlItems[] = 'where ' . preg_replace('@^(AND|OR)\s+@i', '', $frame['sql']);
            } else {
                $sqlItems[] = 'where ' . $frame['sql'];
            }
            if ($frame['args'] !== null && is_array($frame['args'])) {
                $argItems = array_merge($argItems, $frame['args']);
            }
        }
        //GROUP BY
        if ($this->groupItem != null) {
            $groupSql = $this->groupItem->sql;
            $groupArgs = $this->groupItem->args;
            if (!empty($groupSql)) {
                $sqlItems[] = 'group ' . $groupSql;
                if ($groupArgs !== null && is_array($groupArgs)) {
                    $argItems = array_merge($argItems, $groupArgs);
                }
            }
        }
        //处理 havingItem
        if ($this->havingItem != null) {
            $frame = $this->havingItem->getFrame();
            if (!empty($frame['sql'])) {
                if (preg_match('@^(AND|OR)\s+@i', $frame['sql'])) {
                    $sqlItems[] = 'having ' . preg_replace('@^(AND|OR)\s+@i', '', $frame['sql']);
                } else {
                    $sqlItems[] = 'having ' . $frame['sql'];
                }
                if ($frame['args'] !== null && is_array($frame['args'])) {
                    $argItems = array_merge($argItems, $frame['args']);
                }
            }
        }
        //UNION
        if ($type == 2 || $type == 0) {
            if ($this->unionItem) {
                array_unshift($sqlItems, '(');
                $sqlItems[] = ')';
                foreach ($this->unionItem as $item) {
                    $temp = $item->selector->createSql(0);
                    if ($item->all) {
                        $sqlItems[] = 'union all ( ' . $temp['sql'] . ' )';
                    } else {
                        $sqlItems[] = 'union ( ' . $temp['sql'] . ' )';
                    }
                    if (!empty($temp['args'])) {
                        $argItems = array_merge($argItems, $temp['args']);
                    }
                }
            }
        }
        //ORDER BY
        if ($type != 2 && $this->orderItem != null) {
            $orderSql = $this->orderItem->sql;
            $ordeArgs = $this->orderItem->args;
            if (!empty($orderSql)) {
                $sqlItems[] = 'order ' . $orderSql;
            }
            if ($ordeArgs !== null && is_array($ordeArgs)) {
                $argItems = array_merge($argItems, $ordeArgs);
            }
        }
        //LIMIT
        if ($type != 2 && !empty($this->limit)) {
            $sqlItems[] = $this->limit;
        }
        if ($type == 1) {
            $alias = 'Zt';
            $sqlItems[] = ') Y where ' . $alias . '.id=Y.id';
            if ($this->orderItem != null) {
                $orderSql = $this->orderItem->sql;
                $ordeArgs = $this->orderItem->args;
                if (!empty($orderSql)) {
                    $orderSql = preg_replace_callback('@by\s+(`?\w+`?)\s+(desc|asc)@i', function ($math) use ($alias) {
                        return 'by ' . $alias . '.' . $math[1] . ' ' . $math[2];
                    }, $orderSql);
                    $sqlItems[] = 'order ' . $orderSql;
                }
                if ($ordeArgs !== null && is_array($ordeArgs)) {
                    $argItems = array_merge($argItems, $ordeArgs);
                }
            }
        }
        return ['sql' => join(' ', $sqlItems), 'args' => $argItems];
    }
public function createSql($type = 0)
    {

        if (!empty($this->sqlTemplate)) {
            $runtimeDir = Config::get('sdopx.runtime_dir', 'runtime');
            $runtimeDir = Utils::trimPath($runtimeDir);
            $runtimeDir = Utils::path(ROOT_DIR, $runtimeDir);
            if ($type == 2) {
                $order = $this->orderItem;
                $limit = $this->limit;
                $this->orderItem = null;
                $this->limit = null;
                $segment = $this->getSegment();
                $this->orderItem = $order;
                $this->limit = $limit;
                $sql = Sdopx::fetchSQL($this->sqlTemplate, $segment, $runtimeDir);
                return ['sql' => 'select count(1) from (' . $sql . ') countTempTable', 'args' => []];
            } else {
                $segment = $this->getSegment();
                $sql = Sdopx::fetchSQL($this->sqlTemplate, $segment, $runtimeDir);
                return ['sql' => $sql, 'args' => []];
            }
        }
        $sqlItems = [];
        $argItems = [];
        //获取查询数量的sql语句
        if ($type == 2) {
            if ($this->groupItem != null || $this->unionItem != null) {
                $order = $this->orderItem;
                $limit = $this->limit;
                $this->orderItem = null;
                $this->limit = null;
                $temp = $this->createSql(0);
                $temp['sql'] = 'select count(1) from (' . $temp['sql'] . ') countTempTable';
                $this->orderItem = $order;
                $this->limit = $limit;
                return $temp;
            } else {
                $sqlItems[] = 'select count(1) from ' . $this->table;
                if (!empty($this->alias)) {
                    $sqlItems[] = ' ' . $this->alias;
                }
            }
        } //优化查询,只适合简单单表sql
        elseif ($type == 1) {
            $alias = 'Zt';
            $findSql = $alias . '.*';
            if ($this->fieldItem !== null) {
                $findSql = $this->fieldItem->sql;
                if ($this->fieldItem->args !== null) {
                    $argItems = array_merge($argItems, $this->fieldItem->args);
                }
            }
            $sqlItems[] = "select {$findSql} from `{$this->table}` {$alias},(select id from `{$this->table}`";
            if (!empty($this->alias)) {
                $sqlItems[] = ' ' . $this->alias;
            }
        } else {
            $findSql = '*';
            if ($this->fieldItem !== null) {
                $findSql = $this->fieldItem->sql;
                if ($this->fieldItem->args !== null) {
                    $argItems = array_merge($argItems, $this->fieldItem->args);
                }
            }
            $sqlItems[] = "select {$findSql} from `{$this->table}`";
            if (!empty($this->alias)) {
                $sqlItems[] = ' ' . $this->alias;
            }
        }
        //JSON
        if ($type == 2 || $type == 0) {
            if ($this->joinItem !== null) {
                $joinSql = $this->joinItem->sql;
                if ($this->joinItem->args !== null) {
                    $argItems = array_merge($argItems, $this->joinItem->args);
                }
                if (!empty($joinSql)) {
                    $sqlItems[] = ' ' . $joinSql;
                }
            }
        }
        //WHERE
        $frame = $this->condition->getFrame();
        if (!empty($frame['sql'])) {
            if (preg_match('@^(AND|OR)\s+@i', $frame['sql'])) {
                $sqlItems[] = 'where ' . preg_replace('@^(AND|OR)\s+@i', '', $frame['sql']);
            } else {
                $sqlItems[] = 'where ' . $frame['sql'];
            }
            if ($frame['args'] !== null && is_array($frame['args'])) {
                $argItems = array_merge($argItems, $frame['args']);
            }
        }
        //GROUP BY
        if ($this->groupItem != null) {
            $groupSql = $this->groupItem->sql;
            $groupArgs = $this->groupItem->args;
            if (!empty($groupSql)) {
                $sqlItems[] = 'group ' . $groupSql;
                if ($groupArgs !== null && is_array($groupArgs)) {
                    $argItems = array_merge($argItems, $groupArgs);
                }
            }
        }
        //处理 havingItem
        if ($this->havingItem != null) {
            $frame = $this->havingItem->getFrame();
            if (!empty($frame['sql'])) {
                if (preg_match('@^(AND|OR)\s+@i', $frame['sql'])) {
                    $sqlItems[] = 'having ' . preg_replace('@^(AND|OR)\s+@i', '', $frame['sql']);
                } else {
                    $sqlItems[] = 'having ' . $frame['sql'];
                }
                if ($frame['args'] !== null && is_array($frame['args'])) {
                    $argItems = array_merge($argItems, $frame['args']);
                }
            }
        }
        //UNION
        if ($type == 2 || $type == 0) {
            if ($this->unionItem) {
                array_unshift($sqlItems, '(');
                $sqlItems[] = ')';
                foreach ($this->unionItem as $item) {
                    $temp = $item->selector->createSql(0);
                    if ($item->all) {
                        $sqlItems[] = 'union all ( ' . $temp['sql'] . ' )';
                    } else {
                        $sqlItems[] = 'union ( ' . $temp['sql'] . ' )';
                    }
                    if (!empty($temp['args'])) {
                        $argItems = array_merge($argItems, $temp['args']);
                    }
                }
            }
        }
        //ORDER BY
        if ($type != 2 && $this->orderItem != null) {
            $orderSql = $this->orderItem->sql;
            $ordeArgs = $this->orderItem->args;
            if (!empty($orderSql)) {
                $sqlItems[] = 'order ' . $orderSql;
            }
            if ($ordeArgs !== null && is_array($ordeArgs)) {
                $argItems = array_merge($argItems, $ordeArgs);
            }
        }
        //LIMIT
        if ($type != 2 && !empty($this->limit)) {
            $sqlItems[] = $this->limit;
        }
        if ($type == 1) {
            $alias = 'Zt';
            $sqlItems[] = ') Y where ' . $alias . '.id=Y.id';
            if ($this->orderItem != null) {
                $orderSql = $this->orderItem->sql;
                $ordeArgs = $this->orderItem->args;
                if (!empty($orderSql)) {
                    $orderSql = preg_replace_callback('@by\s+(`?\w+`?)\s+(desc|asc)@i', function ($math) use ($alias) {
                        return 'by ' . $alias . '.' . $math[1] . ' ' . $math[2];
                    }, $orderSql);
                    $sqlItems[] = 'order ' . $orderSql;
                }
                if ($ordeArgs !== null && is_array($ordeArgs)) {
                    $argItems = array_merge($argItems, $ordeArgs);
                }
            }
        }
        return ['sql' => join(' ', $sqlItems), 'args' => $argItems];
    }
创建sql
@param int $type
@return array