Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
58.82% |
20 / 34 |
CRAP | |
34.48% |
80 / 232 |
| TDAOCreate | |
0.00% |
0 / 1 |
|
58.82% |
20 / 34 |
1926.17 | |
34.48% |
80 / 232 |
| __construct | |
0.00% |
0 / 1 |
2.01 | |
87.50% |
7 / 8 |
|||
| setTableName | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| getTableName | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getKeyColumnName | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setDatabaseManagementSystem | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getDatabaseManagementSystem | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| setShowSchema | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getShowSchema | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| hasSchema | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
| setWithSqlPagination | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| getWithSqlPagination | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
| getCharParam | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getLinesArray | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| getLinesString | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| addColumn | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
| getColumns | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
| addLine | |
100.00% |
1 / 1 |
3 | |
100.00% |
3 / 3 |
|||
| addBlankLine | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| showVO | |
0.00% |
0 / 1 |
72 | |
0.00% |
0 / 33 |
|||
| saveVO | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 6 |
|||
| addSqlVariable | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 6 |
|||
| addSqlSelectById | |
100.00% |
1 / 1 |
1 | |
100.00% |
7 / 7 |
|||
| addProcessWhereGridParameters | |
100.00% |
1 / 1 |
2 | |
100.00% |
12 / 12 |
|||
| addSqlSelectCount | |
100.00% |
1 / 1 |
1 | |
100.00% |
8 / 8 |
|||
| addSqlSelectAllPagination | |
100.00% |
1 / 1 |
3 | |
100.00% |
16 / 16 |
|||
| addSqlSelectAll | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 10 |
|||
| addSqlInsert | |
0.00% |
0 / 1 |
56 | |
0.00% |
0 / 18 |
|||
| addSqlUpdate | |
0.00% |
0 / 1 |
90 | |
0.00% |
0 / 19 |
|||
| addSqlDelete | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| addConstruct | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 4 |
|||
| showDAO | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 29 |
|||
| saveDAO | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 6 |
|||
| getParams | |
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 10 |
|||
| removeUnderline | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 8 |
|||
| 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 | |
| 42 | |
| 43 | class TDAOCreate { |
| 44 | private $tableName; |
| 45 | private $aColumns; |
| 46 | private $lines; |
| 47 | private $keyColumnName; |
| 48 | private $path; |
| 49 | private $databaseManagementSystem; |
| 50 | private $showSchema; |
| 51 | private $withSqlPagination; |
| 52 | private $charParam = '?'; |
| 53 | |
| 54 | public function __construct($strTableName=null,$strkeyColumnName=null,$strPath=null,$databaseManagementSystem=null) { |
| 55 | $this->aColumns=array(); |
| 56 | $this->setTableName($strTableName); |
| 57 | $this->keyColumnName = $strkeyColumnName; |
| 58 | $this->path = $strPath; |
| 59 | $this->databaseManagementSystem = StringHelper::strtoupper($databaseManagementSystem); |
| 60 | if( $databaseManagementSystem == DBMS_POSTGRES ) { |
| 61 | $this->charParam = '$1'; |
| 62 | } |
| 63 | } |
| 64 | //----------------------------------------------------------------------------------- |
| 65 | public function setTableName($strNewValue) { |
| 66 | $this->tableName=$strNewValue; |
| 67 | } |
| 68 | //------------------------------------------------------------------------------------ |
| 69 | public function getTableName() { |
| 70 | return $this->tableName; |
| 71 | } |
| 72 | //------------------------------------------------------------------------------------ |
| 73 | public function getKeyColumnName() { |
| 74 | return $this->keyColumnName; |
| 75 | } |
| 76 | //------------------------------------------------------------------------------------ |
| 77 | public function setDatabaseManagementSystem($databaseManagementSystem) { |
| 78 | return $this->databaseManagementSystem = StringHelper::strtoupper($databaseManagementSystem); |
| 79 | } |
| 80 | //------------------------------------------------------------------------------------ |
| 81 | public function getDatabaseManagementSystem() { |
| 82 | return $this->databaseManagementSystem; |
| 83 | } |
| 84 | //------------------------------------------------------------------------------------ |
| 85 | public function setShowSchema($showSchema){ |
| 86 | return $this->showSchema = $showSchema; |
| 87 | } |
| 88 | //------------------------------------------------------------------------------------ |
| 89 | public function getShowSchema(){ |
| 90 | return $this->showSchema; |
| 91 | } |
| 92 | //------------------------------------------------------------------------------------ |
| 93 | public function hasSchema(){ |
| 94 | $result = ''; |
| 95 | if($this->getShowSchema() == true){ |
| 96 | $result = '\'.SCHEMA.\''; |
| 97 | } |
| 98 | return $result; |
| 99 | } |
| 100 | //------------------------------------------------------------------------------------ |
| 101 | public function setWithSqlPagination($withSqlPagination) { |
| 102 | return $this->withSqlPagination = $withSqlPagination; |
| 103 | } |
| 104 | //------------------------------------------------------------------------------------ |
| 105 | public function getWithSqlPagination() { |
| 106 | return $this->withSqlPagination; |
| 107 | } |
| 108 | //------------------------------------------------------------------------------------ |
| 109 | public function getCharParam() { |
| 110 | return $this->charParam; |
| 111 | } |
| 112 | //------------------------------------------------------------------------------------ |
| 113 | public function getLinesArray(){ |
| 114 | return $this->lines; |
| 115 | } |
| 116 | //------------------------------------------------------------------------------------ |
| 117 | public function getLinesString(){ |
| 118 | $string = implode($this->lines); |
| 119 | return trim($string); |
| 120 | } |
| 121 | //------------------------------------------------------------------------------------ |
| 122 | public function addColumn($strColumnName) |
| 123 | { |
| 124 | if ( !in_array($strColumnName,$this->aColumns)) |
| 125 | { |
| 126 | $this->aColumns[] = strtolower($strColumnName); |
| 127 | } |
| 128 | } |
| 129 | //-------------------------------------------------------------------------------------- |
| 130 | public function getColumns() |
| 131 | { |
| 132 | return $this->aColumns; |
| 133 | } |
| 134 | //-------------------------------------------------------------------------------------- |
| 135 | public function addLine($strNewValue=null,$boolNewLine=true){ |
| 136 | $strNewValue = is_null( $strNewValue ) ? TAB.'//' . str_repeat( '-', 80 ) : $strNewValue; |
| 137 | $this->lines[] = $strNewValue.( $boolNewLine ? EOL : ''); |
| 138 | } |
| 139 | //-------------------------------------------------------------------------------------- |
| 140 | private function addBlankLine(){ |
| 141 | $this->addLine(''); |
| 142 | } |
| 143 | //-------------------------------------------------------------------------------------- |
| 144 | public function showVO($print=false){ |
| 145 | $this->addLine('<?php'); |
| 146 | $this->addLine("class ".ucfirst($this->getTableName())."VO {"); |
| 147 | $cols=''; |
| 148 | $sets=''; |
| 149 | foreach($this->getColumns() as $k => $v ){ |
| 150 | $this->addLine(TAB.'private $'.$v.' = null;'); |
| 151 | $cols .= $cols == '' ? '' : ', '; |
| 152 | $cols .='$'.$v.'=null'; |
| 153 | $sets .= ($k == 0 ? '' : EOL ).TAB.TAB.'$this->set'.ucFirst($v).'( $'.$v.' );'; |
| 154 | } |
| 155 | $this->addLine(TAB.'public function __construct( '.$cols.' ) {'); |
| 156 | $this->addLine($sets); |
| 157 | $this->addLine(TAB.'}'); |
| 158 | $this->addLine(); |
| 159 | foreach($this->getColumns() as $k=>$v) { |
| 160 | $this->addLine(TAB.'function set'.ucfirst($v).'( $strNewValue = null )'); |
| 161 | $this->addLine(TAB."{"); |
| 162 | if( preg_match('/cpf|cnpj/i',$v) > 0 ) |
| 163 | { |
| 164 | $this->addLine(TAB.TAB.'$this->'.$v.' = preg_replace(\'/[^0-9]/\',\'\',$strNewValue);'); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | $this->addLine(TAB.TAB.'$this->'.$v.' = $strNewValue;'); |
| 169 | } |
| 170 | $this->addLine(TAB."}"); |
| 171 | $this->addLine(TAB.'function get'.ucfirst($v).'()'); |
| 172 | $this->addLine(TAB."{"); |
| 173 | if(preg_match('/^data?_/i',$v) == 1 ) |
| 174 | { |
| 175 | $this->addLine(TAB.TAB."return is_null( \$this->{$v} ) ? date( 'Y-m-d h:i:s' ) : \$this->{$v};"); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | $this->addLine(TAB.TAB.'return $this->'.$v.';'); |
| 180 | } |
| 181 | $this->addLine(TAB."}"); |
| 182 | $this->addLine(); |
| 183 | } |
| 184 | $this->addLine("}"); |
| 185 | $this->addLine('?>'); |
| 186 | if( $print) { |
| 187 | echo trim(implode($this->lines)); |
| 188 | }else { |
| 189 | return trim(implode($this->lines)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | //-------------------------------------------------------------------------------------- |
| 194 | public function saveVO($fileName=null) { |
| 195 | $fileName = $this->path.( is_null($fileName) ? ucfirst($this->getTableName()).'VO.class.php' : $tableName); |
| 196 | |
| 197 | if($fileName) { |
| 198 | if( file_exists($fileName)) { |
| 199 | unlink($fileName); |
| 200 | } |
| 201 | file_put_contents($fileName,$this->showVO(false)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | //-------------------------------------------------------------------------------------- |
| 206 | /*** |
| 207 | * Create variable with string sql basica |
| 208 | **/ |
| 209 | public function addSqlVariable() { |
| 210 | $indent = TAB.TAB.TAB.TAB.TAB.TAB.TAB.TAB.TAB.' '; |
| 211 | $this->addLine( TAB.'private static $sqlBasicSelect = \'select'); |
| 212 | foreach($this->getColumns() as $k=>$v) { |
| 213 | $this->addLine($indent.( $k==0 ? ' ' : ',').$v); |
| 214 | } |
| 215 | $this->addLine($indent.'from '.$this->hasSchema().$this->getTableName().' \';' ); |
| 216 | } |
| 217 | //-------------------------------------------------------------------------------------- |
| 218 | /*** |
| 219 | * Create function for sql select by id |
| 220 | **/ |
| 221 | public function addSqlSelectById() { |
| 222 | $this->addLine( TAB.'public static function selectById( $id ) {'); |
| 223 | $this->addLine( TAB.TAB.'$values = array($id);'); |
| 224 | $this->addLine( TAB.TAB.'$sql = self::$sqlBasicSelect.\' where '.$this->getKeyColumnName().' = '.$this->charParam.'\';'); |
| 225 | $this->addLine( TAB.TAB.'$result = self::executeSql($sql, $values );'); |
| 226 | $this->addLine( TAB.TAB.'return $result;'); |
| 227 | $this->addLine( TAB.'}'); |
| 228 | } |
| 229 | //-------------------------------------------------------------------------------------- |
| 230 | public function addProcessWhereGridParameters() { |
| 231 | $this->addLine( TAB.'private static function processWhereGridParameters( $whereGrid ) {'); |
| 232 | $this->addLine( TAB.TAB.'$result = $whereGrid;'); |
| 233 | $this->addLine( TAB.TAB.'if ( is_array($whereGrid) ){'); |
| 234 | $this->addLine( TAB.TAB.TAB.'$where = \' 1=1 \';'); |
| 235 | foreach($this->getColumns() as $k=>$v) { |
| 236 | $v = StringHelper::strtoupper($v); |
| 237 | $this->addLine( TAB.TAB.TAB.'$where = $where.( paginationSQLHelper::attributeIssetOrNotZero($whereGrid,\''.$v.'\',\' AND '.$v.' like \\\'%\'.$whereGrid[\''.$v.'\'].\'%\\\' \',null) );' ); |
| 238 | } |
| 239 | $this->addLine( TAB.TAB.TAB.'$result = $where;'); |
| 240 | $this->addLine( TAB.TAB.'}'); |
| 241 | $this->addLine( TAB.TAB.'return $result;'); |
| 242 | $this->addLine( TAB.'}'); |
| 243 | } |
| 244 | //-------------------------------------------------------------------------------------- |
| 245 | /*** |
| 246 | * Create function for sql count rows of table |
| 247 | **/ |
| 248 | public function addSqlSelectCount() { |
| 249 | $this->addLine( TAB.'public static function selectCount( $where=null ){'); |
| 250 | $this->addLine( TAB.TAB.'$where = self::processWhereGridParameters($where);'); |
| 251 | $this->addLine( TAB.TAB.'$sql = \'select count('.$this->getKeyColumnName().') as qtd from '.$this->hasSchema().$this->getTableName().'\';' ); |
| 252 | $this->addLine( TAB.TAB.'$sql = $sql.( ($where)? \' where \'.$where:\'\');'); |
| 253 | $this->addLine( TAB.TAB.'$result = self::executeSql($sql);'); |
| 254 | $this->addLine( TAB.TAB.'return $result[\'QTD\'][0];'); |
| 255 | $this->addLine( TAB.'}'); |
| 256 | } |
| 257 | //-------------------------------------------------------------------------------------- |
| 258 | /*** |
| 259 | * Create function for sql select all with Pagination |
| 260 | **/ |
| 261 | public function addSqlSelectAllPagination() { |
| 262 | $this->addLine( TAB.'public static function selectAllPagination( $orderBy=null, $where=null, $page=null, $rowsPerPage= null ) {'); |
| 263 | $this->addLine( TAB.TAB.'$rowStart = PaginationSQLHelper::getRowStart($page,$rowsPerPage);'); |
| 264 | $this->addLine( TAB.TAB.'$where = self::processWhereGridParameters($where);'); |
| 265 | $this->addBlankLine(); |
| 266 | $this->addLine( TAB.TAB.'$sql = self::$sqlBasicSelect'); |
| 267 | $this->addLine( TAB.TAB.'.( ($where)? \' where \'.$where:\'\')'); |
| 268 | $this->addLine( TAB.TAB.'.( ($orderBy) ? \' order by \'.$orderBy:\'\')'); |
| 269 | if($this->getDatabaseManagementSystem() == DBMS_MYSQL){ |
| 270 | $this->addLine( TAB.TAB.'.( \' LIMIT \'.$rowStart.\',\'.$rowsPerPage);'); |
| 271 | } |
| 272 | if($this->getDatabaseManagementSystem() == DBMS_SQLSERVER){ |
| 273 | $this->addLine( TAB.TAB.'.( \' OFFSET \'.$rowStart.\' ROWS FETCH NEXT \'.$rowsPerPage.\' ROWS ONLY \');'); |
| 274 | } |
| 275 | $this->addBlankLine(); |
| 276 | $this->addLine( TAB.TAB.'$result = self::executeSql($sql);'); |
| 277 | $this->addLine( TAB.TAB.'return $result;'); |
| 278 | $this->addLine( TAB.'}'); |
| 279 | } |
| 280 | //-------------------------------------------------------------------------------------- |
| 281 | /*** |
| 282 | * Create function for sql select all |
| 283 | **/ |
| 284 | public function addSqlSelectAll() { |
| 285 | $this->addLine( TAB.'public static function selectAll( $orderBy=null, $where=null ) {'); |
| 286 | $this->addLine( TAB.TAB.'$where = self::processWhereGridParameters($where);'); |
| 287 | $this->addLine( TAB.TAB.'$sql = self::$sqlBasicSelect'); |
| 288 | $this->addLine( TAB.TAB.'.( ($where)? \' where \'.$where:\'\')'); |
| 289 | $this->addLine( TAB.TAB.'.( ($orderBy) ? \' order by \'.$orderBy:\'\');'); |
| 290 | $this->addBlankLine(); |
| 291 | $this->addLine( TAB.TAB.'$result = self::executeSql($sql);'); |
| 292 | $this->addLine( TAB.TAB.'return $result;'); |
| 293 | $this->addLine( TAB.'}'); |
| 294 | } |
| 295 | //-------------------------------------------------------------------------------------- |
| 296 | /*** |
| 297 | * Create function for sql insert |
| 298 | **/ |
| 299 | public function addSqlInsert() { |
| 300 | $this->addLine(TAB.'public static function insert( '.ucfirst($this->tableName).'VO $objVo ) {'); |
| 301 | $this->addLine(TAB.TAB.'if( $objVo->get'.ucFirst($this->keyColumnName).'() ) {'); |
| 302 | $this->addLine(TAB.TAB.' return self::update($objVo);'); |
| 303 | $this->addLine(TAB.TAB.'}'); |
| 304 | $this->addLine(TAB.TAB.'$values = array(',false); |
| 305 | $cnt=0; |
| 306 | foreach($this->getColumns() as $k=>$v) { |
| 307 | if( $v != strtolower($this->keyColumnName) ) { |
| 308 | $this->addLine(( $cnt++==0 ? ' ' : TAB.TAB.TAB.TAB.TAB.TAB.',').' $objVo->get'.ucfirst($v).'() '); |
| 309 | } |
| 310 | } |
| 311 | $this->addLine(TAB.TAB.TAB.TAB.TAB.TAB.');'); |
| 312 | $this->addLine(TAB.TAB.'return self::executeSql(\'insert into '.$this->hasSchema().$this->getTableName().'('); |
| 313 | $cnt=0; |
| 314 | foreach($this->getColumns() as $k=>$v) { |
| 315 | if( $v != strtolower($this->keyColumnName) ) { |
| 316 | $this->addLine(TAB.TAB.TAB.TAB.TAB.TAB.TAB.TAB.( $cnt++==0 ? ' ' : ',').$v); |
| 317 | } |
| 318 | } |
| 319 | //$this->addLine(TAB.TAB.TAB.TAB.TAB.TAB.TAB.TAB.') values (?'.str_repeat(',?',count($this->getColumns())-1 ).')\', $values );'); |
| 320 | $this->addLine(TAB.TAB.TAB.TAB.TAB.TAB.TAB.TAB.') values ('.$this->getParams().')\', $values );'); |
| 321 | $this->addLine(TAB.'}'); |
| 322 | } |
| 323 | //-------------------------------------------------------------------------------------- |
| 324 | /*** |
| 325 | * Create function for sql update |
| 326 | **/ |
| 327 | public function addSqlUpdate() { |
| 328 | $this->addLine(TAB.'public static function update ( '.ucfirst($this->tableName).'VO $objVo ) {'); |
| 329 | $this->addLine(TAB.TAB.'$values = array(',false); |
| 330 | $count=0; |
| 331 | foreach($this->getColumns() as $k=>$v) { |
| 332 | if( strtolower($v) != strtolower($this->keyColumnName)) { |
| 333 | $this->addLine(( $count==0 ? ' ' : TAB.TAB.TAB.TAB.TAB.TAB.',').'$objVo->get'.ucfirst($v).'()'); |
| 334 | $count++; |
| 335 | } |
| 336 | } |
| 337 | $this->addline(TAB.TAB.TAB.TAB.TAB.TAB.',$objVo->get'.ucfirst($this->keyColumnName).'() );'); |
| 338 | $this->addLine(TAB.TAB.'return self::executeSql(\'update '.$this->hasSchema().$this->getTableName().' set '); |
| 339 | $count=0; |
| 340 | foreach($this->getColumns() as $k=>$v) { |
| 341 | if( strtolower($v) != strtolower($this->keyColumnName)) { |
| 342 | $param = $this->databaseManagementSystem == DBMS_POSTGRES ? '$'.($count+1) : '?'; |
| 343 | $this->addLine(TAB.TAB.TAB.TAB.TAB.TAB.TAB.TAB.( $count==0 ? ' ' : ',').$v.' = '.$param); |
| 344 | $count++; |
| 345 | } |
| 346 | } |
| 347 | $param = $this->databaseManagementSystem == DBMS_POSTGRES ? '$'.($count+1) : '?'; |
| 348 | $this->addLine(TAB.TAB.TAB.TAB.TAB.TAB.TAB.TAB.'where '.$this->keyColumnName.' = '.$param.'\',$values);'); |
| 349 | $this->addLine(TAB.'}'); |
| 350 | } |
| 351 | //-------------------------------------------------------------------------------------- |
| 352 | /*** |
| 353 | * Create function for sql delete |
| 354 | **/ |
| 355 | public function addSqlDelete() { |
| 356 | $this->addLine( TAB.'public static function delete( $id ){'); |
| 357 | $this->addLine( TAB.TAB.'$values = array($id);'); |
| 358 | $this->addLine( TAB.TAB.'return self::executeSql(\'delete from '.$this->hasSchema().$this->getTableName().' where '.$this->keyColumnName.' = '.$this->charParam.'\',$values);'); |
| 359 | $this->addLine( TAB.'}'); |
| 360 | } |
| 361 | //-------------------------------------------------------------------------------------- |
| 362 | /** |
| 363 | * No PHP 7.1 classes com construtores ficou deprecated |
| 364 | */ |
| 365 | public function addConstruct() { |
| 366 | if (version_compare(phpversion(), '5.6.0', '<')) { |
| 367 | $this->addLine(TAB.'public function '.$this->getTableName().'DAO() {'); |
| 368 | $this->addLine(TAB.'}'); |
| 369 | } |
| 370 | } |
| 371 | //-------------------------------------------------------------------------------------- |
| 372 | public function showDAO($print=false) { |
| 373 | $this->lines=null; |
| 374 | $this->addLine('<?php'); |
| 375 | $this->addLine('class '.ucfirst($this->getTableName()).'DAO extends TPDOConnection {'); |
| 376 | $this->addBlankLine(); |
| 377 | $this->addSqlVariable(); |
| 378 | $this->addBlankLine(); |
| 379 | |
| 380 | // construct |
| 381 | $this->addConstruct(); |
| 382 | |
| 383 | $this->addProcessWhereGridParameters(); |
| 384 | |
| 385 | // select by Id |
| 386 | $this->addLine(); |
| 387 | $this->addSqlSelectById(); |
| 388 | // fim select |
| 389 | |
| 390 | // Select Count |
| 391 | $this->addLine(); |
| 392 | $this->addSqlSelectCount(); |
| 393 | // fim Select Count |
| 394 | |
| 395 | if( $this->getWithSqlPagination() == GRID_SQL_PAGINATION ){ |
| 396 | $this->addLine(); |
| 397 | $this->addSqlSelectAllPagination(); |
| 398 | } |
| 399 | |
| 400 | // select where |
| 401 | $this->addLine(); |
| 402 | $this->addSqlSelectAll(); |
| 403 | // fim select |
| 404 | |
| 405 | // insert |
| 406 | $this->addLine(); |
| 407 | $this->addSqlInsert(); |
| 408 | //FIM INSERT |
| 409 | |
| 410 | // update |
| 411 | $this->addLine(); |
| 412 | $this->addSqlUpdate(); |
| 413 | //FIM UPDATE |
| 414 | |
| 415 | // EXCLUIR |
| 416 | $this->addLine(); |
| 417 | $this->addSqlDelete(); |
| 418 | //FIM excluir |
| 419 | |
| 420 | |
| 421 | //-------- FIM |
| 422 | $this->addLine("}"); |
| 423 | $this->addLine("?>"); |
| 424 | if ($print) { |
| 425 | echo $this->getLinesString(); |
| 426 | } else { |
| 427 | return $this->getLinesString(); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | //--------------------------------------------------------------------------------------- |
| 432 | public function saveDAO($fileName=null) { |
| 433 | |
| 434 | $fileName = $this->path.(is_null($fileName) ? ucfirst($this->getTableName()).'DAO.class.php' : $tableName); |
| 435 | if($fileName) { |
| 436 | if( file_exists($fileName) ) { |
| 437 | unlink($fileName); |
| 438 | } |
| 439 | file_put_contents($fileName,$this->showDAO(false)); |
| 440 | } |
| 441 | |
| 442 | } |
| 443 | //-------------------------------------------------------------------------------------- |
| 444 | /** |
| 445 | * Returns the number of parameters |
| 446 | * @return string |
| 447 | */ |
| 448 | public function getParams() { |
| 449 | $cols = $this->getColumns(); |
| 450 | $qtd = count($cols); |
| 451 | $result = ''; |
| 452 | for($i = 1; $i <= $qtd ; $i++) { |
| 453 | if( $cols[$i-1] != strtolower($this->keyColumnName) ){ |
| 454 | $result .= ($result=='') ? '' : ','; |
| 455 | if( $this->databaseManagementSystem == DBMS_POSTGRES ){ |
| 456 | $result .= '$'.$i; |
| 457 | } else { |
| 458 | $result.='?'; |
| 459 | } |
| 460 | } |
| 461 | } |
| 462 | return $result; |
| 463 | } |
| 464 | //-------------------------------------------------------------------------------------- |
| 465 | public function removeUnderline($txt) { |
| 466 | $len = StringHelper::strlen($txt); |
| 467 | for ($i = $len-1; $i >= 0; $i--) { |
| 468 | if ($txt[$i] === '_') { |
| 469 | $len--; |
| 470 | $txt = substr_replace($txt, '', $i, 1); |
| 471 | if ($i != $len){ |
| 472 | $txt[$i] = StringHelper::strtoupper($txt[$i]); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | return $txt; |
| 477 | } |
| 478 | } |
| 479 | /* |
| 480 | $vo = new TGenerateDAO('usuario','id'); |
| 481 | $vo->addColumn('id'); |
| 482 | $vo->addColumn('cpf'); |
| 483 | $vo->addColumn('nome'); |
| 484 | //$vo->saveVO(); |
| 485 | //$vo->showDAO(true); |
| 486 | $vo->saveDAO(); |
| 487 | */ |
| 488 | ?> |