Last active
March 21, 2019 17:27
-
-
Save rabehasy/8c3951050b6a8690435572d5a581fd42 to your computer and use it in GitHub Desktop.
PHP - Generate write query (insert....) from SELECT mysql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class PDO2 extends PDO { | |
private static $_instance; | |
/* Constructeur : héritage public obligatoire par héritage de PDO */ | |
public function __construct( ) { | |
} | |
// End of PDO2::__construct() */ | |
/* Singleton */ | |
public static function getInstance() { | |
if (!isset(self::$_instance)) { | |
try { | |
self::$_instance = new PDO(SQL_DSN, SQL_USERNAME, SQL_PASSWORD); | |
} catch (PDOException $e) { | |
echo $e; | |
} | |
} | |
return self::$_instance; | |
} | |
// End of PDO2::getInstance() */ | |
} | |
/** | |
* Generate Write Query (INSERT, UPDATE, DELETE) | |
* | |
* @param String $sql | |
* @param bool $bIgnore - if false do not add IGNORE | |
* | |
* @return string | |
*/ | |
function createWriteQuery($sql, $action = 'insert', $bIgnore = false) | |
{ | |
$aFields = []; | |
$aMetaFields = []; | |
$pdo = PDO2::getInstance(); | |
$res = $pdo->prepare($sql); | |
$res->execute(); | |
// columns count inside fields | |
$nb = $res->columnCount(); | |
// results rows count | |
$nbRows = $res->rowCount(); | |
// First meta | |
$aCol0 = $res->getColumnMeta(0); | |
$sTablename = array_key_exists('table', $aCol0) ? $aCol0['table'] : ''; | |
// columns fields | |
for ($i = 0; $i < $nb; $i++) { | |
$aCol = $res->getColumnMeta($i); | |
array_push($aMetaFields, $aCol); | |
$name = array_key_exists('name', $aCol) ? $aCol['name'] : ''; | |
if ($name !== '') { | |
array_push($aFields, $name); | |
} | |
} | |
$aFields = array_map(function ($v) { | |
return "`" . $v . "`"; | |
}, $aFields); | |
$sFields = implode(', ', $aFields); | |
// All Rows | |
$aValues = []; | |
while ($row = $res->fetch(3)) { | |
$i = 0; | |
foreach ($aMetaFields as $meta) { | |
$flagsExists = array_key_exists('flags', $meta) && count($meta['flags']) > 0 ; | |
$aRow = $row[$i]; | |
// flag exists - field is not null | |
if ($flagsExists) { | |
$aRow = $aRow !== '' ? "'" . str_replace("'","\'",$aRow) . "'" : "''"; | |
} else { | |
$aRow = !is_null($aRow) ? "'" . str_replace("'","\'",$aRow) . "'" : 'NULL'; | |
} | |
// populate row | |
$row[$i] = $aRow; | |
$i++; | |
} | |
// implode rows | |
$srValues = implode(', ', $row); | |
// Create sql | |
$sql = "INSERT "; // Todo add UPDATE, DELETE | |
if ($bIgnore && $action === 'insert') { | |
$sql .= " IGNORE INTO "; | |
} | |
if (!$bIgnore && $action === 'insert') { | |
$sql .= " INTO "; | |
} | |
$sql .= " `" . $sTablename . "`"; | |
if ($action === 'insert') { | |
$sql .= " (" . $sFields . ") VALUES (" . $srValues . ");"; | |
} | |
// Implode values | |
array_push($aValues, $sql); | |
} | |
$sql = implode("\n", $aValues); | |
if ($nbRows === 0) { | |
$sql = ' -- no rows : ' . $sTablename ; | |
} | |
if ($nbRows > 0) { | |
$sql = '-- ' . $sTablename . ' : ' .$nbRows . ' rows ' ."\n -- \n " . $sql; | |
} | |
return $sql; | |
} | |
// Usage | |
$sql = "SELECT * FROM mytable"; | |
$sqlG = createWriteQuery($sql); | |
var_dump($sqlG); | |
// Return INSERT INTO mytable(id,name) VALUES(1,'bob'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment