7 #############################
8 ### db-connection handler ###
9 #############################
11 # get (or first establish) connection to database
12 # and store the DataObject of the connection in the Queue-State
25 ###############################
26 ### query-definition-loader ###
27 ###############################
29 # get the DataObject of the query and store it in the queue
35 # get the first entry of the parameter-list; this is the query-alias
36 $strAlias = array_shift($arrParameter);
38 if(empty($strAlias) || !is_string($strAlias))
39 throw new \Exception(
'no query-alias defined!');
42 throw new \Exception(
"given query alias is unknown: $strAlias");
50 #################################
51 ### set BIND-DATA-TYPE option ###
52 #################################
54 # check if the query has a BIND-DATA-TYPE config.
55 # if not check if there is one given for the database-connection.
56 # if yes, store it as setting for the query, otherwise
57 # set false for this option
61 $objDB = $objQueue->getState()->get(
'DB');
62 $objQuery = $objQueue->getState()->get(
'QUERY');
64 # skip this step, if the query itselfs has its own
65 if($objQuery->exists(
'BIND-DATA-TYPE')) {
66 $objQuery->update(array(
'BIND-DATA-TYPE' => (
bool) $objQuery->get(
'BIND-DATA-TYPE'))); #bugfix
for php-bug #38409
70 # set type to false, if no config is available, otherwise use the given config
71 if(!$objDB->exists(
'BIND-DATA-TYPE'))
72 $objQuery->update(array(
'BIND-DATA-TYPE' =>
false));
74 $objQuery->update(array(
'BIND-DATA-TYPE' => (
bool) $objDB->get(
'BIND-DATA-TYPE')));
84 # get the stored query and prepare() it for the given database-connection
85 # store the resulting PDOStatement
89 # if query is not prepared yet, do this now
90 if(!$objQueue->getState()->get(
'QUERY')->exists(
'PDOStatement')) {
91 $objPDO = $objQueue->getState()->get(
'DB')->get(
'PDO');
92 $objPDO = $objPDO->prepare($objQueue->getState()->get(
'QUERY')->get(
'QUERY'));
93 $objQueue->getState()->get(
'QUERY')->update(array(
'PDOStatement' => $objPDO));
96 # copy reference of prepared statement into queue for execution
97 $objQueue->getState()->update(array(
'PDOStatement' => $objQueue->getState()->get(
'QUERY')->get(
'PDOStatement')));
103 #########################
104 ### execute the query ###
105 #########################
107 # handler, which maps the data-type of a variable to the PDO-constants
111 $arrDataTypeMap = array(
'NULL' => \PDO::PARAM_NULL,
112 'boolean' => \PDO::PARAM_BOOL,
113 'integer' => \PDO::PARAM_INT,
114 'string' => \PDO::PARAM_STR);
116 $strDataType = gettype($mixedParameter);
118 if(!isset($arrDataTypeMap[$strDataType]))
119 throw new \Exception (
"could not bind parameters data type - type is not supported by PDO: $strDataType");
121 return $arrDataTypeMap[$strDataType];
125 # bind the given parameter to the prepared statement,
126 # then set the fetch mode and execute the query
130 $objPDO = $objQueue->getState()->get(
'PDOStatement');
132 # remove the alias from the parameter list
133 array_shift($arrParameter);
135 $objQuery = $objQueue->getState()->get(
'QUERY');
137 if(
true === $objQuery->get(
'BIND-DATA-TYPE')) {
139 foreach($arrParameter AS $intIndex => $mixedParameter)
140 $objPDO->bindValue($intIndex + 1, $mixedParameter,
$cloMapDataType($mixedParameter));
144 foreach($arrParameter AS $intIndex => $mixedParameter)
145 $objPDO->bindValue($intIndex + 1, $mixedParameter);
149 $objPDO->setFetchMode(\PDO::FETCH_ASSOC);
151 # execute the query. if execution fails, throw an exception
152 if(!$objPDO->execute())
159 ###############################
160 ### format the query result ###
161 ###############################
163 # if a result-handler for the query is configured, call it
167 $objQuery = $objQueue->getState()->get(
'QUERY');
169 if(!$objQuery->exists(
'HANDLER'))
172 # get the handler and its config
173 $strHandlerConfig = $objQuery->get(
'HANDLER');
174 $arrHandlerConfig = preg_split(
'/\s+/', $strHandlerConfig);
175 $strHandler = array_shift($arrHandlerConfig);
177 # remove handler-name from config
178 $strHandlerConfig = trim(str_replace($strHandler,
'', $strHandlerConfig));
183 throw new \Exception (
"unknown result-handler: $strHandler");
186 $cloHandler = $objHandler->get(
'HANDLER');
188 $cloHandler($objQueue, $strHandlerConfig);
198 # closing the cursor of the PDOStatement. this will free
199 # the result and enable the connection to execute the next query
203 $objQueryResult = $objQueue->getState()->get(
'PDOStatement');
204 $objQueryResult->closeCursor();