|
| 1 | +const helper = require('think-helper'); |
| 2 | +const {Schema} = require('think-model-abstract'); |
| 3 | +const Debounce = require('think-debounce'); |
| 4 | +const Query = require('./query.js'); |
| 5 | +const Parser = require('./parser.js'); |
| 6 | + |
| 7 | +const QUERY = Symbol('think-model-sqlite-query'); |
| 8 | +const PARSER = Symbol('think-model-sqlite-parser'); |
| 9 | +const GET_SCHEMA = Symbol('think-model-sqlite-schema'); |
| 10 | +const debounce = new Debounce(); |
| 11 | + |
| 12 | +/** |
| 13 | + * mysql Schema |
| 14 | + */ |
| 15 | +module.exports = class MysqlSchema extends Schema { |
| 16 | + /** |
| 17 | + * get query instance |
| 18 | + */ |
| 19 | + get query() { |
| 20 | + if (this[QUERY]) return this[QUERY]; |
| 21 | + this[QUERY] = new Query(this.config); |
| 22 | + return this[QUERY]; |
| 23 | + } |
| 24 | + /** |
| 25 | + * get parset instance |
| 26 | + */ |
| 27 | + get parser() { |
| 28 | + if (this[PARSER]) return this[PARSER]; |
| 29 | + this[PARSER] = new Parser(this.config); |
| 30 | + return this[PARSER]; |
| 31 | + } |
| 32 | + _getItemSchemaValidate(fieldData) { |
| 33 | + const validate = {}; |
| 34 | + switch (fieldData.tinyType) { |
| 35 | + case 'tinyint': |
| 36 | + validate.int = {min: 0, max: 255}; |
| 37 | + break; |
| 38 | + case 'smallint': |
| 39 | + validate.int = {min: fieldData.unsigned ? 0 : -32768, max: 32767}; |
| 40 | + break; |
| 41 | + case 'int': |
| 42 | + validate.int = {min: fieldData.unsigned ? 0 : -2147483648, max: 2147483647}; |
| 43 | + break; |
| 44 | + // case 'bigint': |
| 45 | + // validate.int = {min: fieldData.unique ? 0 : -9223372036854775808, max: 9223372036854775807}; |
| 46 | + // break; |
| 47 | + case 'date': |
| 48 | + validate.date = true; |
| 49 | + break; |
| 50 | + }; |
| 51 | + return validate; |
| 52 | + } |
| 53 | + _parseItemSchema(item) { |
| 54 | + const fieldData = { |
| 55 | + name: item.name, |
| 56 | + type: item.type, |
| 57 | + required: !!item.notnull, |
| 58 | + default: item.dflt_value || '', |
| 59 | + primary: !!item.pk, |
| 60 | + unique: item.unique, |
| 61 | + autoIncrement: false |
| 62 | + }; |
| 63 | + const pos = item.Type.indexOf('('); |
| 64 | + fieldData.tinyType = (pos === -1 ? item.Type : item.Type.slice(0, pos)).toLowerCase(); |
| 65 | + if (fieldData.default && fieldData.tinyType.indexOf('int') > -1) { |
| 66 | + fieldData.default = parseInt(fieldData.default); |
| 67 | + } |
| 68 | + if (item.Type.indexOf('unsigned') > -1) { |
| 69 | + fieldData.unsigned = true; |
| 70 | + } |
| 71 | + fieldData.validate = this._getItemSchemaValidate(fieldData); |
| 72 | + return fieldData; |
| 73 | + } |
| 74 | + /** |
| 75 | + * get table schema |
| 76 | + * @param {String} table |
| 77 | + */ |
| 78 | + getSchema(table = this.table) { |
| 79 | + const _getSchema = () => { |
| 80 | + return debounce.debounce(`getTable${table}Schema`, () => { |
| 81 | + table = this.parser.parseKey(table); |
| 82 | + const fieldsPromise = this.query.query(`PRAGMA table_info( ${table} )`); |
| 83 | + const indexesPromise = this.query.query(`PRAGMA INDEX_LIST( ${table} )`).then(list => { |
| 84 | + const indexes = {}; |
| 85 | + const promises = list.map(item => { |
| 86 | + if (item.unique) return; |
| 87 | + return this.query.query(`PRAGMA index_info( ${item.name} )`).then(data => { |
| 88 | + data.forEach(item => { |
| 89 | + indexes[item.name] = {unique: true}; |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + return Promise.all(promises).then(() => indexes); |
| 94 | + }); |
| 95 | + return Promise.all([fieldsPromise, indexesPromise]).then(([fields, indexes]) => { |
| 96 | + const ret = {}; |
| 97 | + fields.forEach(item => { |
| 98 | + item.unique = indexes[item.name].unique; |
| 99 | + ret[item.name] = this._parseItemSchema(item); |
| 100 | + }); |
| 101 | + return helper.extend(ret, this.schema); |
| 102 | + }); |
| 103 | + }); |
| 104 | + }; |
| 105 | + if (this[GET_SCHEMA] && this[GET_SCHEMA][table]) { |
| 106 | + return Promise.resolve(this[GET_SCHEMA][table]); |
| 107 | + } |
| 108 | + return _getSchema().then(data => { |
| 109 | + if (!this[GET_SCHEMA]) { |
| 110 | + this[GET_SCHEMA] = {}; |
| 111 | + } |
| 112 | + this[GET_SCHEMA][table] = data; |
| 113 | + return data; |
| 114 | + }); |
| 115 | + } |
| 116 | + /** |
| 117 | + * parse type |
| 118 | + * @param {String} tinyType |
| 119 | + * @param {Mixed} value |
| 120 | + */ |
| 121 | + parseType(tinyType, value) { |
| 122 | + if (tinyType === 'enum' || tinyType === 'set' || tinyType === 'bigint') return value; |
| 123 | + if (tinyType.indexOf('int') > -1) return parseInt(value, 10); |
| 124 | + if (['double', 'float', 'decimal'].indexOf(tinyType) > -1) return parseFloat(value, 10); |
| 125 | + if (tinyType === 'bool') return value ? 1 : 0; |
| 126 | + return value; |
| 127 | + } |
| 128 | +}; |
0 commit comments