Permalink
Browse files

inspired by @ScriptFanix, use bash parameter substitution instead of

sed.

Also, actually execute the calculated query instead of just ignoring it
:X
  • Loading branch information...
1 parent 8e342fc commit 7cb559fe62c8234d47de98fe4db66164fe98f471 @jneen committed Aug 5, 2011
Showing with 8 additions and 7 deletions.
  1. +2 −2 lib/model.sh
  2. +6 −5 lib/util.sh
View
@@ -42,9 +42,9 @@ balls::model.find() {
local query="$1"; shift
for param in "$@"; do
db_safe param
- query="$(sed "s/\?/$param/" <<<"$query")"
+ query="${query/\?/$param}"
done
- balls::model.execute "SELECT * from $(balls::model.table_name) WHERE $@"
+ balls::model.execute "SELECT * from $(balls::model.table_name) WHERE $query"
}
balls::model.fetch_fields() {
View
@@ -104,18 +104,19 @@ trim() {
# will quote my_var for mysql.
db_safe() {
local str="${!1}." # append a . so that bash doesn't chomp off newlines at the end
+ local escaped_quote="\\'"
str="$(
- echo "$str" | sed "s/'/\\\\'/g" | while read line; do echo -n "$line\\n"; done)"
- # ^ escape ' escape \n - sed has trouble with this one.
+ echo "${str//\'/$escaped_quote}" | while read line; do echo -n "$line\\n"; done)"
+ # ^ escape ' escape \n - bash has trouble with this one.
export "$1"="'${str:0:${#str}-1}'" # enclose in single quotes, strip off the ., and export the variable
}
# escape ' with '\''. sorry everyone.
bash_safe() {
local str="${!1}."
- str="$(echo "$str" | sed "s/'/'\\\\''/g")"
- # escape ' with (literally) '\'' - sorry everyone
- export "$1"="'${str:0:${#str}-1}'"
@notwa

notwa May 23, 2013

this line was never rewritten. the trailing . will show in compiled views.

@jneen

jneen May 23, 2013

Owner

derp. Good catch :)

+ # escape ' with (literally) '\'' - sorry everyone
+ local escaped_quote="'\\''"
+ export "$1"="'${str//\'/$escaped_quote}'"
}
join() {

1 comment on commit 7cb559f

I did not have time to test it, otherwise i would have sent a pull request :)

Also, I recently understood how file descriptors and FIFO work, i will try to have Balls connect once per page to the database server, instead of once per request.

Please sign in to comment.