|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2016 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import {newError} from "./internal/error"; |
| 21 | + |
| 22 | +function generateFieldLookup( keys ) { |
| 23 | + let lookup = {}; |
| 24 | + keys.forEach( (name, idx) => { lookup[name] = idx; }); |
| 25 | + return lookup; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Records make up the contents of the {@link Result}, and is how you access |
| 30 | + * the output of a statement. A simple statement might yield a result stream |
| 31 | + * with a single record, for instance: |
| 32 | + * |
| 33 | + * MATCH (u:User) RETURN u.name, u.age |
| 34 | + * |
| 35 | + * This returns a stream of records with two fields, named `u.name` and `u.age`, |
| 36 | + * each record represents one user found by the statement above. You can access |
| 37 | + * the values of each field either by name: |
| 38 | + * |
| 39 | + * record.get("n.name") |
| 40 | + * |
| 41 | + * Or by it's position: |
| 42 | + * |
| 43 | + * record.get(0) |
| 44 | + * |
| 45 | + * @access public |
| 46 | + */ |
| 47 | +class Record { |
| 48 | + /** |
| 49 | + * Create a new record object. |
| 50 | + * @constructor |
| 51 | + * @access private |
| 52 | + * @param {Object} keys An array of field keys, in the order the fields appear |
| 53 | + * in the record |
| 54 | + * @param {Object} fields An array of field values |
| 55 | + * @param {Object} fieldLookup An object of fieldName -> value index, used to map |
| 56 | + * field names to values. If this is null, one will be |
| 57 | + * generated. |
| 58 | + */ |
| 59 | + constructor(keys, fields, fieldLookup=null ) { |
| 60 | + this.keys = keys; |
| 61 | + this.length = keys.length; |
| 62 | + this._fields = fields; |
| 63 | + this._fieldLookup = fieldLookup || generateFieldLookup( keys ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Run the given function for each field in this record. The function |
| 68 | + * will get three arguments - the value, the key and this record, in that |
| 69 | + * order. |
| 70 | + * |
| 71 | + * @param visitor |
| 72 | + */ |
| 73 | + forEach( visitor ) { |
| 74 | + for(let i=0;i<this.keys.length;i++) { |
| 75 | + visitor( this._fields[i], this.keys[i], this ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get a value from this record, either by index or by field key. |
| 81 | + * |
| 82 | + * @param {string|Number} key Field key, or the index of the field. |
| 83 | + * @returns {*} |
| 84 | + */ |
| 85 | + get( key ) { |
| 86 | + let index; |
| 87 | + if( !(typeof key === "number") ) { |
| 88 | + index = this._fieldLookup[key]; |
| 89 | + if( index === undefined ) { |
| 90 | + throw newError("This record has no field with key '"+key+"', available key are: [" + this.keys + "]."); |
| 91 | + } |
| 92 | + } else { |
| 93 | + index = key; |
| 94 | + } |
| 95 | + |
| 96 | + if( index > this._fields.length - 1 || index < 0 ) { |
| 97 | + throw newError("This record has no field with index '"+index+"'. Remember that indexes start at `0`, " + |
| 98 | + "and make sure your statement returns records in the shape you meant it to."); |
| 99 | + } |
| 100 | + |
| 101 | + return this._fields[index]; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export {Record} |
0 commit comments