Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function insertSql(
- string $table = null,
- array $columns = array(),
- string $priority = null,
- array $partitions = array(),
- array $onDuplicateKeyUpdate = array())
- {
- $sql = "INSERT ";
- if (!empty($priority)) {
- if (preg_match('/\A([low]+)\z/i', $priority)) {
- $sql .= 'LOW_PRIORITY ';
- } elseif (preg_match('/\A([delayed]+)\z/i', $priority)) {
- $sql .= 'DELAYED ';
- } elseif (preg_match('/\A([high]+)\z/i', $priority)) {
- $sql .= 'HIGH_PRIORITY ';
- } elseif (preg_match('/\A([ignore]+)\z/i', $priority)) {
- $sql .= 'IGNORE ';
- }
- }
- if (!empty($table)) {
- $sql .= 'INTO '. $table . ' ';
- }
- if (!empty($partitions)) {
- $sql .= 'PARTITION (';
- foreach ($partitions as $partition) {
- $sql .= sprintf('`%s`, ', $partition);
- }
- $sql = preg_replace('/,\s$/','', $sql);
- $sql .= ') ';
- }
- if (is_array($columns)) {
- $sql .= 'SET ';
- foreach ($columns as $column => $value) {
- $sql .= sprintf('`%s` = ?, ', $column);
- }
- $sql = preg_replace('/,\s$/','', $sql);
- }
- if (!empty($onDuplicateKeyUpdate)) {
- $sql .= 'ON DUPLICATE KEY UPDATE ';
- foreach ($onDuplicateKeyUpdate as $key => $value) {
- $sql .= sprintf('`%s` = %s, ', $key);
- }
- $sql = preg_replace('/,\s$/','', $sql);
- }
- return $sql;
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement