Fix bookmarks app to work with postgresql.

Rework the bookmark app to manage postgresql.
Add a fetchOne function into the PdoStmtWrapper
Fix a tipo in comments.
This commit is contained in:
Brice Maron 2011-10-16 23:03:03 +02:00
Родитель b0127e3918
Коммит 1f6be85719
4 изменённых файлов: 59 добавлений и 21 удалений

Просмотреть файл

@ -33,6 +33,8 @@ OC_JSON::checkAppEnabled('bookmarks');
$CONFIG_DBTYPE = OC_Config::getValue( "dbtype", "sqlite" );
if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
$_ut = "strftime('%s','now')";
} elseif($CONFIG_DBTYPE == 'pgsql') {
$_ut = 'date_part(\'epoch\',now())::integer';
} else {
$_ut = "UNIX_TIMESTAMP()";
}
@ -51,7 +53,15 @@ $params=array(
OC_User::getUser()
);
$query->execute($params);
$b_id = OC_DB::insertid();
if($CONFIG_DBTYPE == 'pgsql')
{
$query = OC_DB::prepare("SELECT currval('*PREFIX*bookmarks_id_seq')");
$b_id = $query->execute()->fetchOne();
} else {
$b_id = OC_DB::insertid();
}
if($b_id !== false) {
$query = OC_DB::prepare("

Просмотреть файл

@ -33,6 +33,8 @@ OC_JSON::checkAppEnabled('bookmarks');
$CONFIG_DBTYPE = OC_Config::getValue( "dbtype", "sqlite" );
if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
$_ut = "strftime('%s','now')";
} elseif($CONFIG_DBTYPE == 'pgsql') {
$_ut = 'date_part(\'epoch\',now())::integer';
} else {
$_ut = "UNIX_TIMESTAMP()";
}

Просмотреть файл

@ -38,6 +38,9 @@ $filterTag = isset($_GET['tag']) ? '%' . htmlspecialchars_decode($_GET['tag']) .
if($filterTag){
$sqlFilterTag = 'HAVING tags LIKE ?';
$params[] = $filterTag;
if($CONFIG_DBTYPE == 'pgsql' ) {
$sqlFilterTag = 'HAVING array_to_string(array_agg(tag), \' \') LIKE ?';
}
} else {
$sqlFilterTag = '';
}
@ -58,26 +61,40 @@ if( $CONFIG_DBTYPE == 'sqlite' or $CONFIG_DBTYPE == 'sqlite3' ){
$_gc_separator = 'SEPARATOR \' \'';
}
$query = OC_DB::prepare('
SELECT id, url, title,
CASE WHEN *PREFIX*bookmarks.id = *PREFIX*bookmarks_tags.bookmark_id
THEN GROUP_CONCAT( tag ' .$_gc_separator. ' )
ELSE \' \'
END
AS tags
FROM *PREFIX*bookmarks
LEFT JOIN *PREFIX*bookmarks_tags ON 1=1
WHERE (*PREFIX*bookmarks.id = *PREFIX*bookmarks_tags.bookmark_id
OR *PREFIX*bookmarks.id NOT IN (
SELECT *PREFIX*bookmarks_tags.bookmark_id FROM *PREFIX*bookmarks_tags
if($CONFIG_DBTYPE == 'pgsql' ){
$query = OC_DB::prepare('
SELECT id, url, title, array_to_string(array_agg(tag), \' \') as tags
FROM *PREFIX*bookmarks
LEFT JOIN *PREFIX*bookmarks_tags ON *PREFIX*bookmarks.id = *PREFIX*bookmarks_tags.bookmark_id
WHERE
*PREFIX*bookmarks.user_id = ?
GROUP BY id, url, title
'.$sqlFilterTag.'
ORDER BY *PREFIX*bookmarks.'.$sqlSort.'
LIMIT 10
OFFSET ?');
} else {
$query = OC_DB::prepare('
SELECT id, url, title,
CASE WHEN *PREFIX*bookmarks.id = *PREFIX*bookmarks_tags.bookmark_id
THEN GROUP_CONCAT( tag ' .$_gc_separator. ' )
ELSE \' \'
END
AS tags
FROM *PREFIX*bookmarks
LEFT JOIN *PREFIX*bookmarks_tags ON 1=1
WHERE (*PREFIX*bookmarks.id = *PREFIX*bookmarks_tags.bookmark_id
OR *PREFIX*bookmarks.id NOT IN (
SELECT *PREFIX*bookmarks_tags.bookmark_id FROM *PREFIX*bookmarks_tags
)
)
)
AND *PREFIX*bookmarks.user_id = ?
GROUP BY url
'.$sqlFilterTag.'
ORDER BY *PREFIX*bookmarks.'.$sqlSort.'
LIMIT ?, 10');
AND *PREFIX*bookmarks.user_id = ?
GROUP BY url
'.$sqlFilterTag.'
ORDER BY *PREFIX*bookmarks.'.$sqlSort.'
LIMIT ?, 10');
}
$bookmarks = $query->execute($params)->fetchAll();
OC_JSON::success(array('data' => $bookmarks));

Просмотреть файл

@ -28,7 +28,7 @@ class OC_DB {
const BACKEND_PDO=0;
const BACKEND_MDB2=1;
static private $connection; //the prefered conenction to use, either PDO or MDB2
static private $connection; //the prefered connection to use, either PDO or MDB2
static private $backend=null;
static private $MDB2=false;
static private $PDO=false;
@ -480,4 +480,13 @@ class PDOStatementWrapper{
public function __call($name,$arguments){
return call_user_func_array(array($this->statement,$name),$arguments);
}
/**
* Provide a simple fetchOne.
* fetch single column from the next row
* @param int $colnum the column number to fetch
*/
public function fetchOne($colnum = 0){
return $this->statement->fetchColumn($colnum);
}
}