<?php

// $Id: log_to_file.install,v 1.1.2.2 2008/02/11 14:56:27 ianbezanson Exp $

/**********************************************************************
 * Name: log_to_file.install
 * Author: Ian Bezanson
 * Date: 2008-02-10
 *
 * Description: Database Installation File for log_to_file
 *              Drupal Module.
 *********************************************************************/


/**
 * log_to_file module schema
 */
function log_to_file_install() {
	switch ($GLOBALS['db_type']) {
		case 'mysql':
		case 'mysqli':
			//Create Log Level Table
			db_query('
				CREATE TABLE {log_level} (
					log_level_id int(6) NOT NULL auto_increment,
					log_level int(5) NOT NULL,
					log_level_long_name varchar(255) NOT NULL,
					log_level_short_name varchar(5) NOT NULL,
					log_level_description varchar(255) default NULL,
					PRIMARY KEY (log_level_id),
					KEY `log_level` (log_level)
				) ENGINE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */'
			);
			//Insert Log Level `FATAL` into Log Level Table
			db_query('
				INSERT INTO
					{log_level}
				(
					`log_level`,
					`log_level_long_name`,
					`log_level_short_name`,
					`log_level_description`
				)
				VALUES
				(
					0,
					\'LOG_LEVEL_FATAL\',
					\'FATAL\',
					\'The FATAL level designates very severe error events that will presumably lead the application to abort.\'
				)
			');
			//Insert Log Level `ERROR` into Log Level Table
			db_query('
				INSERT INTO
					{log_level}
				(
					`log_level`,
					`log_level_long_name`,
					`log_level_short_name`,
					`log_level_description`
				)
				VALUES
				(
					1,
					\'LOG_LEVEL_ERROR\',
					\'ERROR\',
					\'The ERROR level designates error events that might still allow the application to continue running.\'
				)
			');
			//Insert Log Level `WARN` into Log Level Table
			db_query('
				INSERT INTO
					{log_level}
				(
					`log_level`,
					`log_level_long_name`,
					`log_level_short_name`,
					`log_level_description`
				)
				VALUES
				(
					2,
					\'LOG_LEVEL_WARN\',
					\'WARN\',
					\'The WARN level designates potentially harmful situations.\'
				)
			');
			//Insert Log Level `INFO` into Log Level Table
			db_query('
				INSERT INTO
					{log_level}
				(
					`log_level`,
					`log_level_long_name`,
					`log_level_short_name`,
					`log_level_description`
				)
				VALUES
				(
					3,
					\'LOG_LEVEL_INFO\',
					\'INFO\',
					\'The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.\'
				)
			');
			//Insert Log Level `DEBUG` into Log Level Table
			db_query('
				INSERT INTO
					{log_level}
				(
					`log_level`,
					`log_level_long_name`,
					`log_level_short_name`,
					`log_level_description`
				)
				VALUES
				(
					4,
					\'LOG_LEVEL_DEBUG\',
					\'DEBUG\',
					\'The DEBUG Level designates fine-grained informational events that are most useful to debug an application.\'
				)
			');
			//Insert Log Level `TRACE` into Log Level Table
			db_query('
				INSERT INTO
					{log_level}
				(
					`log_level`,
					`log_level_long_name`,
					`log_level_short_name`,
					`log_level_description`
				)
				VALUES
				(
					5,
					\'LOG_LEVEL_TRACE\',
					\'TRACE\',
					\'The TRACE Level designates finer-grained informational events than the DEBUG.\'
				)
			');
			//Create Log Type Table
			db_query('
				CREATE TABLE {log_type} (
					log_type_id int(6) NOT NULL auto_increment,
					log_type varchar(255),
					log_type_description varchar(255) default NULL,
					PRIMARY KEY (log_type_id),
					KEY `log_type` (log_type)
				) ENGINE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */'
			);
			//Insert Log Type `DRUPAL_BASE` into Log Type Table
			db_query('
				INSERT INTO
					{log_type}
				(
					`log_type`,
					`log_type_description`
				)
				VALUES
				(
					\'DRUPAL_BASE\',
					\'The default log type. Can be used for anything and everything.\'
				)
			');
			break;
		case 'pgsql':
			//Create Log Level Table
			db_query('
				CREATE TABLE {log_level} (
					log_level_id INTEGER,
					log_level INTEGER,
					log_level_long_name VARCHAR NOT NULL,
					log_level_short_name VARCHAR NOT NULL,
					log_level_description VARCHAR NULL,
					PRIMARY KEY (log_level_id)
				)
			');
	                db_query("ALTER TABLE {log_level} ADD CONSTRAINT log_level_key UNIQUE (log_level)");
                        //Insert Log Level `FATAL` into Log Level Table
                        db_query("
                                INSERT INTO
                                        {log_level}
                                (
					log_level_id,
                                        log_level,
                                        log_level_long_name,
                                        log_level_short_name,
                                        log_level_description
                                )
                                VALUES
                                (
					0,
                                        0,
                                        'LOG_LEVEL_FATAL',
                                        'FATAL',
                                        'The FATAL level designates very severe'
                                )
                        ");
                        //Insert Log Level `ERROR` into Log Level Table
                        db_query("
                                INSERT INTO
                                        {log_level}
                                (
					log_level_id,
                                        log_level,
                                        log_level_long_name,
                                        log_level_short_name,
                                        log_level_description
                                )
                                VALUES
                                (
					1,
                                        1,
                                        'LOG_LEVEL_ERROR',
                                        'ERROR',
                                        'The ERROR level designates error events that might still allow the application'
                                )
                        ");
                        //Insert Log Level `WARN` into Log Level Table
                        db_query("
                                INSERT INTO
                                        {log_level}
                                (
					log_level_id,
                                        log_level,
                                        log_level_long_name,
                                        log_level_short_name,
                                        log_level_description
                                )
                                VALUES
                                (
					2,
                                        2,
                                        'LOG_LEVEL_WARN',
                                        'WARN',
                                        'The WARN level designates potentially harmful situations.'
                                )
                        ");
                        //Insert Log Level `INFO` into Log Level Table
                        db_query("
                                INSERT INTO
                                        {log_level}
                                (
					log_level_id,
                                        log_level,
                                        log_level_long_name,
                                        log_level_short_name,
                                        log_level_description
                                )
                                VALUES
                                (
					3,
                                        3,
                                        'LOG_LEVEL_INFO',
                                        'INFO',
                                        'The INFO level designates informational messages that highlight the progress of the application at coarse-grained level.')
                        ");
                        //Insert Log Level `DEBUG` into Log Level Table
                        db_query("
                                INSERT INTO
                                        {log_level}
                                (
					log_level_id,
                                        log_level,
                                        log_level_long_name,
                                        log_level_short_name,
                                        log_level_description
                                )
                                VALUES
                                (
					4,
                                        4,
                                        'LOG_LEVEL_DEBUG',
                                        'DEBUG',
                                        'The DEBUG Level designates fine-grained informational events that are most useful to debug an application.'
                                )
                        ");
                //Insert Log Level `TRACE` into Log Level Table
                db_query("
                        INSERT INTO
                                {log_level}
                        (
				log_level_id,
                                log_level,
                                log_level_long_name,
                                log_level_short_name,
                                log_level_description
                        )
                        VALUES
                        (
				5,
                                5,
                                'LOG_LEVEL_TRACE',
                                'TRACE',
                                'The TRACE Level designates finer-grained informational events than the DEBUG.'
                        )
                ");
                        db_query("
                                CREATE TABLE {log_type} (
                                        log_type_id INTEGER NOT NULL,
                                        log_type VARCHAR,
                                        log_type_description VARCHAR default NULL,
                                        PRIMARY KEY (log_type_id)
                                )
                        ");
                        //Insert Log Type `DRUPAL_BASE` into Log Type Table
                        db_query("
                                INSERT INTO
                                        {log_type}
                                (
					log_type_id,
                                        log_type,
                                        log_type_description
                                )
                                VALUES
                                (
					0,
                                        'DRUPAL_BASE',
                                        'The default log type. Can be used for anything and everything.'
                                )
                        ");
			db_query("ALTER TABLE {log_type} ADD CONSTRAINT log_type_key UNIQUE (log_type)");	
			break;
			}
			drupal_set_message(t('Drupal Log To File module tables have been created.'));
}

/**
 * log_to_file module database uninstall
 */
function log_to_file_uninstall() {
	switch ($GLOBALS['db_type']) {
		case 'mysql':
		case 'mysqli':
			db_query("DROP TABLE IF EXISTS {log_level};");
			db_query("DROP TABLE IF EXISTS {log_type};");
			break;
		case 'pgsql':
			db_query("DROP TABLE {log_level}");
			db_query("DROP TABLE {log_type}");
	}
	drupal_set_message(t('Drupal Log To File module tables have been deleted.'));
}

function log_to_file_update_1() {
	switch ($GLOBALS['db_type']) {
	        case 'mysql':
		case 'mysqli':
	 	case 'pgsql':
		db_query("ALTER TABLE {log_level} ADD CONSTRAINT log_level_key UNIQUE (log_level)");
		break;
		}
}
