Browse Source

Cambio information_schema

Se cambio las consultas al information_schema por problemas del describe
gabriel 7 years ago
parent
commit
d12c47dfa0
1 changed files with 90 additions and 11 deletions
  1. 90 11
      Migrations/MigrationsBase.php

+ 90 - 11
Migrations/MigrationsBase.php

@@ -572,15 +572,60 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
      */
     protected function existFieldInTable($table, $field)
     {
-        $stmt = $this->connection->prepare("DESCRIBE " . $table . ";");
-        $stmt->execute();
-        $resp = $stmt->fetchAll();
-        foreach ($resp as $key => $value) {
-            if (strtolower($value["Field"]) === strtolower($field)) {
-                return true;
+        $existe = false;
+        try {
+            $base = $this->getDataBaseName();
+            $sql = "SELECT ifnull(COLUMN_NAME, '') as nombre " .
+                "FROM information_schema.columns " .
+                "WHERE TABLE_SCHEMA = '" . $base . "' AND " .
+                "      TABLE_NAME = '" . $table . "' AND " .
+                "      COLUMN_NAME = '" . $field . "'; ";
+            $stmt = $this->connection->prepare($sql);
+            $stmt->execute();
+            $resp = $stmt->fetchAll();
+            foreach ($resp as $key => $value) {
+                if (strtolower($value["nombre"]) === strtolower($field)) {
+                    $existe = true;
+                    break;
+                }
             }
+            return $existe;
+        } catch (\Throwable $e) {
+            return $existe;
+        }
+    }
+
+    /**
+     * Funcion que verifica si existe un campo de una tabla.
+     * @param string $table Contiene el nombre de la tabla.
+     * @param string $field Contiene el nombre el campo.
+     * @param string $type Contiene el typo de dato.
+     * @return bool Retorna true si el campo existe.
+     */
+    protected function existFieldType($table, $field, $type)
+    {
+        $existe = false;
+        try {
+            $base = $this->getDataBaseName();
+            $sql = "SELECT ifnull(DATA_TYPE, '') as nombre " .
+                "FROM information_schema.columns " .
+                "WHERE TABLE_SCHEMA = '" . $base . "' AND " .
+                "      TABLE_NAME = '" . $table . "' AND " .
+                "      COLUMN_NAME = '" . $field . "' " .
+                "LIMIT 1;";
+            $stmt = $this->connection->prepare($sql);
+            $stmt->execute();
+            $resp = $stmt->fetchAll();
+            foreach ($resp as $key => $value) {
+                if (strtolower($value["nombre"]) === strtolower($type)) {
+                    $existe = true;
+                    break;
+                }
+            }
+            return $existe;
+        } catch (\Throwable $e) {
+            return $existe;
         }
-        return false;
     }
 
     /**
@@ -590,14 +635,48 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
      */
     protected function existTable($table)
     {
-        $resp = "";
+        $existe = false;
         try {
-            $stmt = $this->connection->prepare("DESCRIBE " . $table . ";");
+            $base = $this->getDataBaseName();
+            $sql = "SELECT ifnull(TABLE_NAME, '') as nombre " .
+                "FROM information_schema.columns " .
+                "WHERE TABLE_SCHEMA = '" . $base . "' AND " .
+                "      TABLE_NAME = '" . $table . "' " .
+                "LIMIT 1;";
+            $stmt = $this->connection->prepare($sql);
+            var_dump($sql);
             $stmt->execute();
             $resp = $stmt->fetchAll();
-            return (count($resp) > 0);
+            foreach ($resp as $key => $value) {
+                if (strtolower($value["nombre"]) === strtolower($table)) {
+                    $existe = true;
+                    break;
+                }
+            }
+            return $existe;
+        } catch (\Throwable $e) {
+            return $existe;
+        }
+    }
+
+    /**
+     * Funcion que obtiene el nombre de la base de datos a la cual estoy conectado.
+     * @return string Retorna el nombre de la base de datos o NULL en caso de error.
+     */
+    protected function getDataBaseName()
+    {
+        $resp = null;
+        try {
+            $stmt = $this->connection->prepare("SELECT DATABASE() as base;");
+            $stmt->execute();
+            $resp = $stmt->fetchAll();
+            foreach ($resp as $key => $value) {
+                $resp = $value["base"];
+                break;
+            }
+            return $resp;
         } catch (\Throwable $e) {
-            return false;
+            return $resp;
         }
     }