Skip to content
Permalink
Browse files

Corrections to the schema.table

  • Loading branch information
niden committed Nov 16, 2019
1 parent 84e1bd5 commit 997947bcb84448cc0173b4480193f88456ad33ad
Showing with 114 additions and 92 deletions.
  1. +29 −16 phalcon/Db/Adapter/AbstractAdapter.zep
  2. +85 −76 phalcon/Mvc/Model.zep
@@ -747,8 +747,9 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
*/
public function insert(string table, array! values, var fields = null, var dataTypes = null) -> bool
{
var placeholders, insertValues, bindDataTypes, bindType, position,
value, escapedTable, joinedValues, escapedFields, field, insertSql;
var bindDataTypes, bindType, escapedTable, escapedFields, field,
insertSql, insertValues, joinedValues, placeholders, position,
tableName, value;

/**
* A valid array with more than one element is required
@@ -759,10 +760,9 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
);
}

let placeholders = [],
insertValues = [];

let bindDataTypes = [];
let placeholders = [],
insertValues = [],
bindDataTypes = [];

/**
* Objects are casted using __toString, null values are converted to
@@ -795,7 +795,13 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
}
}

let escapedTable = this->escapeIdentifier(table);
if strpos(table, ".") > 0 {
let tableName = explode(".", table);
} else {
let tableName = table;
}

let escapedTable = this->escapeIdentifier(tableName);

/**
* Build the final SQL INSERT statement
@@ -1118,14 +1124,13 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
*/
public function update(string table, var fields, var values, var whereCondition = null, var dataTypes = null) -> bool
{
var placeholders, updateValues, position, value, field, bindDataTypes,
escapedField, bindType, escapedTable, setClause, updateSql,
conditions, whereBind, whereTypes;

let placeholders = [],
updateValues = [];
var bindDataTypes, bindType, conditions, escapedField, escapedTable,
field, placeholders, position, setClause, tableName, updateSql,
updateValues, value, whereBind, whereTypes;

let bindDataTypes = [];
let placeholders = [],
updateValues = [],
bindDataTypes = [];

/**
* Objects are casted using __toString, null values are converted to
@@ -1167,9 +1172,17 @@ abstract class AbstractAdapter implements AdapterInterface, EventsAwareInterface
}
}

let escapedTable = this->escapeIdentifier(table);
/**
* Check if we got table and schema and escape it accordingly
*/
if strpos(table, ".") > 0 {
let tableName = explode(".", table);
} else {
let tableName = table;
}

let setClause = join(", ", placeholders);
let escapedTable = this->escapeIdentifier(tableName),
setClause = join(", ", placeholders);

if whereCondition !== null {
let updateSql = "UPDATE " . escapedTable . " SET " . setClause . " WHERE ";
@@ -3436,26 +3436,23 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
protected function _doLowInsert(<MetaDataInterface> metaData, <AdapterInterface> connection,
table, identityField) -> bool
{
var bindSkip, fields, values, bindTypes, attributes, bindDataTypes,
automaticAttributes, field, columnMap, value, attributeField,
success, bindType, defaultValue, sequenceName, defaultValues,
unsetDefaultValues, source, schema, snapshot, lastInsertedId,
manager;
var attributeField, attributes, automaticAttributes, bindDataTypes,
bindSkip, bindType, bindTypes, columnMap, defaultValue, defaultValues,
field, fields, lastInsertedId, manager, sequenceName, schema,
snapshot, source, success, unsetDefaultValues, value, values;
bool useExplicitIdentity;

let bindSkip = Column::BIND_SKIP;
let manager = <ManagerInterface> this->modelsManager;

let fields = [],
values = [],
snapshot = [],
bindTypes = [],
unsetDefaultValues = [];

let attributes = metaData->getAttributes(this),
bindDataTypes = metaData->getBindTypes(this),
let bindSkip = Column::BIND_SKIP,
manager = <ManagerInterface> this->modelsManager,
fields = [],
values = [],
snapshot = [],
bindTypes = [],
unsetDefaultValues = [],
attributes = metaData->getAttributes(this),
bindDataTypes = metaData->getBindTypes(this),
automaticAttributes = metaData->getAutomaticCreateAttributes(this),
defaultValues = metaData->getDefaultValues(this);
defaultValues = metaData->getDefaultValues(this);

if globals_get("orm.column_renaming") {
let columnMap = metaData->getColumnMap(this);
@@ -3493,7 +3490,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
if value === null && isset defaultValues[field] {
let value = connection->getDefaultValue();

let snapshot[attributeField] = defaultValues[field],
let snapshot[attributeField] = defaultValues[field],
unsetDefaultValues[attributeField] = defaultValues[field];
} else {
let snapshot[attributeField] = value;
@@ -3508,21 +3505,21 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
);
}

let fields[] = field,
values[] = value,
let fields[] = field,
values[] = value,
bindTypes[] = bindType;
} else {
if isset defaultValues[field] {
let values[] = connection->getDefaultValue();

let snapshot[attributeField] = defaultValues[field],
let snapshot[attributeField] = defaultValues[field],
unsetDefaultValues[attributeField] = defaultValues[field];
} else {
let values[] = value;
let snapshot[attributeField] = value;
let values[] = value,
snapshot[attributeField] = value;
}

let fields[] = field,
let fields[] = field,
bindTypes[] = bindSkip;
}
}
@@ -3584,17 +3581,24 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
);
}

let values[] = value,
let values[] = value,
bindTypes[] = bindType;
}
} else {
if useExplicitIdentity {
let values[] = defaultValue,
let values[] = defaultValue,
bindTypes[] = bindSkip;
}
}
}

/**
* The insert will escape the table name
*/
if typeof table === "array" {
let table = table[0] . "." . table[1];
}

/**
* The low level insert is performed
*/
@@ -3633,8 +3637,8 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
let lastInsertedId = intval(lastInsertedId, 10);
}

let this->{attributeField} = lastInsertedId;
let snapshot[attributeField] = lastInsertedId;
let this->{attributeField} = lastInsertedId,
snapshot[attributeField] = lastInsertedId;

/**
* Since the primary key was modified, we delete the uniqueParams
@@ -3668,35 +3672,34 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
*/
protected function _doLowUpdate(<MetaDataInterface> metaData, <AdapterInterface> connection, var table) -> bool
{
var bindSkip, fields, values, dataType, dataTypes, bindTypes, manager,
bindDataTypes, field, automaticAttributes, snapshotValue, uniqueKey,
uniqueParams, uniqueTypes, snapshot, nonPrimary, columnMap,
attributeField, value, primaryKeys, bindType, newSnapshot, success;
bool useDynamicUpdate, changed;

let bindSkip = Column::BIND_SKIP,
fields = [],
values = [],
bindTypes = [],
var automaticAttributes, attributeField, bindSkip, bindDataTypes,
bindType, bindTypes, columnMap, dataType, dataTypes, field, fields,
manager, nonPrimary, newSnapshot, success, primaryKeys, snapshot,
snapshotValue, uniqueKey, uniqueParams, uniqueTypes, value, values;
bool changed, useDynamicUpdate;

let bindSkip = Column::BIND_SKIP,
fields = [],
values = [],
bindTypes = [],
newSnapshot = [],
manager = <ManagerInterface> this->modelsManager;
manager = <ManagerInterface> this->modelsManager;

/**
* Check if the model must use dynamic update
*/
let useDynamicUpdate = (bool) manager->isUsingDynamicUpdate(this);

let snapshot = this->snapshot;
let useDynamicUpdate = (bool) manager->isUsingDynamicUpdate(this),
snapshot = this->snapshot;

if useDynamicUpdate {
if typeof snapshot != "array" {
let useDynamicUpdate = false;
}
}

let dataTypes = metaData->getDataTypes(this),
bindDataTypes = metaData->getBindTypes(this),
nonPrimary = metaData->getNonPrimaryKeyAttributes(this),
let dataTypes = metaData->getDataTypes(this),
bindDataTypes = metaData->getBindTypes(this),
nonPrimary = metaData->getNonPrimaryKeyAttributes(this),
automaticAttributes = metaData->getAutomaticUpdateAttributes(this);

if globals_get("orm.column_renaming") {
@@ -3810,8 +3813,8 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
* Only changed values are added to the SQL Update
*/
if changed {
let fields[] = field,
values[] = value,
let fields[] = field,
values[] = value,
bindTypes[] = bindType;
}
}
@@ -3820,8 +3823,8 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
} else {
let newSnapshot[attributeField] = null;

let fields[] = field,
values[] = null,
let fields[] = field,
values[] = null,
bindTypes[] = bindSkip;
}
}
@@ -3838,9 +3841,9 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
return true;
}

let uniqueKey = this->uniqueKey,
let uniqueKey = this->uniqueKey,
uniqueParams = this->uniqueParams,
uniqueTypes = this->uniqueTypes;
uniqueTypes = this->uniqueTypes;

/**
* When unique params is null we need to rebuild the bind params
@@ -3883,21 +3886,28 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
}
}

/**
* The insert will escape the table name
*/
if typeof table === "array" {
let table = table[0] . "." . table[1];
}

/**
* We build the conditions as an array
* Perform the low level update
*/
let success = connection->update(
table,
fields,
values,
[
"conditions" : uniqueKey,
"bind" : uniqueParams,
"bindTypes" : uniqueTypes
],
bindTypes
);
table,
fields,
values,
[
"conditions" : uniqueKey,
"bind" : uniqueParams,
"bindTypes" : uniqueTypes
],
bindTypes
);

if success && manager->isKeepingSnapshots(this) && globals_get("orm.update_snapshot_on_save") {
if typeof snapshot == "array" {
@@ -3918,23 +3928,22 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
protected function _exists(<MetaDataInterface> metaData, <AdapterInterface> connection) -> bool
{
int numberEmpty, numberPrimary;
var uniqueParams, uniqueTypes, uniqueKey, columnMap, primaryKeys,
wherePk, field, attributeField, value, bindDataTypes, joinWhere,
num, type, schema, source, table;
var attributeField, bindDataTypes, columnMap, field, joinWhere, num,
primaryKeys, schema, source, table, type, uniqueKey, uniqueParams,
uniqueTypes, value, wherePk;

let uniqueParams = null,
uniqueTypes = null;
uniqueTypes = null;

/**
* Builds a unique primary key condition
*/
let uniqueKey = this->uniqueKey;

if uniqueKey === null {
let primaryKeys = metaData->getPrimaryKeyAttributes(this),
bindDataTypes = metaData->getBindTypes(this);

let numberPrimary = count(primaryKeys);
let primaryKeys = metaData->getPrimaryKeyAttributes(this),
bindDataTypes = metaData->getBindTypes(this),
numberPrimary = count(primaryKeys);

if !numberPrimary {
return false;
@@ -3949,10 +3958,10 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
let columnMap = null;
}

let numberEmpty = 0,
wherePk = [],
let numberEmpty = 0,
wherePk = [],
uniqueParams = [],
uniqueTypes = [];
uniqueTypes = [];

/**
* We need to create a primary key based on the current data
@@ -3996,7 +4005,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
}

let uniqueTypes[] = type,
wherePk[] = connection->escapeIdentifier(field) . " = ?";
wherePk[] = connection->escapeIdentifier(field) . " = ?";
}

/**
@@ -4013,10 +4022,10 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
* The unique key is composed of 3 parts uniqueKey, uniqueParams,
* uniqueTypes
*/
let this->uniqueKey = joinWhere,
let this->uniqueKey = joinWhere,
this->uniqueParams = uniqueParams,
this->uniqueTypes = uniqueTypes,
uniqueKey = joinWhere;
this->uniqueTypes = uniqueTypes,
uniqueKey = joinWhere;
}

/**

0 comments on commit 997947b

Please sign in to comment.
You can’t perform that action at this time.