1 | <?php |
||
18 | class QueryBuilder extends \yii\db\QueryBuilder |
||
19 | { |
||
20 | /** |
||
21 | * Defines a UNIQUE index for [[createIndex()]]. |
||
22 | * @since 2.0.6 |
||
23 | */ |
||
24 | const INDEX_UNIQUE = 'unique'; |
||
25 | /** |
||
26 | * Defines a B-tree index for [[createIndex()]]. |
||
27 | * @since 2.0.6 |
||
28 | */ |
||
29 | const INDEX_B_TREE = 'btree'; |
||
30 | /** |
||
31 | * Defines a hash index for [[createIndex()]]. |
||
32 | * @since 2.0.6 |
||
33 | */ |
||
34 | const INDEX_HASH = 'hash'; |
||
35 | /** |
||
36 | * Defines a GiST index for [[createIndex()]]. |
||
37 | * @since 2.0.6 |
||
38 | */ |
||
39 | const INDEX_GIST = 'gist'; |
||
40 | /** |
||
41 | * Defines a GIN index for [[createIndex()]]. |
||
42 | * @since 2.0.6 |
||
43 | */ |
||
44 | const INDEX_GIN = 'gin'; |
||
45 | |||
46 | /** |
||
47 | * @var array mapping from abstract column types (keys) to physical column types (values). |
||
48 | */ |
||
49 | public $typeMap = [ |
||
50 | Schema::TYPE_PK => 'serial NOT NULL PRIMARY KEY', |
||
51 | Schema::TYPE_BIGPK => 'bigserial NOT NULL PRIMARY KEY', |
||
52 | Schema::TYPE_STRING => 'varchar(255)', |
||
53 | Schema::TYPE_TEXT => 'text', |
||
54 | Schema::TYPE_SMALLINT => 'smallint', |
||
55 | Schema::TYPE_INTEGER => 'integer', |
||
56 | Schema::TYPE_BIGINT => 'bigint', |
||
57 | Schema::TYPE_FLOAT => 'double precision', |
||
58 | Schema::TYPE_DOUBLE => 'double precision', |
||
59 | Schema::TYPE_DECIMAL => 'numeric(10,0)', |
||
60 | Schema::TYPE_DATETIME => 'timestamp(0)', |
||
61 | Schema::TYPE_TIMESTAMP => 'timestamp(0)', |
||
62 | Schema::TYPE_TIME => 'time(0)', |
||
63 | Schema::TYPE_DATE => 'date', |
||
64 | Schema::TYPE_BINARY => 'bytea', |
||
65 | Schema::TYPE_BOOLEAN => 'boolean', |
||
66 | Schema::TYPE_MONEY => 'numeric(19,4)', |
||
67 | ]; |
||
68 | |||
69 | /** |
||
70 | * @var array map of query condition to builder methods. |
||
71 | * These methods are used by [[buildCondition]] to build SQL conditions from array syntax. |
||
72 | */ |
||
73 | protected $conditionBuilders = [ |
||
74 | 'NOT' => 'buildNotCondition', |
||
75 | 'AND' => 'buildAndCondition', |
||
76 | 'OR' => 'buildAndCondition', |
||
77 | 'BETWEEN' => 'buildBetweenCondition', |
||
78 | 'NOT BETWEEN' => 'buildBetweenCondition', |
||
79 | 'IN' => 'buildInCondition', |
||
80 | 'NOT IN' => 'buildInCondition', |
||
81 | 'LIKE' => 'buildLikeCondition', |
||
82 | 'ILIKE' => 'buildLikeCondition', |
||
83 | 'NOT LIKE' => 'buildLikeCondition', |
||
84 | 'NOT ILIKE' => 'buildLikeCondition', |
||
85 | 'OR LIKE' => 'buildLikeCondition', |
||
86 | 'OR ILIKE' => 'buildLikeCondition', |
||
87 | 'OR NOT LIKE' => 'buildLikeCondition', |
||
88 | 'OR NOT ILIKE' => 'buildLikeCondition', |
||
89 | 'EXISTS' => 'buildExistsCondition', |
||
90 | 'NOT EXISTS' => 'buildExistsCondition', |
||
91 | ]; |
||
92 | |||
93 | |||
94 | /** |
||
95 | * Builds a SQL statement for creating a new index. |
||
96 | * @param string $name the name of the index. The name will be properly quoted by the method. |
||
97 | * @param string $table the table that the new index will be created for. The table name will be properly quoted by the method. |
||
98 | * @param string|array $columns the column(s) that should be included in the index. If there are multiple columns, |
||
99 | * separate them with commas or use an array to represent them. Each column name will be properly quoted |
||
100 | * by the method, unless a parenthesis is found in the name. |
||
101 | * @param boolean|string $unique whether to make this a UNIQUE index constraint. You can pass `true` or [[INDEX_UNIQUE]] to create |
||
102 | * a unique index, `false` to make a non-unique index using the default index type, or one of the following constants to specify |
||
103 | * the index method to use: [[INDEX_B_TREE]], [[INDEX_HASH]], [[INDEX_GIST]], [[INDEX_GIN]]. |
||
104 | * @return string the SQL statement for creating a new index. |
||
105 | * @see http://www.postgresql.org/docs/8.2/static/sql-createindex.html |
||
106 | */ |
||
107 | public function createIndex($name, $table, $columns, $unique = false) |
||
108 | { |
||
109 | if ($unique === self::INDEX_UNIQUE || $unique === true) { |
||
110 | $index = false; |
||
111 | $unique = true; |
||
112 | } else { |
||
113 | $index = $unique; |
||
114 | $unique = false; |
||
115 | } |
||
116 | |||
117 | return ($unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ') . |
||
118 | $this->db->quoteTableName($name) . ' ON ' . |
||
119 | $this->db->quoteTableName($table) . |
||
120 | ($index !== false ? " USING $index" : '') . |
||
121 | ' (' . $this->buildColumns($columns) . ')'; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Builds a SQL statement for dropping an index. |
||
126 | * @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
||
127 | * @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
||
128 | * @return string the SQL statement for dropping an index. |
||
129 | */ |
||
130 | public function dropIndex($name, $table) |
||
134 | |||
135 | /** |
||
136 | * Builds a SQL statement for renaming a DB table. |
||
137 | * @param string $oldName the table to be renamed. The name will be properly quoted by the method. |
||
138 | * @param string $newName the new table name. The name will be properly quoted by the method. |
||
139 | * @return string the SQL statement for renaming a DB table. |
||
140 | */ |
||
141 | 1 | public function renameTable($oldName, $newName) |
|
145 | |||
146 | /** |
||
147 | * Creates a SQL statement for resetting the sequence value of a table's primary key. |
||
148 | * The sequence will be reset such that the primary key of the next new row inserted |
||
149 | * will have the specified value or 1. |
||
150 | * @param string $tableName the name of the table whose primary key sequence will be reset |
||
151 | * @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||
152 | * the next new row's primary key will have a value 1. |
||
153 | * @return string the SQL statement for resetting sequence |
||
154 | * @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||
155 | */ |
||
156 | public function resetSequence($tableName, $value = null) |
||
177 | |||
178 | /** |
||
179 | * Builds a SQL statement for enabling or disabling integrity check. |
||
180 | * @param boolean $check whether to turn on or off the integrity check. |
||
181 | * @param string $schema the schema of the tables. |
||
182 | * @param string $table the table name. |
||
183 | * @return string the SQL statement for checking integrity |
||
184 | */ |
||
185 | public function checkIntegrity($check = true, $schema = '', $table = '') |
||
202 | |||
203 | /** |
||
204 | * Builds a SQL statement for changing the definition of a column. |
||
205 | * @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
||
206 | * @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
||
207 | * @param string $type the new column type. The [[getColumnType()]] method will be invoked to convert abstract |
||
208 | * column type (if any) into the physical one. Anything that is not recognized as abstract type will be kept |
||
209 | * in the generated SQL. For example, 'string' will be turned into 'varchar(255)', while 'string not null' |
||
210 | * will become 'varchar(255) not null'. You can also use PostgreSQL-specific syntax such as `SET NOT NULL`. |
||
211 | * @return string the SQL statement for changing the definition of a column. |
||
212 | */ |
||
213 | 2 | public function alterColumn($table, $column, $type) |
|
223 | |||
224 | /** |
||
225 | * @inheritdoc |
||
226 | */ |
||
227 | 5 | public function batchInsert($table, $columns, $rows) |
|
264 | } |
||
265 |