protected function mysqlQueryInner($tableName, $inverted = FALSE) { if (!$this->hasColumn("array", $this->path)) { return "1 /* " . JsonStoreSearch::$INCOMPLETE_TAG . ": can't check array items at {$this->path} */"; } $columnName = "array" . $this->path; $parentConfig = $this->config; $limitParams = (object) $this->values; $arrayConfig = $parentConfig['columns'][$columnName]; $subSearch = new JsonStoreSearch($arrayConfig, new JsonSchema(), ""); $subTable = new JsonStoreQueryConstructor($arrayConfig['table'], $tableName . "_itemlimits"); $joinOn = $subSearch->tableColumn($subTable, "group") . " = " . $this->tableColumn($tableName, $arrayConfig['parentColumn']); $subQuery = "(\n\tSELECT COUNT(*) AS `row_count`\n\t\tFROM " . str_replace("\n", "\n\t", $subTable->selectFrom()) . "\n\tWHERE {$joinOn}\n)"; $condition = NULL; if (!$inverted) { if (isset($limitParams->minItems)) { if (isset($limitParams->maxItems)) { $condition = "BETWEEN " . JsonStore::mysqlQuote($limitParams->minItems) . " AND " . JsonStore::mysqlQuote($limitParams->maxItems); } else { $condition = ">= " . JsonStore::mysqlQuote($limitParams->minItems); } } else { $condition = "<= " . JsonStore::mysqlQuote($limitParams->maxItems); } } else { if (isset($limitParams->minItems)) { if (isset($limitParams->maxItems)) { $condition = "NOT BETWEEN " . JsonStore::mysqlQuote($limitParams->minItems) . " AND " . JsonStore::mysqlQuote($limitParams->maxItems); } else { $condition = "< " . JsonStore::mysqlQuote($limitParams->minItems); } } else { $condition = "> " . JsonStore::mysqlQuote($limitParams->maxItems); } } return "{$subQuery} {$condition}"; }
function checkConfig($config, $isArray = FALSE, $parentType = NULL) { global $mysqlTypeMap; $columns = array(); if ($isArray) { $groupColumn = isset($config['alias']) && isset($config['alias']['group']) ? $config['alias']['group'] : 'group'; $indexColumn = isset($config['alias']) && isset($config['alias']['group']) ? $config['alias']['index'] : 'index'; if (!$parentType) { $columns[$groupColumn] = JsonStore::escapedColumn($groupColumn) . " INT(11) AUTO_INCREMENT COMMENT 'Array ID (generated)'"; } else { if (isset($mysqlTypeMap[$type])) { $type = $mysqlTypeMap[$type]; $columns[$groupColumn] = JsonStore::escapedColumn($groupColumn) . " {$type} COMMENT 'Link to parent'"; } else { $type = "INT(11) /*{$type}*/"; $columns[$groupColumn] = JsonStore::escapedColumn($groupColumn) . " {$type} COMMENT 'Link to parent'"; } } $columns[$indexColumn] = JsonStore::escapedColumn($indexColumn) . " INT(11) COMMENT 'Array index'"; } foreach ($config['columns'] as $columnSpec => $columnName) { $subConfig = $columnName; if (is_numeric($columnSpec)) { $columnSpec = $columnName; } if (isset($config['alias']) && isset($config['alias'][$columnName])) { $columnName = $config['alias'][$columnName]; } $parts = explode("/", $columnSpec, 2); $type = $parts[0]; $path = substr($columnSpec, strlen($type)); $comment = $columnSpec; if ($type == "array") { $parentKeyType = NULL; if (isset($subConfig['parentKey'])) { foreach ($config['columns'] as $cs => $cn) { if (is_numeric($cs)) { $cs = $cn; } if ($cn == $subConfig['parentKey']) { $keyParts = explode("/", $cs, 2); $keyType = $keyParts[0]; $parentKeyType = $keyType; } } foreach ($config['alias'] as $cs => $cn) { if ($cn == $subConfig['parentKey']) { $keyParts = explode("/", $cs, 2); $keyType = $keyParts[0]; $parentKeyType = $keyType; } } checkConfig($subConfig, TRUE, $parentKeyType); continue; } else { checkConfig($subConfig, TRUE, NULL); $type == " INT(11)"; } } else { if (isset($mysqlTypeMap[$type])) { $type = $mysqlTypeMap[$type]; } else { $type = "TEXT /*{$type}*/"; } } if ($columnSpec == $config['keyColumn']) { $columns[$columnName] = JsonStore::escapedColumn($columnName) . " {$type} AUTO_INCREMENT PRIMARY KEY COMMENT " . JsonStore::mysqlQuote($comment); } else { $columns[$columnName] = JsonStore::escapedColumn($columnName) . " {$type} NULL COMMENT " . JsonStore::mysqlQuote($comment); } } if (isset($_POST['create-tables'])) { $sql = "CREATE TABLE IF NOT EXISTS {$config['table']} ("; $sql .= "\n\t" . implode(",\n\t", $columns) . "\n)"; echo "{$sql};\n"; JsonStore::mysqlQuery($sql); } $result = JsonStore::mysqlQuery("SHOW COLUMNS FROM {$config['table']}"); $observedColumns = array(); foreach ($result as $column) { $observedColumns[$column['Field']] = $column; } if (isset($_POST['update-tables-add'])) { foreach ($columns as $columnName => $column) { if (!isset($observedColumns[$columnName])) { $sql = "ALTER TABLE {$config['table']}\n\tADD COLUMN " . $column; echo "{$sql};\n"; JsonStore::mysqlQuery($sql); } } } if (isset($_POST['update-tables-delete'])) { foreach ($observedColumns as $columnName => $column) { if (!isset($columns[$columnName])) { $sql = "ALTER TABLE {$config['table']}\n\tDROP COLUMN " . $columnName; echo "{$sql};\n"; JsonStore::mysqlQuery($sql); } } } }