Skip to content

Commit ade9b71

Browse files
committed
Add cast feature
1 parent 127e388 commit ade9b71

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

inc/db_pdo.inc

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ class DB_Sql {
1919
/* public: configuration parameters */
2020
var $Auto_Free = 0; ## Set to 1 for automatic mysqli_free_result()
2121
var $Debug = 0; ## Set to 1 for debugging messages.
22-
var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
22+
var $Halt_On_Error = "report"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
2323
var $Seq_Table = "db_sequence";
2424

2525
/* public: result array and current row number */
2626
var $Record = array();
2727
var $Row;
28+
var $Query;
2829

2930
/* public: current error number and error text */
3031
var $ErrCode = 0;
@@ -82,7 +83,10 @@ class DB_Sql {
8283
if (!$this->connect()) { return 0; };
8384
if (!is_array($param)) $param = func_get_args();
8485
if (!((bool)count(array_filter(array_keys($param), 'is_string')))) { // this is NOT an associative array, so we cannot bindValue or bindParams
85-
if (!$status = $this->sth->execute($param)) $this->halt(array_search(null,$param)?"Null Parameter":"Cannot Execute");
86+
if (!$status = $this->sth->execute($param)) {
87+
if ($this->Debug) var_dump($param);
88+
$this->halt(array_search(null,$param)?"Null Parameter":"Cannot Execute");
89+
}
8690
} else {
8791
foreach ($param as $key => &$val) {
8892
if ($val === null) {
@@ -135,8 +139,8 @@ class DB_Sql {
135139
if (!$this->dbh) $this->halt("open($Database) failed.");
136140
break;
137141
case "pgsql";
138-
$this->dbh = new PDO("$this->Server:host=$Host;dbname=$Database;port=5432", $User);
139-
# $this->dbh = new PDO("$this->Server:host=$Host;dbname=$Database;port=5432", $User, $Password);
142+
# $this->dbh = new PDO("$this->Server:host=$Host;dbname=$Database;port=5432", $User);
143+
$this->dbh = new PDO("$this->Server:host=$Host;dbname=$Database;port=5432", $User, $Password);
140144
break;
141145
default:
142146
if ($this->Debug) echo "Connecting to $Host as $User<br>\n";
@@ -202,6 +206,7 @@ class DB_Sql {
202206
/* public: perform a query */
203207
function query($Query_String, $retries=1) {
204208
/* No empty queries, please, since PHP4 chokes on them. */
209+
$this->Query = $Query_String;
205210
if ($Query_String == "") {
206211
/* The empty query string is passed on from the constructor,
207212
* when calling the class without a query, e.g. in situations
@@ -252,13 +257,43 @@ class DB_Sql {
252257
return $this->sth->fetchColumn($col);
253258
}
254259
/* public get all records */
255-
function fetchAll() {
260+
function fetchAll($cast=false,$num=false) {
256261
if (!$this->sth) {
257262
$this->halt("fetchAll called with no query pending.");
258263
return 0;
259264
}
265+
if ($cast) $meta = $this->metadata();
260266
if ($this->sth->columnCount()>1) {
261267
// Array of records (rows)
268+
if ($cast) {
269+
foreach ($this->sth->fetchAll(PDO::FETCH_NUM) as $row) {
270+
$count=0;
271+
foreach($row as $key=>$val) {
272+
if ($num) $name=$count; else
273+
$name = $meta[$key]["name"];
274+
$type = $meta[$key]["type"];
275+
if ( preg_match("/int$/i",$type)) $type = "int";
276+
switch($type) {
277+
case "date":
278+
case "datetime":
279+
$casted_row[$name] = (strtotime($val)/86400)+25569.4167;
280+
break;
281+
case "int":
282+
case "long":
283+
case "longlong":
284+
case "newdecimal":
285+
case "float":
286+
case "double":
287+
$casted_row[$name] = $val + 0;
288+
break;
289+
default:
290+
$casted_row[$name] = $val;
291+
}
292+
$count++;
293+
}
294+
$data[] = $casted_row;
295+
}
296+
} else
262297
$data = $this->sth->fetchAll(PDO::FETCH_ASSOC);
263298
if (!$data) $data = array(); // if no records return empty array/
264299
} else {

0 commit comments

Comments
 (0)