1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208:
<?php
namespace MvcCore\Ext\Models\Db\Providers\Connections;
class PgSql
extends \MvcCore\Ext\Models\Db\Connection
implements \MvcCore\Ext\Models\Db\Model\IConstants,
\MvcCore\Ext\Models\Db\Models\PgSqls\IConstants {
protected $transReadWriteSupport = NULL;
protected $transAllIsolationsSupport = NULL;
protected $transDeferrableSupport = NULL;
public function QuoteName ($identifierName) {
return '"'.$identifierName.'"';
}
public function BeginTransaction ($flags = 0, $name = NULL) {
if ($flags === 0) $flags = self::TRANS_READ_WRITE;
$readWrite = NULL;
if (($flags & self::TRANS_READ_WRITE) > 0) {
$readWrite = TRUE;
} else if (($flags & self::TRANS_READ_ONLY) > 0) {
$readWrite = FALSE;
}
if ($this->inTransaction) {
$cfg = $this->GetConfig();
unset($cfg['password']);
$toolClass = \MvcCore\Application::GetInstance()->GetToolClass();
throw new \RuntimeException(
'Connection has opened transaction already ('.($toolClass::EncodeJson($cfg)).').'
);
}
$sqlItems = [];
$isolationLevel = '';
$readWrite = '';
$deferrable = '';
if ($this->transReadWriteSupport) {
if ($readWrite === TRUE) {
$readWrite = ' READ WRITE';
} else if ($readWrite === FALSE) {
$readWrite = ' READ ONLY';
}
}
if (
$this->transAllIsolationsSupport &&
($flags & self::TRANS_ISOLATION_REPEATABLE_READ) > 0
) {
$isolationLevel = ' ISOLATION LEVEL REPEATABLE READ';
} else if (
($flags & self::TRANS_ISOLATION_READ_COMMITTED) > 0
) {
$isolationLevel = ' ISOLATION LEVEL READ COMMITTED';
} else if (
$this->transAllIsolationsSupport &&
($flags & self::TRANS_ISOLATION_READ_UNCOMMITTED) > 0
) {
$isolationLevel = ' ISOLATION LEVEL READ UNCOMMITTED';
} else if (
($flags & self::TRANS_ISOLATION_SERIALIZABLE) > 0
) {
$isolationLevel = ' ISOLATION LEVEL SERIALIZABLE';
}
if ($this->transDeferrableSupport) {
if (($flags & self::TRANS_DEFERRABLE) > 0) {
$deferrable = ' DEFERRABLE';
} else if (($flags & self::TRANS_NOT_DEFERRABLE) > 0) {
$deferrable = ' NOT DEFERRABLE';
}
}
if ($name !== NULL) {
$toolClass = \MvcCore\Application::GetInstance()->GetToolClass();
$this->transactionName = $toolClass::GetUnderscoredFromPascalCase($name);
$sqlItems[] = "/* trans_start:{$this->transactionName} */";
}
$sqlItems[] = "START TRANSACTION{$isolationLevel}{$readWrite}{$deferrable};";
$this->provider->exec(implode("\n", $sqlItems));
$this->inTransaction = TRUE;
return TRUE;
}
public function Commit ($flags = 0) {
if (!$this->inTransaction) return FALSE;
$sqlItems = [];
if ($this->transactionName !== NULL)
$sqlItems[] = "/* trans_commit:{$this->transactionName} */";
$sqlItems[] = "COMMIT;";
$this->provider->exec(implode("\n", $sqlItems));
$this->inTransaction = FALSE;
$this->transactionName = NULL;
return TRUE;
}
public function RollBack ($flags = NULL) {
if (!$this->inTransaction) return FALSE;
$sqlItems = [];
if ($this->transactionName !== NULL)
$sqlItems[] = "/* trans_rollback:{$this->transactionName} */";
$sqlItems[] = "ROLLBACK;";
$this->provider->exec(implode("\n", $sqlItems));
$this->inTransaction = FALSE;
$this->transactionName = NULL;
return TRUE;
}
protected function isConnectionLost (\Throwable $e) {
return FALSE;
}
protected function setUpConnectionSpecifics () {
parent::setUpConnectionSpecifics();
$this->transReadWriteSupport = (
version_compare($this->version, '7.4', '>=')
);
$this->transAllIsolationsSupport = (
version_compare($this->version, '8.0', '>=')
);
$this->transDeferrableSupport = (
version_compare($this->version, '9.1', '>=')
);
}
}