/**
  * 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);
     }
 }
Esempio n. 2
1
 /**
  * Insert events in bulk to the database.
  *
  * @param array $evententries raw event data
  */
 protected function insert_event_entries($evententries)
 {
     if (!$this->init()) {
         return;
     }
     if (!($dbtable = $this->get_config('dbtable'))) {
         return;
     }
     try {
         $this->extdb->insert_records($dbtable, $evententries);
     } catch (\moodle_exception $e) {
         debugging('Cannot write to external database: ' . $e->getMessage(), DEBUG_DEVELOPER);
     }
 }
 /**
  * Insert multiple records into database as fast as possible.
  *
  * Order of inserts is maintained, but the operation is not atomic,
  * use transactions if necessary.
  *
  * This method is intended for inserting of large number of small objects,
  * do not use for huge objects with text or binary fields.
  *
  * @since Moodle 2.7
  *
  * @param string $table  The database table to be inserted into
  * @param array|Traversable $dataobjects list of objects to be inserted, must be compatible with foreach
  * @return void does not return new record ids
  *
  * @throws coding_exception if data objects have different structure
  * @throws dml_exception A DML specific exception is thrown for any errors.
  */
 public function insert_records($table, $dataobjects)
 {
     if (!is_array($dataobjects) and !$dataobjects instanceof Traversable) {
         throw new coding_exception('insert_records() passed non-traversable object');
     }
     // PostgreSQL does not seem to have problems with huge queries.
     $chunksize = 500;
     if (!empty($this->dboptions['bulkinsertsize'])) {
         $chunksize = (int) $this->dboptions['bulkinsertsize'];
     }
     $columns = $this->get_columns($table, true);
     // Make sure there are no nasty blobs!
     foreach ($columns as $column) {
         if ($column->binary) {
             parent::insert_records($table, $dataobjects);
             return;
         }
     }
     $fields = null;
     $count = 0;
     $chunk = array();
     foreach ($dataobjects as $dataobject) {
         if (!is_array($dataobject) and !is_object($dataobject)) {
             throw new coding_exception('insert_records() passed invalid record object');
         }
         $dataobject = (array) $dataobject;
         if ($fields === null) {
             $fields = array_keys($dataobject);
             $columns = array_intersect_key($columns, $dataobject);
             unset($columns['id']);
         } else {
             if ($fields !== array_keys($dataobject)) {
                 throw new coding_exception('All dataobjects in insert_records() must have the same structure!');
             }
         }
         $count++;
         $chunk[] = $dataobject;
         if ($count === $chunksize) {
             $this->insert_chunk($table, $chunk, $columns);
             $chunk = array();
             $count = 0;
         }
     }
     if ($count) {
         $this->insert_chunk($table, $chunk, $columns);
     }
 }