=encoding euc-jp
=pod
=head1 NAME
=begin original
DBD::SQLite - Self-contained RDBMS in a DBI Driver
=end original
DBD::SQLite - DBI ドライバでの自己完結型(Self Contained)RDBMS
(訳注: (TBR)がついている段落は「みんなの自動翻訳@TexTra」による
機械翻訳です。)
=head1 SYNOPSIS
use DBI;
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
=head1 DESCRIPTION
=begin original
SQLite is a public domain file-based relational database engine that
you can find at L.
=end original
SQLite は、パブリックドメインのファイルベースの関係データベースエンジンで、
この URL で見つけることが出来ます: L
=begin original
B is a Perl DBI driver for SQLite, that includes
the entire thing in the distribution.
So in order to get a fast transaction capable RDBMS working for your
perl project you simply have to install this module, and B
else.
=end original
B は、全てのものをディストリビューションに含んでいる、
SQLite のための Perl DBI ドライバです。
そのためあなたの perl プロジェクトのために、高速の
トランザクション可能な RDBMS を動かすためには、単にこのモジュールを
インストールすることだけが必要です。
他には B<何もありません>。
=begin original
SQLite supports the following features:
=end original
SQLiteは以下の機能をサポートしています:
=over 4
=item Implements a large subset of SQL92
(SQL92 のサブセットの多くを実装)
=begin original
See L for details.
=end original
詳細は L をご覧ください。
=item A complete DB in a single disk file
(単一のディスクファイルでの完全な DB)
=begin original
Everything for your database is stored in a single disk file, making it
easier to move things around than with L.
=end original
あなたのデータベースのための全ては一つのディスクファイルに格納されます。
これにより L よりも簡単に動かすことが出来ます。
=item Atomic commit and rollback
(アトミックなコミットとロールバック)
=begin original
Yes, B is small and light, but it supports full transactions!
=end original
そうです、B は小さく、軽量ですが、
完全なトランザクションをサポートします。
=item Extensible
=begin original
User-defined aggregate or regular functions can be registered with the
SQL parser.
=end original
ユーザー定義の集計関数または標準関数は、SQLパーサーに登録できます。
(TBR)
=back
=begin original
There's lots more to it, so please refer to the docs on the SQLite web
page, listed above, for SQL details. Also refer to L for details
on how to use DBI itself. The API works like every DBI module does.
However, currently many statement attributes are not implemented or
are limited by the typeless nature of the SQLite database.
=end original
これ以上のものがありますが、
そのため SQL の詳細については、上に挙げた SQLite web ページにある
ドキュメントを参照してください。
また、DBI 自身の使い方に関する詳細については
L を参照してください。
API はほぼ各 DBI モジュールがするように機能します。
しかし、現在の所多くの文属性は、未実装であったり、SQLite データベースには
型がないという性質から制限されています。
=head1 NOTABLE DIFFERENCES FROM OTHER DRIVERS
=head2 Database Name Is A File Name
=begin original
SQLite creates a file per a database. You should pass the C of
the database file (with or without a parent directory) in the DBI
connection string (as a database C):
=end original
SQLiteでは、データベースごとにファイルが作成されます。
データベースファイルのC(親ディレクトリの有無にかかわらず)をDBI接続文字列に(データベースCとして)渡す必要があります:
(TBR)
my $dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","");
=begin original
The file is opened in read/write mode, and will be created if
it does not exist yet.
=end original
ファイルは読み取り/書き込みモードで開かれ、まだ存在しない場合は作成されます。
(TBR)
=begin original
Although the database is stored in a single file, the directory
containing the database file must be writable by SQLite because the
library will create several temporary files there.
=end original
データベースは1つのファイルに格納されますが、ライブラリによって複数の一時ファイルが作成されるため、データベースファイルを含むディレクトリはSQLiteによって書き込み可能である必要があります。
(TBR)
=begin original
If the filename C<$dbfile> is ":memory:", then a private, temporary
in-memory database is created for the connection. This in-memory
database will vanish when the database connection is closed.
It is handy for your library tests.
=end original
ファイル名C<$dbfile>が":memory:"の場合、接続用にプライベートな一時的なメモリー内データベースが作成されます。
このメモリー内データベースは、データベース接続が閉じられると消去されます。
これはライブラリのテストに便利です。
(TBR)
=begin original
Note that future versions of SQLite might make use of additional
special filenames that begin with the ":" character. It is recommended
that when a database filename actually does begin with a ":" character
you should prefix the filename with a pathname such as "./" to avoid
ambiguity.
=end original
SQLiteの将来のバージョンでは、「:」文字で始まる追加の特殊なファイル名が使用される可能性があることに注意してください。
データベースファイル名が実際に「:」文字で始まる場合は、あいまいさを避けるために、ファイル名の前に「./」などのパス名を付けることをお薦めします。
(TBR)
=begin original
If the filename C<$dbfile> is an empty string, then a private,
temporary on-disk database will be created. This private database will
be automatically deleted as soon as the database connection is closed.
=end original
ファイル名C<$dbfile>が空の文字列の場合、プライベートな一時的なオンディスクデータベースが作成されます。
このプライベートデータベースは、データベース接続が閉じられるとすぐに自動的に削除されます。
(TBR)
=head2 Accessing A Database With Other Tools
=begin original
To access the database from the command line, try using C
which comes with the L module. Just type:
=end original
コマンドラインからデータベースにアクセスするためには、L モジュールに
入っている C を試してみてください。
以下のようにタイプして:
dbish dbi:SQLite:foo.db
=begin original
On the command line to access the file F.
=end original
コマンドラインでファイル F にアクセスします。
=begin original
Alternatively you can install SQLite from the link above without
conflicting with B and use the supplied C
command line tool.
=end original
代わりとして、上記のリンクから B にぶつかることなく SQLite を
インストールし、提供される C コマンドラインツールを使うことが
出来ます。
=head2 Blobs
=begin original
As of version 1.11, blobs should "just work" in SQLite as text columns.
However this will cause the data to be treated as a string, so SQL
statements such as length(x) will return the length of the column as a NUL
terminated string, rather than the size of the blob in bytes. In order to
store natively as a BLOB use the following code:
=end original
バージョン1.11では、blobはSQLiteではテキスト列として「動作する」はずです。
ただし、これによりデータが文字列として扱われるため、length(x)などのSQL文は、バイト単位のblobのサイズではなく、NULで終了する文字列として列の長さを返します。
BLOBとしてネイティブに格納するには、次のコードを使用します:
(TBR)
use DBI qw(:sql_types);
my $dbh = DBI->connect("dbi:SQLite:dbfile","","");
my $blob = `cat foo.jpg`;
my $sth = $dbh->prepare("INSERT INTO mytable VALUES (1, ?)");
$sth->bind_param(1, $blob, SQL_BLOB);
$sth->execute();
=begin original
And then retrieval just works:
=end original
そして検索は機能します
(TBR)
$sth = $dbh->prepare("SELECT * FROM mytable WHERE id = 1");
$sth->execute();
my $row = $sth->fetch;
my $blobo = $row->[1];
# now $blobo == $blob
=head2 Functions And Bind Parameters
=begin original
As of this writing, a SQL that compares a return value of a function
with a numeric bind value like this doesn't work as you might expect.
=end original
この記事を書いている時点では、関数の戻り値を数値バインド値と比較するSQLは、期待したようには動作しません。
(TBR)
my $sth = $dbh->prepare(q{
SELECT bar FROM foo GROUP BY bar HAVING count(*) > ?;
});
$sth->execute(5);
=begin original
This is because DBD::SQLite assumes that all the bind values are text
(and should be quoted) by default. Thus the above statement becomes
like this while executing:
=end original
これは、DBD::SQLiteでは、デフォルトですべてのバインド値がテキストである(引用符で囲む必要がある)と想定されているためです。
そのため、上記のステートメントを実行すると、次のようになります。
(TBR)
SELECT bar FROM foo GROUP BY bar HAVING count(*) > "5";
=begin original
There are two workarounds for this.
=end original
これには、2つの回避策があります。
(TBR)
=over 4
=item Use bind_param() explicitly
=begin original
As shown above in the C section, you can always use
C to tell the type of a bind value.
=end original
上記のCセクションで示したように、Cを使用してバインド値のタイプを指定できます。
(TBR)
use DBI qw(:sql_types); # Don't forget this
my $sth = $dbh->prepare(q{
SELECT bar FROM foo GROUP BY bar HAVING count(*) > ?;
});
$sth->bind_param(1, 5, SQL_INTEGER);
$sth->execute();
=item Add zero to make it a number
=begin original
This is somewhat weird, but works anyway.
=end original
これは少し奇妙ですが、とにかく機能します。
(TBR)
my $sth = $dbh->prepare(q{
SELECT bar FROM foo GROUP BY bar HAVING count(*) > (? + 0);
});
$sth->execute(5);
=back
=head2 Foreign Keys
=begin original
B
=end original
B<覚悟しろ!オオカミ接近!>
(TBR)
=begin original
SQLite has started supporting foreign key constraints since 3.6.19
(released on Oct 14, 2009; bundled with DBD::SQLite 1.26_05).
To be exact, SQLite has long been able to parse a schema with foreign
keys, but the constraints has not been enforced. Now you can issue
a pragma actually to enable this feature and enforce the constraints.
=end original
SQLiteは3.6.19(2009年10月14日にリリースされ、DBD::SQLite 1.26_05にバンドルされている)から外部キー制約のサポートを開始した。
正確には、SQLiteは長い間外部キーを使用してスキーマを解析することができたが、制約は強制されていなかった。
これで、プラグマを実際に発行してこの機能を有効にし、制約を強制することができる。
(TBR)
=begin original
To do this, issue the following pragma (see below), preferably as
soon as you connect to a database and you're not in a transaction:
=end original
これを行うには、次のプラグマ(下記参照)を発行します。
トランザクション中ではなく、データベースに接続した直後に発行することをお勧めします。
(TBR)
$dbh->do("PRAGMA foreign_keys = ON");
=begin original
And you can explicitly disable the feature whenever you like by
turning the pragma off:
=end original
また、プラグマをオフにすることで、いつでもこの機能を明示的に無効にすることができます。
(TBR)
$dbh->do("PRAGMA foreign_keys = OFF");
=begin original
As of this writing, this feature is disabled by default by the
sqlite team, and by us, to secure backward compatibility, as
this feature may break your applications, and actually broke
some for us. If you have used a schema with foreign key constraints
but haven't cared them much and supposed they're always ignored for
SQLite, be prepared, and B. It is very likely that the sqlite
team will turn it default-on in the future, and we plan to do it
NO LATER THAN they do so.
=end original
この記事を書いている時点では、SQLiteチームと私たちは、後方互換性を確保するために、この機能をデフォルトで無効にしています。
これは、この機能があなたのアプリケーションを破壊する可能性があり、実際に私たちのためにいくつか破壊したからです。
外部キーの制約を持つスキーマを使用したことがあっても、それをあまり気にしておらず、SQLiteでは常に無視されると想定している場合は、準備して、B<外部キーのサポートがデフォルトで有効になっている場合でもアプリケーションが動作し続けることを保証するために、広範なテストを行ってください>。
SQLiteチームが将来的にデフォルトでオンにする可能性は非常に高く、私たちは彼らがそうするまでにそれを行う予定です。
(TBR)
=begin original
See L for details.
=end original
詳細はLをご覧ください。
(TBR)
=head2 Pragma
=begin original
SQLite has a set of "Pragma"s to modifiy its operation or to query
for its internal data. These are specific to SQLite and are not
likely to work with other DBD libraries, but you may find some of
these are quite useful. DBD::SQLite actually sets some (like
C) for you when you connect to a database.
See L for details.
=end original
SQLiteには、操作を変更したり、内部データを照会したりするための「プラグマ」のセットがあります。
これらはSQLiteに固有のもので、他のDBDライブラリでは動作しないようですが、これらのいくつかは非常に便利です。
DBD::SQLiteは、データベースに接続するときに、実際にいくつか(Cなど)を設定します。
詳細はLを参照してください。
(TBR)
=head2 Transactions
=begin original
DBI/DBD::SQLite's transactions may be a bit confusing. They behave
differently according to the status of the C flag:
=end original
DBI/DBD::SQLiteのトランザクションは少し混乱しているかもしれません。
これらは、Cフラグのステータスに応じて動作が異なります。
(TBR)
=over 4
=item When the AutoCommit flag is on
=begin original
You're supposed to always use the auto-commit mode, except you
explicitly begin a transaction, and when the transaction ended,
you're supposed to go back to the auto-commit mode. To begin a
transaction, call C method, or issue a C
statement. To end it, call C methods, or issue
the corresponding statements.
=end original
トランザクションを明示的に開始し、トランザクションが終了したときに自動コミットモードに戻る場合を除き、常に自動コミットモードを使用する必要があります。
トランザクションを開始するには、Cメソッドをコールするか、C文を発行します。
トランザクションを終了するには、Cメソッドをコールするか、対応する文を発行します。
(TBR)
$dbh->{AutoCommit} = 1;
$dbh->begin_work; # or $dbh->do('BEGIN TRANSACTION');
# $dbh->{AutoCommit} is turned off temporarily during a transaction;
$dbh->commit; # or $dbh->do('COMMIT');
# $dbh->{AutoCommit} is turned on again;
=item When the AutoCommit flag is off
=begin original
You're supposed to always use the transactinal mode, until you
explicitly turn on the AutoCommit flag. You can explicitly issue
a C statement (only when an actual transaction has not
begun yet) but you're not allowed to call C method
(if you don't issue a C, it will be issued internally).
You can commit or roll it back freely. Another transaction will
automatically begins if you execute another statement.
=end original
AutoCommitフラグを明示的にオンにするまでは、常にtransactinalモードを使用する必要があります。
C文は明示的に発行できますが(実際のトランザクションがまだ開始されていない場合のみ)、Cメソッドをコールすることはできません(Cを発行しない場合は、内部的に発行されます)。
コミットまたはロールバックは自由に実行できます。
別の文を実行すると、別のトランザクションが自動的に開始されます。
(TBR)
$dbh->{AutoCommit} = 0;
# $dbh->do('BEGIN TRANSACTION') is not necessary, but possible
...
$dbh->commit; # or $dbh->do('COMMIT');
# $dbh->{AutoCommit} stays intact;
$dbh->{AutoCommit} = 1; # ends the transactional mode
=back
=begin original
This C mode is independent from the autocommit mode
of the internal SQLite library, which always begins by a C
statement, and ends by a C or a .
=end original
このCモードは、内部SQLiteライブラリのオートコミットモードから独立しています。
オートコミットモードは常にC文で始まり、Cまたはで終わります。
(TBR)
=head2 Performance
(性能)
=begin original
SQLite is fast, very fast. Matt processed my 72MB log file with it,
inserting the data (400,000+ rows) by using transactions and only
committing every 1000 rows (otherwise the insertion is quite slow),
and then performing queries on the data.
=end original
SQLite は高速です、非常に高速です。
Matt は、私の 72MB のログファイルをそれで処理しました。
単純に 1000 行毎にコミットしながら(そうでなければ挿入がとても遅くなります)、
トランザクションを使ってデータ(400,000 以上の行)を挿入し、
そのデータに問い合わせを実行しました。
=begin original
Queries like count(*) and avg(bytes) took fractions of a second to
return, but what surprised him most of all was:
=end original
count(*) と avg(bytes) のような問い合わせは、たちまち返ってきました。
しかし何より彼が驚いたことは:
SELECT url, count(*) as count
FROM access_log
GROUP BY url
ORDER BY count desc
LIMIT 20
=begin original
To discover the top 20 hit URLs on the site (L),
and it returned within 2 seconds. He was seriously considering
switching his log analysis code to use this little speed demon!
=end original
サイト (L) にヒットし たURL のトップ 20 を
見つけることでした。
これを 2 秒以内で返しました。
彼は真剣に私のログ解析コードを、この小さな
スピードの魔物を使うように変更することを考えています。
=begin original
Oh yeah, and that was with no indexes on the table, on a 400MHz PIII.
=end original
そうそう、テーブルにはインデックスをつけていませんし、400MHz PIII の上です。
=begin original
For best performance be sure to tune your hdparm settings if you
are using linux. Also you might want to set:
=end original
パフォーマンスを最高にするためには、linux を使っているのであれば、
hdparam のセッティングを確実にチューンしてください。
またこのように設定したいかもしれません:
PRAGMA default_synchronous = OFF
=begin original
Which will prevent sqlite from doing fsync's when writing (which
slows down non-transactional writes significantly) at the expense
of some peace of mind. Also try playing with the cache_size pragma.
=end original
これは、いくらかの心配を犠牲にすることで、トランザクションを使わない
書込みを非常に遅くしてしまう fsync がすることから、sqlite を保護します。
cache_size pragma でも楽しんでみるようにしてください。
=begin original
The memory usage of SQLite can also be tuned using the cache_size
pragma.
=end original
SQLiteのメモリ使用量は、cache_sizeプラグマを使用して調整することもできます。
(TBR)
$dbh->do("PRAGMA cache_size = 800000");
=begin original
The above will allocate 800M for DB cache; the default is 2M.
Your sweet spot probably lies somewhere in between.
=end original
上記はDBキャッシュに800Mを割り当てます。
デフォルトは2Mです。
あなたのスイートスポットはおそらくその間のどこかにあります。
(TBR)
=head1 DRIVER PRIVATE ATTRIBUTES
=head2 Database Handle Attributes
=over 4
=item sqlite_version
=begin original
Returns the version of the SQLite library which B is using,
e.g., "2.8.0". Can only be read.
=end original
Bが使用しているSQLiteライブラリのバージョン("2.8.0"など)を返します。
読み取り専用です。
(TBR)
=item sqlite_unicode
=begin original
If set to a true value, B will turn the UTF-8 flag on for all
text strings coming out of the database (this feature is currently disabled
for perl < 5.8.5). For more details on the UTF-8 flag see
L. The default is for the UTF-8 flag to be turned off.
=end original
trueに設定すると、Bは、データベースから出力されるすべてのテキスト文字列に対してUTF-8フラグをオンにします(perl<5.8.5では、この機能は現在無効になっています)。
UTF-8フラグの詳細については、Lを参照してください。
デフォルトでは、UTF-8フラグはオフになっています。
(TBR)
=begin original
Also note that due to some bizarreness in SQLite's type system (see
L), if you want to retain
blob-style behavior for B columns under C<< $dbh->{sqlite_unicode} = 1
>> (say, to store images in the database), you have to state so
explicitly using the 3-argument form of L when doing
updates:
=end original
また、SQLiteの型システム(Lを参照)のいくつかの奇妙さのために、C<< $dbh->{SQLite_unicode}=1>>の下のB列のブロブスタイルの動作を保持したい場合(たとえば、イメージをデータベースに格納する場合)、更新を行うときにLの3引数形式を使用して明示的に宣言する必要があることにも注意してください。
(TBR)
use DBI qw(:sql_types);
$dbh->{sqlite_unicode} = 1;
my $sth = $dbh->prepare("INSERT INTO mytable (blobcolumn) VALUES (?)");
# Binary_data will be stored as is.
$sth->bind_param(1, $binary_data, SQL_BLOB);
=begin original
Defining the column type as C in the DDL is B sufficient.
=end original
DDLで列タイプをCとして定義することは、Bで十分です。
(TBR)
=begin original
This attribute was originally named as C, and renamed to
C for integrity since version 1.26_06. Old C
attribute is still accessible but will be deprecated in the near future.
=end original
この属性は元々Cという名前でしたが、バージョン1.26_06から整合性のためにCに名前が変更されました。
古いC属性はまだアクセス可能ですが、近い将来廃止される予定です。
(TBR)
=back
=head1 METHODS
=head2 table_info
$sth = $dbh->table_info(undef, $schema, $table, $type, \%attr);
=begin original
Returns all tables and schemas (databases) as specified in L.
The schema and table arguments will do a C search. You can specify an
ESCAPE character by including an 'Escape' attribute in \%attr. The C<$type>
argument accepts a comma seperated list of the following types 'TABLE',
'VIEW', 'LOCAL TEMPORARY' and 'SYSTEM TABLE' (by default all are returned).
Note that a statement handle is returned, and not a direct list of tables.
=end original
Lで指定されたすべての表およびスキーマ(データベース)が戻されます。
スキーマおよび表の引数により、C検索が実行されます。
\%attrに'Escape'属性を含めることにより、ESCAPE文字を指定できます。
C<$type>引数には、'TABLE'、'VIEW'、'LOCAL TEMPORARY'、および'SYSTEM TABLE'タイプのカンマ区切りリストを指定できます(デフォルトではすべてが戻されます)。
表の直接リストではなく、文ハンドルが戻されることに注意してください。
(TBR)
=begin original
The following fields are returned:
=end original
次のフィールドが返されます。
(TBR)
=begin original
B: Always NULL, as SQLite does not have the concept of catalogs.
=end original
B:SQLiteにはカタログの概念がないため、常にNULLです。
(TBR)
=begin original
B: The name of the schema (database) that the table or view is
in. The default schema is 'main', temporary tables are in 'temp' and other
databases will be in the name given when the database was attached.
=end original
B:表またはビューが存在するスキーマ(データベース)の名前。
デフォルトのスキーマは'main'、一時表は'temp'、その他のデータベースはデータベースが接続されたときに指定された名前になります。
(TBR)
=begin original
B: The name of the table or view.
=end original
B:テーブルまたはビューの名前。
(TBR)
=begin original
B: The type of object returned. Will be one of 'TABLE', 'VIEW',
'LOCAL TEMPORARY' or 'SYSTEM TABLE'.
=end original
B:戻されるオブジェクトのタイプ。
'TABLE'、'VIEW'、'LOCAL TEMPORARY'、または'SYSTEM TABLE'のいずれかです。
(TBR)
=head1 DRIVER PRIVATE METHODS
=begin original
The following methods can be called via the func() method with a little
tweak, but the use of func() method is now discouraged by the L author
for various reasons (see DBI's document
L
for details). So, if you're using L >= 1.608, use these C
methods. If you need to use an older L, you can call these like this:
=end original
次のメソッドは、少しひねりを加えてfunc()メソッドを介して呼び出すことができますが、Lの作成者は、さまざまな理由からfunc()メソッドの使用を推奨していません(詳細については、DBIの文書Lを参照してください)。
したがって、L>=1.608を使用している場合は、これらのCメソッドを使用します。
古いLを使用する必要がある場合は、次のように呼び出すことができます:
(TBR)
$dbh->func( ..., "(method name without sqlite_ prefix)" );
=head2 $dbh->sqlite_last_insert_rowid()
=begin original
This method returns the last inserted rowid. If you specify an INTEGER PRIMARY
KEY as the first column in your table, that is the column that is returned.
Otherwise, it is the hidden ROWID column. See the sqlite docs for details.
=end original
このメソッドは最後に挿入された rowid を返します。
もし INTEGER PRIMARY KEY をあなたのテーブルでの先頭カラムとして指定すれば、
返されるのはそのカラムになります。
そうでなければ、それは隠された ROWID カラムになります。
詳細については sqlite ドキュメントをご覧ください。
=begin original
Generally you should not be using this method. Use the L last_insert_id
method instead. The usage of this is:
=end original
通常、このメソッドは使用しません。
かわりにLlast_insert_idメソッドを使用します。
このメソッドの使用方法は次のとおりです:
(TBR)
$h->last_insert_id($catalog, $schema, $table_name, $field_name [, \%attr ])
=begin original
Running C<$h-Elast_insert_id("","","","")> is the equivalent of running
C<$dbh-Esqlite_last_insert_rowid()> directly.
=end original
C<$h-Elast_insert_id("","","","")>を実行することは、C<$dbh-Esqlite_last_insert_rowid()>を直接実行することと同じです。
(TBR)
=head2 $dbh->sqlite_busy_timeout()
=begin original
Retrieve the current busy timeout.
=end original
現在のビジータイムアウトを取得します。
(TBR)
=head2 $dbh->sqlite_busy_timeout( $ms )
=begin original
Set the current busy timeout. The timeout is in milliseconds.
=end original
現在のビジータイムアウトを設定します。
タイムアウトはミリ秒単位です。
(TBR)
=head2 $dbh->sqlite_create_function( $name, $argc, $code_ref )
=begin original
This method will register a new function which will be useable in an SQL
query. The method's parameters are:
=end original
このメソッドは、SQLクエリで使用できる新しい関数を登録します。
メソッドのパラメータは次のとおりです。
(TBR)
=over
=item $name
=begin original
The name of the function. This is the name of the function as it will
be used from SQL.
=end original
関数の名前。
これはSQLから使用される関数の名前です。
(TBR)
=item $argc
=begin original
The number of arguments taken by the function. If this number is -1,
the function can take any number of arguments.
=end original
関数が取る引数の数。
この数が-1の場合、関数は任意の数の引数を取ることができます。
(TBR)
=item $code_ref
=begin original
This should be a reference to the function's implementation.
=end original
これは、関数の実装への参照である必要があります。
(TBR)
=back
=begin original
For example, here is how to define a now() function which returns the
current number of seconds since the epoch:
=end original
たとえば、エポックからの現在の秒数を返すnow()関数を定義する方法を次に示します。
(TBR)
$dbh->sqlite_create_function( 'now', 0, sub { return time } );
=begin original
After this, it could be use from SQL as:
=end original
この後、SQLから次のように使用できます。
(TBR)
INSERT INTO mytable ( now() );
=head3 REGEXP function
=begin original
SQLite includes syntactic support for an infix operator 'REGEXP', but
without any implementation. The C driver
automatically registers an implementation that performs standard
perl regular expression matching, using current locale. So for example
you can search for words starting with an 'A' with a query like
=end original
SQLiteには、インフィックス演算子'REGEXP'の構文サポートが含まれていますが、実装はありません。
Cドライバは、現在のロケールを使用して、標準的なperl正規表現の照合を実行する実装を自動的に登録します。
したがって、たとえば、次のようなクエリを使用して、'A'で始まる単語を検索できます。
(TBR)
SELECT * from table WHERE column REGEXP '\bA\w+'
=begin original
If you want case-insensitive searching, use perl regex flags, like this :
=end original
大文字と小文字を区別しない検索が必要な場合は、次のようにperl regexフラグを使用します。
(TBR)
SELECT * from table WHERE column REGEXP '(?i:\bA\w+)'
=begin original
The default REGEXP implementation can be overriden through the
C API described above.
=end original
デフォルトのREGEXP実装は、前述のCAPIを介して上書きできます。
(TBR)
=begin original
Note that regexp matching will B use SQLite indices, but will iterate
over all rows, so it could be quite costly in terms of performance.
=end original
正規表現マッチングはSQLiteインデックスを使用<しません>しますが、すべての行に対して繰り返されるため、パフォーマンスの点で非常にコストがかかる可能性があることに注意してください。
(TBR)
=head2 $dbh->sqlite_create_collation( $name, $code_ref )
=begin original
This method manually registers a new function which will be useable in an SQL
query as a COLLATE option for sorting. Such functions can also be registered
automatically on demand: see section L"COLLATION FUNCTIONS"> below.
=end original
このメソッドは、SQLクエリーでソート用のCOLLATEオプションとして使用できる新しい関数を手動で登録します。
このような関数は、必要に応じて自動的に登録することもできます。
後述のセクションL"COLLATION FUNCTIONS">を参照してください。
(TBR)
=begin original
The method's parameters are:
=end original
このメソッドのパラメータは次のとおりです。
(TBR)
=over
=item $name
=begin original
The name of the function exposed to SQL.
=end original
SQLに公開されている関数の名前。
(TBR)
=item $code_ref
=begin original
Reference to the function's implementation.
The driver will check that this is a proper sorting function.
=end original
関数の実装への参照。
ドライバは、これが適切なソート関数であることを確認します。
(TBR)
=back
=head2 $dbh->sqlite_collation_needed( $code_ref )
=begin original
This method manually registers a callback function that will
be invoked whenever an undefined collation sequence is required
from an SQL statement. The callback is invoked as
=end original
このメソッドは、SQL文から未定義の照合順序が要求されるたびに起動されるコールバック関数を手動で登録します。
コールバックは次のように起動されます。
(TBR)
$code_ref->($dbh, $collation_name)
=begin original
and should register the desired collation using
L"sqlite_create_collation">.
=end original
L"sqlite_create_collation">を使用して目的の照合を登録する必要があります。
(TBR)
=begin original
An initial callback is already registered by C,
so for most common cases it will be simpler to just
add your collation sequences in the C<%DBD::SQLite::COLLATION>
hash (see section L"COLLATION FUNCTIONS"> below).
=end original
最初のコールバックはすでにCによって登録されているため、ほとんどの一般的なケースでは、C<%DBD::SQLite::COLLATION>ハッシュに照合順序を追加する方が簡単です(後述のセクションL"COLLATION FUNCTIONS">を参照)。
(TBR)
=head2 $dbh->sqlite_create_aggregate( $name, $argc, $pkg )
=begin original
This method will register a new aggregate function which can then be used
from SQL. The method's parameters are:
=end original
このメソッドは、SQLから使用できる新しい集計関数を登録します。
メソッドのパラメータは次のとおりです。
(TBR)
=over
=item $name
=begin original
The name of the aggregate function, this is the name under which the
function will be available from SQL.
=end original
集約関数の名前。
これは、SQLから使用できる関数の名前です。
(TBR)
=item $argc
=begin original
This is an integer which tells the SQL parser how many arguments the
function takes. If that number is -1, the function can take any number
of arguments.
=end original
これは、関数が取る引数の数をSQLパーサーに伝える整数です。
その数が-1の場合、関数は任意の数の引数を取ることができます。
(TBR)
=item $pkg
=begin original
This is the package which implements the aggregator interface.
=end original
これは、アグリゲータインタフェースを実装するパッケージです。
(TBR)
=back
=begin original
The aggregator interface consists of defining three methods:
=end original
アグリゲータインタフェースは、次の3つのメソッドの定義で構成されています。
(TBR)
=over
=item new()
=begin original
This method will be called once to create an object which should
be used to aggregate the rows in a particular group. The step() and
finalize() methods will be called upon the reference return by
the method.
=end original
このメソッドは、特定のグループ内の行を集計するために使用するオブジェクトを作成するために1回コールされます。
step()およびfinalize()メソッドは、メソッドからの参照が返されたときにコールされます。
(TBR)
=item step(@_)
=begin original
This method will be called once for each row in the aggregate.
=end original
このメソッドは、集約の各行に対して1回ずつ呼び出されます。
(TBR)
=item finalize()
=begin original
This method will be called once all rows in the aggregate were
processed and it should return the aggregate function's result. When
there is no rows in the aggregate, finalize() will be called right
after new().
=end original
このメソッドは、集計のすべての行が処理されると呼び出され、集計関数の結果を返す必要があります。
集計に行がない場合は、new()の直後にfinalize()が呼び出されます。
(TBR)
=back
=begin original
Here is a simple aggregate function which returns the variance
(example adapted from pysqlite):
=end original
次に、分散を返す単純な集約関数を示します(pysqliteから採用された例)。
(TBR)
package variance;
sub new { bless [], shift; }
sub step {
my ( $self, $value ) = @_;
push @$self, $value;
}
sub finalize {
my $self = $_[0];
my $n = @$self;
# Variance is NULL unless there is more than one row
return undef unless $n || $n == 1;
my $mu = 0;
foreach my $v ( @$self ) {
$mu += $v;
}
$mu /= $n;
my $sigma = 0;
foreach my $v ( @$self ) {
$sigma += ($x - $mu)**2;
}
$sigma = $sigma / ($n - 1);
return $sigma;
}
$dbh->sqlite_create_aggregate( "variance", 1, 'variance' );
=begin original
The aggregate function can then be used as:
=end original
集約関数は、次のように使用できます。
(TBR)
SELECT group_name, variance(score)
FROM results
GROUP BY group_name;
=begin original
For more examples, see the L.
=end original
その他の例については、Lを参照してください。
(TBR)
=head2 $dbh->sqlite_progress_handler( $n_opcodes, $code_ref )
=begin original
This method registers a handler to be invoked periodically during long
running calls to SQLite.
=end original
このメソッドは、長時間実行されるSQLiteの呼び出し中に定期的に呼び出されるハンドラを登録します。
(TBR)
=begin original
An example use for this interface is to keep a GUI updated during a
large query. The parameters are:
=end original
このインタフェースの使用例としては、大規模なクエリーの実行中にGUIを更新しておくことが挙げられます。
パラメータは次のとおりです。
(TBR)
=over
=item $n_opcodes
=begin original
The progress handler is invoked once for every C<$n_opcodes>
virtual machine opcodes in SQLite.
=end original
progressハンドラは、SQLiteのC<$n_opcodes>仮想マシンのオペコードごとに1回呼び出されます。
(TBR)
=item $code_ref
=begin original
Reference to the handler subroutine. If the progress handler returns
non-zero, the SQLite operation is interrupted. This feature can be used to
implement a "Cancel" button on a GUI dialog box.
=end original
ハンドラサブルーチンへの参照。
進行状況ハンドラがゼロ以外を返す場合、SQLite操作は中断されます。
この機能を使用して、GUIダイアログボックスに「キャンセル」ボタンを実装できます。
(TBR)
=begin original
Set this argument to C if you want to unregister a previous
progress handler.
=end original
以前の進捗ハンドラの登録を解除する場合は、この引数をCに設定します。
(TBR)
=back
=head2 $dbh->sqlite_commit_hook( $code_ref )
=begin original
This method registers a callback function to be invoked whenever a
transaction is committed. Any callback set by a previous call to
C is overridden. A reference to the previous
callback (if any) is returned. Registering an C disables the
callback.
=end original
このメソッドは、トランザクションがコミットされるたびに呼び出されるコールバック関数を登録します。
以前のCの呼び出しによって設定されたコールバックはすべて上書きされます。
以前のコールバックへの参照(存在する場合)が返されます。
Cを登録すると、コールバックが無効になります。
(TBR)
=begin original
When the commit hook callback returns zero, the commit operation is
allowed to continue normally. If the callback returns non-zero, then
the commit is converted into a rollback (in that case, any attempt to
I call C<< $dbh->rollback() >> afterwards would yield an
error).
=end original
コミットフックコールバックがゼロを返すと、コミット操作は正常に続行できます。
コールバックがゼロ以外を返すと、コミットはロールバックに変換されます(その場合、後でC<< $dbh->rollback()>>を<明示的に>呼び出そうとすると、エラーが発生します)。
(TBR)
=head2 $dbh->sqlite_rollback_hook( $code_ref )
=begin original
This method registers a callback function to be invoked whenever a
transaction is rolled back. Any callback set by a previous call to
C is overridden. A reference to the previous
callback (if any) is returned. Registering an C disables the
callback.
=end original
このメソッドは、トランザクションがロールバックされるたびに呼び出されるコールバック関数を登録します。
以前のCの呼び出しによって設定されたコールバックはすべて上書きされます。
以前のコールバックへの参照(存在する場合)が返されます。
Cを登録すると、コールバックが無効になります。
(TBR)
=head2 $dbh->sqlite_update_hook( $code_ref )
=begin original
This method registers a callback function to be invoked whenever a row
is updated, inserted or deleted. Any callback set by a previous call to
C is overridden. A reference to the previous
callback (if any) is returned. Registering an C disables the
callback.
=end original
このメソッドは、行が更新、挿入、または削除されるたびに呼び出されるコールバック関数を登録します。
以前のCの呼び出しによって設定されたコールバックはすべて上書きされます。
以前のコールバックへの参照(存在する場合)が返されます。
Cを登録すると、コールバックが無効になります。
(TBR)
=begin original
The callback will be called as
=end original
コールバックは次のように呼び出されます。
(TBR)
$code_ref->($action_code, $database, $table, $rowid)
=begin original
where
=end original
ここで
(TBR)
=over
=item $action_code
=begin original
is an integer equal to either C,
C or C
(see L"Action Codes">);
=end original
C、C、またはC(L"Action Codes">を参照)のいずれかと等しい整数です。
(TBR)
=item $database
=begin original
is the name of the database containing the affected row;
=end original
は、影響を受ける行を含むデータベースの名前です。
(TBR)
=item $table
=begin original
is the name of the table containing the affected row;
=end original
は、影響を受ける行を含むテーブルの名前です。
(TBR)
=item $rowid
=begin original
is the unique 64-bit signed integer key of the affected row within that table.
=end original
は、そのテーブル内の影響を受けるローの一意の64ビット符号付き整数キーです。
(TBR)
=back
=head2 $dbh->sqlite_set_authorizer( $code_ref )
=begin original
This method registers an authorizer callback to be invoked whenever
SQL statements are being compiled by the L method. The
authorizer callback should return C to allow the
action, C to disallow the specific action but
allow the SQL statement to continue to be compiled, or
C to cause the entire SQL statement to be rejected
with an error. If the authorizer callback returns any other value,
then then C call that triggered the authorizer will fail with
an error message.
=end original
このメソッドは、LメソッドによってSQL文がコンパイルされるたびに呼び出される承認者コールバックを登録します。
承認者コールバックは、アクションを許可する場合はC、特定のアクションを禁止するがSQL文のコンパイルを続行する場合はC、またはSQL文全体をエラーで拒否する場合はCを返す必要があります。
承認者コールバックがその他の値を返す場合、承認者をトリガーしたC呼び出しはエラーメッセージで失敗します。
(TBR)
=begin original
An authorizer is used when preparing SQL statements from an untrusted
source, to ensure that the SQL statements do not try to access data
they are not allowed to see, or that they do not try to execute
malicious statements that damage the database. For example, an
application may allow a user to enter arbitrary SQL queries for
evaluation by a database. But the application does not want the user
to be able to make arbitrary changes to the database. An authorizer
could then be put in place while the user-entered SQL is being
prepared that disallows everything except SELECT statements.
=end original
承認者は、信頼できないソースからSQL文を準備するときに、SQL文が表示を許可されていないデータにアクセスしようとしたり、データベースに損害を与える悪意のある文を実行しようとしたりしないようにするために使用されます。
たとえば、アプリケーションでは、データベースによる評価のためにユーザーが任意のSQL問合せを入力できるようにする場合があります。
ただし、アプリケーションでは、ユーザーがデータベースに任意の変更を加えられないようにする必要があります。
承認者は、SELECT文以外のすべてを禁止するユーザー入力SQLが準備されている間に配置できます。
(TBR)
=begin original
The callback will be called as
=end original
コールバックは次のように呼び出されます。
(TBR)
$code_ref->($action_code, $string1, $string2, $database, $trigger_or_view)
=begin original
where
=end original
ここで
(TBR)
=over
=item $action_code
=begin original
is an integer that specifies what action is being authorized
(see L"Action Codes">).
=end original
は、許可されているアクションを指定する整数です(L"Action Codes">を参照)。
(TBR)
=item $string1, $string2
=begin original
are strings that depend on the action code
(see L"Action Codes">).
=end original
は、アクションコードに依存する文字列です(L"Action Codes">を参照)。
(TBR)
=item $database
=begin original
is the name of the database (C, C, etc.) if applicable.
=end original
は、データベースの名前です(C、Cなど)。
(TBR)
=item $trigger_or_view
=begin original
is the name of the inner-most trigger or view that is responsible for
the access attempt, or C if this access attempt is directly from
top-level SQL code.
=end original
は、アクセス試行の原因となった最も内側のトリガまたはビューの名前です。
アクセス試行がトップレベルのSQLコードから直接行われた場合は、Cです。
(TBR)
=back
=head2 $dbh->sqlite_backup_from_file( $filename )
=begin original
This method accesses the SQLite Online Backup API, and will take a backup of
the named database file, copying it to, and overwriting, your current database
connection. This can be particularly handy if your current connection is to the
special :memory: database, and you wish to populate it from an existing DB.
=end original
このメソッドはSQLite Online Backup APIにアクセスし、指定されたデータベースファイルのバックアップを取得し、現在のデータベース接続にコピーして上書きします。
これは、現在の接続が特別な:memory:データベースであり、既存のDBからデータを入力する場合に特に便利です。
(TBR)
=head2 $dbh->sqlite_backup_to_file( $filename )
=begin original
This method accesses the SQLite Online Backup API, and will take a backup of
the currently connected database, and write it out to the named file.
=end original
このメソッドはSQLite Online Backup APIにアクセスし、現在接続されているデータベースのバックアップを取得して、指定されたファイルに書き出します。
(TBR)
=head2 $dbh->sqlite_enable_load_extension( $bool )
=begin original
Calling this method with a true value enables loading (external)
sqlite3 extensions. After the call, you can load extensions like this:
=end original
true値を指定してこのメソッドを呼び出すと、(外部)sqlite3拡張機能を読み込むことができます。
呼び出し後、次のように拡張機能を読み込むことができます。
(TBR)
$dbh->sqlite_enable_load_extension(1);
$sth = $dbh->prepare("select load_extension('libsqlitefunctions.so')")
or die "Cannot prepare: " . $dbh->errstr();
=head1 DRIVER CONSTANTS
=begin original
A subset of SQLite C constants are made available to Perl,
because they may be needed when writing
hooks or authorizer callbacks. For accessing such constants,
the C module must be explicitly C