File Access

file_get_contents()
Want to read the whole file

readfile()
Want to output the entire contents of the file

file()
want to read line-by-line text file as an array

fopen() + fread()
Want to read the file byte by byte

SplFileObject class
want to read CSV

SplFileObject class
want to operate object-oriented

サンプルのjsonデータを使ってみます。

{
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}
$json = file_get_contents(__DIR__ . '/sample.json');
if($json === false){
	echo ('file not found.');
} else {
	$data = json_decode($json, true);
	var_dump($data);
}

file_get_contents() は、使用頻度高いです。

statement handle

A statement handle is a data object that describes on SQL statement and tracks its execution. You can execute a statement only by allocating a statement handle.

SQLAllocHandle() (with HandleType set to SQL_HANDLE_STMT) allocates a statement handle to describe an SQL statement. Descriptions of SQL statements include information such as statement attributes, SQL statement text, dynamic parameters, cursor information, dynamic arguments and column bindings, result values, and status information. Each statement handle associates a connection with statement that the handle describes.

What is bind?

Bind variables are variables used in SQL statements, which are languages that perform writing and query to a database.

When making similar processing requests repeatedly while changing only a certain number to the database, if you re-set the number of the SQL statement each time, many types of SQL statements will be processed, which is inefficient. In such a case, if the bind variable is used, the database recognizes that “the same SQL statement is repeated processed under another condition”, so that the processing time can be efficiently improved and the processing time can be shortened.

In the case of Oracle Database, bind variables are represented by adding a colon(:) to the beginning of the variable name(e.g :number)

$sql = ‘select name, breed, weight from animals where weight > ? and weight < ?'; $conn = db2_connect($database, $user, $password); $stmt = db2_prepare($conn, $sql); $lower_limit = 1; db2_bind_param($stmt, 1, "lower_limit", DB2_PARAM_IN); db2_bind_param($stmt, 2, "upper_limit", DB2_PARAM_IN); $upper_limit = 15.0; if(db2_execute($stmt)){ while ($row = db2_fetch_array($stmt)){ print "{$row[0]}, {$row[1]}, {$row[2]}\n"; } } [/php]

Database Handles

Database handle are fully encapsulated objects. Transactions from one database handle do not cross over or leak with one another.

$rc = $dbh->do($statement) || die $dbh->errstr;
$rc = $dbh->do($statement, \%attr) ||die $dbh->errstr;

$rv = $dbh->do($statement, \%attr,@bind_values) || ...

my $rows =$dbh->do(q{
	DELETE FROM table
},undef,'DONE') || die $dbh->errstr;

$art_ref =$dbh->selectall_arrayref($statement);
$ary_ref = $dbh->selectall_arrayref($statement, \%attr);
$ary_ref = $dbh->selectall_arrayref($statement, \%attr, @bind_values);

$sth = $dbh->prepare($statement) || die $dbh->errstr;
$sth = $dbh->prepare($statement, \%attr) ||die $dbh->errstr;

my $sth =$dbh->@prepare("SELECT * from mytable where name like ?");
$sth->bind_param(1, 'J%');

$sth = $dbh->prepare_cached($statement) || die $dbh->errstr;
$sth = $dbh->prepare_cached($statement, \%attr) || die $dbh->errstr;

すげー、相当頑張らねーと。。。

モダンな開発環境とは何か?

とにかく今のプロジェクトのクオリティを上げることが最優先、と考えていたが、思っていたより直ぐに「モダンな開発環境とは何か?」という問いに直面してしまった。

自分なりに整理してみる。
まず、技術トレンドに乗っている
これが大事では。レガシーなシステムは切り離して、トレンドを忠実に追うこと。そのためには、まずそういう環境に身を置くことが大事。ちゃんと技術トレンドに沿っているエンジニアと一緒に仕事をしないといけない。
– 言語はpython
– クラウドはaws
– mac

積み重ねだと思うが、結構大事かも。

次に、、、
積極的に新しい技術を学んでいけ
やっぱりこれは大事なんじゃないか。。と思い始めた。

結果的にそれが価値に繋がるのでは。

最近思うのは、競争相手は中国やインドなどのエンジニアだということ。働き方改革など悠著な事を言っている場合ではありませんね。

Window function

The window function performs calculations across a set of table rows that are somehow related to the current row. This is similar to the form of calculation performed by aggregate functions. However, unlike regular aggregation functions, the use of window functions does not group rows into a single output row. Each row maintains its own identity.

Windows functions perform aggregation processing(ranking, etc) on acquired(SELECT) data. It can be said that it is an essential function in the recent trend of big data and OLAP.

SELECT
  Start_station_name,
  Duration,
  SUM(Duration) OVER (PARTITION BY Start_station_name ORDER BY Start_time) AS running_total
 FROM
  trip_histories
 WHERE
  Start_time < '2019-01-30'
 LIMIT 10;

SELECT
 Start_station_name,
 Duration,
 SUM(Duration) OVER (PARTITION BY Start_station_name) AS running_total,
 COUNT(Duration) OVER (PARTITION BY Start_station_name) AS running_count,
 AVG(Duration) OVER (PARTITION BY Start_station_name) AS running_avg
FROM
 trip_histories
WHERE
 Start_time < '2019-04-30'
LIMIT 10;

SELECT
 ROW_NUMBER() OVER (order by Start_time DESC) AS row_num,
 Start_station_name,
 Start_time,
 Duration
FROM
 trip_histories
WHERE
 Start_time < '2019-04-30'
LIMIT 10;

なるほどー、これは数こなさないとな~

What is CTE?

What is CTE?
– CTE is a so-called “recursive query”
– The WOTH clause has become available
 → Implemented on other RDMS(PostgreSQL, Oracle etc)
– Hierarchical queries can be written very easily
 → In the past, it had to be nested using subueries.

WITH RECURSIVE Hatoyama AS
(
 select id, last_name, first_name from family
   where last_name= "鳩山" and first_name="和夫"
 union all
   select child.id, child.last_name, child.first_name
   from family as child, Hatoyama
    where Hatoyama.id = child.parent_id
)
select * from Hatoyama;

WITH Yoshida as
(
	select id, last_name, first_name from family
	  where last_name="吉田" and parent id is null
	union all
	  select child.id, child.last_name, child.first_name
	  from family as child, Yoshida
	  where yoshida.id= child.parent_id
)
select * from Yoshida;

select文が階層化しないようにできるわけね。

apache 2.4.39 ~apacheのバージョンって何が違うの?

[vagrant@localhost ~]$ httpd -v
Server version: Apache/2.2.15 (Unix)
Server built: Jun 19 2018 15:45:13
[vagrant@localhost ~]$ apachectl -v
Server version: Apache/2.2.15 (Unix)
Server built: Jun 19 2018 15:45:13

The Apache HTTP Server Project has released “Apache HTTP Server 2.4.39” that addresses six vulnerabilities. This update fixes six vulnerabilities, including CVE-2019-0211, which may allow code execution with higher privileges.

In addition to “CVE-2019-0211”, “CVE-2019-0217” may be authenticated by another user in Digest authentication, and “CVE-2019-2015” may bypass access control by the client certificate. As for “,” the vulnerability is rated as “Important”, which is the second of four in the rating.

In addition, the three vulnerabilities with the lowest “Low” severity of vulnerability listed as “Low” fix “CVE-2019-0196”, “CVE-2019-0197”, “CVE-2019-0220” did.

なるほど、脆弱性に対応してるのね。

About significant changes in PHP 7.3

The following changes in PHP 7.3

– Flexible Heredoc syntax and Nowdoc syntax
– End comma can be used in function call JSON_THROW_ON_ERROR
– Can use reference passing with list () is_countable function
– array_key_first(), array_key_last()
– Argon2 Password Hash Enhancement

Flexible description of heredoc/nowdoc statement
You can now indent the end markers and the text you insert.

if(TRUE){
	print<<<EOS
	foo
	EOS;
}
&#91;/php&#93;

The indent of the string is determined relative to the end marker's position(indent). This allows you to easily control the number of spaces inserted etc. Note that mixing blanks and tabs in a string is not permitted, as it is controlled by indenting, and an error will occur.

<b>mbstring case conversion is enhanced</b>
The mbstring extension was originally developed to handle multi-byte strings, but it is actually used by many PHP programmers, including single-byte users, as extensions for string processing. As of PHP 7.3, upper and lower-case conversion mainly used in single-byte range is enhanced.


echo mb_strtoupper("Straße");

Allow commas to be inserted at the end of function arguments
Previously, in PHP 7.2 and earlier versions, although it was possible to write a comma at the end of the last element of the array, it was not possible to insert a comma at the end of function arguments. For example, when creating a function definition automatically by a script, putting a comma at the end may make writing code easier. In such cases, new features are useful.

$a = [1,2];
$b = [3,4];
$c = array_merge($a,$b,[5,6],);

is_countable(), array_key_*() function added
As of PHP 7.3, the is_countable() function has been added to check for countable functions. It can be used for purposes such as checking the functions that can be specified in the count() function.

if(is_countable($foo)){
	//$fooはカウント可
}

$a = ['one'=>'First','two'=>'Second','three'=>'Thred'];
echo array_key_first($a); // one
echo array_key_last($a); // three

Support for Same-site Cookie
A PHP session supports the Same-site Cookie(suggested as RFC 6265) that is said to be effective for cross-site request forgery(CSRF) protection as a security related function imrovement. When the SameSite attribute is specified, corss-site requests are limited and supported by major we browsers such as Google Chrome.

perl undef

Undefined values can be set using the undef function. Alternatively, the value of the variable specified in the argument is undefined.

#!/usr/bin/perl --

use strict;
use warnings;

print "Content-type:text/html\n\n";
my $name = "Kimoto";
$name = undef;

if (defined $name){
	print "defined";
} else {
	print "Undef";
}

なるほどー