]> git.mjollnir.org Git - moodle.git/commitdiff
New function record_exists_select with unit tests for all the record_exists_* functions.
authortjhunt <tjhunt>
Fri, 18 Aug 2006 11:28:30 +0000 (11:28 +0000)
committertjhunt <tjhunt>
Fri, 18 Aug 2006 11:28:30 +0000 (11:28 +0000)
lib/datalib.php
lib/simpletest/testdatalib.php

index ae3de5228a1add8ce9b53cfacadbafbc0f1e8003..f83d9c0c206afd4765225f64d0bdff31ea654558 100644 (file)
@@ -436,6 +436,24 @@ function record_exists($table, $field1='', $value1='', $field2='', $value2='', $
     return record_exists_sql('SELECT * FROM '. $CFG->prefix . $table .' '. $select .' LIMIT 1');
 }
 
+/**
+ * Test whether any records exists in a table which match a particular WHERE clause.
+ *
+ * @uses $CFG
+ * @param string $table The database table to be checked against.
+ * @param string $select A fragment of SQL to be used in a WHERE clause in the SQL call.
+ * @return bool true if a matching record exists, else false.
+ */
+function record_exists_select($table, $select='') {
+
+    global $CFG;
+
+    if ($select) {
+        $select = 'WHERE '.$select;
+    }
+
+    return record_exists_sql('SELECT * FROM '. $CFG->prefix . $table . ' ' . $select);
+}
 
 /**
  * Test whether a SQL SELECT statement returns any records.
index f7fcec230cce14d8421b00ee86f7c86fe8158671..38bceed0b5600ef8580684a056c9b0b217905a3b 100644 (file)
@@ -51,6 +51,23 @@ class datalib_test extends prefix_changing_test_case {
         $this->assertEqual(where_clause('f1', 'v1', 'f2', 2), "WHERE f1 = 'v1' AND f2 = '2'");
         $this->assertEqual(where_clause('f1', 'v1', 'f2', 1.75, 'f3', 'v3'), "WHERE f1 = 'v1' AND f2 = '1.75' AND f3 = 'v3'");
     }
+    
+    function test_record_exists() {
+        $this->assertTrue(record_exists($this->table, 'number', 101, 'id', 1));
+        $this->assertFalse(record_exists($this->table, 'number', 102, 'id', 1));
+    }
+
+    function test_record_exists_select() {
+        $this->assertTrue(record_exists_select($this->table, 'number = 101 AND id = 1'));
+        $this->assertFalse(record_exists_select($this->table, 'number = 102 AND id = 1'));
+    }
+
+    function test_record_exists_sql() {
+        global $CFG;
+        $this->assertTrue(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE number = 101 AND id = 1"));
+        $this->assertFalse(record_exists_sql("SELECT * FROM {$CFG->prefix}$this->table WHERE number = 102 AND id = 1"));
+    }
+
 
     function test_get_record() {
         // Get particular records.