Convertendo
| MySQL |
PostgreSQL |
| # MySQL comment |
--ANSI SQL comment |
| UNIQUE KEY name (cols) |
UNIQUE (cols) |
| PRIMARY KEY (cols) |
the same |
| KEY name (cols) |
CREATE INDEX name ON TABLE (cols) |
| id int NOT NULL auto_increment |
id SERIAL |
| binary |
doesn't exist |
| enum |
doesn't exist, convert to varchar |
| tinyint(n) or smallint(n) |
smallint |
| mediumint(n) or int(n) |
int |
<?php
$source = "/home/shaggy/shaggy.sql";
$output = "/home/shaggy/pgtest.sql";
$enum = 'varchar(10)'; // convert enum to this
if ( !file_exists($source) ) {
die("File not found: $sourcen");
}
$fd = fopen($source, "r");
$result = fread($fd, filesize($source));
fclose($fd);
$result = mysql2postgre($result);
$fd = fopen($output, "w");
if (fwrite($fd, $result)) {
echo "OKn";
} else {
echo "Failedn";
}
fclose($fd);
function mysql2postgre($source) {
global $enum;
$result = $source;
$result = preg_replace('/Type=MyISAM/i', '', $result);
// convert line comments
$result = preg_replace("/#(.*)/", '--$1', $result);
// and compress newlines
$result = preg_replace("/n{2,}/", "nn", $result);
// get rid of proprietary code
$result = preg_replace("/DROP TABLE IF EXISTSW+.+/i", '', $result);
// indices
$result = preg_replace("/(.*)UNIQUE KEY.+((.+))/i",
"$1UNIQUE ($2)", $result);
// a little hack to save primary keys
$result = preg_replace("/(.*)PRIMARY KEY.+((.+))/i",
"$1PRIMARY ($2)", $result);
$result = preg_replace("/,n.*KEYW.+((.+))/i",
"n-- was KEY ($1)", $result);
$result = preg_replace("/(.*)PRIMARY.+((.+))/i",
"$1PRIMARY KEY (\2)", $result);
$result = preg_replace("/(.*?)(w+).+auto_increment/i",
'$1$2 SERIAL', $result);
// Postgre doesn't support the binary modifier
$result = preg_replace('/binary/i', '', $result);
// type transformations
$result = preg_replace('/enum(.+)/i', $enum, $result);
$result = preg_replace('/tinyint(.+)/i', 'smallint', $result);
$result = preg_replace('/smallint(.+)/i', 'smallint', $result);
$result = preg_replace('/meduimint(.+)/i', 'int', $result);
$result = preg_replace('/int(.+)/i', 'int', $result);
// Most of my default dates are '0000-00-00'
$result = preg_replace("/datetime(.*) default '.*'/i",
'datetime$1', $result);
$result = preg_replace("/date(.*) default '.*'/i",
'date$1', $result);
return $result;
}
?>
Fonte: Mtdev
Por Luiz Felipe Baio
Comentários [7]
function paginacao($page, $totalLinhas, $txtQuantidade, $pagina, $compl)
{
$html = '';
$qtde_pag = ceil($totalLinhas/$txtQuantidade);
if ($totalLinhas > $txtQuantidade)
{
$html .= '<p style="text-align:center">';
if($qtde_pag > 15)
$cont = 15;
else
$cont = $qtde_pag;
if($page>1)
{
$html .= '<a href="'.$pagina.'.php?page='.'1'.$compl.'"><<</a> <a href="'.$pagina.'.php?page='.($page-1).$compl.'"><</a>';
}
if($page<=8 or $qtde_pag<=15)
$ini = 1;
else
if($page>($qtde_pag-7))
{
$ini = floor($qtde_pag-15);
$cont += $ini;
}
else
{
$ini = $page-7;
$cont += ($ini-1);
}
for ($i=$ini; $i<=$cont;$i++)
{
$html .= '<a href="'.$pagina.'.php?page='.$i.$compl.'"> ';
if ($i == $page)
{
$html .= '<b>['.$i.']</b>';
}else{
$html .= $i;
}
$html .= ' </a>';
}
if($page<floor($qtde_pag))
{
(empty($page))?$f=2:$f=$page+1;
$html .= '<a href="'.$pagina.'.php?page='.$f.$compl.'">></a> <a href="'.$pagina.'.php?page='.floor($qtde_pag).$compl.'">>></a>';
}
$html .= '</p>';
}
return $html;
}
Modo de usar
<< Continuação >>
Por Luiz Felipe Baio
Comentários [1]
function stringUpDown($strTexto, $strConverter){
# Convert values from Lower to Upper
if($strConverter=='upper') {
$strTextoConvertido = strtr(strtoupper($strTexto), 'çãâáàäêéèëîíìïõôóòöûúùü', 'ÇÃÂÁÀÄÊÉÈËÎÍÌÏÕÔÓÒÖÛÚÙÜ');
}
elseif($strConverter=='lower') {
$strTextoConvertido = strtr(strtolower($strTexto), 'ÇÃÂÁÀÄÊÉÈËÎÍÌÏÕÔÓÒÖÛÚÙÜ', 'çãâáàäêéèëîíìïõôóòöûúùü');
}
return $strTextoConvertido;
}
Modo de usar
<< Continuação >>
Por Luiz Felipe Baio
Comentários [1]
function extrairMes($data){
switch(date("n",$data)){
case 1:
$mes = "Janeiro";
break;
case 2:
$mes = "Fevereiro";
break;
case 3:
$mes = "Março";
break;
case 4:
$mes = "Abril";
break;
case 5:
$mes = "Maio";
break;
case 6:
$mes = "Junho";
break;
case 7:
$mes = "Julho";
break;
case 8:
$mes = "Agosto";
break;
case 9:
$mes = "Setembro";
break;
case 10:
$mes = "Outubro";
break;
case 11:
$mes = "Novembro";
break;
case 12:
$mes = "Dezembro";
break;
}
return $mes;
}
function extrairDiaSemana($data){
switch(strftime("%u",$data)){
case 7:
$dia = "Domingo";
break;
case 1:
$dia = "Segunda";
break;
case 2:
$dia = "Terça";
break;
case 3:
$dia = "Quarta";
break;
case 4:
$dia = "Quinta";
break;
case 5:
$dia = "Sexta";
break;
case 6:
$dia = "Sábado";
break;
}
return $dia;
}
Modo de usar
<< Continuação >>
Por Luiz Felipe Baio
Comentários [1]
function formatarDataHoraExibicao($strDataBanco)
{
if( $strDataBanco < date("Y-m-d H:i:s") )
$strCorFonte = "green";
else
$strCorFonte = "red";
$arrData = split("-", $strDataBanco);
$strDataFormatada = substr($arrData[2],0,2)."-".$arrData[1]."-".$arrData[0];
$strHoraFormatada = substr($strDataBanco,-8);
return '<font color="'.$strCorFonte.'">'.$strDataFormatada." ".$strHoraFormatada.'</font>';
}
Modo de usar
<< Continuação >>
Por Luiz Felipe Baio
Comentários [1]
function gerarCombo($arrConteudo, $strNomeCombo, $strElementoSelecionado="", $strValor, $strLabel, $strComplemento="")
{
$strHtml = "<select name='$strNomeCombo' $strComplemento>\n";
if(strlen(trim($strElementoSelecionado)) == 0)
$strHtml .= "<option value='-1'>- Selecione -</option>\n";
for($intI=0;$intI<count($arrConteudo);$intI++){
$strValorElemento = $arrConteudo[$intI][$strValor];
$strLabelElemento = $arrConteudo[$intI][$strLabel];
if ($strElementoSelecionado == $strValorElemento)
$strHtml .= "<option value='$strValorElemento' selected>$strLabelElemento</option>";
else
$strHtml .= "<option value='$strValorElemento'>$strLabelElemento
</option>";
}
$strHtml .= "</select>";
return $strHtml;
}
Modo de usar
<< Continuação >>
Por Luiz Felipe Baio
Comentários [1]
- se um método pode ser static, declare-o como static. Isso irá aumentar a performance umas 4 vezes;
- Echo é mais rápido que print;
- Use echo's ao invés de concatenar strings;
- Defina o maximo valor para sua for antes do laço e não dentro da for;
- utilize o unset nas suas variáveis para liberar memória, principalmente em vetores (array) grandes;
- Evite utilizar mágicas como __get, __set e __autoload;
- não use require_once();
- utilize o caminho absoluto na hora de dar os includes, assim o PHP não perde tempo tentando resolver o caminho;
-
Se você precisa saber o momento em que começou a executar o script, $ _SERVER [ 'REQUEST_TIME'] é preferível a time();
-