1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | <?php /** * CouchDB Server Manager */ class SetteeServer { /** * Base URL of the CouchDB REST API */ private $conn_url; /** * HTTP REST Client instance */ protected $rest_client; /** * Class constructor * * @param $conn_url * (optional) URL of the CouchDB server to connect to. Default value: http://127.0.0.1:5984 */ function __construct($conn_url = "http://127.0.0.1:5984") { $this->conn_url = rtrim($conn_url, ' /'); $this->rest_client = SetteeRestClient::get_instance($this->conn_url); } /** * Create database * * @param $db * Either a database object or a String name of the database. * * @return * json string from the server. * * @throws SetteeCreateDatabaseException */ function create_db($db) { if ($db instanceof SetteeDatabase) { $db = $db->get_name(); } $ret = $this->rest_client->http_put($db); if (!empty($ret['decoded']->error)) { throw new SetteeDatabaseException("Could not create database: " . $ret["json"]); } return $ret['decoded']; } /** * Drop database * * @param $db * Either a database object or a String name of the database. * * @return * json string from the server. * * @throws SetteeDropDatabaseException */ function drop_db($db) { if ($db instanceof SetteeDatabase) { $db = $db->get_name(); } $ret = $this->rest_client->http_delete($db); if (!empty($ret['decoded']->error)) { throw new SetteeDatabaseException("Could not create database: " . $ret["json"]); } return $ret['decoded']; } /** * Instantiate a database object * * @param $dbname * name of the newly created database * * @return SetteeDatabase * new SetteeDatabase instance. */ function get_db($dbname) { return new SetteeDatabase($this->conn_url, $dbname); } /** * Return an array containing all databases * * @return Array * an array of database names in the CouchDB instance */ function list_dbs() { $ret = $this->rest_client->http_get('_all_dbs'); if (!empty($ret['decoded']["error"])) { throw new SetteeDatabaseException("Could not get list of databases: " . $ret["json"]); } return $ret['decoded']; } } class SetteeServerErrorException extends Exception {} class SetteeDatabaseException extends Exception {} class SetteeWrongInputException extends Exception {} |