Skip to content

Instantly share code, notes, and snippets.

@Dinir
Created October 9, 2017 11:01
Show Gist options
  • Save Dinir/49981a09c6c31db794a2b7f4a856c376 to your computer and use it in GitHub Desktop.
Save Dinir/49981a09c6c31db794a2b7f4a856c376 to your computer and use it in GitHub Desktop.
<?php
class db_handler extends PDO
{
private function emitError($message)
{
return $message;
}
public function immigrateData(
$table_from, $columns_from, $table_to, $mapping, $start_at = 0, $limit = null
)
{
$page_amount = 1000;
if(!is_array($columns_from) && $columns_from !== '*') {
return $this->emitError(
'Columns in the origin table are not properly supplied.'
);
}
if(count($mapping) === 0) {
return $this->emitError(
'The mapping rules are empty.'
);
}
if($page_amount <= 0) {
return $this->emitError(
'Set a proper amount for a page of rows.'
);
}
$count_done = 0;
if(!is_null($limit)) {
$total_rows = $limit;
} else {
$total_rows = $this->query("select count(*) from $table_from");
$total_rows = $total_rows->fetchColumn();
if ($total_rows == 0) {
return $this->emitError(
'No rows were found in the origin table.'
);
}
}
$query = 'select '.
((is_array($columns_from) && count($columns_from))?
implode($columns_from, ', '):
$columns_from).
" from $table_from";
for(
$start_at;
$start_at < $total_rows;
) {
$query_to_submit = '';
$amount_to_read = min($total_rows-$start_at, $page_amount);
$page = $this->query("$query limit $start_at, $amount_to_read");
$start_at += $amount_to_read;
foreach($page as $row) {
$cells = implode(
array_map(function ($column_name) use ($mapping, $row) {
$cell = $mapping[$column_name]($row);
if (strtoupper($cell) !== 'NULL')
$cell = "'$cell'";
return $cell;
}, array_keys($mapping)),
', '
);
$query_to_submit .= "insert into $table_to".
(count($mapping)?
'('.implode(array_keys($mapping), ' ,').') ':
' ' ).
'values('.
$cells.
");\n";
}
try {
$count_done += (int)($this->exec($query_to_submit));
} catch (PDOException $e) {
return $this->emitError($e->getMessage());
}
}
return $count_done;
}
}
<?php
$mapping[161] = array(
'delflag' => function($v) {return is_null($v['deleteID'])?0:1;},
'uid' => function($v) {return $v['userID'];},
'upw' => function($v) {return $v['passwd'];},
'uname' => function($v) {return $v['userNM'];},
'subject' => function($v) {
return filter_var($v['titleDS'], FILTER_SANITIZE_MAGIC_QUOTES);},
'memo' => function($v) {
return filter_var($v['memoDS'], FILTER_SANITIZE_MAGIC_QUOTES);},
'view' => function($v) {return $v['readCount'];},
'writetime' => function ($v) {
return is_null($v['insertDT']) ? 'NULL' : str_replace(
'/', '-',
"{$v['insertDT']} {$v['insertTM']}"
);
},
'updatetime' => function ($v) {
return is_null($v['updateDT']) ? 'NULL' : str_replace(
'/', '-',
"{$v['updateDT']} {$v['updateTM']}"
);
},
'isimportant' => function($v) {return $v['noticeTP'] == 0? 0: 1;},
'issecret' => function($v) {return $v['secretTP'] == 'Y'? 1: 0;},
'category' => function($v) {return $v['categoryID'];},
'hmode' => function($v) {return $v['htmlTP'] == 'Y'? 2: 0;}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment