Beispiel #1
0
require_once 'Duckk/CouchDB.php';
require_once 'Duckk/CouchDB/Util.php';
$couchdb = new Duckk_CouchDB();
$randomDoc = "testing_" . substr(md5(microtime(true)), 0, 8);
print_r($couchdb->getAllDocuments('arin'));
print_r($couchdb->getAllDocumentsBySequence('arin'));
echo "------------ example doc put (id = {$randomDoc}) -------------------\n";
$doc = new Duckk_CouchDB_Document();
$doc->_id = $randomDoc;
print_r($couchdb->putDocument('arin', $doc));
echo "------------ test copying  -------------------\n";
print_r($couchdb->copyDocument('arin', "testing_16bd3b77", "testing_b3307897copy", "12-2566465478", "1-1053805331"));
echo "------------ delete the example doc ({$randomDoc}) -------------------\n";
print_r($couchdb->deleteDocument('arin', $doc->_id));
echo "------------ get my test document --------------\n";
print_r($couchdb->getDocument('arin', 'booya3'));
echo "------------LIST OF DATABASES---------------\n";
print_r($couchdb->getDatabases());
echo "------------ get rev info for my test document --------------\n";
print_r($couchdb->getDocumentRevisionList('arin', 'booya'));
echo "------------ get rev info for my test document --------------\n";
print_r($couchdb->getDocumentRevisionInfo('arin', 'booya'));
$randomDBName = 'testing' . md5(microtime(true));
echo "------------CREATE A DB NAMED {$randomDBName} --------------\n";
var_dump($couchdb->createDatabase("{$randomDBName}"));
echo "------------TRY TO CREATE {$randomDBName} again --------------\n";
$couchdb->createDatabase($randomDBName);
echo "------------Compact {$randomDBName} --------------\n";
var_dump($couchdb->compactDatabase("{$randomDBName}"));
echo "------------ Get info for {$randomDBName} --------------\n";
print_r($couchdb->getDatabaseInfo($randomDBName));
$arin->phone = "(900) 976-1212";
$putResult = $couchdb->putDocument($database, $arin);
p("Put employee Arin Sarkissian", $putResult);
// create another employee: John Doe
$john = new Duckk_CouchDB_Document();
$john->_id = "employee2";
// you MUST specify an id
$john->firstName = "John";
$john->lastName = "Doe";
$john->phone = "(900) 555-1212";
$john->title = "CTO";
$john->salary = 850000;
$putResult = $couchdb->putDocument($database, $john);
p("Put employee John Doe", $putResult);
// GET the document for Arin Sarkissian
p("Get Arin's Data", $couchdb->getDocument($database, 'employee1'));
// change his job title and save the document again
$arin->title = 'Sr Software Engineer';
$putResult = $couchdb->putDocument($database, $arin);
p("Get Arin's Data after title change", $couchdb->getDocument($database, 'employee1'));
// now create a view
$designDoc = new Duckk_CouchDB_DesignDocument();
$designDoc->setId('empData');
$designDoc->addView('all', 'function(doc) { emit(null, doc.salary); }');
$designDoc->addView('totalPayroll', 'function(doc) { emit("salary", doc.salary); }', 'function(name, salary) { return sum(salary) }');
// PUT the view
$resp = $couchdb->putDocument($database, $designDoc);
p("PUT the design document", $resp);
// run the "all" view we just put
$viewResult = $couchdb->getDocument($database, $designDoc->_id . '/_view/all');
p("Result of the 'ALL' view", $viewResult);