/** * @return string */ function table() { if (!isset($this->table)) { $master_table = Dao::storeNameOf($this->property->class); $foreign_table = Dao::storeNameOf($this->property->getType()->getElementTypeAsString()); $this->table = $master_table < $foreign_table ? $master_table . '_' . $foreign_table : $foreign_table . '_' . $master_table; } return $this->table; }
/** * Builds a Foreign_Key for a column name that links to a given class name * * @param $table_name string the table name * @param $column_name string the column name linking to the foreign key (with or without 'id_') * @param $class_name string the foreign class name * @param $constraint string CASCADE, NO ACTION, RESTRICT, SET NULL * @return Foreign_Key */ public static function buildLink($table_name, $column_name, $class_name, $constraint = 'CASCADE') { if (substr($column_name, 0, 3) !== 'id_') { $column_name = 'id_' . $column_name; } $foreign_key = new Foreign_Key(); $foreign_key->Constraint = substr($table_name . DOT . $column_name, 0, 64); $foreign_key->Fields = $column_name; $foreign_key->On_delete = $constraint; $foreign_key->On_update = $constraint; $foreign_key->Reference_fields = 'id'; $foreign_key->Reference_table = Dao::storeNameOf($class_name); return $foreign_key; }
/** * Add a link class (using the 'link' class annotation) to joins * * @param $path string the property path * @param $class Link_Class the link class itself (which contains the @link) * @param $linked_class_name string the linked class name (the value of @link) * @param $join_mode string * @return Reflection_Property[] the properties that come from the linked class, * for further exclusion */ private function addLinkedClass($path, $class, $linked_class_name, $join_mode) { $linked_class = new Reflection_Class($linked_class_name); $join = new Join(); $join->master_alias = 't' . ($this->alias_counter - 1); $join->master_column = 'id_' . $class->getCompositeProperty($linked_class_name)->getAnnotation('storage')->value; $join->foreign_alias = 't' . $this->alias_counter++; $join->foreign_column = 'id'; $join->foreign_class = Builder::className($linked_class_name); $join->foreign_table = Dao::storeNameOf($join->foreign_class); $join->mode = $join_mode == Join::LEFT ? Join::LEFT : Join::INNER; $join->type = Join::LINK; if (!isset($this->joins[$path])) { // this ensures that the main path is set before the linked path $this->joins[$path] = null; } $this->joins[($path ? $path . '-' : '') . $join->foreign_table . '-@link'] = $join; $this->id_link_joins[$path] = $join; $this->link_joins[$path] = $join; $more_linked_class_name = $linked_class->getAnnotation('link')->value; $exclude_properties = $more_linked_class_name ? $this->addLinkedClass($path, $class, $more_linked_class_name, $join_mode) : []; foreach ($linked_class->getProperties([T_EXTENDS, T_USE]) as $property) { if (!$property->isStatic()) { if (!isset($exclude_properties[$property->name])) { $this->properties[$linked_class_name][$property->name] = $property; $property_path = ($path ? $path . DOT : '') . $property->name; $type = $property->getType(); if ($type->isClass()) { $this->classes[$property_path] = $property->getType()->getElementTypeAsString(); } $this->link_joins[$property_path] = $join; $exclude_properties[$property->name] = true; } } } return $exclude_properties; }
/** * @param $mysqli Contextual_Mysqli * @param $query string * @param $context string[] * @return boolean true if the query with an error can be retried after this error was dealt with */ private function onNoSuchTableError(Contextual_Mysqli $mysqli, $query, $context) { $retry = false; $error_table_names = [$this->parseNameFromError($mysqli->last_error)]; if (!reset($error_table_names)) { $error_table_names = $this->parseNamesFromQuery($query); } foreach ($context as $key => $context_class) { $context_table = is_array($context_class) ? $key : Dao::storeNameOf($context_class); if (in_array($context_table, $error_table_names)) { if (!is_array($context_class)) { $this->createTable($mysqli, $context_class); } else { $this->createImplicitTable($mysqli, $context_table, $context_class); } $retry = true; } } if (!$retry) { foreach ($error_table_names as $error_table_name) { $retry = $retry || $this->createTableWithoutContext($mysqli, $error_table_name, $query); } return $retry; } return $retry; }
/** * @param $property Reflection_Property * @return string */ private static function propertyReferenceTableToMysql(Reflection_Property $property) { return Dao::storeNameOf($property->getType()->asString()); }