77d8eec65a6d272eeee78097bf5a526d3a45059d
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 7 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 7 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 7 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 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 8 years ago

53) 					}
54) 					return $row_out;
55) 				},
56) 				$table_source
57) 			)
58) 		);
59) 	}
60) 	
bfadmin-master minor changes

bfadmin-master authored 7 years ago

61) 	function sql_delete($table_source, $columns)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

62) 	{
63) 		return (
64) 			array_map
65) 			(
66) 				function ($row_in) use (&$columns)
67) 				{
68) 					$row_out = [];
bfadmin-master minor changes

bfadmin-master authored 7 years ago

69) 					foreach ($row_in as $key_in => $value_in)
70) 					{
71) 						if (array_search($key_in, $columns) === false)
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

72) 						{
bfadmin-master minor changes

bfadmin-master authored 7 years ago

73) 							$row_out[$key_in] = $value_in;
74) 						}
75) 					}
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

76) 					return $row_out;
77) 				},
78) 				$table_source
79) 			)
80) 		);
81) 	}
82) 	
bfadmin-master minor changes

bfadmin-master authored 7 years ago

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

bfadmin-master authored 8 years ago

84) 	{
85) 		return (
86) 			array_map
87) 			(
bfadmin-master minor changes

bfadmin-master authored 7 years ago

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

bfadmin-master authored 8 years ago

89) 				{
90) 					$row_out = $row_in;
bfadmin-master minor changes

bfadmin-master authored 7 years ago

91) 					foreach ($columns as $column)
92) 					{
93) 						$row_out[$column] = (array_key_exists($column, $assigners) ? $assigners[$column]($row_in) : null);
94) 					}
95) 					return $row_out;
96) 				},
97) 				$table_source
98) 			)
99) 		);
100) 	}
101) 	
102) 	function sql_rename($table_source, $column_from, $column_to)
103) 	{
104) 		return (
105) 			array_map
106) 			(
107) 				function ($row_in) use (&$column_from,&$column_to)
108) 				{
109) 					$row_out = [];
110) 					foreach ($row_in as $column => $value)
111) 					{
112) 						$row_out[($column == $column_from) ? $column_to : $column] = $row_in[$column];
113) 					}
bfadmin-master fancy table-stuff, woohoo!

bfadmin-master authored 8 years ago

114) 					return $row_out;
115) 				},
116) 				$table_source
117) 			)
118) 		);
119) 	}
120) 	
121) 	function sql_select($table_source, $predicate)
122) 	{
123) 		return (
124) 			array_filter
125) 			(
126) 				$table_source,
127) 				$predicate
128) 			)
129) 		);
130) 	}
131) 	
132) 	function sql_group($table_source, $column, $aggregators)
133) 	{
134) 		$groups = [];
135) 		array_map
136) 		(
137) 			function ($row_source) use (&$table_source,&$column,&$groups)
138) 			{
139) 				$value = $row_source[$column];
140) 				$group = null;
141) 				$index = null;
142) 				// foreach ($groups as $group_)
143) 				for ($index_ = 0; $index_ < count($groups); $index_ += 1)
144) 				{
145) 					$group_ = $groups[$index_];
146) 					if ($group_["value"] == $value)
147) 					{
148) 						$group = $group_;
149) 						$index = $index_;
150) 						break;
151) 					}
152) 				}
153) 				if ($group == null)
154) 				{
155) 					$group = ["value" => $value, "members" => []];
156) 					$index = count($groups);
157) 					array_push($groups, $group);
158) 				}
159) 				// array_push($group["members"], $row_source);
160) 				array_push($groups[$index]["members"], $row_source);
161) 			},
162) 			$table_source
163) 		);
164) 		$table_result = array_map
165) 		(
166) 			function ($group) use (&$column,&$aggregators)
167) 			{
168) 				$row_result = [];
169) 				$row_result[$column] = $group["value"];
170) 				foreach ($aggregators as $aggregator_key => $aggregator_value)
171) 				{
172) 					$args = array_map(function ($member) use (&$aggregator_key) {return $member[$aggregator_key];}, $group["members"]);
173) 					$row_result[$aggregator_key] = $aggregator_value($args);
174) 				}
175) 				return $row_result;
176) 			},
177) 			$groups
178) 		);
179) 		return $table_result;
180) 	}
181) 	
182) 	function sql_snap($table_source)
183) 	{
184) 		$columns = [];
185) 		$table_result = [];
186) 		array_map
187) 		(
188) 			function ($row_source)
189) 			{
190) 				
191) 			},
192) 			$table_source
193) 		);
194) 		return $table_result;
195) 	}
196) 	
197) 	/*
198) 	function sql_sort($table_source, $column)
199) 	{
200) 		
201) 	}
202) 	 */
203)  ?>