当前位置: 首页>>代码示例>>PHP>>正文


PHP moodle_database::insert_record方法代码示例

本文整理汇总了PHP中moodle_database::insert_record方法的典型用法代码示例。如果您正苦于以下问题:PHP moodle_database::insert_record方法的具体用法?PHP moodle_database::insert_record怎么用?PHP moodle_database::insert_record使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在moodle_database的用法示例。


在下文中一共展示了moodle_database::insert_record方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: get_index_from_key

 /**
  * This function generates the unique index for a specific lock key.
  * Once an index is assigned to a key, it never changes - so this is
  * statically cached.
  *
  * @param string $key
  * @return int
  * @throws \moodle_exception
  */
 protected function get_index_from_key($key)
 {
     if (isset(self::$lockidcache[$key])) {
         return self::$lockidcache[$key];
     }
     $index = 0;
     $record = $this->db->get_record('lock_db', array('resourcekey' => $key));
     if ($record) {
         $index = $record->id;
     }
     if (!$index) {
         $record = new \stdClass();
         $record->resourcekey = $key;
         try {
             $index = $this->db->insert_record('lock_db', $record);
         } catch (\dml_exception $de) {
             // Race condition - never mind - now the value is guaranteed to exist.
             $record = $this->db->get_record('lock_db', array('resourcekey' => $key));
             if ($record) {
                 $index = $record->id;
             }
         }
     }
     if (!$index) {
         throw new \moodle_exception('Could not generate unique index for key');
     }
     self::$lockidcache[$key] = $index;
     return $index;
 }
开发者ID:evltuma,项目名称:moodle,代码行数:38,代码来源:postgres_lock_factory.php

示例2: commit

 /**
  * Commit new records.
  *
  * @return void
  */
 public function commit()
 {
     $request = (object) array('pid' => getmypid(), 'threadid' => ZEND_THREAD_SAFE ? zend_thread_id() : null, 'uid' => getmyuid(), 'url' => $this->url->out_as_local_url(false), 'hostname' => gethostname(), 'memory' => memory_get_usage(), 'peakmemory' => memory_get_peak_usage());
     // Not supported on Windows until PHP 7
     if (function_exists('getrusage')) {
         $resourceusage = getrusage();
         $request->numswaps = $resourceusage['ru_nswap'];
         $request->numpagefaults = $resourceusage['ru_majflt'];
         $request->usertime = $resourceusage['ru_utime.tv_usec'];
     }
     $request->id = $this->db->insert_record('telemetry_request', $request);
     foreach ($this->additionalstate as $collector) {
         $table = $collector->get_table();
         $records = $collector->get_records();
         foreach ($records as $record) {
             $record->requestid = $request->id;
         }
         $this->db->insert_records($table, $records);
     }
 }
开发者ID:AVADOLearning,项目名称:moodle-local_telemetry,代码行数:25,代码来源:request.php

示例3: get_lock

 /**
  * Create and get a lock
  * @param string $resource - The identifier for the lock. Should use frankenstyle prefix.
  * @param int $timeout - The number of seconds to wait for a lock before giving up.
  * @param int $maxlifetime - Unused by this lock type.
  * @return boolean - true if a lock was obtained.
  */
 public function get_lock($resource, $timeout, $maxlifetime = 86400)
 {
     $token = $this->generate_unique_token();
     $now = time();
     $giveuptime = $now + $timeout;
     $expires = $now + $maxlifetime;
     if (!$this->db->record_exists('lock_db', array('resourcekey' => $resource))) {
         $record = new \stdClass();
         $record->resourcekey = $resource;
         $result = $this->db->insert_record('lock_db', $record);
     }
     $params = array('expires' => $expires, 'token' => $token, 'resourcekey' => $resource, 'now' => $now);
     $sql = 'UPDATE {lock_db}
                SET
                    expires = :expires,
                    owner = :token
              WHERE
                    resourcekey = :resourcekey AND
                    (owner IS NULL OR expires < :now)';
     do {
         $now = time();
         $params['now'] = $now;
         $this->db->execute($sql, $params);
         $countparams = array('owner' => $token, 'resourcekey' => $resource);
         $result = $this->db->count_records('lock_db', $countparams);
         $locked = $result === 1;
         if (!$locked) {
             usleep(rand(10000, 250000));
             // Sleep between 10 and 250 milliseconds.
         }
         // Try until the giveup time.
     } while (!$locked && $now < $giveuptime);
     if ($locked) {
         $this->openlocks[$token] = 1;
         return new lock($token, $this);
     }
     return false;
 }
开发者ID:evltuma,项目名称:moodle,代码行数:45,代码来源:db_record_lock_factory.php

示例4: test_limits_and_offsets

 public function test_limits_and_offsets()
 {
     $DB = $this->tdb;
     $dbman = $DB->get_manager();
     if (false) {
         $DB = new moodle_database();
     }
     $table = $this->get_test_table();
     $tablename = $table->getName();
     $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
     $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, null, null, null);
     $table->add_field('content', XMLDB_TYPE_TEXT, 'big', XMLDB_UNSIGNED, XMLDB_NOTNULL);
     $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
     $dbman->create_table($table);
     $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'a', 'content' => 'one')));
     $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'b', 'content' => 'two')));
     $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'c', 'content' => 'three')));
     $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'd', 'content' => 'four')));
     $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'e', 'content' => 'five')));
     $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'f', 'content' => 'six')));
     $sqlqm = "SELECT *\n                    FROM {{$tablename}}";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 4));
     $this->assertEquals(2, count($records));
     $this->assertEquals('e', reset($records)->name);
     $this->assertEquals('f', end($records)->name);
     $sqlqm = "SELECT *\n                    FROM {{$tablename}}";
     $this->assertEmpty($records = $DB->get_records_sql($sqlqm, null, 8));
     $sqlqm = "SELECT *\n                    FROM {{$tablename}}";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 4));
     $this->assertEquals(4, count($records));
     $this->assertEquals('a', reset($records)->name);
     $this->assertEquals('d', end($records)->name);
     $sqlqm = "SELECT *\n                    FROM {{$tablename}}";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 8));
     $this->assertEquals(6, count($records));
     $this->assertEquals('a', reset($records)->name);
     $this->assertEquals('f', end($records)->name);
     $sqlqm = "SELECT *\n                    FROM {{$tablename}}";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 1, 4));
     $this->assertEquals(4, count($records));
     $this->assertEquals('b', reset($records)->name);
     $this->assertEquals('e', end($records)->name);
     $sqlqm = "SELECT *\n                    FROM {{$tablename}}";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 4, 4));
     $this->assertEquals(2, count($records));
     $this->assertEquals('e', reset($records)->name);
     $this->assertEquals('f', end($records)->name);
     $sqlqm = "SELECT t.*, t.name AS test\n                    FROM {{$tablename}} t\n                    ORDER BY t.id ASC";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 4, 4));
     $this->assertEquals(2, count($records));
     $this->assertEquals('e', reset($records)->name);
     $this->assertEquals('f', end($records)->name);
     $sqlqm = "SELECT DISTINCT t.name, t.name AS test\n                    FROM {{$tablename}} t\n                    ORDER BY t.name DESC";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 4, 4));
     $this->assertEquals(2, count($records));
     $this->assertEquals('b', reset($records)->name);
     $this->assertEquals('a', end($records)->name);
     $sqlqm = "SELECT 1\n                    FROM {{$tablename}} t\n                    WHERE t.name = 'a'";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 1));
     $this->assertEquals(1, count($records));
     $sqlqm = "SELECT 'constant'\n                    FROM {{$tablename}} t\n                    WHERE t.name = 'a'";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 8));
     $this->assertEquals(1, count($records));
     $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'a', 'content' => 'one')));
     $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'b', 'content' => 'two')));
     $this->assertNotEmpty($DB->insert_record($tablename, array('name' => 'c', 'content' => 'three')));
     $sqlqm = "SELECT t.name, COUNT(DISTINCT t2.id) AS count, 'Test' AS teststring\n                    FROM {{$tablename}} t\n                    LEFT JOIN (\n                        SELECT t.id, t.name\n                        FROM {{$tablename}} t\n                    ) t2 ON t2.name = t.name\n                    GROUP BY t.name\n                    ORDER BY t.name ASC";
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm));
     $this->assertEquals(6, count($records));
     // a,b,c,d,e,f
     $this->assertEquals(2, reset($records)->count);
     // a has 2 records now
     $this->assertEquals(1, end($records)->count);
     // f has 1 record still
     $this->assertNotEmpty($records = $DB->get_records_sql($sqlqm, null, 0, 2));
     $this->assertEquals(2, count($records));
     $this->assertEquals(2, reset($records)->count);
     $this->assertEquals(2, end($records)->count);
 }
开发者ID:rama1712,项目名称:moodle,代码行数:79,代码来源:dml_test.php


注:本文中的moodle_database::insert_record方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。