Breadcrumbs

JsonSQL

If you are dealing with e.g. row objects, which represent SQL table contents, it is tedious to create INSERT statements, detect differences in two versions of the same row object, and create UPDATE statements for it. Especially for tables with many columns, the resulting Action Script is not really maintainable. This library provides means to

  • compare to row objects and see if they differ, and where

  • automatically create INSERT, UPDATE, SELECT WHERE and COUNT WHERE statements

For this to work, the library accepts JSON representations of the row objects, so basically operates on Strings. Using the zjsonpatch Java library, which is an implementation of RFC 6902 JSON Patch, introspection, then becomes possible.

JsonSQL offers methods to create INSERTUPDATE, SELECT, and COUNT statements based on JSON representations of row objects, and a JsonSQLConfig instance capturing required meta information. Support database types are SQLServer and MySQL only.

Operation signatures

jsondiff-class-jsonsql.PNG

JsonSQLConfig Attributes

Name

Type

Mandatory

Description

Allowed Values / Example

tableName

String

https://scheer-pas-doc.atlassian.net/wiki/s/1919486055/6452/c8d220627d4ff438dbc4c2b41ff70b9a00a36d21/_/images/icons/emoticons/check.png

The name of the table to put into the SQL statements.

MyTable

dbType

String

https://scheer-pas-doc.atlassian.net/wiki/s/1919486055/6452/c8d220627d4ff438dbc4c2b41ff70b9a00a36d21/_/images/icons/emoticons/check.png

One of the valid database type (same as used in the SQL Adapter).

SQLServer

Use an SQL Server database (default).

MySQL

Use a MySQL database.

whereColumns

Array of String

https://scheer-pas-doc.atlassian.net/wiki/s/1919486055/6452/c8d220627d4ff438dbc4c2b41ff70b9a00a36d21/_/images/icons/emoticons/check.png

An array of strings containing the names of columns to be used in the WHERE clause when creating UPDATE, SELECT, and COUNT statements.


columnTypes

Array of String


An optional array of strings defining the names and types of specific columns.

The library uses JSON type information and pattern matching to derive the column types

  • JSON distinguishes number, string, and boolean types

  • The library detects DateTime and Blob values (both JSON string type) by pattern matching.

  • Blob types are detected only if the value has more than 50 characters. In general you should indicate Blob columns to be safe.

However, you may want to store a DateTime value into a String type column, or the guessing and pattern matching may fail to work. In these cases you can provide the correct type information for such columns.

  • The Strings going into the columnTypes Array are of the form "<column name>:<bridge type>", e.g. "myDateColumn:DateTime"

  • Supported base types are String, Boolean, Float, Integer, DateTime, and Blob.


JsonSQL Methods

Method

Description

createInsert(json, config)

Creates an INSERT statement based on an object's JSON representation and a config. keyColumns in the configuration are ignored.

createUpdateFromVersions(oldVersion, newVersion, config)

Creates an UPDATE statement based on the row currently stored in the DB (oldVersion) and the row after some changes have been made (newVersion).

createUpdateFromDiff(oldVersion, diff, config)

Creates an UPDATE statement based on the row currently stored in the DB (oldVersion) and the JSON diff to the row after some changes have been applied.

createUpdateFromVersions(oldVersion, newVersion, config) == createUpdateFromDiff(oldVersion, jsonDiff(oldVersion, newVersion), config)

Examples

Please find some examples below. The generated SQL statements are terminated with a ;.

JsonDiff:calcDiff

Input

Library call

Output

jsondiff-example-calc-diff-input.PNG
jsondiff-example-calc-diff-call.PNG
jsondiff-example-calc-diff-output.PNG

JsonSQL:createUpdateFromVersions

Input

Library call

Output

jsondiff-example-update-from-versions-input.PNG
jsondiff-example-update-from-versions-call.PNG
jsondiff-example-update-from-versions-output.PNG

JsonSQL:createUpdateFromDiff

Input

Library call

Output

jsondiff-example-update-from-diff-input.PNG
jsondiff-example-calc-diff-call.PNG
jsondiff-example-update-from-diff-output.PNG

JsonSQL:createInsert

Input

Library call

Output

jsondiff-example-insert-input.PNG
jsondiff-example-insert-call.PNG
jsondiff-example-insert-output.PNG

JsonSQL:createCount

Input

Library call

Output

jsondiff-example-count-input.PNG

set sql = jsonSql:createCount(obj.classToJson(), config);

jsondiff-example-count-output.PNG

JsonSQL:createSelect

Input

Library call

Output

jsondiff-example-select-input.PNG

set sql = jsonSql:createSelect(obj.classToJson(), config);

jsondiff-example-select-output.PNG

JsonSQL with custom column type

(compare input/output with line above)

Input

Library call

Output

jsondiff-example-typed-insert-input.PNG
jsondiff-example-insert-call.PNG
jsondiff-example-typed-insert-output.PNG
📗

Related Pages:

📘