Browse Source

generate json serialization for sp dbo

tags/0.1.8^0
Robin Thoni 8 years ago
parent
commit
92e7bd82c6
1 changed files with 54 additions and 4 deletions
  1. 54
    4
      src/Generator/sp.php.twig

+ 54
- 4
src/Generator/sp.php.twig View File

7
 
7
 
8
 namespace {{ sp_namespace.as_it }};
8
 namespace {{ sp_namespace.as_it }};
9
 
9
 
10
-use Luticate\Utils\LuSpModel;
10
+use Luticate\Utils\LuSpDbo;
11
 use Luticate\Utils\LuMultipleDbo;
11
 use Luticate\Utils\LuMultipleDbo;
12
 use Luticate\Utils\LuStringUtils;
12
 use Luticate\Utils\LuStringUtils;
13
 use Illuminate\Support\Facades\DB;
13
 use Illuminate\Support\Facades\DB;
14
 
14
 
15
-class {{ sp.sp_name.camel_upper }} extends LuSpModel {
15
+class {{ sp.sp_name.camel_upper }} extends LuSpDbo {
16
 
16
 
17
+    /**
18
+    * @param $dam
19
+    * @return \{{ sp_namespace.as_it }}\{{ sp.sp_name.camel_upper }}|null
20
+    */
17
     protected static function damToDbo($dam)
21
     protected static function damToDbo($dam)
18
     {
22
     {
19
         if (is_null($dam))
23
         if (is_null($dam))
21
         $dbo = new {{ sp.sp_name.camel_upper }}();
25
         $dbo = new {{ sp.sp_name.camel_upper }}();
22
 
26
 
23
 {% for arg in args.out %}
27
 {% for arg in args.out %}
24
-        $dbo->set{{ arg.name.camel_upper }}($dam->{{ arg.name.as_it }});
28
+        $dbo->set{{ arg.name.camel_upper }}(LuStringUtils::convertJsonString($dam->{{ arg.name.as_it }}));
25
 {% endfor %}
29
 {% endfor %}
26
 
30
 
27
         return $dbo;
31
         return $dbo;
30
 {% set spcall %}{{ sp.sp_name.as_it }}({% for arg in args.in %}:{{ arg.name.as_it }}{{ loop.last ? "" : ", " }}{% endfor %}){% endset %}
34
 {% set spcall %}{{ sp.sp_name.as_it }}({% for arg in args.in %}:{{ arg.name.as_it }}{{ loop.last ? "" : ", " }}{% endfor %}){% endset %}
31
 {% set argsarray %}{% for arg in args.in %}":{{ arg.name.as_it }}" => ${{ arg.name.as_it }}{{ loop.last ? "" : ", " }}{% endfor %}{% endset %}
35
 {% set argsarray %}{% for arg in args.in %}":{{ arg.name.as_it }}" => ${{ arg.name.as_it }}{{ loop.last ? "" : ", " }}{% endfor %}{% endset %}
32
 
36
 
37
+    /**
38
+{% for arg in args.in %}
39
+    * @param ${{ arg.name.as_it }} {{ arg.data_type.php.as_it }}
40
+{% endfor %}
41
+    * @return \{{ sp_namespace.as_it }}\{{ sp.sp_name.camel_upper }}{% if sp.proretset %}[]{% endif %};
42
+    */
33
     public static function execute({% for arg in args.in %}${{ arg.name.as_it }}{{ loop.last ? "" : ", " }}{% endfor %})
43
     public static function execute({% for arg in args.in %}${{ arg.name.as_it }}{{ loop.last ? "" : ", " }}{% endfor %})
34
     {
44
     {
35
-        $values = DB::select('SELECT * FROM {{ spcall }}', array({{ argsarray }}));
45
+        $values = DB::select('SELECT {% for arg in args.out %}to_json(data.{{ arg.name.as_it }}) AS {{ arg.name.as_it }}{{ loop.last ? "" : ", " }}{% endfor %} FROM {{ spcall }} data', array({{ argsarray }}));
36
 {% if sp.proretset %}
46
 {% if sp.proretset %}
37
         $dboValues = array();
47
         $dboValues = array();
38
         foreach ($values as $value)
48
         foreach ($values as $value)
44
     }
54
     }
45
 
55
 
46
 {% if sp.proretset %}
56
 {% if sp.proretset %}
57
+    /**
58
+{% for arg in args.in %}
59
+    * @param ${{ arg.name.as_it }} {{ arg.data_type.php.as_it }}
60
+{% endfor %}
61
+    * @param $page int The page number, 0 based
62
+    * @param $perPage int The number of items per page
63
+    * @return \Luticate\Utils\LuMultipleDbo;
64
+    */
47
     public static function getMultipleJson({% for arg in args.in %}${{ arg.name.as_it }}, {% endfor %}$page, $perPage)
65
     public static function getMultipleJson({% for arg in args.in %}${{ arg.name.as_it }}, {% endfor %}$page, $perPage)
48
     {
66
     {
49
         $values = DB::select('SELECT (SELECT count(*) FROM {{ spcall }}) as count, (SELECT json_agg(q) FROM (SELECT * FROM {{ spcall }} OFFSET (:page::int * :perPage::int) LIMIT :perPage) q) as data',
67
         $values = DB::select('SELECT (SELECT count(*) FROM {{ spcall }}) as count, (SELECT json_agg(q) FROM (SELECT * FROM {{ spcall }} OFFSET (:page::int * :perPage::int) LIMIT :perPage) q) as data',
58
     }
76
     }
59
 {% endif %}
77
 {% endif %}
60
 
78
 
79
+    public function jsonSerialize()
80
+    {
81
+        return array(
82
+{% for arg in args.out %}
83
+            "{{ arg.name.camel_upper }}" => $this->_{{ arg.name.camel_lower }}{{ loop.last ? "" : "," }}
84
+{% endfor %}
85
+        );
86
+    }
87
+
88
+    public static function jsonDeserialize($json)
89
+    {
90
+        $dbo = new {{ sp.sp_name.camel_upper }}();
91
+{% for arg in args.out %}
92
+        if (isset($json["{{ arg.name.camel_upper }}"])) {
93
+            $dbo->set{{ arg.name.camel_upper }}($json["{{ arg.name.camel_upper }}"]);
94
+        }
95
+{% endfor %}
96
+        return $dbo;
97
+    }
98
+
99
+    public static function generateSample()
100
+    {
101
+        $dbo = new {{ sp.sp_name.camel_upper }}();
102
+{% for arg in args.out %}
103
+        $dbo->set{{ arg.name.camel_upper }}({% if arg.data_type.php.as_it == "double" %}42.42{%
104
+elseif arg.data_type.php.as_it == "integer" %}42{%
105
+elseif arg.data_type.php.as_it == "boolean" %}true{%
106
+else %}"sample string"{% endif %});
107
+{% endfor %}
108
+        return $dbo;
109
+    }
110
+
61
 {% for arg in args.out %}
111
 {% for arg in args.out %}
62
 
112
 
63
     /**
113
     /**

Loading…
Cancel
Save