/** create row **/
 public function doCreateRow()
 {
     $this->db = x("db");
     $this->collection = xn("collection");
     $id = rock_real_id(x("id"));
     import("lib.mongo.RQuery");
     //selected format last time
     $this->last_format = rock_cookie("rock_format", "json");
     //if is duplicating ...
     if (!$this->isPost() && $id) {
         $query = new RQuery($this->_mongo, $this->db, $this->collection);
         $row = $query->id($id)->findOne();
         if (!empty($row)) {
             unset($row["_id"]);
             import("classes.VarExportor");
             $export = new VarExportor($query->db(), $row);
             x("data", $export->export($this->last_format));
         }
     }
     //initialize
     if (!$this->isPost() && !x("data")) {
         x("data", $this->last_format == "json" ? "{\n\t\n}" : "array(\n\n)");
     }
     //try to deal with data
     if ($this->isPost()) {
         $format = x("format");
         $count = xi("count");
         $this->last_format = $format;
         $data = xn("data");
         $data = str_replace(array("%{created_at}"), time(), $data);
         $row = null;
         $eval = new VarEval($data, $format, $this->_mongo->selectDb($this->db));
         $row = $eval->execute();
         if ($row === false || !is_array($row)) {
             $this->error = "Data must be a valid {$format}.";
             $this->display();
             return;
         }
         $query = new RQuery($this->_mongo, $this->db, $this->collection);
         try {
             for ($i = 1; $i <= $count; $i++) {
                 $query->insert($row);
                 unset($row['_id']);
             }
         } catch (Exception $e) {
             $this->error = $e->getMessage();
             $this->display();
             return;
         }
         //remember format choice
         $this->_rememberFormat($format);
         $this->redirect("collection.index", array("db" => $this->db, "collection" => $this->collection), true);
     }
     $this->display();
 }
示例#2
0
<?php

require '../src/autoload.php';
$rQueryConf = new RQueryConfiguratorDatabase('mysql', 'localhost', 'root', '', 'rquery');
$rQuery = new RQuery($rQueryConf, new RQueryDriverPdo());
$rQuery->dropTable("test");
$rQuery->dropTable(["test", "toto"]);
$rQuery->useSqlFile('test_file.sql');
$rQuery->dropTable("test");
$rQuery->dropTable(["test", "toto"]);
$rQuery->exec("CREATE TABLE `test` (\n  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,\n  `name` VARCHAR(255) NOT NULL,\n  PRIMARY KEY (`id`) );");
$rQuery->optimize("test");
$rQuery->optimize(["test", "toto"]);
$id = $rQuery->insert("INSERT INTO test (`name`) VALUES (:name)", array('name' => 'A'));
var_dump("Insert Into test last id: " . $id);
$idB = $rQuery->insert("INSERT INTO test (`name`) VALUES (:name)", array('name' => 'B'));
var_dump("Insert Into test last id: " . $idB);
$id = $rQuery->insert("INSERT INTO test (`name`) VALUES (:name)", array('name' => 'C'));
var_dump("Insert Into test last id: " . $id);
$id = $rQuery->insert("INSERT INTO test (`name`) VALUES (:name)", array('name' => 'chickenskill'));
var_dump("Insert Into test last id: " . $id);
$rowsAffected = $rQuery->update("UPDATE test SET name = :name WHERE id = :id", array('id' => $idB, 'name' => 'google'));
var_dump("Row affected: " . $rowsAffected);
$rowsAffected = $rQuery->delete("DELETE FROM test WHERE name = :name1 OR name = :name2", array('name1' => 'A', 'name2' => 'C'));
var_dump("Row affected: " . $rowsAffected);
$cursor = $rQuery->select("SELECT * FROM test");
while ($row = $rQuery->read($cursor)) {
    var_dump($row);
}
$pdo = $rQuery->getDriver();
var_dump($pdo);