Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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