JsonSQL::Query::Insert - JsonSQL::Query::Insert object. Stores a Perl representation of a set of INSERT statements created from a JSON string.
version 0.4
Use this to generate an SQL INSERT statement from a JSON string.
To use this:
use JsonSQL::Query::Insert; my $jsonString = '{ "inserts": [ { "table": {"table": "table1", "schema": "MySchema"}, "values": [ {"column": "column1", "value": "value1"}, {"column": "column2", "value": "value2"} ], "returning": [{"column": "column1", "as": "bestcolumn"}, {"column": "column2"}] }, { "table": {"table": "table2"}, "values": [ {"column": "columnA", "value": "valueA"}, {"column": "columnB", "value": "valueB"} ] } ] }'; my $whitelisting_rules = [ { schema => '#anySchema', 'table1' => [ 'column1', 'column2' ], 'table2' => [ 'columnA', 'columnB' ] } ]; my ( $insertObj, $err ) = JsonSQL::Query::Insert->new($whitelisting_rules, $jsonString); if ( $insertObj ) { my ( $sql, $binds ) = $insertObj->get_all_inserts; <...> } else { die $err; }
Now you can go ahead and use $sql and $binds directly with the DBI module to do the query.
This is a JsonSQL Query module that supports SQL generation for batched INSERT statements.
Examples of INSERT features supported by this module:
{ "inserts": [ { "table": {"table": "MyTable"}, "values": [ {"column": "Animal", "value": "Giraffe"}, {"column": "Color", "value": "Yellow/Brown"} ] } ] }
{ "inserts": [ { "table": {"table": "MyTable"}, "values": [ {"column": "Animal", "value": "Giraffe"}, {"column": "Color", "value": "Yellow/Brown"} ], "returning": [ {"column": "animal_id"} ] } ] }
{ "inserts": [ { "table": {"table": "MyTable"}, "values": [ {"column": "Animal", "value": "Giraffe"}, {"column": "Color", "value": "Yellow/Brown"} ] }, { "table": {"table": "MyTable"}, "values": [ {"column": "Animal", "value": "Elephant"}, {"column": "Color", "value": "Grey"} ] }, { "table": {"table": "MyTable"}, "values": [ {"column": "Animal", "value": "Horse"}, {"column": "Color", "value": "Black"} ] } ] }
The top-level property is the "inserts" property, which is an array of objects representing each INSERT. Each INSERT object has the following properties:
Generates: INSERT INTO 'table1' See L<JsonSQL::Param::Table> for more info.
Generates ('scientist','theory') VALUES (?,?) Bind: ['Einstein','Relativity'] See L<JsonSQL::Param::InsertValues> for more info.
Generates: RETURNING 'column_id'; See L<JsonSQL::Param::Insert> for more info.
If you are using DB schemas, this property can be used to generate the schema identifier for your queries. Particularly useful for per-user DB schemas.
See JsonSQL::Schemas::insert to view the restrictions enforced by the JSON schema.
A set of whitelisting rules is required to successfully use this module to generate SQL. See JsonSQL::Validator to learn how this works.
Instantiates and returns a new JsonSQL::Query::Insert object.
$query_rulesets => The whitelisting rules to validate the query with. $json_query => A stringified JSON object representing the query.
Returns (0, <error message>) on failure.
Generates the SQL statement represented by the object. Returns:
$sql => An arrayref of SQL INSERT strings. $binds => An arrayref of arrays of parameterized values to pass with each INSERT query.
Chris Hoefler <bhoefler@draper.com>
This software is copyright (c) 2017 by Chris Hoefler.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install JsonSQL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JsonSQL
CPAN shell
perl -MCPAN -e shell install JsonSQL
For more information on module installation, please visit the detailed CPAN module installation guide.