-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added get_update_query #6
Open
mpfrenette
wants to merge
1
commit into
Joseffb:dev
Choose a base branch
from
mpfrenette:dev
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,6 +58,107 @@ public function get_model_path($class_name, $namespace = false, $db_type = false | |
return $path; | ||
} | ||
|
||
|
||
|
||
|
||
/** | ||
* get_select_query | ||
* | ||
* This function properly compiles an UPDATE query using events to modify the parameters. | ||
* | ||
* Please note that $updatedfields is a an associative array of fields and their new values. | ||
* | ||
* | ||
* @param mixed $table the table name or an array with the parameters | ||
* @param string $where the WHERE part of the query. Can either be with or without the WHERE. | ||
* Ignored if "WHERE" is found in previous parameters | ||
* @param array $updatedfields the field names associated to their new values. | ||
* @param string $limit the LIMIT part of the query. Can either be with or without the LIMIT. | ||
* Ignored if "LIMIT" is found in previous parameters | ||
* @return string returns the built query | ||
* @author: Martin-Pierre Frenette <[email protected]> | ||
*/ | ||
public function get_update_query( $table,$where = null, $updatedfields= array(), $limit = null){ | ||
|
||
if ( is_array($table) ){ | ||
if ( !empty($table['table']) && !is_array($table['table']) ){ | ||
$args = $table; | ||
$table = $args['table']; | ||
$where = !empty($args['where'])?$args['where']: null; | ||
$updatedfields = !empty($args['updatedfields'])?$args['updatedfields']: null; | ||
$limit = !empty($args['limit'])?$args['limit']: null; | ||
} | ||
else{ | ||
return false; | ||
} | ||
} | ||
|
||
$query = ''; | ||
|
||
// First, process the table and the select parameters. | ||
$table = $this->event->emit('controller_model_get_update_query_table_' . $table, $table); | ||
if ( strpos($table, 'UPDATE') === false){ | ||
$query .= 'UPDATE '. $table. ' '; | ||
} | ||
else{ | ||
// we have a select in the table section, which means that we might have a full query instead! | ||
$query .= $table. ' '; | ||
} | ||
|
||
// second, we process the SET | ||
if ( strpos($query, ' SET ') === false){ | ||
$updatedfields = $this->event->emit('controller_model_get_update_query_set_' . $table, $updatedfields); | ||
if (!empty($updatedfields) && is_array($updatedfields) && count($updatedfields) > 0) { | ||
|
||
$setvalues = array(); | ||
foreach($updatedfields as $key => $value) { | ||
$setvalues[] = $key.'="'.mysql_real_escape_string($value).'"'; | ||
} | ||
|
||
$query .= ' SET '; | ||
$query .= implode(',',$setvalues); | ||
} | ||
} | ||
|
||
|
||
// third, let's handle the where if there is one. | ||
$where = $this->event->emit('controller_model_get_update_query_where_' . $table, $where); | ||
if ( !empty($where)){ | ||
if ( strpos($query, 'WHERE') !== false){ | ||
// we already have a WHERE in our query!!! | ||
} | ||
else if ( strpos($where, 'WHERE')){ | ||
// we have a WHERE in our where, so let's just add it directly | ||
$query .= ' ' . $where; | ||
} | ||
else{ | ||
$query .= ' WHERE ' . $where; | ||
|
||
} | ||
} | ||
|
||
// finally, let's handle the LIMIT if there is one. | ||
$limit = $this->event->emit('controller_model_get_update_query_limit_' . $table, $limit); | ||
if ( !empty($limit)){ | ||
if ( strpos($query, 'LIMIT') !== false){ | ||
// we already have a LIMIT in our query!!! | ||
} | ||
else if ( strpos($limit, 'LIMIT')){ | ||
// we have a WHERE in our where, so let's just add it directly | ||
$query .= ' ' . $limit; | ||
} | ||
else{ | ||
$query .= ' LIMIT ' . $limit; | ||
|
||
} | ||
} | ||
$query = $this->event->emit('controller_model_get_update_query_query_' . $table, $query); | ||
|
||
return $query; | ||
} | ||
|
||
|
||
|
||
public function get_data_as_object(array $options) | ||
{ | ||
/* | ||
|
@@ -440,4 +541,4 @@ public function escape($value) | |
die(); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why you're using mysql_real_escape_string they're bad function use filter function or pdo 😛
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is getting re-evaluated as a whole. I agree we will move toward using the mappers built into f3 instead of recreating the wheel.