adddf67eec76f436fa75c82b648ce73a414d2a5c
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 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 8 years ago

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

bfadmin-master authored 8 years ago

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

bfadmin-master authored 8 years ago

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

bfadmin-master authored 8 years ago

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

bfadmin-master authored 8 years ago

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

bfadmin-master authored 8 years ago

48) 					{
bfadmin-master minor changes

bfadmin-master authored 8 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 8 years ago

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

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

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

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

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

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

71) 					{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

73) 						{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

75) 						}
Christian Fraß better table-snap

Christian Fraß authored 8 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 8 years ago

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

bfadmin-master authored 8 years ago

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

Christian Fraß authored 8 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 8 years ago

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

bfadmin-master authored 8 years ago

98) 	{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

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

Christian Fraß authored 8 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 8 years ago

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

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

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

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

113) 	{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

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

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

119) 			{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 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 8 years ago

145) 		return $groups;
146) 	}
147) 		
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 8 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) 	
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

169) 	/*
170) 	
171) 	+------+------+------+
172) 	|  xA  |  xB  |  xC  |
173) 	+------+------+------+
174) 	|  a2  |  b1  |  c3  |
175) 	+------+------+------+
176) 	|  a1  |  b1  |  c0  |
177) 	+------+------+------+
178) 	|  a1  |  b3  |  c2  |
179) 	+------+------+------+
180) 	|  a2  |  b2  |  c4  |
181) 	+------+------+------+
182) 	|  a1  |  b2  |  c1  |
183) 	+------+------+------+
184) 	|  a2  |  b4  |  c5  |
185) 	+------+------+------+
186) 	
187) 	+------+------+------+
188) 	|  xA  |  xB  |  xC  |
189) 	+------+------+------+
190) 	|  a1  |  b1  |  c0  |
191) 	+------+------+------+
192) 	|  a1  |  b2  |  c1  |
193) 	+------+------+------+
194) 	|  a1  |  b3  |  c2  |
195) 	+------+------+------+
196) 	|  a2  |  b1  |  c3  |
197) 	+------+------+------+
198) 	|  a2  |  b2  |  c4  |
199) 	+------+------+------+
200) 	|  a2  |  b4  |  c5  |
201) 	+------+------+------+
202) 	
203) 	+------+------+------+
204) 	|  xA  |  xB  |  xC  |
205) 	+------+------+------+
206) 	|  a1  |  b1  |  c0  |
207) 	|      +------+------+
208) 	|      |  b2  |  c1  |
209) 	|      +------+------+
210) 	|      |  b3  |  c2  |
211) 	+------+------+------+
212) 	|  a2  |  b1  |  c3  |
213) 	|      +------+------+
214) 	|      |  b2  |  c4  |
215) 	|      +------+------+
216) 	|      |  b4  |  c5  |
217) 	+------+------+------+
218) 	
219) 	+------+------+------+------+------+
220) 	|  xA  | xB:b1| xB:b2| xB:b3| xB:b4|
221) 	+------+------+------+------+------+
222) 	|  a1  |  c0  |  c1  |  c2  |  --  |
223) 	+------+------+------+------+------+
224) 	|  a2  |  c3  |  c4  |  --  |  c5  |
225) 	+------+------+------+------+------+
226) 	
227) 	 */
228) 	function sql_snap($table_source, $column_vertical, $column_horizontal, $column_data)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

229) 	{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

230) 		$values = [];
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

231) 		array_map
232) 		(
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

233) 			function ($row) use (&$values,&$column_horizontal)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

234) 			{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

235) 				$value = $row[$column_horizontal];
236) 				if (array_search($value, $values) === false) array_push($values, $value);
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

237) 			},
238) 			$table_source
239) 		);
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

240) 		$groups = sql_groups($table_source, $column_vertical);
241) 		// print(json_encode(array_slice($groups, 0, 2)) . "\n");
242) 		$table_result = array_map
243) 		(
244) 			function ($group) use (&$column_vertical,&$column_horizontal,&$column_data,&$values)
245) 			{
246) 				$row = [];
247) 				$row[$column_vertical] = $group["value"];
248) 				array_map
249) 				(
250) 					function ($value) use (&$row)
251) 					{
252) 						$row[$value] = [];
253) 					},
254) 					$values
255) 				);
256) 				array_map
257) 				(
258) 					function ($member) use (&$column_horizontal,&$column_data,&$row)
259) 					{
260) 						$value = $member[$column_horizontal];
261) 						$data = $member[$column_data];
262) 						// print("-- adding value " . json_encode($data) . " for field " . $value . " to dataset " . json_encode($row) . "\n");
263) 						array_push($row[$value], $data);
264) 					},
265) 					$group["members"]
266) 				);
267) 				return $row;
268) 			},
269) 			$groups
270) 		);
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

271) 		return $table_result;
272) 	}
273) 	
274) 	/*
275) 	function sql_sort($table_source, $column)
276) 	{
277) 		
278) 	}
279) 	 */
280)  ?>