/** * Generate RDBMS-specific SQL for CREATE TABLE * * @param Charcoal_ITableModel $model table model object related with th query * @param boolean|Charcoal_Boolean $if_not_exists If TRUE, output SQL includes "IF NOT EXISTS" wuth "CREATE TABLE" * * @return string SQL */ public function buildCreateTableSQL($model, $if_not_exists = false) { Charcoal_ParamTrait::validateIsA(1, 'Charcoal_ITableModel', $model); Charcoal_ParamTrait::validateBoolean(2, $if_not_exists); try { $field_list = $model->getFieldList(); $SQL_pk_list = array(); $SQL_field_list = array(); foreach ($field_list as $name) { $name = s($name); $pk = $model->getAnnotationValue($name, s('pk')); $type = $model->getAnnotationValue($name, s('type')); $notnull = $model->getAnnotationValue($name, s('notnull')); $charset = $model->getAnnotationValue($name, s('charset')); $serial = $model->getAnnotationValue($name, s('serial')); $default = $model->getAnnotationValue($name, s('default')); $comment = $model->getAnnotationValue($name, s('comment')); /** @var Charcoal_AnnotationValue $type */ $type_name = $this->mapType($type->getValue()); $field_expr = "`{$name}` {$type_name}"; if ($pk) { $SQL_pk_list[] = "`{$name}`"; if ($notnull) { $field_expr .= ' NOT NULL'; } if ($serial) { $field_expr .= ' AUTO_INCREMENT'; } if ($comment) { $field_expr .= " COMMENT '" . $comment->getValue() . "'"; } } else { if ($default) { $field_expr .= ' DEFAULT ' . $default->getValue(); } if ($charset) { $field_expr .= " CHARACTER SET {$charset}"; } if ($notnull) { $field_expr .= ' NOT NULL'; } if ($comment) { $field_expr .= " COMMENT '" . $comment->getValue() . "'"; } } $SQL_field_list[] = $field_expr; } $SQL_field_list = implode(",\n", $SQL_field_list); $SQL_pk_list = implode(',', $SQL_pk_list); $table_name = $model->getTableName(); $if_not_exists = ub($if_not_exists) ? 'IF NOT EXISTS' : ''; $sql = "CREATE TABLE {$if_not_exists} `{$table_name}` (\n {$SQL_field_list} \n ,PRIMARY KEY( {$SQL_pk_list} ) )"; log_debug("debug,sql,smart_gateway", "buildCreateTableSQL result: {$sql}", self::TAG); return $sql; } catch (Exception $e) { _throw(new Charcoal_SQLBuilderException("MySQL_SQLBuilder#buildCreateTableSQL failed")); } return ''; }
public function createFile($file_path, $contents, $overwrite = TRUE, $mode = 0777) { Charcoal_ParamTrait::validateString(1, $file_path); Charcoal_ParamTrait::validateString(2, $contents); Charcoal_ParamTrait::validateBoolean(3, $overwrite); Charcoal_ParamTrait::validateInteger(4, $mode); $obj = new Charcoal_File($file_path, $this->_base_dir_obj); if ($overwrite) { if ($obj->exists() && !$obj->canWrite()) { _throw(new Charcoal_FileSystemComponentException('specified file is not writeable.')); } } elseif ($obj->exists()) { _throw(new Charcoal_FileSystemComponentException('specified file is already exists.')); } try { // create file with parent directory $obj->makeFile($mode, $contents, TRUE); return $obj; } catch (Exception $e) { _catch($e); _throw(new Charcoal_FileSystemComponentException(s('creating file failed.'), $e)); } }
public function autoCommit($on) { $on = ub($on); try { Charcoal_ParamTrait::validateBoolean(1, $on); // 接続処理 $this->connect(); $this->getConnection()->setAttribute(PDO::ATTR_AUTOCOMMIT, $on); } catch (Exception $e) { _catch($e); _throw(new Charcoal_DBDataSourceException(__METHOD__ . " Failed.", $e)); } }
/** * parse XML * * @param Charcoal_String|string $data Chunk of data to parse * @param Charcoal_Boolean|bool $is_final If set and TRUE, data is the last piece of data sent in this parse. * * @return bool Returns 1 on success or 0 on failure. */ public function parse($data, $is_final = false) { Charcoal_ParamTrait::validateString(1, $data); Charcoal_ParamTrait::validateBoolean(2, $is_final); if (!$this->parser) { _throw(new PhpXmlParserComponentException('parser object is not created')); } return xml_parse($this->parser, us($data), ub($is_final)); }
/** * Set overwrite mode * * @param bool|Charcoal_Boolean $overwrite TRUE if the temporary file should be overwritten, FALSE otherwise. */ public function setOverwrite($overwrite) { Charcoal_ParamTrait::validateBoolean(1, $overwrite); $this->overwrite = ub($overwrite); }
public function validate($sequence, $form_token, $throws = TRUE) { Charcoal_ParamTrait::validateIsA(1, 'Charcoal_ISequence', $sequence); Charcoal_ParamTrait::validateString(2, $form_token); Charcoal_ParamTrait::validateBoolean(3, $throws); $throws = ub($throws); log_debug("debug", "sequence: " . print_r($sequence, true)); log_debug("debug", "form_token: " . print_r($form_token, true)); if ($this->getSandbox()->isDebug() && $this->debug_mode) { ad($sequence, array('title' => "sequence")); } $token_key = $this->token_key; log_debug("debug", "token_key: " . print_r($token_key, true)); if ($this->getSandbox()->isDebug() && $this->debug_mode) { ad($token_key, array('title' => "token_key", "type" => "div")); } // get token container from session. $token_list = $sequence->get(s($token_key)); if ($this->getSandbox()->isDebug() && $this->debug_mode) { ad($token_list, array('title' => "token list")); } log_debug("debug", "token_list: " . print_r($token_list, true)); if ($token_list === NULL || !is_array($token_list)) { $token_list = array(); } // find token from token list. $token_index = NULL; foreach ($token_list as $idx => $token) { log_info("debug", "token: {$token}"); if ($this->getSandbox()->isDebug() && $this->debug_mode) { ad($token, array('title' => "token", "type" => "div")); } if ($token == $form_token) { $token_index = $idx; break; } } if ($token_index === NULL) { // illegal access log_warning("system, debug", "token not found: {$form_token}"); if ($this->getSandbox()->isDebug() && $this->debug_mode) { ad($form_token, array('title' => "token not found", "type" => "div")); } if ($throws) { _throw(new Charcoal_FormTokenValidationException('token not found in sequence:' . $form_token), FALSE); } return FALSE; } else { // authorized access log_debug("debug", "token accepted: {$form_token}"); if ($this->getSandbox()->isDebug() && $this->debug_mode) { ad($form_token, array('title' => "token accepted", "type" => "div")); } // erase token from token list to prevent duplicate form submission. unset($token_list[$token_index]); } // update token list in sequence. $sequence->set($token_key, $token_list); // the event was successfully processed. return TRUE; }
/** * real implementation of Charcoal_SmartGateway::dropTable() * * @param Charcoal_String|string $comment comment text * @param Charcoal_String|string $model_name * @param boolean|Charcoal_Boolean $if_exists If TRUE, output SQL includes "IF EXISTS" wuth "DROP TABLE" */ public function dropTable($comment, $model_name, $if_exists = false) { Charcoal_ParamTrait::validateString(1, $comment, TRUE); Charcoal_ParamTrait::validateString(2, $model_name); Charcoal_ParamTrait::validateBoolean(3, $if_exists); $model = $this->getModel($model_name); $sql = $this->sql_builder->buildDropTableSQL($model, $if_exists); $sql = !empty($comment) ? $this->sql_builder->prependComment($sql, $comment) : $sql; $rows_affected = $this->data_source->execute($sql); log_debug("debug,sql,smart_gateway", "dropTable result: {$rows_affected}", self::TAG); return $rows_affected; }
/** * Generate RDBMS-specific SQL for DROP TABLE * * @param Charcoal_ITableModel $model table model object related with th query * @param boolean|Charcoal_Boolean $if_exists If TRUE, output SQL includes "IF EXISTS" wuth "DROP TABLE" * * @return string SQL */ public function buildDropTableSQL($model, $if_exists = false) { Charcoal_ParamTrait::validateIsA(1, 'Charcoal_ITableModel', $model); Charcoal_ParamTrait::validateBoolean(2, $if_exists); $table_name = $model->getTableName(); $if_exists = ub($if_exists) ? 'IF EXISTS' : ''; $sql = "DROP TABLE {$if_exists} `{$table_name}`"; return $sql; }