Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Fixes for PG (PG9)
Browse files Browse the repository at this point in the history
Use specific SQL comments to avoid parsing for specific sql queries.
  • Loading branch information
cdujeu committed Aug 6, 2014
1 parent 4c09408 commit 8e64ec3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
30 changes: 21 additions & 9 deletions core/src/core/classes/class.AJXP_Utils.php
Expand Up @@ -1690,19 +1690,31 @@ public static function runCreateTablesQuery($p, $file)
$result = array();
$file = dirname($file) ."/". str_replace(".sql", $ext, basename($file) );
$sql = file_get_contents($file);
$parts = explode(";", $sql);
$remove = array();
for ($i = 0 ; $i < count($parts); $i++) {
$part = $parts[$i];
if (strpos($part, "BEGIN") && isSet($parts[$i+1])) {
$parts[$i] .= ';'.$parts[$i+1];
$remove[] = $i+1;
$separators = explode("/** SEPARATOR **/", $sql);

$allParts = array();

foreach($separators as $sep){
$firstLine = array_shift(explode("\n", trim($sep)));
if($firstLine == "/** BLOCK **/"){
$allParts[] = $sep;
}else{
$parts = explode(";", $sep);
$remove = array();
for ($i = 0 ; $i < count($parts); $i++) {
$part = $parts[$i];
if (strpos($part, "BEGIN") && isSet($parts[$i+1])) {
$parts[$i] .= ';'.$parts[$i+1];
$remove[] = $i+1;
}
}
foreach($remove as $rk) unset($parts[$rk]);
$allParts = array_merge($allParts, $parts);
}
}
foreach($remove as $rk) unset($parts[$rk]);
dibi::connect($p);
dibi::begin();
foreach ($parts as $createPart) {
foreach ($allParts as $createPart) {
$sqlPart = trim($createPart);
if (empty($sqlPart)) continue;
try {
Expand Down
12 changes: 9 additions & 3 deletions core/src/plugins/conf.sql/class.sqlConfDriver.php
Expand Up @@ -313,6 +313,9 @@ public function getRepositoryById($repositoryId)
$repo_row = $res->fetchAll();
if (count($repo_row) > 0) {
$repo_row = $repo_row[0];
if($this->sqlDriver["driver"] == "postgre"){
dibi::nativeQuery("SET bytea_output=escape");
}
$res_opts = dibi::query('SELECT * FROM [ajxp_repo_options] WHERE [uuid] = %s', $repo_row['uuid']);
$opts = $res_opts->fetchPairs('name', 'val');
$repository = $this->repoFromDb($repo_row, $opts);
Expand All @@ -335,6 +338,9 @@ public function getRepositoryByAlias($repositorySlug)
$repo_row = $res->fetchAll();
if (count($repo_row) > 0) {
$repo_row = $repo_row[0];
if($this->sqlDriver["driver"] == "postgre"){
dibi::nativeQuery("SET bytea_output=escape");
}
$res_opts = dibi::query('SELECT * FROM [ajxp_repo_options] WHERE [uuid] = %s', $repo_row['uuid']);
$opts = $res_opts->fetchPairs('name', 'val');
$repository = $this->repoFromDb($repo_row, $opts);
Expand Down Expand Up @@ -439,7 +445,7 @@ public function deleteRepository($repositoryId)
case "sqlite":
case "sqlite3":
case "postgre":
dibi::query("SET bytea_output=escape");
dibi::nativeQuery("SET bytea_output=escape");
$children_results = dibi::query('SELECT * FROM [ajxp_roles] WHERE [searchable_repositories] LIKE %~like~ GROUP BY [role_id]', '"'.$repositoryId.'";s:');
break;
case "mysql":
Expand Down Expand Up @@ -608,7 +614,7 @@ public function listRoles($roleIds = array(), $excludeReserved = false)
$wClauses[] = array('[role_id] NOT LIKE %like~', 'AJXP_');
}
if($this->sqlDriver["driver"] == "postgre"){
dibi::query("SET bytea_output=escape");
dibi::nativeQuery("SET bytea_output=escape");
}
$res = dibi::query('SELECT * FROM [ajxp_roles] %if', count($wClauses), 'WHERE %and', $wClauses);
$all = $res->fetchAll();
Expand Down Expand Up @@ -818,7 +824,7 @@ public function simpleStoreClear($storeID, $dataID)
public function simpleStoreGet($storeID, $dataID, $dataType, &$data)
{
if($this->sqlDriver["driver"] == "postgre"){
dibi::query("SET bytea_output=escape");
dibi::nativeQuery("SET bytea_output=escape");
}
$children_results = dibi::query("SELECT * FROM [ajxp_simple_store] WHERE [store_id]=%s AND [object_id]=%s", $storeID, $dataID);
$value = $children_results->fetchAll();
Expand Down
7 changes: 7 additions & 0 deletions core/src/plugins/meta.syncable/create.pgsql
Expand Up @@ -27,6 +27,8 @@ CREATE TABLE ajxp_index (
CREATE INDEX ajxp_index_repo_id ON ajxp_index (repository_identifier);
CREATE INDEX ajxp_index_md5 ON ajxp_index (md5);

/** SEPARATOR **/
/** BLOCK **/
CREATE FUNCTION ajxp_index_delete() RETURNS trigger AS $ajxp_index_delete$
BEGIN
INSERT INTO ajxp_changes (repository_identifier, node_id,source,target,type)
Expand All @@ -35,6 +37,8 @@ CREATE FUNCTION ajxp_index_delete() RETURNS trigger AS $ajxp_index_delete$
END;
$ajxp_index_delete$ LANGUAGE plpgsql;

/** SEPARATOR **/
/** BLOCK **/
CREATE FUNCTION ajxp_index_insert() RETURNS trigger AS $ajxp_index_insert$
BEGIN
INSERT INTO ajxp_changes (repository_identifier, node_id,source,target,type)
Expand All @@ -43,6 +47,8 @@ CREATE FUNCTION ajxp_index_insert() RETURNS trigger AS $ajxp_index_insert$
END;
$ajxp_index_insert$ LANGUAGE plpgsql;

/** SEPARATOR **/
/** BLOCK **/
CREATE FUNCTION ajxp_index_update() RETURNS trigger AS $ajxp_index_update$
BEGIN
IF OLD.node_path = NEW.node_path THEN
Expand All @@ -55,6 +61,7 @@ CREATE FUNCTION ajxp_index_update() RETURNS trigger AS $ajxp_index_update$
RETURN NEW;
END;
$ajxp_index_update$ LANGUAGE plpgsql;
/** SEPARATOR **/

CREATE TRIGGER LOG_DELETE AFTER DELETE ON ajxp_index
FOR EACH ROW EXECUTE PROCEDURE ajxp_index_delete();
Expand Down

0 comments on commit 8e64ec3

Please sign in to comment.