Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Using XMLDB: Difference between revisions

From MoodleDocs
m (formatting)
Line 1: Line 1:
(This page is in progress of writing and editing, intended for third-party module developers to help them switch to XMLDB.)
{{Moodle 1.7}}Note: This page is a work in progress. It is intended for third-party module developers to help them switch to XMLDB.
{{Moodle 1.7}}
 
==What is XMLDB and how can it help you==
==What is XMLDB and how can it help you==
XMLDB is a database format created to help developer from creating custom database schemas for different database system. It's like a database in XML format. Everywhere in Moodle now use XMLDB.
XMLDB is a database format created to help developers from creating custom database schemas for different database system. It's like a database in XML format. Everywhere in Moodle now uses XMLDB.


In every module folder, there is db/ folder where database structure stored. The folder usually contains mysql.php, mysql.sql, etc. for different database.
In every module folder, there is a ''db/'' folder where the database structure stored. The folder usually contains ''mysql.php'', ''mysql.sql'', etc. for different databases.


It's mean that you have to create different files for each database systems. If there are more database you want your module to be compatible with, you have to create more files. XMLDB is intended to solve this problem. Switching to XMLDB is very easy, save your time and effort by creating just 1 file.
It means that you have to create different files for each database systems. If there are more databases you want your module to be compatible with, you have to create more files. XMLDB is intended to solve this problem. Switching to XMLDB is very easy and saves your time and effort by creating just 1 file.


==Getting Started==
==Getting started==
XMLDB file in module is stored in db/ folder, named install.xml. You can use a built-in XMLDB editor to create or edit any install.xml files. If you're using MySQL as your default database schemes. XMLDB editor's powerful tools allowing retrofit MySQL table to XMLDB format.
XMLDB file in module is stored in ''db/'' folder, named ''install.xml''. You can use a built-in XMLDB editor to create or edit any ''install.xml'' files. If you're using MySQL as your default database schemes. XMLDB editor's powerful tools allowing retrofit MySQL table to XMLDB format.


XMLDB editor is located in administration block, go to ''Miscellaneous > XMLDB editor''. Find your module name. You won't be able to click on ''Create'' link and also can't edit your module (If ''Create'' link is clickable you can skip this step). Since you must make your module folder has write permission first (And if you haven't created db/ folder in your module folder yet, create it now). In Linux operating system, ''chmod'' your /db folder to 777 solves this. Now you're ready to create your module's XMLDB files.
The XMLDB editor is located in administration block, go to ''Miscellaneous > XMLDB editor''. Find your module name. Initially you won't be able to click on ''Create'' link and also can't edit your module (If ''Create'' link is clickable you can skip this step). Since you must make your module folder has write permission first (And if you haven't created ''db/'' folder in your module folder yet, create it now). In Linux operating system, ''chmod'' your ''/db'' folder to 777 solves this. Now you're ready to create your module's XMLDB files.


Note that XMLDB editor is not a tool for creating your module (It can help you about database). It is a tool to manage XMLDB database file (think about phpmyadmin manages SQL database). You can create tables/fields/keys/indexes or export your exist MySQL tables to XMLDB.
Note that XMLDB editor is not a tool for creating your module, though it can help you with the database. It is a tool to manage XMLDB database file (think about phpmyadmin manages SQL database). You can create tables/fields/keys/indexes or export your existing MySQL tables to XMLDB.


Another XMLDB's advantage is you can create table's comment as you want. To describe it to other developer.
Another XMLDB's advantage is you can create table comments as you want. To describe it to other developer.


XMLDB editor is really easy to work with. We strongly recommend you to play with it for a while.
XMLDB editor is really easy to work with. We strongly recommend you to play with it for a while and read [[XMLDB editor]] and  about XMLDB structure [[XMLDB defining an XML structure]].


More information about XMLDB editor : [https://docs.moodle.org/en/XMLDB_editor], XMLDB Structure : [https://docs.moodle.org/en/Development:XMLDB_defining_an_XML_structure].
===Create ''install.xml''===


===Create install.xml===
If you never create ''install.xml'' before (your ''db/'' doesn't have ''install.xml''), click ''Create'' first. install.xml will store your database info for this module.
 
If you never create install.xml before (your db/ doesn't have install.xml), click ''Create'' first. install.xml will store your database info for this module.


===Start editing===
===Start editing===
To start edit install.xml, click ''Load'' and then ''Edit''. You need to do this every time before editing any XMLDB file.  If this is your first time, there is a default table named like your module stored.
To start editing ''install.xml'', click ''Load'' and then ''Edit''. You need to do this every time before editing any XMLDB file.  If this is your first time, there is a default table named like your module stored.


===Create new table===
===Create new table===
Line 36: Line 34:


# Click ''Edit'' on your desired table.
# Click ''Edit'' on your desired table.
# Click ''New Field'', You can now edit field information like name, comment, type, length, etc. See [https://docs.moodle.org/en/Development:XMLDB_column_types] for XMLDB column type comparing with other database system.
# Click ''New Field'', You can now edit field information like name, comment, type, length, etc. See [[XMLDB column types] for XMLDB column type comparing with other database system.
# When you're done, click ''Change''.
# When you're done, click ''Change''.


Line 46: Line 44:
# Click ''New Table from MySQL''
# Click ''New Table from MySQL''
# In ''Create Table'' box, choose your table from MySQL database. In ''After Table'' choose where would you like to add this table to (It doesn't matter, just for arrangement). Click ''Create''.
# In ''Create Table'' box, choose your table from MySQL database. In ''After Table'' choose where would you like to add this table to (It doesn't matter, just for arrangement). Click ''Create''.
# Do it again untill every table added, you are done !
# Do it again until every table added, you are done!


Note : Table name must contains only lowercase character, number, underscore and length must not exceed 28 characters. More info [https://docs.moodle.org/en/Development:XMLDB_defining_an_XML_structure#Conventions]
Note: Table name must contains only lowercase character, number, underscore and length must not exceed 28 characters. More info here: [[XMLDB defining an XML structure]]


===Other operation===
===Other operation===
It's intuitive once you use it. There are visible link for editing, moving up or down and deleting.
It's intuitive once you use it. There are visible links for editing, moving up or down and deleting.
 
==See Also==
[[XMLDB_introduction]]
 
[[XMLDB defining an XML structure]]
 
[[XMLDB_column_types]]


[[XMLDB editor]]
==See also==
*[[XMLDB editor]]
*[[XMLDB introduction]]
*[[XMLDB defining an XML structure]]
*[[XMLDB column types]]


[[Category:Using XMLDB]]
[[Category:Using XMLDB]]
[[Category:XMLDB]]
[[Category:XMLDB]]

Revision as of 19:40, 12 January 2008

Moodle1.7 Note: This page is a work in progress. It is intended for third-party module developers to help them switch to XMLDB.

What is XMLDB and how can it help you

XMLDB is a database format created to help developers from creating custom database schemas for different database system. It's like a database in XML format. Everywhere in Moodle now uses XMLDB.

In every module folder, there is a db/ folder where the database structure stored. The folder usually contains mysql.php, mysql.sql, etc. for different databases.

It means that you have to create different files for each database systems. If there are more databases you want your module to be compatible with, you have to create more files. XMLDB is intended to solve this problem. Switching to XMLDB is very easy and saves your time and effort by creating just 1 file.

Getting started

XMLDB file in module is stored in db/ folder, named install.xml. You can use a built-in XMLDB editor to create or edit any install.xml files. If you're using MySQL as your default database schemes. XMLDB editor's powerful tools allowing retrofit MySQL table to XMLDB format.

The XMLDB editor is located in administration block, go to Miscellaneous > XMLDB editor. Find your module name. Initially you won't be able to click on Create link and also can't edit your module (If Create link is clickable you can skip this step). Since you must make your module folder has write permission first (And if you haven't created db/ folder in your module folder yet, create it now). In Linux operating system, chmod your /db folder to 777 solves this. Now you're ready to create your module's XMLDB files.

Note that XMLDB editor is not a tool for creating your module, though it can help you with the database. It is a tool to manage XMLDB database file (think about phpmyadmin manages SQL database). You can create tables/fields/keys/indexes or export your existing MySQL tables to XMLDB.

Another XMLDB's advantage is you can create table comments as you want. To describe it to other developer.

XMLDB editor is really easy to work with. We strongly recommend you to play with it for a while and read XMLDB editor and about XMLDB structure XMLDB defining an XML structure.

Create install.xml

If you never create install.xml before (your db/ doesn't have install.xml), click Create first. install.xml will store your database info for this module.

Start editing

To start editing install.xml, click Load and then Edit. You need to do this every time before editing any XMLDB file. If this is your first time, there is a default table named like your module stored.

Create new table

  1. Go to New Table.
  2. Enter table name and comment, click change.

Add field in table

  1. Click Edit on your desired table.
  2. Click New Field, You can now edit field information like name, comment, type, length, etc. See [[XMLDB column types] for XMLDB column type comparing with other database system.
  3. When you're done, click Change.

Retrofit MySQL table to XMLDB

XMLDB editor can retrofit MySQL table to XMLDB. In other word, it can create XMLDB table from existing SQL. This is useful if your module database already using MySQL. First, your module has to be installed. Then follow these steps:

  1. Click New Table from MySQL
  2. In Create Table box, choose your table from MySQL database. In After Table choose where would you like to add this table to (It doesn't matter, just for arrangement). Click Create.
  3. Do it again until every table added, you are done!

Note: Table name must contains only lowercase character, number, underscore and length must not exceed 28 characters. More info here: XMLDB defining an XML structure

Other operation

It's intuitive once you use it. There are visible links for editing, moving up or down and deleting.

See also