insertData() публичный Метод

Insert a row into the database.
public insertData ( string $query, array $params = [] ) : integer | false
$query string The query to execute.
$params array Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve']
Результат integer | false The database id of the inserted row if a AUTO_INCREMENT field is defined, 0 if not, and false on failure.
Пример #1
0
 /**
  * Creates a table {prefix}{$namespace}_lock that is used as a mutex.
  *
  * @param string $namespace Allows having separate locks for separate processes
  * @return bool
  */
 public function lock($namespace)
 {
     $this->assertNamespace($namespace);
     if (!$this->isLocked($namespace)) {
         // Lock it
         $this->db->insertData("CREATE TABLE {$this->db->getTablePrefix()}{$namespace}_lock (id INT)");
         $this->logger->info("Locked mutex for {$namespace}");
         return true;
     }
     $this->logger->warn("Cannot lock mutex for {$namespace}: already locked.");
     return false;
 }
Пример #2
0
 /**
  * {@inheritDoc}
  */
 public function write($session_id, $session_data)
 {
     $id = sanitize_string($session_id);
     $time = time();
     $sess_data_sanitised = sanitize_string($session_data);
     $query = "REPLACE INTO {$this->db->getTablePrefix()}users_sessions\n\t\t\t(session, ts, data) VALUES\n\t\t\t('{$id}', '{$time}', '{$sess_data_sanitised}')";
     if ($this->db->insertData($query) !== false) {
         return true;
     } else {
         return false;
     }
 }
Пример #3
0
 /**
  * {@inheritDoc}
  */
 public function write($session_id, $session_data)
 {
     $id = sanitize_string($session_id);
     $time = time();
     $sess_data_sanitised = sanitize_string($session_data);
     $query = "INSERT INTO {$this->db->prefix}users_sessions\n\t\t\t(session, ts, data) VALUES\n\t\t\t('{$id}', '{$time}', '{$sess_data_sanitised}')\n\t\t\tON DUPLICATE KEY UPDATE ts = '{$time}', data = '{$sess_data_sanitised}'";
     if ($this->db->insertData($query) !== false) {
         return true;
     } else {
         return false;
     }
 }
Пример #4
0
 /**
  * Create a relationship between two entities. E.g. friendship, group membership, site membership.
  *
  * This function lets you make the statement "$guid_one is a $relationship of $guid_two". In the statement,
  * $guid_one is the subject of the relationship, $guid_two is the target, and $relationship is the type.
  *
  * @param int    $guid_one     GUID of the subject entity of the relationship
  * @param string $relationship Type of the relationship
  * @param int    $guid_two     GUID of the target entity of the relationship
  * @param bool   $return_id    Return the ID instead of bool?
  *
  * @return bool|int
  * @throws \InvalidArgumentException
  */
 public function add($guid_one, $relationship, $guid_two, $return_id = false)
 {
     if (strlen($relationship) > \ElggRelationship::RELATIONSHIP_LIMIT) {
         $msg = "relationship name cannot be longer than " . \ElggRelationship::RELATIONSHIP_LIMIT;
         throw new \InvalidArgumentException($msg);
     }
     // Check for duplicates
     // note: escape $relationship after this call, we don't want to double-escape
     if ($this->check($guid_one, $relationship, $guid_two)) {
         return false;
     }
     $sql = "\n\t\t\tINSERT INTO {$this->db->prefix}entity_relationships\n\t\t\t       (guid_one, relationship, guid_two, time_created)\n\t\t\tVALUES (:guid1, :relationship, :guid2, :time)\n\t\t\t\tON DUPLICATE KEY UPDATE time_created = :time\n\t\t";
     $params = [':guid1' => (int) $guid_one, ':guid2' => (int) $guid_two, ':relationship' => $relationship, ':time' => $this->getCurrentTime()->getTimestamp()];
     $id = $this->db->insertData($sql, $params);
     if (!$id) {
         return false;
     }
     $obj = $this->get($id);
     $result = $this->events->trigger('create', 'relationship', $obj);
     if (!$result) {
         $this->delete($id, false);
         return false;
     }
     return $return_id ? $obj->id : true;
 }
Пример #5
0
 /**
  * Create a relationship between two entities. E.g. friendship, group membership, site membership.
  *
  * This function lets you make the statement "$guid_one is a $relationship of $guid_two". In the statement,
  * $guid_one is the subject of the relationship, $guid_two is the target, and $relationship is the type.
  *
  * @param int    $guid_one     GUID of the subject entity of the relationship
  * @param string $relationship Type of the relationship
  * @param int    $guid_two     GUID of the target entity of the relationship
  *
  * @return bool
  * @throws \InvalidArgumentException
  */
 public function add($guid_one, $relationship, $guid_two)
 {
     if (strlen($relationship) > \ElggRelationship::RELATIONSHIP_LIMIT) {
         $msg = "relationship name cannot be longer than " . \ElggRelationship::RELATIONSHIP_LIMIT;
         throw new \InvalidArgumentException($msg);
     }
     // Check for duplicates
     // note: escape $relationship after this call, we don't want to double-escape
     if ($this->check($guid_one, $relationship, $guid_two)) {
         return false;
     }
     $guid_one = (int) $guid_one;
     $relationship = $this->db->sanitizeString($relationship);
     $guid_two = (int) $guid_two;
     $time = time();
     $id = $this->db->insertData("\n\t\t\tINSERT INTO {$this->db->getTablePrefix()}entity_relationships\n\t\t\t       (guid_one, relationship, guid_two, time_created)\n\t\t\tVALUES ({$guid_one}, '{$relationship}', {$guid_two}, {$time})\n\t\t\t\tON DUPLICATE KEY UPDATE time_created = {$time}\n\t\t");
     if (!$id) {
         return false;
     }
     $obj = $this->get($id);
     $result = $this->events->trigger('create', 'relationship', $obj);
     if (!$result) {
         $this->delete($id, false);
         return false;
     }
     return true;
 }
Пример #6
0
 /**
  * Create a new annotation.
  *
  * @param int    $entity_guid GUID of entity to be annotated
  * @param string $name        Name of annotation
  * @param string $value       Value of annotation
  * @param string $value_type  Type of value (default is auto detection)
  * @param int    $owner_guid  Owner of annotation (default is logged in user)
  * @param int    $access_id   Access level of annotation
  *
  * @return int|bool id on success or false on failure
  */
 function create($entity_guid, $name, $value, $value_type = '', $owner_guid = 0, $access_id = ACCESS_PRIVATE)
 {
     $result = false;
     $entity_guid = (int) $entity_guid;
     $value_type = detect_extender_valuetype($value, $value_type);
     $owner_guid = (int) $owner_guid;
     if ($owner_guid == 0) {
         $owner_guid = $this->session->getLoggedInUserGuid();
     }
     $access_id = (int) $access_id;
     // @todo we don't check that the entity is loaded which means the user may
     // not have access to the entity
     $entity = get_entity($entity_guid);
     if ($this->events->trigger('annotate', $entity->type, $entity)) {
         $sql = "INSERT INTO {$this->db->prefix}annotations\n\t\t\t\t(entity_guid, name, value, value_type, owner_guid, time_created, access_id)\n\t\t\t\tVALUES\n\t\t\t\t(:entity_guid, :name, :value, :value_type, :owner_guid, :time_created, :access_id)";
         $result = $this->db->insertData($sql, [':entity_guid' => $entity_guid, ':name' => $name, ':value' => $value, ':value_type' => $value_type, ':owner_guid' => $owner_guid, ':time_created' => $this->getCurrentTime()->getTimestamp(), ':access_id' => $access_id]);
         if ($result !== false) {
             $obj = elgg_get_annotation_from_id($result);
             if ($this->events->trigger('create', 'annotation', $obj)) {
                 return $result;
             } else {
                 // plugin returned false to reject annotation
                 elgg_delete_annotation_by_id($result);
                 return false;
             }
         }
     }
     return $result;
 }
Пример #7
0
 /**
  * Sets a private setting for an entity.
  *
  * @param int    $entity_guid The entity GUID
  * @param string $name        The name of the setting
  * @param string $value       The value of the setting
  * @return bool
  */
 public function set($entity_guid, $name, $value)
 {
     $entity_guid = (int) $entity_guid;
     $name = $this->db->sanitizeString($name);
     $value = $this->db->sanitizeString($value);
     $result = $this->db->insertData("INSERT into {$this->table}\n\t\t\t(entity_guid, name, value) VALUES\n\t\t\t({$entity_guid}, '{$name}', '{$value}')\n\t\t\tON DUPLICATE KEY UPDATE value='{$value}'");
     return $result !== false;
 }
Пример #8
0
 /**
  * Sets a private setting for an entity.
  *
  * @param int    $entity_guid The entity GUID
  * @param string $name        The name of the setting
  * @param string $value       The value of the setting
  * @return bool
  */
 public function set($entity_guid, $name, $value)
 {
     $this->cache->clear($entity_guid);
     _elgg_services()->boot->invalidateCache();
     if (!$this->entities->exists($entity_guid)) {
         return false;
     }
     $query = "\n\t\t\tINSERT into {$this->table}\n\t\t\t(entity_guid, name, value) VALUES\n\t\t\t(:entity_guid, :name, :value)\n\t\t\tON DUPLICATE KEY UPDATE value = :value\n\t\t";
     $params = [':entity_guid' => (int) $entity_guid, ':name' => (string) $name, ':value' => (string) $value];
     $result = $this->db->insertData($query, $params);
     return $result !== false;
 }
Пример #9
0
 /**
  * Set the value for a datalist element.
  * 
  * Plugin authors should use elgg_save_config() and pass null for the site GUID.
  * 
  * @warning Names should be selected so as not to collide with the names for the
  * site config.
  * 
  * @warning Values set here are not available in $CONFIG until next page load.
  *
  * @param string $name  The name of the datalist
  * @param string $value The new value
  *
  * @return bool
  * @access private
  */
 function set($name, $value)
 {
     $name = trim($name);
     if (!$this->validateName($name)) {
         return false;
     }
     $escaped_name = $this->db->sanitizeString($name);
     $escaped_value = $this->db->sanitizeString($value);
     $success = $this->db->insertData("INSERT INTO {$this->table}" . " SET name = '{$escaped_name}', value = '{$escaped_value}'" . " ON DUPLICATE KEY UPDATE value = '{$escaped_value}'");
     $this->cache->put($name, $value);
     return $success !== false;
 }
Пример #10
0
 /**
  * Create a new metadata object, or update an existing one.
  *
  * Metadata can be an array by setting allow_multiple to true, but it is an
  * indexed array with no control over the indexing.
  *
  * @param int    $entity_guid    The entity to attach the metadata to
  * @param string $name           Name of the metadata
  * @param string $value          Value of the metadata
  * @param string $value_type     'text', 'integer', or '' for automatic detection
  * @param int    $owner_guid     GUID of entity that owns the metadata. Default is logged in user.
  * @param int    $access_id      Default is ACCESS_PRIVATE
  * @param bool   $allow_multiple Allow multiple values for one key. Default is false
  *
  * @return int|false id of metadata or false if failure
  */
 function create($entity_guid, $name, $value, $value_type = '', $owner_guid = 0, $access_id = ACCESS_PRIVATE, $allow_multiple = false)
 {
     $entity_guid = (int) $entity_guid;
     // name and value are encoded in add_metastring()
     $value_type = detect_extender_valuetype($value, $this->db->sanitizeString(trim($value_type)));
     $time = time();
     $owner_guid = (int) $owner_guid;
     $allow_multiple = (bool) $allow_multiple;
     if (!isset($value)) {
         return false;
     }
     if ($owner_guid == 0) {
         $owner_guid = $this->session->getLoggedInUserGuid();
     }
     $access_id = (int) $access_id;
     $query = "SELECT * from {$this->table}" . " WHERE entity_guid = {$entity_guid} and name_id=" . $this->metastringsTable->getId($name) . " limit 1";
     $existing = $this->db->getDataRow($query);
     if ($existing && !$allow_multiple) {
         $id = (int) $existing->id;
         $result = $this->update($id, $name, $value, $value_type, $owner_guid, $access_id);
         if (!$result) {
             return false;
         }
     } else {
         // Support boolean types
         if (is_bool($value)) {
             $value = (int) $value;
         }
         // Add the metastrings
         $value_id = $this->metastringsTable->getId($value);
         if (!$value_id) {
             return false;
         }
         $name_id = $this->metastringsTable->getId($name);
         if (!$name_id) {
             return false;
         }
         // If ok then add it
         $query = "INSERT into {$this->table}" . " (entity_guid, name_id, value_id, value_type, owner_guid, time_created, access_id)" . " VALUES ({$entity_guid}, '{$name_id}','{$value_id}','{$value_type}', {$owner_guid}, {$time}, {$access_id})";
         $id = $this->db->insertData($query);
         if ($id !== false) {
             $obj = $this->get($id);
             if ($this->events->trigger('create', 'metadata', $obj)) {
                 $this->cache->save($entity_guid, $name, $value, $allow_multiple);
                 return $id;
             } else {
                 $this->delete($id);
             }
         }
     }
     return $id;
 }
Пример #11
0
 /**
  * Set the value for a datalist element.
  * 
  * Plugin authors should use elgg_save_config() and pass null for the site GUID.
  * 
  * @warning Names should be selected so as not to collide with the names for the
  * site config.
  * 
  * @warning Values set here are not available in $CONFIG until next page load.
  *
  * @param string $name  The name of the datalist
  * @param string $value The new value
  *
  * @return bool
  * @access private
  */
 function set($name, $value)
 {
     $name = trim($name);
     // cannot store anything longer than 255 characters in db, so catch before we set
     if (elgg_strlen($name) > 255) {
         $this->logger->error("The name length for configuration variables cannot be greater than 255");
         return false;
     }
     $escaped_name = $this->db->sanitizeString($name);
     $escaped_value = $this->db->sanitizeString($value);
     $success = $this->db->insertData("INSERT INTO {$this->table}" . " SET name = '{$escaped_name}', value = '{$escaped_value}'" . " ON DUPLICATE KEY UPDATE value = '{$escaped_value}'");
     $this->cache->put($name, $value);
     return $success !== false;
 }
Пример #12
0
 /**
  * Register \ElggEntities with a certain type and subtype to be loaded as a specific class.
  *
  * By default entities are loaded as one of the 4 parent objects: site, user, object, or group.
  * If you subclass any of these you can register the classname with add_subtype() so
  * it will be loaded as that class automatically when retrieved from the database with
  * {@link get_entity()}.
  *
  * @warning This function cannot be used to change the class for a type-subtype pair.
  * Use update_subtype() for that.
  *
  * @param string $type    The type you're subtyping (site, user, object, or group)
  * @param string $subtype The subtype
  * @param string $class   Optional class name for the object
  *
  * @return int
  * @see update_subtype()
  * @see remove_subtype()
  * @see get_entity()
  */
 public function add($type, $subtype, $class = "")
 {
     if (!$subtype) {
         return 0;
     }
     $id = $this->getId($type, $subtype);
     if (!$id) {
         $sql = "\n\t\t\t\tINSERT INTO {$this->db->prefix}entity_subtypes\n\t\t\t\t\t(type,  subtype,  class) VALUES\n\t\t\t\t\t(:type, :subtype, :class)\n\t\t\t";
         $params = [':type' => $type, ':subtype' => $subtype, ':class' => $class];
         $id = $this->db->insertData($sql, $params);
         $this->invalidateCache();
     }
     return $id;
 }
Пример #13
0
 /**
  * Create a new metadata object, or update an existing one.
  *
  * Metadata can be an array by setting allow_multiple to true, but it is an
  * indexed array with no control over the indexing.
  *
  * @param int    $entity_guid    The entity to attach the metadata to
  * @param string $name           Name of the metadata
  * @param string $value          Value of the metadata
  * @param string $value_type     'text', 'integer', or '' for automatic detection
  * @param int    $owner_guid     GUID of entity that owns the metadata. Default is logged in user.
  * @param int    $access_id      Default is ACCESS_PRIVATE
  * @param bool   $allow_multiple Allow multiple values for one key. Default is false
  *
  * @return int|false id of metadata or false if failure
  */
 function create($entity_guid, $name, $value, $value_type = '', $owner_guid = 0, $access_id = ACCESS_PRIVATE, $allow_multiple = false)
 {
     $entity_guid = (int) $entity_guid;
     $value_type = detect_extender_valuetype($value, $this->db->sanitizeString(trim($value_type)));
     $owner_guid = (int) $owner_guid;
     $allow_multiple = (bool) $allow_multiple;
     if (!isset($value)) {
         return false;
     }
     if ($owner_guid == 0) {
         $owner_guid = $this->session->getLoggedInUserGuid();
     }
     $access_id = (int) $access_id;
     $query = "SELECT * FROM {$this->table}\n\t\t\tWHERE entity_guid = :entity_guid and name = :name LIMIT 1";
     $existing = $this->db->getDataRow($query, null, [':entity_guid' => $entity_guid, ':name' => $name]);
     if ($existing && !$allow_multiple) {
         $id = (int) $existing->id;
         $result = $this->update($id, $name, $value, $value_type, $owner_guid, $access_id);
         if (!$result) {
             return false;
         }
     } else {
         // Support boolean types
         if (is_bool($value)) {
             $value = (int) $value;
         }
         // If ok then add it
         $query = "INSERT INTO {$this->table}\n\t\t\t\t(entity_guid, name, value, value_type, owner_guid, time_created, access_id)\n\t\t\t\tVALUES (:entity_guid, :name, :value, :value_type, :owner_guid, :time_created, :access_id)";
         $id = $this->db->insertData($query, [':entity_guid' => $entity_guid, ':name' => $name, ':value' => $value, ':value_type' => $value_type, ':owner_guid' => (int) $owner_guid, ':time_created' => $this->getCurrentTime()->getTimestamp(), ':access_id' => $access_id]);
         if ($id !== false) {
             $obj = $this->get($id);
             if ($this->events->trigger('create', 'metadata', $obj)) {
                 $this->cache->clear($entity_guid);
                 return $id;
             } else {
                 $this->delete($id);
             }
         }
     }
     return $id;
 }
Пример #14
0
 /**
  * Add or update a config setting.
  *
  * Plugin authors should use elgg_set_config().
  *
  * If the config name already exists, it will be updated to the new value.
  *
  * @note Internal: These settings are stored in the dbprefix_config table and read
  * during system boot into $CONFIG.
  *
  * @note Internal: The value is serialized so we maintain type information.
  *
  * @param string $name  The name of the configuration value
  * @param mixed  $value Its value
  *
  * @return bool
  */
 function set($name, $value)
 {
     $name = trim($name);
     // cannot store anything longer than 255 characters in db, so catch before we set
     if (elgg_strlen($name) > 255) {
         $this->logger->error("The name length for configuration variables cannot be greater than 255");
         return false;
     }
     $this->CONFIG->{$name} = $value;
     $dbprefix = $this->CONFIG->dbprefix;
     $sql = "\n\t\t\tINSERT INTO {$dbprefix}config\n\t\t\tSET name = :name,\n\t\t\t\tvalue = :value\n\t\t\tON DUPLICATE KEY UPDATE value = :value\n\t\t";
     $params = [':name' => $name, ':value' => serialize($value)];
     $version = (int) $this->CONFIG->version;
     if (!empty($version) && $version < 2016102500) {
         // need to do this the old way as long as site_guid columns have not been dropped
         $sql = "\n\t\t\t\tINSERT INTO {$dbprefix}config\n\t\t\t\tSET name = :name,\n\t\t\t\t\tvalue = :value,\n\t\t\t\t\tsite_guid = :site_guid\n\t\t\t\tON DUPLICATE KEY UPDATE value = :value\n\t\t\t";
         $params[':site_guid'] = 1;
     }
     $result = $this->db->insertData($sql, $params);
     $this->boot->invalidateCache();
     return $result !== false;
 }
Пример #15
0
 /**
  * Adds a new row to the entity table
  *
  * @param stdClass $row        Entity base information
  * @param array    $attributes All primary and secondary table attributes
  *                             Used by database mock services to allow mocking
  *                             entities that were instantiated using new keyword
  *                             and calling ElggEntity::save()
  * @return int|false
  */
 public function insertRow(stdClass $row, array $attributes = [])
 {
     $sql = "INSERT INTO {$this->db->prefix}entities\n\t\t\t(type, subtype, owner_guid, container_guid,\n\t\t\t\taccess_id, time_created, time_updated, last_action)\n\t\t\tVALUES\n\t\t\t(:type, :subtype_id, :owner_guid, :container_guid,\n\t\t\t\t:access_id, :time_created, :time_updated, :last_action)";
     return $this->db->insertData($sql, [':type' => $row->type, ':subtype_id' => $row->subtype_id, ':owner_guid' => $row->owner_guid, ':container_guid' => $row->container_guid, ':access_id' => $row->access_id, ':time_created' => $row->time_created, ':time_updated' => $row->time_updated, ':last_action' => $row->last_action]);
 }
Пример #16
0
 /**
  * {@inheritdoc}
  */
 public function insertData($query, array $params = [])
 {
     return $this->db->insertData($query, $params);
 }
Пример #17
0
 /**
  * Add a metastring.
  *
  * @warning You should not call this directly. Use elgg_get_metastring_id().
  *
  * @param string $string The value to be normalized
  * @return int The identifier for this string
  */
 function add($string)
 {
     $escaped_string = $this->db->sanitizeString(trim($string));
     return $this->db->insertData("INSERT INTO {$this->getTableName()} (string) VALUES ('{$escaped_string}')");
 }