Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 410 | Rev 3186 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper\Common;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
use Laminas\Db\Adapter\Driver\ResultInterface;
9
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
10
use Laminas\Db\Sql\Sql;
11
use Laminas\Db\Sql\Select;
12
use Laminas\Db\Sql\Insert;
13
use Laminas\Db\Sql\Update;
14
use Laminas\Db\Sql\Delete;
15
 
16
class MapperCommon
17
{
18
    const SQL_DUPLICATE_RECORD                      = 1062;
19
    const SQL_CANNOT_ADD_OR_UPDATE_A_CHILD          = 1452;
20
    const SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW  = 1451;
21
 
22
 
23
 
24
    /**
25
     *
26
     * @var AdapterInterface
27
     */
28
    protected $adapter;
29
 
30
 
31
 
32
    /**
33
     *
34
     * @var Sql
35
     */
36
    protected $sql;
37
 
38
    /**
39
     *
40
     * @var integer
41
     */
42
    protected $affectedRows;
43
 
44
    /**
45
     *
46
     * @var integer
47
     */
48
    protected $lastInsertId;
49
 
50
    /**
51
     *
52
     * @var integer
53
     */
54
    protected $errno;
55
 
56
    /**
57
     *
58
     * @var String
59
     */
60
    protected $error;
61
 
62
 
63
 
64
    /**
65
     *
66
     * @var MapperCommon
67
     */
68
    private static $_instance;
69
 
70
    /**
71
     *
72
     * @param AdapterInterface $adapter
73
     */
74
    public function __construct($adapter)
75
    {
76
        $this->sql      = new Sql($adapter);
77
        $this->adapter  = $adapter;
78
    }
79
 
80
    /**
81
     *
82
     * @param AdapterInterface $adapter
83
     * @return MapperCommon
84
     */
85
    public static function getInstance($adapter)
86
    {
87
        if(self::$_instance == null) {
88
            self::$_instance = new MapperCommon($adapter);
89
        }
90
        return self::$_instance;
91
    }
92
 
93
    /**
94
     *
95
     * @return Sql
96
     */
97
    public function getSql()
98
    {
99
        return $this->sql;
100
    }
101
 
102
    /**
103
     *
104
     * @return integer
105
     */
106
    public function getAffectedRows()
107
    {
108
        return $this->affectedRows;
109
    }
110
 
111
    /**
112
     *
113
     * @return integer
114
     */
115
    public function getErrno()
116
    {
117
        return $this->errno;
118
    }
119
 
120
    /**
121
     *
122
     * @return string
123
     */
124
    public function getError()
125
    {
126
        return $this->error;
127
    }
128
 
129
    /**
130
     *
131
     * @return integer
132
     */
133
    public function getLastInsertId()
134
    {
135
        return $this->lastInsertId;
136
    }
137
 
138
    private function initRespose()
139
    {
140
        $this->errno = 0;
141
        $this->error = '';
142
        $this->affectedRows = 0;
143
        $this->lastInsertId = 0;
144
    }
145
 
146
    /**
147
     *
148
     * @param Insert $insert
149
     * @return boolean
150
     */
151
    protected function executeInsert($insert)
152
    {
153
        $this->initRespose();
154
        try
155
        {
156
 
157
            $stmt = $this->sql->prepareStatementForSqlObject($insert);
158
            $result = $stmt->execute();
159
            if ($result) {
160
                $this->lastInsertId = $result->getGeneratedValue();
161
                $this->affectedRows = $result->getAffectedRows();
162
                return true;
163
            }
164
        }
165
        catch(\Exception $e)
166
        {
167
            error_log($insert->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
168
            $this->processError($e);
169
        }
170
 
171
        return false;
172
    }
173
 
174
    /**
175
     *
176
     * @param Update $update
177
     * @return boolean
178
     */
179
    protected function executeUpdate($update)
180
    {
181
        $this->initRespose();
182
        try
183
        {
184
 
185
            $stmt = $this->sql->prepareStatementForSqlObject($update);
186
            $result = $stmt->execute();
187
 
188
            if($result) {
189
                $this->affectedRows = $result->getAffectedRows();
190
                return true;
191
            }
192
        }
193
        catch(\Exception $e)
194
        {
195
            error_log($update->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
196
            $this->processError($e);
197
        }
198
 
199
        return false;
200
    }
201
 
202
    /**
203
     *
204
     * @param Delete $delete
205
     * @return boolean
206
     */
207
    protected function executeDelete($delete)
208
    {
209
        $this->initRespose();
210
        try
211
        {
212
            $stmt = $this->sql->prepareStatementForSqlObject($delete);
213
            $result = $stmt->execute();
214
            if($result) {
215
                $this->affectedRows = $result->getAffectedRows();
216
                return true;
217
            }
218
        }
219
        catch(\Exception $e)
220
        {
221
            error_log($delete->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
222
            $this->processError($e);
223
        }
224
 
225
        return false;
226
    }
227
 
228
    /**
229
     *
230
     * @param Select $select
231
     * @param Object $prototype
232
     * @return array
233
     */
234
    protected function executeFetchAllObject($select, $prototype)
235
    {
236
 
237
        $this->initRespose();
238
        try {
239
            $stmt = $this->sql->prepareStatementForSqlObject($select);
240
            $result = $stmt->execute();
241
 
242
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
243
                $hydrator = new ObjectPropertyHydrator();
244
 
245
                $records = [];
246
                foreach($result as $row)
247
                {
248
                    $record = clone $prototype;
249
                    $hydrator->hydrate($row, $record);
250
 
251
                    array_push($records, $record);
252
                }
253
 
254
                return $records;
255
            }
256
        }
257
        catch(\Exception $e) {
258
            error_log($select->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
259
            $this->processError($e);
260
        }
261
 
262
        return [];
263
    }
264
 
265
    /**
266
     *
267
     * @param Select $select
268
     * @return array
269
     */
270
    protected function executeFetchAllArray($select)
271
    {
272
 
273
        $this->initRespose();
274
 
275
        try {
276
            $stmt = $this->sql->prepareStatementForSqlObject($select);
277
            $result = $stmt->execute();
278
 
279
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
280
 
281
                $records = [];
282
                foreach($result as $record)
283
                {
284
                    array_push($records, $record);
285
                }
286
                return $records;
287
            }
288
        }
289
        catch(\Exception $e) {
290
            error_log($select->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
291
            $this->processError($e);
292
        }
293
        return [];
294
    }
295
 
296
    /**
297
     *
298
     * @param Select $select
299
     * @param Object $prototype
300
     * @return void|Object
301
     */
302
    protected function executeFetchOneObject($select, $prototype)
303
    {
304
        $this->initRespose();
305
 
306
        try {
307
            $select->limit(1);
308
 
309
            $stmt = $this->sql->prepareStatementForSqlObject($select);
310
            $result = $stmt->execute();
311
 
312
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
313
 
314
                $hydrator = new ObjectPropertyHydrator();
315
                $hydrator->hydrate($result->current(), $prototype);
316
 
317
                return $prototype;
318
            }
319
        }
320
        catch(\Exception $e) {
321
            error_log($select->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
322
            $this->processError($e);
323
        }
324
 
325
        return;
326
    }
327
 
328
    /**
329
     *
330
     * @param Select $select
331
     * @return void|array
332
     */
333
    protected function executeFetchOneArray($select)
334
    {
335
        $this->initRespose();
336
 
337
        try {
338
            $select->limit(1);
339
 
340
            $stmt = $this->sql->prepareStatementForSqlObject($select);
341
            $result = $stmt->execute();
342
 
343
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
344
 
345
                return $result->current();
346
            }
347
        }
348
        catch(\Exception $e) {
349
            error_log($select->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
350
            $this->processError($e);
351
        }
352
 
353
        return;
354
    }
355
 
356
    /**
357
     *
358
     * @param string $sql
359
     * @param array $parameters
360
     * @return boolean
361
     */
362
    protected function executeInsertUsingSentenceWithParameters($sql, $parameters = [])
363
    {
364
        $this->initRespose();
365
 
366
        try {
367
 
368
            $stmt = $this->adapter->createStatement($sql);
369
            $stmt->prepare();
370
            $result = $stmt->execute($parameters);
371
 
372
            if ($result) {
373
                $this->lastInsertId = $result->getGeneratedValue();
374
                $this->affectedRows = $result->getAffectedRows();
375
                return true;
376
            }
377
        }
378
        catch(\Exception $e) {
379
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
380
            $this->processError($e);
381
        }
382
 
383
        return false;
384
    }
385
 
386
 
387
    /**
388
     *
389
     * @param string $sql
390
     * @param array $parameters
391
     * @return boolean
392
     */
393
    protected function executeSentenceWithParameters($sql, $parameters = [])
394
    {
395
        $this->initRespose();
396
        try
397
        {
398
 
399
            $stmt = $this->adapter->createStatement($sql);
400
            $stmt->prepare();
401
            $result = $stmt->execute($parameters);
402
 
403
 
404
            if($result) {
405
                $this->affectedRows = $result->getAffectedRows();
406
                return true;
407
            }
408
        }
409
        catch(\Exception $e) {
410
            $this->processError($e);
411
        }
412
 
413
        return false;
414
    }
415
 
416
    /**
417
     *
418
     * @param Object $prototype
419
     * @param string $sql
420
     * @param array $parameters
421
     * @return array
422
     */
423
    protected function executeFetchAllObjectUsingParameters($prototype, $sql, $parameters = [])
424
    {
425
        $this->initRespose();
426
        try {
427
 
428
            $stmt = $this->adapter->createStatement($sql);
429
            $stmt->prepare();
430
            $result = $stmt->execute($parameters);
431
 
432
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
433
 
434
                if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
435
                    $hydrator = new ObjectPropertyHydrator();
436
 
437
                    $records = [];
438
                    foreach($result as $row)
439
                    {
440
                        $record = clone $prototype;
441
                        $hydrator->hydrate($row, $record);
442
 
443
                        array_push($records, $record);
444
                    }
445
 
446
                    return $records;
447
                }
448
            }
449
        }
450
        catch(\Exception $e) {
451
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
452
            $this->processError($e);
453
        }
454
 
455
        return [];
456
    }
457
 
458
    /**
459
     *
460
     * @param string $sql
461
     * @param array $parameters
462
     * @return array
463
     */
464
   protected function executeFetchAllArrayUsingParameters($sql, $parameters = [] )
465
    {
466
        $this->initRespose();
467
 
468
        try {
469
            $stmt = $this->adapter->createStatement($sql);
470
            $stmt->prepare();
471
            $result = $stmt->execute($parameters);
472
 
473
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
474
 
475
                $records = [];
476
                foreach($result as $row)
477
                {
478
                    array_push($records, $row);
479
                }
480
 
481
                return $records;;
482
            }
483
        }
484
        catch(\Exception $e) {
485
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
486
            $this->processError($e);
487
        }
488
        return [];
489
    }
490
 
491
    /**
492
     *
493
     * @param Object $prototype
494
     * @param string $sql
495
     * @param array $parameters
496
     * @return void|Object
497
     */
498
    protected function executeFetchOneObjectUsingParameters($prototype, $sql, $parameters = [])
499
    {
500
        $this->initRespose();
501
 
502
        try {
503
            $stmt = $this->adapter->createStatement($sql);
504
            $stmt->prepare();
505
            $result = $stmt->execute($parameters);
506
 
507
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
508
 
509
                $hydrator = new ObjectPropertyHydrator();
510
                $hydrator->hydrate($result->current(), $prototype);
511
 
512
                return $prototype;
513
            }
514
        }
515
        catch(\Exception $e) {
516
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
517
            $this->processError($e);
518
        }
519
 
520
        return;
521
    }
522
 
523
    /**
524
     *
525
     * @param string $sql
526
     * @param array $parameters
527
     * @return void|array
528
     */
529
    protected function executeFetchOneArrayUsingParameters($sql, $parameters = [])
530
    {
531
        $this->initRespose();
532
 
533
        try {
534
            $stmt = $this->adapter->createStatement($sql);
535
            $stmt->prepare();
536
            $result = $stmt->execute($parameters);
537
 
538
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
539
 
540
                return $result->current();
541
            }
542
        }
543
        catch(\Exception $e) {
544
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
545
            $this->processError($e);
546
        }
547
 
548
        return;
549
    }
550
 
551
    private function processError($e)
552
    {
553
 
554
 
555
        $this->errno = $e->getCode();
556
 
557
        switch($this->errno)
558
        {
559
            case self::SQL_DUPLICATE_RECORD :
560
                $this->error = 'ERROR_SQL_DUPLICATE_RECORD';
561
                break;
562
 
563
            case self::SQL_CANNOT_ADD_OR_UPDATE_A_CHILD :
564
                $this->error = 'ERROR_SQL_CANNOT_ADD_OR_UPDATE_A_CHILD';
565
                break;
566
 
567
            case self::SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW  :
568
                $this->error = 'ERROR_SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW';
569
                break;
570
 
571
            default :
411 geraldo 572
                $this->error = 'ERROR_THERE_WAS_AN_ERROR';
1 www 573
                break;
574
        }
575
    }
576
 
577
 
578
    /**
579
     *
580
     * @param array $values
581
     * @param boolean $onlyNull
582
     * @return array
583
     */
584
    protected function removeEmpty($values, $onlyNull = false)
585
    {
586
        if($onlyNull) {
587
            $values = array_filter($values, function($v) {
588
                return !is_null($v);
589
            });
590
        } else {
591
            $values = array_filter($values, function($v) {
592
               return !empty($v);
593
            });
594
        }
595
 
596
        unset($values['added_on']);
597
        unset($values['updated_on']);
598
 
599
        return $values;
600
    }
601
}