Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file postgresql_conv.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120# 1 "shared/postgresql_conv.ml"(* Copyright (C) 2022--2023 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 LGPL-3.0 Linking 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
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)openCaqti_platformopenPrintfletbool_of_pgstring=function|"t"->true|"f"->false|_->invalid_arg"Pg_ext.bool_of_string"letpgstring_of_bool=functiontrue->"t"|false->"f"letpdate_of_pgstring=Conv.pdate_of_iso8601letpgstring_of_ptimex=Ptime.to_rfc3339~space:true~tz_offset_s:0~frac_s:6xletpgstring_of_ptime_spant=letis_neg=Ptime.Span.comparetPtime.Span.zero<0inletd,ps=Ptime.Span.(to_d_ps(abst))inletbuf=Buffer.create32inifd<>0thenbprintfbuf"%d days "(ifis_negthen-delsed);lets,ps=Int64.(divps1_000_000_000_000L|>to_int,remps1_000_000_000_000L)inlethour,s=s/3600,smod3600inletminute,s=s/60,smod60inifis_negthenBuffer.add_charbuf'-';bprintfbuf"%02d:%02d:%02d"hourminutes;ifps<>0Lthenbprintfbuf".%06Ld"(Int64.divps1_000_000L);Buffer.contentsbufletps_of_decimals=try(matchString.lengthswith|2->Ok(Int64.(mul(of_strings)1_000_000_000_000L))|nwhenn<2->Error"Missing digits in seconds of interval string."|_whens.[2]<>'.'->Error"Expected period after seconds in interval string."|n->letbuf=Bytes.make14'0'inBytes.blit_strings0buf02;Bytes.blit_strings3buf2(min12(n-3));Ok(Int64.of_string(Bytes.to_stringbuf)))withFailure_->Error"Seconds in interval string is not numeric."letps_of_hmss=(matchString.split_on_char':'swith|[hours;minutes;seconds]->(trylethour=int_of_stringhoursinletminute=int_of_stringminutesin(matchps_of_decimalsecondswith|Okps->lethm=Int64.of_int(abshour*60+minute)inletps=Int64.(addps(mulhm60_000_000_000_000L))inOk(ifs.[0]='-'thenInt64.negpselseps)|Errormsg->Errormsg)withFailure_->Error"Non-integer hour or minute in interval string.")|_->Error"Expected HH:MM:SS[.F] in interval string.")letptime_span_of_pgstrings=letspan_of_d_ps(d,ps)=letd,ps=ifInt64.comparepsInt64.zero>=0then(d,ps)else(d-1,Int64.addps86_400_000_000_000_000L)in(matchPtime.Span.of_d_ps(d,ps)with|Somet->Okt|None->Error"Out for range for Ptime.span.")intry(matchString.split_on_char' 'swith|[d;"days"]->span_of_d_ps(int_of_stringd,0L)|[d;"days";hms]->Result.bind(ps_of_hmshms)@@funps->span_of_d_ps(int_of_stringd,ps)|[hms]->Result.bind(ps_of_hmshms)@@funps->span_of_d_ps(0,ps)|_->Error"Unhandled interval format.")withFailure_->Error"Non-integer days in interval string."letcause_of_sqlstatesqlstate=(matchsqlstate.[0],sqlstate.[1],sqlstatewith|'2','3',"23001"->`Restrict_violation|'2','3',"23502"->`Not_null_violation|'2','3',"23503"->`Foreign_key_violation|'2','3',"23505"->`Unique_violation|'2','3',"23514"->`Check_violation|'2','3',"23P01"->`Exclusion_violation|'2','3',_->`Integrity_constraint_violation__don't_match|'5','3',"53100"->`Disk_full|'5','3',"53200"->`Out_of_memory|'5','3',"53300"->`Too_many_connections|'5','3',"53400"->`Configuration_limit_exceeded|'5','3',_->`Insufficient_resources__don't_match|_->`Unspecified__don't_match)