Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 41
TDb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 5
462
0.00% covered (danger)
0.00%
0 / 40
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 __clone
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 setDbType
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getDbType
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 sql
0.00% covered (danger)
0.00%
0 / 1
306
0.00% covered (danger)
0.00%
0 / 35
1<?php
2/*
3 * Formdin Framework
4 * Copyright (C) 2012 Ministério do Planejamento
5 * Criado por Luís Eugênio Barbosa
6 * Essa versão é um Fork https://github.com/bjverde/formDin
7 *
8 * ----------------------------------------------------------------------------
9 * This file is part of Formdin Framework.
10 *
11 * Formdin Framework is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License version 3
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License version 3
21 * along with this program; if not,  see <http://www.gnu.org/licenses/>
22 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
23 * Fifth Floor, Boston, MA  02110-1301, USA.
24 * ----------------------------------------------------------------------------
25 * Este arquivo é parte do Framework Formdin.
26 *
27 * O Framework Formdin é um software livre; você pode redistribuí-lo e/ou
28 * modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
29 * do Software Livre (FSF).
30 *
31 * Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
32 * GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
33 * APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
34 * para maiores detalhes.
35 *
36 * Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
37 * "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses/>
38 * ou escreva para a Fundação do Software Livre (FSF) Inc.,
39 * 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
40 */
41
42include_once( 'autoload_formdin.php');
43class TDb
44{
45    private static $conn    = array();
46    private static $dbType     = null;
47
48    // construtor
49    private function __construct()
50    {
51    }
52    //------------------------------------------------------------------------------------------
53    private function __clone()
54    {
55    }
56    public static function setDbType( $newType = null )
57    {
58        self::$dbType = $newType;
59    }
60    public static function getDbType()
61    {
62        return self::$dbType;
63    }
64    //------------------------------------------------------------------------------------------
65    public static function sql($sql=null,$arrParams=null,$fetchMode=null,$dbType=null)
66    {
67        $fetchMode = is_null($fetchMode) ? PDO::FETCH_ASSOC : $fetchMode;
68        if( is_null( self::$dbType ) && defined('DEFAULT_DBMS' ) )
69        {
70            self::$dbType = DEFAULT_DBMS;
71        }
72        $dbType = is_null($dbType) ? self::$dbType : $dbType;
73        if( ! $dbType )
74        {
75            throw new Exception("Necessário informar o tipo do banco de dados. Ex:TDb::setDbType('mysql'); ou defina a constante DEFAULT_DBMS. ex:define('DEFAULT_DBMS','mysql');");
76        }
77        if( is_null( self::$dbType ) )
78        {
79            self::$dbType = $dbType;
80        }
81        try
82        {
83            if( array_key_exists( $dbType, self::$conn ) )
84            {
85                $conn = self::$conn[$dbType];
86            }
87            else
88            {
89                $conn = TConnection::connect($dbType);
90                if( !array_key_exists( $dbType, self::$conn ) )
91                {
92                    self::$conn[$dbType] = $conn;
93                }
94            }
95        }
96        catch( Exception $e )
97        {
98
99            throw new Exception("<br><h3>Erro de conexão</h3>".$e->getMessage().'<br>');
100        }
101        $result=null;
102        try
103        {
104            if( !$sql)
105            {
106                return null;
107            }
108            $sql = trim($sql);
109            if( $stmt = $conn->prepare( $sql ) )
110            {
111                if( $result = $stmt->execute( (array) $arrParams ) )
112                {
113                    try
114                    {
115                        $data = $stmt->fetchAll( $fetchMode );
116                        if( preg_match('/^select/i',$sql ) > 0 )
117                        {
118                            $result = $data;
119                        }
120                    }
121                    catch(Exception $e)
122                    {
123                        return $result;
124                    }
125                }
126            }
127        }
128        catch( Exception $e )
129        {
130            $erro = "<h3>Erro de SQL</h3>".$sql.'<br><br>';
131            if( $arrParams )
132            {
133                $erro .= '<b>Parametros:</b><br>';
134                $erro .= print_r($arrParams,true);
135                $erro .= '<br><br>';
136            }
137            $erro .= '<b>Mensagem:</b><br>'.$e->getMessage();
138            throw new Exception($erro);
139        }
140        return $result;
141    }
142    //public static function
143
144}
145?>