多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
5.4 KiB

6 years ago
"use strict";
7 years ago
exports.findOrInsertObjectArrayByKey = (obj, key, array) => {
let found = 0;
let i = 0;
for (i = 0; i < array.length; ++i) {
if (key in array[i]) {
if (obj[key] === array[i][key]) {
found = 1;
break;
}
}
}
if (!found) {
6 years ago
array.push(obj);
7 years ago
}
return array[i];
};
exports.findObjectInArrayByKey = (key, value, objArray) => {
for (let i = 0; i < objArray.length; ++i) {
if (objArray[i][key] === value) {
return objArray[i];
}
}
return null;
};
6 years ago
exports.round = function(number, precision) {
var factor = Math.pow(10, precision);
var tempNumber = number * factor;
var roundedTempNumber = Math.round(tempNumber);
return roundedTempNumber / factor;
};
exports.numberRound = (number, precision) => {
var factor = Math.pow(10, precision);
var tempNumber = number * factor;
var roundedTempNumber = Math.round(tempNumber);
return roundedTempNumber / factor;
6 years ago
};
6 years ago
exports.numberGetLength = number => {
var n = number;
if (number < 0) {
n = n * -1;
}
return n.toString().length;
6 years ago
};
6 years ago
exports.numberGetFixed = number => {
//console.log(number, typeof number);
6 years ago
return parseInt(number.toFixed());
};
6 years ago
exports.getStepArraySimple = function(min, max, step) {
var arr = [];
for (var i = min; i <= max; i = i + step) {
6 years ago
arr.push(i);
}
return arr;
};
exports.getStepArray = (min, max, stddev) => {
// console.log(' = = = = = = = ');
//console.log('original numbers', min, max, stddev);
6 years ago
min = this.numberGetFixed(min);
max = this.numberGetFixed(max);
stddev = this.numberGetFixed(stddev);
// console.log('fixed numbers', min, max, stddev);
6 years ago
let minMinusHalf = min - stddev / 2;
let maxMinusHalf = max + stddev / 2;
6 years ago
minMinusHalf = this.numberGetFixed(minMinusHalf);
maxMinusHalf = this.numberGetFixed(maxMinusHalf);
// console.log('fixed numbers + (min,max)', min, max, stddev, '(', minMinusHalf, ',', maxMinusHalf, ')');
// console.log('numbers length', 'min', numberGetLength(min), 'max', numberGetLength(max), 'stddev', numberGetLength(stddev));
6 years ago
let minLen = this.numberGetLength(minMinusHalf);
let maxLen = this.numberGetLength(maxMinusHalf);
let stddevLen = this.numberGetLength(stddev);
//
// console.log('- - - -');
// console.log('Range', 'min', numberRound(minMinusHalf, -1));
// console.log('Range', 'max', numberRound(maxMinusHalf, -1));
// console.log('Range', 'stddev', numberRound(stddev, -1));
6 years ago
if (minLen > 1) minMinusHalf = this.numberRound(minMinusHalf, -1);
6 years ago
if (maxLen > 2) maxMinusHalf = this.numberRound(maxMinusHalf, -1);
6 years ago
if (stddevLen !== 1) stddev = this.numberRound(stddev, -1);
6 years ago
var arr = [];
for (var step = minMinusHalf; step < maxMinusHalf; step = step + stddev) {
6 years ago
arr.push(step);
}
6 years ago
arr.push(maxMinusHalf);
// console.log(arr);
return arr;
7 years ago
};
6 years ago
exports.getSchemaQuery = function() {
return (
"select c.table_name, c.column_name, c.ordinal_position,c.column_key,c.is_nullable, c.data_type, c.column_type,c.extra,c.privileges, " +
"c.column_comment,c.column_default,c.data_type,c.character_maximum_length, " +
"k.constraint_name, k.referenced_table_name, k.referenced_column_name, " +
"s.index_name,s.seq_in_index, " +
"v.table_name as isView " +
"from " +
"information_schema.columns as c " +
"left join " +
"information_schema.key_column_usage as k " +
"on " +
"c.column_name=k.column_name and " +
"c.table_schema = k.referenced_table_schema and " +
"c.table_name = k.table_name " +
"left join " +
"information_schema.statistics as s " +
"on " +
"c.column_name = s.column_name and " +
"c.table_schema = s.index_schema and " +
"c.table_name = s.table_name " +
"LEFT JOIN " +
"information_schema.VIEWS as v " +
"ON " +
"c.table_schema = v.table_schema and " +
"c.table_name = v.table_name " +
"where " +
"c.table_schema=? " +
"order by " +
"c.table_name, c.ordinal_position"
);
};
6 years ago
exports.getRoutines = function () {
return 'select routine_name from information_schema.routines'
}
6 years ago
exports.getChartQuery = function() {
return "select ? as ??, count(*) as _count from ?? where ?? between ? and ? ";
};
exports.getDataType = function(colType, typesArr) {
// console.log(colType,typesArr);
for (let i = 0; i < typesArr.length; ++i) {
if (colType.indexOf(typesArr[i]) !== -1) {
return 1;
}
}
return 0;
6 years ago
};
6 years ago
exports.getColumnType = function(column) {
let strTypes = [
"varchar",
"text",
"char",
"tinytext",
"mediumtext",
"longtext",
"blob",
"mediumblob",
"longblob",
"tinyblob",
"binary",
"varbinary"
];
let intTypes = ["int", "long", "smallint", "mediumint", "bigint", "tinyint"];
let flatTypes = ["float", "double", "decimal"];
let dateTypes = ["date", "datetime", "timestamp", "time", "year"];
//console.log(column);
6 years ago
if (this.getDataType(column["data_type"], strTypes)) {
return "string";
} else if (this.getDataType(column["data_type"], intTypes)) {
return "int";
} else if (this.getDataType(column["data_type"], flatTypes)) {
return "float";
} else if (this.getDataType(column["data_type"], dateTypes)) {
return "date";
} else {
6 years ago
return "string";
}
6 years ago
};
exports.getType = function(colType, typesArr) {
for (let i = 0; i < typesArr.length; ++i) {
// if (typesArr[i].indexOf(colType) !== -1) {
// return 1;
// }
if (colType.indexOf(typesArr[i]) !== -1) {
return 1;
}
}
return 0;
6 years ago
};