26#include "valueconverter.h"
43Value func_getpivotdata (valVector args, ValueCalc *calc, FuncExtra *);
46void RegisterDatabaseFunctions()
48 FunctionRepository* repo = FunctionRepository::self();
51 f = new Function ( "DAVERAGE", func_daverage);
55 f = new Function ( "DCOUNT", func_dcount);
59 f = new Function ( "DCOUNTA", func_dcounta);
63 f = new Function ( "DGET", func_dget);
67 f = new Function ( "DMAX", func_dmax);
71 f = new Function ( "DMIN", func_dmin);
75 f = new Function ( "DPRODUCT", func_dproduct);
79 f = new Function ( "DSTDEV", func_dstdev);
83 f = new Function ( "DSTDEVP", func_dstdevp);
87 f = new Function ( "DSUM", func_dsum);
91 f = new Function ( "DVAR", func_dvar);
95 f = new Function ( "DVARP", func_dvarp);
99 f = new Function ( "GETPIVOTDATA", func_getpivotdata);
100 f->setParamCount (2);
101 f->setAcceptArray ();
114 int cols = database. columns ();
115 for ( int i = 0; i < cols; ++i)
132 bool matches ( unsigned row);
134 void parse ( Value conds);
142 Value conds) : calc(vc), cond(0), rows(0), cols(0), db(database)
147DBConditions::~DBConditions () {
148 int count = rows*cols;
149 for ( int r = 0; r < count; ++r)
154void DBConditions::parse ( Value conds)
157 rows = conds. rows() - 1;
159 int count = rows*cols;
160 cond = new Condition* [count];
161 for ( int r = 0; r < count; ++r)
166 for ( int c = 0; c < cc; ++c)
169 int col = getFieldIndex (calc, conds. element (c, 0), db);
170 if (col < 0) continue;
173 for ( int r = 0; r < rows; ++r) {
176 int idx = r * cols + col;
177 if (cond[idx]) delete cond[idx];
178 cond[idx] = new Condition;
179 calc-> getCond (*cond[idx], cnd);
184bool DBConditions::matches ( unsigned row)
186 if (row >= db.rows() - 1)
190 for ( int r = 0; r < rows; ++r) {
193 for ( int c = 0; c < cols; ++c) {
194 int idx = r * cols + c;
195 if (!cond[idx]) continue;
196 if (!calc-> matches (*cond[idx], db.element (c, row + 1))) {
217 Value database = args[0];
218 Value conditions = args[2];
219 int fieldIndex = getFieldIndex (calc, args[1], database);
223 DBConditions conds (calc, database, conditions);
225 int rows = database. rows() - 1;
226 Value res( Value::Float );
227 for ( int r = 0; r < rows; ++r)
228 if (conds.matches (r)) {
232 res = calc-> add (res, val);
241 Value database = args[0];
242 Value conditions = args[2];
243 int fieldIndex = getFieldIndex (calc, args[1], database);
247 DBConditions conds (calc, database, conditions);
249 int rows = database. rows() - 1;
252 for ( int r = 0; r < rows; ++r)
253 if (conds.matches (r)) {
257 res = calc-> add (res, val);
261 if (count) res = calc->div (res, count);
268 Value database = args[0];
269 Value conditions = args[2];
270 int fieldIndex = getFieldIndex (calc, args[1], database);
274 DBConditions conds (calc, database, conditions);
276 int rows = database. rows() - 1;
278 for ( int r = 0; r < rows; ++r)
279 if (conds.matches (r)) {
286 return Value (count);
292 Value database = args[0];
293 Value conditions = args[2];
294 int fieldIndex = getFieldIndex (calc, args[1], database);
298 DBConditions conds (calc, database, conditions);
300 int rows = database. rows() - 1;
302 for ( int r = 0; r < rows; ++r)
303 if (conds.matches (r)) {
310 return Value (count);
316 Value database = args[0];
317 Value conditions = args[2];
318 int fieldIndex = getFieldIndex (calc, args[1], database);
322 DBConditions conds (calc, database, conditions);
326 int rows = database. rows() - 1;
327 for ( int r = 0; r < rows; ++r)
328 if (conds.matches (r)) {
334 result = database. element (fieldIndex, r + 1);
344 Value database = args[0];
345 Value conditions = args[2];
346 int fieldIndex = getFieldIndex (calc, args[1], database);
350 DBConditions conds (calc, database, conditions);
352 int rows = database. rows() - 1;
355 for ( int r = 0; r < rows; ++r)
356 if (conds.matches (r)) {
376 Value database = args[0];
377 Value conditions = args[2];
378 int fieldIndex = getFieldIndex (calc, args[1], database);
382 DBConditions conds (calc, database, conditions);
384 int rows = database. rows() - 1;
387 for ( int r = 0; r < rows; ++r)
388 if (conds.matches (r)) {
397 if (calc-> lower (val, res))
408 Value database = args[0];
409 Value conditions = args[2];
410 int fieldIndex = getFieldIndex (calc, args[1], database);
414 DBConditions conds (calc, database, conditions);
416 int rows = database. rows() - 1;
419 for ( int r = 0; r < rows; ++r)
420 if (conds.matches (r)) {
425 res = calc->mul (res, val);
437 return calc->sqrt (func_dvar (args, calc, 0));
444 return calc->sqrt (func_dvarp (args, calc, 0));
450 Value database = args[0];
451 Value conditions = args[2];
452 int fieldIndex = getFieldIndex (calc, args[1], database);
456 DBConditions conds (calc, database, conditions);
458 int rows = database. rows() - 1;
461 for ( int r = 0; r < rows; ++r)
462 if (conds.matches (r)) {
466 avg = calc-> add (avg, val);
471 avg = calc->div (avg, count);
474 for ( int r = 0; r < rows; ++r)
475 if (conds.matches (r)) {
479 res = calc-> add (res, calc->sqr (calc->sub (val, avg)));
483 return calc->div (res, count - 1);
489 Value database = args[0];
490 Value conditions = args[2];
491 int fieldIndex = getFieldIndex (calc, args[1], database);
495 DBConditions conds (calc, database, conditions);
497 int rows = database. rows() - 1;
500 for ( int r = 0; r < rows; ++r)
501 if (conds.matches (r)) {
505 avg = calc-> add (avg, val);
510 avg = calc->div (avg, count);
513 for ( int r = 0; r < rows; ++r)
514 if (conds.matches (r)) {
518 res = calc-> add (res, calc->sqr (calc->sub (val, avg)));
522 return calc->div (res, count);
527Value func_getpivotdata (valVector args, ValueCalc *calc, FuncExtra *)
529 Value database = args[0];
530 int fieldIndex = getFieldIndex (calc, args[1], database);
534 int row = database. rows() - 1;
536 return database. element (fieldIndex, row);
The ValueCalc class is used to perform all sorts of calculations.
bool matches(const Condition &cond, Value d) Returns true if value d matches the condition cond, built with getCond().
bool greater(const Value &a, const Value &b) numerical comparison
void getCond(Condition &cond, Value val) This method parses the condition in string text to the condition cond.
bool lower(const Value &a, const Value &b) numerical comparison
Value add(const Value &a, const Value &b) basic arithmetic operations
Provides a wrapper for cell value.
unsigned columns() const If this value is an array, return the number of columns.
Value element(unsigned column, unsigned row) const Return an element in the array value.
static const Value & errorDIV0() Returns constant reference to #DIV/0! error.
unsigned rows() const If this value is an array, return the number of rows.
bool isBoolean() const Returns true if the type of this value is Boolean.
bool isEmpty() const Returns true if empty.
bool isNumber() const Returns true if the type of this value is either integer or floating-point.
long asInteger() const Returns the integer value of this value.
TQString asString() const Returns the string value of this value.
static const Value & errorVALUE() Returns constant reference to #VALUE! error.
bool isString() const Returns true if the type of this value is string.
|