38 public function __construct($parameters =
array())
41 $this->dbHelper = $this->dbConnection->getSqlHelper();
44 $entityName = trim((
string)($parameters[
'entityName'] ??
''));
45 $tableName = trim((
string)($parameters[
'tableName'] ??
''));
46 if ($entityName !==
'' && is_a($entityName,
Main\
ORM\
Data\DataManager::class,
true))
51 $this->tableName = $table::getTableName();
52 $this->tableMap = $table::getMap();
55 if (!empty($parameters[
'exactFields']) && is_array($parameters[
'exactFields']))
57 foreach($parameters[
'exactFields'] as $fld)
59 if(!isset($this->tableMap[$fld]))
61 throw new Main\SystemException(
'Field does not exist in ORM class, but present in "exactFields" parameter: '.$fld, 0, __FILE__, __LINE__);
65 $this->fldVector[$fld] =
true;
70 foreach($this->tableMap as $fld =>
$params)
73 $this->fldVector[$fld] =
true;
79 $this->tableName = $this->dbHelper->forSql($parameters[
'tableName']);
81 if (!empty($parameters[
'exactFields']) && is_array($parameters[
'exactFields']))
83 $this->tableMap = $parameters[
'exactFields'];
102 foreach($this->tableMap as $fld =>
$params)
105 $this->fldVector[$fld] =
true;
111 $autoIncrementFld = trim((
string)($parameters[
'parameters'][
'autoIncrementFld'] ??
''));
112 if ($autoIncrementFld !==
'')
114 $this->autoIncFld = $autoIncrementFld;
115 if (!isset($this->fldVector[$this->autoIncFld]))
120 'data_type' =>
'integer',
129 $this->mtu = (int)($parameters[
'parameters'][
'mtu'] ?? 0);
135 $this->mtu = min($this->mtu, (
int)Helper::getMaxTransferUnit());
137 $this->insertHead = Helper::getBatchInsertHead($this->tableName,
$map);
138 $this->insertTail = Helper::getBatchInsertTail();
141 isset($parameters[
'parameters'][
'CALLBACKS'][
'ON_BEFORE_FLUSH'])
142 && is_callable($parameters[
'parameters'][
'CALLBACKS'][
'ON_BEFORE_FLUSH'])
145 $this->callbacks[
'ON_BEFORE_FLUSH'] = $parameters[
'parameters'][
'CALLBACKS'][
'ON_BEFORE_FLUSH'];
157 $fld = $this->dbHelper->forSql($fld);
161 $res = $this->dbConnection->query($sql)->fetch();
162 $this->index = intval(
$res[
'VAL']);
182 if($this->autoIncFld ===
false)
185 return Helper::dropAutoIncrementRestrictions($this->tableName);
190 if($this->autoIncFld ===
false)
193 return Helper::restoreAutoIncrementRestrictions($this->tableName);
198 Helper::resetAutoIncrement($this->tableName, $this->
getIndex() + 1);
203 if(!is_array($row) || empty($row))
209 if($this->autoIncFld !==
false)
212 Helper::incrementSequenceForTable($this->tableName);
215 $sql = Helper::getBatchInsertValues($row, $this->tableName, $this->fldVector, $this->map);
222 $nextBuffer = (empty($this->
buffer) ? $this->insertHead : $this->
buffer.Helper::getBatchInsertSeparator()).$sql;
224 if(($this->mtu - (mb_strlen($nextBuffer) + 100)) < self::RED_LINE)
227 $this->
buffer = $this->insertHead.$sql;
230 $this->
buffer = $nextBuffer;
240 if(isset($this->callbacks[
'ON_BEFORE_FLUSH']))
241 call_user_func($this->callbacks[
'ON_BEFORE_FLUSH']);
243 $this->
buffer .=
' '.$this->insertTail;
245 if ($this->dbType ===
'pgsql' && $this->autoIncFld)
247 $this->
buffer .=
'; ALTER SEQUENCE ' . $this->tableName .
'_' . $this->autoIncFld .
'_seq RESTART WITH ' . $this->index + 1 .
';';
258 $this->bufferSize = 0;
static getConnection($name="")
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)