-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataModel.php
More file actions
1044 lines (908 loc) · 25.6 KB
/
DataModel.php
File metadata and controls
1044 lines (908 loc) · 25.6 KB
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**********************************************************
*
* basemms/back/App/Models/DataModel.php
*
* Class for handling MySQL tables, provides create,
* read, use and delete methods:
*
*
* initialiseTable() - Will create the table if
* it doesn't exist.
*
* insertTableRow() - Insert a row into the
* current table.
*
* updateTableRow() - Edit an existing row.
*
* findTablerow() - Will return one or more
* rows from the table.
*
* deleteTableRow() - Delete a row from the
* table.
*
* There's a few other methods but these are the most
* useful.
*
* M. Nealon, 2019.
*
*/
namespace App\Models;
use PDO;
/**
* These are used as keys to store and index the
* table layout.
*/
define('DB_SQL_TYPE', '__db_sql_type');
define('DB_SQL_SIZE', '__db_sql_size');
define('DB_SQL_PRIMARY', '__db_sql_primary');
define('DB_SQL_REQUIRED', '__db_sql_required');
/**
* Data types - if => true then a size parameter is
* required.
*/
define('DB_SQL_TYPES', Array(
"varchar" => true,
"char" => true,
"text" => true,
"int" => false
));
Class DataModel
{
protected $tableName;
protected $tableColumns;
protected $dbHandler;
protected $errorMessage;
/**
*
* Doesn't do much - creates the connection. Expects
* the following to be defined:
*
* DB_HOST
* DB_NAME
* DB_USER
* DB_PSWD
*
*/
public function __construct()
{
$this->__setError();
$this->tableName = false;
$this->tableColumns = false;
$_dsn = "mysql:dbhost=" . DB_HOST . ";dbname=" . DB_NAME;
try
{
$_con = new PDO($_dsn, DB_USER, DB_PSWD);
}
catch (PDOException $ex)
{
$this->errorMessage = $ex->getMessage();
return;
}
$this->dbHandler = $_con;
}
/**
*
* Sets the errorMessage and returns false.
*
* @param errorMessage
* The error message to be stored.
*
* @return
* Always returns false.
*
*/
private function __setError($errorMessage = false)
{
$this->errorMessage = $errorMessage;
return false;
}
/**
*
* Returns false if no errorMessage exists or
* returns the errorMessage, otherwise.
*
* @param reportError
* If true, any existing error message will be
* echo'd.
*
* @return
* Returns errorMessage, which will be either
* false (no errors) or return an error message.
*
*/
public function isError($reportError = false)
{
if ($this->errorMessage !== false)
{
if ($reportError !== false)
echo $this->errorMessage . PHP_EOL;
}
return $this->errorMessage;
}
/**
*
* Executes the gven query, doesn't return any
* results, this is used to create new tables,
* etc.
*
* @param sqlQuery
* Query string to be executed.
*
* @return
* True on success. On failure returns false and
* the errorMessage will be set.
*
*/
private function __execSQLQuery($sqlQuery)
{
try
{
$_stmt = $this->dbHandler->prepare($sqlQuery);
$_stmt->execute();
}
catch (\PDOException $ex)
{
return $this->__setError($ex->getMessage());
}
return true;
}
/**
*
* Creates the tableColumns array - this array
* essentially defines the table structure:
*
* [ 'column_id', 'type', size, 'OPTION' ... ]
*
* Where TYPE may be any of the types defined in
* DB_SQL_TYPES. Size must be an unquoted string of
* digits (if required).
*
* OPTION can be either of:
*
* primary Make this column the primary key
* required NOT NULL
* auto AUTO_INCREMENT
*
* @param tableName
* String containing the name of the table.
*
* @param tableColumns
* Array describing the table structure.
*
* @return
* Returns true on succes. On error, false will be
* returned and the errorMessage set.
*
*/
public function initialiseTable($tableName, $tableColumns)
{
$this->tableName = $tableName;
$this->tableColumns = Array();
$_sqlQuery = "";
$_primaryKey = false;
foreach ($tableColumns as $columnDefs)
{
$_sqlString = $this->getCreateTableString($columnDefs, $_primaryKey);
if ($_sqlString === false)
return false;
if ($_sqlQuery === "")
$_sqlQuery = $_sqlString;
else
$_sqlQuery .= "," . PHP_EOL . $_sqlString;
}
if ($_primaryKey !== false)
$_sqlQuery .= "," . PHP_EOL . "PRIMARY KEY ($_primaryKey)";
if ($this->tableExists($tableName) === false) {
try
{
$this->dbHandler->query("CREATE TABLE $tableName (" . PHP_EOL . $_sqlQuery . PHP_EOL . ")");
//echo "Created table $tableName: |$_sqlQuery|<br>\n";
//die();
}
catch (PDOException $ex)
{
$this->__setError("Error in initialiseTable(): " . $ex->getMessage());
}
}
return false;
}
/**
*
* Check whether a table exists - returns true
* if the table does exist and false if it
* does not.
*
* @param tableName
* Name of the table to check for.
*
* @return
* Since a false indicates the table doesn't
* exist - the isError() method should be used by
* the caller to determine failure/success.
*
*/
public function tableExists($tableName)
{
$_sqlQuery = "SHOW TABLES LIKE '$tableName'";
try
{
$_sqlResult = $this->dbHandler->query($_sqlQuery);
if ($_sqlResult->rowCount() > 0)
return true;
}
catch (PDOException $ex)
{
return $this->__setError("Error in tableExists(): " . $ex->getMessage());
}
return false;
}
/**
*
* The initialiseTable method calls this to build
* SQL data definitions from the tableColumns
* array.
*
* It takes in an array like:
*
* [ 'id', 'int', 'primary', 'required', 'auto' ]
*
* And returns the SQL for the definition:
*
* id INT PRIMARY NOT NULL AUT_INCREMENT
*
* Which is used to build the query for creating the
* new table.
*
* @param columnDefs
* Array defining a single column in the new table.
*
* @param primaryKey
* Reference to a string - if this column is marked
* 'primary' then the column name is returned here so
* the caller can add:
*
* PRIMARY KEY ($primaryKey)
*
* To the SQL.
*
* @return
* True on success, false on a fail. On error the
* errorMessage will be set.
*
*/
public function getCreateTableString($columnDefs, &$primaryKey)
{
// Record the SQL data type.
//
$this->tableColumns[$columnDefs[0]][DB_SQL_TYPE] = $columnDefs[1];
// This will return the data type definition without the
// size or any parameters (NOT NULL, AUTO_INCREMENT, etc).
//
if (($_sqlString = $this->__getSQLTypeDefinition($columnDefs)) === false)
return false;
// Both methods will return false if the specified value
// isn't in the columnDefs.
//
$_sqlDataSize = $this->__getSQLDataSize($columnDefs);
$_primaryKey = $this->__getSQLPrimaryKey($columnDefs);
// Does this data type require a size?
//
if (DB_SQL_TYPES[$columnDefs[1]] == true)
{
if ($_sqlDataSize === false)
return $this->__setError("Error in getSQLString(): data type {$columnDefs[1]} requires a size parameter");
$_sqlString .= "(" . $_sqlDataSize . ")";
}
// __getSQLDataRequired will returna an empty string if
// columnDefs doesn't contain the 'required' field, otherwise
// it will return the "NOT NULL" SQL option.
//
$_sqlString .= $this->__getSQLDataRequired($columnDefs);
// Lastly - if 'auto' is set in the columnDefs then this
// column has the AUTO_INCREMENT option set.
//
if (array_search("auto", $columnDefs) !== false)
$_sqlString .= " AUTO_INCREMENT";
if ($_primaryKey !== false)
$primaryKey = $_primaryKey;
return $_sqlString;
}
/**
*
* Grabs the first two tokens of the column
* definition. Token 0 should always be the
* colunm name. Token 1 should always be the
* type.
*
* Will return a string like:
*
* column_name type
*
* @param columnDefs
* Array defining the column.
*
* @return
* Returns the type definition string on success,
* returns false on failure. On failure, the
* errorMessage will be set.
*
*/
private function __getSQLTypeDefinition($columnDefs)
{
if (count($columnDefs) < 2)
return $this->__setError("Error in getSQLString(): no column parameters");
if (! isset(DB_SQL_TYPES[strtolower($columnDefs[1])]))
return $this->__setError("Error in getSQLString(): invalid column type {$columnDefs[1]}");
return $columnDefs[0] . " " . strtoupper($columnDefs[1]);
}
/**
*
* Checks the column definition for the size
* property.
*
* Easy to identiy, it's never a quoted string and
* it's all digits.
*
* @param columnDefs
* The array containing the column defninition.
*
* @return
* Returns the size property if found in the array,
* otherwise returns false.
*
*/
private function __getSQLDataSize($columnDefs)
{
$this->tableColumns[$columnDefs[0]][DB_SQL_SIZE] = false;
if (count($columnDefs) > 2)
{
if (preg_match('/^[0-9]+$/', $columnDefs[2], $match) == 1)
$this->tableColumns[$columnDefs[0]][DB_SQL_SIZE] = $match[0];
}
return $this->tableColumns[$columnDefs[0]][DB_SQL_SIZE];
}
/**
*
* Will check the given column definition array
* for the 'primary' keyword. If found, the column
* Id is returned - if not, then false is returned.
*
* @param columnDefs
* Array defining the column.
*
* @return
* Returns the primary key if found, otherwise
* returns false.
*
*/
private function __getSQLPrimaryKey($columnDefs)
{
$_primaryKey = false;
if (array_search("primary", $columnDefs) !== false) {
$_primaryKey = $columnDefs[0];
$this->tableColumns[$columnDefs[0]][DB_SQL_PRIMARY] = true;
}
else
$this->tableColumns[$columnDefs[0]][DB_SQL_PRIMARY] = false;
return $_primaryKey;
}
/**
*
* Will check the given column definition array for
* the 'required' keyword. If found, the "NOT NULL"
* string is returned.
*
* If not found, then an empty string is returned.
*
* @param columnDefs
* The array containing the column definition.
*
* @return
* Will return "NOT SULL" is the array contains the
* 'required' keyword, will return an empty string
* otherwise.
*
*/
private function __getSQLDataRequired($columnDefs)
{
$_sqlString = "";
if (array_search("required", $columnDefs) !== false) {
$_sqlString = " NOT NULL";
$this->tableColumns[$columnDefs[0]][DB_SQL_REQUIRED] = true;
}
else
$this->tableColumns[$columnDefs[0]][DB_SQL_REQUIRED] = false;
return $_sqlString;
}
/**
*
* When we use a the update or where method, we can
* pass an array containing search criteria.
*
* More specifically, we can pass arrays of arrays to
* describe relative complex relationships:
*
* [ 'id', '=', 'this', 'OR', 'id', '=', 'that' ]
*
* Resolves to:
*
* (id = :this OR id == :that)
*
* We can also nest these structures:
*
* [[ 'a', '=', 'b'], 'OR', [ 'a', '=', 'z' ]]
*
* Returns:
*
* ((a = :b) OR (a = :z))
*
* This is done through recursion hence the reference
* paramenters.
*
* @param newTableRow
* Array containing the new row information.
*
* @param sqlExpression
* The output string - i.e ((a = :b) OR (a = :z)) is
* returned here.
*
* @param exprParameters
* This is used to build the corresponding parameter
* list for the call to PDO execute().
*
*/
public function getSelectExpression($newTableRow, &$sqlExpression, &$exprParameters)
{
if ($sqlExpression === false)
$sqlExpression = "";
if ($exprParameters === false)
$exprParameters = Array();
$_breakRecursion = false;
$sqlExpression .= "(";
// Work backwards.
//
for ($column = (count($newTableRow) - 1); $column >= 0; $column--)
{
// If an array is found, it's a recursive call.
//
if (is_array($newTableRow[$column]))
{
// In this case an error has occured.
//
if (($_breakRecursion = $this->getSelectExpression(
$newTableRow[$column], $sqlExpression, $exprParameters
)) === false)
break;
}
else
{
if ($newTableRow[$column] == "AND" || $newTableRow[$column] == "OR")
{
if ($column == (count($newTableRow) - 1))
return $this->__serError("Error in getSelectExpression(): unexpected {$newTableRow[$column]} token");
$sqlExpression .= " {$newTableRow[$column]} ";
continue;
}
if ($this->buildExpressionString($newTableRow, $sqlExpression, $exprParameters, $column) === false)
return false;
$column -= 2;
}
}
$sqlExpression .= ")";
if ($_breakRecursion === true)
return false;
return true;
}
/**
*
* Where the getSelectExpression method will return
* resolve an expression, this method will retuns
* the individual expressions within the array.
*
* @param columnData
* Passed on from the getSelectExpression method,
* array defining the column.
*
* @param sqlExpression
* Passed on from the getSelectExpression method,
* it's used to build the WHERE type clause.
*
* @param exprParameters
* Passed in from the getSelectExpression method,
* this is used to accumulate the parameters for
* the WHERE clause.
*
* @param column
* Current column/token position in the array.
*
* @return
* True on success, on failure false is returned
* and the errorMessage is set.
*
*/
public function buildExpressionString($columnData, &$sqlExpression, &$exprParameters, $column)
{
if (($column - 2) < 0)
return $this->__setError("Error in getSelectExpression(): too few parameters in expression");
$_exprLValue = $columnData[($column - 2)];
$_exprOperator = $columnData[($column - 1)];
$_exprRValue = $columnData[$column];
$sqlExpression .= "{$_exprLValue} {$_exprOperator} :wh_{$_exprLValue}";
if (count($exprParameters) < 1)
$exprParameters["wh_" . $_exprLValue] = $_exprRValue;
else
$exprParameters["wh_" . $_exprLValue] = $_exprRValue;
return true;
}
/**
*
* Validates new table rows.
*
* First - it will ensure that all of the column names
* in the new row definitions are valid (i.e they are
* in the tableColumns definitions table).
*
* Next, if isUpdate is false, it will ensure that all
* columns marked as 'required' in the tableColumns are
* present and correct.
*
* It also build the sqlParams as well as the VALUES
* string required for an insert.
*
* @param newTableRow
* Array contianing new table row definition.
*
* @param sqlParams
* Array for accumulating parameters for the sqlValues
* string.
*
* @param sqlValues
* VALUES() string build for use with SQL UPDATE's.
*
* @param isUpdate
* If we're updating an existing record we will want to
* set this to true. If this is false then any new
* records MUST contain any columns marked as required
* (NOT NULL)
*
* @return
* True on success, false on failure and errorMessage
* will be set.
*
*/
public function validateTableRow($newTableRow, &$sqlParams, &$sqlValues, $isUpdate = false)
{
// This loop ensures that all of the columns in
// newTableRow have a valid id.
//
// If the column name/id exists there should be
// an entry in the tableColumns.
//
foreach ($newTableRow as $columnName=>$columnData)
{
if (! isset($this->tableColumns[$columnName]))
return $this->__setError("validateTableRow(): column $columnName not defined in table schema");
if ($sqlParams !== "")
$sqlParams .= ", ";
$sqlParams .= $columnName;
if ($sqlValues !== "")
$sqlValues .= ", ";
$sqlValues .= ":" . $columnName;
}
// If an updat eis being done we want to skip the next
// check.
//
if ($isUpdate === true)
return true;
// This loop ensures that all required fields in
// the table are present in newTableRow.
//
foreach ($this->tableColumns as $columnName=>$columnData)
{
if (isset($columnData[DB_SQL_REQUIRED]) && $columnData[DB_SQL_REQUIRED] === true) {
if (isset($columnData[DB_SQL_PRIMARY]) && $columnData[DB_SQL_PRIMARY] === true)
continue;
if (! isset($newTableRow[$columnName]))
return $this->__setError("validateTableRow(): column $columnName is a required field for this table");
}
}
return true;
}
/**
*
* Insert a new table row.
*
* @param newTableRow
* Array contiang table structure definitons.
*
* @return
* True on success, on failure false will be
* returned and errorMessage will be set.
*
*/
public function insertTableRow($newTableRow)
{
$_sqlParams = "";
$_sqlValues = "";
if ($this->validateTableRow(
$newTableRow,
$_sqlParams,
$_sqlValues
) === false)
return false;
$_sqlQuery = "INSERT INTO " . $this->tableName . "(" . $_sqlParams . ") VALUES(" . $_sqlValues . ")";
try
{
$_stmt = $this->dbHandler->prepare($_sqlQuery);
$_stmt->execute($newTableRow);
}
catch (PDOException $ex)
{
return $this->__setError("insertTableRow(): " . $ex->getMessage());
}
return true;
}
/**
*
* Update an existing record.
*
* The updateTableData array is an associative
* array stating the new columns and data to be
* stored in the record:
*
* [
* 'column_name' => 'new data',
* 'other_column' => 'more data'
* ]
*
* The matchTableData parameter is a search
* (WHERE) type clause and is resolved by the
* getSelectExpression method.
*
* @param updateTableData
* Array containing new data to be added to the
* record.
*
* @param matchTableData
* WHERE type clause string.
*
* @return
* True on success, on failure false is returned
* `and errorMessage is set.
*
*/
public function updateTableRow($updateTableData, $matchTableData)
{
$_sqlExpression = false;
$_exprParameters = Array();
$_sqlSet = $this->getSQLDataSet($updateTableData, $_exprParameters);
if ($_sqlSet === false)
return false;
if ($this->getSelectExpression($matchTableData, $_sqlExpression, $_exprParameters) === false)
return false;
$_sqlQuery = "UPDATE $this->tableName SET $_sqlSet WHERE $_sqlExpression";
try
{
// echo "update query: $_sqlQuery<br";
// print_r($_exprParameters);
// die();
$_stmt = $this->dbHandler->prepare($_sqlQuery);
$_stmt->execute($_exprParameters);
}
catch (PDOException $ex)
{
die($ex->getMessage());
return $this->__setError("Error in updateTableRow: " . $ex->getMessage());
}
return true;
}
/**
*
* Returns the string required for the SET in
* the UPDATE string.
*
* Example, if we do:
*
* updateTableRow([
* 'id' => '1'
* ], [
* 'id', '=', '0'
* ]);
*
* The SQL string we need is:
*
* UPDATE table SET id = :id WHERE(id = :id)
*
* The getSelectExpression and related method will
* return the WHERE part (id = :id) and order the
* parameters for that portion of the command.
*
* The SET portion of the string:
*
* id = :id
*
* If buld here, reason being if we are setting more
* than one field:
*
* id = :id, status = :status
*
* For example, we need to separate them with comma's,
* but the WHERE part we don't. Hence the separate
* method and parameters being constructed here.
*
* @param tableData
* Array containing the new record data.
*
* @param exprParameters
* Reference to an array where parameters are collected
* for this string.
*
* @return
* Returns a string containing the SET portion of
* the SQL command.
*
*/
public function getSQLDataSet($tableData, &$exprParameters)
{
$_sqlSet = "";
foreach ($tableData as $columnName=>$columnData)
{
if ($_sqlSet !== "")
$_sqlSet .= ",";
$_sqlSet .= "{$columnName} = :{$columnName}";
$exprParameters[$columnName] = $columnData;
}
return $_sqlSet;
}
/**
*
* Method used to search tables.
*
* I hacked this together for the search function
* on bitraq.
*
* Not much to say - returns any rows that match
* the given criteria.
*
* @param columnName
* Column to search for matches.
*
* @param searchQuery
* Query string to search for.
*
* @return
* Array of rows (if any) where a match was found.
*
*/
public function getRowsLike($columnName, $searchQuery)
{
$_sqlString = "SELECT * FROM {$this->tableName} WHERE $columnName LIKE ?";
try
{
$_stmt = $this->dbHandler->prepare($_sqlString);
$_stmt->execute(
Array(
"%" . $searchQuery . "%"
)
);
return $_stmt->fetchAll();
}
catch (PDOException $ex)
{
$this->__setError("Error in getRowsLike(): " . $ex->getMessage());
}
return false;
}
/**
*
* This method will return any rows who meet the
* criteria specified by the matchTableData array.
*
* If the matchTableData array parameter is false,
* then all rows in the table are returned.
*
* Uses the setSelectExpression method to resolve
* the expression array.
*
* @param matchTableData
* Array outlining match criteria - see comments
* for the getSelectExpression method.
*
* @return
* Returns the row or rows selected (if any). On
* error, false is returned and the errorMessage
* is set.
*
*/
public function findTableRow($matchTableData = false)
{
$_sqlExpression = false;
$_exprParameters = false;
if ($matchTableData !== false)
{
if ($this->getSelectExpression(
$matchTableData, $_sqlExpression, $_exprParameters
) === false)
return false;
$_sqlExpression = "WHERE " . $_sqlExpression;
}
else
$_sqlExpression = "";
$_sqlQuery = "SELECT * FROM {$this->tableName} $_sqlExpression";
try
{
if ($matchTableData === false)
$_stmt = $this->dbHandler->query($_sqlQuery);
else
{
$_stmt = $this->dbHandler->prepare($_sqlQuery);
$_stmt->execute($_exprParameters);
}
}
catch (PDOException $ex)
{
$this->__setError("Error in findTableRow(): " . $ex->getMessage());
}
return $_stmt->fetchAll(PDO::FETCH_ASSOC);
}
/**
*
* Deletes a row from the table. Again, the
* $matchTableData is an array describing a
* WHERE clause - getSelectExpression will
* be used to resolve.
*
* @param matchTableData
* Array desribing the match criteria for the
* DELETE.
*
* @return
* True on success, otherwise will return
* false and the errorMessage will be set.
*
*/
public function deleteTableRow($matchTableData)
{
$_sqlExpression = false;
$_exprParameters = false;
if ($this->getSelectExpression(
$matchTableData, $_sqlExpression, $_exprParameters
) === false)
return false;