93d9ca56b8bd442ad80ed14bfc5c179bcc6d3947
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ß save

Christian Fraß authored 7 years ago

61) 	function sql_order($table_source, $order)
62) 	{
63) 		usort
64) 		(
65) 			$table_source,
66) 			$order
67) 		);
68) 		return $table_source;
69) 	}
70) 	
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

72) 	{
73) 		return (
74) 			array_map
75) 			(
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

77) 				{
78) 					$row_out = [];
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

81) 					{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

83) 						{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

85) 						}
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

86) 						else
87) 						{
88) 							$values[$column] = $value;
89) 						}
90) 					}
91) 					foreach ($columns_to as $column)
92) 					{
93) 						$row_out[$column] = (array_key_exists($column, $condensators) ? $condensators[$column]($values) : null);
bfadmin-master minor changes

bfadmin-master authored 8 years ago

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

bfadmin-master authored 8 years ago

95) 					return $row_out;
96) 				},
97) 				$table_source
98) 			)
99) 		);
100) 	}
101) 	
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

102) 	function sql_delete($table_source, $columns)
103) 	{
104) 		return sql_condense($table_source, $columns, [], []);
105) 	}
106) 	
bfadmin-master minor changes

bfadmin-master authored 8 years ago

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

bfadmin-master authored 8 years ago

108) 	{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

110) 	}
111) 	
112) 	function sql_rename($table_source, $column_from, $column_to)
113) 	{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

114) 		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

115) 	}
116) 	
117) 	function sql_select($table_source, $predicate)
118) 	{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

120) 	}
121) 	
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

123) 	{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

125) 		$groups = [];
126) 		array_map
127) 		(
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

129) 			{
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

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

bfadmin-master authored 8 years ago

131) 				$group = null;
132) 				$index = null;
133) 				// foreach ($groups as $group_)
134) 				for ($index_ = 0; $index_ < count($groups); $index_ += 1)
135) 				{
136) 					$group_ = $groups[$index_];
137) 					if ($group_["value"] == $value)
138) 					{
139) 						$group = $group_;
140) 						$index = $index_;
141) 						break;
142) 					}
143) 				}
144) 				if ($group == null)
145) 				{
146) 					$group = ["value" => $value, "members" => []];
147) 					$index = count($groups);
148) 					array_push($groups, $group);
149) 				}
150) 				// array_push($group["members"], $row_source);
151) 				array_push($groups[$index]["members"], $row_source);
152) 			},
153) 			$table_source
154) 		);
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

155) 		return $groups;
156) 	}
Christian Fraß minor changes; move to kora...

Christian Fraß authored 8 years ago

157) 	
Christian Fraß better table-snap

Christian Fraß authored 8 years ago

158) 	function sql_groupify($table_source, $column, $aggregators)
159) 	{
160) 		$groups = sql_groups($table_source, $column);
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

161) 		$table_result = array_map
162) 		(
163) 			function ($group) use (&$column,&$aggregators)
164) 			{
165) 				$row_result = [];
166) 				$row_result[$column] = $group["value"];
167) 				foreach ($aggregators as $aggregator_key => $aggregator_value)
168) 				{
169) 					$args = array_map(function ($member) use (&$aggregator_key) {return $member[$aggregator_key];}, $group["members"]);
170) 					$row_result[$aggregator_key] = $aggregator_value($args);
171) 				}
172) 				return $row_result;
173) 			},
174) 			$groups
175) 		);
176) 		return $table_result;
177) 	}
178) 	
179)  ?>