Skip to content

Commit ee168e7

Browse files
committed
Get SQL code without executing it, fix #7
1 parent 41f3e7e commit ee168e7

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ $table->where('field', '=', 'value');
7070
// equals:
7171
// ... WHERE field1 = "value1" OR field2 LIKE "value2"
7272

73-
// peek at the generated sql code
73+
// peek at the generated sql code without executing it
7474
echo $table->sql() . '<br />';
7575

7676
// loop through the results and display them

classes/connector.mysqli.easy.class.php

+26-10
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,9 @@ public function contains($field_name, $value)
250250
public function delete()
251251
{
252252
$this->is_dirty = true;
253+
$this->buildQuery(MONTY_QUERY_DELETE);
253254

254-
return $this->buildQuery(MONTY_QUERY_DELETE);
255+
return $this->query();
255256
}
256257

257258
/**
@@ -331,8 +332,7 @@ protected function buildQuery($type = MONTY_QUERY_SELECT)
331332
break;
332333
}
333334
$this->is_dirty = false;
334-
335-
return $this->query($query_string);
335+
$this->query_string = $query_string;
336336
}
337337

338338
/**
@@ -511,13 +511,16 @@ protected function buildQueryLimit($type)
511511
/**
512512
* Monty_MySQLI_Easy::query()
513513
*
514-
* @param string $query_string The SQL query to execute
514+
* @param string $query_string optional The SQL query to execute
515515
*
516516
* @return bool $boolHasSucceeded
517517
*/
518-
public function query($query_string)
518+
public function query($query_string = null)
519519
{
520520
$this->is_dirty = false;
521+
if ($query_string === null) {
522+
$query_string = $this->query_string;
523+
}
521524
if (!parent::query($query_string)) {
522525
trigger_error($this->error(), E_USER_ERROR);
523526

@@ -602,6 +605,7 @@ public function limit()
602605
public function next($type = null)
603606
{
604607
$this->buildQuery();
608+
$this->query();
605609

606610
return parent::next($type);
607611
}
@@ -616,6 +620,7 @@ public function next($type = null)
616620
public function nextfield($field_data = 0)
617621
{
618622
$this->buildQuery();
623+
$this->query();
619624

620625
return parent::nextfield($field_data);
621626
}
@@ -631,6 +636,7 @@ public function nextfield($field_data = 0)
631636
public function queryall($query_string, $type = null)
632637
{
633638
$this->query($query_string);
639+
634640
return $this->all($type);
635641
}
636642

@@ -644,6 +650,7 @@ public function queryall($query_string, $type = null)
644650
public function all($type = null)
645651
{
646652
$this->buildQuery();
653+
$this->query();
647654

648655
return parent::all($type);
649656
}
@@ -685,7 +692,8 @@ public function insert($fields_list, $type = MONTY_INSERT_NORMAL)
685692
$this->is_dirty = true;
686693
$this->insert_type = $type;
687694

688-
return $this->buildQuery(MONTY_QUERY_INSERT);
695+
$this->buildQuery(MONTY_QUERY_INSERT);
696+
return $this->query();
689697
}
690698

691699
/**
@@ -696,6 +704,7 @@ public function insert($fields_list, $type = MONTY_INSERT_NORMAL)
696704
public function rows()
697705
{
698706
$this->buildQuery();
707+
$this->query();
699708

700709
return parent::rows();
701710
}
@@ -710,6 +719,7 @@ public function rows()
710719
public function seek($row_number)
711720
{
712721
$this->buildQuery();
722+
$this->query();
713723

714724
return parent::seek($row_number);
715725
}
@@ -731,12 +741,16 @@ public function sort($by, $is_asc = 1)
731741
/**
732742
* Monty_MySQLI_Easy::sql()
733743
*
734-
* @param int $type The query type
744+
* @param int $type optional The query type
745+
* @param int $insert_type optional INSERT type
735746
*
736747
* @return string $query_string
737748
*/
738-
public function sql($type = MONTY_QUERY_SELECT)
749+
public function sql($type = MONTY_QUERY_SELECT, $insert_type = MONTY_INSERT_NORMAL)
739750
{
751+
if ($type === MONTY_QUERY_INSERT) {
752+
$this->insert_type = $insert_type;
753+
}
740754
$this->buildQuery($type);
741755

742756
return $this->query_string;
@@ -764,7 +778,8 @@ public function truncate()
764778
{
765779
$this->is_dirty = true;
766780

767-
return $this->buildQuery(MONTY_QUERY_TRUNCATE);
781+
$this->buildQuery(MONTY_QUERY_TRUNCATE);
782+
return $this->query();
768783
}
769784

770785
/**
@@ -783,6 +798,7 @@ public function update($fields_list, $value = null)
783798
$this->fields_list = $fields_list;
784799
$this->is_dirty = true;
785800

786-
return $this->buildQuery(MONTY_QUERY_UPDATE);
801+
$this->buildQuery(MONTY_QUERY_UPDATE);
802+
return $this->query();
787803
}
788804
}

0 commit comments

Comments
 (0)