Merge branch 'maint/7.0'
[ninja.git] / src / op5 / ninja_sdk / orm / ORMLSSetGenerator.php
blob0ed544d09ee3054a2b56403af1359cebd186613e
1 <?php
3 require_once("ORMObjectSetGenerator.php");
5 class ORMLSSetGenerator extends ORMObjectSetGenerator {
7 public function generate($skip_generated_note = false) {
8 parent::generate($skip_generated_note);
11 public function generate_backend_specific_functions() {
14 /**
15 * Generate stats
17 * @return void
18 **/
19 public function generate_stats() {
20 $this->init_function('stats',array('intersections'));
21 $this->write('$ls = op5livestatus::instance();');
23 $this->write('$single = !is_array($intersections);');
24 $this->write('if($single) $intersections = array($intersections);');
26 $this->write('$fb_visit = new LivestatusFilterBuilderVisitor(array(%s, "map_name_to_backend"));', $this->structure['class'].'Set'.self::$model_suffix);
27 $this->write('$sb_visit = new LivestatusStatsBuilderVisitor(array(%s, "map_name_to_backend"));', $this->structure['class'].'Set'.self::$model_suffix);
28 $this->write('$ls_filter = $this->filter->visit($fb_visit, false);');
30 $this->write('$ls_intersections = array();');
31 $this->write('foreach( $intersections as $name => $intersection ) {');
32 $this->write('if($intersection->table == $this->table) {');
33 $this->write('$ls_intersections[$name] = $intersection->filter->visit($sb_visit, false);');
34 $this->write('}'); // TODO: Error handling...
35 $this->write('}');
37 $this->write('$result = $ls->stats_single($this->table, $ls_filter, $ls_intersections);');
39 $this->write('if($single) $result = $result[0];');
40 $this->write('return $result;');
41 $this->finish_function();
44 /**
45 * Generate count
47 * @return void
48 **/
49 public function generate_count() {
50 $this->init_function('count');
51 $this->write('$ls = op5livestatus::instance();');
53 $this->write('$filter = $this->get_auth_filter();');
54 $this->write('$fb_visit = new LivestatusFilterBuilderVisitor(array(%s, "map_name_to_backend"));', $this->structure['class'].'Set'.self::$model_suffix);
55 $this->write('$ls_filter = $filter->visit($fb_visit, false);');
56 $this->write('$ls_filter .= "Limit: 0\n";');
58 $this->write('$result = $ls->query($this->table, $ls_filter, false);');
60 $this->write('return $result[2];');
61 $this->finish_function();
64 /**
65 * Generates set
67 * @return void
68 **/
69 public function generate_it() {
70 $this->init_function( 'it', array('columns','order','limit','offset'), array(), array('order' => array(), 'limit'=>false, 'offset'=>false) );
71 $this->write('$ls = op5livestatus::instance();');
73 $this->write('$filter = $this->get_auth_filter();');
74 $this->write('$fb_visit = new LivestatusFilterBuilderVisitor(array(%s, "map_name_to_backend"));', $this->structure['class'].'Set'.self::$model_suffix);
75 $this->write('$ls_filter = $filter->visit($fb_visit, false);');
77 foreach(array('$order','$this->default_sort') as $sortfield) {
78 $this->write('foreach('.$sortfield.' as $col_attr) {');
79 $this->write( '$parts = explode(" ",$col_attr);');
80 $this->write( '$original_part_0 = $parts[0];');
81 $this->write( '$parts[0] = static::map_name_to_backend($parts[0]);');
82 /* Throw exception if column is not found */
83 $this->write( 'if($parts[0] === false) {');
84 $this->write( 'throw new ORMException(%s.$original_part_0."\'");', "Table '".$this->name."' has no column '");
85 $this->write( '}');
86 $this->write( '$parts = array_filter($parts);');
87 $this->write( '$ls_filter .= "Sort: ".implode(" ",$parts)."\n";');
88 $this->write('}');
91 $this->write('if( $offset !== false ) {');
92 $this->write('$ls_filter .= "Offset: ".intval($offset)."\n";');
93 $this->write('}');
95 $this->write('if( $limit !== false ) {');
96 $this->write('$ls_filter .= "Limit: ".intval($limit)."\n";');
97 $this->write('}');
99 $this->write('$valid_columns = false;');
100 $this->write('if( $columns !== false ) {');
101 $this->write( '$processed_columns = array_merge($columns, $this->key_columns);');
102 $this->write( '$processed_columns = static::apply_columns_rewrite($processed_columns);');
103 $this->write( '$valid_columns = array();');
104 $this->write( 'foreach($processed_columns as $col) {');
105 $this->write( '$new_name = static::map_name_to_backend($col);');
106 $this->write( 'if($new_name !== false) {');
107 $this->write( '$valid_columns[] = $new_name;');
108 $this->write( '}');
109 $this->write( '}');
110 $this->write( '$valid_columns = array_unique($valid_columns);');
111 $this->write('}');
113 $this->write('try {');
114 $this->write('list($fetched_columns, $objects, $count) = $ls->query($this->table, $ls_filter, $valid_columns);');
115 $this->write('} catch( op5LivestatusException $e ) {');
116 $this->write('throw new ORMException( $e->getPlainMessage() );');
117 $this->write('}');
119 $this->write('if($columns === false) {');
120 $this->write( '$columns = static::get_all_columns_list();');
121 $this->write('}');
123 $this->write('return new LivestatusSetIterator($objects, $fetched_columns, $columns, $this->class);');
124 $this->finish_function();
128 * Generate the method map_name_to_backend for the object set
130 * @param $oset ORMObjectSetGenerator
132 public function generate_map_name_to_backend() {
133 $this->init_function('map_name_to_backend', array('name', 'prefix'), array('static'), array('prefix' => false));
134 $this->write('if($prefix === false) {');
135 $this->write('$prefix = "";');
136 $this->write('}');
137 foreach($this->structure['structure'] as $field => $type ) {
138 $backend_field = $field;
139 if(isset($this->structure['rename']) && isset($this->structure['rename'][$field])) {
140 $backend_field = $this->structure['rename'][$field];
142 if(is_array($type)) {
143 $subobjset_class = $type[0].'Set'.self::$model_suffix;
144 $this->write('if(substr($name,0,%s) == %s) {', strlen($field)+1, $field.'.');
145 $this->write('return '.$subobjset_class.'::map_name_to_backend(substr($name,%d),%s);', strlen($field)+1, $type[1]);
146 $this->write('}');
147 } else {
148 $this->write('if($name == %s) {', $field);
149 $this->write('return $prefix.%s;',$backend_field);
150 $this->write('}');
153 $this->write('return false;');
154 $this->write('}');