/** * Create a PHP array in the proper format with the relationship values. * * @return bool * @uses Generator::$iterator * @uses Relationship::$relationship_array */ protected function generate() { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!$this->iterator instanceof DbIterator) { $this->Log->write('iterator is not valid', Log::LOG_LEVEL_WARNING, Helpers::get_type_size($this->iterator)); return false; } $array = array(); foreach ($this->iterator as $row) { if (!Helpers::is_string_ne($row['child_table'])) { continue; } if (!array_key_exists($row['child_table'], $array)) { $array[$row['child_table']] = array(); } $array[$row['child_table']][$row['child_field']] = array('table' => $row['parent_table'], 'field' => $row['parent_field']); } $this->relationship_array = $array; return Helpers::is_array_ne($this->relationship_array); }
/** * Split SQL string into individual lines of SQL by the provided delimiter. * * @param string $delimiter * @return bool * @todo Add handling for changing delimiters during SQL execution * @todo @see http://stackoverflow.com/questions/147821/loading-sql-files-from-within-php#answer-149456 */ private function split($delimiter = ';') { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!Helpers::is_string_ne($this->sql)) { $this->Log->write('SQL needs content before processing', Log::LOG_LEVEL_WARNING); return false; } if (!Helpers::is_string_ne($delimiter)) { $this->Log->write('delimiter provided is not a string', Log::LOG_LEVEL_WARNING, Helpers::get_type_size($delimiter)); return false; } // split string into possible SQL statements $tokens = explode($delimiter, $this->sql); // save memory since we have the SQL in tokens $this->sql = ''; $this->sql_lines = array(); $matches = array(); $token_count = count($tokens); for ($i = 0; $i < $token_count; $i++) { $token = $tokens[$i]; if ($i != $token_count - 1 || strlen($token) > 0) { // total number of single quotes in the token $total_quotes = preg_match_all("/'/", $token, $matches); // count escaped quotes (preceded by odd number of backslashes) $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $token, $matches); $unescaped_quotes = $total_quotes - $escaped_quotes; // if unescaped quotes is even, the delimiter did not occur in the token if ($unescaped_quotes % 2 == 0) { // this is a complete SQL statement $this->sql_lines[] = $token; $tokens[$i] = ''; } else { // this is an incomplete SQL statement $temp = $token . $delimiter; $tokens[$i] = ''; $complete_stmt = false; for ($j = $i + 1; !$complete_stmt && $j < $token_count; $j++) { // total number of single quotes in the token $total_quotes = preg_match_all("/'/", $tokens[$j], $matches); // count escaped quotes (preceded by odd number of backslashes) $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches); $unescaped_quotes = $total_quotes - $escaped_quotes; if ($unescaped_quotes % 2 == 1) { // odd number of quotes matches this statement with the previous statement $this->sql_lines[] = $temp . $tokens[$j]; $tokens[$j] = ''; $temp = ''; // exit the loop $complete_stmt = true; // update outer loop iteration $i = $j; } else { // even number of quotes indicates an incomplete statement $temp .= $tokens[$j] . $delimiter; $tokens[$j] = ''; } } // end for inner loop } } } // end for outer loop return count($this->sql) > 0; }