Index: trunk/phase3/includes/DatabaseMssql.php |
— | — | @@ -4,7 +4,7 @@ |
5 | 5 | * - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html) |
6 | 6 | * - Author: [http://www.organicdesign.co.nz/nad User:Nad] |
7 | 7 | * |
8 | | - * {{php}}{{category:Extensions|DatabaseMssql.php}} |
| 8 | + * See maintenance/mssql/README for development notes and other specific information |
9 | 9 | */ |
10 | 10 | |
11 | 11 | /** |
— | — | @@ -14,6 +14,8 @@ |
15 | 15 | |
16 | 16 | var $mAffectedRows; |
17 | 17 | var $mLastResult; |
| 18 | + var $mLastError; |
| 19 | + var $mLastErrorNo; |
18 | 20 | var $mDatabaseFile; |
19 | 21 | |
20 | 22 | /** |
— | — | @@ -135,11 +137,20 @@ |
136 | 138 | } |
137 | 139 | |
138 | 140 | /** |
139 | | - * MSSQL doesn't seem to do buffered results |
| 141 | + * - MSSQL doesn't seem to do buffered results |
| 142 | + * - the trasnaction syntax is modified here to avoid having to replicate |
| 143 | + * Database::query which uses BEGIN, COMMIT, ROLLBACK |
140 | 144 | */ |
141 | 145 | function doQuery($sql) { |
142 | | - print "<pre>\n$sql\n</pre>"; |
| 146 | + if ($sql == 'BEGIN' || $sql == 'COMMIT' || $sql == 'ROLLBACK') return true; # $sql .= ' TRANSACTION'; |
| 147 | + $sql = preg_replace('|[^\x07-\x7e]|','?',$sql); # TODO: need to fix unicode - just removing it here while testing |
143 | 148 | $ret = mssql_query($sql, $this->mConn); |
| 149 | + if ($ret === false) { |
| 150 | + $err = mssql_get_last_message(); |
| 151 | + if ($err) $this->mlastError = $err; |
| 152 | + $row = mssql_fetch_row(mssql_query('select @@ERROR')); |
| 153 | + if ($row[0]) $this->mlastErrorNo = $row[0]; |
| 154 | + } else $this->mlastErrorNo = false; |
144 | 155 | return $ret; |
145 | 156 | } |
146 | 157 | |
— | — | @@ -172,7 +183,7 @@ |
173 | 184 | $res = $res->result; |
174 | 185 | } |
175 | 186 | @/**/$row = mssql_fetch_object( $res ); |
176 | | - if( $this->lastErrno() ) { |
| 187 | + if ( $this->lastErrno() ) { |
177 | 188 | throw new DBUnexpectedError( $this, 'Error in fetchObject(): ' . htmlspecialchars( $this->lastError() ) ); |
178 | 189 | } |
179 | 190 | return $row; |
— | — | @@ -205,7 +216,7 @@ |
206 | 217 | $res = $res->result; |
207 | 218 | } |
208 | 219 | @/**/$n = mssql_num_rows( $res ); |
209 | | - if( $this->lastErrno() ) { |
| 220 | + if ( $this->lastErrno() ) { |
210 | 221 | throw new DBUnexpectedError( $this, 'Error in numRows(): ' . htmlspecialchars( $this->lastError() ) ); |
211 | 222 | } |
212 | 223 | return $n; |
— | — | @@ -264,16 +275,14 @@ |
265 | 276 | * Get the last error number |
266 | 277 | */ |
267 | 278 | function lastErrno() { |
268 | | - $row = mssql_fetch_row(mssql_query('select @@ERROR')); |
269 | | - return $row[0]; |
| 279 | + return $this->mlastErrorNo; |
270 | 280 | } |
271 | 281 | |
272 | 282 | /** |
273 | 283 | * Get a description of the last error |
274 | 284 | */ |
275 | 285 | function lastError() { |
276 | | - return mssql_get_last_message(); |
277 | | - return $error; |
| 286 | + return $this->mlastError; |
278 | 287 | } |
279 | 288 | |
280 | 289 | /** |
— | — | @@ -600,7 +609,7 @@ |
601 | 610 | # If multiple and ignore, then do each row as a separate conditional insert |
602 | 611 | foreach ($a as $row) { |
603 | 612 | $prival = $row[$keys[0]]; |
604 | | - $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = $prival) $sql"; |
| 613 | + $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = '$prival') $sql"; |
605 | 614 | if (!$this->query("$sql (".$this->makeListWithoutNulls($row).')', $fname)) return false; |
606 | 615 | } |
607 | 616 | return true; |
— | — | @@ -614,7 +623,7 @@ |
615 | 624 | } else { |
616 | 625 | if ($ignore) { |
617 | 626 | $prival = $a[$keys[0]]; |
618 | | - $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = $prival) $sql"; |
| 627 | + $sql = "IF NOT EXISTS (SELECT * FROM $table WHERE $keys[0] = '$prival') $sql"; |
619 | 628 | } |
620 | 629 | $sql .= '('.$this->makeListWithoutNulls($a).')'; |
621 | 630 | } |
— | — | @@ -681,10 +690,10 @@ |
682 | 691 | } |
683 | 692 | |
684 | 693 | /** |
685 | | - * Use MySQL's naming (accounts for prefix etc) but remove surrounding backticks |
| 694 | + * MSSQL has a problem with the backtick quoting, so all this does is ensure the prefix is added exactly once |
686 | 695 | */ |
687 | 696 | function tableName($name) { |
688 | | - return str_replace('`','',parent::tableName($name)); |
| 697 | + return strpos($name, $this->mTablePrefix) === 0 ? $name : "{$this->mTablePrefix}$name"; |
689 | 698 | } |
690 | 699 | |
691 | 700 | /** |
— | — | @@ -867,31 +876,6 @@ |
868 | 877 | |
869 | 878 | /** |
870 | 879 | * Begin a transaction, committing any previously open transaction |
871 | | - */ |
872 | | - function begin( $fname = 'Database::begin' ) { |
873 | | - $this->query( 'BEGIN TRANSACTION', $fname ); |
874 | | - $this->mTrxLevel = 1; |
875 | | - } |
876 | | - |
877 | | - /** |
878 | | - * End a transaction |
879 | | - */ |
880 | | - function commit( $fname = 'Database::commit' ) { |
881 | | - $this->query( 'COMMIT TRANSACTION', $fname ); |
882 | | - $this->mTrxLevel = 0; |
883 | | - } |
884 | | - |
885 | | - /** |
886 | | - * Rollback a transaction. |
887 | | - * No-op on non-transactional databases. |
888 | | - */ |
889 | | - function rollback( $fname = 'Database::rollback' ) { |
890 | | - $this->query( 'ROLLBACK TRANSACTION', $fname, true ); |
891 | | - $this->mTrxLevel = 0; |
892 | | - } |
893 | | - |
894 | | - /** |
895 | | - * Begin a transaction, committing any previously open transaction |
896 | 880 | * @deprecated use begin() |
897 | 881 | */ |
898 | 882 | function immediateBegin( $fname = 'Database::immediateBegin' ) { |
— | — | @@ -972,7 +956,7 @@ |
973 | 957 | $mssql_tmpl = "$IP/maintenance/mssql/tables.sql"; |
974 | 958 | |
975 | 959 | # Make an MSSQL template file if it doesn't exist (based on the same one MySQL uses to create a new wiki db) |
976 | | - if (1 || !file_exists($mssql_tmpl)) { # todo: make this conditional again |
| 960 | + if (!file_exists($mssql_tmpl)) { # todo: make this conditional again |
977 | 961 | $sql = file_get_contents($mysql_tmpl); |
978 | 962 | $sql = preg_replace('/^\s*--.*?$/m','',$sql); # strip comments |
979 | 963 | $sql = preg_replace('/^\s*(UNIQUE )?(INDEX|KEY|FULLTEXT).+?$/m', '', $sql); # These indexes should be created with a CREATE INDEX query |