Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3639 | | 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
 
3186 efrain 199
 
1 www 200
        return false;
201
    }
202
 
203
    /**
204
     *
205
     * @param Delete $delete
206
     * @return boolean
207
     */
208
    protected function executeDelete($delete)
209
    {
210
        $this->initRespose();
211
        try
212
        {
213
            $stmt = $this->sql->prepareStatementForSqlObject($delete);
214
            $result = $stmt->execute();
215
            if($result) {
216
                $this->affectedRows = $result->getAffectedRows();
217
                return true;
218
            }
219
        }
220
        catch(\Exception $e)
221
        {
222
            error_log($delete->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
223
            $this->processError($e);
224
        }
225
 
3186 efrain 226
 
1 www 227
        return false;
228
    }
229
 
3639 efrain 230
    protected function executeDeleteUsingSentenceWithParameters($sql, $parameters = [])
231
    {
232
        $this->initRespose();
233
 
234
        try {
235
 
236
            $stmt = $this->adapter->createStatement($sql);
237
            $stmt->prepare();
238
            $result = $stmt->execute($parameters);
239
 
240
            if ($result) {
241
                $this->affectedRows = $result->getAffectedRows();
242
                return true;
243
            }
244
        }
245
        catch(\Exception $e)
246
        {
247
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
248
            $this->processError($e);
249
        }
250
 
251
        return false;
252
    }
253
 
1 www 254
    /**
255
     *
256
     * @param Select $select
257
     * @param Object $prototype
258
     * @return array
259
     */
260
    protected function executeFetchAllObject($select, $prototype)
261
    {
262
 
263
        $this->initRespose();
264
        try {
265
            $stmt = $this->sql->prepareStatementForSqlObject($select);
266
            $result = $stmt->execute();
267
 
268
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
269
                $hydrator = new ObjectPropertyHydrator();
270
 
271
                $records = [];
272
                foreach($result as $row)
273
                {
274
                    $record = clone $prototype;
275
                    $hydrator->hydrate($row, $record);
276
 
277
                    array_push($records, $record);
278
                }
279
 
280
                return $records;
281
            }
282
        }
3186 efrain 283
        catch(\Exception $e)
284
        {
1 www 285
            error_log($select->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
286
            $this->processError($e);
287
        }
288
 
289
        return [];
290
    }
291
 
292
    /**
293
     *
294
     * @param Select $select
295
     * @return array
296
     */
297
    protected function executeFetchAllArray($select)
298
    {
299
 
300
        $this->initRespose();
301
 
302
        try {
303
            $stmt = $this->sql->prepareStatementForSqlObject($select);
304
            $result = $stmt->execute();
305
 
306
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
307
 
308
                $records = [];
309
                foreach($result as $record)
310
                {
311
                    array_push($records, $record);
312
                }
313
                return $records;
314
            }
315
        }
3186 efrain 316
        catch(\Exception $e)
317
        {
1 www 318
            error_log($select->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
319
            $this->processError($e);
320
        }
3186 efrain 321
 
322
 
1 www 323
        return [];
324
    }
325
 
326
    /**
327
     *
328
     * @param Select $select
329
     * @param Object $prototype
330
     * @return void|Object
331
     */
332
    protected function executeFetchOneObject($select, $prototype)
333
    {
334
        $this->initRespose();
335
 
336
        try {
337
            $select->limit(1);
338
 
339
            $stmt = $this->sql->prepareStatementForSqlObject($select);
340
            $result = $stmt->execute();
341
 
342
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
343
 
344
                $hydrator = new ObjectPropertyHydrator();
345
                $hydrator->hydrate($result->current(), $prototype);
346
 
347
                return $prototype;
348
            }
349
        }
3186 efrain 350
        catch(\Exception $e)
351
        {
1 www 352
            error_log($select->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
353
            $this->processError($e);
354
        }
3186 efrain 355
 
1 www 356
 
357
        return;
358
    }
359
 
360
    /**
361
     *
362
     * @param Select $select
363
     * @return void|array
364
     */
365
    protected function executeFetchOneArray($select)
366
    {
367
        $this->initRespose();
368
 
369
        try {
370
            $select->limit(1);
371
 
372
            $stmt = $this->sql->prepareStatementForSqlObject($select);
373
            $result = $stmt->execute();
374
 
375
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
376
 
377
                return $result->current();
378
            }
379
        }
3186 efrain 380
        catch(\Exception $e)
381
        {
1 www 382
            error_log($select->getSqlString($this->adapter->platform) . ' ' .  $e->getMessage());
383
            $this->processError($e);
384
        }
3186 efrain 385
 
1 www 386
 
387
        return;
388
    }
389
 
390
    /**
391
     *
392
     * @param string $sql
393
     * @param array $parameters
394
     * @return boolean
395
     */
396
    protected function executeInsertUsingSentenceWithParameters($sql, $parameters = [])
397
    {
398
        $this->initRespose();
399
 
400
        try {
401
 
402
            $stmt = $this->adapter->createStatement($sql);
403
            $stmt->prepare();
404
            $result = $stmt->execute($parameters);
405
 
406
            if ($result) {
407
                $this->lastInsertId = $result->getGeneratedValue();
408
                $this->affectedRows = $result->getAffectedRows();
409
                return true;
410
            }
411
        }
3186 efrain 412
        catch(\Exception $e)
413
        {
1 www 414
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
415
            $this->processError($e);
416
        }
3186 efrain 417
 
1 www 418
        return false;
419
    }
420
 
421
 
422
    /**
423
     *
424
     * @param string $sql
425
     * @param array $parameters
426
     * @return boolean
427
     */
428
    protected function executeSentenceWithParameters($sql, $parameters = [])
429
    {
430
        $this->initRespose();
431
        try
432
        {
433
 
434
            $stmt = $this->adapter->createStatement($sql);
435
            $stmt->prepare();
436
            $result = $stmt->execute($parameters);
437
 
438
 
439
            if($result) {
440
                $this->affectedRows = $result->getAffectedRows();
441
                return true;
442
            }
443
        }
3186 efrain 444
        catch(\Exception $e)
445
        {
446
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
1 www 447
            $this->processError($e);
448
        }
449
 
450
        return false;
451
    }
452
 
453
    /**
454
     *
455
     * @param Object $prototype
456
     * @param string $sql
457
     * @param array $parameters
458
     * @return array
459
     */
460
    protected function executeFetchAllObjectUsingParameters($prototype, $sql, $parameters = [])
461
    {
462
        $this->initRespose();
463
        try {
464
 
465
            $stmt = $this->adapter->createStatement($sql);
466
            $stmt->prepare();
467
            $result = $stmt->execute($parameters);
468
 
469
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
470
 
471
                if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
472
                    $hydrator = new ObjectPropertyHydrator();
473
 
474
                    $records = [];
475
                    foreach($result as $row)
476
                    {
477
                        $record = clone $prototype;
478
                        $hydrator->hydrate($row, $record);
479
 
480
                        array_push($records, $record);
481
                    }
482
 
483
                    return $records;
484
                }
485
            }
486
        }
3186 efrain 487
        catch(\Exception $e)
488
        {
1 www 489
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
490
            $this->processError($e);
491
        }
3186 efrain 492
 
1 www 493
 
494
        return [];
495
    }
496
 
497
    /**
498
     *
499
     * @param string $sql
500
     * @param array $parameters
501
     * @return array
502
     */
503
   protected function executeFetchAllArrayUsingParameters($sql, $parameters = [] )
504
    {
505
        $this->initRespose();
506
 
507
        try {
508
            $stmt = $this->adapter->createStatement($sql);
509
            $stmt->prepare();
510
            $result = $stmt->execute($parameters);
511
 
512
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
513
 
514
                $records = [];
515
                foreach($result as $row)
516
                {
517
                    array_push($records, $row);
518
                }
519
 
520
                return $records;;
521
            }
522
        }
3186 efrain 523
        catch(\Exception $e)
524
        {
1 www 525
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
526
            $this->processError($e);
527
        }
3186 efrain 528
 
529
 
1 www 530
        return [];
531
    }
532
 
533
    /**
534
     *
535
     * @param Object $prototype
536
     * @param string $sql
537
     * @param array $parameters
538
     * @return void|Object
539
     */
540
    protected function executeFetchOneObjectUsingParameters($prototype, $sql, $parameters = [])
541
    {
542
        $this->initRespose();
543
 
544
        try {
545
            $stmt = $this->adapter->createStatement($sql);
546
            $stmt->prepare();
547
            $result = $stmt->execute($parameters);
548
 
549
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
550
 
551
                $hydrator = new ObjectPropertyHydrator();
552
                $hydrator->hydrate($result->current(), $prototype);
553
 
554
                return $prototype;
555
            }
556
        }
3186 efrain 557
        catch(\Exception $e)
558
        {
1 www 559
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
560
            $this->processError($e);
561
        }
3186 efrain 562
 
1 www 563
 
564
        return;
565
    }
566
 
567
    /**
568
     *
569
     * @param string $sql
570
     * @param array $parameters
571
     * @return void|array
572
     */
573
    protected function executeFetchOneArrayUsingParameters($sql, $parameters = [])
574
    {
575
        $this->initRespose();
576
 
577
        try {
578
            $stmt = $this->adapter->createStatement($sql);
579
            $stmt->prepare();
580
            $result = $stmt->execute($parameters);
581
 
582
            if ($result instanceof ResultInterface && $result->isQueryResult() && $result->count()) {
583
 
584
                return $result->current();
585
            }
586
        }
3186 efrain 587
        catch(\Exception $e)
588
        {
1 www 589
            error_log($sql  .  ' (' . implode(',', $parameters) .  ') ' .  $e->getMessage());
590
            $this->processError($e);
591
        }
3186 efrain 592
 
1 www 593
 
594
        return;
595
    }
596
 
597
    private function processError($e)
598
    {
599
 
3186 efrain 600
        $message = $e->getMessage();
601
        if (strpos($message, strval(self::SQL_DUPLICATE_RECORD) ) !== false) {
602
 
603
            $this->errno = self::SQL_DUPLICATE_RECORD;
604
            $this->error = 'ERROR_SQL_DUPLICATE_RECORD';
605
 
606
        }
607
        else if (strpos($message, strval(self::SQL_CANNOT_ADD_OR_UPDATE_A_CHILD)  ) !== false) {
608
 
609
            $this->errno = self::SQL_CANNOT_ADD_OR_UPDATE_A_CHILD ;
610
            $this->error = 'ERROR_SQL_CANNOT_ADD_OR_UPDATE_A_CHILD';
611
 
612
        }
613
        else if (strpos($message, strval(self::SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW) ) !== false) {
614
 
615
            $this->errno = self::SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW;
616
            $this->error = 'ERROR_SQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW';
617
 
618
        } else {
619
 
620
            $this->errno = 0;
621
            $this->error = 'ERROR_THERE_WAS_AN_ERROR';
622
 
623
        }
1 www 624
 
625
    }
626
 
627
 
628
    /**
629
     *
630
     * @param array $values
631
     * @param boolean $onlyNull
632
     * @return array
633
     */
634
    protected function removeEmpty($values, $onlyNull = false)
635
    {
636
        if($onlyNull) {
637
            $values = array_filter($values, function($v) {
638
                return !is_null($v);
639
            });
640
        } else {
641
            $values = array_filter($values, function($v) {
642
               return !empty($v);
643
            });
644
        }
645
 
646
        unset($values['added_on']);
647
        unset($values['updated_on']);
648
 
649
        return $values;
650
    }
6388 efrain 651
 
652
    /**
653
     *
654
     * @return string
655
     */
656
    public function getDatebaseNow()
657
    {
658
        $record = $this->executeFetchOneArrayUsingParameters('SELECT now() AS now');
659
        return $record['now'];
660
 
661
    }
1 www 662
}