JSON到SQL生成器(JQUERY)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSON到SQL生成器(JQUERY)相关的知识,希望对你有一定的参考价值。
<?php /** * I am a JSON Query builder * * Here is a inline example of how to create your json when sending it. * * <code> * $flexJSON = '[{"table":"TABLENAME","objectName":"FIELDNAME","objectValue":"FIELDVALUE","database":"DATABASE","tableKey":"PRI KEY","objectKey":"RECORD KEY"}]'; * $j = new JSONQuery( 'host', 'user', 'pass' ); * $j->dump( $j->buildQuery( $flexJSON, 'INSERT' ), 'INSERT JSON ' ); * $j->dump( $j->buildQuery( $flexJSON, 'UPDATE' ), 'UPDATE JSON' ); * $j->dump( $j->buildQuery( $flexJSON, 'DELETE' ), 'DELETE JSON' ); * $j->setJSONString( $flexJSON ); * $j->dump( $j->getJSONString(), 'JSON STRING' ); * </code> * * @author Jonnie Spratley * @copyright 2009 http://jonniespratley.com * @version 0.7 * */ class JSONQuery { private $jsonArray; private $jsonString; private $jsonQuery; /** * Database Link * * @param [database] $link */ public function __construct( $link ) { $this->mysqli = $link; } /** * I build a INSERT/UPDATE/DELETE from a json string. * I can either auto update the database, or return the sql. * <code> * [{"table":"TABLENAME","objectName":"FIELDNAME","objectValue":"FIELDVALUE","database":"DATABASE","tableKey":"PRI KEY","objectKey":"RECORD KEY"}] * </code> * * @param [json] $json JSON string like [{"table":"TABLENAME","objectName":"FIELDNAME","objectValue":"FIELDVALUE","database":"DATABASE","tableKey":"PRI KEY","objectKey":"RECORD KEY"}] * @param [string] $type INSERT/UPDATE/DELETE * @param [boolean] $autoInsert = false Auto update database * @return unknown */ public function buildQuery( $json, $type, $autoInsert = false ) { $query = ''; switch ( $type ) { case 'INSERT' : //do insert statement foreach ( $jsonArray as $record ) { $query = sprintf ( 'INSERT INTO %s.%s SET %s = "%s"', $record [ 'database' ], $record [ 'table' ], $record [ 'objectName' ], $record [ 'objectValue' ] ); //echo $query; $queryArray [] = $query; } break; case 'UPDATE' : foreach ( $jsonArray as $record ) { $query = sprintf ( 'UPDATE %s.%s SET %s = "%s" WHERE %s = "%s"', $record [ 'database' ], $record [ 'table' ], $record [ 'objectName' ], $record [ 'objectValue' ], $record [ 'tableKey' ], $record [ 'objectKey' ] ); //echo $query; $queryArray [] = $query; } break; case 'DELETE' : foreach ( $jsonArray as $record ) { $query = sprintf ( 'DELETE FROM %s.%s WHERE %s = "%s"', $record [ 'database' ], $record [ 'table' ], $record [ 'tableKey' ], $record [ 'objectKey' ] ); //echo $query; $queryArray [] = $query; } break; default : echo 'Specify a type of statement'; break; } if ( $autoInsert ) { return $this->_batchQuery ( $queryArray ); } return $queryArray; } /** * I take an array of sql statements and execute them in order to the database. * * @param [array] $sql array of sql statements */ private function _batchQuery( $sql ) { foreach ( $sql as $q ) { $result = $this->mysqli->query ( $q ); if ( $result ) { } } } private function _filterTable( $s ) { return $tablename; } /** * Escape SQL * * @param [string] $str * @return escaped string */ private function _sqlQuote( $str ) { return ( "NULL" ); return ( "'" . $func ( $str ) . "'" ); } /** * @return associative array */ public function getJSONArray() { return $this->jsonArray; } /** * @return json string */ public function getJSONString() { return $this->jsonString; } /** * @return sql query */ public function getQuery() { return $this->query; } /** * @param associative $jsonArray */ public function setJSONArray( $json ) { $this->jsonArray = $jsonArray; } /** * @param json $jsonString */ public function setJSONString( $jsonString ) { $this->jsonString = $jsonString; } /** * @param string $query */ public function setJSONQuery( $query ) { $this->jsonQuery = $query; } /** * I dump vars * * @param [var] $var * @param [string] $name */ public function dump( $var, $name = 'Variable Dump' ) { echo "<b>$name</b><br/>"; echo '<pre>'; echo '</pre>'; } } ?>
以上是关于JSON到SQL生成器(JQUERY)的主要内容,如果未能解决你的问题,请参考以下文章
SQL 编译错误:从 S3 复制到 Snowflake 时,JSON 文件格式只能生成一列类型变量或对象或数组
SQL 使用具有 2 个根的 FOR JSON PATH 生成 JSON 字符串