本文整理汇总了PHP中DBC::getMap方法的典型用法代码示例。如果您正苦于以下问题:PHP DBC::getMap方法的具体用法?PHP DBC::getMap怎么用?PHP DBC::getMap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBC
的用法示例。
在下文中一共展示了DBC::getMap方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: export
/**
* Exports given DBC in XML format to given target (defaults to output stream)
*/
public function export(DBC $dbc, $target = self::OUTPUT)
{
$map = $dbc->getMap();
if ($map === null) {
throw new DBCException(self::NO_MAP);
return;
}
$dom = new DOMDocument('1.0');
$dom->formatOutput = true;
$edbc = $dom->appendChild($dom->createElement('dbc'));
$efields = $edbc->appendChild($dom->createElement('fields'));
$erecords = $edbc->appendChild($dom->createElement('records'));
$fields = $map->getFields();
foreach ($fields as $name => $rule) {
$count = max($rule & 0xff, 1);
if ($rule & DBCMap::UINT_MASK) {
$type = 'uint';
} else {
if ($rule & DBCMap::INT_MASK) {
$type = 'int';
} else {
if ($rule & DBCMap::FLOAT_MASK) {
$type = 'float';
} else {
if ($rule & DBCMap::STRING_MASK || $rule & DBCMap::STRING_LOC_MASK) {
$type = 'string';
}
}
}
}
for ($i = 1; $i <= $count; $i++) {
$suffix = $count > 1 ? $i : '';
$efields->appendChild($dom->createElement($name . $suffix, $type));
}
}
foreach ($dbc as $i => $record) {
$pairs = $record->extract();
$erecord = $erecords->appendChild($dom->createElement('record'));
foreach ($pairs as $field => $value) {
$attr = $dom->createAttribute($field);
$attr->value = $value;
$erecord->appendChild($attr);
}
}
$data = $dom->saveXML();
file_put_contents($target, $data);
}
示例2: export
/**
* Exports given DBC in JSON format to given target (defaults to output stream)
*/
public function export(DBC $dbc, $target = self::OUTPUT)
{
$map = $dbc->getMap();
if ($map === null) {
throw new DBCException(self::NO_MAP);
return;
}
$data = array('fields' => array(), 'records' => array());
$fields = $map->getFields();
foreach ($fields as $name => $rule) {
$count = max($rule & 0xff, 1);
if ($rule & DBCMap::UINT_MASK) {
$type = 'uint';
} else {
if ($rule & DBCMap::INT_MASK) {
$type = 'int';
} else {
if ($rule & DBCMap::FLOAT_MASK) {
$type = 'float';
} else {
if ($rule & DBCMap::STRING_MASK || $rule & DBCMap::STRING_LOC_MASK) {
$type = 'string';
}
}
}
}
for ($i = 1; $i <= $count; $i++) {
$suffix = $count > 1 ? $i : '';
$data['fields'][$name . $suffix] = $type;
}
}
foreach ($dbc as $record) {
$data['records'][] = array_values($record->extract());
}
file_put_contents($target, json_encode($data));
}
示例3: fromDBC
/**
* Attempts to construct a map based on given DBC, predicting what each field could possibly hold by way of sampling
*/
public static function fromDBC(DBC $dbc, $attach = true)
{
$fields = $dbc->getFieldCount();
$samples = $dbc->getRecordCount() > self::SAMPLES ? self::SAMPLES : $dbc->getRecordCount();
$block = $dbc->getStringBlock();
preg_match_all('#\\0#', $block, $matches, PREG_OFFSET_CAPTURE);
$strings = array();
foreach ($matches[0] as $offset) {
$offset = (int) $offset[1] + 1;
if ($offset < strlen($block) - 1) {
$strings[$offset] = true;
}
}
$matrix = array_fill(1, $fields, 0);
for ($i = 0; $i < $samples; $i++) {
$record = $dbc->getRecord($i);
$values = $record->asArray();
foreach ($values as $offset => $value) {
if ($value < 0) {
$matrix[$offset] += 1 << 0;
}
if (self::isProbableFloat($value)) {
$matrix[$offset] += 1 << 8;
}
if (isset($strings[$value]) || $value === 0) {
$matrix[$offset] += 1 << 16;
if ($value !== 0) {
$matrix[$offset] |= 1 << 24;
}
}
}
}
$map = new self();
for ($i = 1; $i <= $fields; $i++) {
$probs = $matrix[$i];
$int = ($probs & 0xff) / $samples;
$flt = (($probs & 0xff00) >> 8) / $samples;
$str = (($probs & 0xff0000) >> 16) / $samples;
$strbit = ($probs & 0xff000000) >> 24;
$field = 'field' . $i;
if ($flt > 0.6) {
$type = DBC::FLOAT;
} else {
if ($strbit > 0 && $str > 0.99) {
$type = DBC::STRING;
if ($i + DBC::LOCALIZATION <= $fields) {
$type = DBC::STRING_LOC;
for ($j = $i + 1; $j <= $i + DBC::LOCALIZATION; $j++) {
$probs = $matrix[$j];
$str = (($probs & 0xff0000) >> 16) / $samples;
$strbit = ($probs & 0xff000000) >> 24;
if ($str !== 1 || $strbit !== 0) {
$type = DBC::STRING;
}
}
if ($type === DBC::STRING_LOC) {
$i += DBC::LOCALIZATION;
}
}
} else {
if ($int > 0.01) {
$type = DBC::INT;
} else {
$type = DBC::UINT;
}
}
}
$map->add($field, $type);
}
if ($attach && $dbc->getMap() === null) {
$dbc->attach($map);
}
return $map;
}
示例4: export
/**
* Exports given DBC in SQL format to given target (defaults to output stream) using given table name
*/
public function export(DBC $dbc, $target = self::OUTPUT, $table = 'dbc')
{
$target = $target === null ? self::OUTPUT : $target;
$map = $dbc->getMap();
if ($map === null) {
throw new DBCException(self::NO_MAP);
return;
}
$sql = fopen($target, 'w+');
$table = "`" . $this->escape($table) . "`";
fwrite($sql, "DROP TABLE IF EXISTS " . $table . ";" . PHP_EOL . PHP_EOL);
$dd = array();
$fields = $map->getFields();
foreach ($fields as $name => $rule) {
$count = max($rule & 0xff, 1);
$null = false;
if ($rule & DBCMap::UINT_MASK) {
$type = 'INT(11) UNSIGNED';
} else {
if ($rule & DBCMap::INT_MASK) {
$type = 'INT(11) SIGNED';
} else {
if ($rule & DBCMap::FLOAT_MASK) {
$type = 'FLOAT';
} else {
if ($rule & DBCMap::STRING_MASK || $rule & DBCMap::STRING_LOC_MASK) {
$type = 'TEXT';
$null = true;
}
}
}
}
for ($i = 1; $i <= $count; $i++) {
$suffix = $count > 1 ? $i : '';
$dd[] = ' `' . $this->escape($name) . $suffix . '` ' . $type . ' ' . ($null ? 'NULL' : 'NOT NULL');
}
}
reset($fields);
fwrite($sql, "CREATE TABLE " . $table . " (" . PHP_EOL . implode(',' . PHP_EOL, $dd) . ',' . PHP_EOL . ' PRIMARY KEY (`' . key($fields) . '`)' . PHP_EOL . ');' . PHP_EOL . PHP_EOL);
foreach ($dbc as $i => $record) {
if ($i % $this->recordsPerQuery === 0) {
fwrite($sql, "INSERT INTO " . $table . " VALUES" . PHP_EOL . "\t(");
} else {
fwrite($sql, "," . PHP_EOL . "\t(");
}
fwrite($sql, $this->join($record->extract()));
if (($i + 1) % $this->recordsPerQuery === 0 || $i === $dbc->getRecordCount() - 1) {
fwrite($sql, ");" . PHP_EOL);
} else {
fwrite($sql, ")");
}
}
fclose($sql);
return $target;
}