]> git.mjollnir.org Git - moodle.git/commitdiff
Introducing sql_concat_join($sep, $array) helper
authormartinlanghoff <martinlanghoff>
Tue, 26 Sep 2006 05:05:54 +0000 (05:05 +0000)
committermartinlanghoff <martinlanghoff>
Tue, 26 Sep 2006 05:05:54 +0000 (05:05 +0000)
returns the SQL to do the equivalente of a join() or implode()
on the DB server. Watch the RDBMS work! ;-)

lib/dmllib.php

index b149383afa52c480986a890cc44853bcc1a0e2b6..bf0ad74cca9a56f0c699d35330ee02b79d3699bd 100644 (file)
@@ -1249,6 +1249,31 @@ function sql_concat() {
     return call_user_func_array(array('Concat', $db), $args);
 }
 
+/**
+ * Returns the proper SQL to do CONCAT between the elements passed
+ * with a given separator
+ *
+ * @uses $db
+ * @param string $separator
+ * @param array  $elements
+ * @return string
+ */
+function sql_concat_join($separator="' '", $elements=array()) {
+    global $db;
+    // copy to ensure pass by value
+    $elem = $elements;
+
+    // Intersperse $elements in the array.
+    // Add items to the array on the fly, walking it
+    // _backwards_ splicing the elements in. The loop definition
+    // should skip first and last positions.
+    for ($n=count($elem)-1; $n > 0 ; $n--) {
+        array_splice($elem, $n, 0, $separator);
+    }
+    return call_user_func_array(array('Concat', $db), $elem);
+}
+
 /**
  * Returns the proper SQL to do IS NULL
  * @uses $CFG