Example #1
0
 public function __construct($options)
 {
     //$this->logger = new DatabaseLogger();
     $this->options = $options;
     $this->connect($options);
     NimbleRecord::set_connection($this->connection);
 }
 public function test_load_sqlite3()
 {
     $settings = array('file' => dirname(__FILE__) . 'my.db', 'adapter' => 'sqlite3', 'database' => 'test');
     NimbleRecord::establish_connection($settings);
     $this->assertTrue(isset(NimbleRecord::$adapter));
     $this->assertTrue(is_a(NimbleRecord::$adapter, 'Sqlite3Adapter'));
     $this->assertTrue(class_exists('Sqlite3QueryResult'));
 }
Example #3
0
 public function clear()
 {
     if (isset($this->key)) {
         NimbleRecord::remove_from_cache($this->key);
     }
     unset($this->array);
     unset($this->length);
     unset($this);
 }
 public function search()
 {
     $methods = get_class_methods($this);
     $sql = array();
     foreach ($methods as $method) {
         $matches = array();
         if (preg_match('/([a-zA-z0-9_]+)_conditions$/', $method, $matches)) {
             $retrun = call_user_func(array($this, $method));
             if (!empty($return)) {
                 $sql[] = NimbleRecord::sanitize($return);
             }
         }
     }
     $conditions = implode(' ', $sql);
     return Package::find_all(array('conditions' => $conditions));
 }
Example #5
0
 public static function do_method($method, $class, $table, $options = array())
 {
     if (!array_include($method, static::$methods)) {
         throw new NimbleException("{$method} is not a math method");
     }
     $defaults = array('column' => 'id', 'cache' => true, 'conditions' => NULL);
     $options = array_merge($defaults, $options);
     static::check_args_for_math_functions($options);
     $query = new NimbleQuery();
     $query->select = $method . '(' . $table . '.' . $options['column'] . ') AS ' . $method;
     $query->from = $table;
     if (isset($options['joins']) && !empty($options['joins'])) {
         $query->join = $options['joins'];
     }
     if (isset($options['group']) && !empty($options['group'])) {
         $query->group_by = $options['group'];
     }
     if (isset($options['conditions']) && !empty($options['conditions'])) {
         $query->where = NimbleRecord::build_conditions($options['conditions']);
     }
     $sql = $query->build();
     return $class::execute_query($sql, false, $options['cache'])->{$method};
 }
Example #6
0
<?php

/**
* @package FrameworkTest
*/
require_once 'PHPUnit/Framework.php';
require_once __DIR__ . '/../../nimblize.php';
require_once __DIR__ . '/../../nimble_record/migrations/migration.php';
require_once __DIR__ . '/../../nimble_record/migrations/lib/migration_runner.php';
foreach (array('User', 'Photo', 'Comment', 'Club', 'ClubUser') as $model) {
    require_once __DIR__ . "/model/{$model}.php";
}
if (!defined('CONNECTED')) {
    if (file_exists(__DIR__ . '/database.json')) {
        /**
         * Example database.json contents
         * {"host":"localhost","database":"nimble_record_test","username":"******","password":"","adapter":"mysql"}
         */
        $json = file_get_contents(__DIR__ . '/database.json');
        $settings = json_decode($json, true);
    } else {
        $settings = array('host' => 'localhost', 'database' => 'nimble_record_test', 'username' => 'root', 'password' => '', 'adapter' => 'mysql');
    }
    NimbleRecord::establish_connection($settings);
    define('MYSQL_DATABASE', $settings['database']);
    define('CONNECTED', true);
}
NimbleRecord::$debug = true;
 public function run(PHPUnit_Framework_TestResult $result = NULL)
 {
     NimbleRecord::start_transaction();
     parent::run($result);
     NimbleRecord::rollback_transaction();
 }
Example #8
0
 /**
  * Renames a current column
  * @param string $old - old column name
  * @param string $new - new column name
  */
 public function rename($old, $new)
 {
     //CHANGE [column] old new options
     $options = array();
     $current_type = NimbleRecord::select_one("SHOW COLUMNS FROM " . $this->quoted_table_name . " LIKE '{$old}'");
     $current_type = $current_type["Type"];
     $rename_column_sql = $this->alter_table_sql() . 'CHANGE ' . Migration::quote_column_name($old) . ' ' . Migration::quote_column_name($new) . ' ' . $current_type;
     array_push($this->columns, $rename_column_sql);
 }
Example #9
0
#!/usr/bin/env php
<?php 
require_once dirname(__FILE__) . '/../../test/nimble_record/config.php';
NimbleRecord::$debug = false;
$start = time();
$start_memory = memory_get_usage();
$number_of_iterations = 10000;
for ($i = 0; $i < $number_of_iterations; ++$i) {
    User::find(1);
}
$t1 = time();
$m1 = memory_get_usage();
for ($i = 0; $i < $number_of_iterations; ++$i) {
    User::_find(1);
}
$t2 = time();
$m2 = memory_get_usage();
printf("Finder profile test, {$number_of_iterations} iterations:\n\n");
printf("Caching Memusage: %d\n", $m1 - $start_memory);
printf("Caching: %d sec.\n", $t1 - $start);
printf("No Caching: %d sec.\n", $t2 - $t1);
printf("No Caching Memusage: %d\n", $m2 - $m1);
printf('Peak Memusage %d', memory_get_peak_usage());
echo "\n";
Example #10
0
 public function testSanitizeSingleValueManyQ()
 {
     $sql = NimbleRecord::sanitize(array('name LIKE ? OR summary LIKE ? OR description LIKE ?', 'foo'));
     $this->assertEquals("name LIKE 'foo' OR summary LIKE 'foo' OR description LIKE 'foo'", $sql);
 }
Example #11
0
 public function tearDown()
 {
     MigrationRunner::drop_migration_table();
     NimbleRecord::end_transaction();
     ob_clean();
 }
Example #12
0
 public function reset()
 {
     $this->close();
     $conn = $this->connect();
     NimbleRecord::set_connection($conn);
 }
Example #13
0
 /**
  * @see NimbleRecord::columns
  */
 public static function columns($table)
 {
     return NimbleRecord::load_columns($table);
 }
Example #14
0
 public function testBuildDeleteConditions()
 {
     $query = new NimbleQuery(NimbleQuery::DELETE);
     $query->from = NimbleRecord::table_name('User');
     $query->where = "`id` = 5";
     $match = "DELETE FROM `users` WHERE `id` = 5";
     $this->assertEquals($match, $query->build());
 }
Example #15
0
 */
function echo_memory_usage()
{
    $mem_usage = memory_get_usage(true);
    if ($mem_usage < 1024) {
        var_dump($mem_usage . " bytes");
    } elseif ($mem_usage < 1048576) {
        var_dump(round($mem_usage / 1024, 2) . " kilobytes");
    } else {
        var_dump(round($mem_usage / 1048576, 2) . " megabytes");
    }
}
echo_memory_usage();
define('MYSQL_DATABASE', 'nimble_record_test');
require_once dirname(__FILE__) . '/../../../nimble_support/base.php';
require_once dirname(__FILE__) . '/../../../nimble_record/base.php';
require_once dirname(__FILE__) . '/../../../nimble_record/migrations/migration.php';
require_once dirname(__FILE__) . '/../../../nimble_record/migrations/lib/migration_runner.php';
require_once dirname(__FILE__) . '/../model/user.php';
require_once dirname(__FILE__) . '/../model/photo.php';
$settings = array('host' => 'localhost', 'database' => MYSQL_DATABASE, 'username' => 'root', 'password' => '', 'adapter' => 'mysql');
NimbleRecord::establish_connection($settings);
echo_memory_usage();
$u = User::find_all(array('limit' => '0,500'));
echo_memory_usage();
$u->clear();
echo_memory_usage();
$u2 = User::find_all();
echo_memory_usage();
$u2->clear();
echo_memory_usage();
Nimble::set_config('stylesheet_folder', FileUtils::join(dirname(__FILE__), '..', 'public', 'style'));
Nimble::set_config('stylesheet_folder_url', '/public/style');
Nimble::set_config('javascript_folder', FileUtils::join(dirname(__FILE__), '..', 'public', 'javascript'));
Nimble::set_config('javascript_folder_url', '/public/javascript');
Nimble::set_config('image_url', 'public/image');
Nimble::set_config('image_path', FileUtils::join(dirname(__FILE__), '..', 'public', 'image'));
Nimble::set_config('uri', '/');
//define the root
define('NIMBLE_ROOT', FileUtils::join(dirname(__FILE__), '..'));
// load any custom global config options
require_once FileUtils::join(dirname(__FILE__), 'config.php');
require_once FileUtils::join(dirname(__FILE__), 'routes.php');
require_once FileUtils::join(dirname(__FILE__), 'r404.php');
// load any custom enviroment config options
// Nimble::Log('loading ' . NIMBLE_ENV . ' enviroment');
require_once FileUtils::join(dirname(__FILE__), NIMBLE_ENV, 'config.php');
require_once FileUtils::join(NIMBLE_ROOT, 'app', 'controller', 'application_controller.php');
/** load controlers and models */
foreach (array('model', 'controller') as $dir) {
    __load_files(FileUtils::join(dirname(__FILE__), '..', 'app', $dir));
}
session_set_cookie_params(time() + 1000, '/', '.' . DOMAIN);
session_start();
//load database connection
$database_info = json_decode(file_get_contents(FileUtils::join(NIMBLE_ROOT, 'config', NIMBLE_ENV, 'database.json')), true);
$database_info = $database_info[NIMBLE_ENV];
NimbleRecord::establish_connection($database_info);
/** boot the framework */
if (!defined('CLI_RUNNER')) {
    Run();
}
Example #17
0
 public function tareDown()
 {
     NimbleRecord::rollback_transaction();
 }
 /**
  * Returns the table name for a model
  * @param string $name - Model name
  * @return string
  */
 public static function table_name($name)
 {
     return NimbleRecord::table_name(static::model($name));
 }