Ejemplo n.º 1
0
	/**
	 * @covers Bedrock\Model::desanitize
	 *
	 * @return void
	 */
	public function testDesanitize() {
		// Setup
		$sanitizedValue = 'it\\\'s it';
		$rawValue = \Bedrock\Model::desanitize($sanitizedValue);

		// Assertions
		$this->assertEquals('it\'s it', $rawValue);
	}
Ejemplo n.º 2
0
	/**
	 * Initializes a resultset collection.
	 * 
	 * @param array $records an array of records to add to the ResultSet
	 */
	public function __construct($records = array()) {
		try {
			if($records) {
				foreach($records as $record) {
					$this->add($record);
				}
			}
			
			parent::__construct();
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\ResultSet\Exception('A result set cound not be initialized.');
		}
	}
Ejemplo n.º 3
0
	/**
	 * Initializes the table object.
	 * 
	 * @param array $tableConfig the table configuration
	 * @param \Bedrock\Model\Database $database the Database object to use
	 */
	public function __construct($tableConfig = array(), $database = NULL) {
		try {
			parent::__construct($database);
			
			$this->_name = $tableConfig['name'];
			$this->_properties = $tableConfig['properties'];
			// @todo handle column details in tableConfig
			$this->_state = self::STATE_NEW;
		}
		catch(\PDOException $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Exception('The table object could not be initialized.');
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Exception('The table object could not be initialized.');
		}
	}
Ejemplo n.º 4
0
	/**
	 * Initializes the record object.
	 * 
	 * @param \PDO $connection the database connection to use
	 * @param \Bedrock\Model\Table the record's corresponding table
	 * @param array $values the record's values
	 */
	public function __construct($table, $values = array(), $database = NULL) {
		try {
			parent::__construct($database);
			
			$this->_table = new \Bedrock\Model\Table(array('name' => $table));
			$this->_table->load();
			$this->_columns = $this->_table->getColumns();
			$this->_state = self::STATE_UNCHANGED;
			
			foreach($this->_columns as $column) {
				switch($column->type) {
					case \Bedrock\Model\Column::FIELD_TYPE_BOOL:
						if(is_bool($values[$column->name])) {
							$this->_data[$column->name] = $values[$column->name];
						}
						else {
							$this->_data[$column->name] = ($values[$column->name] == 1 ? true : false);
						}
						break;
						
					default:
						$this->_data[$column->name] = $values[$column->name];
						break;
				}
				
				if($column->primary_key) {
					$this->_key_primary = $column;
					
					if(!$values[$column->name]) {
						$this->_state = self::STATE_NEW;
					}
				}
				
				// Foreign Key Reference?
				
			}
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Record\Exception('A record object could not be initialized.');
		}
	}
Ejemplo n.º 5
0
	/**
	 * Used to initialize a query. See the static from() method for public
	 * initialization.
	 *
	 * @param string $targetName the name of the table/view/procedure to be queried
	 * @param integer $targetType the type of target being queried
	 * @param \PDO $database an optional database connection to use, the default will be used otherwise
	 */
	public function __construct($targetName = '', $targetType = self::TARGET_TABLE, $database = NULL) {
		try {
			parent::__construct($database);
			
			switch($targetType) {
				default:
				case self::TARGET_TABLE:
				case self::TARGET_VIEW:
					$this->_target = self::TARGET_TABLE;
					$this->_table = $targetName;
					$this->_query['from'] = 'SELECT * FROM ' . self::sanitize($targetName);
					break;
					
				case self::TARGET_PROCEDURE:
					$this->_target = self::TARGET_PROCEDURE;
					$this->_procedure = $targetName;
					break;
			}
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Query\Exception('The query could not be initialized for "' . $targetName . '"');
		}
	}
Ejemplo n.º 6
0
	/**
	 * Exports all table schemas to the specified location.
	 *
	 * @param string $exportLocation the file to write
	 * @param string $exportType the format to use
	 */
	public function exportTableSchemas($exportLocation, $exportType = \Bedrock\Model::FORMAT_SQL) {
		try {
			// Setup
			$fileContents = '';
			
			\Bedrock\Common\Logger::info('Exporting table schemas for database "' . $this->_name . '"...');

			// Build String
			$this->load();

			switch($exportType) {
				case \Bedrock\Model::FORMAT_SQL:
					foreach($this->_tables as $table) {
						\Bedrock\Common\Logger::info('Exporting schema of table "' . $table->getProperty('name') . '"...');
						$fileContents .= $table->schemaToString($exportType) . ';' . \Bedrock\Common::TXT_NEWLINE;
					}
					break;

				case \Bedrock\Model::FORMAT_XML:
					foreach($this->_tables as $table) {
						$fileContents .= $table->schemaToString($exportType);
						$fileContents = str_replace('<?xml version="1.0"?>' . \Bedrock\Common::TXT_NEWLINE, '', $fileContents);
					}

					$fileContents = '<tables>' . $fileContents . '</tables>';
					$fileContents = \DOMDocument::loadXML($fileContents);
					$fileContents->formatOutput = true;
					$fileContents->preserveWhitespace = false;
					$fileContents = $fileContents->saveXML();
					break;

				case \Bedrock\Model::FORMAT_YAML:
					$yaml = new \Bedrock\Common\Data\YAML();
					$tables = array();

					foreach($this->_tables as $table) {
						$table = new \Bedrock\Common\Data\YAML($table->schemaToString($exportType));
						$tables[] = $table->table;
					}

					$yaml->tables = $tables;

					$fileContents = (string) $yaml;
					break;

				case \Bedrock\Model::FORMAT_CSV:
					$first = true;

					foreach($this->_tables as $table) {
						$tableCsv = $table->schemaToString($exportType);

						if(!$first) {
							$tableCsv = substr($tableCsv, strpos($tableCsv, \Bedrock\Common::TXT_NEWLINE) + 1);
						}

						$fileContents .= $tableCsv;
						$first = false;
					}
					break;
			}
			
			\Bedrock\Model::writeFile($exportLocation, $fileContents);
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Exception('Table schema export failed.');
		}
	}