TRY AND ERROR

気になったこと、勉強したこと、その他雑記など色々メモしていきます。。Sometimes these posts will be written in English.,

Behavior of "BETWEEN" operator for datetime in MySQL.

I had a big misunderstanding to the "BETWEEN" operator respect to datetime columns in MySQL.

I had thought that "BETWEEN" does not contain the value of end when the date value is used respect to datetime columns like this.

SELECT * FROM sample WHERE some_datetime_col BETWEEN "2017-01-01" AND "2017-02-01"

In Above case, I had thought that the records that has "2017-02-01 xx:xx:xx" value in its some_datetime_col wouldn't be fetched.

But it was misunderstanding.
Actually in this case, the record that has "2017-02-01 00:00:00" is fetched.

The truth, "BETWEEN" definitely contains the start and end values. And also if the date values are as used respect to datetime columns, its time part is filled of "00:00:00". So that about continuous datetime records like "2017-02-01 00:00:00", "2017-02-01 00:00:01", ...,"2017-02-01 23:59:59" to be considered, only "2017-02-01 00:00:00" record will be unexpectedly fetched in these range.

The Point of this post is that when using date values to datetime columns, date is forcely casted to datetime with appending 00:00:00.

Python-boto3でAWSのEC2インスタンスを立ち上げ、そのPublicIPを取得する


boto3とawscliを入れておく

$pip install boto3
$pip install awscli

AWSのCredentialsなどをセットする

$ aws configure
AWS Access Key ID [None]: アクセスキー
AWS Secret Access Key [None]: アクセスキーシークレット
Default region name [None]: regionコード
Default output format [None]: json


homeの.awsに設定情報が格納される。boto3ではこれを読んで認証するので、毎回Credentialsをセットする必要はない。

~/.aws/config
~/.aws/credentials

ここからが本題です。
実際にboto3を使ってEC2インスタンスを立ち上げ、立ち上げたインスタンスのPublicIPを取得してみる。

import boto3

ec2 = boto3.resource('ec2')


# Terminateしたときにボリュームも一緒に消したい場合、その設定などをかく
blockDeviceMappings = [{
    "DeviceName": "/dev/sda1",
    "Ebs": {
        "DeleteOnTermination": True,
        "VolumeType": "gp2"
    },
}]

# インスタンス生成
ret = ec2.create_instances(
                            ImageId="イメージID",
                            MinCount=1,
                            MaxCount=1,
                            InstanceType="インスタンスの種類(t2.microなど)",
                            KeyName="キーペアの名前",
                            SecurityGroupIds=["セキュリティグループID"],
                            BlockDeviceMappings=blockDeviceMappings
                            )

# 生成したインスタンスIDを取得
createdInstanceId = ret[0].instance_id
createdInstance = ec2.Instance(createdInstanceId)
pprint(createdInstance.public_ip_address) # None....

インスタンス生成後すぐにはPublicIPなどは取得できないので、running状態になるまで待つ。

# インスタンス生成
ret = ec2.create_instances(
                            ImageId="イメージID",
                            MinCount=1,
                            MaxCount=1,
                            InstanceType="インスタンスの種類(t2.microなど)",
                            KeyName="キーペアの名前",
                            SecurityGroupIds=["セキュリティグループID"],
                            BlockDeviceMappings=blockDeviceMappings
                            )

# 生成したインスタンスIDを取得
createdInstanceId = ret[0].instance_id

# PublicIPを取得するため、EC2インスタンスがrunning状態になるまで待つ
ret[0].wait_until_running()

# waitingをかけた後に再度インスタンスを取得
createdInstance = ec2.Instance(createdInstanceId)

# ここでPublicIPがとれる。
pprint(createdInstance.public_ip_address)

wait_until_runningでrunningになるまで待つことで、PublicIPが取得できました。



boto3を使ったEC2用のプロトタイプみたいなものを作ったので、よかったら参考にどうぞ。
github.com

ChatworkAPIがバージョンアップしました。

ChatworkAPIがv1からv2にバージョンアップしました。

help.chatwork.com


ユーザー数が増えてmessage_idの桁が足りなくなったみたいな感じか??
対応としては、基本エンドポイントを変更するくらいです。

以前アップしたChatworkのAPIラッパーについてはは修正済みです。

github.com
github.com
github.com

How to check if index is exist in Array or Slice in Golang?

I guess in Golang, there isn't any method to check that index is exist in Slice, for PHP like "isset()".
So I take a method I made like below.

func isset(arr []string, index int) bool {
    return (len(arr) > index)
}

By the way, in Golang when you check that index is exist in array and then also check that value of index of array is not blank"", it works like below.

// Assume "len(arr) == 2"  in this time
if (isset(arr, 3) && arr[3] != "") {
    // Any code.
}

Though the right side divided with "&&" looks likely to throw the error "out of range", the left side is false that's why the right side is ignored in Golang.


You have any ideas about "isset()"??

In CakePHP3, change just a specific table class's datasource.

If you want to get the Table objects to access to DB without appalently using ConnectionManager, I think you use the "TableRegistry" like following.

TableRegistry::get("Yours");


Above then, "default" datasource in app.php is chosen.
If you use the datasource of not default, you set "defaultConnectionName" function to Model/Table/YoursTable.php

public static function defaultConnectionName() {
    return 'your datasource key';   // Defined at Datasources in app.php.
}

Just only it!

google-api-php-clientを使ったOAuth2認証で、セッションを使わずにコールバックを経由しても保持される任意のパラメータをセットする

OAuth2認証において、

Googleに認証リクエスト → Google認証画面にてアプリ許可 → コールバック画面に遷移

という流れの中で、コールバックをもらった際にどのユーザー(アプリ側の)で認証したかを持ち回る必要があった。


アプリ側で必要となる任意のパラメータをコールバック時にGoogleから返してもらいたいときは、
認証urlのリクエストパラメータ「state」をセットする。

$client = new Google_Client();

// 中略....

$params = ['usr_id' => 'xxxx'];
$client->setState(json_encode($params));
$client->authRequest();

こうすると、認証時に指定した任意のパラメータをコールバック画面で受け取れる。

$usr_id = json_decode($_GET["state"],true)["usr_id"] ?? "";

In the HTML5, "readonly" attribute just works for text fields.

In the HTML5, "readonly" attribute just works for text fields like <input type="text"> or <textarea>...
I didn't know about that!

For example, in the case of to put an select tag.

Like this, it doesn't work as you think. It is editable.

<select readonly>
    <option value="v1">v1</option>
    <option value="v2">v2</option>
    <option value="v3">v3</option>
</select>


If you set a "disabled" attribute Instead of "readonly", It seems to be correct but you cannot receive this in server side when you submitted. So it doesn't work too.

<select disabled>
    <option value="v1">v1</option>
    <option selected value="v2">v2</option>
    <option value="v3">v3</option>
</select>


For result, If you want to make a not editable "select" tag, you should set a "disabled" attribute to child items but the selected one.
This works as not editable and could receive in server side.

<select>
    <option disabled value="v1">v1</option>
    <option selected value="v2">v2</option>
    <option disabled value="v3">v3</option>
</select>


Hmm...I feel It is a little technically.