* @package Mammut\DB\Adapter\MSSQL */ abstract class MSSQLBase extends \Mammut\DB\DB { /** * * @var resource */ protected $dbi = false; public function getDatabase() { $row = $this->getRow('SELECT db_name()'); if(is_array($row)) return $row[0]; else return false; } public function isTransactionSupported() { return true; } public function prepareStatement($query, $limit = -1, $skip = 0) { $query = $this->checkQuery($query); $stmt = new Statement($this, $query, $limit, $skip); return $stmt; } public function setAutocommit($doAutoCommit) { $sql = 'SET IMPLICIT_TRANSACTIONS '.($doAutoCommit ? 'OFF' : 'ON'); $this->query($sql); } public function getInsertId() { return $this->getObject("SELECT @@IDENTITY AS 'identity'")->identity; } public function table($table) { if($table instanceof \Mammut\DB\Model\TableInfo) $table = $table->getName(); if (!is_string($table)) throw new \InvalidArgumentException('empty table name or invalid type'); if(array_search($table, $this->tableList()) === false) throw new DBException('table "' . $table . '" dosen\'t exist'); return new Table($this, $table); } public function getTableInfo($tablename) { return MSSQLUtil::getTableInfo($this, $tablename); } public function dateCol2uts($date) { if(preg_match('#([0-9]{1,4})[./-]([0-9]{1,2})[./-]([0-9]{1,2})#', $date, $match)) { $t = mktime(0, 0, 0, $match[2], $match[3], $match[1]); return $t; } else return false; } public function uts2dateCol($uts) { return date('Y-m-d', $uts); } public function datetimeCol2uts($datetime) { if(preg_match('#([0-9]{1,4})[.-/]([0-9]{1,4})[.-/]([0-9]{1,4})\s+([0-9]{1,2})[.:]([0-9]{1,2})[.:]([0-9]{1,2})#', $datetime, $match)) { $t = mktime($match[4], $match[5], $match[6], $match[2], $match[3], $match[1]); return $t; } else return false; } public function uts2datetimeCol($uts) { return gmdate('Y-m-d\TH:i:s', $uts); } public function escapeValue($value, $addQuotes = true) { if(is_null($value)) return 'NULL'; if(is_bool($value)) return $value ? '1' : '0'; if(is_int($value) || is_long($value)) return (int) $value; if($value instanceof SQLFunction) return $this->getDialect()->getFunctionSQL($value); $q = $addQuotes ? '\'' : ''; if($value instanceof \Mammut\IO\File) { if(!$value->isDir()) { $data = file_get_contents($value->getPath()); $data = '0x' . bin2hex($data); return $data; } else throw new \InvalidArgumentException('could not add a directory to a table', 503); } if($value instanceof \DateTime) return $q . $value->format('Y-m-d\TH:i:s') . $q; return $q . $this->escapeString($value) . $q; // ansi syntax } }