文字のバイト数とは?

システムによって、バイト数の制限があることがあります。
よくある例としては、画像ファイルは〇M byteまで など。

まず、言葉の定義から。
1バイトとは
0か1かを表すビットという単位が8つ並んで構成される。2の8乗で256通りのデータ

1KB、1MB、1GB
1キロバイト=1024バイト、1メガバイト=1024キロバイト、1ギガバイト=1024メガバイト
※1000バイト単位でないので注意が必要

半角のアルファベットと数字、1文字のデータ量は1バイト
– 日本語は漢字が数千あり、256種類では表現しきれないので、2バイト(16ビット)で1文字を表現する。いわゆる全角文字。2バイト(2^16)は65536種類の情報を表現できる。

500バイトだと250文字
WordのA4でフォントサイズ10.5で40文字程度

アルファベットは7ビットで表現
つまり1バイト?

PHPでバイト数を図ってみましょう。
strlen()でバイト数を計測

$single = "a";
$multi = "あ";
var_dump(strlen($single), strlen($multi));

あれ、”あ”が3byteになってる。何故だ!?

phpのマニュアルを見てみる。
http://php.net/manual/ja/function.strlen.php
うーん、やっぱりバイトだ。 あ、マルチバイト文字列はmb_strlen()でカウントするらしい。なるほど。つまり、”あ”は3バイトで表現されているってことか。

Mysqlのテーブルの文字コードを変更する

まずmysql のtableの文字コードを確認します。
show create table table_name
mysql> show create table items;
+——-+———————————————————————————————————————————————————————————–+
| Table | Create Table |
+——-+———————————————————————————————————————————————————————————–+
| items | CREATE TABLE `items` (
`item_id` int(11) NOT NULL AUTO_INCREMENT,
`name` text,
`price` int(11) DEFAULT NULL,
PRIMARY KEY (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+——-+———————————————————————————————————————————————————————————–+
1 row in set (0.06 sec)


utf-8なのがわかります。

[vagrant@localhost ~]$ cd /usr/share/mysql/charsets/
[vagrant@localhost charsets]$ ls
Index.xml cp1251.xml cp866.xml hp8.xml latin2.xml swe7.xml
README cp1256.xml dec8.xml keybcs2.xml latin5.xml
armscii8.xml cp1257.xml geostd8.xml koi8r.xml latin7.xml
ascii.xml cp850.xml greek.xml koi8u.xml macce.xml
cp1250.xml cp852.xml hebrew.xml latin1.xml macroman.xml
ん? なんだこれは?

とりあえず、テーブルの文字コードを変更します。
alter table table_name default character set utf8mb4;
mysql> alter table items default character set utf8mb4;
Query OK, 0 rows affected (0.16 sec)
Records: 0 Duplicates: 0 Warnings: 0

utf8mb4に変更されているのがわかります。
mysql> show create table items;
+——-+———————————————————————————————————————————————————————————————————+
| Table | Create Table |
+——-+———————————————————————————————————————————————————————————————————+
| items | CREATE TABLE `items` (
`item_id` int(11) NOT NULL AUTO_INCREMENT,
`name` text CHARACTER SET utf8,
`price` int(11) DEFAULT NULL,
PRIMARY KEY (`item_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+——-+———————————————————————————————————————————————————————————————————+
1 row in set (0.00 sec)

こちらは変更ありません。
mysql> show variables like “chara%”;
+————————–+—————————-+
| Variable_name | Value |
+————————–+—————————-+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+————————–+—————————-+
8 rows in set (0.00 sec)

mysql> describe items;
+———+———+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+———+———+——+—–+———+—————-+
| item_id | int(11) | NO | PRI | NULL | auto_increment |
| name | text | YES | | NULL | |
| price | int(11) | YES | | NULL | |
+———+———+——+—–+———+—————-+
3 rows in set (0.09 sec)

mysql> select * from items;
Empty set (0.07 sec)

mysql> insert into items (name) values (‘\U+1F363’);
Query OK, 1 row affected (0.06 sec)

mysql> select * from items;
+———+———+——-+
| item_id | name | price |
+———+———+——-+
| 1 | U+1F363 | NULL |
+———+———+——-+
1 row in set (0.00 sec)

あれ、なんか違くねー???

MySQLで文字コードを確認

メェー 今日は異常に疲れたぞ。。。無駄に気を張った。。明日が山場。終われば、今年もホボ終わりでござる。

ということで、MySQLで文字コードを確認したい。vagrantからmysqlにログインします。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 22
Server version: 5.6.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

show variables like ‘%char%’と打つ
mysql> show variables like ‘%char%’;
+————————–+—————————-+
| Variable_name | Value |
+————————–+—————————-+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+————————–+—————————-+
8 rows in set (0.52 sec)

character_set_client:クライアントがサーバにクエリを送信
character_set_connection:クライアントが送ったクエリをサーバが解析する際に使用
character_set_database:サーバのデフォルト
character_set_server:サービス起動時のデフォルト
character_set_system:ファイル名のcharset
character_sets_dir: charsetsのディレクトリ

う、限界だ。。

システム開発工程における注意点

システム開発における注意点!?
チームで開発するなら、気を付けておきたい箇所は一杯ありますよね。

– 要件定義
 – 仕様
  — 関係者で合意が取れているか
 – システム要件
  — 本当に必要な機能か
  — 運用負担とならないか
  — 運用が回るか

- 基本設計
 – 文字コードの配慮(UTF-8,ShiftJIS)
— データ長
 – AWSメンテナンスやサービスダウンを前提とした可用性/自動リトライなど
 – 運用フローを作成し、合意をとる

– リリース
 – お知らせ告知
— 外部システムへの配慮
 – 切り戻し
— 連絡体制

Larave5.7フォームリクエストのメッセージ設定

フォームリクエストとはなにか?公式のフォームリクエスト作成を見てみましょう。
https://readouble.com/laravel/5.7/ja/validation.html
フォームリクエストはバリデーションロジックを含んだカスタムリクエストクラス。フォームリクエストクラスを作成するには、make:request Artisan CLIコマンドを使用する。
生成されたクラスは、app/Http/Request ディレクトリへ設置される。

ふーん

とりあえず、やってみましょう。

1. make:requestでフォームリクエストクラスを作成
CompanyRequestを作ってみたいと思います。

[vagrant@localhost zeus]$ php artisan make:request CompanyRequest
Request created successfully.

app/Http のディレクトリに、Reqeust/CompanyReqeust.phpが生成されているのがわかります。

ファイルの中身を見てみると、authorizeとrulesメソッドが作られています。

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CompanyRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

2.生成されたクラス内のrules()の中身を編集
バリデーションを記述します。

public function rules()
    {
        return [
            'company_name' => 'required',
            'agent_name' => 'required',
        ];
    }

3.コントローラーでフォームリクエストを呼び込む
use App\Http\Requests\CompanyRequest; を追加
public function confirm(Request $request) を、public function confirm(CompanyRequest $request)に変更する

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Company;
use App\Agent_mst;
use App\Http\Requests\CompanyRequest;

class CompanyConfirmController extends Controller
{
    public function confirm(CompanyRequest $request){
    	// $validatedData = $request->validate([
     //        'company_name' => 'required',
     //        'agent_name' => 'required',
     //    ]);
    	$confirm = new Company($request->all());

    	return view('companyconfirm', compact('confirm'));
    }
}

フォームリクエスト側でバリデーションができるようになりました。

やったーーーー^^

laravel5.7のカスタムバリデーションでエラーメッセージを出す

まず公式ドキュメントのバリデーションを見てみましょう。
https://readouble.com/laravel/5.7/ja/validation.html

*.blade.phpを作ります。

@extends('layouts.account')
@section('title', '原稿管理会社登録')

@section('breadcrumb')
	@@parent
	<ul class="breadcrumb">
      <li itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" >
        <a href="/top" itemprop="url">
          <span itemprop="title">ホーム</span>
        </a>
      </li>
      <li itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb" >
        <a href="/top" itemprop="url">
          <span itemprop="title">原稿管理会社登録</span>
        </a>
      </li>
    </ul>
@endsection

@section('content')
<h2>原稿管理会社登録</h2>
      <hr>
      <form action="/company/confirm" method="post" id="form1">
      <table id="tbl">
        @csrf        
        <tr>
          <th>会社名</th><td><input type="text" name="company_name" size="40"  value="{{ old('company_name') }}">{{$errors->first('company_name')}}</td>
        </tr>
        <tr>
          <th>代理店</th><td><input type="text" name="agent_name" size="40"  value="{{ old('agent_name') }}">{{$errors->first('agent_name')}}</td>
        </tr>
      </table>      

      <div class="button_wrapper remodal-bg">
         <button type="submit" value="送信" id="square_btn" onClick="location.href='#modal'">登録</button>
      </div>
      </form>

      <!-- remodal -->
      <div class="remodal" data-remodal-id="modal">
        <button data-remodal-action="close" class="remodal-close"></button>
        <h1>登録しますか</h1>
        <p>
          入力した内容で宜しいでしょうか?
        </p>
        <br>
        <button data-remodal-action="cancel" class="remodal-cancel">Cancel</button>
        <button data-remodal-action="confirm" class="remodal-confirm">OK</button>
      </div>
@endsection

で、どうするか?
→ フォームリクエストを使う。

イベントドリブンで処理を行えるNetty

公式を覗いてみます。
https://netty.io/

Netty is an asynchronous event-driven network application framework
for rapid development of maintainable high performance protocol servers & clients. と記載があります。
asynchronous event-driven network とは、イベントドリブンということですが。。

It greatly simplifies and streamlines network programming such as TCP and UDP socket server.
TCPとUDPをシンプル化したと書いてあります。プロトコルサポートが充実しているのがわかります。

非同期通信というとajaxがまず思いつきますが、ajaxのようなフレームワークと考えてよいでしょうか?
うわあああああああああああああ、これは片手間では無理だ。しっかりjavaやらんと。とりあえず、表層を覗くだけにしておきます。

psコマンドの見方

psコマンドとは?

psコマンドは、Linux上で現在動作しているプロセスを表示するコマンド。
とりあえずコマンドラインで叩いてみよう。

[vagrant@localhost test]$ ps
PID TTY TIME CMD
15686 pts/3 00:00:00 bash
17690 pts/3 00:00:00 ps

PID:実行しているプロセスの番号
CMD:コマンド

Linuxのシンボリックリンクとは?

シンボリックリンク(symbolic link)って何?
イメージ的にはこんな感じ?

それはシンプルリングやねん。

シンボリックリンクとは、ファイルやフォルダの代理ファイルのことです。
よく分からないので、作ってみましょう。

[vagrant@localhost test]$ ls
test.php
[vagrant@localhost test]$ ln -s test.php ./test.php
ln: creating symbolic link `./test.php': ファイルが存在します

あれ!?
lnコマンドは、ディレクトリやファイルへのリンクを登録するコマンド
-sオプションは、シンボリックリンクを作成するオプション

ってことは、再度やり直します。
[vagrant@localhost test]$ ls
test.php
[vagrant@localhost test]$ ln -s test.php s.php
[vagrant@localhost test]$ ls
s.php test.php
[vagrant@localhost test]$

あれ、なんかそれっぽいのが出来ている!?

シンボリックリンクを叩く
なんでもいいですが、catコマンドを使ってみます。


[vagrant@localhost test]$ cat s.php
";
echo "addign git tag!";

?>

test.phpをcatしているのと同じ結果になりました。

やるわねー

Git tag(タグ)って何?

ねーねー、これGit Tagらしいよ。

あー知ってる、知ってる、Git tagね。(やべ、全く知らねー。。)

ということでGit tagを学びたいと思います。

まず、gitの公式を見ます。
Git タグ
公式ページの見出しを拾うと、色々な機能や種類があることがわかります。
タグの一覧表示、タグの作成、注釈付きのタグ、署名付きのタグ、軽量版のタグ、タグの検証、後からのタグ付け、タグの共有

まず、Githubのレポジトリにファイルを作成します。適当にtest.phpとしておきます。

vagrantにgit cloneします。

[vagrant@localhost test]$ git clone https://github.com/githubix/test.git
Initialized empty Git repository in /home/vagrant/local/test/test/.git/
remote: Enumerating objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 3
Unpacking objects: 100% (3/3), done.

1.git tagの一覧表示
コマンドラインでgit tagと打ちます。何も表示されません。タグはついていない状態です。

[vagrant@localhost test]$ git tag

git cloneしたファイルを適当に編集します。

echo "this is test<br>";
echo "addign git tag!";

2.git add .
[vagrant@localhost test]$ git add .

3.git commit -m “comment”
git commit します。
[vagrant@localhost test]$ git commit -m “tag commit”
[master a74bdba] tag commit
Committer: vagrant
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config –global user.name “Your Name”
git config –global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

git commit –amend –author=’Your Name

1 files changed, 2 insertions(+), 1 deletions(-)

4.git tag -a でタグ作成
git tag -a tagname -m “comment”でタグを生成します。
[vagrant@localhost test]$ git tag -a gittag -m “first tag”

5.git tagでタグ一覧表示
タグが生成されていることがわかります。
[vagrant@localhost test]$ git tag
gittag

6.リポジトリへpush
git remote set-url origin hogehogeとしてから、git pushします。
[vagrant@localhost test]$ git remote set-url origin https://githubix@github.com/githubix/test.git
[vagrant@localhost test]$ git push -u origin master
Password:
Counting objects: 5, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 291 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://githubix@github.com/githubix/test.git
e44f89c..a74bdba master -> master
Branch master set up to track remote branch master from origin.

7.tag も git push
[vagrant@localhost test]$ git push origin gittag
Password:
Counting objects: 1, done.
Writing objects: 100% (1/1), 161 bytes, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://githubix@github.com/githubix/test.git
* [new tag] gittag -> gittag

8.githubで確認
tagもpushされているのがわかります。

お疲れ様でしたー