Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
14.29% covered (danger)
14.29%
2 / 14
12.50% covered (danger)
12.50%
1 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
common_persistence_sql_SchemaManager
14.29% covered (danger)
14.29%
2 / 14
12.50% covered (danger)
12.50%
1 / 8
48.30
0.00% covered (danger)
0.00%
0 / 1
 setAttribute
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSchemaManager
n/a
0 / 0
n/a
0 / 0
0
 getColumnNames
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createSchema
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addColumnToTable
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getTables
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTableIndexes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createIndex
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getDbalSchemaManager
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getIndexAlreadyExistsErrorCode
n/a
0 / 0
n/a
0 / 0
0
 getColumnNotFoundErrorCode
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3/**
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; under version 2
7 * of the License (non-upgradable).
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 *
18 * Copyright (c) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 * @author "Lionel Lecaque, <lionel@taotesting.com>"
21 * @license GPLv2
22 * @package generis
23
24 *
25 */
26abstract class common_persistence_sql_SchemaManager
27{
28    /**
29     * HACK to set "PDO::MYSQL_ATTR_MAX_BUFFER_SIZE" for fileupload
30     *
31     * @author Lionel Lecaque, lionel@taotesting.com
32     * @param unknown $name
33     * @param unknown $value
34     * @throws core_kernel_persistence_Exception
35     */
36    public function setAttribute($name, $value)
37    {
38        throw new core_kernel_persistence_Exception('setattribute only availlable for mysql pdo implementation');
39    }
40
41    /**
42     * @author "Lionel Lecaque, <lionel@taotesting.com>"
43     * @return Doctrine\DBAL\Schema\AbstractSchemaManager;
44     */
45    abstract protected function getSchemaManager();
46
47    /**
48     * Returns the column names of a given table
49     *
50     * @access public
51     * @author Jerome Bogaerts, <jerome@taotesting.com>
52     * @param  string table
53     * @return array
54     */
55    public function getColumnNames($table)
56    {
57        return $this->getSchemaManager()->listTableColumns($table);
58    }
59
60
61
62    /**
63     * @author "Lionel Lecaque, <lionel@taotesting.com>"
64     */
65    public function createSchema()
66    {
67        $tables = $this->getSchemaManager()->listTables();
68        return new \Doctrine\DBAL\Schema\Schema($tables, [], $this->getSchemaManager()->createSchemaConfig());
69    }
70
71
72    /**
73     * @author "Lionel Lecaque, <lionel@taotesting.com>"
74     * @param unknown $schema
75     * @param unknown $tblname
76     * @param unknown $column
77     * @return unknown
78     */
79    public function addColumnToTable($schema, $tblname, $column)
80    {
81        $newSchema = clone $schema;
82        $table = $newSchema->getTable($tblname);
83        $table->addColumn($column, "text", ["notnull" => false]);
84        return $newSchema;
85    }
86
87    /**
88     * Returns an array of string containting the names of the tables contained
89     * the currently selected database in the storage engine.
90     *
91     * @abstract
92     * @access public
93     * @author Jerome Bogaerts, <jerome@taotesting.com>
94     * @return array
95     */
96    public function getTables()
97    {
98        return $this->getSchemaManager()->listTableNames();
99    }
100
101    /**
102     * @author "Lionel Lecaque, <lionel@taotesting.com>"
103     * @param string $tableName
104     */
105    public function getTableIndexes($tableName)
106    {
107        return $this->getSchemaManager()->listTableIndexes($tableName);
108    }
109
110     /**
111     * Create an index on a given table and selected columns. This method throws
112     * in case of error.
113     *
114     * @abstract
115     * @access public
116     * @author Jerome Bogaerts, <jerome@taotesting.com>
117     * @param  string indexName The name of the index to create.
118     * @param  string tableName A table name
119     * @param  array columns
120     * @return void
121     */
122    public function createIndex($indexName, $tableName, $columns)
123    {
124        $index = new \Doctrine\DBAL\Schema\Index($indexName, array_keys($columns));
125        $table = new \Doctrine\DBAL\Schema\Table($tableName);
126        $this->getSchemaManager()->createIndex($index, $table);
127    }
128
129    /**
130     * @return Doctrine\DBAL\Schema\AbstractSchemaManager;
131     */
132    public function getDbalSchemaManager()
133    {
134        return $this->getSchemaManager();
135    }
136
137    /**
138     * The error code returned by PDO in when an Index already exists in a table
139     * a given DBMS implementation.
140     * @access public
141     * @author Jerome Bogaerts, <jerome@taotesting.com>
142     * @return string
143     */
144    abstract public function getIndexAlreadyExistsErrorCode();
145
146    /**
147     *
148     * @author Lionel Lecaque, lionel@taotesting.com
149     */
150    abstract public function getColumnNotFoundErrorCode();
151}