| 1 | # ABSTRACT: JsonSQL 'insert' JSON schema. | ||
| 2 | |||
| 3 | |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | package JsonSQL::Schemas::insert; | ||
| 9 | |||
| 10 | 2 | our $VERSION = '0.4'; # VERSION | |
| 11 | |||
| 12 | 2 | ▶ | use base qw( JsonSQL::Schemas::Schema ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | 2 | my $jsonSchema = ' | |
| 17 | { | ||
| 18 | "title": "SQL Insert Schema", | ||
| 19 | "id": "sqlInsertSchema", | ||
| 20 | "$schema": "http://json-schema.org/draft-04/schema#", | ||
| 21 | "description": "JSON schema to describe an SQL INSERT", | ||
| 22 | "type": "object", | ||
| 23 | "properties": { | ||
| 24 | "defaultschema": { | ||
| 25 | "type": "string", | ||
| 26 | "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$" | ||
| 27 | }, | ||
| 28 | "inserts": { | ||
| 29 | "type": "array", | ||
| 30 | "items": { | ||
| 31 | "type": "object", | ||
| 32 | "properties": { | ||
| 33 | "table": {"$ref": "#/definitions/sqlTableObject"}, | ||
| 34 | "values": { | ||
| 35 | "type": "array", | ||
| 36 | "items": {"$ref": "#/definitions/sqlInsertValueObject"}, | ||
| 37 | "minItems": 1 | ||
| 38 | }, | ||
| 39 | "returning": { | ||
| 40 | "type": "array", | ||
| 41 | "items": { | ||
| 42 | "type": "object", | ||
| 43 | "properties" : { | ||
| 44 | "column": {"type": "string", "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"}, | ||
| 45 | "as": {"type": "string", "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$"} | ||
| 46 | }, | ||
| 47 | "additionalProperties": false, | ||
| 48 | "required": ["column"] | ||
| 49 | }, | ||
| 50 | "minItems": 1 | ||
| 51 | } | ||
| 52 | }, | ||
| 53 | "required": ["table", "values"] | ||
| 54 | }, | ||
| 55 | "minItems": 1 | ||
| 56 | } | ||
| 57 | }, | ||
| 58 | "additionalProperties": false, | ||
| 59 | "required": ["inserts"], | ||
| 60 | "definitions": { | ||
| 61 | "sqlTableObject": { | ||
| 62 | "type": "object", | ||
| 63 | "properties": { | ||
| 64 | "schema": { | ||
| 65 | "type": "string", | ||
| 66 | "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$" | ||
| 67 | }, | ||
| 68 | "table": { | ||
| 69 | "type": "string", | ||
| 70 | "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$" | ||
| 71 | } | ||
| 72 | }, | ||
| 73 | "additionalProperties": false, | ||
| 74 | "required": ["table"] | ||
| 75 | }, | ||
| 76 | "sqlInsertValueObject": { | ||
| 77 | "type": "object", | ||
| 78 | "properties": { | ||
| 79 | "column": { | ||
| 80 | "type": "string", | ||
| 81 | "pattern": "^[a-zA-Z_][a-zA-Z0-9_]*$" | ||
| 82 | }, | ||
| 83 | "value": { | ||
| 84 | "oneOf": [ | ||
| 85 | {"type": "string"}, | ||
| 86 | {"type": "number"} | ||
| 87 | ] | ||
| 88 | } | ||
| 89 | }, | ||
| 90 | "additionalProperties": false, | ||
| 91 | "required": ["column", "value"] | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | '; | ||
| 96 | |||
| 97 | |||
| 98 | |||
| 99 | sub new { | ||
| 100 | 7 | ▶ | my $class = shift; |
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||
| 101 | |||
| 102 | 7 | my $self = $class->SUPER::new(); | |
| 103 | |||
| 104 | 7 | $self->{_json} = $jsonSchema; | |
| 105 | |||
| 106 | 7 | return $self; | |
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | 2 | 1; | |
| 111 | |||
| 112 | __END__ | ||
| 113 | |||
| 114 | =pod | ||
| 115 | |||
| 116 | =encoding UTF-8 | ||
| 117 | |||
| 118 | =head1 NAME | ||
| 119 | |||
| 120 | JsonSQL::Schemas::insert - JsonSQL 'insert' JSON schema. | ||
| 121 | |||
| 122 | =head1 VERSION | ||
| 123 | |||
| 124 | version 0.4 | ||
| 125 | |||
| 126 | =head1 SYNOPSIS | ||
| 127 | |||
| 128 | This is a JSON schema describing an SQL INSERT statement. The main "inserts" property is an array, allowing support for batching | ||
| 129 | of multiple INSERT statements into one query. For each INSERT, the table and values parameters are specified as separate properties. | ||
| 130 | There is rudimentary support for the RETURNING clause if your database supports it. | ||
| 131 | |||
| 132 | You can instantiate this directly, but it is better to use the load_schema dispatcher from L<JsonSQL::Schemas::Schema>. | ||
| 133 | |||
| 134 | To use this: | ||
| 135 | |||
| 136 | my $schema = JsonSQL::Schemas::insert->new; | ||
| 137 | if ( eval { $schema->is_error } ) { | ||
| 138 | return "Could not load JSON schema: $schema->{message}"; | ||
| 139 | } else { | ||
| 140 | my $schemaObj = parse_json($schema->{_json}); | ||
| 141 | ... | ||
| 142 | } | ||
| 143 | |||
| 144 | For this to be useful, you will have to create a JSON::Validator object to validate parsed JSON strings, or just use L<JsonSQL::Validator>. | ||
| 145 | |||
| 146 | =head1 ATTRIBUTES | ||
| 147 | |||
| 148 | =head2 $jsonSchema | ||
| 149 | |||
| 150 | The INSERT schema as a JSON string. | ||
| 151 | |||
| 152 | =head1 METHODS | ||
| 153 | |||
| 154 | =head2 Constructor new -> JsonSQL::Schemas::insert | ||
| 155 | |||
| 156 | Constructor method to return the $jsonSchema as a property of a new instance of this object. | ||
| 157 | |||
| 158 | =head1 AUTHOR | ||
| 159 | |||
| 160 | Chris Hoefler <bhoefler@draper.com> | ||
| 161 | |||
| 162 | =head1 COPYRIGHT AND LICENSE | ||
| 163 | |||
| 164 | This software is copyright (c) 2017 by Chris Hoefler. | ||
| 165 | |||
| 166 | This is free software; you can redistribute it and/or modify it under | ||
| 167 | the same terms as the Perl 5 programming language system itself. | ||
| 168 | |||
| 169 | =cut | ||