123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106(* Copyright (C) 2014--2017 Petter A. Urkedal <paurkedal@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at your
* option) any later version, with the OCaml static compilation exception.
*
* 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*)exceptionMissing_query_stringletformat_query?envsqllang=letn=String.lengthsqlinletbuf=Buffer.createninletadd_substring=matchenvwith|None->Buffer.add_substringbufsql|Someenv->funin->Buffer.add_substitutebuf(envlang)(String.subsqlin)inletrecskip_quotedj=ifj=ntheninvalid_arg("format_query: Unmatched quote: "^sql)elseifsql.[j]<>'\''thenskip_quoted(j+1)elseifj+1<n&&sql.[j+1]='\''thenskip_quoted(j+2)elsej+1inletreclooppij=ifj=nthenadd_substringi(j-i)elsematchsql.[j]with|'\''->add_substringi(j-i);letk=skip_quoted(j+1)inBuffer.add_substringbufsqlj(k-j);looppkk|'?'whenlang=`Pgsql->add_substringi(j-i);Printf.bprintfbuf"$%d"(p+1);loop(p+1)(j+1)(j+1)|_->looppi(j+1)inloop000;Buffer.contentsbuftypeoneshot_query=Caqti_driver_info.t->stringtypeprepared_query={pq_index:int;pq_name:string;pq_encode:Caqti_driver_info.t->string;}typequery=|Oneshotofoneshot_query|Preparedofprepared_queryletoneshot_fullf=Oneshotfletoneshot_funf=Oneshot(fundi->f(Caqti_driver_info.dialect_tagdi))letoneshot_anys=Oneshot(fun_->s)letoneshot_sqls=oneshot_fun@@function|#Caqti_driver_info.sql_dialect_tag->s|_->raiseMissing_query_stringletoneshot_sql_p?envsql=oneshot_fun@@function|#Caqti_driver_info.sql_dialect_tagaslang->format_query?envsqllang|_->raiseMissing_query_stringletnext_prepared_index=ref0letprepare_full?namepq_encode=letpq_index=!next_prepared_indexinnext_prepared_index:=succ!next_prepared_index;letpq_name=matchnamewith|None->"_s"^(string_of_intpq_index)|Somename->nameinPrepared{pq_index;pq_name;pq_encode}letprepare_fun?namef=prepare_full?name(fundi->f(Caqti_driver_info.dialect_tagdi))letprepare_any?nameqs=prepare_full?name(fun_->qs)letprepare_sql?names=prepare_fun?name@@function|#Caqti_driver_info.sql_dialect_tag->s|_->raiseMissing_query_stringletprepare_sql_p?name?envsql=prepare_fun?name@@function|#Caqti_driver_info.sql_dialect_tagaslang->format_query?envsqllang|_->raiseMissing_query_stringtypequery_info=[`Oneshotofstring|`Preparedofstring*string]letmake_query_infodriver_info=function|Oneshotqsf->`Oneshot(qsfdriver_info)|Prepared{pq_name;pq_encode;_}->`Prepared(pq_name,pq_encodedriver_info)