public static function makeInserts($tbl, $rows, array $fields = [], $implode = ";\n", $query_max_len = 524288): array
    {
        if ($query_max_len > 1048575) {
            throw new InvalidArgumentException('Each query should be shorter than 1MB.');
        }

        if ($fields) {
            foreach ($fields as & $v) {
                $v = '`' . $v . '`';
            }
            unset($v);
            $fields = ' (' . implode(', ', $fields) . ')';
        } else {
            $fields = '';
        }

        $tmp = $res = [];
        $tmp_len = 0;

        foreach ($rows as $line) {
            foreach ($line as & $v) {
                $v = "'" . self::sql_prepare($v) . "'";
            }
            unset($v);

            $q = '(' . implode(', ', $line) . ')';
            $tmp[] = $q;
            $tmp_len += \strlen($q);

            // Make all inserts in multiple queries
            if ($tmp_len >= $query_max_len) {
                $res[] = 'INSERT INTO `' . $tbl . '`' . $fields . ' VALUES ' . implode(', ', $tmp);
                $tmp = [];
                $tmp_len = 0;
            }
        }

        if ($tmp) {
            $res[] = 'INSERT INTO `' . $tbl . '`' . $fields . ' VALUES ' . implode(', ', $tmp);
        }

        return $implode ? implode($implode, $res) : $res;
    }