git.schokokeks.org
Repositories
Help
Report an Issue
fs-draft.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
adddf67
Branches
Tags
master
midgard
vorlage
fs-draft.git
source
logic
server
sql.php
better table-snap
Christian Fraß
commited
adddf67
at 2016-08-08 20:58:42
sql.php
Blame
History
Raw
<?php function sql_cross($table_left, $table_right) { $table_result = []; array_map ( function ($row_left) use (&$table_left,&$table_right,&$table_result) { array_map ( function ($row_right) use (&$table_left,&$table_right,&$table_result,&$row_left) { $row_result = []; foreach ($row_left as $key_left => $value_left) $row_result[$key_left] = $value_left; foreach ($row_right as $key_right => $value_right) $row_result[$key_right] = $value_right; array_push($table_result, $row_result); }, $table_right ); }, $table_left ); return $table_result; } function sql_cross_all($tables_source) { $n = count($tables_source); if ($n == 1) { return $tables_source[0]; } else { return sql_cross(sql_cross_all(array_slice($tables_source, $n-1)), $tables_source[$n-1]); } } function sql_project($table_source, $columns) { return ( array_map ( function ($row_in) use (&$columns) { $row_out = []; foreach ($row_in as $key_in => $value_in) { if (array_search($key_in, $columns) !== false) { $row_out[$key_in] = $value_in; } } return $row_out; }, $table_source ) ); } function sql_condense($table_source, $columns_from, $columns_to, $condensators) { return ( array_map ( function ($row_in) use (&$columns_from,&$columns_to,&$condensators) { $row_out = []; $values = []; foreach ($row_in as $column => $value) { if (array_search($column, $columns_from) === false) { $row_out[$column] = $row_in[$column]; } else { $values[$column] = $value; } } foreach ($columns_to as $column) { $row_out[$column] = (array_key_exists($column, $condensators) ? $condensators[$column]($values) : null); } return $row_out; }, $table_source ) ); } function sql_delete($table_source, $columns) { return sql_condense($table_source, $columns, [], []); } function sql_add($table_source, $columns, $assigners = []) { return sql_condense($table_source, [], $columns, $assigners); } function sql_rename($table_source, $column_from, $column_to) { return sql_condense($table_source, [$column_from], [$column_to], [$column_to => function ($values) use (&$column_from) {return $values[$column_from];}]); } function sql_select($table_source, $predicate) { return array_filter($table_source, $predicate); } function sql_groups($table_source, $column, $extraction = null) { if ($extraction == null) $extraction = (function ($column, $row) {return $row[$column];}); $groups = []; array_map ( function ($row_source) use (&$table_source,&$column,&$extraction,&$groups) { $value = $extraction($column, $row_source); $group = null; $index = null; // foreach ($groups as $group_) for ($index_ = 0; $index_ < count($groups); $index_ += 1) { $group_ = $groups[$index_]; if ($group_["value"] == $value) { $group = $group_; $index = $index_; break; } } if ($group == null) { $group = ["value" => $value, "members" => []]; $index = count($groups); array_push($groups, $group); } // array_push($group["members"], $row_source); array_push($groups[$index]["members"], $row_source); }, $table_source ); return $groups; } function sql_groupify($table_source, $column, $aggregators) { $groups = sql_groups($table_source, $column); $table_result = array_map ( function ($group) use (&$column,&$aggregators) { $row_result = []; $row_result[$column] = $group["value"]; foreach ($aggregators as $aggregator_key => $aggregator_value) { $args = array_map(function ($member) use (&$aggregator_key) {return $member[$aggregator_key];}, $group["members"]); $row_result[$aggregator_key] = $aggregator_value($args); } return $row_result; }, $groups ); return $table_result; } /* +------+------+------+ | xA | xB | xC | +------+------+------+ | a2 | b1 | c3 | +------+------+------+ | a1 | b1 | c0 | +------+------+------+ | a1 | b3 | c2 | +------+------+------+ | a2 | b2 | c4 | +------+------+------+ | a1 | b2 | c1 | +------+------+------+ | a2 | b4 | c5 | +------+------+------+ +------+------+------+ | xA | xB | xC | +------+------+------+ | a1 | b1 | c0 | +------+------+------+ | a1 | b2 | c1 | +------+------+------+ | a1 | b3 | c2 | +------+------+------+ | a2 | b1 | c3 | +------+------+------+ | a2 | b2 | c4 | +------+------+------+ | a2 | b4 | c5 | +------+------+------+ +------+------+------+ | xA | xB | xC | +------+------+------+ | a1 | b1 | c0 | | +------+------+ | | b2 | c1 | | +------+------+ | | b3 | c2 | +------+------+------+ | a2 | b1 | c3 | | +------+------+ | | b2 | c4 | | +------+------+ | | b4 | c5 | +------+------+------+ +------+------+------+------+------+ | xA | xB:b1| xB:b2| xB:b3| xB:b4| +------+------+------+------+------+ | a1 | c0 | c1 | c2 | -- | +------+------+------+------+------+ | a2 | c3 | c4 | -- | c5 | +------+------+------+------+------+ */ function sql_snap($table_source, $column_vertical, $column_horizontal, $column_data) { $values = []; array_map ( function ($row) use (&$values,&$column_horizontal) { $value = $row[$column_horizontal]; if (array_search($value, $values) === false) array_push($values, $value); }, $table_source ); $groups = sql_groups($table_source, $column_vertical); // print(json_encode(array_slice($groups, 0, 2)) . "\n"); $table_result = array_map ( function ($group) use (&$column_vertical,&$column_horizontal,&$column_data,&$values) { $row = []; $row[$column_vertical] = $group["value"]; array_map ( function ($value) use (&$row) { $row[$value] = []; }, $values ); array_map ( function ($member) use (&$column_horizontal,&$column_data,&$row) { $value = $member[$column_horizontal]; $data = $member[$column_data]; // print("-- adding value " . json_encode($data) . " for field " . $value . " to dataset " . json_encode($row) . "\n"); array_push($row[$value], $data); }, $group["members"] ); return $row; }, $groups ); return $table_result; } /* function sql_sort($table_source, $column) { } */ ?>