Skip to content

Commit 5f0e04f

Browse files
committed
Moved method parameters and table columns data from local method variable to model properties to also be able to use it for validate method
1 parent 9cc9ddd commit 5f0e04f

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

system/sqlp/sqlp.php

+41-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class Sqlp {
3636

3737
private $tree = [];
3838

39+
private $filters = [];
40+
41+
private $params = [];
42+
3943
private $db;
4044

4145
private $namespace;
@@ -414,17 +418,20 @@ function ($matches) {
414418
$filter[$name] = $column;
415419
}
416420

417-
$filterArray = var_export($filter, true);
421+
//$filterArray = var_export($filter, true);
418422
$return = ! empty($match['return']) ? $match['return'] : $match['data'];
419423
$return = '$params' . $this->sqlPhpArrayKey($return);
420424
$key = '$params' . $this->sqlPhpArrayKey($match['data']);
425+
$filterName = '$this->filters[\'' . $this->prefix . $match['columns'] . '\']';
421426

422-
$filterFunction = '\';' . TAB . '$filterArray = ' . $filterArray . ";\n" . TAB;
427+
//$filterFunction = '\';' . TAB . '$filterArray = ' . $filterArray . ";\n" . TAB;
428+
$filterFunction = '\';' . TAB;
429+
$this->filters[$this->prefix . $match['columns']] = $filter;
423430

424431
if ($isArray == 'true') {
425-
$filterFunction .= 'foreach ( ' . $key . ' as $key => &$filter) ' . $return . '[$key] = $this->db->filter($filter, $filterArray,' . $addMissingDefaults . ');' . TAB . '$sql = \'';
432+
$filterFunction .= 'if (isset(' . $key . ') && is_array(' . $key . ')) foreach ( ' . $key . ' as $key => &$filter) ' . $return . '[$key] = $this->db->filter($filter, ' . $filterName . ',' . $addMissingDefaults . ');' . TAB . '$sql = \'';
426433
} else {
427-
$filterFunction .= $return . '= $this->db->filter(' . $key . ', $filterArray,' . $addMissingDefaults . ');' . TAB . '$sql = \'';
434+
$filterFunction .= $return . '= $this->db->filter(' . $key . ', ' . $filterName . ',' . $addMissingDefaults . ');' . TAB . '$sql = \'';
428435
}
429436

430437
$statement = str_replace($match[0], $filterFunction, $statement);
@@ -452,6 +459,10 @@ function parseParameters($params) {
452459
$param['length'] = (int)preg_replace('/[^\d]/', '',$match[4]);
453460
}
454461

462+
if (isset($match[5])) {
463+
$param['comment'] = trim($match[5], " \n\r\t\v\x00-");
464+
}
465+
455466
$parameters[] = $param;
456467
}
457468
}
@@ -510,12 +521,14 @@ function ($m) {
510521

511522
//remove comments
512523
$sql = preg_replace('@(--.*)\s+@', '', $sql);
524+
$this->tree = [];
513525

514526
if (preg_match_all($this->config['functionRegex'], $sql, $matches, PREG_SET_ORDER)) {
515527
foreach ($matches as $match) {
516528
$method['name'] = trim($match['name'], '`"\'');
517529
//add slashes only for single quotes
518530
$method['statement'] = str_replace("'", "\'",trim($match['statement']));
531+
$method['statement'] = preg_replace('@(--.*)\s+@', '', $method['statement']);
519532

520533
$method['params'] = $this->parseParameters($match['params']);
521534

@@ -526,6 +539,10 @@ function ($m) {
526539
}
527540
}
528541

542+
function getModel() {
543+
return $this->tree;
544+
}
545+
529546
function generateModel() {
530547
$methods = '';
531548

@@ -614,12 +631,26 @@ function ($param) {
614631
}
615632
} ,$method['params'])), "\n\t");
616633
*/
634+
/*
617635
$method['param_types'] = 'array(' . trim(implode(', ', array_map(
618636
function ($param) {
619637
if ($param['in_out'] == 'IN' && ($type = $this->paramType($param['type']))) {
620638
return '\'' . $param['name'] . '\' => \'' . $type . '\'';
621639
}
622640
} ,$method['params'])), ', ') . ')';
641+
*/
642+
643+
$paramTypes = [];
644+
645+
foreach ($method['params'] as $param) {
646+
if ($param['in_out'] == 'IN' && ($type = $this->paramType($param['type']))) {
647+
$paramTypes[$param['name']] = $type;
648+
}
649+
}
650+
651+
$this->paramTypes[$method['name']] = $paramTypes;
652+
653+
$method['param_types'] = '$this->paramTypes[\'' . $method['name'] . '\']';
623654

624655
$method['fetch'] = $this->fetchType('fetch_all');
625656

@@ -661,10 +692,12 @@ function ($param) {
661692
}
662693

663694
$model = $this->template($this->model['model'],[
664-
'name' => ucfirst($this->modelName),
665-
'namespace' => ucfirst($this->namespace),
666-
'filename' => $this->filename,
667-
'methods' => $methods,
695+
'name' => ucfirst($this->modelName),
696+
'namespace' => ucfirst($this->namespace),
697+
'filename' => $this->filename,
698+
'methods' => $methods,
699+
'filters' => var_export($this->filters, true),
700+
'paramTypes' => var_export($this->paramTypes, true),
668701
]);
669702

670703
return $model;

0 commit comments

Comments
 (0)