Exemplo n.º 1
1
<?php

require 'concatToOneFile.php';
require 'FluentPDO.php';
$fpdo = new FluentPDO('mysql:host=localhost;dbname=test;charset=utf8', 'root', '');
$q = $fpdo->from('sometable')->where('1=1');
echo $q->getQuery(), "\n";
var_dump($q->fetchAll());
echo "\n\n";
$q = $fpdo->from('sometable')->select('count(1)')->where('1=1');
echo $q->getQuery(), "\n";
var_dump($q->fetchAll());
echo "\n\n";
$q = $fpdo->query('SELECT * FROM sometable WHERE x=?', array('1'));
echo $q->getQuery(), "\n";
var_dump($q->fetchAll());
echo "\n\n";
Exemplo n.º 2
0
 /**
  * Performs a delete query using FluentPDO
  */
 public function delete($table, $key, $keyValue = null)
 {
     if (!$this->isConnected()) {
         static::connect();
     }
     $this->num_queries++;
     return $this->fpdo->deleteFrom($table)->where($key, $keyValue)->execute();
 }
Exemplo n.º 3
0
 public function delete(Entity $entity)
 {
     $query = $this->queryBuilder->delete($this->getTableName(), $entity->getId());
     try {
         $result = $query->execute();
     } catch (\PDOException $ex) {
         throw new QueryException($ex, $query);
     }
     return $result;
 }
Exemplo n.º 4
0
 /**
  * @param $value
  *
  * @return string
  */
 protected function quote($value)
 {
     if (!isset($value)) {
         return "NULL";
     }
     if (is_array($value)) {
         // (a, b) IN ((1, 2), (3, 4))
         return "(" . implode(", ", array_map(array($this, 'quote'), $value)) . ")";
     }
     $value = $this->formatValue($value);
     if (is_float($value)) {
         return sprintf("%F", $value);
         // otherwise depends on setlocale()
     }
     if ($value === false) {
         return "0";
     }
     if (is_int($value) || $value instanceof FluentLiteral) {
         // number or SQL code - for example "NOW()"
         return (string) $value;
     }
     return $this->fpdo->getPdo()->quote($value);
 }
Exemplo n.º 5
0
 public function from($table, $primaryKey = null)
 {
     return $this->db->from($table, $primaryKey);
 }
Exemplo n.º 6
0
<?php

require 'vendor/autoload.php';
// Koristi :
// - SlimPHP - http://www.slimframework.com (kao minimalni frejmork)
// - FluentPDO - http://lichtner.github.io/fluentpdo (za rad sa bazom podataka)
// Ukljuci debug
// ini_set('display_errors',1);
// ini_set('display_startup_errors',1);
// error_reporting(-1);
$app = new \Slim\Slim();
// Dodaj JSON midleware globalno
$app->add(new \SlimJson\Middleware(array('json.status' => true, 'json.override_error' => true, 'json.override_notfound' => true)));
// Povezi se sa bazom
$pdo = new PDO("sqlite:db.sqlite");
$fpdo = new FluentPDO($pdo);
// API za pretragu umetnika
$app->post('/artists/search', function () use($app, $fpdo) {
    $count = $fpdo->from('artist')->where("Name LIKE '%" . $_POST['keyword'] . "%'")->count();
    if (isset($_POST['perPage']) && intval($_POST['perPage']) > 0) {
        $perPage = intval($_POST['perPage']);
    } else {
        $perPage = 5;
    }
    $pageCount = ceil($count / $perPage);
    if (isset($_POST['page']) && intval($_POST['page']) > 0) {
        $page = intval($_POST['page']);
        if ($page > $pageCount) {
            throw new \Exception('Page ' . $page . ' does not exist.');
        }
        $artists = $fpdo->from('artist')->where("Name LIKE '%" . $_POST['keyword'] . "%'")->limit($perPage)->offset(($page - 1) * $perPage)->fetchAll();
Exemplo n.º 7
0
<?php

require 'vendor/autoload.php';
// ini_set('display_errors',1);
// ini_set('display_startup_errors',1);
// error_reporting(-1);
$app = new \Slim\Slim();
// Dodaj JSON midleware globalno
$app->add(new \SlimJson\Middleware(array('json.status' => true, 'json.override_error' => true, 'json.override_notfound' => true, 'json.status' => false)));
// DB CONNECT
$pdo = new PDO("sqlite:db.sqlite");
$fpdo = new FluentPDO($pdo);
// GET /persons
$app->get('/persons', function () use($app, $fpdo) {
    $count = $fpdo->from('persons')->count();
    if (isset($_GET['perPage']) && intval($_GET['perPage']) > 0) {
        $perPage = intval($_GET['perPage']);
    } else {
        $perPage = 5;
    }
    $pageCount = ceil($count / $perPage);
    if (isset($_GET['page']) && intval($_GET['page']) > 0) {
        $page = intval($_GET['page']);
        if ($page > $pageCount) {
            throw new \Exception('Page ' . $page . ' does not exist.');
        }
        $persons = $fpdo->from('persons')->limit($perPage)->offset(($page - 1) * $perPage)->fetchAll();
    } else {
        $page = 1;
        $persons = $fpdo->from('persons')->limit($perPage)->fetchAll();
    }
Exemplo n.º 8
0
<?php

require 'vendor/autoload.php';
require 'Random.php';
// Ukljuci debug
// ini_set('display_errors',1);
// ini_set('display_startup_errors',1);
// error_reporting(-1);
use sweelix\guid\Guid;
use Slim\Slim;
use SlimJson\Middleware;
$app = new Slim();
// Dodaj JSON midleware globalno
$app->add(new Middleware(array('json.status' => true, 'json.override_error' => true, 'json.override_notfound' => true)));
// Povezi se sa bazom
$pdo = new PDO("sqlite:db.sqlite");
$fpdo = new FluentPDO($pdo);
// API za generisanje slucajnog broja
$app->get('/match(/:id)', function ($id = '%NO-DATA%') use($app, $fpdo) {
    if ($id == '%NO-DATA%') {
        $id = Guid::v4();
    }
    $match = $fpdo->from('matches')->where('id', $id)->fetch();
    if (!$match) {
        $data = ['id' => $id, 'player1_id' => NULL, 'player2_id' => NULL, 'data' => implode(';', Random::generate($id, 500, 0, 100))];
        $query = $fpdo->insertInto('matches', $data)->execute();
        $match = $data;
    }
    $app->render(200, $match);
});
$app->run();