public function saveForm($dbName) { $stmt = SQLTools::getConnection()->prepare("SELECT COUNT(*) total\n FROM information_schema.tables\n WHERE table_schema = :db\n AND table_name = :table;"); $stmt->execute(array("db" => $dbName, "table" => $this->name)); if ($stmt->fetchColumn()) { // ALTER /** * @var IComponent $component */ $columns = array("id"); foreach ($this->components as $component) { $stmt = SQLTools::getConnection()->prepare("SELECT COUNT(*) total\n FROM information_schema.columns\n WHERE\n table_schema = :db AND\n table_name = :table AND\n column_name = :column;"); $stmt->execute(array("db" => $dbName, "table" => $this->name, "column" => $component->getId())); if ($stmt->fetchColumn()) { SQLTools::execute(new ChangeField($this->name, $component->getId(), $component->toTableField())); } else { SQLTools::execute(new AddField($this->name, $component->toTableField())); } if (!empty($this->onFieldCreate)) { $this->onFieldCreate->__invoke($this, $component); } $columns[] = $component->getName(); } $stmt = SQLTools::getConnection()->prepare("SELECT column_name\n FROM information_schema.columns\n WHERE\n table_schema = :db AND\n table_name = :table AND\n column_name not in(:columns);"); $stmt->execute(array("db" => $dbName, "table" => $this->name, "columns" => implode(",", $columns))); $columnsToDelete = $stmt->fetchAll(\PDO::FETCH_OBJ); foreach ($columnsToDelete as $columnToDelete) { SQLTools::execute(new DropField($this->name, $columnToDelete->column_name)); } } else { $fields = $this->getTableFields(); // CREATE SQLTools::create_table($this->name, $fields); if (!empty($this->onFieldCreate)) { foreach ($fields as $field) { $this->onFieldCreate->__invoke($this, $field); } } } }
"rows": 3, "required": true, "id": "title" } }, { "type": "textField", "properties": { "name": "description", "placeholder": "Write your description here", "label": "Description", "classes": "form-control", "maxLength": "250", "multiLine": true, "rows": 3, "required": true, "id": "description" } } ] } '); FormBuilder::$autoAddPrimaryKey = true; $formBuilder->parse($json); try { SQLTools::execute(new DropDataBase("formbuilder_test")); } catch (PDOException $e) { } SQLTools::create_database(DB_NAME); SQLTools::getConfig()->setDb(DB_NAME); $formBuilder->saveForm(DB_NAME);
<?php require_once __DIR__ . '/../vendor/autoload.php'; use SQLTools\Command\CreateDataBase; use SQLTools\Command\CreateTable; use SQLTools\Entity\Field; use SQLTools\SQLConfig; use SQLTools\SQLTools; $dbName = "sql_tools_example"; $config = new SQLConfig("localhost", "root"); SQLTools::configure($config); SQLTools::execute(new CreateDataBase($dbName)); $config->setDb($dbName); $idField = new Field("id", "INT", null, false, null, true, false, 'AUTO_INCREMENT'); $nameField = new Field("name", "VARCHAR", 100, false); $descriptionField = new Field("description", "TEXT"); $dateField = new Field("date", "DATE"); $command = new CreateTable("event", array($idField, $nameField, $descriptionField, $dateField)); $errorInfo = SQLTools::execute($command)->errorInfo(); if (!empty($errorInfo) && $errorInfo[0] != '00000') { print_r($errorInfo); } else { echo "Everything is gonna be alright"; }
public function testCreateForeignIndex() { SQLTools::configure(new SQLConfig(self::HOST, self::USER, self::PWD, self::DB)); SQLTools::execute(new AddForeignKey("news", "categoryId", "category", "id")); }