Skip to content

Commit

Permalink
Bugfix in anyTable.php dbExecListStmt().
Browse files Browse the repository at this point in the history
Other minor improvements.
  • Loading branch information
arnemorken committed Mar 25, 2024
1 parent 73c53c5 commit aa77ba6
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 90 deletions.
55 changes: 26 additions & 29 deletions data/alasql/anyTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,19 @@ anyTable.prototype.initProperties = function(paramOrType)
// Override class properties from properties in paramOrType (if it is an array)
//
if (typeof paramOrType == "object") {
if (paramOrType["id"]) this.id = paramOrType["id"];
if (paramOrType["idKey"]) this.idKey = paramOrType["idKey"];
if (paramOrType["nameKey"]) this.nameKey = paramOrType["nameKey"];
if (paramOrType["orderBy"]) this.orderBy = paramOrType["orderBy"];
if (paramOrType["orderDir"]) this.orderDir = paramOrType["orderDir"];
if (paramOrType["header"] != undefined) this.header = paramOrType["header"];
if (paramOrType["tableName"]) this.tableName = paramOrType["tableName"];
if (paramOrType["tableNameGroup"]) this.tableNameGroup = paramOrType["tableNameGroup"];
if (paramOrType["tableNameGroupLink"]) this.tableNameGroupLink = paramOrType["tableNameGroupLink"];
if (paramOrType["tableFields"]) this.tableFields = paramOrType["tableFields"];
if (paramOrType["tableFieldsLeftJoin"]) this.tableFieldsLeftJoin = paramOrType["tableFieldsLeftJoin"];
if (paramOrType["linkTypes"]) this.linkTypes = paramOrType["linkTypes"];
if (paramOrType["path"]) this.path = paramOrType["path"];
if (paramOrType["id"]) this.id = paramOrType["id"];
if (paramOrType["idKey"]) this.idKey = paramOrType["idKey"];
if (paramOrType["nameKey"]) this.nameKey = paramOrType["nameKey"];
if (paramOrType["orderBy"]) this.orderBy = paramOrType["orderBy"];
if (paramOrType["orderDir"]) this.orderDir = paramOrType["orderDir"];
if (paramOrType["header"] != undefined) this.header = paramOrType["header"];
if (paramOrType["tableName"]) this.tableName = paramOrType["tableName"];
if (paramOrType["tableNameGroup"]) this.tableNameGroup = paramOrType["tableNameGroup"];
if (paramOrType["tableNameGroupLink"]) this.tableNameGroupLink = paramOrType["tableNameGroupLink"];
if (paramOrType["tableFields"]) this.tableFields = paramOrType["tableFields"];
if (paramOrType["tableFieldsLeftJoin"]) this.tableFieldsLeftJoin = paramOrType["tableFieldsLeftJoin"];
if (paramOrType["linkTypes"]) this.linkTypes = paramOrType["linkTypes"];
if (paramOrType["path"]) this.path = paramOrType["path"];
}
//
// Set defaults if not set yet
Expand Down Expand Up @@ -583,6 +583,10 @@ anyTable.prototype.dbSearchItemListOfType = async function(linkType,groupId,grou

//////////////////////////////// List search ////////////////////////////////

//
// Search database for a list
// Returns true on success, false on error
//
anyTable.prototype.dbSearchList = async function(options)
{
if (!this.type) {
Expand Down Expand Up @@ -664,34 +668,33 @@ anyTable.prototype.dbSearchList = async function(options)
return Promise.resolve(this.data);
}; // dbSearchList

anyTable.prototype.dbExecListStmt = async function(groupId,type,linkType,linkId,grouping,simple,groupType,searchTerm)
anyTable.prototype.dbExecListStmt = function(groupId,type,linkType,linkId,grouping,simple,groupType,searchTerm)
{
// Build and execute the query for a group
if (groupId == "nogroup")
groupId = null;
let stmt = this.dbPrepareSearchListStmt(groupId,type,linkType,linkId,grouping,groupType,searchTerm);
//console.log("dbExecListStmt1:"+stmt);
let self = this;
return await alasql.promise(stmt)
return alasql.promise(stmt)
.then( function(rows) {
let success = self.getRowData(rows,"list",grouping,simple);
self.numResults += rows.length;
//console.log("dbExecListStmt, raw list data:"); console.log(data);
return Promise.resolve(success);
});
return success;
}; // dbExecListStmt

// Get query fragments and build the query
anyTable.prototype.dbPrepareSearchListStmt = function(groupId,type,linkType,linkId,grouping,groupType,searchTerm)
{
let table = this.findLinkTableName(linkType);
// Get query fragments
let select = this.findListSelect (table,groupId,type,linkType,linkId,grouping);
let left_join = this.findListLeftJoin(table,groupId,type,linkType,linkId,grouping);
let where = this.findListWhere (table,groupId,type,linkType,linkId,grouping,groupType,searchTerm);
let link_tablename = this.findLinkTableName(linkType);

let select = this.findListSelect (link_tablename,groupId,type,linkType,linkId,grouping);
let left_join = this.findListLeftJoin(link_tablename,groupId,type,linkType,linkId,grouping);
let where = this.findListWhere (link_tablename,groupId,type,linkType,linkId,grouping,groupType,searchTerm);
let order_by = this.findListOrderBy();

// Build the query
let stmt = select+
"FROM "+this.tableName+" "+
left_join+
Expand Down Expand Up @@ -759,10 +762,8 @@ anyTable.prototype.findListLeftJoinOne = function(groupId,linkType,linkId,groupi
{
let linktable = this.findLinkTableName(linkType);
let typetable = this.findTypeTableName(linkType);

let linktable_id = this.findLinkTableId(linkType);
let typetable_id = this.findTypeTableId(linkType);

let has_linktable = this.tableExists(linktable);
let has_typetable = this.tableExists(typetable);

Expand Down Expand Up @@ -915,7 +916,6 @@ anyTable.prototype.getRowData = function(rows,mode,grouping,simple)
{
if (!this.data)
this.data = {};

for (let i=0; i<rows.length; i++) {
//console.log(i+":"+JSON.stringify(rows[i]));
let gid = rows[i]["group_id"]
Expand Down Expand Up @@ -1173,7 +1173,7 @@ anyTable.prototype.buildDataTree = function(flatdata,parentId)
pid = subdata["parent_id"];
delete subdata["parent_id"];
}
if (typeof subdata === "object" || typeof subdata === "array") {
if (typeof subdata === "object") {
if (!subdata["parent_id"])
delete subdata["parent_id"]; // = null;
if (subdata["parent_id"] == parentId) {
Expand Down Expand Up @@ -1237,9 +1237,6 @@ anyTable.prototype.dbAttachToGroups = function(group_tree,data_tree)
}
} // if idx
} // if group
else {
//console.log(gid); console.log(group);
}
} // for
} // if group_tree
}; // dbAttachToGroups
Expand Down
96 changes: 49 additions & 47 deletions data/mysql/anyTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,10 @@ protected function findItemSelect($includeUser)
$si = "SELECT DISTINCT ".$this->mTableName.".* ";

// Select from left joined user table (if this is not a user table)
if ($includeUser)
if ($includeUser && isset($this->mTableFieldsLeftJoin["user"]))
foreach ($this->mTableFieldsLeftJoin["user"] as $field)
$si .= ", ".$this->mTableNameUserLink.".".$field;
if (isset($field))
$si .= ", ".$this->mTableNameUserLink.".".$field;
// Get parent name
if ($this->hasParentId())
$si .= ", temp.".$this->mNameKey." AS parent_name";
Expand Down Expand Up @@ -785,8 +786,10 @@ protected function dbSearchItemLists($grouping)
return false;
}
// Search through all registered link types/tables
foreach ($this->mLinkTypes as $i => $link_type)
$this->dbSearchItemListOfType($link_type,$grouping);
if (isset($this->mLinkType)) {
foreach ($this->mLinkTypes as $i => $link_type)
$this->dbSearchItemListOfType($link_type,$grouping);
}
return true;
} // dbSearchItemLists

Expand Down Expand Up @@ -978,13 +981,12 @@ protected function dbExecListStmt($gid=null,$type=null,$linkType=null,$linkId=nu
else
$gr_idx = $gid;
$this->mData[$gr_idx]["grouping_num_results"] = $row["num_results"];
$this->mNumResults += $row["num_results"];
//elog("num_res:".$this->mNumResults);
$this->mNumResults .= $row["num_results"];
}
} // if
else {
// Report back number of elements in groups
if (!$gid || $gid == "")
if ((!$gid && $gid !== 0) || $gid == "")
$gr_idx = "nogroup";
else
if (isInteger($gid))
Expand All @@ -994,23 +996,24 @@ protected function dbExecListStmt($gid=null,$type=null,$linkType=null,$linkId=nu
if (array_key_exists($gr_idx,$this->mData)) {
$n = sizeof($this->mData[$gr_idx]);
$this->mData[$gr_idx]["grouping_num_results"] = $n;
$this->mNumResults += $n;
$this->mNumResults .= $n;
}
}
return $success;
} // dbExecListStmt

// Get query fragments and build the query
protected function dbPrepareSearchListStmt($gid=null,$type=null,$linkType=null,$linkId=null,$grouping=true)
{
// Get query fragments
$linktable_name = $this->findLinkTableName($linkType);

$search_term = Parameters::get("term");
$group_type = Parameters::get("group_type");
$select = $this->findListSelect ($gid,$type,$linkType,$linkId,$grouping);
$left_join = $this->findListLeftJoin($gid,$type,$linkType,$linkId,$grouping);
$where = $this->findListWhere ($gid,$type,$linkType,$linkId,$grouping,$group_type,$search_term);
$select = $this->findListSelect ($linktable_name,$gid,$type,$linkType,$linkId,$grouping);
$left_join = $this->findListLeftJoin($linktable_name,$gid,$type,$linkType,$linkId,$grouping);
$where = $this->findListWhere ($linktable_name,$gid,$type,$linkType,$linkId,$grouping,$group_type,$search_term);
$order_by = $this->findListOrderBy ();

// Build the query
$stmt = $select.
"FROM ".$this->mTableName." ".
$left_join.
Expand All @@ -1019,7 +1022,7 @@ protected function dbPrepareSearchListStmt($gid=null,$type=null,$linkType=null,$
return $stmt;
} // dbPrepareSearchListStmt

protected function findListSelect($gid,$type=null,$linkType=null,$linkId=null,$grouping=true)
protected function findListSelect($linkTableName,$gid,$type=null,$linkType=null,$linkId=null,$grouping=true)
{
// Select from own table
$sl = "SELECT DISTINCT ".$this->mTableName.".* ";
Expand Down Expand Up @@ -1048,7 +1051,7 @@ protected function findListSelect($gid,$type=null,$linkType=null,$linkId=null,$g
return $sl;
} // findListSelect

protected function findListLeftJoin($gid,$type=null,$linkType=null,$linkId=null,$grouping=true)
protected function findListLeftJoin($linkTableName,$gid,$type=null,$linkType=null,$linkId=null,$grouping=true)
{
$cur_uid = $this->mPermission["current_user_id"];
$lj = "";
Expand All @@ -1073,18 +1076,16 @@ protected function findListLeftJoinOne($cur_uid,$gid,$type=null,$linkType=null,$
$linktable = $this->findLinkTableName($linkType);
$typetable = $this->findTypeTableName($linkType);
$metatable = $this->findMetaTableName($linkType);

$linktable_id = $this->findLinkTableId($linkType);
$typetable_id = $this->findTypeTableId($linkType);
$metatable_id = $this->findMetaTableId($linkType);

$has_linktable = $this->tableExists($linktable);
$has_typetable = $this->tableExists($typetable);
$has_metatable = $this->tableExists($metatable);

$lj = "";
if ($has_linktable) {
$lj .= "LEFT JOIN ".$linktable." ON CAST(".$linktable.".".$this->mIdKey. " AS INT)=CAST(".$this->mTableName.".".$this->mIdKeyTable." AS INT) ";
$lj .= "LEFT JOIN ".$linktable." ON CAST(".$linktable.".".$this->mIdKey." AS INT)=CAST(".$this->mTableName.".".$this->mIdKeyTable." AS INT) ";
if ($this->hasParentId() && $linkId == null)
$lj .= "OR ".$linktable.".".$this->mIdKeyTable."=temp.".$this->mIdKeyTable." ";
if (!isset($linkType) && $linkType == "user" && $cur_uid)
Expand All @@ -1109,7 +1110,7 @@ protected function findListLeftJoinOne($cur_uid,$gid,$type=null,$linkType=null,$
return $lj;
} // findListLeftJoinOne

protected function findListWhere($gid,$type=null,$linkType=null,$linkId=null,$grouping=true,$groupType=null,$searchTerm="")
protected function findListWhere($linkTableName,$gid,$type=null,$linkType=null,$linkId=null,$grouping=true,$groupType=null,$searchTerm="")
{
$where = null;
$skipOwnType = $linkType == $type;
Expand Down Expand Up @@ -1384,7 +1385,7 @@ protected function getRowData($mode,$grouping=true,$simple=false)
? $nextrow[$this->mIdKeyTable]
: null;
if (!$idx && $idx !== 0)
continue;
continue; // Ignore element without id

// Force idx to be a string in order to maintain ordering when sending JSON data to a json client
$idx = isInteger($idx) ? "+".$idx : $idx;
Expand Down Expand Up @@ -1415,7 +1416,7 @@ protected function getRowData($mode,$grouping=true,$simple=false)
// Link tables for item
if (isset($this->mLinkTypes)) {
foreach ($this->mLinkTypes as $i => $link_type) {
if (isset($this->mTableFieldsLeftJoin[$link_type])) {
if (isset($link_type) && isset($this->mTableFieldsLeftJoin[$link_type])) {
for ($t=0; $t<count($this->mTableFieldsLeftJoin[$link_type]); $t++) {
$field = $this->mTableFieldsLeftJoin[$link_type][$t];
if (!$simple || $field == $this->mIdKey || $field == $this->mNameKey || $field == "parent_id")
Expand Down Expand Up @@ -1585,30 +1586,34 @@ protected function buildGroupTreeAndAttach($group_data,$type,$linkId=null,$group
// Make sure parent/child items are present in all groups where parent exists
//vlog("buildGroupTreeAndAttach,data before copying parent/child:",$this->mData);
foreach ($this->mData as $gidx => $grp) {
foreach ($grp as $idx => $item) {
if (isset($item["parent_id"])) {
$pid = $item["parent_id"];
foreach ($this->mData as $gidx2 => &$grp2) {
$item_parent = isset($grp2[$pid])
? $grp2[$pid]
: ( isset($grp2["+".$pid])
? $grp2["+".$pid]
: null );
if ($item_parent && $gidx2 != $gidx) {
//elog("found child $idx in group $gidx with parent $pid...");
if (!isset($grp2[$idx]) && !isset($grp2["+".$idx]))
$grp2[$idx] = $item; // Copy child to other group
if (!isset($grp[$pid]) && !isset($grp["+".$pid])) {
$name = $item[$this->mNameKey];
$err = "Warning: Item $idx ($name) does not have parent in same group. "; // TODO i18n
$this->setMessage($err);
error_log($err);
if (isset($grp)) {
foreach ($grp as $idx => $item) {
if (isset($item["parent_id"])) {
$pid = $item["parent_id"];
foreach ($this->mData as $gidx2 => &$grp2) {
if (isset($grp2)) {
$item_parent = isset($grp2[$pid])
? $grp2[$pid]
: ( isset($grp2["+".$pid])
? $grp2["+".$pid]
: null );
if ($item_parent && $gidx2 != $gidx) {
//elog("found child $idx in group $gidx with parent $pid...");
if (!isset($grp2[$idx]) && !isset($grp2["+".$idx]))
$grp2[$idx] = $item; // Copy child to other group
if (!isset($grp[$pid]) && !isset($grp["+".$pid])) {
$name = $item[$this->mNameKey];
$err = "Warning: Item $idx ($name) does not have parent in same group. "; // TODO i18n
$this->setMessage($err);
error_log($err);
}
}
}
}
} // foreach
}
}
}
}
} // foreach
} // if grp
} // foreach

// Build data tree
//vlog("buildGroupTreeAndAttach,group_data:", $group_data);
Expand Down Expand Up @@ -1787,12 +1792,10 @@ private function dbAttachToGroups(&$group_tree,$data_tree,$type)
if (isset($data_tree[$idx]["data"]) && $data_tree[$idx]["data"] != "") {
$group["head"] = "group";
if ($type != "group") {
// TODO! Not tested!
if (isset($group["list"])) unset($group["list"]);
if (isset($group["item"])) unset($group["item"]);
}
if (array_key_exists("data",$group) && !isset($group["data"]) && $group["data"] != "")
$group["data"] = array();
if (array_key_exists("data",$group) && !isset($group["data"]) && $group["data"] != "") $group["data"] = array(); // TODO! Is this correct?
foreach ($data_tree[$idx]["data"] as $id => $obj)
$group["data"][$id] = $data_tree[$idx]["data"][$id];
}
Expand Down Expand Up @@ -1850,7 +1853,6 @@ public function prepareData($type)

$this->mData = $data;
//vlog("data after prepare:",$data);

return $this->mData;
} // prepareData

Expand Down
4 changes: 2 additions & 2 deletions data/mysql/types/document/documentTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ protected function initFilters($filters)
/////////////////////// Database query fragments ////////////////////////////
/////////////////////////////////////////////////////////////////////////////

protected function findListWhere($gid,$type=null,$linkType=null,$linkId=null,$grouping=true,$groupType=null,$searchTerm="")
protected function findListWhere($linkTableName,$gid,$type=null,$linkType=null,$linkId=null,$grouping=true,$groupType=null,$searchTerm="")
{
$where = parent::findListWhere($gid,$type,$linkType,$linkId,$grouping,$groupType,$searchTerm);
$where = parent::findListWhere($linkTableName,$gid,$type,$linkType,$linkId,$grouping,$groupType,$searchTerm);
return $where;
} // findListWhere

Expand Down
4 changes: 2 additions & 2 deletions data/mysql/types/event/eventTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ public function hasParentId()
/////////////////////// Database query fragments ////////////////////////////
/////////////////////////////////////////////////////////////////////////////

protected function findListWhere($gid,$type=null,$linkType=null,$linkId=null,$grouping=true,$groupType=null,$searchTerm="")
protected function findListWhere($linkTableName,$gid,$type=null,$linkType=null,$linkId=null,$grouping=true,$groupType=null,$searchTerm="")
{
$where = parent::findListWhere($gid,$type,$linkType,$linkId,$grouping,$groupType,$searchTerm);
$where = parent::findListWhere($linkTableName,$gid,$type,$linkType,$linkId,$grouping,$groupType,$searchTerm);
$w = "";
$event_date_start = Parameters::get("event_date_start");
$event_date_end = Parameters::get("event_date_end");
Expand Down
6 changes: 4 additions & 2 deletions data/mysql/types/group/groupTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ protected function dbValidateInsert()
// Return info of one or all groups of a given type
protected function dbSearchGroupInfo($type=null,$group_id=null)
{
if (!$this->tableExists($this->getTableName()))
return null;

$data = array();
if ($group_id != "nogroup") { // No need to search if we dont want a group!
$stmt = "SELECT ".$this->getTableName().".group_id,".
Expand All @@ -188,8 +191,7 @@ protected function dbSearchGroupInfo($type=null,$group_id=null)
}
$stmt .= $where."ORDER BY group_sort_order,group_id,group_type";
//error_log("dbSearchGroupInfo:".$stmt);
if (!$this->tableExists($this->getTableName()) ||
!$this->query($stmt))
if (!$this->query($stmt))
error_log("Warning: No group tree. ");

while (($nextrow = $this->getNext(true)) != null) {
Expand Down
Loading

0 comments on commit aa77ba6

Please sign in to comment.