5 $objQueue = Singleton::getInstance('\DDDBL\Queue');
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
14 $cloStoreDBConnection = function(\DDDBL\Queue $objQueue, array $arrParameter) {
19 $objQueue->getState()->update(array('DB' => getDBDataObject()));
23 $objQueue->addHandler(QUEUE_GET_DB_CONNECTION_POSITION, $cloStoreDBConnection);
25 ###############################
26 ### query-definition-loader ###
27 ###############################
29 # get the DataObject of the query and store it in the queue
31 $cloGetQuery = function(\DDDBL\Queue $objQueue, array $arrParameter) {
33 $objDataObjectPool = new DataObjectPool('Query-Definition');
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!');
41 if(!$objDataObjectPool->exists($strAlias))
42 throw new \Exception("given query alias is unknown: $strAlias");
44 $objQueue->getState()->update(array('QUERY' => $objDataObjectPool->get($strAlias)));
48 $objQueue->addHandler(QUEUE_GET_QUERY_POSITION, $cloGetQuery);
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
59 $cloSetBindDataTypeConfig = function(\DDDBL\Queue $objQueue, array $arrParameter) {
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')));
78 $objQueue->addHandler(QUEUE_BIND_DATA_TYPE_POSITION, $cloSetBindDataTypeConfig);
84 # get the stored query and prepare() it for the given database-connection
85 # store the resulting PDOStatement
87 $cloPrepareQuery = function(\DDDBL\Queue $objQueue, array $arrParameter) {
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')));
101 $objQueue->addHandler(QUEUE_PREPARE_QUERY_POSITION, $cloPrepareQuery);
103 #########################
104 ### execute the query ###
105 #########################
107 # handler, which maps the data-type of a variable to the PDO-constants
109 $cloMapDataType = function($mixedParameter) {
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
128 $cloQueryExcecute = function(\DDDBL\Queue $objQueue, array $arrParameter) use ($cloMapDataType) {
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())
153 throw new QueryException($objPDO, $objQuery->getAll());
157 $objQueue->addHandler(QUEUE_EXECUTE_QUERY_POSITION, $cloQueryExcecute);
159 ###############################
160 ### format the query result ###
161 ###############################
163 # if a result-handler for the query is configured, call it
165 $cloFormatQueryResult = function(\DDDBL\Queue $objQueue, array $arrParameter) {
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));
180 $objDataObjectPool = new DataObjectPool('Result-Handler');
182 if(!$objDataObjectPool->exists($strHandler))
183 throw new \Exception ("unknown result-handler: $strHandler");
185 $objHandler = $objDataObjectPool->get($strHandler);
186 $cloHandler = $objHandler->get('HANDLER');
188 $cloHandler($objQueue, $strHandlerConfig);
192 $objQueue->addHandler(QUEUE_FORMAT_RESULT_POSITION, $cloFormatQueryResult);
198 # closing the cursor of the PDOStatement. this will free
199 # the result and enable the connection to execute the next query
201 $cloCloseCursor = function(\DDDBL\Queue $objQueue, array $arrParameter) {
203 $objQueryResult = $objQueue->getState()->get('PDOStatement');
204 $objQueryResult->closeCursor();
208 $objQueue->addHandler(QUEUE_CLOSE_CURSOR_POSITION, $cloCloseCursor);