<?php
/**
 * This file is part of TheWebMind 3rd generation.
 * 
 * @author Felipe Nascimento de Moura <felipenmoura@gmail.com>
 * @license licenses/mind3rd.license
 */

/**
 * Description of pgsql
 *
 * @author Felipe Nascimento de Moura <felipenmoura@gmail.com>
 * @package DBMS
 */
class pgsql implements DBMS{
	
	/**
	 * Returns the template to be used to set a default value to a column.
	 * @return string
	 */
	public function createDefault()
	{
		return "<object>DEFAULT</object> <value><defaultvalue></value>";
	}
	
	/**
	 * References another table straight from a column representation.
	 * NOTE: use this method with a TRUE in mustSort method, otherwise a refered table
	 * may no be created yet, and it will result in an error.
	 * If the currend DBMS allows you to alter the table and add a foreign key in another querym
	 * use the createFK method, and you may return false in the mustSort method(it will increase
	 * the performance). In this case, this method(createReferences) may return an empty string.
	 * 
	 * @return string
	 */
	public function createReferences()
	{
		return "";
	}
	
	/**
	 * A template to create properties into a table.
	 * This template will be used to create each column into a table.
	 * 
	 * @return string
	 */
	public function property()
	{
		return "<property><propertyname></property> <propertytype><propertysize> <propertydetails>";
	}
	
	/**
	 * Template to specify how to CHECK value in the database IF the DBMS supports it.
	 * If the DBMS does not support it, just return an empty string.
	 * 
	 * @return string
	 */
	public function createOptionsCheck()
	{
		return "<object>CHECK</object> (<options>)";
	}
	
	/**
	 * The not null keyword.
	 * 
	 * @return string
	 */
	public function notNullDefinition()
	{
		return "<object>NOT NULL</object>";
	}
	
	/**
	 * The autoincremental properties type.
	 * In some DBMSs, the auto increment column has a different type(as serial, in postgres),
	 * for those, return here the respective type, otherwise, return "int".
	 * 
	 * @return string
	 */
	public function autoIncrementType()
	{
		return "serial";
	}
	
	/**
	 * Returns the unique keyword.
	 * 
	 * @return string
	 */
	public function createUnique()
	{
		return "<object>UNIQUE</object>";
	}
	
	/**
	 * Returns a header for the SQL file.
	 * You can use it to set an author, data/time, links, license or anything that might
	 * be useful.
	 * Please, do NOT remove the "Generated by theWebMind project(mind3rd release)" message.
	 * 
	 * @return string 
	 */
	public function getHeader()
	{
		$author= substr($_SESSION['login'], 0, 30);
		$author= str_pad($author, 30);
		$header= "<comment>  -".str_pad('', 60, '-')."----
  --| ".str_pad("Generated by theWebMind project(mind3rd release)", 58)."|--
  --| ".str_pad("DBMS: ".str_pad('PostgreSQL', 34)."Date: ". date('d/m/Y'), 58)."|--
  --| ".str_pad("Generator Author: Felipe Nascimento de Moura", 58)."|--
  --| ".str_pad("Source Author: ".$author, 58)."|--
";
		$header.= "  -".str_pad('', 60, '-')."----
  --".str_pad(strtoupper(Mind::$currentProject['name']).
					 " (version ".Mind::$currentProject['version'].")",
		  61, ' ', STR_PAD_BOTH)."--
  --".str_pad('', 60, '-')."---</comment>
";
		return $header;
	}
	
	/**
	 * Template for altering a table, setting a foreign key.
	 * 
	 * @return string
	 */
	public function createFK()
	{
		return "
<keyword>ALTER </keyword><object>TABLE</object> <element><tablename></element>
  <keyword>ADD</keyword> <object>CONSTRAINT</object> <constraintname>
  <object>FOREIGN KEY</object> (<element><propertyname></element>) <keyword>REFERENCES </keyword>
<referencetablename>(<referencecolumnname>);
";
	}
	
	/**
	 * Returns a template specifying how specify a primary key for a table.
	 * 
	 * @return string
	 */
	public function createPrimaryKeys()
	{
		return "
    <object>CONSTRAINT</object> <element><fkname></element> <object>PRIMARY KEY</object> (<element><propertiesnames></element>)
";
	}
	
	/**
	 * Returns a teplate to alter a table, setting a new Primary Key.
	 * @return string
	 */
	public function createPK()
	{
		return "
<keyword>ALTER</keyword> <object>TABLE</object> <tablename>
  <keyword>ADD</keyword> <object>PRIMARY KEY</object> (<propertiesnames>);
";
	}
	
	/**
	 * Template to set a column as auto_increment.
	 * If the DBMS does not have a different type of datafor autoincremental columns,
	 * you may set here the keyword to do so, such as "AUTO_INCREMENT"
	 * 
	 * @return string
	 */
	public function createAutoIncrement()
	{
		return "";
	}
	
	/**
	 * Gets a template of how to create a table.
	 * The properties and primary keys will be treated by the system.
	 * 
	 * @return sting
	 */
	public function createTable()
	{
		return "
<keyword>CREATE </keyword><object>TABLE</object> <element class='mindTableName'><tablename></element>
(
    <properties>
    <primarykeys>
);
";
	}
	
	/**
	 * Sort the tables.
	 * If true is returned, the system will run an algorythm to sort the tables
	 * in order to allow the references to work. In these cases, the performance MAY
	 * be lower, so, if the DBMS supports an ALTER TABLE command to add a foreign 
	 * key in another moment, you should use that.
	 * 
	 * @return boolean
	 */
	public function mustSort()
	{
		return false;
	}

    public function getAttributes($tableName)
    {
        "select * from information_schema.columns where table_name='father'";
        "select * from information_schema.referential_constraints";
        // for not null or check
        "select * from information_schema.check_constraints where substring(constraint_schema, 0,3) <> 'pg' and constraint_schema <> 'information_schema'";
    }
    
    public function getAllTables()
    {
        return "select table_catalog||'.'||table_name as table_name from information_schema.tables where table_type = 'BASE TABLE' and substring(table_schema, 0,3) <> 'pg' and table_schema <> 'information_schema';";
    }
    
	/**
	 * Method which will return the required string.
	 * This method is generic and may be kept just as it is. Although, if
	 * you may change anything here, just be sure to return the templates
	 * as a pattern.
	 * 
	 * @param string $keyword
	 * @return string/boolean
	 */
	public function getModel($keyword)
	{
		if(method_exists($this, $keyword))
			return $this->$keyword();
		return false;
	}
}