1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette\Database\Drivers;
9:
10: use Nette;
11:
12:
13: /**
14: * Supplemental Oracle database driver.
15: *
16: * @author David Grudl
17: */
18: class OciDriver extends Nette\Object implements Nette\Database\ISupplementalDriver
19: {
20: /** @var Nette\Database\Connection */
21: private $connection;
22:
23: /** @var string Datetime format */
24: private $fmtDateTime;
25:
26:
27: public function __construct(Nette\Database\Connection $connection, array $options)
28: {
29: $this->connection = $connection;
30: $this->fmtDateTime = isset($options['formatDateTime']) ? $options['formatDateTime'] : 'U';
31: }
32:
33:
34: /********************* SQL ****************d*g**/
35:
36:
37: /**
38: * Delimites identifier for use in a SQL statement.
39: */
40: public function delimite($name)
41: {
42: // @see http://download.oracle.com/docs/cd/B10500_01/server.920/a96540/sql_elements9a.htm
43: return '"' . str_replace('"', '""', $name) . '"';
44: }
45:
46:
47: /**
48: * Formats boolean for use in a SQL statement.
49: */
50: public function formatBool($value)
51: {
52: return $value ? '1' : '0';
53: }
54:
55:
56: /**
57: * Formats date-time for use in a SQL statement.
58: */
59: public function formatDateTime(/*\DateTimeInterface*/ $value)
60: {
61: return $value->format($this->fmtDateTime);
62: }
63:
64:
65: /**
66: * Encodes string for use in a LIKE statement.
67: */
68: public function formatLike($value, $pos)
69: {
70: throw new Nette\NotImplementedException;
71: }
72:
73:
74: /**
75: * Injects LIMIT/OFFSET to the SQL query.
76: */
77: public function applyLimit(& $sql, $limit, $offset)
78: {
79: if ($offset > 0) {
80: // see http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
81: $sql = 'SELECT * FROM (SELECT t.*, ROWNUM AS "__rnum" FROM (' . $sql . ') t '
82: . ($limit >= 0 ? 'WHERE ROWNUM <= ' . ((int) $offset + (int) $limit) : '')
83: . ') WHERE "__rnum" > '. (int) $offset;
84:
85: } elseif ($limit >= 0) {
86: $sql = 'SELECT * FROM (' . $sql . ') WHERE ROWNUM <= ' . (int) $limit;
87: }
88: }
89:
90:
91: /**
92: * Normalizes result row.
93: */
94: public function normalizeRow($row)
95: {
96: return $row;
97: }
98:
99:
100: /********************* reflection ****************d*g**/
101:
102:
103: /**
104: * Returns list of tables.
105: */
106: public function getTables()
107: {
108: $tables = array();
109: foreach ($this->connection->query('SELECT * FROM cat') as $row) {
110: if ($row[1] === 'TABLE' || $row[1] === 'VIEW') {
111: $tables[] = array(
112: 'name' => $row[0],
113: 'view' => $row[1] === 'VIEW',
114: );
115: }
116: }
117: return $tables;
118: }
119:
120:
121: /**
122: * Returns metadata for all columns in a table.
123: */
124: public function getColumns($table)
125: {
126: throw new Nette\NotImplementedException;
127: }
128:
129:
130: /**
131: * Returns metadata for all indexes in a table.
132: */
133: public function getIndexes($table)
134: {
135: throw new Nette\NotImplementedException;
136: }
137:
138:
139: /**
140: * Returns metadata for all foreign keys in a table.
141: */
142: public function getForeignKeys($table)
143: {
144: throw new Nette\NotImplementedException;
145: }
146:
147:
148: /**
149: * Returns associative array of detected types (IReflection::FIELD_*) in result set.
150: */
151: public function getColumnTypes(\PDOStatement $statement)
152: {
153: return Nette\Database\Helpers::detectTypes($statement);
154: }
155:
156:
157: /**
158: * @param string
159: * @return bool
160: */
161: public function isSupported($item)
162: {
163: return $item === self::SUPPORT_SEQUENCE || $item === self::SUPPORT_SUBSELECT;
164: }
165:
166: }
167: