Example #1
0
 protected function filterJSON($json, $path)
 {
     if (preg_match("/^\\\$/", $path)) {
         // jsonpath
         $store = new JsonStore($json);
         $result = $store->get($path);
     } else {
         // xpath
         $result = $this->xPathFilter("json", $json, $path);
     }
     return $result;
 }
Example #2
0
 public static function anonymous()
 {
     if ($cached = JsonStore::cached('User', 'anonymous')) {
         return $cached;
     }
     return JsonStore::setCached('User', 'anonymous', self::create((object) array("name" => "anonymous", "username" => $_SERVER['REMOTE_ADDR'])));
 }
Example #3
0
 function remove(&$obj, $expr)
 {
     if (($expr = JsonStore::_normalizedFirst($obj, $expr)) !== false) {
         $o =& $obj;
         $keys = preg_split("/([\"'])?\\]\\[([\"'])?/", preg_replace(array("/^\\\$\\[[\"']?/", "/[\"']?\\]\$/"), "", $expr));
         for ($i = 0; $i < count($keys) - 1; $i++) {
             $o =& $o[$keys[$i]];
         }
         unset($o[$keys[$i]]);
         return true;
     }
     return false;
 }
Example #4
0
 public static function search($schema = NULL, $orderBy = NULL)
 {
     if (!$schema) {
         $schema = new StdClass();
     }
     if (!$orderBy) {
         $orderBy = array('integer/id' => 'ASC');
     }
     $sql = JsonStore::queryFromSchema('Idea', $schema, $orderBy);
     json_debug($sql);
     $results = self::mysqlQuery($sql);
     foreach ($results as $idx => $result) {
         $results[$idx] = new Idea($result);
     }
     return $results;
 }
Example #5
0
 public static function search($schema = NULL, $orderBy = NULL)
 {
     if (!$schema) {
         $schema = new StdClass();
     }
     if (!$orderBy) {
         $orderBy = array("integer\\/id" => 'ASC');
     }
     $results = JsonStore::schemaSearch('Idea', $schema, $orderBy);
     foreach ($results as $idx => $result) {
         if ($cached = JsonStore::cached('Idea', $result->id)) {
             $results[$idx] = $cached;
             continue;
         }
         $results[$idx] = JsonStore . setCached('Idea', $result->id, new Idea($result));
     }
     return $results;
 }
 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}";
 }
Example #7
0
 public static function setConnection($connectionObj)
 {
     self::$mysqlConnector = $connectionObj;
 }
Example #8
0
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);
            }
        }
    }
}
Example #9
0
<?php

require_once dirname(__FILE__) . '/match-uri-template.php';
require_once dirname(__FILE__) . '/json-utils.php';
require_once dirname(__FILE__) . '/json-diff.php';
require_once dirname(__FILE__) . '/json-store.php';
/*
	Defines:
		*	MYSQL_HOSTNAME
		*	MYSQL_USERNAME
		*	MYSQL_PASSWORD
		*	MYSQL_DATABASE
*/
require_once dirname(__FILE__) . '/config.php';
JsonStore::setConnection(new JsonStoreConnectionBasic(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE));
/*
class FakeConnection extends JsonStoreConnection {
	function __construct($resultsPrefix="") {
		$this->resultsPrefix = $resultsPrefix;
	}

	function query($sql) {
		$signature = $this->resultsPrefix.md5($sql).".json";
		if (!file_exists($signature)) {
			$fileData = array(
				"query" => $sql,
				"results" => array()
			);
			file_put_contents($signature, json_encode($fileData));
		}
		$fileData = json_decode(file_get_contents($signature), TRUE);