Beispiel #1
0
 private function _prepare()
 {
     db::connect();
     dibi::addSubst('table', 'students');
     dibi::addSubst('courses_students', 'courses_students');
     dibi::addSubst('courses', 'courses');
 }
Beispiel #2
0
 /**
  * Creates object and (optionally) connects to a database.
  * @param  array|string|ArrayObject connection parameters
  * @param  string       connection name
  * @throws DibiException
  */
 public function __construct($config, $name = NULL)
 {
     if (class_exists('Debug', FALSE)) {
         /*Nette\*/
         Debug::addColophon(array('dibi', 'getColophon'));
     }
     // DSN string
     if (is_string($config)) {
         parse_str($config, $config);
     } elseif ($config instanceof ArrayObject) {
         $config = (array) $config;
     } elseif (!is_array($config)) {
         throw new InvalidArgumentException('Configuration must be array, string or ArrayObject.');
     }
     self::alias($config, 'username', 'user');
     self::alias($config, 'password', 'pass');
     self::alias($config, 'host', 'hostname');
     if (!isset($config['driver'])) {
         $config['driver'] = dibi::$defaultDriver;
     }
     $driver = preg_replace('#[^a-z0-9_]#', '_', $config['driver']);
     $class = "Dibi" . $driver . "Driver";
     if (!class_exists($class, FALSE)) {
         include_once dirname(__FILE__) . "/../drivers/{$driver}.php";
         if (!class_exists($class, FALSE)) {
             throw new DibiException("Unable to create instance of dibi driver '{$class}'.");
         }
     }
     $config['name'] = $name;
     $this->config = $config;
     $this->driver = new $class();
     if (!empty($config['profiler'])) {
         $class = $config['profiler'];
         if (is_numeric($class) || is_bool($class)) {
             $class = 'DibiProfiler';
         }
         if (!class_exists($class)) {
             throw new DibiException("Unable to create instance of dibi profiler '{$class}'.");
         }
         $this->setProfiler(new $class());
     }
     if (!empty($config['substitutes'])) {
         foreach ($config['substitutes'] as $key => $value) {
             dibi::addSubst($key, $value);
         }
     }
     if (empty($config['lazy'])) {
         $this->connect();
     }
 }
 public static function initialize()
 {
     $conf = Environment::getConfig('database');
     $connection = dibi::connect($conf[$conf->engine]);
     if ($conf->engine == 'sqlite') {
         $connection->getDriver()->registerFunction('regexp', 'Sqlite::regexp', 2);
     } elseif ($conf->engine == 'postgre') {
         dibi::addSubst('', '::');
     }
     if ($conf->profiler) {
         $profiler = is_numeric($conf->profiler) || is_bool($conf->profiler) ? new DibiProfiler(array()) : new $conf->profiler();
         $profiler->setFile(Environment::expand('%logDir%') . '/sql.log');
         $connection->setProfiler($profiler);
     }
 }
Beispiel #4
0
 private function _prepareLearningTimes()
 {
     db::connect();
     dibi::addSubst('table', 'learning_times');
 }
 /**
  * Connection options: (see driver-specific options too)
  *   - lazy (bool) => if TRUE, connection will be established only when required
  *   - result (array) => result set options
  *       - detectTypes (bool) => detect the types of result set fields?
  *       - formatDateTime => date-time format (if empty, DateTime objects will be returned)
  *   - profiler (array or bool)
  *       - run (bool) => enable profiler?
  *       - class => profiler class name (default is DibiProfiler)
  *   - substitutes (array) => map of driver specific substitutes (under development)
  * @param  mixed   connection parameters
  * @param  string  connection name
  * @throws DibiException
  */
 public function __construct($config, $name = NULL)
 {
     // DSN string
     if (is_string($config)) {
         parse_str($config, $config);
     } elseif ($config instanceof Traversable) {
         $tmp = array();
         foreach ($config as $key => $val) {
             $tmp[$key] = $val instanceof Traversable ? iterator_to_array($val) : $val;
         }
         $config = $tmp;
     } elseif (!is_array($config)) {
         throw new InvalidArgumentException('Configuration must be array, string or object.');
     }
     self::alias($config, 'username', 'user');
     self::alias($config, 'password', 'pass');
     self::alias($config, 'host', 'hostname');
     self::alias($config, 'result|detectTypes', 'resultDetectTypes');
     // back compatibility
     self::alias($config, 'result|formatDateTime', 'resultDateTime');
     if (!isset($config['driver'])) {
         $config['driver'] = dibi::$defaultDriver;
     }
     $driver = preg_replace('#[^a-z0-9_]#', '_', strtolower($config['driver']));
     $class = "Dibi" . $driver . "Driver";
     if (!class_exists($class, FALSE)) {
         include_once dirname(__FILE__) . "/../drivers/{$driver}.php";
         if (!class_exists($class, FALSE)) {
             throw new DibiException("Unable to create instance of dibi driver '{$class}'.");
         }
     }
     $config['name'] = $name;
     $this->config = $config;
     $this->driver = new $class();
     $this->translator = new DibiTranslator($this->driver);
     // profiler
     $profilerCfg =& $config['profiler'];
     if (is_scalar($profilerCfg)) {
         // back compatibility
         $profilerCfg = array('run' => (bool) $profilerCfg, 'class' => strlen($profilerCfg) > 1 ? $profilerCfg : NULL);
     }
     if (!empty($profilerCfg['run'])) {
         $class = isset($profilerCfg['class']) ? $profilerCfg['class'] : 'DibiProfiler';
         if (!class_exists($class)) {
             throw new DibiException("Unable to create instance of dibi profiler '{$class}'.");
         }
         $this->setProfiler(new $class($profilerCfg));
     }
     if (!empty($config['substitutes'])) {
         foreach ($config['substitutes'] as $key => $value) {
             dibi::addSubst($key, $value);
         }
     }
     if (empty($config['lazy'])) {
         $this->connect();
     }
 }
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">

<h1>Using Substitutions | dibi</h1>

<?php 
require_once 'Nette/Debug.php';
require_once '../dibi/dibi.php';
dibi::connect(array('driver' => 'sqlite', 'database' => 'data/sample.sdb'));
// create new substitution :blog:  ==>  wp_
dibi::addSubst('blog', 'wp_');
dibi::test("SELECT * FROM [:blog:items]");
// -> SELECT * FROM [wp_items]
// create new substitution :: (empty)  ==>  my_
dibi::addSubst('', 'my_');
dibi::test("UPDATE ::table SET [text]='Hello World'");
// -> UPDATE my_table SET [text]='Hello World'
// create substitutions using fallback callback
function substFallBack($expr)
{
    $const = 'SUBST_' . strtoupper($expr);
    if (defined($const)) {
        return constant($const);
    } else {
        throw new Exception("Undefined substitution :{$expr}:");
    }
}
// define callback
dibi::setSubstFallBack('substFallBack');
// define substitutes as constants
define('SUBST_ACCOUNT', 'eshop_');
define('SUBST_ACTIVE', 7);
Beispiel #7
0
 private function _prepare()
 {
     dibi::addSubst('table', 'teachers');
     dibi::addSubst('assigned_courses', 'courses_teachers');
     dibi::addSubst('courses', 'courses');
 }
Beispiel #8
0
 * My NApplication bootstrap file.
 *
 * @copyright  Copyright (c) 2010 John Doe
 * @package    MyApplication
 */
// Step 1: Load Nette Framework
// this allows load Nette Framework classes automatically so that
// you don't have to litter your code with 'require' statements
require LIBS_DIR . '/Nette/loader.php';
// Step 2: Configure environment
// 2a) enable NDebug for better exception and error visualisation
NDebug::enable(NDebug::DETECT, APP_DIR . '/log/php_error.log', '*****@*****.**');
NEnvironment::getHttpResponse()->enableCompression();
// Load dibi
dibi::connect(NEnvironment::getConfig('database'));
dibi::addSubst('graweb', 'gw2010__');
// Step 3: Configure application
NEnvironment::setVariable("sizes", array(0 => array(880, 330), 1 => array(263, 174), 2 => array(600, 510)));
// 3a) get and setup a front controller
$application = NEnvironment::getApplication();
$application->errorPresenter = 'Front:Error';
$application->catchExceptions = TRUE;
// Step 4: Setup application router
$router = $application->getRouter();
$router[] = new NRoute('index.php', array('module' => 'Front', 'presenter' => 'Default'), NRoute::ONE_WAY);
$router[] = new NRoute('', array('module' => 'Front', 'presenter' => 'Default', 'action' => 'default'));
// Presmerovani starych URL
$router[] = new NRoute('internet/<? ceny|postup|sluzby|vyroba>', array('module' => 'Front', 'presenter' => 'Page', 'action' => 'webdesign'), NRoute::ONE_WAY);
$router[] = new NRoute('graficke-navrhy', array('module' => 'Front', 'presenter' => 'Reklama', 'action' => 'grafikaDesign'), NRoute::ONE_WAY);
$router[] = new NRoute('<? venkovni-reklama|reklama/reference/>', array('module' => 'Front', 'presenter' => 'Reklama', 'action' => 'reklama'), NRoute::ONE_WAY);
$router[] = new NRoute('internet/reference/', array('module' => 'Front', 'presenter' => 'Reference', 'action' => 'default'), NRoute::ONE_WAY);