Completed
Push — dev ( 94208d...ada7da )
by James Ekow Abaka
02:35
created

InformationSchemaInfo::columnNulable()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 1
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/* 
4
 * The MIT License
5
 *
6
 * Copyright 2015 ekow.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace yentu\tests\schema_info;
28
29
class InformationSchemaInfo extends \yentu\tests\SchemaInfo
30
{
31
    public function foreignKeyExists($table) 
32
    {
33
        if(!isset($table['schema']))
34
        {
35
            $table['schema'] = $GLOBALS['DEFAULT_SCHEMA'];
36
        }
37
        
38
        $response = $this->getPDO()->query(
39
            sprintf(
40
                "SELECT count(*) as c FROM information_schema.table_constraints WHERE table_name = '%s' and table_schema = '%s' and constraint_name = '%s' and constraint_type='FOREIGN KEY'",
41
                $table['table'], 
42
                $table['schema'],
43
                $table['name']
44
            )
45
        )->fetchAll(\PDO::FETCH_ASSOC);
46
        
47
        return $response[0]['c'] == 1;
48
    }
49
50
    public function columnExists($column) 
51
    {
52
        $response = $this->getPDO()->query(
53
            sprintf(
54
                "SELECT count(*) as c FROM information_schema.columns  where table_name = '%s' and table_schema = '%s' and column_name = '%s'",
55
                $this->table['table'], 
56
                $this->table['schema'],
57
                $column
58
            )
59
        )->fetchAll(\PDO::FETCH_ASSOC);
60
        return $response[0]['c'] == 1;
61
    }
62
63
    public function schemaExists($table) {
64
        
65
    }
66
67
    public function tableExists($table) 
68
    {
69
        if(is_string($table))
70
        {
71
            $table = array(
72
                'table' => $table,
73
                'schema' => $GLOBALS['DEFAULT_SCHEMA']
74
            );
75
        }
76
        
77
        $response = $this->getPDO()->query(
78
            sprintf(
79
                "SELECT count(*) as c FROM information_schema.tables  where table_name = '%s' and table_schema = '%s'",
80
                $table['table'], 
81
                $table['schema']
82
            )
83
        );      
84
        
85
        $count = $response->fetchAll(\PDO::FETCH_ASSOC);
86
        return $count[0]['c'] == 1;
87
    }
88
89
    public function columnNulable($column, $nullability) 
90
    {
91
        $response = $this->getPDO()->query(
92
            sprintf("SELECT count(*) as c FROM information_schema.columns  where table_name = '%s' and table_schema = '%s' and column_name = '%s' and is_nullable = '%s'",
93
                $this->table['table'], 
94
                $this->table['schema'],
95
                $column,
96
                $nullability ? 'YES' : 'NO'
97
            )
98
        )->fetchAll(\PDO::FETCH_ASSOC);
99
        return $response[0]['c'] == 1;
100
    }
101
}