rev 1.0.0
August-11-2004
phpBB supports several databases, and as MOD'ers we should attempt to support as many as we can. The problem is that doing so is cumbersome and that very few people know the SQL for each DB in first place. The purpose of this formatting standard is to provide a simple base that can then easily be translated to the SQL of any of the DB's. Since the predominant number of MOD Authors understand the format of MySQL, it makes sense to use it as our model.
NOTE: the standards are not written in stone and are open for discussion. However, whatever is implemented MUST be able to translate to all major database types.
NOTE: I still need help with MSSQL and Postgresql
The least you need to know:
- Can't use SELECT
- Can CREATE and DROP tables. Can ADD, MODIFY, and DROP columns
- Have as many SQL declarations and as many commands inside those declarations as you desire.
- Closely resembles MySQL. See the documentation below for actual formatting.
MOD Installations are concerned with things like creating tables and populating them. Until our tools get more sophisticated, SELECT statements are not allowed. It will be cool to have more interactive MOD scripts in the future though
What can be done?
Essentially what we are trying to do here is extend the existing phpBB DBAL. The Dev Team already has INSERT and UPDATE nailed, so just use these as you would inside phpBB code and it'll get passed "as is" to the phpBB DBAL. What we are now adding to the mix is the ability to CREATE and DROP tables and also ADD, MODIFY, and DROP columns. Although we're leery as to why you'd want to DROP something
The format is valid MySQL. However, this does not mean that all valid MySQL will work. So think of the format as being MySQL "Lite" So, unfortunately there are some restrictions and we'll cover them now.
Command Declaration:
Let's start with the basics. The command is denoted as you would probably expect:
A key issue to note is dealing with comments. Often times folks will like to leave comments stating what the SQL is doing. Unfortunately this isn't highly compatible with our system. The problem is that the comments look too much like a MOD Template command and will throw off automated tools like EasyMOD. So there is a right way and a wrong way to leave comments.standard declaration wrote:#
#-----[ SQL ]---------------------------------
#
...notice how all the lines above start with a # and are not interrupted by an empty line.the right way wrote:#
#-----[ SQL ]---------------------------------
#
# Table structure for table `phpbb_easymod`
# (-- you can have as many comment lines here as you want
# as long as they are not separated by an empty line --)
#
...notice the break between comment lines. After the break, as soon as the first # is detected, that signifies the start of the next MOD Template command.the wrong way wrote:#
#-----[ SQL ]---------------------------------
#
#
# Table structure for table `phpbb_easymod`
# (-- since there is a break between the #'s, this is not
# a valid comment and invalidated the whole command --)
#
General Layout
Sticking with the easy stuff, let's discuss how many SQL commands you can have. The answer is easy.... unlimited! You can have any number of MOD Template [ SQL ] declaration in a script, and you can have any number of valid SQL commands per [ SQL ] declaration. The only restriction is that you may only have one SQL command per line. Also each line should end with a semicolon ";"
valid multiple [ SQL ] declaration wrote:#
#-----[ SQL ]---------------------------------
#
ALTER TABLE phpbb_categories ADD icon VARCHAR(255);
INVALID multiple SQL commands on one line wrote:#
#-----[ SQL ]---------------------------------
#
ALTER TABLE phpbb_categories ADD icon VARCHAR(255); ALTER TABLE phpbb_forums ADD forum_link VARCHAR(255);
Also note that commands are (pretty much) whitespace independent. So feel free to format commands anyway you want. The follow examples are all valid:valid multiple SQL commands in declaration wrote:#
#-----[ SQL ]---------------------------------
#
ALTER TABLE phpbb_categories ADD icon VARCHAR(255);
ALTER TABLE phpbb_forums ADD forum_link VARCHAR(255);
Code: Select all
#
#-----[ SQL ]---------------------------------
#
ALTER TABLE phpbb_categories ADD icon VARCHAR(255);
ALTER TABLE phpbb_forums ADD
forum_link VARCHAR(255);
ALTER TABLE phpbb_forums
ADD forum_link VARCHAR(255);
ALTER
TABLE `phpbb_users` ADD `user_autospell`
TINYINT( 1 ) DEFAULT '1'
Command Syntax:
Whenever possible, the formatting is not case sensitive. We'll explain column definitions in the next section.
NOTE: for all commands, table_name could be enclosed tick marks like `table_name`. Generally, simple SQL can be cut and pasted from a phpMyAdmin export and used directly as it appears.
INSERT and UPDATE:
We'll start with the easy ones first. For UPDATE and INSERT, the normal phpBB DBAL is used. Simpy use whatever is valid for phpBB with just one exception. For INSERT, it is expected that the 3rd word is the table name, and for UPDATE it is expected that the 2nd word is the table name. For example, the following is required...
Create Table:INSERT INTO table_name ...rest of command...
UPDATE table_name ...rest of command...
(reference) You may define as many columns as you wish when creating a table. See the column definition section for a complete explanation. Not implemented: "TEMPORARY", "IF NOT EXISTS", "LIKE", "INDEX", "CONSTRAINT", "UNIQUE", "FOREIGN KEY", "FULLTEXT", "SPATIAL", "CHECK", table_options, select_statement
Drop Table:CREATE TABLE table_name ( column_definition, ... ) ;
(reference) Dropping only one table at a time is supported! Not implemented: "TEMPORARY", "IF EXISTS", "RESTRICT", "CASCADE"
Add Column:DROP TABLE table_name ;
(reference) You may add multiple columns with one ALTER statement. The "COLUMN" reserved word cannot be used. Parenthesis "()" cannot be used around the column definition. You can use KEY, but this might change to INDEX instead. Not implemented: "COLUMN", "FIRST", "AFTER", "INDEX", "CONSTRAINT", "UNIQUE", "FULLTEXT", "SPATIAL", table_options
Modify Column:ALTER TABLE table_name ADD column_definition, ... ;
(reference) You may modify only one column at a time (true of MySQL also). The "COLUMN" reserved word cannot be used. "ALTER" and "CHANGE" cannot be used in place of "MODIFY". Not implemented: "COLUMN", "ALTER", "CHANGE", "FIRST", "AFTER"
Drop Column:ALTER TABLE table_name MODIFY column_definition ;
(reference) You may drop only one column at a time (true of MySQL also). Not implemented: "PRIMARY KEY", "INDEX", "FOREIGN KEY"
ALTER TABLE table_name DROP column_name ;
Column Definition:
See the types section for a complete explanation. The column_name can be enclosed in tick marks like `column_name` just as phpMyAdmin formats an export. "NULL/NOT NULL" and "DEFAULT" are optional, but will be set to defaults of "NULL" and either "DEFAULT ''" or "DEFAULT '0'" depending on the type. "AUTO_INCREMENT" is optional but must appear as the last parameter of the definition. Not implemented: "COMMENT", reference_definition
Keys, must appear on there own line and must not appear prior to the column definition. Important Note: keys are not yet implemented correctly yet and will need an overhaul.column definition wrote:column_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
Valid column definitions...key definition wrote:[PRIMARY] KEY col_name (col_name)
Code: Select all
SSN int(11) unsigned not null default '0' auto_increment
SSN int(11) UNSIGNED NOT NULL DEFAULT '0' AUTO_INCREMENT
SSN int(11) UNSIGNED DEFAULT '0' NOT NULL AUTO_INCREMENT
SSN int(11) UNSIGNED AUTO_INCREMENT
'SSN' int(11) UNSIGNED AUTO_INCREMENT
Code: Select all
SSN int(11) NOT NULL UNSIGNED DEFAULT '0' AUTO_INCREMENT
SSN int(11) NOT NULL DEFAULT '0' UNSIGNED AUTO_INCREMENT
SSN int(11) AUTO_INCREMENT UNSIGNED NOT NULL DEFAULT '0'
SSN int(11) AUTO_INCREMENT UNSIGNED
DOUBLE, FLOAT, and DECIMAL are really implemented correctly yet. Unfortunately, BLOBs are not implemented yet, but probably should be. Length and UNSIGNED are optional when appropriate. Not Implemented: "ZEROFILL", "BINARY", "ASCII", "UNICODE", spatial_type. Types not implemented: "REAL", "DATE", "TIME", "TIMESTAMP", "DATETIME", "NUMERIC", "TINYBLOB", "BLOB", "MEDIUMBLOB", "LONGBLOB", "ENUM", "SET"
-NuttzyTINYINT[(length)] [UNSIGNED]
SMALLINT[(length)] [UNSIGNED]
MEDIUMINT[(length)] [UNSIGNED]
INT[(length)] [UNSIGNED]
INTEGER[(length)] [UNSIGNED]
BIGINT[(length)] [UNSIGNED]
DOUBLE[(length,decimals)] [UNSIGNED]
FLOAT[(length,decimals)] [UNSIGNED]
DECIMAL(length,decimals) [UNSIGNED]
CHAR(length)
VARCHAR(length)
TINYTEXT
TEXT
MEDIUMTEXT
LONGTEXT