Example #1
0
 function execute()
 {
     global $osC_Cache;
     if (isset($this->cache_key)) {
         if ($osC_Cache->read($this->cache_key, $this->cache_expire)) {
             $this->cache_data = $osC_Cache->getCache();
             $this->cache_read = true;
         }
     }
     if ($this->cache_read === false) {
         if ($this->db_class->use_foreign_keys == false) {
             $query_action = substr($this->sql_query, 0, strpos($this->sql_query, ' '));
             if ($query_action == 'delete' || $query_action == 'update') {
                 if (empty($this->db_class->fkeys)) {
                     $Qfk = new self($this->db_class);
                     $Qfk->setQuery('select * from :table_fk_relationships');
                     $Qfk->bindTable(':table_fk_relationships', TABLE_FK_RELATIONSHIPS);
                     //              $Qfk->setCache('fk_relationships');
                     $Qfk->execute();
                     while ($Qfk->next()) {
                         $this->db_class->fkeys[$Qfk->value('to_table')][] = array('from_table' => $Qfk->value('from_table'), 'from_field' => $Qfk->value('from_field'), 'to_field' => $Qfk->value('to_field'), 'on_update' => $Qfk->value('on_update'), 'on_delete' => $Qfk->value('on_delete'));
                     }
                     $Qfk->freeResult();
                 }
             }
             if ($query_action == 'delete') {
                 $query_data = split(' ', $this->sql_query, 4);
                 $query_table = substr($query_data[2], strlen(DB_TABLE_PREFIX));
                 if (isset($this->db_class->fkeys[$query_table])) {
                     // check for RESTRICT constraints first
                     foreach ($this->db_class->fkeys[$query_table] as $fk) {
                         if ($fk['on_delete'] == 'restrict') {
                             $child_query = $this->db_class->simpleQuery('select ' . $fk['to_field'] . ' from ' . $query_data[2] . ' ' . $query_data[3]);
                             while ($child_result = $this->db_class->next($child_query)) {
                                 $Qcheck = new self($this->db_class);
                                 $Qcheck->setQuery('select ' . $fk['from_field'] . ' from ' . DB_TABLE_PREFIX . $fk['from_table'] . ' where ' . $fk['from_field'] . ' = "' . $child_result[$fk['to_field']] . '" limit 1');
                                 $Qcheck->execute();
                                 if ($Qcheck->numberOfRows() === 1) {
                                     $this->db_class->setError('RESTRICT constraint condition from table ' . DB_TABLE_PREFIX . $fk['from_table'], null, $this->sql_query);
                                     return false;
                                 }
                             }
                         }
                     }
                     foreach ($this->db_class->fkeys[$query_table] as $fk) {
                         $parent_query = $this->db_class->simpleQuery('select * from ' . $query_data[2] . ' ' . $query_data[3]);
                         while ($parent_result = $this->db_class->next($parent_query)) {
                             if ($fk['on_delete'] == 'cascade') {
                                 $Qdel = new self($this->db_class);
                                 $Qdel->setQuery('delete from :from_table where :from_field = :' . $fk['from_field']);
                                 $Qdel->bindTable(':from_table', DB_TABLE_PREFIX . $fk['from_table']);
                                 $Qdel->bindRaw(':from_field', $fk['from_field'], false);
                                 $Qdel->bindValue(':' . $fk['from_field'], $parent_result[$fk['to_field']]);
                                 if ($this->logging === true) {
                                     if ($this->db_class->logging_transaction === false) {
                                         $this->db_class->logging_transaction = true;
                                     }
                                     $Qdel->setLogging($this->logging_module, $this->logging_module_id);
                                 }
                                 $Qdel->execute();
                             } elseif ($fk['on_delete'] == 'set_null') {
                                 $Qupdate = new self($this->db_class);
                                 $Qupdate->setQuery('update :from_table set :from_field = :' . $fk['from_field'] . ' where :from_field = :' . $fk['from_field']);
                                 $Qupdate->bindTable(':from_table', DB_TABLE_PREFIX . $fk['from_table']);
                                 $Qupdate->bindRaw(':from_field', $fk['from_field'], false);
                                 $Qupdate->bindRaw(':' . $fk['from_field'], 'null');
                                 $Qupdate->bindRaw(':from_field', $fk['from_field'], false);
                                 $Qupdate->bindValue(':' . $fk['from_field'], $parent_result[$fk['to_field']], false);
                                 if ($this->logging === true) {
                                     if ($this->db_class->logging_transaction === false) {
                                         $this->db_class->logging_transaction = true;
                                     }
                                     $Qupdate->setLogging($this->logging_module, $this->logging_module_id);
                                 }
                                 $Qupdate->execute();
                             }
                         }
                     }
                 }
             } elseif ($query_action == 'update') {
                 $query_data = split(' ', $this->sql_query, 3);
                 $query_table = substr($query_data[1], strlen(DB_TABLE_PREFIX));
                 if (isset($this->db_class->fkeys[$query_table])) {
                     // check for RESTRICT constraints first
                     foreach ($this->db_class->fkeys[$query_table] as $fk) {
                         if ($fk['on_update'] == 'restrict') {
                             $child_query = $this->db_class->simpleQuery('select ' . $fk['to_field'] . ' from ' . $query_data[2] . ' ' . $query_data[3]);
                             while ($child_result = $this->db_class->next($child_query)) {
                                 $Qcheck = new self($this->db_class);
                                 $Qcheck->setQuery('select ' . $fk['from_field'] . ' from ' . DB_TABLE_PREFIX . $fk['from_table'] . ' where ' . $fk['from_field'] . ' = "' . $child_result[$fk['to_field']] . '" limit 1');
                                 $Qcheck->execute();
                                 if ($Qcheck->numberOfRows() === 1) {
                                     $this->db_class->setError('RESTRICT constraint condition from table ' . DB_TABLE_PREFIX . $fk['from_table'], null, $this->sql_query);
                                     return false;
                                 }
                             }
                         }
                     }
                     foreach ($this->db_class->fkeys[$query_table] as $fk) {
                         // check to see if foreign key column value is being changed
                         if (strpos(substr($this->sql_query, strpos($this->sql_query, ' set ') + 4, strpos($this->sql_query, ' where ') - strpos($this->sql_query, ' set ') - 4), ' ' . $fk['to_field'] . ' ') !== false) {
                             $parent_query = $this->db_class->simpleQuery('select * from ' . $query_data[1] . substr($this->sql_query, strrpos($this->sql_query, ' where ')));
                             while ($parent_result = $this->db_class->next($parent_query)) {
                                 if ($fk['on_update'] == 'cascade' || $fk['on_update'] == 'set_null') {
                                     $on_update_value = '';
                                     if ($fk['on_update'] == 'cascade') {
                                         $on_update_value = $this->logging_fields[$fk['to_field']];
                                     }
                                     $Qupdate = new self($this->db_class);
                                     $Qupdate->setQuery('update :from_table set :from_field = :' . $fk['from_field'] . ' where :from_field = :' . $fk['from_field']);
                                     $Qupdate->bindTable(':from_table', DB_TABLE_PREFIX . $fk['from_table']);
                                     $Qupdate->bindRaw(':from_field', $fk['from_field'], false);
                                     if (empty($on_update_value)) {
                                         $Qupdate->bindRaw(':' . $fk['from_field'], 'null');
                                     } else {
                                         $Qupdate->bindValue(':' . $fk['from_field'], $on_update_value);
                                     }
                                     $Qupdate->bindRaw(':from_field', $fk['from_field'], false);
                                     $Qupdate->bindValue(':' . $fk['from_field'], $parent_result[$fk['to_field']], false);
                                     if ($this->logging === true) {
                                         if ($this->db_class->logging_transaction === false) {
                                             $this->db_class->logging_transaction = true;
                                         }
                                         $Qupdate->setLogging($this->logging_module, $this->logging_module_id);
                                     }
                                     $Qupdate->execute();
                                 }
                             }
                         }
                     }
                 }
             }
         }
         if ($this->logging === true) {
             $this->logging_action = substr($this->sql_query, 0, strpos($this->sql_query, ' '));
             if ($this->logging_action == 'update') {
                 $db = split(' ', $this->sql_query, 3);
                 $this->logging_database = $db[1];
                 $test = $this->db_class->simpleQuery('select ' . implode(', ', array_keys($this->logging_fields)) . ' from ' . $this->logging_database . substr($this->sql_query, strrpos($this->sql_query, ' where ')));
                 while ($result = $this->db_class->next($test)) {
                     foreach ($this->logging_fields as $key => $value) {
                         if ($result[$key] != $value) {
                             $this->logging_changed[] = array('key' => $this->logging_database . '.' . $key, 'old' => $result[$key], 'new' => $value);
                         }
                     }
                 }
             } elseif ($this->logging_action == 'insert') {
                 $db = split(' ', $this->sql_query, 4);
                 $this->logging_database = $db[2];
                 foreach ($this->logging_fields as $key => $value) {
                     $this->logging_changed[] = array('key' => $this->logging_database . '.' . $key, 'old' => '', 'new' => $value);
                 }
             } elseif ($this->logging_action == 'delete') {
                 $db = split(' ', $this->sql_query, 4);
                 $this->logging_database = $db[2];
                 $del = $this->db_class->simpleQuery('select * from ' . $this->logging_database . ' ' . $db[3]);
                 while ($result = $this->db_class->next($del)) {
                     foreach ($result as $key => $value) {
                         $this->logging_changed[] = array('key' => $this->logging_database . '.' . $key, 'old' => $value, 'new' => '');
                     }
                 }
             }
         }
         $this->query_handler = $this->db_class->simpleQuery($this->sql_query, $this->debug);
         if ($this->logging === true) {
             if ($this->db_class->logging_transaction_action === false) {
                 $this->db_class->logging_transaction_action = $this->logging_action;
             }
             if ($this->affectedRows($this->query_handler) > 0) {
                 if (!empty($this->logging_changed)) {
                     if ($this->logging_action == 'insert' && !is_numeric($this->logging_module_id)) {
                         $this->logging_module_id = $this->db_class->nextID();
                         $this->setNextID($this->logging_module_id);
                     }
                     if (class_exists('osC_AdministratorsLog_Admin')) {
                         osC_AdministratorsLog_Admin::insert($this->logging_module, $this->db_class->logging_transaction_action, $this->logging_module_id, $this->logging_action, $this->logging_changed, $this->db_class->logging_transaction);
                     }
                 }
             }
         }
         if ($this->batch_query === true) {
             $this->batch_size = $this->db_class->getBatchSize($this->sql_query, $this->batch_select_field);
             $this->batch_to = $this->batch_rows * $this->batch_number;
             if ($this->batch_to > $this->batch_size) {
                 $this->batch_to = $this->batch_size;
             }
             $this->batch_from = $this->batch_rows * ($this->batch_number - 1);
             if ($this->batch_to == 0) {
                 $this->batch_from = 0;
             } else {
                 $this->batch_from++;
             }
         }
         return $this->query_handler;
     }
 }