1 |
<?php
|
2 |
/*
|
3 |
* $Id: DataDict.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $
|
4 |
*
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
16 |
*
|
17 |
* This software consists of voluntary contributions made by many individuals
|
18 |
* and is licensed under the LGPL. For more information, see
|
19 |
* <http://www.phpdoctrine.com>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_DataDict
|
24 |
*
|
25 |
* @package Doctrine
|
26 |
* @subpackage DataDict
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
28 |
* @link www.phpdoctrine.com
|
29 |
* @since 1.0
|
30 |
* @version $Revision: 2963 $
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
32 |
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
|
33 |
*/
|
34 |
class Doctrine_DataDict extends Doctrine_Connection_Module
|
35 |
{
|
36 |
/**
|
37 |
* Obtain an array of changes that may need to applied
|
38 |
*
|
39 |
* @param array $current new definition
|
40 |
* @param array $previous old definition
|
41 |
* @return array containing all changes that will need to be applied
|
42 |
*/
|
43 |
public function compareDefinition($current, $previous)
|
44 |
{
|
45 |
$type = !empty($current['type']) ? $current['type'] : null;
|
46 |
|
47 |
if ( ! method_exists($this, "_compare{$type}Definition")) {
|
48 |
throw new Doctrine_DataDict_Exception('type "'.$current['type'].'" is not yet supported');
|
49 |
}
|
50 |
|
51 |
if (empty($previous['type']) || $previous['type'] != $type) {
|
52 |
return $current;
|
53 |
}
|
54 |
|
55 |
$change = $this->{"_compare{$type}Definition"}($current, $previous);
|
56 |
|
57 |
if ($previous['type'] != $type) {
|
58 |
$change['type'] = true;
|
59 |
}
|
60 |
|
61 |
$previous_notnull = !empty($previous['notnull']) ? $previous['notnull'] : false;
|
62 |
$notnull = !empty($current['notnull']) ? $current['notnull'] : false;
|
63 |
if ($previous_notnull != $notnull) {
|
64 |
$change['notnull'] = true;
|
65 |
}
|
66 |
|
67 |
$previous_default = array_key_exists('default', $previous) ? $previous['default'] :
|
68 |
($previous_notnull ? '' : null);
|
69 |
$default = array_key_exists('default', $current) ? $current['default'] :
|
70 |
($notnull ? '' : null);
|
71 |
if ($previous_default !== $default) {
|
72 |
$change['default'] = true;
|
73 |
}
|
74 |
|
75 |
return $change;
|
76 |
}
|
77 |
|
78 |
/**
|
79 |
* parseBoolean
|
80 |
* parses a literal boolean value and returns
|
81 |
* proper sql equivalent
|
82 |
*
|
83 |
* @param string $value boolean value to be parsed
|
84 |
* @return string parsed boolean value
|
85 |
*/
|
86 |
public function parseBoolean($value)
|
87 |
{
|
88 |
// parse booleans
|
89 |
if ($value == 'true') {
|
90 |
$value = 1;
|
91 |
} elseif ($value == 'false') {
|
92 |
$value = 0;
|
93 |
}
|
94 |
return $value;
|
95 |
}
|
96 |
} |