「データ定義API」の版間の差分

提供:MoodleDocs
移動先:案内検索
(ページの作成:「{{Moodle_2.0}} 作成中です - ~~~ In this page you'll access to the available functions under Moodle to be able to handle DB structures (tables, fields, indexes...)....」)
 
編集の要約なし
 
(同じ利用者による、間の59版が非表示)
1行目: 1行目:
{{Moodle_2.0}}
{{Moodle_2.0}}


作成中です - [[利用者:Mitsuhiro Yoshida|Mitsuhiro Yoshida]] ([[利用者・トーク:Mitsuhiro Yoshida|トーク]])
あなたはこのページでMoodleがデータベース構造 (テーブル、フィールド、インデックス等) を処理するために利用可能な関数に接します。


In this page you'll access to the available functions under Moodle to be able to handle DB structures (tables, fields, indexes...).
関数の目的はすべてのデータベース構造(DDL文)を扱える明確に定義された関数群を1つの中立的な記述で持つことにより、RDBMSが要求する正しいSQL文を実行できるようにすることです。これらすべての関数は[[プラグインデータベーステーブルのインストールおよびアップグレード|インストールおよびアップグレード処理]]にのみ使用されます。


The objective is to have a well-defined group of functions able to handle all the DB structure (DDL statements) using one neutral description, being able to execute the correct SQL statements required by each RDBMS. All these functions are used '''[[Installing and upgrading plugin database tables|exclusively by the installation and upgrade processes]]'''.
あなたはこのページで説明、裏技および使用説明を含む関数等の完全な一覧を閲覧できます。もし、興味をお持ちでしたら、DBデータ処理 (選択、更新、削除等のDML文) のすべてが説明されている[[DML 関数|DML関数ページ]]をご覧ください。


In this page you'll see a complete list of such functions, with some explanations, tricks and examples of their use. If you are interested, it's also highly recommendable to take a look to the [[DML functions|DML functions page]] where everything about how to handle DB data (select, insert, update, delete i.e. DML statements) is defined.
もちろん、このドキュメントの内容の明確化、作成および情報の追加等、ご自由に編集してください。心より歓迎致します!


Of course, feel free to clarify, complete and add more info to all this documentation. It will be welcome, absolutely!
==主要情報==
'''重要なメモ:''' このページに記載されるすべての関数は私たちが新しい機能をサポートするために変更した「Moodle 2.0以降」の[[DBレイヤ2.0|DBレイヤ]]で使用されます。Moodleの以前のバージョンの情報が必要な場合、 [[DDL関数 - 2.0以前|DDL関数 - 2.0以前]]ページをご覧ください。変更点の詳細は[[DBレイヤ2.0移行ドキュメント|移行ドキュメント]]をご覧ください。


==Main info==
* このページでコールされる関数すべては$DBグローバルオブジェクトからアクセス可能な「データベースマネージャ」のパブリックメソッドです。そのため、以下のように「upgrade.php」のメイン関数に「インポート」する必要があります:
 
'''Important note:''' All the functions showed in this page are for use in '''Moodle 2.0 upwards''', where we changed the [[DB layer 2.0|DB layer]] to support some new features. If you need information for previous Moodle version, take a look to the [[DDL functions - pre 2.0|DDL functions - pre 2.0]] page. For a detailed reference of changes, see the [[DB layer 2.0 migration docs|migration docs]].
 
* All the function calls in this page are public methods of the '''database manager''', accessible from the $DB global object. So you'll need to "import" it within your '''upgrade.php''' main function with something like:


<code php>
<code php>
22行目: 19行目:
     global $DB;
     global $DB;
    
    
     $dbman = $DB->get_manager(); // loads ddl manager and xmldb classes
     $dbman = $DB->get_manager(); // ddlマネージャおよびxmldbクラスを読み込みます。
    
    
     /// Your upgrade code goes here
     /// ここにあなたのアップグレードコードを記述します。
}
}


</code>
</code>


* Once more, the use of these functions is '''restricted''' to the upgrade processes and it shouldn't be used ever out from there.
* 再度、これら関数の使用はアップグレード処理のみに「制限」してください。そして、ここ以外では使用されないようにすべきです。
* All the $table, $field, $index parameters are, always, XMLDB objects. Don't forget to read carefully the complete documentation about [[XMLDB creating new DDL functions|creating new DDL functions]] before playing with these functions. Everything is explained there, with one general example and some really useful tricks to improve the use of all the functions detailed below.
* すべての「$table, $field, $index」パラメータは常にXMLDオブジェクトです。これらの関数を試す前に[[XMLDB 新しいDDL関数を作成する|新しいDDL関数の作成]]に関する完全なドキュメントを忘れずにご覧ください。そこでは1つの例および以下に詳述されているすべての関数の使用に関する実用的なトリックが説明されています。
* If you want real examples of the usage of these functions it's 100% interesting to examine the '''upgrade.php''' scripts that are responsible for Moodle upgrading since the 1.7 release.  
* あなたがこれらの関数の実際の使用を必要とする場合、1.7リリース以降のアップグレードを担当する「upgrade.php」スクリプトをお試しください。
* Also, it's a '''very good idea''' (highly recommended!) to use the [[XMLDB editor|XMLDB Editor]] itself to generate automatically the desired PHP code. Just play with it!
* また、必要なPHPコードを自動生成する目的での[[XMLDBエディタ]]の使用は本当に素晴らしいアイデアです (強くお勧めします!)。


==The functions==
==関数==


===Handling tables===
===テーブル処理===
<code php>
<code php>
/// To detect if one table exists:
/// テーブルの存在を検知するため:
     $dbman->table_exists($table)
     $dbman->table_exists($table)


/// To create one table:
/// テーブルを作成するため:
     $dbman->create_table($table, $continue=true, $feedback=true)
     $dbman->create_table($table, $continue=true, $feedback=true)


/// To drop one table:
/// テーブルを削除するため:
     $dbman->drop_table($table, $continue=true, $feedback=true)
     $dbman->drop_table($table, $continue=true, $feedback=true)


/// To rename one table:
/// テーブルをリネームするため:
     $dbman->rename_table($table, $newname, $continue=true, $feedback=true)
     $dbman->rename_table($table, $newname, $continue=true, $feedback=true)
</code>
</code>


===Handling fields===
===フィールド処理===
<code php>
<code php>
/// To detect if one field exists:
/// フィールドの存在を検出するため:
     $dbman->field_exists($table, $field)
     $dbman->field_exists($table, $field)


/// To create one field:
/// フィールドを作成するため:
     $dbman->add_field($table, $field, $continue=true, $feedback=true)
     $dbman->add_field($table, $field, $continue=true, $feedback=true)


/// To drop one field:
/// フィールドを削除するため:
     $dbman->drop_field($table, $field, $continue=true, $feedback=true)
     $dbman->drop_field($table, $field, $continue=true, $feedback=true)


/// To change the type of one field:
/// フィールドタイプを変更するため:
     $dbman->change_field_type($table, $field, $continue=true, $feedback=true)
     $dbman->change_field_type($table, $field, $continue=true, $feedback=true)


/// To change the precision of one field:
/// フィールドの精度を変更するため:
     $dbman->change_field_precision($table, $field, $continue=true, $feedback=true)
     $dbman->change_field_precision($table, $field, $continue=true, $feedback=true)


/// To change the signed/unsigned status of one field:
/// フィールドの符号付き/符号なしステータスを変更するため:
     $dbman->change_field_unsigned($table, $field, $continue=true, $feedback=true)
     $dbman->change_field_unsigned($table, $field, $continue=true, $feedback=true)


/// To make one field nullable or not:
/// フィールドをNULL可またはNULL不可にするため:
     $dbman->change_field_notnull($table, $field, $continue=true, $feedback=true)
     $dbman->change_field_notnull($table, $field, $continue=true, $feedback=true)


/// To drop one enum (check constraint) from one field:
/// フィールドからenum (列挙型) を削除して制約を確認するため:
/// (note this function will be only available in Moodle 2.0 as it's needed
/// (この関数はMoodle 2.0で必要なため、Moodle 2.0のみで利用できる点に留意してください。 Moodle 2.1で除外されます。)
/// to drop any enum existing in previous Moodle releases). Will be out in Moodle 2.1
     $dbman->drop_enum_from_field($table, $field, $continue=true, $feedback=true)
     $dbman->drop_enum_from_field($table, $field, $continue=true, $feedback=true)


/// To change the default value of one field:
/// フィールドのデフォルト値を変えるため:
     $dbman->change_field_default($table, $field, $continue=true, $feedback=true)
     $dbman->change_field_default($table, $field, $continue=true, $feedback=true)


/// To rename one field:
/// フィールドをリネームするため:
     $dbman->rename_field($table, $field, $newname, $continue=true, $feedback=true)
     $dbman->rename_field($table, $field, $newname, $continue=true, $feedback=true)
</code>
</code>


===Handling indexes===
===インデックス処理===
<code php>
<code php>
/// To detect if one index exists:
/// インデックスの存在を検知するため:
     $dbman->index_exists($table, $index)
     $dbman->index_exists($table, $index)


/// To return the name of one index in DB:
/// DBのインデックス名を返すため:
     $dbman->find_index_name($table, $index)
     $dbman->find_index_name($table, $index)


/// To add one index:
/// インデックスを追加するため:
     $dbman->add_index($table, $index, $continue=true, $feedback=true)
     $dbman->add_index($table, $index, $continue=true, $feedback=true)


/// To drop one index:
/// インデックスを削除するため:
     $dbman->drop_index($table, $index, $continue=true, $feedback=true)
     $dbman->drop_index($table, $index, $continue=true, $feedback=true)
</code>
</code>


==Some considerations==
==考慮すべき点==
# All the $table, $field, $index parameters are, always, XMLDB objects.
# すべての$table、$field、$indexパラメータは常にXMLDBオブジェクトです。
# All the $newtablename, $newfieldname parameters are, always, simple strings.
# すべての$newtablename、$newfieldnameパラメータは常に単純文字列です。
# All the ***_exists() functions return boolean true/false.
# 関数実行時のすべてのトラブルは1つの例外に投げられた上でアップグレードプロセスが中止されます。
# Any problem in the execution of the functions will throw one Exception and the upgrade process will die.
# 自動的にPHPコードを生成する場合、[[XMLDBエディタ|XMLDBエディタ]]の使用をお勧めします。(did I say this before? :-P )
# It's recommendable to use the [[XMLDB editor|XMLDB Editor]] to generate the PHP code automatically (did I say this before? :-P )


==See also==
==関連情報==


* [[Core APIs]]
* [[コアAPI]]
* [[XMLDB Documentation|XMLDB Documentation]]: Main page of the whole XMLDB documentation, where all the process is defined and all the related information resides.
* [[XMLDBドキュメンテーション|XMLDBドキュメンテーション]]: 全XMLDBドキュメンテーションのメインページです。すべての処理定義および関連情報が記載されています。
* [[XMLDB Defining one XML structure]]: Where you will know a bit more about the underlying XML structure used to define the DB objects, that is used continuously by the functions described in this page.
* [[XMLDB XML構造定義]]: DBオブジェクトの定義に使用されるXML構造に関して、さらに少しだけ理解できます。このページでの関数に繰り返し使用されています。
* [[Installing and upgrading plugin database tables|Installing and upgrading plugin DB tables]]
* [[プラグインデータベーステーブルのインストールおよびアップグレード|プラグインDBテーブルのインストールおよびアップグレード]]
* [[DML functions|DML functions]]: Where all the functions used to handle DB data ([[wikipedia:Data_Manipulation_Language|DML]]) are defined.
* [[DML関数|DML 関数]]: DBデータを処理する関数 ([[wikipedia:データ操作言語|DML]]) すべてが定義されています。
* [[DDL functions - pre 2.0|DDL functions - pre 2.0]]: '''(deprecated!)''' For information valid before Moodle 2.0.
* [[DDL関数- pre 2.0|DDL関数- pre 2.0]]: '''(廃止予定!)''' Moodle 2.0以前の情報です。
* [[DB layer 2.0 migration docs|DB layer 2.0 migration docs]]: Information about how to modify your code to work with the new Moodle 2.0 DB layer.
* [[DBレイヤ2.0移行ドキュメント|DBレイヤ2.0移行ドキュメント]]: あなたのコードを新しいMoodle 2.0 DBレイヤで動作させるための修正方法に関する情報です。
* [[DTL functions|DTL functions]]: Exporting, importing and moving of data stored in sql databases
* [[DTL関数|DTL関数]]: SQLデータベースに保存されたデータをエクスポート、インポートおよび移動します。


[[Category:DB]]
[[Category:DB]]

2020年10月8日 (木) 06:53時点における最新版

Moodle 2.0


あなたはこのページでMoodleがデータベース構造 (テーブル、フィールド、インデックス等) を処理するために利用可能な関数に接します。

関数の目的はすべてのデータベース構造(DDL文)を扱える明確に定義された関数群を1つの中立的な記述で持つことにより、RDBMSが要求する正しいSQL文を実行できるようにすることです。これらすべての関数はインストールおよびアップグレード処理にのみ使用されます。

あなたはこのページで説明、裏技および使用説明を含む関数等の完全な一覧を閲覧できます。もし、興味をお持ちでしたら、DBデータ処理 (選択、更新、削除等のDML文) のすべてが説明されているDML関数ページをご覧ください。

もちろん、このドキュメントの内容の明確化、作成および情報の追加等、ご自由に編集してください。心より歓迎致します!

主要情報

重要なメモ: このページに記載されるすべての関数は私たちが新しい機能をサポートするために変更した「Moodle 2.0以降」のDBレイヤで使用されます。Moodleの以前のバージョンの情報が必要な場合、 DDL関数 - 2.0以前ページをご覧ください。変更点の詳細は移行ドキュメントをご覧ください。

  • このページでコールされる関数すべては$DBグローバルオブジェクトからアクセス可能な「データベースマネージャ」のパブリックメソッドです。そのため、以下のように「upgrade.php」のメイン関数に「インポート」する必要があります:

function xmldb_xxxx_upgrade {

   global $DB;
 
   $dbman = $DB->get_manager(); // ddlマネージャおよびxmldbクラスを読み込みます。
 
   /// ここにあなたのアップグレードコードを記述します。

}

  • 再度、これら関数の使用はアップグレード処理のみに「制限」してください。そして、ここ以外では使用されないようにすべきです。
  • すべての「$table, $field, $index」パラメータは常にXMLDオブジェクトです。これらの関数を試す前に新しいDDL関数の作成に関する完全なドキュメントを忘れずにご覧ください。そこでは1つの例および以下に詳述されているすべての関数の使用に関する実用的なトリックが説明されています。
  • あなたがこれらの関数の実際の使用を必要とする場合、1.7リリース以降のアップグレードを担当する「upgrade.php」スクリプトをお試しください。
  • また、必要なPHPコードを自動生成する目的でのXMLDBエディタの使用は本当に素晴らしいアイデアです (強くお勧めします!)。

関数

テーブル処理

/// テーブルの存在を検知するため:

   $dbman->table_exists($table)

/// テーブルを作成するため:

   $dbman->create_table($table, $continue=true, $feedback=true)

/// テーブルを削除するため:

   $dbman->drop_table($table, $continue=true, $feedback=true)

/// テーブルをリネームするため:

   $dbman->rename_table($table, $newname, $continue=true, $feedback=true)

フィールド処理

/// フィールドの存在を検出するため:

   $dbman->field_exists($table, $field)

/// フィールドを作成するため:

   $dbman->add_field($table, $field, $continue=true, $feedback=true)

/// フィールドを削除するため:

   $dbman->drop_field($table, $field, $continue=true, $feedback=true)

/// フィールドタイプを変更するため:

   $dbman->change_field_type($table, $field, $continue=true, $feedback=true)

/// フィールドの精度を変更するため:

   $dbman->change_field_precision($table, $field, $continue=true, $feedback=true)

/// フィールドの符号付き/符号なしステータスを変更するため:

   $dbman->change_field_unsigned($table, $field, $continue=true, $feedback=true)

/// フィールドをNULL可またはNULL不可にするため:

   $dbman->change_field_notnull($table, $field, $continue=true, $feedback=true)

/// フィールドからenum (列挙型) を削除して制約を確認するため: /// (この関数はMoodle 2.0で必要なため、Moodle 2.0のみで利用できる点に留意してください。 Moodle 2.1で除外されます。)

   $dbman->drop_enum_from_field($table, $field, $continue=true, $feedback=true)

/// フィールドのデフォルト値を変えるため:

   $dbman->change_field_default($table, $field, $continue=true, $feedback=true)

/// フィールドをリネームするため:

   $dbman->rename_field($table, $field, $newname, $continue=true, $feedback=true)

インデックス処理

/// インデックスの存在を検知するため:

   $dbman->index_exists($table, $index)

/// DBのインデックス名を返すため:

   $dbman->find_index_name($table, $index)

/// インデックスを追加するため:

   $dbman->add_index($table, $index, $continue=true, $feedback=true)

/// インデックスを削除するため:

   $dbman->drop_index($table, $index, $continue=true, $feedback=true)

考慮すべき点

  1. すべての$table、$field、$indexパラメータは常にXMLDBオブジェクトです。
  2. すべての$newtablename、$newfieldnameパラメータは常に単純文字列です。
  3. 関数実行時のすべてのトラブルは1つの例外に投げられた上でアップグレードプロセスが中止されます。
  4. 自動的にPHPコードを生成する場合、XMLDBエディタの使用をお勧めします。(did I say this before? :-P )

関連情報