5 * MS SQL extensions for the ADODB DB backend.
7 require_once 'lib/WikiDB/backend/ADODB.php';
9 class WikiDB_backend_ADODB_mssqlnative
10 extends WikiDB_backend_ADODB
15 function WikiDB_backend_ADODB_mssqlnative($dbparams) {
16 // Lowercase Assoc arrays
17 define('ADODB_ASSOC_CASE',0);
19 // Backend constructor
20 $this->WikiDB_backend_ADODB($dbparams);
22 // Empty strings in MSSQL? NULLS?
23 $this->_expressions['notempty'] = "NOT LIKE ''";
24 //doesn't work if content is of the "text" type http://msdn2.microsoft.com/en-us/library/ms188074.aspx
25 $this->_expressions['iscontent'] = "dbo.hasContent({$this->_table_names['version_tbl']}.content)";
27 $this->_prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
35 // Do nothing here -- Leave that for the DB
36 // Cost Based Optimizer tuning vary from version to version
42 function _sql_match_clause($word) {
43 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
44 $word = $this->_dbh->qstr("%$word%");
45 return "LOWER(pagename) LIKE $word";
48 // Fulltext -- case sensitive :-\
49 function _fullsearch_sql_match_clause($word) {
50 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
51 $wordq = $this->_dbh->qstr("%$word%");
52 return "LOWER(pagename) LIKE $wordq "
53 . "OR CHARINDEX(content, '$word') > 0";
59 function _serialize($data) {
62 assert(is_array($data));
63 return addslashes(serialize($data));
69 function _unserialize($data) {
70 return empty($data) ? array() : unserialize(stripslashes($data));
75 * on DEBUG: delete old, deleted links from page
77 function set_links($pagename, $links) {
78 // FIXME: optimize: mysql can do this all in one big INSERT/REPLACE.
81 extract($this->_table_names);
83 $this->lock(array('link'));
84 $pageid = $this->_get_pageid($pagename, true);
86 $oldlinks = $dbh->getAssoc("SELECT $link_tbl.linkto as id, page.pagename FROM $link_tbl"
87 ." JOIN page ON ($link_tbl.linkto = page.id)"
88 ." WHERE linkfrom=$pageid");
89 // Delete current links,
90 $dbh->Execute("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
91 // and insert new links. Faster than checking for all single links
93 foreach ($links as $link) {
94 $linkto = $link['linkto'];
95 if (isset($link['relation']))
96 $relation = $this->_get_pageid($link['relation'], true);
99 if ($linkto === "") { // ignore attributes
103 if (isset($linkseen[$linkto]) and !$relation) {
107 $linkseen[$linkto] = true;
109 $linkid = $this->_get_pageid($linkto, true);
112 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto, relation)"
113 . " VALUES ($pageid, $linkid, $relation)");
115 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
116 . " VALUES ($pageid, $linkid)");
118 if ($oldlinks and array_key_exists($linkid, $oldlinks)) {
119 // This was also in the previous page
120 unset($oldlinks[$linkid]);
124 // purge page table: delete all non-referenced pages
125 // for all previously linked pages, which have no other linkto links
126 if (DEBUG and $oldlinks) {
127 // trigger_error("purge page table: delete all non-referenced pages...", E_USER_NOTICE);
128 foreach ($oldlinks as $id => $name) {
129 // ...check if the page is empty and has no version
131 $result = $dbh->getRow("SELECT $page_tbl.id FROM $page_tbl"
132 . " LEFT JOIN $nonempty_tbl ON ($nonempty_tbl.id = $page_tbl.id)"//'"id" is not a recognized table hints option'
133 . " LEFT JOIN $version_tbl ON ($version_tbl.id = $page_tbl.id)"//'"id" is not a recognized table hints option'
134 . " WHERE $nonempty_tbl.id is NULL"
135 . " AND $version_tbl.id is NULL"
136 . " AND $page_tbl.id=$id");
137 $linkto = $dbh->getRow("SELECT linkfrom FROM $link_tbl WHERE linkto=$id");
138 if ($result and empty($linkto)) {
139 trigger_error("delete empty and non-referenced link $name ($id)", E_USER_NOTICE);
140 $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
141 $dbh->Execute("DELETE FROM $link_tbl WHERE linkto=$id");
142 $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id"); // this purges the link
147 $this->unlock(array('link'));
151 /* get all oldlinks in hash => id, relation
152 check for all new links
154 function set_links1($pagename, $links) {
157 extract($this->_table_names);
159 $this->lock(array('link'));
160 $pageid = $this->_get_pageid($pagename, true);
162 $oldlinks = $dbh->getAssoc("SELECT $link_tbl.linkto as linkto, $link_tbl.relation, page.pagename"
164 ." JOIN page ON ($link_tbl.linkto = page.id)"
165 ." WHERE linkfrom=$pageid");
167 * X => [1,0 2,0 1,1] X => [1,1 3,0]
168 * => delete 1,0 2,0 + insert 3,0
171 foreach ($links as $link) {
172 $linkto = $link['linkto'];
173 if ($link['relation'])
174 $relation = $this->_get_pageid($link['relation'], true);
178 if (isset($linkseen[$linkto]) and !$relation) {
182 $linkseen[$linkto] = true;
184 $linkid = $this->_get_pageid($linkto, true);
187 // find linkfrom,linkto,relation triple in oldlinks
188 foreach ($oldlinks as $l) {
189 if ($relation) { // relation NOT NULL
190 if ($l['linkto'] == $linkid and $l['relation'] == $relation) {
200 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto, relation)"
201 . " VALUES ($pageid, $linkid, $relation)");
203 $dbh->Execute("INSERT INTO $link_tbl (linkfrom, linkto)"
204 . " VALUES ($pageid, $linkid)");
208 if (array_key_exists($linkid, $oldlinks)) {
209 // This was also in the previous page
210 unset($oldlinks[$linkid]);
214 // purge page table: delete all non-referenced pages
215 // for all previously linked pages...
216 if (DEBUG and $oldlinks) {
217 // trigger_error("purge page table: delete all non-referenced pages...", E_USER_NOTICE);
218 foreach ($oldlinks as $id => $name) {
219 // ...check if the page is empty and has no version
221 if ($dbh->getRow("SELECT $page_tbl.id FROM $page_tbl"
222 . " LEFT JOIN $nonempty_tbl ON ($nonempty_tbl.id = $page_tbl.id)"//'"id" is not a recognized table hints option'
223 . " LEFT JOIN $version_tbl ON ($version_tbl.id = $page_tbl.id)"//'"id" is not a recognized table hints option'
224 . " WHERE $nonempty_tbl.id is NULL"
225 . " AND $version_tbl.id is NULL"
226 . " AND $page_tbl.id=$id"))
228 trigger_error("delete empty and non-referenced link $name ($id)", E_USER_NOTICE);
229 $dbh->Execute("DELETE FROM $page_tbl WHERE id=$id"); // this purges the link
230 $dbh->Execute("DELETE FROM $recent_tbl WHERE id=$id"); // may fail
235 $this->unlock(array('link'));
245 // c-hanging-comment-ender-p: nil
246 // indent-tabs-mode: nil