| Новости | FAQ | Авторы | Документация | В действии | Библиотека |
| Инструменты | Полезные ссылки | Хостинги | Скачать | Примеры | Форум |
lexR119 05.09.2011 13:37
################################################################################################################################
# $Id: mysql.p,v 1.24 2005/03/02 10:38:30 misha Exp $
################################################################################################################################
@CLASS
mysql
@USE
sql.p
@BASE
sql
###########################################################################
@auto[]
$server_name[mysql]
#end @auto[]
###########################################################################
@init[connect-string;params]
^BASE:init[$connect-string;$params]
#end @init[]
###########################################################################
###########################################################################
# РАБОТА С ДАТАМИ
###########################################################################
@today[]
$result[CURDATE()]
#end @today[]
###########################################################################
@now[]
$result[NOW()]
#end @now[]
###########################################################################
@year[source]
$result[YEAR($source)]
#end @year[]
###########################################################################
@month[source]
$result[MONTH($source)]
#end @month[]
###########################################################################
@day[source]
$result[DATE_FORMAT($source,'%d')]
#end @day[]
###########################################################################
@ymd[source]
$result[DATE_FORMAT($source,'%Y-%m-%d')]
#end @ymd[]
###########################################################################
@date_diff[t;dt_from;dt_to]
$result[^if(def $dt_to){TO_DAYS($dt_to)}{^now[]} - TO_DAYS($dt_from)]
#end @date_diff[]
###########################################################################
@date_sub[date;days]
$result[DATE_SUB(^if(def $date){$date}{^today[]},INTERVAL $days DAY)]
#end @date_sub[]
# функции, имеющие аналоги не во всех серверах
###########################################################################
#----- не работает, заменителя не знаю
@date_add[date;days]
$result[]
#end @date_add[]
###########################################################################
# нету у MSSQL
@date_format[source;format_string]
$result[DATE_FORMAT($source, '^if(def $format_string){$format_string}{%Y-%m-%d}')]
#end @date_format[]
###########################################################################
# РАБОТА С LAST_INSERT_ID()
###########################################################################
@last_insert_id[table]
$result(^int:sql{SELECT DISTINCT last_insert_id() FROM $table}[$.default{0}])
#end @last_insert_id[]
###########################################################################
@set_last_insert_id[table;field]
$result(^last_insert_id[$table])
^void:sql{UPDATE $table SET ^if(def $field){$field}{sort_order} = $result WHERE ${table}_id = $result}
#end @set_last_insert_id[]
###########################################################################
###########################################################################
# РАБОТА СО СТРОКАМИ
###########################################################################
@substring[source;pos;length]
$result[SUBSTRING($source,^if(def $pos){$pos}{1},^if(def $length){$length}{1})]
#end @substring[]
###########################################################################
@upper[field]
$result[UPPER($field)]
#end @upper[]
###########################################################################
@lower[field]
$result[LOWER($field)]
#end @lower[]
###########################################################################
@concat[params]
$result[CONCAT($params)]
#end @concat[]
###########################################################################
###########################################################################
# РАЗНОЕ
###########################################################################
@password[pass]
$result[PASSWORD($pass)]
#end @password[]
###########################################################################
@left_join[type;table;join_conditions;last]
^if(^type.lower[] eq "from"){
$result[LEFT JOIN $table ON ($join_conditions)]
}{
$result[1 = 1 ^if(!def $last){ AND}]
}
#end @left_join[]
###########################################################################
# перекрываем заглушку для того, чтобы для каждого запроса получать explain информацию
@_get_query_detail[type;query;sql_options][eres;cols]
^if(def $query && $type ne "void"){
^try{
$eres[^table::sql{explain $query}[^if($sql_options is "hash"){^if($sql_options.offset){$.offset($sql_options.offset)}^if($sql_options.limit){$.limit($sql_options.limit)}}]]
$cols[^eres.columns[]]
$result[EXPLAIN:^#0A^cols.menu{$cols.column}[^#09]^#0A^eres.menu{^cols.menu{$eres.[$cols.column]}[^#09]}[^#0A]]
}{
$result[]
$exception.handled(1)
}
}{
$result[]
}
#end @_get_query_detail[]