1+ <?php
2+
3+ /**
4+ * JSONDB - JSON Database Manager
5+ *
6+ * Manage local databases with JSON files and JSON Query Language (JQL)
7+ *
8+ * This content is released under the MIT License (MIT)
9+ *
10+ * Copyright (c) 2016, Centers Technologies
11+ *
12+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13+ * of this software and associated documentation files (the "Software"), to deal
14+ * in the Software without restriction, including without limitation the rights
15+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+ * copies of the Software, and to permit persons to whom the Software is
17+ * furnished to do so, subject to the following conditions:
18+ *
19+ * The above copyright notice and this permission notice shall be included in
20+ * all copies or substantial portions of the Software.
21+ *
22+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+ * THE SOFTWARE.
29+ *
30+ * @package JSONDB
31+ * @author Nana Axel
32+ * @copyright Copyright (c) 2016, Centers Technologies
33+ * @license http://opensource.org/licenses/MIT MIT License
34+ * @filesource
35+ */
36+
37+ namespace JSONDB ;
38+
39+ /**
40+ * Class PreparedQueryStatement
41+ *
42+ * @package JSONDB
43+ * @subpackage Utilities
44+ * @category Query
45+ * @author Nana Axel
46+ */
47+ class PreparedQueryStatement
48+ {
49+ /**
50+ * The JSONDB instance
51+ * @var JSONDB
52+ */
53+ private $ database ;
54+
55+ /**
56+ * The query string.
57+ * @var string
58+ */
59+ private $ queryString ;
60+
61+ /**
62+ * An array of keys inserted in the
63+ * query.
64+ * @var array
65+ */
66+ private $ preparedQueryKeys = array ();
67+
68+ /**
69+ * PreparedQueryStatement __constructor.
70+ * @param string $query
71+ * @param JSONDB $database
72+ */
73+ public function __construct ($ query , &$ database )
74+ {
75+ $ this ->queryString = $ query ;
76+ $ this ->database = &$ database ;
77+
78+ $ this ->_prepareQuery ();
79+ }
80+
81+ /**
82+ * Binds a value in a prepared query.
83+ * @param string $key The parameter's key
84+ * @param string|int|bool $value The parameter's value
85+ * @param int $parse_method The parse method to use
86+ * @throws Exception
87+ */
88+ public function bindValue ($ key , $ value , $ parse_method = JSONDB ::PARAM_STRING )
89+ {
90+ if ($ this ->database ->queryIsPrepared ()) {
91+ if (in_array ($ key , $ this ->preparedQueryKeys , TRUE )) {
92+ switch ($ parse_method ) {
93+ default :
94+ case JSONDB ::PARAM_STRING :
95+ $ value = JSONDB ::quote ((string )$ value );
96+ break ;
97+
98+ case JSONDB ::PARAM_INT :
99+ $ value = (int )$ value ;
100+ break ;
101+
102+ case JSONDB ::PARAM_BOOL :
103+ $ value = ((string )((int )$ value )) . ':JSONDB::TO_BOOL: ' ;
104+ break ;
105+
106+ case JSONDB ::PARAM_NULL :
107+ $ value = (string )$ value . ':JSONDB::TO_NULL: ' ;
108+ break ;
109+ }
110+ $ this ->queryString = str_replace ($ key , $ value , $ this ->queryString );
111+ } else {
112+ throw new Exception ("JSONDB Error: Can't bind the value \"{$ value }\" for the key \"{$ key }\". The key isn't in the query. " );
113+ }
114+ } else {
115+ throw new Exception ("JSONDB Error: Can't use JSONDB::bindValue() with non prepared queries. Send your query with JSONDB::prepare() first. " );
116+ }
117+ }
118+
119+ /**
120+ * Execute the prepared query
121+ * @throws Exception
122+ * @return mixed
123+ */
124+ public function execute ()
125+ {
126+ if ($ this ->database ->queryIsPrepared ()) {
127+ return $ this ->database ->query ($ this ->queryString );
128+ } else {
129+ throw new Exception ("JSONDB Error: Can't execute the prepared query. There is no prepared query to execute or the prepared query is already executed. " );
130+ }
131+ }
132+
133+ /**
134+ * Prepare a query
135+ * @return PreparedQueryStatement
136+ */
137+ private function _prepareQuery ()
138+ {
139+ $ query = $ this ->queryString ;
140+ preg_match_all ('#(:[\w]+)# ' , $ query , $ keys );
141+ $ this ->preparedQueryKeys = $ keys [0 ];
142+ }
143+
144+ }
0 commit comments