gabriel 7 anos atrás
pai
commit
6e0ece2068
1 arquivos alterados com 67 adições e 2 exclusões
  1. 67 2
      Migrations/MigrationsBase.php

+ 67 - 2
Migrations/MigrationsBase.php

@@ -491,7 +491,7 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
      * @param array $arrayPrepare Contiene un array con los valores. La key contiene el valor a
      *                            buscar en la sentencia sql y el value es el valor.
      */
-    private function executeSQL($sql, $type, $arrayPrepare = null)
+    public function executeSQL($sql, $type, $arrayPrepare = null)
     {
         $stmt = $this->connection->prepare($sql);
         $param = "";
@@ -725,5 +725,70 @@ class MigrationsBase extends AbstractMigration implements ContainerAwareInterfac
         }
     }
 
+    /**
+     * Funcion que obtiene el valor del auto_increment.
+     * @param string $table Contiene el nombre de la tabla.
+     * @return array Retorna el array con los datos encontrados. O NULL en caso de error.
+     */
+    public function getAutoIncrementValue($table)
+    {
+        $autoIncrement = 1;
+        try {
+            $base = $this->getDataBaseName();
+            $sql = "SELECT `AUTO_INCREMENT` " .
+                "FROM  INFORMATION_SCHEMA.TABLES " .
+                "WHERE TABLE_SCHEMA = '" . $base . "' AND " .
+                "      TABLE_NAME = '" . $table . "';";
+
+            $stmt = $this->connection->prepare($sql);
+            $stmt->execute();
+            $resp = $stmt->fetchAll();
+            foreach ($resp as $key => $value) {
+                if (isset($value["AUTO_INCREMENT"])) {
+                    $autoIncrement = $value["AUTO_INCREMENT"];
+                }
+            }
+        } catch (\Throwable $e) {
+        }
+        return $autoIncrement;
+    }
+
+    /**
+     * Funcion que setea el valor del auto_increment.
+     * @param string $table Contiene el nombre de la tabla.
+     * @param int $value Contiene el proximo numero autilizar.
+     */
+    public function setAutoIncrementValue($table, $value)
+    {
+        try {
+            $base = $this->getDataBaseName();
+            $stmt = $this->connection->prepare("ALTER TABLE " . $base . "." . $table . " AUTO_INCREMENT=" . $value . ";");
+            $stmt->execute();
+        } catch (\Throwable $e) {
+        }
+    }
+
+    /**
+     * Funcion que verifica si existe un valor dentro de una tabla.
+     * @param string $table Contiene el nombre de la tabla.
+     * @param string $field Contiene el/los campos a mostrar.
+     * @param string $where Contiene el where de la consulta. Solo los campos con los valores. SIN
+     *                      LA PALABRA CLAVE.
+     * @return array Retorna el array con los datos encontrados. O NULL en caso de error.
+     */
+    public function existValueInTable($table, $field, $where)
+    {
+        try {
+            $sql = "SELECT " . $field . " " .
+                "FROM  " . $table . " " .
+                "WHERE " . $where . "; ";
+            $stmt = $this->connection->prepare($sql);
+            $stmt->execute();
+            $resp = $stmt->fetchAll();
+            return $resp;
+        } catch (\Throwable $e) {
+            return null;
+        }
+    }
+}
 
-}