Copyright (C) 2010 All Rights Reserved. Contributor(s): Mark J Crane */ include "root.php"; //define the follow me class class schema { public $db; public $apps; public $db_type; public $result; //get the list of installed apps from the core and mod directories public function __construct() { $config_list = glob($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH . "/*/*/app_config.php"); $x=0; foreach ($config_list as &$config_path) { include($config_path); $x++; } $this->apps = $apps; } //create the database schema public function sql() { $sql = ''; $sql_schema = ''; foreach ($this->apps as $app) { if (count($app['db'])) { foreach ($app['db'] as $row) { //create the sql string $table_name = $row['table']; $sql = "CREATE TABLE " . $row['table'] . " (\n"; $field_count = 0; foreach ($row['fields'] as $field) { if ($field['deprecated'] == "true") { //skip this field } else { if ($field_count > 0 ) { $sql .= ",\n"; } if (is_array($field['name'])) { $sql .= $field['name']['text']." "; } else { $sql .= $field['name']." "; } if (is_array($field['type'])) { $sql .= $field['type'][$this->db_type]; } else { $sql .= $field['type']; } if ($field['key']['type'] == "primary") { $sql .= " PRIMARY KEY"; } if ($field['key']['type'] == "foreign") { if ($this->db_type == "pgsql") { //$sql .= " references ".$field['key']['reference']['table']."(".$field['key']['reference']['field'].")"; } if ($this->db_type == "sqlite") { //$sql .= " references ".$field['key']['reference']['table']."(".$field['key']['reference']['field'].")"; } if ($this->db_type == "mysql") { //$sql .= " references ".$field['key']['reference']['table']."(".$field['key']['reference']['field'].")"; } } $field_count++; } } if ($this->db_type == "mysql") { $sql .= ") ENGINE=INNODB;"; } else { $sql .= ");"; } $this->result['sql'][] = $sql; unset($sql); } } } } //create the database schema public function exec() { foreach ($this->result['sql'] as $sql) { //start the sql transaction $this->db->beginTransaction(); //execute the sql query try { $this->db->query($sql); } catch (PDOException $error) { echo "error: " . $error->getMessage() . " sql: $sql
"; } //complete the transaction $this->db->commit(); } } }