Permalink
Browse files

mysql_safe -> db_safe and docs

  • Loading branch information...
1 parent ba8b996 commit 728472112aac7a1890f3006d9526b92b5cb61c2f @jneen committed Apr 9, 2011
Showing with 24 additions and 5 deletions.
  1. +6 −1 lib/model.sh
  2. +18 −4 lib/util.sh
View
@@ -40,7 +40,7 @@ balls::model() {
balls::model.find() {
local query="$1"; shift
for param in "$@"; do
- mysql_safe param
+ db_safe param
query="$(sed "s/\?/$param/" <<<"$query")"
done
balls::model.execute "SELECT * from $(balls::model.table_name) WHERE $@"
@@ -51,6 +51,11 @@ balls::model.fields() {
cut -f1 # bah
}
+balls::model.fields() {
+ local fields_var="$model"_FIELDS
+ echo "${!fields_var}"
+}
+
balls::model.field_map() {
balls::model.fields | nl -nrz -ba
}
View
@@ -80,15 +80,29 @@ trim() {
trim_r "$@"
}
+# usage: db_safe my_var
+# will quote my_var for mysql.
+db_safe() {
+ local str="${!1}." # append a . so that bash doesn't chomp off newlines at the end
+ str="$(
+ echo "$str" | sed "s/'/\\\\'/g" | while read line; do echo -n "$line\\n"; done)"
+ # ^ escape ' escape \n - sed 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}'"
}
-mysql_safe() {
- local str="${!1}."
- str="$(echo "$str" | sed "s/'/\\\\'/g" | tr '\n' '\\n')"
- export "$1"="'${str:0:${#str}-1}'"
+join() {
+ local delim="$1"
+ read line && echo -n "$line"
+ while read line; do
+ echo -n "$delim"
+ echo -n "$line"
+ done
}

0 comments on commit 7284721

Please sign in to comment.