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

Added smallint support in Phalcon\Db #12523

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Added afterBinding event to `Phalcon\Dispatcher` and `Phalcon\Mvc\Micro`, added `Phalcon\Mvc\Micro::afterBinding`
- Added the ability to set custom Resultset class returned by find() [#12166](https://github.com/phalcon/cphalcon/issues/12166)
- Added the ability to clear appended and prepended title elements (Phalcon\Tag::appendTitle, Phalcon\Tag::prependTitle). Now you can use array to add multiple titles. For more details check [#12238](https://github.com/phalcon/cphalcon/issues/12238).
- Added SMALLINT support in `Phalcon\Db`

# [3.0.3](https://github.com/phalcon/cphalcon/releases/tag/v3.0.3) (201X-XX-XX)
- Fixed implementation of Iterator interface in a `Phalcon\Forms\Form` that could cause a run-time warning
Expand Down
11 changes: 9 additions & 2 deletions phalcon/db/adapter/pdo/mysql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,21 @@ class Mysql extends PdoAdapter
let definition["type"] = Column::TYPE_CHAR;
} elseif memstr(columnType, "bigint") {
/**
* Smallint/Bigint/Integers/Int are int
* Bigint are int
*/
let definition["type"] = Column::TYPE_BIGINTEGER,
definition["isNumeric"] = true,
definition["bindType"] = Column::BIND_PARAM_INT;
} elseif memstr(columnType, "smallint") {
/**
* Smallint are int
*/
let definition["type"] = Column::TYPE_SMALLINTEGER,
definition["isNumeric"] = true,
definition["bindType"] = Column::BIND_PARAM_INT;
} elseif memstr(columnType, "int") {
/**
* Smallint/Bigint/Integers/Int are int
* Integers/Int are int
*/
let definition["type"] = Column::TYPE_INTEGER,
definition["isNumeric"] = true,
Expand Down
7 changes: 7 additions & 0 deletions phalcon/db/adapter/pdo/postgresql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ class Postgresql extends PdoAdapter
let definition["type"] = Column::TYPE_BIGINTEGER,
definition["isNumeric"] = true,
definition["bindType"] = Column::BIND_PARAM_INT;
} elseif memstr(columnType, "smallint") {
/**
* Smallint
*/
let definition["type"] = Column::TYPE_SMALLINTEGER,
definition["isNumeric"] = true,
definition["bindType"] = Column::BIND_PARAM_INT;
} elseif memstr(columnType, "int") {
/**
* Int
Expand Down
9 changes: 8 additions & 1 deletion phalcon/db/adapter/pdo/sqlite.zep
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,16 @@ class Sqlite extends PdoAdapter
let definition["type"] = Column::TYPE_BIGINTEGER,
definition["isNumeric"] = true,
definition["bindType"] = Column::BIND_PARAM_INT;
} elseif memstr(columnType, "smallint") {
/**
* Smallint are int
*/
let definition["type"] = Column::TYPE_SMALLINTEGER,
definition["isNumeric"] = true,
definition["bindType"] = Column::BIND_PARAM_INT;
} elseif memstr(columnType, "int") || memstr(columnType, "INT") {
/**
* Smallint/Integers/Int are int
* Integers/Int are int
*/
let definition["type"] = Column::TYPE_INTEGER,
definition["isNumeric"] = true,
Expand Down
8 changes: 8 additions & 0 deletions phalcon/db/column.zep
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ class Column implements ColumnInterface
*/
const TYPE_TIMESTAMP = 17;

/**
* Small integer abstract type
*/
const TYPE_SMALLINTEGER = 18;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about just TYPE_SMALLINT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a type TYPE_BIGINTEGER. It would be very strange (though quite in the spirit of PHP 😆) to use the name TYPE_SMALLINT.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree


/**
* Bind Type Null
*/
Expand Down Expand Up @@ -337,6 +342,7 @@ class Column implements ColumnInterface
case self::TYPE_DECIMAL:
case self::TYPE_DOUBLE:
case self::TYPE_BIGINTEGER:
case self::TYPE_SMALLINTEGER:
let this->_scale = scale;
break;

Expand Down Expand Up @@ -377,6 +383,7 @@ class Column implements ColumnInterface

case self::TYPE_INTEGER:
case self::TYPE_BIGINTEGER:
case self::TYPE_SMALLINTEGER:
let this->_autoIncrement = true;
break;

Expand Down Expand Up @@ -528,6 +535,7 @@ class Column implements ColumnInterface
case self::TYPE_DECIMAL:
case self::TYPE_DOUBLE:
case self::TYPE_BIGINTEGER:
case self::TYPE_SMALLINTEGER:
let definition["scale"] = scale;
break;
}
Expand Down
24 changes: 20 additions & 4 deletions phalcon/db/dialect/mysql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ class Mysql extends Dialect
if empty columnSql {
let columnSql .= "INT";
}
let columnSql .= "(" . column->getSize() . ")";
let size = column->getSize();
if size {
let columnSql .= "(" . size . ")";
}
if column->isUnsigned() {
let columnSql .= " UNSIGNED";
}
Expand Down Expand Up @@ -159,9 +162,22 @@ class Mysql extends Dialect
if empty columnSql {
let columnSql .= "BIGINT";
}
let scale = column->getSize();
if scale {
let columnSql .= "(" . column->getSize() . ")";
let size = column->getSize();
if size {
let columnSql .= "(" . size . ")";
}
if column->isUnsigned() {
let columnSql .= " UNSIGNED";
}
break;

case Column::TYPE_SMALLINTEGER:
if empty columnSql {
let columnSql .= "SMALLINT";
}
let size = column->getSize();
if size {
let columnSql .= "(" . size . ")";
}
if column->isUnsigned() {
let columnSql .= " UNSIGNED";
Expand Down
11 changes: 11 additions & 0 deletions phalcon/db/dialect/postgresql.zep
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ class Postgresql extends Dialect
}
break;

case Column::TYPE_SMALLINTEGER:
if empty columnSql {
if column->isAutoIncrement() {
let columnSql .= "SMALLSERIAL";
} else {
let columnSql .= "SMALLINT";
}
}
break;

case Column::TYPE_JSON:
if empty columnSql {
let columnSql .= "JSON";
Expand Down Expand Up @@ -661,6 +671,7 @@ class Postgresql extends Dialect

if columnType === Column::TYPE_INTEGER ||
columnType === Column::TYPE_BIGINTEGER ||
columnType === Column::TYPE_SMALLINTEGER ||
columnType === Column::TYPE_DECIMAL ||
columnType === Column::TYPE_FLOAT ||
columnType === Column::TYPE_DOUBLE {
Expand Down
9 changes: 9 additions & 0 deletions phalcon/db/dialect/sqlite.zep
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ class Sqlite extends Dialect
}
break;

case Column::TYPE_SMALLINTEGER:
if empty columnSql {
let columnSql .= "SMALLINT";
}
if column->isUnsigned() {
let columnSql .= " UNSIGNED";
}
break;

case Column::TYPE_TINYBLOB:
if empty columnSql {
let columnSql .= "TINYBLOB";
Expand Down
3 changes: 3 additions & 0 deletions phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
switch attribute[1] {

case Column::TYPE_INTEGER:
case Column::TYPE_SMALLINTEGER:
let castValue = intval(value, 10);
break;

Expand All @@ -591,6 +592,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
switch attribute[1] {

case Column::TYPE_INTEGER:
case Column::TYPE_SMALLINTEGER:
case Column::TYPE_DOUBLE:
case Column::TYPE_DECIMAL:
case Column::TYPE_FLOAT:
Expand Down Expand Up @@ -2527,6 +2529,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
break;

case Column::TYPE_INTEGER:
case Column::TYPE_SMALLINTEGER:
let changed = (int) snapshotValue !== (int) value;
break;

Expand Down
2 changes: 2 additions & 0 deletions tests/_support/Helper/Dialect/PostgresqlDialect.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ protected function getColumnDefinition()
['column21', 'BIGSERIAL'],
['column22', 'BIGINT'],
['column23', 'SERIAL'],
['column24', 'SMALLINT'],
['column25', 'SMALLSERIAL'],
];
}

Expand Down
8 changes: 8 additions & 0 deletions tests/_support/Helper/DialectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ protected function getColumns()
'type' => Column::TYPE_INTEGER,
'autoIncrement' => true,
]),
'column24' => new Column('column24', [
'type' => Column::TYPE_SMALLINTEGER,
'autoIncrement' => false,
]),
'column25' => new Column('column25', [
'type' => Column::TYPE_SMALLINTEGER,
'autoIncrement' => true,
]),
];
}

Expand Down