2809be596dcea0f46a219e32172bce03e4e19677
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

1) <?php
2) 	function sql_cross($table_left, $table_right)
3) 	{
4) 		$table_result = [];
5) 		array_map
6) 		(
7) 			function ($row_left) use (&$table_left,&$table_right,&$table_result)
8) 			{
9) 				array_map
10) 				(
11) 					function ($row_right) use (&$table_left,&$table_right,&$table_result,&$row_left)
12) 					{
13) 						$row_result = [];
14) 						foreach ($row_left as $key_left => $value_left) $row_result[$key_left] = $value_left;
15) 						foreach ($row_right as $key_right => $value_right) $row_result[$key_right] = $value_right;
16) 						array_push($table_result, $row_result);
17) 					},
18) 					$table_right
19) 				);
20) 			},
21) 			$table_left
22) 		);
23) 		return $table_result;
24) 	}
25) 	
26) 	function sql_cross_all($tables_source)
27) 	{
28) 		$n = count($tables_source);
29) 		if ($n == 1)
30) 		{
31) 			return $tables_source[0];
32) 		}
33) 		else
34) 		{
35) 			return sql_cross(sql_cross_all(array_slice($tables_source, $n-1)), $tables_source[$n-1]);
36) 		}
37) 	}
38) 	
bfadmin-master minor changes

bfadmin-master authored 7 years ago

39) 	function sql_project($table_source, $columns)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

40) 	{
41) 		return (
42) 			array_map
43) 			(
bfadmin-master minor changes

bfadmin-master authored 7 years ago

44) 				function ($row_in) use (&$columns)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

45) 				{
46) 					$row_out = [];
bfadmin-master minor changes

bfadmin-master authored 7 years ago

47) 					foreach ($row_in as $key_in => $value_in)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

48) 					{
bfadmin-master minor changes

bfadmin-master authored 7 years ago

49) 						if (array_search($key_in, $columns) !== false)
50) 						{
51) 							$row_out[$key_in] = $value_in;
52) 						}
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

53) 					}
54) 					return $row_out;
55) 				},
56) 				$table_source
57) 			)
58) 		);
59) 	}
60) 	
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

61) 	function sql_condense($table_source, $columns_from, $columns_to, $condensators)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

62) 	{
63) 		return (
64) 			array_map
65) 			(
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

66) 				function ($row_in) use (&$columns_from,&$columns_to,&$condensators)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

67) 				{
68) 					$row_out = [];
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

69) 					$values = [];
70) 					foreach ($row_in as $column => $value)
bfadmin-master minor changes

bfadmin-master authored 7 years ago

71) 					{
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

72) 						if (array_search($column, $columns_from) === false)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

73) 						{
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

74) 							$row_out[$column] = $row_in[$column];
bfadmin-master minor changes

bfadmin-master authored 7 years ago

75) 						}
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

76) 						else
77) 						{
78) 							$values[$column] = $value;
79) 						}
80) 					}
81) 					foreach ($columns_to as $column)
82) 					{
83) 						$row_out[$column] = (array_key_exists($column, $condensators) ? $condensators[$column]($values) : null);
bfadmin-master minor changes

bfadmin-master authored 7 years ago

84) 					}
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

85) 					return $row_out;
86) 				},
87) 				$table_source
88) 			)
89) 		);
90) 	}
91) 	
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

92) 	function sql_delete($table_source, $columns)
93) 	{
94) 		return sql_condense($table_source, $columns, [], []);
95) 	}
96) 	
bfadmin-master minor changes

bfadmin-master authored 7 years ago

97) 	function sql_add($table_source, $columns, $assigners = [])
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

98) 	{
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

99) 		return sql_condense($table_source, [], $columns, $assigners);
bfadmin-master minor changes

bfadmin-master authored 7 years ago

100) 	}
101) 	
102) 	function sql_rename($table_source, $column_from, $column_to)
103) 	{
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

104) 		return sql_condense($table_source, [$column_from], [$column_to], [$column_to => function ($values) use (&$column_from) {return $values[$column_from];}]);
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

105) 	}
106) 	
107) 	function sql_select($table_source, $predicate)
108) 	{
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

109) 		return array_filter($table_source, $predicate);
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

110) 	}
111) 	
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

112) 	function sql_groups($table_source, $column, $extraction = null)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

113) 	{
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

114) 		if ($extraction == null) $extraction = (function ($column, $row) {return $row[$column];});
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

115) 		$groups = [];
116) 		array_map
117) 		(
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

118) 			function ($row_source) use (&$table_source,&$column,&$extraction,&$groups)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

119) 			{
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

120) 				$value = $extraction($column, $row_source);
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

121) 				$group = null;
122) 				$index = null;
123) 				// foreach ($groups as $group_)
124) 				for ($index_ = 0; $index_ < count($groups); $index_ += 1)
125) 				{
126) 					$group_ = $groups[$index_];
127) 					if ($group_["value"] == $value)
128) 					{
129) 						$group = $group_;
130) 						$index = $index_;
131) 						break;
132) 					}
133) 				}
134) 				if ($group == null)
135) 				{
136) 					$group = ["value" => $value, "members" => []];
137) 					$index = count($groups);
138) 					array_push($groups, $group);
139) 				}
140) 				// array_push($group["members"], $row_source);
141) 				array_push($groups[$index]["members"], $row_source);
142) 			},
143) 			$table_source
144) 		);
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

145) 		return $groups;
146) 	}
Christian Fraß minor changes; move to kora...

Christian Fraß authored 7 years ago

147) 	
Christian Fraß better table-snap

Christian Fraß authored 7 years ago

148) 	function sql_groupify($table_source, $column, $aggregators)
149) 	{
150) 		$groups = sql_groups($table_source, $column);
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 7 years ago

151) 		$table_result = array_map
152) 		(
153) 			function ($group) use (&$column,&$aggregators)
154) 			{
155) 				$row_result = [];
156) 				$row_result[$column] = $group["value"];
157) 				foreach ($aggregators as $aggregator_key => $aggregator_value)
158) 				{
159) 					$args = array_map(function ($member) use (&$aggregator_key) {return $member[$aggregator_key];}, $group["members"]);
160) 					$row_result[$aggregator_key] = $aggregator_value($args);
161) 				}
162) 				return $row_result;
163) 			},
164) 			$groups
165) 		);
166) 		return $table_result;
167) 	}
168) 	
169)  ?>