| 1 |
<?php |
<?php |
| 2 |
|
|
| 3 |
|
// $Id$ |
| 4 |
|
|
| 5 |
class pressflow_transaction { |
class pressflow_transaction { |
| 6 |
private static $layers = 0; |
private static $layers = 0; |
| 7 |
private static $allow_commit = TRUE; |
private static $allow_commit = TRUE; |
| 44 |
return !self::$allow_commit; |
return !self::$allow_commit; |
| 45 |
} |
} |
| 46 |
} |
} |
| 47 |
|
|
| 48 |
|
// Transaction-friendly db_next_id() |
| 49 |
|
function pressflow_transaction_db_next_id($name) { |
| 50 |
|
$name = db_prefix_tables($name); |
| 51 |
|
// Reset LAST_INSERT_ID so it doesn't have a stale value if INSERT succeeds. |
| 52 |
|
db_query('SELECT LAST_INSERT_ID(1)'); |
| 53 |
|
db_query('INSERT INTO {sequences} VALUES ("%s", 1) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id + 1)', $name); |
| 54 |
|
$id = db_result(db_query('SELECT LAST_INSERT_ID()')); |
| 55 |
|
return $id; |
| 56 |
|
} |
| 57 |
|
|
| 58 |
|
// Update or insert a single row |
| 59 |
|
function pressflow_transaction_update($table, $primary_key, $fields) { |
| 60 |
|
$txn = new pressflow_transaction(); |
| 61 |
|
|
| 62 |
|
// Die if the primary key is unspecified |
| 63 |
|
if (empty($primary_key)) { |
| 64 |
|
return FALSE; |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
// Accept a string if the primary key is one column |
| 68 |
|
$return_simple_primary_key = FALSE; |
| 69 |
|
if (!is_array($primary_key)) { |
| 70 |
|
$return_simple_primary_key = TRUE; |
| 71 |
|
$primary_key = array($primary_key); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
// Convert a passed object to an array |
| 75 |
|
$fields = (array) $fields; |
| 76 |
|
|
| 77 |
|
// Check if primary key columns are specified |
| 78 |
|
$primary_key_set = TRUE; |
| 79 |
|
foreach ($primary_key as $column) { |
| 80 |
|
if (!array_key_exists($column, $fields)) { |
| 81 |
|
$primary_key_set = FALSE; |
| 82 |
|
} |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
// Update |
| 86 |
|
if ($primary_key_set) { |
| 87 |
|
$sql = 'UPDATE {' . db_escape_table($table) . '} SET '; |
| 88 |
|
$updates = array(); |
| 89 |
|
foreach ($fields as $key => $value) { |
| 90 |
|
if (!in_array($key, $primary_key)) { |
| 91 |
|
if ($value !== NULL) { |
| 92 |
|
$updates[] = $key . ' = "' . db_escape_string($value) . '"'; |
| 93 |
|
} |
| 94 |
|
else { |
| 95 |
|
$updates[] = $key . ' = NULL'; |
| 96 |
|
} |
| 97 |
|
} |
| 98 |
|
} |
| 99 |
|
$sql .= implode(',', $updates) . ' '; |
| 100 |
|
$where .= ' WHERE 1 '; |
| 101 |
|
|
| 102 |
|
foreach ($primary_key as $column) { |
| 103 |
|
if ($fields[$column] !== NULL) { |
| 104 |
|
$where .= 'AND ' . $column . ' = "' . $fields[$column] . '" '; |
| 105 |
|
} |
| 106 |
|
else { |
| 107 |
|
$where .= 'AND ' . $column . ' IS NULL '; |
| 108 |
|
} |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
$sql .= $where; |
| 112 |
|
|
| 113 |
|
db_query($sql); |
| 114 |
|
|
| 115 |
|
// Return if we've actually updated something |
| 116 |
|
if (db_affected_rows()) { |
| 117 |
|
// If we were passed a simple primary key, return the same |
| 118 |
|
if ($return_simple_primary_key) { |
| 119 |
|
return $fields[$primary_key[0]]; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
|
$return_fields = array(); |
| 123 |
|
foreach ($primary_key as $column) { |
| 124 |
|
$return_fields[$column] = $fields[$column]; |
| 125 |
|
} |
| 126 |
|
return $return_fields; |
| 127 |
|
} |
| 128 |
|
|
| 129 |
|
// Return if there was nothing to change, but the row actually exists |
| 130 |
|
$sql = 'SELECT COUNT(*) FROM {' . db_escape_table($table) . '} ' . $where; |
| 131 |
|
$exists = db_result(db_query($sql, 0, 1)); |
| 132 |
|
if ($exists) { |
| 133 |
|
$return_fields = array(); |
| 134 |
|
foreach ($primary_key as $column) { |
| 135 |
|
$return_fields[$column] = $fields[$column]; |
| 136 |
|
} |
| 137 |
|
return $return_fields; |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
else { |
| 141 |
|
// We can't autogenerate if the primary key is more than one column |
| 142 |
|
if (count($primary_key) > 1) { |
| 143 |
|
return FALSE; |
| 144 |
|
} |
| 145 |
|
$fields[$primary_key[0]] = pressflow_transaction_db_next_id('{' . db_escape_table($table) . '}_' . $primary_key[0]); |
| 146 |
|
} |
| 147 |
|
|
| 148 |
|
// Insert |
| 149 |
|
$sql = 'INSERT INTO {' . db_escape_table($table) . '} '; |
| 150 |
|
$keys = array_keys($fields); |
| 151 |
|
$values = array(); |
| 152 |
|
foreach ($fields as $key => $value) { |
| 153 |
|
if ($value !== NULL) { |
| 154 |
|
$values[] = '"' . db_escape_string($value) . '"'; |
| 155 |
|
} |
| 156 |
|
else { |
| 157 |
|
$values[] = 'NULL'; |
| 158 |
|
} |
| 159 |
|
} |
| 160 |
|
$sql .= '(`' . implode('`,`', $keys) . '`) '; |
| 161 |
|
$sql .= 'VALUES (' . implode(',', $values) . ')'; |
| 162 |
|
db_query($sql); |
| 163 |
|
|
| 164 |
|
// If we were passed a simple primary key, return the same |
| 165 |
|
if ($return_simple_primary_key) { |
| 166 |
|
return $fields[$primary_key[0]]; |
| 167 |
|
} |
| 168 |
|
|
| 169 |
|
$return_fields = array(); |
| 170 |
|
foreach ($primary_key as $column) { |
| 171 |
|
$return_fields[$column] = $fields[$column]; |
| 172 |
|
} |
| 173 |
|
return $return_fields; |
| 174 |
|
} |