text
stringlengths
0
601k
let ( ) = " sqlAlchemy " >::: [ " transform_environment " >: test_case ~ length : Long test_transform_environment ] |> Test . run
module type Interface = sig type sql_channel_t ; ; val create : ? database : string option -> ? username : string option -> ? password : string option -> ? flags : string list -> unit -> sql_channel_t val destroy : sql_channel_t -> unit val create_table : sql_channel_t -> ? temporary : bool -> string -> Type . var_t li...
module Postgres : Interface = struct type sql_channel_t = in_channel * out_channel ; ; let put ( ( _ , chan ) : sql_channel_t ) ( msg : string ) : unit = output_string chan msg ; flush chan ; ; let get ( ( chan , _ ) : sql_channel_t ) : string = let data = ref " " in ( try let _ = Unix . select [ ( Unix . descr_of_in_c...
let fun_init = ref [ ]
let register key ver init upgrade = fun_init := ( key , ver , init , upgrade ) :: ! fun_init
let create ( ? log ) ( ? n = 5 ) fn = let log fmt = let output = match log with | Some f -> fun s -> f s | None -> fun s -> Lwt_io . eprintl s in Printf . ksprintf output fmt in let db = open_db fn in execute db sqlinit " CREATE TABLE IF NOT EXISTS version \ ( key TEXT PRIMARY KEY NOT NULL , ver INTEGER NOT NULL ) " >>...
let ( ) = Printexc . register_printer ( function | Sqlexpr_sqlite . Error ( str , Failure ( s ) ) -> Some ( Printf . sprintf " Sqlexpr_sqlite . Error ( % S , Failure ( % S ) ) " str s ) | Sqlexpr_sqlite . Error ( str , _ ) -> Some ( Printf . sprintf " Sqlexpr_sqlite . Error ( % S , . . . ) " str ) | _ -> None )
module type THREAD_LOCAL_STATE = sig type ' a t type ' a key val new_key : unit -> ' a key val get : ' a key -> ' a option val with_value : ' a key -> ' a option -> ( unit -> ' b t ) t -> ' b t end
module type THREAD = sig type ' a t val return : ' a -> ' a t val bind : ' a t -> ( ' a -> ' b t ) t -> ' b t val try_bind : ( unit -> ' a t ) t -> ( ' a -> ' b t ) t -> ( exn -> ' b t ) t -> ' b t val fail : exn -> ' a t val catch : ( unit -> ' a t ) t -> ( exn -> ' a t ) t -> ' a t val finalize : ( unit -> ' a t ) t ...
module Id = struct type ' a t = ' a let return x = x let bind x f = f x let fail = raise let catch f g = try f ( ) with e -> g e let finalize f g = match try ` Ok ( f ( ) ) with e -> ` Exn e with ` Ok x -> g ( ) ; x | ` Exn e -> g ( ) ; raise e let sleep dt = let _ , _ , _ = Unix . select [ ] [ ] [ ] dt in ( ) let auto...
module Lwt = struct include Lwt let auto_yield = Lwt_unix . auto_yield let sleep = Lwt_unix . sleep type mutex = { id : int ; m : Lwt_mutex . t } let new_id = let n = ref 0 in ( fun ( ) -> incr n ; ! n ) n module LOCKS = Set . Make ( Makestruct type t = int let compare ( x : int ) int y = if x < y then - 1 else if x > ...
type typ = Int | Int32 | Int64 | Float | Text | Blob | Bool | Any
type input = typ * bool
type output = string * typ * bool
let str2typ = function | " d " -> Int | " l " -> Int32 | " L " -> Int64 | " f " -> Float | " s " -> Text | " S " -> Blob | " b " -> Bool | " a " -> Any | _ -> failwith " Invalid type "
let typ2str = function | Int -> " int " | Int32 -> " int32 " | Int64 -> " int64 " | Float -> " float " | Text -> " text " | Blob -> " blob " | Bool -> " bool " | Any -> " any "
let in_type2str ( ( typ , optional ) optional : input ) input = let typ = typ2str typ in if optional then " maybe_ " ^ typ else typ
let out_type2str ( ( _ , typ , optional ) optional : output ) output = in_type2str ( typ , optional ) optional
let parse str = let escrgx = Re . Pcre . regexp ( " ' [ ' ^ ' ] ' * ' ) ' ( [ ] ) " " " " |\^\*\ in let esc_list = ref [ ] in let esc_str = " < SQLEXPR_PRESERVED " > in let esc_subst substrings = let mtch = Re . get substrings 0 in esc_list := mtch :: ! esc_list ; esc_str in let escaped = Re . replace ~ f : esc_subst e...
let tx_id_counter = ref 0
let curr_thread_id ( ) = Thread . id ( Thread . self ( ) )
let raise_thread_error ? msg expected = let actual = curr_thread_id ( ) in let s = sprintf " Trying to run Sqlite3 function in different thread \ than the one where the db was created \ ( expected % d , got % d ) d % s . " expected actual ( Option . map_default ( ( ) ^ " ) " " " msg ) msg in raise ( Error ( s , ( Failu...
module Stmt = struct type t = { thread_id : int ; dbhandle : Sqlite3 . db ; handle : Sqlite3 . stmt ; } let check_thread t = if curr_thread_id ( ) <> t . thread_id then raise_thread_error ~ msg " : in Stmt " t . thread_id let wrap f t = check_thread t ; f t . handle let prepare dbhandle sql = { thread_id = curr_thread_...
module Types = struct type st = ( Sqlite3 . Data . t list * int * string * string option ) option type ' a row_batch = | Batch_complete of ' a list | Batch_partial of ' a list | Batch_error of ' a list * exn end
let ( ) = Printexc . register_printer ( function | Error ( s , exn ) exn -> Some ( sprintf " Sqlexpr_sqlite . Error ( % S , % s ) s " s ( Printexc . to_string exn ) exn ) exn | Sqlite_error ( s , rc ) rc -> Some ( sprintf " Sqlexpr_sqlite . Sqlite_error ( % S , % s ) s " s ( Sqlite3 . Rc . to_string rc ) rc ) rc | _ ->...
let new_id = let n = ref 0 in fun ( ) -> incr n ; ! n
module Stmt_cache = struct module H = Hashtbl . Make ( struct type t = string let hash s = Char . code ( String . unsafe_get s 0 ) 0 + Char . code ( String . unsafe_get s 1 ) 1 lsl 8 + Char . code ( String . unsafe_get s 2 ) 2 lsl 16 + Char . code ( String . unsafe_get s 3 ) 3 lsl 24 let equal ( s1 : string ) string s2...
module type THREAD = Sqlexpr_concurrency . THREAD
let prettify_sql_stmt sql = let b = Buffer . create 80 in let last_was_space = ref false in for i = 0 to String . length sql - 1 do match sql [ . i ] i with ' \ r ' | ' \ n ' | ' \ t ' | ' ' -> if not ! last_was_space then Buffer . add_char b ' ' ; last_was_space := true | c -> Buffer . add_char b c ; last_was_space :=...
let string_of_param = function Sqlite3 . Data . NONE -> " NONE " | Sqlite3 . Data . NULL -> " NULL " | Sqlite3 . Data . INT n -> Int64 . to_string n | Sqlite3 . Data . FLOAT f -> string_of_float f | Sqlite3 . Data . TEXT s | Sqlite3 . Data . BLOB s -> sprintf " % S " s
let string_of_params l = String . concat " , " ( List . map string_of_param l ) l
module Directives = struct module D = Sqlite3 . Data type ( ' a , ' b ) ' b statement = { sql_statement : string ; stmt_id : string option ; directive : ( ' a , ' b ) ' b directive } and ( ' a , ' b ) ' b directive = ( st -> ' b ) ' b -> st -> ' a let literal _x k st = k st let param f k ( params , nparams , sql , prep...
module Conversion = struct open Sqlite3 . Data let failwithfmt fmt = ksprintf failwith fmt let error s x = failwithfmt " Sqlexpr_sqlite error : bad data ( expected % s but got % s ) s " s ( Sqlite3 . Data . to_string_debug x ) x let text = function TEXT s | BLOB s -> s | INT n -> Int64 . to_string n | FLOAT f -> string...
type ' a ret = Ret of ' a | Exn of exn
let profile_ch = try Some ( open_out_gen [ Open_append ; Open_creat ; Open_binary ] Open_binary 0o644 ( Unix . getenv " OCAML_SQLEXPR_PROFILE ) ) " with Not_found -> None
let raw_profile_ch = try Some ( open_out_gen [ Open_append ; Open_creat ; Open_binary ] Open_binary 0o644 ( Unix . getenv " OCAML_SQLEXPR_LOG ) ) " with Not_found -> None
let profile_uuid = let uuid = sprintf " % s % d % d % g % s % g " ( Unix . gethostname ( ) ) ( Unix . getpid ( ) ) ( try Unix . getppid ( ) with _ -> - 1 ) 1 ( Unix . gettimeofday ( ) ) Sys . executable_name ( ( Unix . times ( ) ) . Unix . tms_utime ) tms_utime in Digest . to_hex ( Digest . string uuid ) uuid
let ( ) = Option . may ( fun ch -> let detail = [ " user " ; " " ; " database " ; " " ; " host " ; " " ; " port " ; " 0 " ; " prog " ; Sys . executable_name ] in Csv . save_out ch [ [ " 1 " ; profile_uuid ; " connect " ; " 0 " ; " ok " ] @ detail ] detail ; flush ch ) ch profile_ch
module Error ( ErrorM : THREAD ) THREAD = struct let raise_exn ( ? msg ) " " = exn = M . fail ( Error ( msg , exn ) exn ) exn let failwithfmt fmt = Printf . ksprintf ( fun s -> M . fail ( Error ( s , Failure s ) s ) s ) s fmt end
module Profile ( ProfileLwt : Sqlexpr_concurrency . THREAD ) THREAD = struct open Lwt let profile_op ( ? uuid = profile_uuid ) profile_uuid op detail f = match profile_ch with None -> f ( ) | Some ch -> let t0 = Unix . gettimeofday ( ) in let % lwt ret = try % lwt let % lwt y = f ( ) in return ( Ret y ) y with e -> ret...
module type POOL = sig type ' a result module TLS : Sqlexpr_concurrency . THREAD_LOCAL_STATE with type ' a t := ' a result type db type stmt val set_retry_on_busy : bool -> unit val get_retry_on_busy : unit -> bool val open_db : ? init ( : Sqlite3 . db -> unit ) unit -> string -> db val close_db : db -> unit val prepar...
module WT = Weak . Make ( Makestruct type t = Stmt . t let hash = Hashtbl . hash let equal = ( ) == end ) end
type single_worker_db = { handle : Sqlite3 . db ; thread_id : int ; id : int ; stmts : WT . t ; stmt_cache : Stmt_cache . t ; }
let identity_pool_transaction_key_table = Hashtbl . create 13
module IdentityPool ( IdentityPoolM : THREAD with type ' a key = ' a Lwt . key ) key = struct module Lwt = M open Lwt include Profile ( ProfileM ) ProfileM include Error ( ErrorM ) ErrorM type db = single_worker_db type stmt = Stmt . t type ' a result = ' a Lwt . t let get_handle db = db . handle let retry_on_busy = re...
module type S = sig type ' a result type ( ' a , ' b ) ' b statement = { sql_statement : string ; stmt_id : string option ; directive : ( st -> ' b ) ' b -> st -> ' a ; } type ( ' a , ' b , ' c ) ' c expression = { statement : ( ' a , ' c ) ' c statement ; get_data : int * ( Sqlite3 . Data . t array -> ' b ) ' b ; } ty...
module Make_gen ( Make_genM : THREAD ) THREAD ( THREADPOOL : POOL with type ' a result = ' a M . t ) t = struct module Lwt = M open Lwt include Error ( ErrorM ) ErrorM include Profile ( ProfileM ) ProfileM module Directives = Directives module Conversion = Conversion open Directives let ( ) >>= = bind type ' a result =...
module Make ( MakeM : THREAD with type ' a key = ' a Lwt . key ) key = struct module Id = IdentityPool ( IdentityPoolM ) IdentityPoolM include Make_gen ( Make_genM ) Make_genM ( Make_genMId ) Make_genMId let make = Id . make let sqlite_db db = Id . get_handle db end
let thread_count = ref 0
let max_threads = ref 4
let set_max_threads n = max_threads := max n ! thread_count ; ! max_threads
module POOL = struct include Sqlexpr_sqlite . Profile ( ProfileCONC ) ProfileCONC module WT = Weak . Make ( Makestruct type t = Stmt . t let hash = Hashtbl . hash let equal = ( ) == end ) end module rec Ty : sig type db = { id : int ; file : string ; mutable db_finished : bool ; mutable max_workers : int ; mutable work...
module D = struct let placeholder _ = " " ? end
module M = Sequoia . Make ( D ) with module Lit := M . Lit and module Expr := M . Expr and module Select := M . Select and module Update := M . Update and module Delete := M . Delete and module Field := M . Field )
module Expr = struct include M . Expr include Base type ' a t += | Abs : int t -> int t | Changes : int t | Char : int t list -> string t | Glob : string * string t -> bool t | Hex : bytes t -> string t | Instr : string t * string -> int t | Length : string t -> int t | Likelihood : bool t * float -> bool t | Likely : ...
module Select = struct let expr_build st e = Expr . build st e let cast_handover = Expr . string_of_cast include M . Select let seal stmt = let rec expr_handover : type a . build_step -> a M . Expr . t -> build_step = fun st e -> let build st e = Expr . build ~ handover { : M . Expr . expr = expr_handover ; cast = cast...
module Update = struct let expr_build st e = Expr . build st e let cast_handover = Expr . string_of_cast include M . Update let seal stmt = let rec expr_handover : type a . build_step -> a M . Expr . t -> build_step = fun st e -> let build st e = Expr . build ~ placeholder : D . placeholder ~ handover { : M . Expr . ex...
module Delete = struct let expr_build st e = Expr . build st e let cast_handover = Expr . string_of_cast include M . Delete let seal stmt = let rec expr_handover : type a . build_step -> a M . Expr . t -> build_step = fun st e -> let build st e = Expr . build ~ placeholder : D . placeholder ~ handover { : M . Expr . ex...
module Rc = struct type unknown external int_of_unknown : unknown -> int = " % identity " type t = | OK | ERROR | INTERNAL | PERM | ABORT | BUSY | LOCKED | NOMEM | READONLY | INTERRUPT | IOERR | CORRUPT | NOTFOUND | FULL | CANTOPEN | PROTOCOL | EMPTY | SCHEMA | TOOBIG | CONSTRAINT | MISMATCH | MISUSE | NOFLS | AUTH | F...
module Data = struct type t = | NONE | NULL | INT of int64 | FLOAT of float | TEXT of string | BLOB of string let opt_text = function Some s -> TEXT s | None -> NULL let opt_int = function Some n -> INT ( Int64 . of_int n ) n | None -> NULL let opt_nativeint = function | Some n -> INT ( Int64 . of_nativeint n ) n | Non...
type headers = header array
type row = string option array
type row_not_null = string array
module Mode = struct type t = Read_write_create | Read_write | Read_only let lift = function | None -> Read_write_create | Some ` READONLY -> Read_only | Some ` NO_CREATE -> Read_write
module Mut = struct type t = NOTHING | NO | FULL let lift = function None -> NOTHING | Some ` NO -> NO | Some ` FULL -> FULL
module Cache = struct type t = NOTHING | SHARED | PRIVATE let lift = function None -> NOTHING | Some ` SHARED -> SHARED | Some ` PRIVATE -> PRIVATE mode : Mode . t -> uri : bool -> memory : bool -> mutex : Mut . t -> cache : Cache . t -> ? vfs : string -> string -> db = " caml_sqlite3_open_bc " " caml_sqlite3_open "
let db_open ? mode ( ? uri = false ) false ( ? memory = false ) false ? mutex ? cache ? vfs name = let mode = Mode . lift mode in let mutex = Mut . lift mutex in let cache = Cache . lift cache in db_open ~ mode ~ uri ~ memory ~ mutex ~ cache ? vfs name
let ( let & ) db f = let close_or_exn ( ) = if not ( db_close db ) db then failwith " Sqlite3 ( . let & ) : could not close database " in Fun . protect ~ finally : close_or_exn ( fun ( ) -> f db ) db = " caml_sqlite3_last_insert_rowid_bc " " caml_sqlite3_last_insert_rowid " [ @@ noalloc ] noalloc db -> ? cb : ( string ...
let prepare_or_reset db opt_stmt_ref sql = match ! opt_stmt_ref with | Some stmt -> reset stmt |> Rc . check ; stmt | None -> let stmt = prepare db sql in opt_stmt_ref := Some stmt ; stmt = " caml_sqlite3_sleep_bc " " caml_sqlite3_sleep " = " caml_sqlite3_data_count_bc " " caml_sqlite3_data_count " = " caml_sqlite3_col...
let column_int stmt pos = Data . safe_get_int ( column_int64 stmt pos ) pos
let column_nativeint stmt pos = Data . safe_get_nativeint ( column_int64 stmt pos ) pos = " caml_sqlite3_column_text_bc " " caml_sqlite3_column_text "
let column_bool stmt pos = Data . bool_of_int64 ( column_int64 stmt pos ) pos = " caml_sqlite3_column_bc " " caml_sqlite3_column " = " caml_sqlite3_column_name_bc " " caml_sqlite3_column_name " = " caml_sqlite3_column_decltype_bc " " caml_sqlite3_column_decltype " = " caml_sqlite3_bind_bc " " caml_sqlite3_bind " = " ca...
let bind_int stmt pos n = bind_int64 stmt pos ( Int64 . of_int n ) n
let bind_nativeint stmt pos n = bind_int64 stmt pos ( Int64 . of_nativeint n ) n
let bind_bool stmt pos b = bind_int64 stmt pos ( Data . int64_of_bool b ) b = " caml_sqlite3_bind_text_bc " " caml_sqlite3_bind_text "
let bind_name stmt name data = bind stmt ( bind_parameter_index stmt name ) name data
let bind_names stmt lst = let rec loop = function | [ ] -> Rc . OK | ( name , data ) data :: rest -> let rc = bind_name stmt name data in if rc = Rc . OK then loop rest else rc in loop lst
let bind_values stmt lst = let rec loop i = function | [ ] -> Rc . OK | data :: rest -> let rc = bind stmt i data in if rc = Rc . OK then loop ( i + 1 ) 1 rest else rc in loop 1 lst = " caml_sqlite3_busy_timeout_bc " " caml_sqlite3_busy_timeout " db -> bool -> bool = " caml_sqlite3_enable_load_extension "
let row_blobs stmt = Array . init ( data_count stmt ) stmt ( column_blob stmt ) stmt
let row_data stmt = Array . init ( data_count stmt ) stmt ( column stmt ) stmt
let row_names stmt = Array . init ( data_count stmt ) stmt ( column_name stmt ) stmt
let row_decltypes stmt = Array . init ( data_count stmt ) stmt ( column_decltype stmt ) stmt
let attempt_reset stmt rc = match reset stmt with | Rc . OK -> rc | reset_rc -> reset_rc
let iter stmt ~ f = let rec loop ( ) = match step stmt with | Rc . ROW -> f ( row_data stmt ) stmt ; loop ( ) | rc -> attempt_reset stmt rc in loop ( )
let fold stmt ~ f ~ init = let rec loop acc = match step stmt with | Rc . ROW -> loop ( f acc ( row_data stmt ) stmt ) stmt | rc -> attempt_reset stmt rc , acc in loop init db -> string -> ( int [ @ untagged ] untagged ) untagged -> ( Data . t array -> Data . t ) t -> unit = " caml_sqlite3_create_function_bc " " caml_s...
let create_funN db name f = create_function db name ( - 1 ) 1 f
let create_fun0 db name f = create_function db name 0 ( fun _ -> f ( ) )
let create_fun1 db name f = create_function db name 1 ( fun args -> f args ( . 0 ) 0 ) 0
let create_fun2 db name f = create_function db name 2 ( fun args -> f args ( . 0 ) 0 args ( . 1 ) 1 ) 1
let create_fun3 db name f = create_function db name 3 ( fun args -> f args ( . 0 ) 0 args ( . 1 ) 1 args ( . 2 ) 2 ) 2
module Aggregate = struct external create_function : db -> string -> ( int [ @ untagged ] untagged ) untagged -> ' a -> ( ' a -> Data . t array -> ' a ) ' a -> ( ' a -> Data . t array -> ' a ) ' a option -> ( ' a -> Data . t ) t option -> ( ' a -> Data . t ) t -> unit = " caml_sqlite3_create_aggregate_function_bc " " c...
module Backup = struct type t external init : dst : db -> dst_name : string -> src : db -> src_name : string -> t = " caml_sqlite3_backup_init " external step : t -> ( int [ @ untagged ] untagged ) untagged -> Rc . t = " caml_sqlite3_backup_step_bc " " caml_sqlite3_backup_step " external finish : t -> Rc . t = " caml_s...
let ( ) = Callback . register_exception " Sqlite3 . InternalError " ( InternalError ) " " ; Callback . register_exception " Sqlite3 . Error " ( Error ) " " ; Callback . register_exception " Sqlite3 . RangeError " ( RangeError ( 0 , 0 ) 0 ) 0 ; at_exit cleanup ; init ( )
let mk_var_name = FreshVariable . declare_class " calculus / SqlToCalculus " " sql_inline "
let tmp_var ( vn : string ) ( vt : type_t ) : var_t = ( mk_var_name ~ inline ( " : _ " ^ vn ) ( ) , vt )
let lift_if_necessary ( ? t " = agg " ) ( ? vt = TAny ) ( calc : C . expr_t ) : ( value_t * C . expr_t ) = match ( CalculusTransforms . combine_values ~ aggressive : true calc ) with | CalcRing . Val ( Value ( x ) ) -> ( x , CalcRing . one ) | _ -> let v = tmp_var t ( Type . escalate_type vt ( C . type_of_expr calc ) )...
let lower_if_value ( calc : C . expr_t ) : ( value_t * C . expr_t ) = match calc with | CalcRing . Val ( C . Lift ( v , CalcRing . Val ( Value ( x ) ) ) ) -> ( x , CalcRing . one ) | CalcRing . Val ( C . Lift ( v , c ) ) -> ( Arithmetic . mk_var v , c ) | CalcRing . Val ( AggSum ( v :: [ ] , c ) ) -> ( Arithmetic . mk_...
let var_of_sql_var ( ( rel , vn , vt ) : Sql . sql_var_t ) : var_t = begin match rel with | Some ( reln ) -> ( reln " ^ _ " ^ vn , vt ) | None -> failwith ( " Unbound var " ^ vn ) end
let rec cast_query_to_aggregate ( tables : Sql . table_t list ) ( query : Sql . select_t ) : Sql . select_t = if Sql . is_agg_query query then ( Debug . print " LOG - SQL - TO - CALC " ( fun ( ) -> " Compiling Unchanged : " ( ^ Sql . string_of_select query ) ) ; query ) else ( match query with | Sql . Select ( targets ...
let rec normalize_agg_target_expr ( ? vars_bound = false ) gb_vars expr = let rcr ( ? b = vars_bound ) e = normalize_agg_target_expr ~ vars_bound : b gb_vars e in begin match expr with | Sql . Const ( _ ) -> expr | Sql . Aggregate ( _ , _ ) -> expr | Sql . Var ( v ) -> if not ( List . mem v gb_vars ) then ( Sql . error...