function testDefaultSchemaNameForUser()
 {
     $user = '******';
     $password = '******';
     $default_schema = $user;
     $another_schema = 'bar';
     $this->_dropSchema($default_schema);
     $this->_dropSchema($another_schema);
     $this->_dropUser($user);
     $this->_createSchema($default_schema);
     $this->_createSchema($another_schema);
     $this->_createUser($user, $password);
     $sql = 'CREATE TABLE %schema_name%.test_default_schema_name (
   "id" SERIAL,
   "value" integer,
   PRIMARY KEY (id)
 )';
     $this->_executeForSchema($sql, $default_schema);
     $this->_executeForSchema($sql, $another_schema);
     $this->_changeTableOwner($default_schema . '.test_default_schema_name', $user);
     $this->_changeTableOwner($another_schema . '.test_default_schema_name', $user);
     $sql = "INSERT INTO %schema_name%.test_default_schema_name(value) VALUES(1)";
     $this->_executeForSchema($sql, $default_schema);
     $dsn = $this->_getDsnForUser($user, $password);
     $new_connection = $this->toolkit->createDbConnection($dsn);
     lmbToolkit::instance()->setDefaultDbConnection($new_connection);
     $sql = "SELECT COUNT(*) as count from test_default_schema_name";
     $this->assertEqual(lmbDBAL::fetchOneValue($sql), 1);
     $dbinfo = $new_connection->getDatabaseInfo();
     $this->assertTrue($dbinfo->hasTable('test_default_schema_name'));
 }
示例#2
0
 protected function _setPriority()
 {
     if (!($parent_id = $this->getParentId())) {
         $parent_id = lmbCmsDocument::findRoot()->getId();
     }
     $sql = "SELECT MAX(priority) FROM " . $this->_db_table_name . " WHERE parent_id = " . $parent_id;
     $max_priority = lmbDBAL::fetchOneValue($sql);
     $this->setPriority($max_priority + 10);
 }
示例#3
0
 protected static function _getMetadataForUrl($uri = null)
 {
     if (!$uri) {
         $uri = lmbToolkit::instance()->getRequest()->getUri();
     }
     $count_path = $uri->countPath();
     $meta = null;
     $sql = 'SELECT keywords, description, title FROM lmb_cms_seo WHERE url = \'/\' OR ';
     for ($i = 1; $i < $count_path; $i++) {
         $sql .= ' url = \'' . self::getDefaultConnection()->escape($uri->getPathToLevel($i)) . '\'' . ($i < $count_path - 1 ? ' OR ' : '');
     }
     $sql .= ' ORDER BY url DESC LIMIT 1';
     $meta = lmbDBAL::fetchOneRow($sql);
     if (!empty($meta)) {
         self::$_meta = $meta;
     } else {
         self::$_meta = new lmbObject(array('title' => '', 'description' => '', 'keywords' => ''));
     }
 }
 function testProcessPrefixedFieldsAsRelatedActiveRecords()
 {
     $course = $this->_createCourseWithTwoLectures();
     $lecture = new LectureForTest();
     $course_info = $lecture->getRelationInfo('course');
     $conn = lmbToolkit::instance()->getDefaultDbConnection();
     $db = new lmbSimpleDb($conn);
     $sql = 'SELECT ' . $conn->quoteIdentifier("lecture_for_test") . '.*, ' . $conn->quoteIdentifier("course_for_test.id") . ' as ' . $conn->quoteIdentifier("course__id") . ', ' . $conn->quoteIdentifier("course_for_test.title") . ' as ' . $conn->quoteIdentifier("course__title") . ' 
         FROM ' . $conn->quoteIdentifier("lecture_for_test") . ' LEFT JOIN ' . $conn->quoteIdentifier("course_for_test") . ' ON ' . $conn->quoteIdentifier("course_for_test.id") . ' = ' . $conn->quoteIdentifier("lecture_for_test.course_id");
     $decorated = lmbDBAL::fetch($sql);
     $iterator = new lmbARRecordSetJoinDecorator($decorated, new LectureForTest(), null, array('course' => $course_info));
     // let's fetch all data in order to actually call rewind() and current();
     $arr = $iterator->getArray();
     // now let's remove everything from db tables so we can be sure that processing is correct
     $db->delete('lecture_for_test');
     $db->delete('course_for_test');
     $this->assertEqual($arr[0]->get('course')->getTitle(), $course->getTitle());
     $this->assertEqual($arr[1]->get('course')->getTitle(), $course->getTitle());
 }
示例#5
0
    }
}
echo "native pgsql fetching: " . (microtime(true) - $mark) . "\n";
$conn = lmbDBAL::newConnection('pgsql://*****:*****@localhost/medkrug');
$mark = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    $rs = lmbDBAL::fetch('SELECT bar FROM foo', $conn);
    foreach ($rs as $record) {
        $bar = $record['bar'];
    }
    $rs->freeQuery();
}
echo "lmbDBAL :: fetch(), array access: " . (microtime(true) - $mark) . "\n";
$mark = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    $rs = lmbDBAL::fetch('SELECT bar FROM foo', $conn);
    foreach ($rs as $record) {
        $bar = $record->get('bar');
    }
    $rs->freeQuery();
}
echo "lmbDBAL :: fetch(), getter: " . (microtime(true) - $mark) . "\n";
$mark = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    $stmt = $conn->newStatement('SELECT bar FROM foo');
    $rs = $stmt->getRecordSet();
    foreach ($rs as $record) {
        $bar = $record->get('bar');
    }
}
echo "lmbPgsqlConnection :: newStatement(), getter: " . (microtime(true) - $mark) . "\n";
 function testSetDontReInsertSameRecordsIfTheyExists()
 {
     $group1 = $this->_initGroup();
     $group2 = $this->_initGroup();
     $group3 = $this->_initGroup();
     $group4 = $this->_initGroup();
     $user = $this->_createUserAndSave(array($group1, $group2, $group3));
     $table = lmbDBAL::table('user_for_test2group_for_test', $this->conn);
     $records = $table->select()->getArray();
     $this->assertEqual(count($records), 3);
     $collection = new lmbARManyToManyCollection('groups', $user);
     $collection->set(array($group1, $group2, $group3, $group4));
     $new_records = $table->select()->getArray();
     $this->assertEqual(count($new_records), 4);
     $this->assertEqual($records[0]['id'], $new_records[0]['id']);
     $this->assertEqual($records[1]['id'], $new_records[1]['id']);
     $this->assertEqual($records[2]['id'], $new_records[2]['id']);
     $this->assertEqual($new_records[3]['user_id'], $user->getId());
 }
示例#7
0
 function testDeleteQueryUsingDefaultConnection()
 {
     $query = lmbDBAL::deleteQuery('test_db_table');
     $this->assertIsA($query, 'lmbDeleteQuery');
     $this->assertEqual($query->getTable(), 'test_db_table');
 }
示例#8
0
    }
}
echo "native linter fetching: " . (microtime(true) - $mark) . "\n";
$conn = lmbDBAL::newConnection('linter://*****:*****@localhost/Demo');
$mark = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    $rs = lmbDBAL::fetch('SELECT "bar" FROM "foo";', $conn);
    foreach ($rs as $record) {
        $bar = $record['bar'];
    }
    $rs->freeQuery();
}
echo "lmbDBAL :: fetch(), array access: " . (microtime(true) - $mark) . "\n";
$mark = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    $rs = lmbDBAL::fetch('SELECT "bar" FROM "foo";', $conn);
    foreach ($rs as $record) {
        $bar = $record->get('bar');
    }
    $rs->freeQuery();
}
echo "lmbDBAL :: fetch(), getter: " . (microtime(true) - $mark) . "\n";
$mark = microtime(true);
for ($i = 0; $i < 1000; $i++) {
    $stmt = $conn->newStatement('SELECT "bar" FROM "foo";');
    $rs = $stmt->getRecordSet();
    foreach ($rs as $record) {
        $bar = $record->get('bar');
    }
}
echo "lmbLinterConnection :: newStatement(), getter: " . (microtime(true) - $mark) . "\n";
示例#9
0
/**
 * @desc create db by specified DSN
 * @deps project_db_init_config
 * @example project.php create_db -D DSN=mysqli://root:test@localhost/limb_app?charset=utf8
 */
function task_project_db_create($args)
{
    lmb_package_require('dbal');
    lmb_require('limb/dbal/src/lmbDbDSN.class.php');
    $code = <<<EOD
    \$toolkit = lmbToolkit :: instance();
    echo \$toolkit->getDefaultDbDSN()->toString();
EOD;
    $output = lmb_cli_run_code_in_project_env($code);
    $dsn = new lmbDbDSN($output);
    $db_name = $dsn->database;
    $dsn->database = false;
    $conn = lmbToolkit::instance()->createDbConnection($dsn);
    lmbDBAL::execute('CREATE DATABASE ' . $conn->quoteIdentifier($db_name), $conn);
    taskman_msg("Database ({$db_name}) created...\n");
}
 protected function _createRootElementIfNotExists()
 {
     if (!($root = lmbDBAL::fetchOneRow('SELECT * FROM ' . $this->_table->getName()))) {
         lmbDBAL::execute('INSERT INTO ' . $this->_table->getName() . ' (parent_id, level, priority, is_published, path, identifier, ctime, utime) VALUES (0,0,0,0,\'/1/\',\'\',0,0)');
     }
 }
 protected function _changeItemsPriority($model, $where_field, $where_field_value)
 {
     $priority_items = $this->request->get('priority_items');
     $info_item = new $model();
     $sql = 'SELECT id, priority FROM ' . $info_item->getTableName() . ' WHERE ' . $where_field . '=' . $where_field_value;
     $current_priorities_object = lmbDBAL::fetch($sql);
     $current_priorities_object = $current_priorities_object->getArray();
     $current_priorities = array();
     foreach ($current_priorities_object as $item) {
         $current_priorities[$item->get('id')] = $item->get('priority');
     }
     foreach ($priority_items as $id => $priority) {
         $current_priorities[$id] = $priority;
     }
     asort($current_priorities);
     $i = 10;
     $table_name = $info_item->getTableName();
     foreach ($current_priorities as $id => $priority) {
         $sql = "UPDATE " . $table_name . " SET priority='" . $i . "' WHERE id='" . $id . "'";
         lmbDBAL::execute($sql);
         $i += 10;
     }
 }