// get rid of comments starting with # and --
- $string = preg_replace("/^(#|--).*\R*/m", "", $string);
+ $string = self::stripComments($string);
$queries = preg_split('/;\s*$/m', $string);
foreach ($queries as $query) {
}
}
}
+ /**
+ *
+ * Strips comment from a possibly multiline SQL string
+ *
+ * @param string $string
+ *
+ * @return string
+ * stripped string
+ */
+ public static function stripComments($string) {
+ return preg_replace("/^(#|--).*\R*/m", "", $string);
+ }
/**
* @param $ext
));
}
}
+ public function testStripComment() {
+ $strings = array(
+ "\nab\n-- cd\nef"=>"\nab\nef",
+ "ab\n-- cd\nef"=>"ab\nef",
+ "ab\n-- cd\nef\ngh"=>"ab\nef\ngh",
+ "ab\n--cd\nef"=>"ab\nef",
+ "ab\n--cd\nef\n"=>"ab\nef\n",
+ "ab\n#cd\nef\n"=>"ab\nef\n",
+ "ab\n--cd\nef"=>"ab\nef",
+ "ab\n#cd\nef"=>"ab\nef",
+ "ab\nfoo#cd\nef"=>"ab\nfoo#cd\nef",
+ "ab\r\n--cd\r\nef"=>"ab\r\nef",
+ "ab\r\n#cd\r\nef"=>"ab\r\nef",
+ "ab\r\nfoo#cd\r\nef"=>"ab\r\nfoo#cd\r\nef",
+ );
+ foreach ($strings as $string => $check) {
+ $test=CRM_Utils_File::stripComments($string);
+ $this->assertEquals($test,
+ $check,
+ sprintf("original=[%s]\nstripped=[%s]\nexpected=[%s]",
+ json_encode($string),
+ json_encode($test),
+ json_encode($check)
+ )
+ );
+ }
+ }
}