Exemplo n.º 1
0
 public function save($pkey_values = '', $pre_args = array(), $post_args = array())
 {
     // Check for pre_save()
     if (method_exists($this, 'pre_save')) {
         if (!is_array($pre_args)) {
             $pre_args = array($pre_args);
         }
         call_user_func_array(array($this, 'pre_save'), $pre_args);
     }
     $qa = array();
     $ret_val = false;
     // Set Table
     if (isset($this->schema)) {
         $qa['table'] = "{$this->schema}.{$this->table}";
     } else {
         $qa['table'] = $this->table;
     }
     foreach ($this->data as $field => &$value) {
         // Set to Save by default
         $save = true;
         // Check if field is not supposed to save
         if ($this->table_info[$field]['no_save']) {
             $save = false;
         } else {
             if ($value == '') {
                 // If Save Default set
                 if (isset($this->table_info[$field]['save_default'])) {
                     $value = $this->table_info[$field]['save_default'];
                 } else {
                     if (array_key_exists($this->table_info[$field]['data_type'], $this->save_default_types)) {
                         $value = $this->save_default_types[$this->table_info[$field]['data_type']];
                     } else {
                         if (isset($this->table_info[$field]['no_save_empty']) && $this->table_info[$field]['no_save_empty']) {
                             $save = false;
                         } else {
                             if (array_key_exists($this->table_info[$field]['data_type'], $this->no_save_empty_types)) {
                                 $save = false;
                             }
                         }
                     }
                 }
                 // If NULL
                 if (is_null($value)) {
                     $value = 'NULL';
                     $this->table_info[$field]['quotes'] = 'disable';
                 }
             }
         }
         // Check if field is not supposed to save
         if ($save) {
             // Use Bind Parameters
             if ($this->use_bind_params && $this->table_info[$field]['can_bind_param']) {
                 switch ($this->db_type) {
                     case 'mysqli':
                         $qa['fields'][$field] = '?';
                         $this->bind_param_count++;
                         $this->bind_params[] =& $value;
                         $tmp_type = isset($GLOBALS['mysql_bind_types'][strtoupper($this->table_info[$field]['data_type'])]) ? $GLOBALS['mysql_bind_types'][strtoupper($this->table_info[$field]['data_type'])] : 's';
                         $this->bind_params[0] .= $tmp_type;
                         break;
                     case 'sqlsrv':
                         $this->bind_param_count++;
                         $qa['fields'][$field] = '?';
                         $this->bind_params[] =& $value;
                         break;
                     case 'pgsql':
                         $this->bind_param_count++;
                         $tmp_param = '$' . $this->bind_param_count;
                         $qa['fields'][$field] = $tmp_param;
                         $this->bind_params[] = $value;
                         break;
                     case 'oracle':
                         $this->bind_param_count++;
                         $tmp_param = 'p' . $this->bind_param_count;
                         $qa['fields'][$field] = ':' . $tmp_param;
                         $this->bind_params[$tmp_param] = $value;
                         break;
                     case 'mssql':
                     case 'mysql':
                     case 'sqlite':
                         switch ($this->table_info[$field]['quotes']) {
                             // Force quotes
                             case 'force':
                                 $qa['fields'][$field] = "'{$value}'";
                                 break;
                                 // Disable quotes
                             // Disable quotes
                             case 'disable':
                                 $qa['fields'][$field] = $value;
                                 break;
                                 // Auto detect if quotes are needed
                             // Auto detect if quotes are needed
                             default:
                                 if (isset($this->quoted_types[$this->db_type][$this->table_info[$field]['data_type']])) {
                                     $qa['fields'][$field] = "'{$value}'";
                                 } else {
                                     $qa['fields'][$field] = $value;
                                 }
                                 break;
                         }
                         break;
                     case 'db2':
                         $qa['fields'][$field] = '?';
                         $this->bind_params[] = $value;
                         $this->bind_param_count++;
                         break;
                     default:
                 }
             } else {
                 switch ($this->table_info[$field]['quotes']) {
                     // Force quotes
                     case 'force':
                         $qa['fields'][$field] = "'{$value}'";
                         break;
                         // Disable quotes
                     // Disable quotes
                     case 'disable':
                         $qa['fields'][$field] = $value;
                         break;
                         // Auto detect if quotes are needed
                     // Auto detect if quotes are needed
                     default:
                         if (isset($this->quoted_types[$this->db_type][$this->table_info[$field]['data_type']])) {
                             $qa['fields'][$field] = "'{$value}'";
                         } else {
                             $qa['fields'][$field] = $value;
                         }
                         break;
                 }
             }
         }
     }
     // Set Query Type (insert or update)
     if (!empty($pkey_values)) {
         $qa['type'] = 'update';
         $qa['filter_phrase'] = $this->build_where($pkey_values);
     } else {
         $qa['type'] = 'insert';
     }
     $query = new data_query($qa);
     $strsql = $query->render();
     if ($this->print_trans) {
         $ret_val = $strsql;
         if ($this->use_bind_params) {
             ob_start();
             print "<br/><pre>\n";
             print_r($this->bind_params);
             print "</pre>\n";
             $ret_val .= ob_get_clean();
         }
     } else {
         // Create a new data transaction and execute query
         $data1 = new data_trans($this->data_source);
         // Use Bind Parameters
         if ($this->use_bind_params) {
             // Prepare Query
             $prep_status = $data1->prepare($strsql);
             // Execute Query
             $exec_status = $data1->execute($this->bind_params);
             $ret_val = $exec_status;
             // Reset Bind Variables
             $this->reset_bind_vars();
         } else {
             $query_result = $data1->data_query($strsql);
             $ret_val = $query_result;
         }
         // Lsat Insert ID if Insert Statement performed and a valid ID is returned
         if ($qa['type'] == 'insert') {
             $lii = $data1->last_insert_id();
             if ($lii !== false) {
                 $ret_val = $lii;
             }
         }
     }
     // Check for post_save()
     if (method_exists($this, 'post_save')) {
         if (!is_array($post_args)) {
             $post_args = array($post_args);
         }
         call_user_func_array(array($this, 'post_save'), $post_args);
     }
     return $ret_val;
 }