Ejemplo n.º 1
0
 private function route()
 {
     //Route determination
     $url = Tools::getParam("url", null);
     Log::write('MicroMuffin::route : url called is ' . $url);
     $route = Router::get(!is_null($url) ? $url : "");
     if (!is_null($route)) {
         $this->route = $route;
     } else {
         header("HTTP/1.0 404 Not Found");
         $e = new Error("Page not found", "The page you are looking for doesn't exist.");
         $e->display();
     }
 }
Ejemplo n.º 2
0
 /**
  * @return string
  */
 public function getCapName()
 {
     return Tools::capitalize($this->name);
 }
Ejemplo n.º 3
0
 private function writeT_Models()
 {
     $save_dir = Generator::$relativeTModelSaveDir;
     foreach ($this->tables as $table) {
         $fileName = 't_' . Tools::removeSFromTableName($table->getName()) . '.php';
         $file = fopen($save_dir . $fileName, "w");
         fwrite($file, $this->T_ModelToString($table));
         fclose($file);
     }
 }
Ejemplo n.º 4
0
 /**
  * @return string
  */
 public function getClassName()
 {
     return Tools::capitalize($this->name, 4);
 }
Ejemplo n.º 5
0
 /**
  * @param self[] $objects
  */
 public static function saveAll(array $objects)
 {
     if (count($objects) == 0) {
         return;
     }
     /** @var self $class */
     $class = '\\' . get_called_class();
     $table = $class::getTableName();
     $attributes = $objects[0]->getAttributes(new \ReflectionClass($objects[0]));
     $nbColumns = count($attributes);
     $nbTotalRows = count($objects);
     $rowsByInsert = 0;
     /* Building attribute => getter list */
     $attrGetter = array();
     $fields = '(';
     foreach ($attributes as $k => $v) {
         if ($k != 'id' || $v != 0) {
             $attrGetter[$k] = 'get' . Tools::capitalize($k);
             $fields .= $k . ', ';
         }
     }
     $fields = substr($fields, 0, -2) . ')';
     unset($attributes);
     /* Depending of how many columns we have to insert, we don't put the same rows number in a single query */
     if ($nbColumns > 0 && $nbColumns <= 4) {
         $rowsByInsert = 50;
     } else {
         if ($nbColumns <= 10) {
             $rowsByInsert = 10;
         } else {
             $rowsByInsert = 2;
         }
     }
     $rowsInPartialInsert = $nbTotalRows % $rowsByInsert;
     /* Constructing queries, one for a full insertion, one for remaining rows that cannont fill a full insert */
     $pdo = PDOS::getInstance();
     $sql = 'INSERT INTO ' . $table . ' ' . $fields . ' VALUES ';
     $sqlValues1 = '';
     $sqlValues2 = '';
     for ($i = 0; $i < $rowsByInsert; $i++) {
         $sqlValues1 .= '(';
         if ($i < $rowsInPartialInsert) {
             $sqlValues2 .= '(';
         }
         foreach ($attrGetter as $k => $v) {
             $sqlValues1 .= ':' . $k . '_' . $i . ', ';
             if ($i < $rowsInPartialInsert) {
                 $sqlValues2 .= ':' . $k . '_' . $i . ', ';
             }
         }
         $sqlValues1 = substr($sqlValues1, 0, -2) . '), ';
         if ($i < $rowsInPartialInsert) {
             $sqlValues2 = substr($sqlValues2, 0, -2) . '), ';
         }
     }
     $sqlValues1 = substr($sqlValues1, 0, -2);
     $sqlValues2 = substr($sqlValues2, 0, -2);
     $queryFull = $pdo->prepare($sql . $sqlValues1);
     if ($rowsInPartialInsert > 0) {
         $queryPartial = $pdo->prepare($sql . $sqlValues2);
     } else {
         $queryPartial = null;
     }
     /* Executing loop, filling values and executing queries */
     $nbFullInserts = (int) ($nbTotalRows / $rowsByInsert);
     $reflection = new \ReflectionClass(get_called_class());
     $pdo->beginTransaction();
     $i = 0;
     for ($j = 0; $j < $nbFullInserts; $j++) {
         for ($k = 0; $k < $rowsByInsert; $k++) {
             $object = $objects[$i];
             foreach ($attrGetter as $attr => $getter) {
                 $method = $reflection->getMethod($getter);
                 if ($method->isPrivate()) {
                     $property = $reflection->getProperty('_' . $attr);
                     $property->setAccessible(true);
                     $val = $property->getValue($object);
                     $property->setAccessible(false);
                 } else {
                     $val = $object->{$getter}();
                 }
                 $queryFull->bindValue(':' . $attr . '_' . $k, is_bool($val) ? $val ? 'true' : 'false' : $val);
             }
             $i++;
         }
         $queryFull->execute();
     }
     if ($rowsInPartialInsert > 0) {
         for ($k = 0; $k < $rowsInPartialInsert; $k++) {
             $object = $objects[$i];
             foreach ($attrGetter as $attr => $getter) {
                 $method = $reflection->getMethod($getter);
                 if ($method->isPrivate()) {
                     $property = $reflection->getProperty('_' . $attr);
                     $property->setAccessible(true);
                     $val = $property->getValue($object);
                     $property->setAccessible(false);
                 } else {
                     $val = $object->{$getter}();
                 }
                 $queryPartial->bindValue(':' . $attr . '_' . $k, is_bool($val) ? $val ? 'true' : 'false' : $val);
             }
             $i++;
         }
         $queryPartial->execute();
     }
     $pdo->commit();
 }
 /**
  * @return string
  */
 public function getReturnedClassName()
 {
     return Tools::capitalize(Tools::removeSFromTableName($this->returnType));
 }
Ejemplo n.º 7
0
 /**
  * WARNING ! Foreign denomination is inverted compared with constraints query result
  *
  * @param string $foreignTable
  * @param string $foreignColumn
  * @param string $foreignColumnClean
  * @param string $tableName
  * @param string $columnType
  * @return string
  */
 public function writeOneToManyProcedure($foreignTable, $foreignColumn, $foreignColumnClean, $tableName, $columnType)
 {
     $procedureName = strtolower('otm_' . $foreignTable . 'from' . Tools::removeSFromTableName($tableName) . '_' . $foreignColumnClean);
     $pdo = PDOS::getInstance();
     $pdo->exec("\n        CREATE OR REPLACE function " . $procedureName . "(foreign_column " . $columnType . ")\n        RETURNS SETOF " . $foreignTable . " AS\n        'SELECT * FROM " . $foreignTable . " WHERE " . $foreignColumn . " = \$1'\n        LANGUAGE sql VOLATILE\n        COST 100\n        ROWS 1000;\n        ALTER function " . $procedureName . "(" . $columnType . ")\n        OWNER TO \"" . DBUSER . "\";");
     return $procedureName;
 }