-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLinda.php
822 lines (666 loc) · 29.4 KB
/
Linda.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
<?php
namespace solutionstack\Linda;
require_once \realpath(\dirname(__FILE__)) . "/" . 'Linda.inc';
/**
*
* @brief Linda is an Object Oriented ORM for PHP Built on top PDO, it currently supports MySql.
* @author Olubodun Agbalaya ([email protected])
* @version GIT: 1.1.0
* @copyright 2018 Olubodun Agbalaya
* @license MIT License
* @link https://github.com./solutionstack/Linda
*/
class Linda
{
/** Holds last error raised */
protected $lindaError = null;
protected $dbLink;
protected $tableModel;
/** Holds last run query */
protected $currentQuery;
/** Holds column names for current schema @var array */
protected $modelSchema = array();
protected $defaultLimit = 1000;
protected $defaultStartIndex = 0;
/** Apply MySQL DSTINCT to query @var boolean */
protected $distinctResult = false;
protected $resultObject = null;
/** Number of rows returned or affected by last ops */
protected $lastAffectedRowCount = 0;
protected $queryConfig = array();
protected $defaultPrimaryKeyColumn = null;
protected $isFectchOps = false;
/** configured in Linda.inc */
const LINDA_DB_HOST = LINDA_DB_HOSTC;
/** configured in Linda.inc */
const LINDA_DB_NAME = LINDA_DB_NAMEC;
/** configured in Linda.inc */
const LINDA_DB_TYPE = LINDA_DB_TYPEC;
/** configured in Linda.inc */
const LINDA_DB_USER = LINDA_DB_USERC;
/** configured in Linda.inc */
const LINDA_DB_PASSW = LINDA_DB_PASSWC;
public function __construct()
{
$this->initConnnection();
}
/**
* @ignore
* @internal
*/
protected function setTable($tableName)
{
if (\is_string($tableName)) {
$this->tableModel = $tableName;
}
}
/**
* Fetches the table schema (columns) and finds the primary key if available
*
* @ignore
* @internal
*/
//+========================================================================================
protected function parseModel()
{
$this->modelSchema = []; //always reset;
$sql
= "SELECT COLUMN_NAME,COLUMN_KEY FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table AND TABLE_SCHEMA = :schema";
try {
$core = $this->dbLink;
$stmt = $core->prepare($sql);
$stmt->bindValue(':table', $this->tableModel, \PDO::PARAM_STR);
$stmt->bindValue(':schema', self::LINDA_DB_NAME, \PDO::PARAM_STR);
$stmt->execute();
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$this->modelSchema[] = $row['COLUMN_NAME'];
//detect primary key
if (isset($row['COLUMN_KEY']) && $row['COLUMN_KEY'] === "PRI") {
$this->defaultPrimaryKeyColumn = $row['COLUMN_NAME'];
}
}
if ( ! count($this->modelSchema)) {
throw new Exception("Schemea *" . $this->tableModel . "* not valid or doesn't exist");
}
return $this->modelSchema;
} catch (PDOException $pe) {
trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR);
}
}
//+=========================================================================================================
/**
* @ignore
* @internal
*/
private function initConnnection()
{
try {
$this->dbLink = new \PDO(
self::LINDA_DB_TYPE . ':host=' . self::LINDA_DB_HOST . ';dbname=' . self::LINDA_DB_NAME,
self::LINDA_DB_USER, self::LINDA_DB_PASSW, array(\PDO::ATTR_PERSISTENT => true)
);
$this->dbLink->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
return true;
} catch (\PDOException $pe) {
$this->lindaError = "DB_CONNECT_ERROR";
throw $pe;
}
}
//+=========================================================================================================
/**
* The get method fetches data from the table
*
* @param array() | string $feilds An associative array containing fields to get from the table, or the string *
* @param array() $queryConfig A configuration array, that contains option for the get operation
*
* <pre>
* $queryConfig = array(
*
* "whereGroup" => array(
* [
* "actor_id"=>array("value"=>5, "operator"=>"="),
* "last_name"=>array("value"=>"'%ER", "operator"=>"LIKE"),
* "comparisonOp"=>"AND",
* "nextOp"=>"OR"
* ],
*
* [
* "actor_id"=>array("value"=>"last_name", "operator"=>"LIKE")
*
* ] ),
*
*
*
*
* "where_in"=>array(
* fieldName => "id",
* options => " 10, 15, 22,
* query => "",
* operator = > "AND"
*
* ),
*
* "innerJoinGroup" => array(
* [
* table => "table_name",
* conditional_column_a => "column name"
* conditional_column_b => "column name"
* ],
* [
* table => "table_name",
* conditional_column => "column name"
* ]
*
*
* ) ,
*
* "limit" =>[
* index => 2,
* count =>18
*
* ],
*
* )
* </pre>
* @return Linda
*/
protected function fetch($fields, $queryConfig = array())
{
$this->isFectchOps = true;
$this->resultObject = null;
$this->queryBuilder("select", $fields, $queryConfig);
$this->runQuery();
return $this;
}
//+=========================================================================================================
/**
* Update fields in the DB
*
* @param array $fields is an associative array containing values to set
* <pre>
* $fields = array(
* id= "1",
* email = "example.example.org
* )
* using NOW() or TIME() as values, inserts the date/time
* </pre>
* @param array $queryConfig see #insert for structure of the queryConfig parameter
*
*
* @return Linda
*/
protected function update($fields, $queryConfig)
{
$this->isFectchOps = false;
$this->resultObject = null;
$this->queryBuilder("update", $fields, $queryConfig);
$this->runQuery();
return $this;
}
//+=========================================================================================================
/**
* Inserts data into the table
*
* @param array $fields An associative array, containing column names that matches the actual table
* @param array $values An associative array, values to be inserted per column, using NOW() or TIME(), inserts the date/time
* in either YMD format or YMD H:m:s format
*
* @return Linda
*/
protected function put($fields, $values)
{
$this->isFectchOps = false;
$this->resultObject = null;
$this->createInsert($fields, $values);
$this->runQuery();
return $this;
}
//+=========================================================================================================
/**
* Deletes data from a table
*
* @param array $queryConfig See #insert for structure of the queryConfig parameter
*
* @return Linda
*/
protected function delete($queryConfig)
{
$this->isFectchOps = false;
$this->resultObject = null;
$this->queryBuilder("delete", null, $queryConfig);
$this->runQuery();
return $this;
}
//+======================================================+
/**
*
* @internal
* @ignore
*/
protected function sanitize($inp)
{
//escape values for database query
if (\is_array($inp)) {
return \array_map(__METHOD__, $inp);
}
if ( ! empty($inp) && \is_string($inp)) {
return \str_replace(
array('\\', "\0", "\n", "\r", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', '\\"', '\\Z'), $inp
);
}
return \trim(\strip_tags(\stripslashes(\htmlentities($inp, ENT_QUOTES, 'UTF-8'))));
}
//+======================================================+
//*argument $inserts is an associative array of column names and value-data
protected function createInsert($fields, $inserts)
{
//call our custom escape function on the data-values
$values = \array_map(array($this, "sanitize"),
\array_map(array($this, "stringOrInt"), \array_values($inserts)));
// $keys = \array_keys($inserts);
$this->currentQuery = "INSERT INTO `" . $this->tableModel . "` (" . \implode(',', $fields) . ") VALUES ("
. \implode(", ", $values) . ")";
}
//+==================================================================+
//this method checks if a value is a string and quotes it if it is
protected function stringOrInt($val, $key = '')
{
if ( ! \is_numeric($val) || "+" === \substr($val, 0, 1)) {
//if it not numeric
if ( ! empty($val)) { //if its not null
return "'" . $val . "'";
}
//add quotes
if (empty($val) && ! \is_string($key)) {
//null colums
return "" . null . "";
}
}
return $val;
}
//+=========================================================================================================
/**
*
* @internal
* @ignore
*/
protected function queryBuilder($mode, $fields, $queryConfig)
{
$this->currentQuery = "";
switch ($mode) {
case "select":
$this->currentQuery .= " SELECT " . ($this->distinctResult ? "distinctResult " : "") . (\is_string($fields)
? "* " : \implode(",", $fields)) . " FROM `" . $this->tableModel . "` ";
//handle joins
$join_count = 0; //
foreach ($queryConfig as $index => $item) {
//loop through each inner join array argument
if (false !== \strpos($index, "innerJoinGroup")) {
$this->currentQuery .= " AS T" . (++$join_count);
foreach ($item as $key => $val) {
$this->currentQuery .= " INNER JOIN `" . $val['table'] . "` AS T" . (++$join_count) . " ON T1."
. $val['conditional_column_a'] . " = T" . ($join_count) . "." . $val['conditional_column_b']
. " ";
}
}
}
//handle where clause
$where_clause_counter = 1;
$in_where_clause = 1;
foreach ($queryConfig as $index => $item) {
//loop through each where Groups
if (false !== \strpos($index, "whereGroup")) {
//HANDLE WHERE CLUASE
if ($where_clause_counter++ === 1) {
$this->currentQuery .= " WHERE (";
}
foreach ($item as $key => $whereGroupIndex) {
//loop through each where Groups
$nextComparisonOp = isset($whereGroupIndex["nextOp"]) ? $whereGroupIndex["nextOp"] : "AND";
if ($in_where_clause > 1) {
$in_where_clause = 1;
}
foreach ($whereGroupIndex as $key2 => $val2) {
//loop through each whereGroups
if ($key2 !== "comparisonOp" && $key2 !== "nextOp" && $key2 !== "join_index") {
if ($in_where_clause++ > 1) {
$this->currentQuery .= isset($whereGroupIndex['comparisonOp']) ? " "
. $whereGroupIndex['comparisonOp'] . " " : " AND ";
}
if ($key2 !== "operator") { //this key shouldnt be added as a value
$this->currentQuery .= " "
//add the column name and comparison operator
. "`" . $key2 . "`" . " " . (isset($val2['operator']) ? $val2['operator'] : "=")
. " "
//add the column value we are comparing with
. $this->sanitize($this->stringOrInt($val2['value']));
}
}
}
$this->currentQuery .= " )";
if (\next($item)) {
$this->currentQuery .= " " . $nextComparisonOp . " (";
}
}
}
}
//handle where_in_*
if (isset($queryConfig['where_in'])) {
$where_operator = isset($queryConfig['where_in']['operator']) ? " "
. $queryConfig['where_in']['operator'] . " " : " AND ";
if ($where_clause_counter++ > 1) {
$this->currentQuery .= $where_operator . " `" . $queryConfig['where_in']['fieldName'] . "` IN (" .
(isset($queryConfig['where_in']['query']) ? $queryConfig['where_in']['query']
: $queryConfig['where_in']['options']) . ")";
}
else {
$this->currentQuery .= " WHERE `" . $queryConfig['where_in']['fieldName'] . "` IN (" .
(isset($queryConfig['where_in']['query']) ? $queryConfig['where_in']['query']
: $queryConfig['where_in']['options']) . ")";
}
}
//handle where_not_in_*
if (isset($queryConfig['where_not_in'])) {
$where_operator = isset($queryConfig['where_not_in']['operator']) ? " "
. $queryConfig['where_not_in']['operator'] . " " : " AND ";
if ($where_clause_counter++ > 1) {
$this->currentQuery .= $where_operator . " `" . $queryConfig['where_not_in']['fieldName']
. "` NOT IN (" .
(isset($queryConfig['where_not_in']['query']) ? $queryConfig['where_not_in']['query']
: $queryConfig['where_not_in']['options']) . ")";
}
else {
$this->currentQuery .= " WHERE `" . $queryConfig['where_not_in']['fieldName'] . "` NOT IN (" .
(isset($queryConfig['where_not_in']['query']) ? $queryConfig['where_not_in']['query']
: $queryConfig['where_not_in']['options']) . ")";
}
}
if (isset($queryConfig['limit'])) {
$this->currentQuery .= " LIMIT " . $queryConfig['limit']['index'];
$this->currentQuery .= ", " . $queryConfig['limit']['count'];
}
else {
$this->currentQuery .= " LIMIT " . $this->defaultStartIndex;
$this->currentQuery .= ", " . $this->defaultLimit;
}
$this->currentQuery .= ";";
break;
case "update":
$update_column_count = 1;
$this->currentQuery = "UPDATE `" . $this->tableModel . "` SET `";
foreach ($fields as $key => $val) {
if ($update_column_count++ > 1) {
$this->currentQuery .= ",`";
}
//seperate the next row feild/value
if (\is_array($val)) {
$this->currentQuery .= $key . "` = (" . $val[0] . " )";
}
else {
$this->currentQuery .= $key . "` = " . $this->sanitize($this->stringOrInt($val, $key, true)) . " ";
}
}
//handle where clause
$where_clause_counter = 1;
$in_where_clause = 1;
foreach ($queryConfig as $index => $item) {
//loop through each where Groups
if (false !== \strpos($index, "whereGroup")) {
//HANDLE WHERE CLUASE
if ($where_clause_counter++ === 1) {
$this->currentQuery .= " WHERE (";
}
foreach ($item as $key => $whereGroupIndex) {
//loop through each where Groups
$nextComparisonOp = isset($whereGroupIndex["nextOp"]) ? $whereGroupIndex["nextOp"] : "AND";
if ($in_where_clause > 1) {
$in_where_clause = 1;
}
foreach ($whereGroupIndex as $key2 => $val2) {
//loop through each whereGroups
if ($key2 !== "comparisonOp" && $key2 !== "nextOp") {
//add comparison (defaults to AND)
if ($in_where_clause++ > 1) {
$this->currentQuery .= isset($whereGroupIndex['comparisonOp']) ? " "
. $whereGroupIndex['comparisonOp'] . " " : " AND ";
}
if ($key2 !== "operator") { //this key shouldnt be added as a value
$this->currentQuery .= " `" . $key2 . "` " . (isset($val2['operator'])
? $val2['operator'] : "=") . " " . $this->sanitize(
$this->stringOrInt($val2['value'])
);
}
}
}
$this->currentQuery .= " )";
if (\next($item)) {
$this->currentQuery .= " " . $nextComparisonOp . " (";
}
}
}
}
//handle where_in_*
if (isset($queryConfig['where_in'])) {
$where_operator = isset($queryConfig['where_in']['operator']) ? " "
. $queryConfig['where_in']['operator'] . " " : " AND ";
if ($where_clause_counter++ > 1) {
$this->currentQuery .= $where_operator . " `" . $queryConfig['where_in']['fieldName'] . "` IN (" .
($queryConfig['where_in']['query'] ? $queryConfig['where_in']['query']
: $queryConfig['where_in']['options']) . ")";
}
else {
$this->currentQuery .= " WHERE `" . $queryConfig['where_in']['fieldName'] . "` IN (" .
($queryConfig['where_in']['query'] ? $queryConfig['where_in']['query']
: $queryConfig['where_in']['options']) . ")";
}
}
//handle where_not_in_*
if (isset($queryConfig['where_not_in'])) {
$where_operator = isset($queryConfig['where_not_in']['operator']) ? " "
. $queryConfig['where_not_in']['operator'] . " " : " AND ";
if ($where_clause_counter++ > 1) {
$this->currentQuery .= $where_operator . " `" . $queryConfig['where_not_in']['fieldName']
. "` NOT IN (" .
(isset($queryConfig['where_not_in']['query']) ? $queryConfig['where_not_in']['query']
: $queryConfig['where_not_in']['options']) . ")";
}
else {
$this->currentQuery .= " WHERE `" . $queryConfig['where_not_in']['fieldName'] . "` NOT IN (" .
(isset($queryConfig['where_not_in']['query']) ? $queryConfig['where_not_in']['query']
: $queryConfig['where_not_in']['options']) . ")";
}
}
$this->currentQuery .= " ;";
break;
case "delete":
$this->currentQuery = "DELETE FROM `" . $this->tableModel . "` ";
$this->currentQuery .= "";
//handle where clause
$where_clause_counter = 1;
$in_where_clause = 1;
foreach ($queryConfig as $index => $item) {
//loop through each where Groups
if (false !== \strpos($index, "whereGroup")) {
//HANDLE WHERE CLUASE
if ($where_clause_counter++ === 1) {
$this->currentQuery .= " WHERE(";
}
foreach ($item as $key => $whereGroupIndex) {
//loop through each where Groups
$nextComparisonOp = isset($whereGroupIndex["nextOp"]) ? $whereGroupIndex["nextOp"] : "AND";
if ($in_where_clause > 1) {
$in_where_clause = 1;
}
foreach ($whereGroupIndex as $key2 => $val2) {
//loop through each whereGroups
if ($key2 !== "comparisonOp" && $key2 !== "nextOp") {
if ($in_where_clause++ > 1) {
$this->currentQuery .= isset($whereGroupIndex['comparisonOp']) ? " "
. $whereGroupIndex['comparisonOp'] . " " : " AND ";
}
if ($key2 !== "operator") {
//this key shouldnt be added as a value
$this->currentQuery .= " `" . $key2 . "` " . (isset($val2['operator'])
? $val2['operator'] : "=") . " " . $this->sanitize(
$this->stringOrInt($val2['value'])
);
}
}
}
$this->currentQuery .= " )";
if (\next($item)) {
$this->currentQuery .= " " . $nextComparisonOp . " (";
}
}
}
}
//handle where_in_*
if (isset($queryConfig['where_in'])) {
$where_operator = isset($queryConfig['where_in']['operator']) ? $queryConfig['where_in']['operator']
: " AND ";
if ($where_clause_counter++ > 1) {
$this->currentQuery .= $where_operator . " `" . $queryConfig['where_in']['fieldName'] . "` IN (" .
($queryConfig['where_in']['query'] ? $queryConfig['where_in']['query']
: $queryConfig['where_in']['options']) . ")";
}
else {
$this->currentQuery .= " WHERE `" . $queryConfig['where_in']['fieldName'] . "` IN (" .
($queryConfig['where_in']['query'] ? $queryConfig['where_in']['query']
: $queryConfig['where_in']['options']) . ")";
}
}
//handle where_not_in_*
if (isset($queryConfig['where_not_in'])) {
$where_operator = isset($queryConfig['where_not_in']['operator']) ? " "
. $queryConfig['where_not_in']['operator'] . " " : " AND ";
if ($where_clause_counter++ > 1) {
$this->currentQuery .= $where_operator . " `" . $queryConfig['where_not_in']['fieldName']
. "` NOT IN (" .
(isset($queryConfig['where_not_in']['query']) ? $queryConfig['where_not_in']['query']
: $queryConfig['where_not_in']['options']) . ")";
}
else {
$this->currentQuery .= " WHERE `" . $queryConfig['where_not_in']['fieldName'] . "` NOT IN (" .
(isset($queryConfig['where_not_in']['query']) ? $queryConfig['where_not_in']['query']
: $queryConfig['where_not_in']['options']) . ")";
}
}
if (isset($queryConfig['LIMIT'])) {
$this->currentQuery .= " LIMIT " . (int)$queryConfig['LIMIT'];
}
$this->currentQuery .= ";";
break;
}
return $this;
}
//+===========================================================================================
//run the query and set the return object
/**
* @ignore
* @return $this
*/
protected function runQuery()
{
$this->currentQuery = \str_replace("NOW()", \date("Y-m-d"), $this->currentQuery);
$this->currentQuery = \str_replace("TIME()", \date("Y-m-d H:i:s"), $this->currentQuery);
$this->lindaError = "";
$this->lastAffectedRowCount = 0;
$stmnt = null;
try {
if (($stmnt = $this->dbLink->prepare($this->currentQuery))) {
if ( ! $stmnt->execute()) {
$this->lindaError = "ERROR_EXECUTING_QUERY";
$this->resultObject = null;
}
if ($stmnt->rowCount()) {
$this->lastAffectedRowCount = $stmnt->rowCount();
}
if ($this->isFectchOps) {
$this->resultObject = $stmnt->fetchAll(\PDO::FETCH_ASSOC);
}
if (false === $this->resultObject) {
$this->lindaError = "ERROR_EXECUTING_QUERY";
$this->resultObject = null;
}
//set the number of rows returned for select operation
if ( ! $this->lastAffectedRowCount && \count($this->resultObject)) {
$this->lastAffectedRowCount = \count($this->resultObject);
}
if ($this->resultObject) {
if (\count($this->resultObject) || $this->lastAffectedRowCount) {
}
else {
$this->resultObject = null;
}
};
}
} catch (PDOException $e) {
$this->lindaError = $e->getMessage();
$this->resultObject = null;
echo $this->lindaError;
}
catch(\Error $e){
$this->lindaError = $e->getMessage();
$this->resultObject = null;
echo $this->lindaError;
}
//after a query, reset these vars
$this->defaultLimit = 1000;
$this->defaultStartIndex = 0;
$this->distinctResult = false;
$this->queryConfig = array();
return $this;
}
//+===================================================================================================
/**
* Fetches row(s) containing the maximum value of a particular column, this method executes a query directly on the table and doesnt
* work on the retrieved/stored data - so you must have set the table using the #setTable method, prior to calling
*
* DO NOT CALL DIRECTLY, CALLED BY LindaModel::maxRow
*
* @param string $fieldName field to get the maximum value from
*
* @return array()
*/
protected function maxRow_($fieldName)
{
$this->isFectchOps = true;//set this unless a result obj wouldnt be geneated
$this->currentQuery = "SELECT * FROM " . $this->tableModel . " WHERE " . $fieldName . " = (SELECT MAX("
. $fieldName . ") FROM " . $this->tableModel . ")";
$this->runQuery();
$this->isFectchOps = false;
return $this;
}
//+===================================================================================================
/**
* Fetches row(s) containing the maximum value of a particular column,, this method executes a query directly on the table and doesnt
* work on the retrieved/stored data - so you must have set the table using the #setTable method, prior to calling
*
* @param string $columnName field to get the minimum value from
*
* DO NOT CALL DIRECTLY, CALLED BY LindaModel::minRow
*
* @return array()
*/
protected function minRow_($columnName)
{
$this->isFectchOps = true;//set this unless a result obj wouldnt be geneated
$this->currentQuery = "SELECT * FROM " . $this->tableModel . " WHERE " . $columnName . " = (SELECT MIN("
. $columnName . ") FROM " . $this->tableModel . ")";
$this->runQuery();
$this->isFectchOps = false;
return $this;
}
/**
* Returns the total number of rows in the result set, or the numbers of rows affected by the last update/delete operation
*
* @return integer
*/
protected function numRows()
{
return $this->lastAffectedRowCount;
}
public function __destruct()
{
}
}