DBIx::Customã®ããã¼ãã«ãã¨ãåãã®æ å ±ã調ã¹ãã¡ã½ãã
Perl
›
ã¢ã¸ã¥ã¼ã«
›
ãã¼ã¿ãã¼ã¹
›
DBIx::Custom
›
here
DBIx::Customã®ããã¼ãã«ãã¨ãåãã®æ å ±ã調ã¹ãã¡ã½ãããç´¹ä»ãã¾ãã
ãã¹ã¦ã®ãã¼ãã«ã®æ å ±ãé çªã«èª¿ã¹ã each_table
ãã¹ã¦ã®ãã¼ãã«ã®æ å ±ãé çªã«èª¿ã¹ãã«ã¯each_tableã¡ã½ããã使ç¨ãã¾ãã
$dbi->each_table( sub { my ($dbi, $table, $table_info) = @_; my $table_name = $table_info->{TABLE_NAME}; } );
ãã¼ã¿ãã¼ã¹ã«ãããã¹ã¦ã®ãã¼ãã«ã®æ å ±ãé çªã«ãã©ãã¾ããå¼æ°ã«ã¯ãã¼ãã«ãã¨ã«å®ãã¼ãã«ãããã³ã¼ã«ããã¯é¢æ°ãæå®ãã¾ããã³ã¼ã«ããã¯é¢æ°ã¯æ¬¡ã®3ã¤ã®å¼æ°ãåãåãã¾ãã
- DBIx::Customãªãã¸ã§ã¯ã
- ãã¼ãã«å
- ãã¼ãã«ã®æ å ±
each_tableã®é«éå
each_tableã¡ã½ããã¯é ãã§ããç¹ã«å¤§éã®ã·ã¹ãã ãã¼ãã«ããã¼ã¿ãã¼ã¹ã«åå¨ããå ´åã¯ãããªãé ãã§ãã
ããuser_table_infoãè¨å®ããã¦ããå ´åã¯ãeach_tableã¯ãã¼ãã«ã®æ å ±ã¨ãã¦ãããå©ç¨ãã¾ãã次ã®ããã«ãã¦each_tableãé«éåãããã¨ãã§ãã¾ãã
my $table_infos = $dbi->get_table_info(exclude => qr/^system_/); $dbi->user_table_info($table_info); $dbi->each_table(sub { ... });
ãã¼ãã«ã®æ å ±ãåå¾ãã get_table_info
ããã¼ãã«ã®æ å ±ãåå¾ããã«ã¯get_table_infoã¡ã½ããã使ç¨ãã¾ãã
my $table_infos = $dbi->get_table_info(exclude => qr/^system_/);
excludeãªãã·ã§ã³ã§ãé¤å¤ãããã¼ãã«åãæ£è¦è¡¨ç¾ã§æå®ãããã¨ãã§ãã¾ãã
ãã¼ã¿ãã¼ã¹ã®ãã¼ãã«ã®æ å ±ãè¨å®ãã - user_table_info
ãã¼ã¿ãã¼ã¹ã®ãã¼ãã«ã®æ å ±ãåå¾ããã«ã¯é常ã¯each_tableã¡ã½ãããå©ç¨ãã¾ãããã·ã¹ãã ãã¼ãã«ã大éã«ä¿æãã¦ãããã¼ã¿ãã¼ã¹ã®å ´åã¯ãã¢ã¯ã»ã¹ãããã³ã«ãã¼ãã«ã®æ å ±ã®åå¾ãè¡ãã¨é常ã«ä½éã«ãªãã¾ãã
user_table_infoã¡ã½ãã使ç¨ããã¨ããã¼ãã«æ å ±ãè¨å®ãã¦ããã¦ãeach_tableããã®ãã¼ãã«æ å ±ãå©ç¨ã§ããããã«ãªãã¾ãã
$dbi->user_table_info($user_table_info)
each_tableã¡ã½ããã¯é常ã«éããªãã¾ãã
è¨å®ããæ å ±ã®ãã¼ã¿æ§é ã¯ä»¥ä¸ã®ããã«ãªã£ã¦ãã¾ãã
[ {table => 'book', info => {...}}, {table => 'author', info => {...}} ]
é常ã¯get_table_infoã§ãããããåå¾ãã¦ãããæ å ±ãuser_table_infoã«è¨å®ãã¾ãã
my $user_table_info = $dbi->get_table_info(exclude => qr/^system/); $dbi->user_table_info($user_table_info);
åã®æ å ±ãåå¾ãã - get_column_info
åã®æ å ±ãåå¾ããã«ã¯get_column_infoã¡ã½ããã使ç¨ãã¾ãã
my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/);
exclude_tableãªãã·ã§ã³ã§ãé¤å¤ãããã¼ãã«åãæ£è¦è¡¨ç¾ã§æå®ãããã¨ãã§ãã¾ãã
ãã¹ã¦ã®åã®æ å ±ãé çªã«èª¿ã¹ã - each_column
ãã¹ã¦ã®åã®æ å ±ãé çªã«èª¿ã¹ãã«ã¯each_columnã¡ã½ããã使ç¨ãã¾ãã
$dbi->each_column( sub { my ($dbi, $table, $column, $column_info) = @_; my $type = $column_info->{TYPE_NAME}; if ($type eq 'DATE') { # ... } } );
ãã¼ã¿ãã¼ã¹ã«ãããã¹ã¦ã®åã®æ å ±ãé çªã«ãã©ãã¾ããå¼æ°ã«ã¯åãã¨ã«å®è¡ãããã³ã¼ã«ããã¯é¢æ°ãæå®ãã¾ããã³ã¼ã«ããã¯é¢æ°ã¯æ¬¡ã®4ã¤ã®å¼æ°ãåãåãã¾ãã
- DBIx::Customãªãã¸ã§ã¯ã
- ãã¼ãã«å
- åå
- åã®æ å ±
each_columnã®é«éå
each_columnã¡ã½ããã¯é ãã§ããç¹ã«å¤§éã®ã·ã¹ãã åããã¼ã¿ãã¼ã¹ã«åå¨ããå ´åã¯ãããªãé ãã§ãã
ããuser_column_infoãè¨å®ããã¦ããå ´åã¯ãeach_columnã¯åã®æ å ±ã¨ãã¦ãããå©ç¨ãã¾ãã次ã®ããã«ãã¦each_columnãé«éåãããã¨ãã§ãã¾ãã
my $column_infos = $dbi->get_column_info(exclude_table => qr/^system_/); $dbi->user_column_info($column_info); $dbi->each_column(sub { ... });
ãã®æ¹æ³ã§ãeach_columnãå é¨ã§å©ç¨ãã¦ãããsetup_modelã¨type_ruleãé«éåããã¾ãã
ãã¼ã¿ãã¼ã¹ã®åã®æ å ±ãè¨å®ãã - user_column_info
ãã¼ã¿ãã¼ã¹ã®åã®æ å ±ãåå¾ããã«ã¯é常ã¯each_columnã¡ã½ãããå©ç¨ãã¾ãããã·ã¹ãã ãã¼ãã«ã大éã«ä¿æãã¦ãããã¼ã¿ãã¼ã¹ã®å ´åã¯ãã¢ã¯ã»ã¹ãããã³ã«åã®æ å ±ãåå¾ããã¨é常ã«ä½éã«ãªãã¾ãã
user_column_infoã¡ã½ããã使ç¨ããã¨ãåæ å ±ãè¨å®ã§ããeach_columnããã®åæ å ±ãå©ç¨ã§ããããã«ãªãã¾ãã
$dbi->user_column_info($user_column_info)
each_columnã¡ã½ããã¯é常ã«é«éã«ãªãã¾ãããtype_ruleãsetup_modelã¨ãã£ãeach_columnãå é¨ã§å©ç¨ãã¦ããã¡ã½ãããéããªãã¾ãã
è¨å®ããæ å ±ã®ãã¼ã¿æ§é ã¯ä»¥ä¸ã®ããã«ãªã£ã¦ãã¾ãã
[ {table => 'book', column => 'title', info => {...}}, {table => 'author', column => 'name', info => {...}} ]
é常ã¯get_column_infoã§ãããããåå¾ãã¦ãããæ å ±ãuser_column_infoã«è¨å®ãã¾ãã
my $user_column_info = $dbi->get_column_info(exclude_table => qr/^system/); $dbi->user_column_info($user_column_info);
ãã¼ãã«ãæå®ãã¦ãã¹ã¦ã®åã®ã¿ã¤ãåã表示ãã show_typename
ãã¼ãã«ãæå®ãã¦ãã¹ã¦ã®åã®ã¿ã¤ãåã表示ããã«ã¯show_typenameã¡ã½ããã使ç¨ãã¾ãã
$dbi->show_typename($table);
ä¸è¡ç®ã«ã¯ãã¼ãã«åã2è¡ç®ä»¥éã¯ååã¨ã¿ã¤ãåã表示ããã¾ãã
book title: varchar issue_date: date
ãã®ã¿ã¤ãåã¯type_ruleã®into1ã¨into2ã§å©ç¨ãããã¨ãã§ãã¾ãã
æå®ãããã¼ãã«ã®ãã¹ã¦ã®åã®ãã¼ã¿ã¿ã¤ãã表示ãã - show_datatype
æå®ãããã¼ãã«ã®ãã¹ã¦ã®åã®ãã¼ã¿ã¿ã¤ãã表示ããã«ã¯show_datatypeã¡ã½ããã使ç¨ãã¾ãã
$dbi->show_datatype($table);
ä¸è¡ç®ã¯ãã¼ãã«åã2è¡ç®ä»¥éã¯ååã¨ãã¼ã¿ã¿ã¤ãã表示ããã¾ãã
book title: 5 issue_date: 91
ãã®ãã¼ã¿ã¿ã¤ãã¯type_ruleã®from1ã¨from2ã§å©ç¨ãããã¨ãã§ãã¾ãã
ã¿ã¤ãåã®ä¸è¦§ã®åå¾ available_typename
ãã¼ã¿ãã¼ã¹ã§å©ç¨ã§ããã¿ã¤ãåã®ä¸è¦§ãåå¾ããã«ã¯available_typenameã¡ã½ããã使ç¨ãã¾ãã
print $dbi->available_typename;
ã¿ã¤ãåã¯ããã¼ã¿ãã¼ã¹ã®ãã¼ãã«å®ç¾©ã®æã«æå®ããåã¨ã ãããä¸è´ãã¾ãããå®å ¨ã«ä¸è´ããããã§ã¯ãªãã®ã§æ³¨æãã¦ãã ããã
æ£ããã¿ã¤ãåãç¥ãã«ã¯show_typenameã¡ã½ããã使ç¨ãã¦ãã ããã
ãã¼ã¿ã¿ã¤ãã®ä¸è¦§ã®åå¾ available_datatype
ãã¼ã¿ãã¼ã¹ã§å©ç¨ã§ãããã¼ã¿ã¿ã¤ãã®ä¸è¦§ãåå¾ããã«ã¯available_datatypeã¡ã½ããã使ç¨ãã¾ãã
print $dbi->available_datatype;
ãã®ã¡ã½ããã§åæããããã¼ã¿ã¿ã¤ãã¯ä»¥ä¸ã®ãããªãã¸ãã¯ã§åå¾ããããã®ã§ãã
for my $i (-1000 .. 1000) { $dbh->type_info($i); my $data_type = $type_info->{DATA_TYPE}; }
ã¹ãã¼ãã¡ã³ããã³ãã«ã®TYPEå±æ§ã§åå¾ã§ããå¤ã¨ä¼¼ã¦ãã¾ãããã¾ã£ããä¸è´ããããã§ã¯ãããã§ãã®ã§æ³¨æãã¦ãã ãããã¾ãSQLiteã§ã¯ãä½ãåæããã¾ããã
$sth->{TYPE}
æ£ãããã¼ã¿ã¿ã¤ããç¥ãã«ã¯show_datatypeã¡ã½ãããå©ç¨ãã¦ãã ããã
PostgreSQLã§å©ç¨å¯è½ãªãã¼ã¿ã¿ã¤ã
DBIã§åå¾ã§ããPostgreSQLã§å©ç¨å¯è½ãªDataTypeã§ãã
Data Type (Type name)
-3 (bytea)
0 (unknown)
1 (bpchar)
2 (numeric)
3 (numeric)
4 (int4)
5 (int2)
6 (float4)
7 (float8)
8 (int8)
9 (date) # ããã¯ééã£ã¦ããã®ãã
10 (tinterval)
11 (timestamp)
12 (text)
16 (bool)
50 (array)
91 (date) # dateã¯ãããæ£ããã¨ãããã¾ãã
92 (time)
93 (timestamp)
94 (timetz)
95 (timestamptz)
Microsoft SQL Server 2008 R2ã§å©ç¨å¯è½ãªãã¼ã¿ã¿ã¤ã
DBIã§åå¾ã§ããMicrosoft SQL Server 2008 R2ãè¿ããã¼ã¿ã¿ã¤ãã®ä¸è¦§ã§ããavailable_datatypeã®å¤ãDBD::ODBCçµç±ã§ã¢ã¯ã»ã¹ããã¨ãã®ãã®ã§ãã
Data Type (Type name) -150 (sql_variant) -11 (uniqueidentifier) -10 (ntext) -9 (nvarchar) -8 (nchar) -7 (bit) -6 (tinyint) -5 (bigint) -4 (image) -3 (varbinary) -2 (binary) -1 (text) -150 (sql_variant) 1 (char) 2 (numeric) 3 (decimal) 4 (int) 5 (smallint) 6 (float) 7 (real) 12 (varchar) 93 (datetime)
dateåãè¦å½ããã¾ããããSQL Serverã§ã¯Dateåã¯ãªãã¨ã-9(nvarchar)ã§è¿ã£ã¦ããããã§ããdateåã®å ´åã¯SQLãµã¼ãã¼ã®å ´åã¯ãèªåå¤å®ãã§ããªãããã§ãã
æ¤ç´¢ãããã¼ãã«ããç¹å®ã®ãã¼ãã«ãé¤å¤ãã - exclude_table
DBIx::Customã«ã¯ãã¼ã¿ãã¼ã¹ã®ãã¼ãã«ãæ¤ç´¢ããã¡ã½ãããããã¾ããç¹å®ã®ãã¼ãã«ãæ¤ç´¢å¯¾è±¡ããé¤å¤ãããå ´åã¯exclude_tableãå©ç¨ãã¾ãã
$dbi->exclude_table(qr/pg_/);
å¤ã«ã¯æ£è¦è¡¨ç¾ã®ãªãã¡ã¬ã³ã¹ãæå®ãã¾ãã
each_column, each_table, type_rule, setup_modelã¨ããåã¤ã®ã¡ã½ããã¯ããã¼ã¿ãã¼ã¹ã®ãã¼ãã«æ å ±ãåå¾ãã«ããã¾ãããexclude_tableã§æå®ãããæ£è¦è¡¨ç¾ã«ããããããã¼ãã«ãæ¤ç´¢å¯¾è±¡ããé¤å¤ãã¾ãã
Microsoft SQL ServerãOracleã¯å é¨ã«å¤§éã®ã·ã¹ãã ãã¼ãã«ãä¿æãã¦ããã®ã§ããã¾ãè¨å®ããã°ããã©ã¼ãã³ã¹ããããªãã¾ãã