Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Basic Support for MSSQL Database #84

Merged
merged 10 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"webfiori/ui":"2.2.2",
"webfiori/jsonx":"2.1.0",
"webfiori/http":"3.1.0",
"webfiori/database":"0.2.12"
"webfiori/database":"0.3.1"
},
"keywords": [
"framework",
Expand Down
8 changes: 7 additions & 1 deletion webfiori/framework/cli/commands/AddCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ public function exec() {
return 0;
}
private function _addDbConnection() {
$connInfoObj = new ConnectionInfo('mysql', 'roor', 'pass', 'ok');
$dbType = $this->select('Select database type:', ConnectionInfo::SUPPORTED_DATABASES);
if ($dbType == 'mysql') {
$connInfoObj = new ConnectionInfo('mysql', 'roor', 'pass', 'ok');
} else if ($dbType == 'mssql') {
$connInfoObj = new ConnectionInfo('mssql', 'roor', 'pass', 'ok');
}

$connInfoObj->setHost($this->getInput('Database host:', '127.0.0.1'));
$connInfoObj->setPort($this->getInput('Port number:', 3306));
$connInfoObj->setUsername($this->getInput('Username:'));
Expand Down
9 changes: 7 additions & 2 deletions webfiori/framework/cli/commands/RunSQLQueryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ private function queryOnSchema(DB $schema) {

return $this->confirmExecute($schema);
}
/**
*
* @param type $schema
* @param Table $tableObj
*/
private function tableQuery($schema, $tableObj) {
$queryTypes = [
'Create database table.',
Expand All @@ -232,9 +237,9 @@ private function tableQuery($schema, $tableObj) {
} else if ($selectedQuery == 'Add Forign Key.' || $selectedQuery == 'Drop Forign Key.') {
$this->fkQuery($schema, $selectedQuery, $tableObj);
} else if ($selectedQuery == 'Create database table.') {
$schema->table($tableObj->getName())->createTable();
$schema->table($tableObj->getNormalName())->createTable();
} else if ($selectedQuery == 'Drop database table.') {
$schema->table($tableObj->getName())->drop();
$schema->table($tableObj->getNormalName())->drop();
}
}
}
35 changes: 28 additions & 7 deletions webfiori/framework/cli/helpers/CreateTableObj.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
use Error;
use Exception;
use webfiori\database\mysql\MySQLColumn;
use webfiori\database\mssql\MSSQLColumn;
use webfiori\database\mysql\MySQLTable;
use webfiori\database\mssql\MSSQLTable;
use webfiori\database\Table;
use webfiori\framework\cli\commands\CreateCommand;
use webfiori\framework\cli\writers\QueryClassWriter;
use webfiori\database\ConnectionInfo;
/**
* A helper class for creating database tables classes.
*
Expand All @@ -26,11 +29,16 @@ class CreateTableObj {
*/
public function __construct(CreateCommand $command) {
$this->command = $command;

$dbType = $this->_getCommand()->select('Database type:', ConnectionInfo::SUPPORTED_DATABASES);

$classInfo = $this->_getCommand()->getClassInfo(APP_DIR_NAME.'\\database');


$tempTable = new MySQLTable();
if ($dbType == 'mysql') {
$tempTable = new MySQLTable();
} else if ($dbType == 'mssql') {
$tempTable = new MSSQLTable();
}
$this->_setTableName($tempTable);
$this->_setTableComment($tempTable);
$this->_getCommand()->println('Now you have to add columns to the table.');
Expand All @@ -42,7 +50,11 @@ public function __construct(CreateCommand $command) {
$this->_getCommand()->warning("The table already has a key with name '$colKey'.");
continue;
}
$col = new MySQLColumn();
if ($tempTable instanceof MySQLTable) {
$col = new MySQLColumn();
} else {
$col = new MSSQLColumn();
}
$col->setName(str_replace('-', '_', str_replace(' ', '_', $colKey)));
$colDatatype = $this->_getCommand()->select('Select column data type:', $col->getSupportedTypes(), 0);
$col->setDatatype($colDatatype);
Expand Down Expand Up @@ -199,7 +211,7 @@ private function _isPrimaryCheck($colObj) {
}
$this->_setDefaultValue($colObj);
$colObj->setIsNull($this->_getCommand()->confirm('Can this column have null values?', false));
} else if ($colObj->getDatatype() == 'int') {
} else if ($colObj->getDatatype() == 'int' && $colObj instanceof MySQLColumn) {
$colObj->setIsAutoInc($this->_getCommand()->confirm('Is this column auto increment?', false));
}
}
Expand Down Expand Up @@ -250,14 +262,23 @@ private function _setScale($colObj) {
*/
private function _setSize($colObj) {
$type = $colObj->getDatatype();
$supportSize = $type == 'int'
$mySqlSupportSize = $type == 'int'
|| $type == 'varchar'
|| $type == 'decimal'
|| $type == 'float'
|| $type == 'double'
|| $type == 'text';

if ($supportSize) {
$mssqlSupportSize = $type == 'char'
|| $type == 'nchar'
|| $type == 'varchar'
|| $type == 'nvarchar'
|| $type == 'binary'
|| $type == 'varbinary'
|| $type == 'decimal'
|| $type == 'float';

if (($colObj instanceof MySQLColumn && $mySqlSupportSize)
|| $colObj instanceof MSSQLColumn && $mssqlSupportSize) {
$valid = false;

do {
Expand Down
22 changes: 16 additions & 6 deletions webfiori/framework/cli/writers/QueryClassWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use InvalidArgumentException;
use webfiori\database\EntityMapper;
use webfiori\database\mysql\MySQLColumn;
use webfiori\database\mysql\MySQLTable;
use webfiori\database\mssql\MSSQLTable;
use webfiori\database\Table;

/**
Expand Down Expand Up @@ -223,7 +225,7 @@ private function _appendColObj($key, $colObj) {
if ($colObj->isPrimary()) {
$this->append("'primary' => true,", 4);

if ($colObj->isAutoInc()) {
if ($colObj instanceof MySQLColumn && $colObj->isAutoInc()) {
$this->append("'auto-inc' => true,", 4);
}
}
Expand Down Expand Up @@ -258,7 +260,7 @@ private function _writeConstructor() {
$this->append(" * Creates new instance of the class.", 1);
$this->append(" */", 1);
$this->append('public function __construct(){', 1);
$this->append('parent::__construct(\''.$this->tableObj->getName().'\');', 2);
$this->append('parent::__construct(\''.$this->tableObj->getNormalName().'\');', 2);

if ($this->tableObj->getComment() !== null) {
$this->append('$this->setComment(\''.$this->tableObj->getComment().'\');', 2);
Expand All @@ -270,21 +272,29 @@ private function _writeConstructor() {
private function _writeHeaderSec() {
$this->append("<?php\n");
$this->append('namespace '.$this->getNamespace().";\n");
$this->append("use webfiori\database\mysql\MySQLTable;");
if ($this->tableObj instanceof MySQLTable) {
$this->append("use webfiori\database\mysql\MySQLTable;");
} else if ($this->tableObj instanceof MSSQLTable) {
$this->append("use webfiori\database\mssql\MSSQLTable;");
}
$this->addFksTables();

$this->append('');
$this->append("/**\n"
." * A class which represents the database table '".$this->tableObj->getName()."'.\n"
." * A class which represents the database table '".$this->tableObj->getNormalName()."'.\n"
." * The table which is associated with this class will have the following columns:\n"
." * <ul>"
);

foreach ($this->tableObj->getCols() as $key => $colObj) {
$this->append(" * <li><b>$key</b>: Name in database: '".$colObj->getName()."'. Data type: '".$colObj->getDatatype()."'.</li>");
$this->append(" * <li><b>$key</b>: Name in database: '".$colObj->getNormalName()."'. Data type: '".$colObj->getDatatype()."'.</li>");
}
$this->append(" * </ul>\n */");
$this->append('class '.$this->getName().' extends MySQLTable {');
if ($this->tableObj instanceof MySQLTable) {
$this->append('class '.$this->getName().' extends MySQLTable {');
} else if ($this->tableObj instanceof MSSQLTable) {
$this->append('class '.$this->getName().' extends MSSQLTable {');
}
}
private function addFksTables() {
$fks = $this->tableObj->getForignKeys();
Expand Down