1: <?php
2:
3: namespace zsql;
4:
5: /**
6: * Insert query generator
7: */
8: class Insert extends Query
9: {
10: /**
11: * Set delayed clause
12: *
13: * @var boolean
14: */
15: protected $delayed;
16:
17: /**
18: * Set ignore clause
19: *
20: * @var boolean
21: */
22: protected $ignore;
23:
24: /**
25: * Set on duplicate key update clause
26: *
27: * @var array
28: */
29: protected $onDuplicateKeyUpdate;
30:
31: /**
32: * Use replace instead of insert
33: *
34: * @var boolean
35: */
36: protected $replace;
37:
38: /**
39: * Values
40: *
41: * @var array
42: */
43: protected $values;
44:
45: /**
46: * Assemble parts
47: *
48: * @return void
49: */
50: protected function assemble()
51: {
52: $this->push($this->replace ?
53: 'REPLACE' :
54: 'INSERT')
55: ->pushIgnoreDelayed()
56: ->push('INTO')
57: ->pushTable()
58: ->push('SET')
59: ->pushValues()
60: ->pushOnDuplicateKeyUpdate();
61: }
62:
63: /**
64: * Set delayed clause
65: *
66: * @param boolean $delayed
67: * @return \zsql\Insert
68: */
69: public function delayed($delayed = true)
70: {
71: $this->delayed = (bool) $delayed;
72: return $this;
73: }
74:
75: /**
76: * Set ignore clause
77: *
78: * @param boolean $ignore
79: * @return \zsql\Insert
80: */
81: public function ignore($ignore = true)
82: {
83: $this->ignore = (bool) $ignore;
84: return $this;
85: }
86:
87: /**
88: * Alias for {@link Insert::values()}
89: *
90: * @param array $values
91: * @return \zsql\Insert
92: */
93: public function insert($values)
94: {
95: $this->values($values);
96: return $this;
97: }
98:
99: /**
100: * Alias for {@link Query::table()}
101: *
102: * @param string $table
103: * @return \zsql\Insert
104: */
105: public function into($table)
106: {
107: $this->table($table);
108: return $this;
109: }
110:
111: /**
112: * Set on duplicate key update clause
113: *
114: * @param array $values
115: */
116: public function onDuplicateKeyUpdate($key, $value = null)
117: {
118: if( func_num_args() == 2 ) {
119: $this->onDuplicateKeyUpdate[$key] = $value;
120: } else if( $key instanceof Expression ) {
121: $this->onDuplicateKeyUpdate[] = $key;
122: } else if( is_array($key) ) {
123: $this->onDuplicateKeyUpdate = $key;
124: }
125: return $this;
126: }
127:
128: /**
129: * Push ignore or delayed onto parts
130: *
131: * @return \zsql\Insert
132: */
133: protected function pushIgnoreDelayed()
134: {
135: if( $this->delayed ) {
136: $this->parts[] = 'DELAYED';
137: }
138: if( $this->ignore && !$this->replace ) {
139: $this->parts[] = 'IGNORE';
140: }
141: return $this;
142: }
143:
144: /**
145: * Push on duplicate key update clause
146: *
147: * @return \zsql\Insert
148: */
149: protected function pushOnDuplicateKeyUpdate()
150: {
151: if( $this->onDuplicateKeyUpdate ) {
152: $this->parts[] = 'ON DUPLICATE KEY UPDATE';
153: $tmp = $this->values;
154: $this->values = $this->onDuplicateKeyUpdate;
155: $this->pushValues();
156: $this->values = $tmp;
157: }
158: return $this;
159: }
160:
161: /**
162: * Use replace instead of insert
163: *
164: * @param boolean $replace
165: * @return \zsql\Insert
166: */
167: public function replace($replace = true)
168: {
169: $this->replace = (bool) $replace;
170: return $this;
171: }
172:
173: /**
174: * Alias for {@link Insert::value()} or {@link Insert::values()}
175: *
176: * @param mixed $key
177: * @param mixed $value
178: * @return \zsql\Insert
179: */
180: public function set($key, $value = null)
181: {
182: if( is_array($key) ) {
183: $this->values($key);
184: } else {
185: $this->value($key, $value);
186: }
187: return $this;
188: }
189:
190: /**
191: * Set a value
192: *
193: * @param mixed $key
194: * @param mixed $value
195: * @return \zsql\Insert
196: */
197: public function value($key, $value = null)
198: {
199: if( null === $value && $key instanceof Expression ) {
200: $this->values[] = $key;
201: } else {
202: $this->values[$key] = $value;
203: }
204: return $this;
205: }
206:
207: /**
208: * Set values
209: *
210: * @param array $values
211: * @return \zsql\Insert
212: */
213: public function values(array $values)
214: {
215: $this->values = $values;
216: return $this;
217: }
218: }
219: