/** * Parse values in map and add to corresponding table array. * * @return bool * @uses DatabaseMap::$map * @uses DatabaseMap::$table_separator * @uses DatabaseMap::$from_tables * @uses DatabaseMap::$to_tables */ private function getTables() { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (empty($this->map)) { $this->Log->write('map is empty', Log::LOG_LEVEL_WARNING); return false; } $this->Log->write('looping through $this->map', Log::LOG_LEVEL_USER, $this->map); foreach ($this->map as $from => $to) { $this->Log->write('from', Log::LOG_LEVEL_USER, $from); $this->Log->write('to', Log::LOG_LEVEL_USER, $to); if ($to === 'skip') { $this->Log->write('skipping this one', Log::LOG_LEVEL_USER); continue; } elseif ($to === null || is_null($to) || !Helpers::is_string_ne($to)) { $this->Log->write('this is not a field to use', Log::LOG_LEVEL_WARNING, $from . ' => to ' . Helpers::get_type_size($to)); continue; } list($from_table, $from_field) = explode($this->table_separator, $from); if (Helpers::is_string_ne($from_table)) { if (!array_key_exists($from_table, $this->from_tables)) { $this->from_tables[$from_table] = array(); } if (Helpers::is_string_ne($from_field) && !array_search($from_field, $this->from_tables[$from_table])) { $this->from_tables[$from_table][] = $from_field; } } // populate is a value that is used to indicate the value is needed, but the field is not needed if ($to !== 'populate' && strstr($to, $this->table_separator)) { list($to_table, $to_field) = explode($this->table_separator, $to); if (Helpers::is_string_ne($to_table)) { if (!array_key_exists($to_table, $this->to_tables)) { $this->to_tables[$to_table] = array(); } if (Helpers::is_string_ne($to_field) && !array_search($to_field, $this->to_tables[$to_table])) { $this->to_tables[$to_table][] = $to_field; } } } } $this->Log->write('finished looping through $this->map', Log::LOG_LEVEL_USER); return true; }
/** * Import a file to MySQL through command line. * * @param string $file * @return bool * @todo Make this available for other SQL variants. */ public function importFile($file = '') { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!Helpers::is_string_ne($file)) { $this->Log->write('no file provided', Log::LOG_LEVEL_WARNING, Helpers::get_type_size($file)); return false; } if (!is_file($file)) { $this->Log->write('file does not exist', Log::LOG_LEVEL_WARNING, $file); return false; } $ext = substr($file, -3); if (strtolower($ext) !== 'sql') { $this->Log->write('file extension must be sql', Log::LOG_LEVEL_WARNING, $ext); return false; } $command = MYSQL_BIN . 'mysql --verbose -h ' . $this->host . ' -u' . $this->user; if (Helpers::is_string_ne($this->pass)) { $command .= ' -p\'' . trim($this->pass) . '\''; } $command .= ' ' . $this->dbname . ' < ' . $file; $output = array(); exec($command, $output, $return_var); if ($return_var != 0) { $this->Log->write($return_var . ': Failed to import from file', Log::LOG_LEVEL_WARNING, $output); return false; } return true; }
/** * Generate PHP string for this table and field. * * @param array $array Row of results from constant list * @return bool|int * @uses Db::query() * @uses Db::quote() */ protected function generate($array = array()) { $this->Log->write(__METHOD__, Log::LOG_LEVEL_SYSTEM_INFORMATION); // input validation if (!Helpers::is_array_ne($array)) { $this->Log->write('array is invalid', Log::LOG_LEVEL_WARNING, Helpers::get_call_string()); return false; } // these fields need to be present in the array $fields = array('table_name', 'name_field', 'value_field', 'type'); $valid = true; // check for the existence of each field in the array and break if one of them does not exist foreach ($fields as $field) { if (!array_key_exists($field, $array)) { $valid = false; break; } } if (!$valid) { $this->Log->write('input invalid', Log::LOG_LEVEL_WARNING); return false; } // assign parameters to variables $table = $array['table_name']; $field = $array['name_field']; $value_field = $array['value_field']; $type = $array['type']; $prefix = array_key_exists('prefix', $array) ? $array['prefix'] : $table; // build SELECT query for field and value $sql = 'SELECT ' . $field . ', ' . $value_field . PHP_EOL; $sql .= ' FROM ' . $table . PHP_EOL; $this->Log->write('generate SQL', Log::LOG_LEVEL_USER, $sql); // get rows from table $rows = $this->query($sql, array(), 'iterator'); if (!$rows instanceof DbIterator) { $this->Log->write('could not find rows from query', Log::LOG_LEVEL_WARNING); return false; } $this->Log->write('found rows for generate query', Log::LOG_LEVEL_USER); // build PHP string with comments to indicate table and field used in generation $php = PHP_EOL . '/**' . PHP_EOL; $php .= ' * ' . $table . '.' . $field . PHP_EOL; $php .= ' */' . PHP_EOL; foreach ($rows as $row) { if ($row === null || !array_key_exists($field, $row)) { continue; } // prepare constant name (upper case, underscores instead of spaces, no multiple underscores together) $val = strtoupper(Helpers::space_to_underscore($prefix . '_' . $row[$field])); // add define statement to string $php .= 'define(\'' . $val . '\', ' . $this->quote($row[$value_field], $type) . ');' . PHP_EOL; } $php .= '// END ' . $table . '.' . $field . PHP_EOL . PHP_EOL; $this->Log->write('built PHP string with ' . strlen($php) . ' characters', Log::LOG_LEVEL_USER); if (!Helpers::is_string_ne($php)) { $this->Log->write('There was an issue building the PHP.', Log::LOG_LEVEL_WARNING, Helpers::get_type_size($php)); return false; } // append string to global string $this->php .= $php; return strlen($php); }