4 * Copyright (c) 2008 Jerry Jalava <jerry.jalava@gmail.com>
5 * Licensed under the GPLv3 license
6 * http://www.gnu.org/licenses/gpl.html
12 private $configuration;
15 public function __construct(&$configuration)
17 $this->configuration
=& $configuration;
19 $this->documents
= new couchdb_documents($this->configuration
);
22 public function __get($name)
24 return $this->execute($name);
27 public function __set($name, $value)
31 return $this->remove($name);
34 $results = $this->update($name, $value);
36 if ( !isset($results->ok
)
40 if ( isset($results->error
)
41 && isset($results->reason
))
43 $reason = "CouchDB said error: '{$results->error}', reason: '{$results->reason}'";
46 throw new couchdb_view_exception("Couldn't save view {$name}. {$reason}");
50 public function __isset($name)
52 return $this->exists($name);
55 public function temp($function, $args=null)
57 $view = new couchdb_views_view('_temp_view', $this->configuration
);
58 $view->set_args($function, true);
62 $view->set_args($args);
65 $results = $view->run();
66 if (isset($results->rows
))
68 $results->rows
= $this->documents
->convert($results->rows
);
74 public function create($name, $value)
76 return $this->update($name, $value);
79 public function delete($name)
84 public function execute($name, $args=null)
88 $view = new couchdb_views_view($name, $this->configuration
);
89 $view->set_args($args);
91 $results = $view->run();
92 if (isset($results->rows
))
94 $results->rows
= $this->documents
->convert($results->rows
);
100 public function update($name, $value)
102 $update_only_view = false;
104 if (strpos($name, '/') !== false)
106 $name_parts = explode('/', $name);
107 $name = $name_parts[0];
108 $update_only_view = $name_parts[1];
112 $view = new couchdb_views_view($name, $this->configuration
);
114 $existing_view = null;
115 if ($this->exists($name, true))
117 $existing_view = $view->get();
120 if ($update_only_view)
122 if (is_null($existing_view))
124 throw new couchdb_view_exception("View {$name} doesn't exist! Cannot update.");
129 '_rev' => $existing_view->_rev
,
130 'language' => $existing_view->language
,
131 'views' => get_object_vars($existing_view->views
)
133 $value['views'][$update_only_view] = $new_view;
135 $view->set_args($value);
137 return $view->run(true);
140 if ( !is_null($existing_view)
141 && isset($existing_view->_rev
))
143 $value['_rev'] = $existing_view->_rev
;
146 if (! isset($value['language']))
148 $value['language'] = 'text/javascript';
150 if ( !is_null($existing_view)
151 && isset($existing_view->language
))
153 $value['language'] = $existing_view->language
;
157 $view->set_args($value);
159 return $view->run(true);
162 private function exists($name, $graceful=false)
166 $view = new couchdb_views_view($name, $this->configuration
);
171 catch(couchdb_view_exception
$e)
178 if ($e->getCode() == 404)
180 throw new couchdb_view_exception("View '{$name}' doesn't exist!");
189 public function clear_caches()