123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184(* macaque : sql_public.ml
MaCaQue : Macros for Caml Queries
Copyright (C) 2009 Gabriel Scherer, Jérôme Vouillon
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; see the file LICENSE. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*)openSql_internalsopenSql_typesletsql_of_queryq=Sql_printers.string_of_query(Sql_flatten.flatten_queryq)letsql_of_viewv=sql_of_query(Selectv)letparsety=Sql_parsers.use_unsafe_parser(Sql_parsers.parser_of_typety)moduleValue=structletunit()=Atom(Unit()),Non_nullableTUnitletboolb=Atom(Boolb),Non_nullableTBoolletint16i=Atom(Int16i),Non_nullableTInt16letint32i=Atom(Int32i),Non_nullableTInt32letint64i=Atom(Int64i),Non_nullableTInt64letfloatx=Atom(Floatx),Non_nullableTFloatletstrings=Atom(Strings),Non_nullableTStringletbyteai=Atom(Byteai),Non_nullableTBytealettimei=Atom(Timei),Non_nullableTTimeletdatei=Atom(Datei),Non_nullableTDatelettimestampi=Atom(Timestampi),Non_nullableTTimestamplettimestamptzi=Atom(Timestamptzi),Non_nullableTTimestamptzletintervali=Atom(Intervali),Non_nullableTIntervalletint32_arrayjs=Atom(Int32_arrayjs),Non_nullableTInt32_arrayendtype'asequence=string*atom_typemoduleSequence=structletserialseq_name=seq_name,TInt32letbigserialseq_name=seq_name,TInt64letsequence=bigserialendmoduleOp=structopenSql_buildersletnullable(r,t)=r,matchtwith|Non_nullablet->Nullable(Somet)|Nullablet->Nullabletletnull=nullletpostfixopvalueop=Op([null_workaroundvalue],op,[])letis_nullvalue=postfixopvalue"IS NULL",Non_nullableTBoolletis_not_nullvalue=postfixopvalue"IS NOT NULL",Non_nullableTBoolletof_option=function|None->null|Somev->nullablevletsame_opop_str=op(funt->t)op_strletmono_optop_str=op(unify(Non_nullablet))op_strletpoly_opreturn_top_str=lettype_fun=function|Non_nullable_->Non_nullablereturn_t|Nullable_->Nullable(Somereturn_t)inoptype_funop_strtype'phantarith_op='phantbinary_opconstraint'phant=<in_t:#numeric_tas't;out_t:'t;..>letarithop=same_opoplet(+),(-),(/),(*)=arith"+",arith"-",arith"/",arith"*"type'phantcomp_op='phantbinary_opconstraint'phant=<out_t:bool_t;..>letcompop=poly_opTBooloplet(<),(<=),(<>),(=),(>=),(>)=comp"<",comp"<=",comp"<>",comp"=",comp">=",comp">"letis_distinct_fromab=fixed_op"IS DISTINCT FROM"ab(Non_nullableTBool)letis_not_distinct_fromab=fixed_op"IS NOT DISTINCT FROM"ab(Non_nullableTBool)letin'((_,t)asv)l=letchange_ty=function|Non_nullable_->Non_nullableTBool|NullableNone->NullableNone|Nullable(Some_)->Nullable(SomeTBool)inletv=null_workaroundvinletl=List.mapnull_workaroundlinlett=List.fold_left(funacc(_,x)->unifyaccx)tlinletdefault=(Atom(Boolfalse),Non_nullableTBool)inOpTuple(v,"IN",l,Somedefault),change_tyttype'phantlogic_op='phantbinary_opconstraint'phant=<in_t:#bool_tas't;out_t:'t;..>letlogicop=mono_opTBooloplet(&&),(||)=logic"AND",logic"OR"letprefixopopv=Op([],op,[null_workaroundv])letnot(value,typ)=prefixop"NOT"(value,typ),typletcountx=prefixop"count"x,Non_nullableTInt64letmin(v,t)=nullable(prefixop"min"(v,t),t)letmax(v,t)=nullable(prefixop"max"(v,t),t)letsum(v,t)=nullable(prefixop"sum"(v,t),t)letmd5(v,t)=prefixop"md5"(v,t),tletlabelseq_name=Atom(Stringseq_name),Non_nullableTStringletnextval(seq_name,typ)=prefixop"nextval"(labelseq_name),Non_nullabletypletcurrval(seq_name,typ)=prefixop"currval"(labelseq_name),Non_nullabletypletcurrent_timestampu=check_atom_type(get_typeu)TUnit;Op([],"current_timestamp",[]),Non_nullableTTimestamptzletlocaltimestampu=check_atom_type(get_typeu)TUnit;Op([],"localtimestamp",[]),Non_nullableTTimestampendmoduleTable_type=structlet_typet=function|true->Nullable(Somet)|false->Non_nullabletletboolean=_typeTBoolletsmallint=_typeTInt16letinteger=_typeTInt32letbigint=_typeTInt64letdouble=_typeTFloatlettext=_typeTStringletcitext=_typeTCIStringletbytea=_typeTBytealettime=_typeTTimeletdate=_typeTDatelettimestamp=_typeTTimestamplettimestamptz=_typeTTimestamptzletinterval=_typeTIntervalletint32_array=_typeTInt32_arrayendmoduleView=structopenSql_buildersletonet=view(simple_selectt)[][]endmoduleViewOp=structletbinopopv1v2={v1withdescr=unify_descrv1.descrv2.descr;data=View_op(v1.data,op,v2.data);}letunion=binop"UNION"letunion_all=binop"UNION ALL"letintersect=binop"INTERSECT"letintersect_all=binop"INTERSECT ALL"letexcept=binop"EXCEPT"letexcept_all=binop"EXCEPT ALL"endtype'anullable_data=<get:unit;t:'a;nul:nullable>ttype'anon_nullable_data=<get:unit;t:'a;nul:non_nullable>t