pax_global_header00006660000000000000000000000064125112176770014523gustar00rootroot0000000000000052 comment=39d46bf28baff4094e04dd581e5eefac5e1f096b pgocaml-2.2/000077500000000000000000000000001251121767700130105ustar00rootroot00000000000000pgocaml-2.2/.gitignore000066400000000000000000000000571251121767700150020ustar00rootroot00000000000000_build/ pgocaml_prof.byte setup.data setup.log pgocaml-2.2/CHANGELOG.txt000066400000000000000000000060051251121767700150410ustar00rootroot00000000000000Release 2.2 =========== * Jacques-Pascal Deplaix: Use Bytes instead of String when mutation is required. * Jacques-Pascal Deplaix: fix bug in AuthenticationCryptPassword. Release 2.1 =========== * Dario Teixeira: add List.iteri and List.mapi for compatibility with older versions of OCaml. Release 2.0 =========== * Dario Teixeira & Jacques-Pascal Deplaix: remove dependencies on ExtLib or Batteries. Auxilliary functions which used to be provided by Batteries are located in the newly created PGOCaml_aux module. * Dario Teixeira and Jacques-Pascal Deplaix: fixing issues with arrays. This requires all array types to change from 'a array to 'a option array, which breaks backward compatibility. * Dario Teixeira's patch making PostgreSQL's NUMERIC type be converted to/from OCaml strings. This change is not backward's compatible, requiring a bump in the major version number (though there seems to be no actual code in the wild relying on the previous behaviour). * Dario Teixeira's patch adding function 'uuid', which exposes the unique connection handle identifier. * Jacques-Pascal Deplaix's patches adding 'catch', 'transact', 'alive', 'inject', and 'alter' functions. Release 1.7.1 ============= * Fixed missing dependency in _oasis file. Release 1.7 =========== * Build system now uses OASIS. * Directory tree reorganisation. * Now using Batteries only. * Migration to Batteries 2.0. Release 1.6 =========== * Fixed Makefile: it should now work with any OCaml version. * Richard Jones patch converting all references of 'loc' into '_loc'. The former has been deprecated for a while now. Release 1.5 =========== * Dario Teixeira's patch adding support for more array types, namely bool[], int8[], text[], float4[], and float8[]. * Michael Ekstrand's patch to make PG'Ocaml work with batteries, if so requested (it still uses ExtLib by default). * Dario Teixeira's patch adding support for Hstore. * David Allsopp's patch fixing connection on Windows. * David Allsopp's patch for better reporting of nullable results. * Dario Teixeira's patch adding support for the 'hex' serialisation format introduced with PostgreSQL 9.0. * Matías Giovannini's patch adding support for cursors. * Dario Teixeira's patch adding support for the various transaction options in function 'begin_work'. Release 1.4 =========== * Boris Yakobowski's patch that replaces 'rows' and 'varname' with '_rows' and '_varname' (avoids unused variable warnings) * Support for INET type (patch by David Allsopp) * Dario Teixeira's patch for type unravelling Release 1.3 =========== * Applied (slightly modified) Vincent Bernardoff's patches for 'point' type * Large data set patch by Mykola Stryebkov Release 1.2 =========== * Jérôme Vouillon's monadic version * Updated to Calendar 2.x * Password authentication from $PGPASSWORD (by Dario Teixeira) * Syntax package now called 'syntax' instead of 'statements' Release 1.1 =========== * Added proper copyright notices to license. Release 1.0 =========== * First public release pgocaml-2.2/LICENSE.txt000066400000000000000000000634451251121767700146470ustar00rootroot00000000000000This library is distributed under the terms of the GNU LGPL with the OCaml linking exception. ---------------------------------------------------------------------- As a special exception to the GNU Library General Public License, you may link, statically or dynamically, a "work that uses the Library" with a publicly distributed version of the Library to produce an executable file containing portions of the Library, and distribute that executable file under terms of your choice, without any of the additional requirements listed in clause 6 of the GNU Library General Public License. By "a publicly distributed version of the Library", we mean either the unmodified Library as distributed by INRIA, or a modified version of the Library that is distributed under the conditions defined in clause 3 of the GNU Library General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU Library General Public License. ---------------------------------------------------------------------- GNU LIBRARY GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1991 Free Software Foundation, Inc. 59 Temple Place - Suite 330 Boston, MA 02111-1307, USA. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. [This is the first released version of the library GPL. It is numbered 2 because it goes with version 2 of the ordinary GPL.] Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public Licenses are intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This license, the Library General Public License, applies to some specially designated Free Software Foundation software, and to any other libraries whose authors decide to use it. You can use it for your libraries, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the library, or if you modify it. For example, if you distribute copies of the library, whether gratis or for a fee, you must give the recipients all the rights that we gave you. You must make sure that they, too, receive or can get the source code. If you link a program with the library, you must provide complete object files to the recipients so that they can relink them with the library, after making changes to the library and recompiling it. And you must show them these terms so they know their rights. Our method of protecting your rights has two steps: (1) copyright the library, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the library. Also, for each distributor's protection, we want to make certain that everyone understands that there is no warranty for this free library. If the library is modified by someone else and passed on, we want its recipients to know that what they have is not the original version, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that companies distributing free software will individually obtain patent licenses, thus in effect transforming the program into proprietary software. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. Most GNU software, including some libraries, is covered by the ordinary GNU General Public License, which was designed for utility programs. This license, the GNU Library General Public License, applies to certain designated libraries. This license is quite different from the ordinary one; be sure to read it in full, and don't assume that anything in it is the same as in the ordinary license. The reason we have a separate public license for some libraries is that they blur the distinction we usually make between modifying or adding to a program and simply using it. Linking a program with a library, without changing the library, is in some sense simply using the library, and is analogous to running a utility program or application program. However, in a textual and legal sense, the linked executable is a combined work, a derivative of the original library, and the ordinary General Public License treats it as such. Because of this blurred distinction, using the ordinary General Public License for libraries did not effectively promote software sharing, because most developers did not use the libraries. We concluded that weaker conditions might promote sharing better. However, unrestricted linking of non-free programs would deprive the users of those programs of all benefit from the free status of the libraries themselves. This Library General Public License is intended to permit developers of non-free programs to use free libraries, while preserving your freedom as a user of such programs to change the free libraries that are incorporated in them. (We have not seen how to achieve this as regards changes in header files, but we have achieved it as regards changes in the actual functions of the Library.) The hope is that this will lead to faster development of free libraries. The precise terms and conditions for copying, distribution and modification follow. Pay close attention to the difference between a "work based on the library" and a "work that uses the library". The former contains code derived from the library, while the latter only works together with the library. Note that it is possible for a library to be covered by the ordinary General Public License rather than by this special one. GNU LIBRARY GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License Agreement applies to any software library which contains a notice placed by the copyright holder or other authorized party saying it may be distributed under the terms of this Library General Public License (also called "this License"). Each licensee is addressed as "you". A "library" means a collection of software functions and/or data prepared so as to be conveniently linked with application programs (which use some of those functions and data) to form executables. The "Library", below, refers to any such software library or work which has been distributed under these terms. A "work based on the Library" means either the Library or any derivative work under copyright law: that is to say, a work containing the Library or a portion of it, either verbatim or with modifications and/or translated straightforwardly into another language. (Hereinafter, translation is included without limitation in the term "modification".) "Source code" for a work means the preferred form of the work for making modifications to it. For a library, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the library. Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running a program using the Library is not restricted, and output from such a program is covered only if its contents constitute a work based on the Library (independent of the use of the Library in a tool for writing it). Whether that is true depends on what the Library does and what the program that uses the Library does. 1. You may copy and distribute verbatim copies of the Library's complete source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and distribute a copy of this License along with the Library. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Library or any portion of it, thus forming a work based on the Library, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) The modified work must itself be a software library. b) You must cause the files modified to carry prominent notices stating that you changed the files and the date of any change. c) You must cause the whole of the work to be licensed at no charge to all third parties under the terms of this License. d) If a facility in the modified Library refers to a function or a table of data to be supplied by an application program that uses the facility, other than as an argument passed when the facility is invoked, then you must make a good faith effort to ensure that, in the event an application does not supply such function or table, the facility still operates, and performs whatever part of its purpose remains meaningful. (For example, a function in a library to compute square roots has a purpose that is entirely well-defined independent of the application. Therefore, Subsection 2d requires that any application-supplied function or table used by this function must be optional: if the application does not supply it, the square root function must still compute square roots.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Library, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Library, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Library. In addition, mere aggregation of another work not based on the Library with the Library (or with a work based on the Library) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may opt to apply the terms of the ordinary GNU General Public License instead of this License to a given copy of the Library. To do this, you must alter all the notices that refer to this License, so that they refer to the ordinary GNU General Public License, version 2, instead of to this License. (If a newer version than version 2 of the ordinary GNU General Public License has appeared, then you can specify that version instead if you wish.) Do not make any other change in these notices. Once this change is made in a given copy, it is irreversible for that copy, so the ordinary GNU General Public License applies to all subsequent copies and derivative works made from that copy. This option is useful when you wish to copy part of the code of the Library into a program that is not a library. 4. You may copy and distribute the Library (or a portion or derivative of it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange. If distribution of object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place satisfies the requirement to distribute the source code, even though third parties are not compelled to copy the source along with the object code. 5. A program that contains no derivative of any portion of the Library, but is designed to work with the Library by being compiled or linked with it, is called a "work that uses the Library". Such a work, in isolation, is not a derivative work of the Library, and therefore falls outside the scope of this License. However, linking a "work that uses the Library" with the Library creates an executable that is a derivative of the Library (because it contains portions of the Library), rather than a "work that uses the library". The executable is therefore covered by this License. Section 6 states terms for distribution of such executables. When a "work that uses the Library" uses material from a header file that is part of the Library, the object code for the work may be a derivative work of the Library even though the source code is not. Whether this is true is especially significant if the work can be linked without the Library, or if the work is itself a library. The threshold for this to be true is not precisely defined by law. If such an object file uses only numerical parameters, data structure layouts and accessors, and small macros and small inline functions (ten lines or less in length), then the use of the object file is unrestricted, regardless of whether it is legally a derivative work. (Executables containing this object code plus portions of the Library will still fall under Section 6.) Otherwise, if the work is a derivative of the Library, you may distribute the object code for the work under the terms of Section 6. Any executables containing that work also fall under Section 6, whether or not they are linked directly with the Library itself. 6. As an exception to the Sections above, you may also compile or link a "work that uses the Library" with the Library to produce a work containing portions of the Library, and distribute that work under terms of your choice, provided that the terms permit modification of the work for the customer's own use and reverse engineering for debugging such modifications. You must give prominent notice with each copy of the work that the Library is used in it and that the Library and its use are covered by this License. You must supply a copy of this License. If the work during execution displays copyright notices, you must include the copyright notice for the Library among them, as well as a reference directing the user to the copy of this License. Also, you must do one of these things: a) Accompany the work with the complete corresponding machine-readable source code for the Library including whatever changes were used in the work (which must be distributed under Sections 1 and 2 above); and, if the work is an executable linked with the Library, with the complete machine-readable "work that uses the Library", as object code and/or source code, so that the user can modify the Library and then relink to produce a modified executable containing the modified Library. (It is understood that the user who changes the contents of definitions files in the Library will not necessarily be able to recompile the application to use the modified definitions.) b) Accompany the work with a written offer, valid for at least three years, to give the same user the materials specified in Subsection 6a, above, for a charge no more than the cost of performing this distribution. c) If distribution of the work is made by offering access to copy from a designated place, offer equivalent access to copy the above specified materials from the same place. d) Verify that the user has already received a copy of these materials or that you have already sent this user a copy. For an executable, the required form of the "work that uses the Library" must include any data and utility programs needed for reproducing the executable from it. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. It may happen that this requirement contradicts the license restrictions of other proprietary libraries that do not normally accompany the operating system. Such a contradiction means you cannot use both them and the Library together in an executable that you distribute. 7. You may place library facilities that are a work based on the Library side-by-side in a single library together with other library facilities not covered by this License, and distribute such a combined library, provided that the separate distribution of the work based on the Library and of the other library facilities is otherwise permitted, and provided that you do these two things: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities. This must be distributed under the terms of the Sections above. b) Give prominent notice with the combined library of the fact that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 8. You may not copy, modify, sublicense, link with, or distribute the Library except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense, link with, or distribute the Library is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 9. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Library or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Library (or any work based on the Library), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Library or works based on it. 10. Each time you redistribute the Library (or any work based on the Library), the recipient automatically receives a license from the original licensor to copy, distribute, link with or modify the Library subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 11. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Library at all. For example, if a patent license would not permit royalty-free redistribution of the Library by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Library. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply, and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 12. If the distribution and/or use of the Library is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Library under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 13. The Free Software Foundation may publish revised and/or new versions of the Library General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Library does not specify a license version number, you may choose any version ever published by the Free Software Foundation. 14. If you wish to incorporate parts of the Library into other free programs whose distribution conditions are incompatible with these, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS Appendix: How to Apply These Terms to Your New Libraries If you develop a new library, and you want it to be of the greatest possible use to the public, we recommend making it free software that everyone can redistribute and change. You can do so by permitting redistribution under these terms (or, alternatively, under the terms of the ordinary General Public License). To apply these terms, attach the following notices to the library. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 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 COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Also add information on how to contact you by electronic and paper mail. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the library, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the library `Frob' (a library for tweaking knobs) written by James Random Hacker. , 1 April 1990 Ty Coon, President of Vice That's all there is to it! pgocaml-2.2/Makefile000066400000000000000000000016201251121767700144470ustar00rootroot00000000000000# OASIS_START # DO NOT EDIT (digest: a3c674b4239234cbbe53afe090018954) SETUP = ocaml setup.ml build: setup.data $(SETUP) -build $(BUILDFLAGS) doc: setup.data build $(SETUP) -doc $(DOCFLAGS) test: setup.data build $(SETUP) -test $(TESTFLAGS) all: $(SETUP) -all $(ALLFLAGS) install: setup.data $(SETUP) -install $(INSTALLFLAGS) uninstall: setup.data $(SETUP) -uninstall $(UNINSTALLFLAGS) reinstall: setup.data $(SETUP) -reinstall $(REINSTALLFLAGS) clean: $(SETUP) -clean $(CLEANFLAGS) distclean: $(SETUP) -distclean $(DISTCLEANFLAGS) setup.data: $(SETUP) -configure $(CONFIGUREFLAGS) configure: $(SETUP) -configure $(CONFIGUREFLAGS) .PHONY: build doc test all install uninstall reinstall clean distclean configure # OASIS_STOP DIR=$(if $(wildcard /var/run/postgresql),"/var/run/postgresql","/tmp") config: echo "let default_unix_domain_socket_dir = \"$(DIR)\"" > src/PGOCaml_config.ml pgocaml-2.2/README.txt000066400000000000000000000037601251121767700145140ustar00rootroot00000000000000------------------------------------------------------------------------- | Readme. | ------------------------------------------------------------------------- PG'OCaml is a set of OCaml bindings for the PostgreSQL database. Please note that this is not the first or only PGSQL bindings for OCaml. Here are the others which you may want to consider: http://www.ocaml.info/home/ocaml_sources.html - PostgreSQL-OCaml by Markus Mottl http://home.gna.org/ocamlodbc/ - ODBC bindings by Max Guesdon which can be used to access PostgreSQL http://download.savannah.nongnu.org/releases/modcaml/ - ocamldbi, a Perl-like DBI layer by the present author PG'OCAML is different than the above bindings: (1) It ISN'T just a wrapper around the C libpq library. Instead it's a pure OCaml library which talks the frontend/backend protocol directly with the database. (2) It has a camlp4 layer which lets you write SQL statements directly in your code, TYPE SAFE at compile time, with TYPE INFERENCE into the SQL, and using the full PostgreSQL SQL grammar (sub-selects, PG-specific SQL, etc.). But the flip side of this is that you need to have access to the database at _compile_ time, so the type checking magic can be done; also if you change the database schema, you will need to recompile your code to check it is still correctly typed. (3) (A minor point) - It requires PostgreSQL >= 7.4. It's also synchronous, so if you want to have an interactive interface you'll need to use threads. (4) It doesn't work with other databases, nor will it ever work with other databases. ---------------------------------------------------------------------- PG'OCaml (C) Copyright 2005-2009 Merjis Ltd, Richard W.M. Jones (rich@annexia.org) and other authors (see CONTRIBUTORS.txt for details). This software is distributed under the GNU LGPL with OCaml linking exception. Please see the file COPYING.LIB for full license. ---------------------------------------------------------------------- For an example, please see test*.ml pgocaml-2.2/_oasis000066400000000000000000000033711251121767700142140ustar00rootroot00000000000000OASISFormat: 0.4 Name: PG'OCaml Version: 2.2 Synopsis: OCaml bindings for the PostgreSQL database Authors: Richard W. M. Jones et al. Maintainers: Dario Teixeira License: LGPL-2.0 with OCaml linking exception LicenseFile: LICENSE.txt Plugins: META (0.4), DevFiles (0.4) BuildTools: ocamlbuild, make, camlp4 PreBuildCommand: $make config Flag "pgprof" Description: Build PG'OCaml profiling utility Default: true Library "pgocaml" Path: src Modules: PGOCaml_aux, PGOCaml, PGOCaml_generic, PGOCaml_config BuildDepends: unix, calendar, csv, pcre, bytes FindlibName: pgocaml XMETARequires: unix, calendar, csv, pcre, bytes Library "pa_pgsql" Path: src Modules: Pa_pgsql CompiledObject: byte BuildDepends: pgocaml, camlp4.quotations, camlp4.extend FindlibParent: pgocaml FindlibName: syntax XMETAType: syntax XMETADescription: Syntax extension for PG'OCaml XMETARequires: pgocaml, camlp4 Document "APIDOC" Install: true InstallDir: $htmldir Title: API reference for PG'OCaml Type: ocamlbuild (0.4) BuildTools+: ocamldoc XOCamlbuildPath: src XOCamlbuildLibraries: pgocaml Executable "pgocaml_prof" Build$: flag(pgprof) Install: false Path: utils MainIs: pgocaml_prof.ml BuildDepends: pgocaml Executable "test_pgocaml_lowlevel" Build$: flag(tests) Install: false Path: tests MainIs: test_pgocaml_lowlevel.ml BuildDepends: pgocaml Executable "test_pgocaml_highlevel" Build$: flag(tests) Install: false Path: tests MainIs: test_pgocaml_highlevel.ml BuildDepends: pgocaml, pgocaml.syntax Test "pgocaml_lowlevel" Command: $test_pgocaml_lowlevel WorkingDirectory: tests Test "pgocaml_highlevel" Command: $test_pgocaml_highlevel WorkingDirectory: tests pgocaml-2.2/_tags000066400000000000000000000046521251121767700140370ustar00rootroot00000000000000# OASIS_START # DO NOT EDIT (digest: 2195434e610096d70cae76da7908ab60) # Ignore VCS directories, you can use the same kind of rule outside # OASIS_START/STOP if you want to exclude directories that contains # useless stuff for the build process true: annot, bin_annot <**/.svn>: -traverse <**/.svn>: not_hygienic ".bzr": -traverse ".bzr": not_hygienic ".hg": -traverse ".hg": not_hygienic ".git": -traverse ".git": not_hygienic "_darcs": -traverse "_darcs": not_hygienic # Library pgocaml "src/pgocaml.cmxs": use_pgocaml # Library pa_pgsql "src/pa_pgsql.cmxs": use_pa_pgsql : pkg_bytes : pkg_calendar : pkg_camlp4.extend : pkg_camlp4.quotations : pkg_csv : pkg_pcre : pkg_unix : use_pgocaml # Executable pgocaml_prof "utils/pgocaml_prof.byte": pkg_bytes "utils/pgocaml_prof.byte": pkg_calendar "utils/pgocaml_prof.byte": pkg_csv "utils/pgocaml_prof.byte": pkg_pcre "utils/pgocaml_prof.byte": pkg_unix "utils/pgocaml_prof.byte": use_pgocaml : pkg_bytes : pkg_calendar : pkg_csv : pkg_pcre : pkg_unix : use_pgocaml # Executable test_pgocaml_lowlevel "tests/test_pgocaml_lowlevel.byte": pkg_bytes "tests/test_pgocaml_lowlevel.byte": pkg_calendar "tests/test_pgocaml_lowlevel.byte": pkg_csv "tests/test_pgocaml_lowlevel.byte": pkg_pcre "tests/test_pgocaml_lowlevel.byte": pkg_unix "tests/test_pgocaml_lowlevel.byte": use_pgocaml # Executable test_pgocaml_highlevel "tests/test_pgocaml_highlevel.byte": pkg_bytes "tests/test_pgocaml_highlevel.byte": pkg_calendar "tests/test_pgocaml_highlevel.byte": pkg_camlp4.extend "tests/test_pgocaml_highlevel.byte": pkg_camlp4.quotations "tests/test_pgocaml_highlevel.byte": pkg_csv "tests/test_pgocaml_highlevel.byte": pkg_pcre "tests/test_pgocaml_highlevel.byte": pkg_unix "tests/test_pgocaml_highlevel.byte": use_pa_pgsql "tests/test_pgocaml_highlevel.byte": use_pgocaml : pkg_bytes : pkg_calendar : pkg_camlp4.extend : pkg_camlp4.quotations : pkg_csv : pkg_pcre : pkg_unix : use_pa_pgsql : use_pgocaml # OASIS_STOP : syntax_camlp4o : syntax_camlp4o, pkg_pgocaml.syntax true: safe_string pgocaml-2.2/configure000077500000000000000000000005531251121767700147220ustar00rootroot00000000000000#!/bin/sh # OASIS_START # DO NOT EDIT (digest: dc86c2ad450f91ca10c931b6045d0499) set -e FST=true for i in "$@"; do if $FST; then set -- FST=false fi case $i in --*=*) ARG=${i%%=*} VAL=${i##*=} set -- "$@" "$ARG" "$VAL" ;; *) set -- "$@" "$i" ;; esac done ocaml setup.ml -configure "$@" # OASIS_STOP pgocaml-2.2/doc/000077500000000000000000000000001251121767700135555ustar00rootroot00000000000000pgocaml-2.2/doc/BUGS.txt000066400000000000000000000102131251121767700150530ustar00rootroot00000000000000------------------------------------------------------------------------- | Current bugs and other problems. | ------------------------------------------------------------------------- 1. LEFT OUTER JOIN and nullability of named columns [FIXED] ----------------------------------------------------------- Consider tables like: create table users ( id serial not null primary key, u.name text not null, ... ); create table adverts ( author int references users (id), -- could be null ... ); and a query like: select a.author, u.name from adverts a LEFT OUTER JOIN users u on a.author = u.id The LEFT OUTER JOIN ensures that rows are returned even if the adverts.author field is null. For example, this query can return u.name as NULL. Unfortunately our nullability heuristic doesn't work well here. The u.name column is marked as NOT NULL and so we don't expect it to come out of the database as a NULL. The current workaround is to use PGSQL(dbh) "nullable-results" "query" instead of PGSQL(dbh) "query". This disables the nullability heuristic for all columns, and so all return columns will have type "'a option". 2. CREATE TEMPORARY TABLE [FIXED] --------------------------------- Normally at compile time, statements are only prepared in the database. You wouldn't ordinarily want to run statements at compile time, since that would be dangerous. However there is one case where it is desirable to run the statement, and that is if the statement creates a temporary table. By actually creating the temporary table, we allow further statements which use the table to be checked, and since temporary tables are temporary, no harm is done by creating them, even at compile time. For this case, use PGSQL(dbh) "execute" "query" instead of PGSQL(dbh) "query". For example: PGSQL(dbh,"execute") "create temporary table employees ( id serial not null primary key, name text not null, salary int4 not null, email text )"; let insert name salary email = PGSQL(dbh) "insert into employees (name, salary, email) values ($name, $salary, $?email)" in insert "Ann" 10_000_l None; insert "Bob" 45_000_l None; insert "Jim" 20_000_l None; insert "Mary" 30_000_l (Some "mary@example.com"); Note that the CREATE TEMPORARY TABLE statement needs to come before any statements which use the table. There doesn't seem to be a good way around this. 3. Specifying lists as parameters [FIXED] ----------------------------------------- Suppose I want to select a subset of employees from my database. We'd like to be able to write: let employee_ids = [ 3; 4; 5 ] in PGSQL(dbh) "select name from employees where id in $@employee_ids" which, at runtime, would expand to: select name from employees where id in (3, 4, 5) The implementation of this is complex. At compile time we prototype the following statement: select name from employees where id in ($1) and get the type of $1 as the type of each element in the list. At runtime we then have to be careful to cache each list arity separately - quite complex if there are several lists in the statement. This doesn't work at all if the list could be empty, because "... in ()" is a syntax error in SQL. This is a problem with the SQL standard. You need to treat that as a special case. 4. Generating SQL from fragments -------------------------------- It is fairly common to construct SQL statements from string fragments, as in this pseudocode example: let order_clause = match key, reverse with | `Author, false -> "author asc" | `Author, true -> "author desc" | `Title, false -> "title asc" | `Title, true -> "title desc" let sql = "select title, author from books " ^ order_clause Such statement-building is not currently permitted by PG'OCaml, unless you ditch the camlp4 extension and use the low-level, unsafe interface. It would be nice to have some sort of "fragment constructor" operator to allow the above to be expressed in a type-safe way. However because it is not possible to compile the fragments, it doesn't look like such a constructor could be written. If anyone has any ideas about this, please contact the author. pgocaml-2.2/doc/CONTRIBUTORS.txt000066400000000000000000000015151251121767700162550ustar00rootroot00000000000000------------------------------------------------------------------------- | Contributors. | ------------------------------------------------------------------------- * Gabriel de Perthuis: Contributed patches to make OMakefile builds work using a '-syntax' flag. * Dario Teixeira: Tutorial, web site, miscellaneous patches, release maintenance. * Jérôme Vouillon: Code refactoring to work in a monadic fashion (for Lwt, for example). * Mykola Stryebkov: Support for large data sets. * Vincent Bernardoff: Support for "point" type. * Boris Yakobowski: Patch that replaces 'rows' and 'varname' with '_rows' and '_varname' (avoids unused variable warnings) * David Allsopp: Support for INET type, miscellaneous fixes * Michael Ekstrand: OCaml Batteries support. * Matías Giovannini: Cursor support. pgocaml-2.2/doc/HOW_IT_WORKS.txt000066400000000000000000000063441251121767700163430ustar00rootroot00000000000000------------------------------------------------------------------------- | How PG'OCaml works. | ------------------------------------------------------------------------- Enough people have asked me how PG'OCaml works that I am compiling this document to explain what is going on. 1. Background ------------- 1.1 PREPARE A common operation on databases is 'PREPARE statement'. What this does is to take a fixed statement and optimise it. The idea is that if you execute the same statement lots of times, instead of having the database optimise it each time (a process which can be time-consuming), you PREPARE [optimise] it once and then EXECUTE the already optimised statement. For example: PREPARE SELECT [some very complicated set of joins which take a long time to optimise] EXECUTE EXECUTE EXECUTE Most databases extend this notion with placeholders in the original statement, so: PREPARE SELECT name FROM users where id = $1 EXECUTE ($1 = 10) EXECUTE ($1 = 20) EXECUTE ($1 = 23) 1.2 DESCRIBE Recent versions of PostgreSQL added a 'DESCRIBE statement' command to the database backend. It isn't normally exposed through clients like psql or libPQ (well - that may have changed by the time you read this), but it's there if you code directly to the database frontend/backend wire protocol. 'DESCRIBE' is just an extension of 'PREPARE'. Because the optimiser has decoded the statement into some internal format, it knows already the types of the placeholders and the types of the return columns, and it can supply this information back to the caller. For example: PREPARE SELECT id, name FROM users WHERE salary > $1 DESCRIBE ==> placeholder $1 has type DECIMAL ==> 2 columns returned with types SERIAL, VARCHAR(80) 2. In OCaml ----------- On the OCaml side we like to know the type of everything, and using DESCRIBE we can extend type inference right the way through to the database. Consider some code like: let salary = 15000.00 in let rows = PGSQL(dbh) "SELECT id, name FROM users WHERE salary > $salary" in List.iter ( fun (id, name) -> printf "id = %d, name = %s\n" id name ) rows How do we know that salary (type: float) when passed to the database has the same type that the database expects? How do we know that the id and name fields have the same type as what the database returns? During compilation we can use DESCRIBE to convert this code into: let salary = 15000.00 in let rows = (*vv-- generated by macro --vv*) do_PREPARE "SELECT id, name FROM users WHERE salary > $1"; let placeholder1 = string_of_decimal salary (* placeholder $1 *) in let rows = do_EXECUTE placeholder1 in List.map ( fun (col1, col2) -> (* returned columns id, name *) (serial_of_string col1, string_of_string col2) ) rows (*^^-- generated by macro --^^*) List.iter ( fun (id, name) -> printf "id = %d, name = %s\n" id name ) rows Notes: (1) The real code generated by the macro is a lot more complicated. (2) The database actually takes and returns strings. (3) In the real macro, prepared statements are cached so they don't need to be reoptimised each time. 3. Further reading ------------------ Now go and read: BUGS.txt, pGOCaml.mli, pa_pgsql.ml4 pgocaml-2.2/doc/PROFILING.txt000066400000000000000000000015571251121767700156570ustar00rootroot00000000000000------------------------------------------------------------------------- | Profiling. | ------------------------------------------------------------------------- If PG'OCaml programs are started with the environment variable $PGPROFILING pointing to the name of a writable file, then profiling information about events is written to this file. This information can be analysed later by using the pgocaml_prof tool. For example: PGPROFILING=$HOME/.test_prof export PGPROFILING ./test pgocaml_prof $HOME/.test_prof | less Information currently tracked includes the running time for SQL statements and connection time. You can usually use this information to find out which statements are taking too long, and optimise them accordingly. The profile file will contain some potentially sensitive information such as database and user names (but not passwords). pgocaml-2.2/myocamlbuild.ml000066400000000000000000000417171251121767700160350ustar00rootroot00000000000000(* OASIS_START *) (* DO NOT EDIT (digest: f841356cd28294705cfe988a752cc54d) *) module OASISGettext = struct (* # 22 "src/oasis/OASISGettext.ml" *) let ns_ str = str let s_ str = str let f_ (str: ('a, 'b, 'c, 'd) format4) = str let fn_ fmt1 fmt2 n = if n = 1 then fmt1^^"" else fmt2^^"" let init = [] end module OASISExpr = struct (* # 22 "src/oasis/OASISExpr.ml" *) open OASISGettext type test = string type flag = string type t = | EBool of bool | ENot of t | EAnd of t * t | EOr of t * t | EFlag of flag | ETest of test * string type 'a choices = (t * 'a) list let eval var_get t = let rec eval' = function | EBool b -> b | ENot e -> not (eval' e) | EAnd (e1, e2) -> (eval' e1) && (eval' e2) | EOr (e1, e2) -> (eval' e1) || (eval' e2) | EFlag nm -> let v = var_get nm in assert(v = "true" || v = "false"); (v = "true") | ETest (nm, vl) -> let v = var_get nm in (v = vl) in eval' t let choose ?printer ?name var_get lst = let rec choose_aux = function | (cond, vl) :: tl -> if eval var_get cond then vl else choose_aux tl | [] -> let str_lst = if lst = [] then s_ "" else String.concat (s_ ", ") (List.map (fun (cond, vl) -> match printer with | Some p -> p vl | None -> s_ "") lst) in match name with | Some nm -> failwith (Printf.sprintf (f_ "No result for the choice list '%s': %s") nm str_lst) | None -> failwith (Printf.sprintf (f_ "No result for a choice list: %s") str_lst) in choose_aux (List.rev lst) end # 132 "myocamlbuild.ml" module BaseEnvLight = struct (* # 22 "src/base/BaseEnvLight.ml" *) module MapString = Map.Make(String) type t = string MapString.t let default_filename = Filename.concat (Sys.getcwd ()) "setup.data" let load ?(allow_empty=false) ?(filename=default_filename) () = if Sys.file_exists filename then begin let chn = open_in_bin filename in let st = Stream.of_channel chn in let line = ref 1 in let st_line = Stream.from (fun _ -> try match Stream.next st with | '\n' -> incr line; Some '\n' | c -> Some c with Stream.Failure -> None) in let lexer = Genlex.make_lexer ["="] st_line in let rec read_file mp = match Stream.npeek 3 lexer with | [Genlex.Ident nm; Genlex.Kwd "="; Genlex.String value] -> Stream.junk lexer; Stream.junk lexer; Stream.junk lexer; read_file (MapString.add nm value mp) | [] -> mp | _ -> failwith (Printf.sprintf "Malformed data file '%s' line %d" filename !line) in let mp = read_file MapString.empty in close_in chn; mp end else if allow_empty then begin MapString.empty end else begin failwith (Printf.sprintf "Unable to load environment, the file '%s' doesn't exist." filename) end let rec var_expand str env = let buff = Buffer.create ((String.length str) * 2) in Buffer.add_substitute buff (fun var -> try var_expand (MapString.find var env) env with Not_found -> failwith (Printf.sprintf "No variable %s defined when trying to expand %S." var str)) str; Buffer.contents buff let var_get name env = var_expand (MapString.find name env) env let var_choose lst env = OASISExpr.choose (fun nm -> var_get nm env) lst end # 237 "myocamlbuild.ml" module MyOCamlbuildFindlib = struct (* # 22 "src/plugins/ocamlbuild/MyOCamlbuildFindlib.ml" *) (** OCamlbuild extension, copied from * http://brion.inria.fr/gallium/index.php/Using_ocamlfind_with_ocamlbuild * by N. Pouillard and others * * Updated on 2009/02/28 * * Modified by Sylvain Le Gall *) open Ocamlbuild_plugin type conf = { no_automatic_syntax: bool; } (* these functions are not really officially exported *) let run_and_read = Ocamlbuild_pack.My_unix.run_and_read let blank_sep_strings = Ocamlbuild_pack.Lexers.blank_sep_strings let exec_from_conf exec = let exec = let env_filename = Pathname.basename BaseEnvLight.default_filename in let env = BaseEnvLight.load ~filename:env_filename ~allow_empty:true () in try BaseEnvLight.var_get exec env with Not_found -> Printf.eprintf "W: Cannot get variable %s\n" exec; exec in let fix_win32 str = if Sys.os_type = "Win32" then begin let buff = Buffer.create (String.length str) in (* Adapt for windowsi, ocamlbuild + win32 has a hard time to handle '\\'. *) String.iter (fun c -> Buffer.add_char buff (if c = '\\' then '/' else c)) str; Buffer.contents buff end else begin str end in fix_win32 exec let split s ch = let buf = Buffer.create 13 in let x = ref [] in let flush () = x := (Buffer.contents buf) :: !x; Buffer.clear buf in String.iter (fun c -> if c = ch then flush () else Buffer.add_char buf c) s; flush (); List.rev !x let split_nl s = split s '\n' let before_space s = try String.before s (String.index s ' ') with Not_found -> s (* ocamlfind command *) let ocamlfind x = S[Sh (exec_from_conf "ocamlfind"); x] (* This lists all supported packages. *) let find_packages () = List.map before_space (split_nl & run_and_read (exec_from_conf "ocamlfind" ^ " list")) (* Mock to list available syntaxes. *) let find_syntaxes () = ["camlp4o"; "camlp4r"] let well_known_syntax = [ "camlp4.quotations.o"; "camlp4.quotations.r"; "camlp4.exceptiontracer"; "camlp4.extend"; "camlp4.foldgenerator"; "camlp4.listcomprehension"; "camlp4.locationstripper"; "camlp4.macro"; "camlp4.mapgenerator"; "camlp4.metagenerator"; "camlp4.profiler"; "camlp4.tracer" ] let dispatch conf = function | After_options -> (* By using Before_options one let command line options have an higher * priority on the contrary using After_options will guarantee to have * the higher priority override default commands by ocamlfind ones *) Options.ocamlc := ocamlfind & A"ocamlc"; Options.ocamlopt := ocamlfind & A"ocamlopt"; Options.ocamldep := ocamlfind & A"ocamldep"; Options.ocamldoc := ocamlfind & A"ocamldoc"; Options.ocamlmktop := ocamlfind & A"ocamlmktop"; Options.ocamlmklib := ocamlfind & A"ocamlmklib" | After_rules -> (* When one link an OCaml library/binary/package, one should use * -linkpkg *) flag ["ocaml"; "link"; "program"] & A"-linkpkg"; if not (conf.no_automatic_syntax) then begin (* For each ocamlfind package one inject the -package option when * compiling, computing dependencies, generating documentation and * linking. *) List.iter begin fun pkg -> let base_args = [A"-package"; A pkg] in (* TODO: consider how to really choose camlp4o or camlp4r. *) let syn_args = [A"-syntax"; A "camlp4o"] in let (args, pargs) = (* Heuristic to identify syntax extensions: whether they end in ".syntax"; some might not. *) if Filename.check_suffix pkg "syntax" || List.mem pkg well_known_syntax then (syn_args @ base_args, syn_args) else (base_args, []) in flag ["ocaml"; "compile"; "pkg_"^pkg] & S args; flag ["ocaml"; "ocamldep"; "pkg_"^pkg] & S args; flag ["ocaml"; "doc"; "pkg_"^pkg] & S args; flag ["ocaml"; "link"; "pkg_"^pkg] & S base_args; flag ["ocaml"; "infer_interface"; "pkg_"^pkg] & S args; (* TODO: Check if this is allowed for OCaml < 3.12.1 *) flag ["ocaml"; "compile"; "package("^pkg^")"] & S pargs; flag ["ocaml"; "ocamldep"; "package("^pkg^")"] & S pargs; flag ["ocaml"; "doc"; "package("^pkg^")"] & S pargs; flag ["ocaml"; "infer_interface"; "package("^pkg^")"] & S pargs; end (find_packages ()); end; (* Like -package but for extensions syntax. Morover -syntax is useless * when linking. *) List.iter begin fun syntax -> flag ["ocaml"; "compile"; "syntax_"^syntax] & S[A"-syntax"; A syntax]; flag ["ocaml"; "ocamldep"; "syntax_"^syntax] & S[A"-syntax"; A syntax]; flag ["ocaml"; "doc"; "syntax_"^syntax] & S[A"-syntax"; A syntax]; flag ["ocaml"; "infer_interface"; "syntax_"^syntax] & S[A"-syntax"; A syntax]; end (find_syntaxes ()); (* The default "thread" tag is not compatible with ocamlfind. * Indeed, the default rules add the "threads.cma" or "threads.cmxa" * options when using this tag. When using the "-linkpkg" option with * ocamlfind, this module will then be added twice on the command line. * * To solve this, one approach is to add the "-thread" option when using * the "threads" package using the previous plugin. *) flag ["ocaml"; "pkg_threads"; "compile"] (S[A "-thread"]); flag ["ocaml"; "pkg_threads"; "doc"] (S[A "-I"; A "+threads"]); flag ["ocaml"; "pkg_threads"; "link"] (S[A "-thread"]); flag ["ocaml"; "pkg_threads"; "infer_interface"] (S[A "-thread"]); flag ["ocaml"; "package(threads)"; "compile"] (S[A "-thread"]); flag ["ocaml"; "package(threads)"; "doc"] (S[A "-I"; A "+threads"]); flag ["ocaml"; "package(threads)"; "link"] (S[A "-thread"]); flag ["ocaml"; "package(threads)"; "infer_interface"] (S[A "-thread"]); | _ -> () end module MyOCamlbuildBase = struct (* # 22 "src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *) (** Base functions for writing myocamlbuild.ml @author Sylvain Le Gall *) open Ocamlbuild_plugin module OC = Ocamlbuild_pack.Ocaml_compiler type dir = string type file = string type name = string type tag = string (* # 62 "src/plugins/ocamlbuild/MyOCamlbuildBase.ml" *) type t = { lib_ocaml: (name * dir list * string list) list; lib_c: (name * dir * file list) list; flags: (tag list * (spec OASISExpr.choices)) list; (* Replace the 'dir: include' from _tags by a precise interdepends in * directory. *) includes: (dir * dir list) list; } let env_filename = Pathname.basename BaseEnvLight.default_filename let dispatch_combine lst = fun e -> List.iter (fun dispatch -> dispatch e) lst let tag_libstubs nm = "use_lib"^nm^"_stubs" let nm_libstubs nm = nm^"_stubs" let dispatch t e = let env = BaseEnvLight.load ~filename:env_filename ~allow_empty:true () in match e with | Before_options -> let no_trailing_dot s = if String.length s >= 1 && s.[0] = '.' then String.sub s 1 ((String.length s) - 1) else s in List.iter (fun (opt, var) -> try opt := no_trailing_dot (BaseEnvLight.var_get var env) with Not_found -> Printf.eprintf "W: Cannot get variable %s\n" var) [ Options.ext_obj, "ext_obj"; Options.ext_lib, "ext_lib"; Options.ext_dll, "ext_dll"; ] | After_rules -> (* Declare OCaml libraries *) List.iter (function | nm, [], intf_modules -> ocaml_lib nm; let cmis = List.map (fun m -> (String.uncapitalize m) ^ ".cmi") intf_modules in dep ["ocaml"; "link"; "library"; "file:"^nm^".cma"] cmis | nm, dir :: tl, intf_modules -> ocaml_lib ~dir:dir (dir^"/"^nm); List.iter (fun dir -> List.iter (fun str -> flag ["ocaml"; "use_"^nm; str] (S[A"-I"; P dir])) ["compile"; "infer_interface"; "doc"]) tl; let cmis = List.map (fun m -> dir^"/"^(String.uncapitalize m)^".cmi") intf_modules in dep ["ocaml"; "link"; "library"; "file:"^dir^"/"^nm^".cma"] cmis) t.lib_ocaml; (* Declare directories dependencies, replace "include" in _tags. *) List.iter (fun (dir, include_dirs) -> Pathname.define_context dir include_dirs) t.includes; (* Declare C libraries *) List.iter (fun (lib, dir, headers) -> (* Handle C part of library *) flag ["link"; "library"; "ocaml"; "byte"; tag_libstubs lib] (S[A"-dllib"; A("-l"^(nm_libstubs lib)); A"-cclib"; A("-l"^(nm_libstubs lib))]); flag ["link"; "library"; "ocaml"; "native"; tag_libstubs lib] (S[A"-cclib"; A("-l"^(nm_libstubs lib))]); flag ["link"; "program"; "ocaml"; "byte"; tag_libstubs lib] (S[A"-dllib"; A("dll"^(nm_libstubs lib))]); (* When ocaml link something that use the C library, then one need that file to be up to date. This holds both for programs and for libraries. *) dep ["link"; "ocaml"; tag_libstubs lib] [dir/"lib"^(nm_libstubs lib)^"."^(!Options.ext_lib)]; dep ["compile"; "ocaml"; tag_libstubs lib] [dir/"lib"^(nm_libstubs lib)^"."^(!Options.ext_lib)]; (* TODO: be more specific about what depends on headers *) (* Depends on .h files *) dep ["compile"; "c"] headers; (* Setup search path for lib *) flag ["link"; "ocaml"; "use_"^lib] (S[A"-I"; P(dir)]); ) t.lib_c; (* Add flags *) List.iter (fun (tags, cond_specs) -> let spec = BaseEnvLight.var_choose cond_specs env in let rec eval_specs = function | S lst -> S (List.map eval_specs lst) | A str -> A (BaseEnvLight.var_expand str env) | spec -> spec in flag tags & (eval_specs spec)) t.flags | _ -> () let dispatch_default conf t = dispatch_combine [ dispatch t; MyOCamlbuildFindlib.dispatch conf; ] end # 606 "myocamlbuild.ml" open Ocamlbuild_plugin;; let package_default = { MyOCamlbuildBase.lib_ocaml = [("pgocaml", ["src"], []); ("pa_pgsql", ["src"], [])]; lib_c = []; flags = []; includes = [("utils", ["src"]); ("tests", ["src"])] } ;; let conf = {MyOCamlbuildFindlib.no_automatic_syntax = false} let dispatch_default = MyOCamlbuildBase.dispatch_default conf package_default;; # 623 "myocamlbuild.ml" (* OASIS_STOP *) Ocamlbuild_plugin.dispatch dispatch_default;; pgocaml-2.2/setup.ml000066400000000000000000005660071251121767700145200ustar00rootroot00000000000000(* setup.ml generated for the first time by OASIS v0.3.0~rc6 *) (* OASIS_START *) (* DO NOT EDIT (digest: bb76fd3ff2c896e5074a6dc636bcc5e8) *) (* Regenerated by OASIS v0.4.5 Visit http://oasis.forge.ocamlcore.org for more information and documentation about functions used in this file. *) module OASISGettext = struct (* # 22 "src/oasis/OASISGettext.ml" *) let ns_ str = str let s_ str = str let f_ (str: ('a, 'b, 'c, 'd) format4) = str let fn_ fmt1 fmt2 n = if n = 1 then fmt1^^"" else fmt2^^"" let init = [] end module OASISContext = struct (* # 22 "src/oasis/OASISContext.ml" *) open OASISGettext type level = [ `Debug | `Info | `Warning | `Error] type t = { (* TODO: replace this by a proplist. *) quiet: bool; info: bool; debug: bool; ignore_plugins: bool; ignore_unknown_fields: bool; printf: level -> string -> unit; } let printf lvl str = let beg = match lvl with | `Error -> s_ "E: " | `Warning -> s_ "W: " | `Info -> s_ "I: " | `Debug -> s_ "D: " in prerr_endline (beg^str) let default = ref { quiet = false; info = false; debug = false; ignore_plugins = false; ignore_unknown_fields = false; printf = printf; } let quiet = {!default with quiet = true} let fspecs () = (* TODO: don't act on default. *) let ignore_plugins = ref false in ["-quiet", Arg.Unit (fun () -> default := {!default with quiet = true}), s_ " Run quietly"; "-info", Arg.Unit (fun () -> default := {!default with info = true}), s_ " Display information message"; "-debug", Arg.Unit (fun () -> default := {!default with debug = true}), s_ " Output debug message"; "-ignore-plugins", Arg.Set ignore_plugins, s_ " Ignore plugin's field."; "-C", (* TODO: remove this chdir. *) Arg.String (fun str -> Sys.chdir str), s_ "dir Change directory before running."], fun () -> {!default with ignore_plugins = !ignore_plugins} end module OASISString = struct (* # 22 "src/oasis/OASISString.ml" *) (** Various string utilities. Mostly inspired by extlib and batteries ExtString and BatString libraries. @author Sylvain Le Gall *) let nsplitf str f = if str = "" then [] else let buf = Buffer.create 13 in let lst = ref [] in let push () = lst := Buffer.contents buf :: !lst; Buffer.clear buf in let str_len = String.length str in for i = 0 to str_len - 1 do if f str.[i] then push () else Buffer.add_char buf str.[i] done; push (); List.rev !lst (** [nsplit c s] Split the string [s] at char [c]. It doesn't include the separator. *) let nsplit str c = nsplitf str ((=) c) let find ~what ?(offset=0) str = let what_idx = ref 0 in let str_idx = ref offset in while !str_idx < String.length str && !what_idx < String.length what do if str.[!str_idx] = what.[!what_idx] then incr what_idx else what_idx := 0; incr str_idx done; if !what_idx <> String.length what then raise Not_found else !str_idx - !what_idx let sub_start str len = let str_len = String.length str in if len >= str_len then "" else String.sub str len (str_len - len) let sub_end ?(offset=0) str len = let str_len = String.length str in if len >= str_len then "" else String.sub str 0 (str_len - len) let starts_with ~what ?(offset=0) str = let what_idx = ref 0 in let str_idx = ref offset in let ok = ref true in while !ok && !str_idx < String.length str && !what_idx < String.length what do if str.[!str_idx] = what.[!what_idx] then incr what_idx else ok := false; incr str_idx done; if !what_idx = String.length what then true else false let strip_starts_with ~what str = if starts_with ~what str then sub_start str (String.length what) else raise Not_found let ends_with ~what ?(offset=0) str = let what_idx = ref ((String.length what) - 1) in let str_idx = ref ((String.length str) - 1) in let ok = ref true in while !ok && offset <= !str_idx && 0 <= !what_idx do if str.[!str_idx] = what.[!what_idx] then decr what_idx else ok := false; decr str_idx done; if !what_idx = -1 then true else false let strip_ends_with ~what str = if ends_with ~what str then sub_end str (String.length what) else raise Not_found let replace_chars f s = let buf = Buffer.create (String.length s) in String.iter (fun c -> Buffer.add_char buf (f c)) s; Buffer.contents buf end module OASISUtils = struct (* # 22 "src/oasis/OASISUtils.ml" *) open OASISGettext module MapExt = struct module type S = sig include Map.S val add_list: 'a t -> (key * 'a) list -> 'a t val of_list: (key * 'a) list -> 'a t val to_list: 'a t -> (key * 'a) list end module Make (Ord: Map.OrderedType) = struct include Map.Make(Ord) let rec add_list t = function | (k, v) :: tl -> add_list (add k v t) tl | [] -> t let of_list lst = add_list empty lst let to_list t = fold (fun k v acc -> (k, v) :: acc) t [] end end module MapString = MapExt.Make(String) module SetExt = struct module type S = sig include Set.S val add_list: t -> elt list -> t val of_list: elt list -> t val to_list: t -> elt list end module Make (Ord: Set.OrderedType) = struct include Set.Make(Ord) let rec add_list t = function | e :: tl -> add_list (add e t) tl | [] -> t let of_list lst = add_list empty lst let to_list = elements end end module SetString = SetExt.Make(String) let compare_csl s1 s2 = String.compare (String.lowercase s1) (String.lowercase s2) module HashStringCsl = Hashtbl.Make (struct type t = string let equal s1 s2 = (String.lowercase s1) = (String.lowercase s2) let hash s = Hashtbl.hash (String.lowercase s) end) module SetStringCsl = SetExt.Make (struct type t = string let compare = compare_csl end) let varname_of_string ?(hyphen='_') s = if String.length s = 0 then begin invalid_arg "varname_of_string" end else begin let buf = OASISString.replace_chars (fun c -> if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') then c else hyphen) s; in let buf = (* Start with a _ if digit *) if '0' <= s.[0] && s.[0] <= '9' then "_"^buf else buf in String.lowercase buf end let varname_concat ?(hyphen='_') p s = let what = String.make 1 hyphen in let p = try OASISString.strip_ends_with ~what p with Not_found -> p in let s = try OASISString.strip_starts_with ~what s with Not_found -> s in p^what^s let is_varname str = str = varname_of_string str let failwithf fmt = Printf.ksprintf failwith fmt end module PropList = struct (* # 22 "src/oasis/PropList.ml" *) open OASISGettext type name = string exception Not_set of name * string option exception No_printer of name exception Unknown_field of name * name let () = Printexc.register_printer (function | Not_set (nm, Some rsn) -> Some (Printf.sprintf (f_ "Field '%s' is not set: %s") nm rsn) | Not_set (nm, None) -> Some (Printf.sprintf (f_ "Field '%s' is not set") nm) | No_printer nm -> Some (Printf.sprintf (f_ "No default printer for value %s") nm) | Unknown_field (nm, schm) -> Some (Printf.sprintf (f_ "Field %s is not defined in schema %s") nm schm) | _ -> None) module Data = struct type t = (name, unit -> unit) Hashtbl.t let create () = Hashtbl.create 13 let clear t = Hashtbl.clear t (* # 78 "src/oasis/PropList.ml" *) end module Schema = struct type ('ctxt, 'extra) value = { get: Data.t -> string; set: Data.t -> ?context:'ctxt -> string -> unit; help: (unit -> string) option; extra: 'extra; } type ('ctxt, 'extra) t = { name: name; fields: (name, ('ctxt, 'extra) value) Hashtbl.t; order: name Queue.t; name_norm: string -> string; } let create ?(case_insensitive=false) nm = { name = nm; fields = Hashtbl.create 13; order = Queue.create (); name_norm = (if case_insensitive then String.lowercase else fun s -> s); } let add t nm set get extra help = let key = t.name_norm nm in if Hashtbl.mem t.fields key then failwith (Printf.sprintf (f_ "Field '%s' is already defined in schema '%s'") nm t.name); Hashtbl.add t.fields key { set = set; get = get; help = help; extra = extra; }; Queue.add nm t.order let mem t nm = Hashtbl.mem t.fields nm let find t nm = try Hashtbl.find t.fields (t.name_norm nm) with Not_found -> raise (Unknown_field (nm, t.name)) let get t data nm = (find t nm).get data let set t data nm ?context x = (find t nm).set data ?context x let fold f acc t = Queue.fold (fun acc k -> let v = find t k in f acc k v.extra v.help) acc t.order let iter f t = fold (fun () -> f) () t let name t = t.name end module Field = struct type ('ctxt, 'value, 'extra) t = { set: Data.t -> ?context:'ctxt -> 'value -> unit; get: Data.t -> 'value; sets: Data.t -> ?context:'ctxt -> string -> unit; gets: Data.t -> string; help: (unit -> string) option; extra: 'extra; } let new_id = let last_id = ref 0 in fun () -> incr last_id; !last_id let create ?schema ?name ?parse ?print ?default ?update ?help extra = (* Default value container *) let v = ref None in (* If name is not given, create unique one *) let nm = match name with | Some s -> s | None -> Printf.sprintf "_anon_%d" (new_id ()) in (* Last chance to get a value: the default *) let default () = match default with | Some d -> d | None -> raise (Not_set (nm, Some (s_ "no default value"))) in (* Get data *) let get data = (* Get value *) try (Hashtbl.find data nm) (); match !v with | Some x -> x | None -> default () with Not_found -> default () in (* Set data *) let set data ?context x = let x = match update with | Some f -> begin try f ?context (get data) x with Not_set _ -> x end | None -> x in Hashtbl.replace data nm (fun () -> v := Some x) in (* Parse string value, if possible *) let parse = match parse with | Some f -> f | None -> fun ?context s -> failwith (Printf.sprintf (f_ "Cannot parse field '%s' when setting value %S") nm s) in (* Set data, from string *) let sets data ?context s = set ?context data (parse ?context s) in (* Output value as string, if possible *) let print = match print with | Some f -> f | None -> fun _ -> raise (No_printer nm) in (* Get data, as a string *) let gets data = print (get data) in begin match schema with | Some t -> Schema.add t nm sets gets extra help | None -> () end; { set = set; get = get; sets = sets; gets = gets; help = help; extra = extra; } let fset data t ?context x = t.set data ?context x let fget data t = t.get data let fsets data t ?context s = t.sets data ?context s let fgets data t = t.gets data end module FieldRO = struct let create ?schema ?name ?parse ?print ?default ?update ?help extra = let fld = Field.create ?schema ?name ?parse ?print ?default ?update ?help extra in fun data -> Field.fget data fld end end module OASISMessage = struct (* # 22 "src/oasis/OASISMessage.ml" *) open OASISGettext open OASISContext let generic_message ~ctxt lvl fmt = let cond = if ctxt.quiet then false else match lvl with | `Debug -> ctxt.debug | `Info -> ctxt.info | _ -> true in Printf.ksprintf (fun str -> if cond then begin ctxt.printf lvl str end) fmt let debug ~ctxt fmt = generic_message ~ctxt `Debug fmt let info ~ctxt fmt = generic_message ~ctxt `Info fmt let warning ~ctxt fmt = generic_message ~ctxt `Warning fmt let error ~ctxt fmt = generic_message ~ctxt `Error fmt end module OASISVersion = struct (* # 22 "src/oasis/OASISVersion.ml" *) open OASISGettext type s = string type t = string type comparator = | VGreater of t | VGreaterEqual of t | VEqual of t | VLesser of t | VLesserEqual of t | VOr of comparator * comparator | VAnd of comparator * comparator (* Range of allowed characters *) let is_digit c = '0' <= c && c <= '9' let is_alpha c = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') let is_special = function | '.' | '+' | '-' | '~' -> true | _ -> false let rec version_compare v1 v2 = if v1 <> "" || v2 <> "" then begin (* Compare ascii string, using special meaning for version * related char *) let val_ascii c = if c = '~' then -1 else if is_digit c then 0 else if c = '\000' then 0 else if is_alpha c then Char.code c else (Char.code c) + 256 in let len1 = String.length v1 in let len2 = String.length v2 in let p = ref 0 in (** Compare ascii part *) let compare_vascii () = let cmp = ref 0 in while !cmp = 0 && !p < len1 && !p < len2 && not (is_digit v1.[!p] && is_digit v2.[!p]) do cmp := (val_ascii v1.[!p]) - (val_ascii v2.[!p]); incr p done; if !cmp = 0 && !p < len1 && !p = len2 then val_ascii v1.[!p] else if !cmp = 0 && !p = len1 && !p < len2 then - (val_ascii v2.[!p]) else !cmp in (** Compare digit part *) let compare_digit () = let extract_int v p = let start_p = !p in while !p < String.length v && is_digit v.[!p] do incr p done; let substr = String.sub v !p ((String.length v) - !p) in let res = match String.sub v start_p (!p - start_p) with | "" -> 0 | s -> int_of_string s in res, substr in let i1, tl1 = extract_int v1 (ref !p) in let i2, tl2 = extract_int v2 (ref !p) in i1 - i2, tl1, tl2 in match compare_vascii () with | 0 -> begin match compare_digit () with | 0, tl1, tl2 -> if tl1 <> "" && is_digit tl1.[0] then 1 else if tl2 <> "" && is_digit tl2.[0] then -1 else version_compare tl1 tl2 | n, _, _ -> n end | n -> n end else begin 0 end let version_of_string str = str let string_of_version t = t let version_compare_string s1 s2 = version_compare (version_of_string s1) (version_of_string s2) let chop t = try let pos = String.rindex t '.' in String.sub t 0 pos with Not_found -> t let rec comparator_apply v op = match op with | VGreater cv -> (version_compare v cv) > 0 | VGreaterEqual cv -> (version_compare v cv) >= 0 | VLesser cv -> (version_compare v cv) < 0 | VLesserEqual cv -> (version_compare v cv) <= 0 | VEqual cv -> (version_compare v cv) = 0 | VOr (op1, op2) -> (comparator_apply v op1) || (comparator_apply v op2) | VAnd (op1, op2) -> (comparator_apply v op1) && (comparator_apply v op2) let rec string_of_comparator = function | VGreater v -> "> "^(string_of_version v) | VEqual v -> "= "^(string_of_version v) | VLesser v -> "< "^(string_of_version v) | VGreaterEqual v -> ">= "^(string_of_version v) | VLesserEqual v -> "<= "^(string_of_version v) | VOr (c1, c2) -> (string_of_comparator c1)^" || "^(string_of_comparator c2) | VAnd (c1, c2) -> (string_of_comparator c1)^" && "^(string_of_comparator c2) let rec varname_of_comparator = let concat p v = OASISUtils.varname_concat p (OASISUtils.varname_of_string (string_of_version v)) in function | VGreater v -> concat "gt" v | VLesser v -> concat "lt" v | VEqual v -> concat "eq" v | VGreaterEqual v -> concat "ge" v | VLesserEqual v -> concat "le" v | VOr (c1, c2) -> (varname_of_comparator c1)^"_or_"^(varname_of_comparator c2) | VAnd (c1, c2) -> (varname_of_comparator c1)^"_and_"^(varname_of_comparator c2) let rec comparator_ge v' = let cmp v = version_compare v v' >= 0 in function | VEqual v | VGreaterEqual v | VGreater v -> cmp v | VLesserEqual _ | VLesser _ -> false | VOr (c1, c2) -> comparator_ge v' c1 || comparator_ge v' c2 | VAnd (c1, c2) -> comparator_ge v' c1 && comparator_ge v' c2 end module OASISLicense = struct (* # 22 "src/oasis/OASISLicense.ml" *) (** License for _oasis fields @author Sylvain Le Gall *) type license = string type license_exception = string type license_version = | Version of OASISVersion.t | VersionOrLater of OASISVersion.t | NoVersion type license_dep_5_unit = { license: license; excption: license_exception option; version: license_version; } type license_dep_5 = | DEP5Unit of license_dep_5_unit | DEP5Or of license_dep_5 list | DEP5And of license_dep_5 list type t = | DEP5License of license_dep_5 | OtherLicense of string (* URL *) end module OASISExpr = struct (* # 22 "src/oasis/OASISExpr.ml" *) open OASISGettext type test = string type flag = string type t = | EBool of bool | ENot of t | EAnd of t * t | EOr of t * t | EFlag of flag | ETest of test * string type 'a choices = (t * 'a) list let eval var_get t = let rec eval' = function | EBool b -> b | ENot e -> not (eval' e) | EAnd (e1, e2) -> (eval' e1) && (eval' e2) | EOr (e1, e2) -> (eval' e1) || (eval' e2) | EFlag nm -> let v = var_get nm in assert(v = "true" || v = "false"); (v = "true") | ETest (nm, vl) -> let v = var_get nm in (v = vl) in eval' t let choose ?printer ?name var_get lst = let rec choose_aux = function | (cond, vl) :: tl -> if eval var_get cond then vl else choose_aux tl | [] -> let str_lst = if lst = [] then s_ "" else String.concat (s_ ", ") (List.map (fun (cond, vl) -> match printer with | Some p -> p vl | None -> s_ "") lst) in match name with | Some nm -> failwith (Printf.sprintf (f_ "No result for the choice list '%s': %s") nm str_lst) | None -> failwith (Printf.sprintf (f_ "No result for a choice list: %s") str_lst) in choose_aux (List.rev lst) end module OASISText = struct (* # 22 "src/oasis/OASISText.ml" *) type elt = | Para of string | Verbatim of string | BlankLine type t = elt list end module OASISTypes = struct (* # 22 "src/oasis/OASISTypes.ml" *) type name = string type package_name = string type url = string type unix_dirname = string type unix_filename = string type host_dirname = string type host_filename = string type prog = string type arg = string type args = string list type command_line = (prog * arg list) type findlib_name = string type findlib_full = string type compiled_object = | Byte | Native | Best type dependency = | FindlibPackage of findlib_full * OASISVersion.comparator option | InternalLibrary of name type tool = | ExternalTool of name | InternalExecutable of name type vcs = | Darcs | Git | Svn | Cvs | Hg | Bzr | Arch | Monotone | OtherVCS of url type plugin_kind = [ `Configure | `Build | `Doc | `Test | `Install | `Extra ] type plugin_data_purpose = [ `Configure | `Build | `Install | `Clean | `Distclean | `Install | `Uninstall | `Test | `Doc | `Extra | `Other of string ] type 'a plugin = 'a * name * OASISVersion.t option type all_plugin = plugin_kind plugin type plugin_data = (all_plugin * plugin_data_purpose * (unit -> unit)) list (* # 115 "src/oasis/OASISTypes.ml" *) type 'a conditional = 'a OASISExpr.choices type custom = { pre_command: (command_line option) conditional; post_command: (command_line option) conditional; } type common_section = { cs_name: name; cs_data: PropList.Data.t; cs_plugin_data: plugin_data; } type build_section = { bs_build: bool conditional; bs_install: bool conditional; bs_path: unix_dirname; bs_compiled_object: compiled_object; bs_build_depends: dependency list; bs_build_tools: tool list; bs_c_sources: unix_filename list; bs_data_files: (unix_filename * unix_filename option) list; bs_ccopt: args conditional; bs_cclib: args conditional; bs_dlllib: args conditional; bs_dllpath: args conditional; bs_byteopt: args conditional; bs_nativeopt: args conditional; } type library = { lib_modules: string list; lib_pack: bool; lib_internal_modules: string list; lib_findlib_parent: findlib_name option; lib_findlib_name: findlib_name option; lib_findlib_containers: findlib_name list; } type object_ = { obj_modules: string list; obj_findlib_fullname: findlib_name list option; } type executable = { exec_custom: bool; exec_main_is: unix_filename; } type flag = { flag_description: string option; flag_default: bool conditional; } type source_repository = { src_repo_type: vcs; src_repo_location: url; src_repo_browser: url option; src_repo_module: string option; src_repo_branch: string option; src_repo_tag: string option; src_repo_subdir: unix_filename option; } type test = { test_type: [`Test] plugin; test_command: command_line conditional; test_custom: custom; test_working_directory: unix_filename option; test_run: bool conditional; test_tools: tool list; } type doc_format = | HTML of unix_filename | DocText | PDF | PostScript | Info of unix_filename | DVI | OtherDoc type doc = { doc_type: [`Doc] plugin; doc_custom: custom; doc_build: bool conditional; doc_install: bool conditional; doc_install_dir: unix_filename; doc_title: string; doc_authors: string list; doc_abstract: string option; doc_format: doc_format; doc_data_files: (unix_filename * unix_filename option) list; doc_build_tools: tool list; } type section = | Library of common_section * build_section * library | Object of common_section * build_section * object_ | Executable of common_section * build_section * executable | Flag of common_section * flag | SrcRepo of common_section * source_repository | Test of common_section * test | Doc of common_section * doc type section_kind = [ `Library | `Object | `Executable | `Flag | `SrcRepo | `Test | `Doc ] type package = { oasis_version: OASISVersion.t; ocaml_version: OASISVersion.comparator option; findlib_version: OASISVersion.comparator option; alpha_features: string list; beta_features: string list; name: package_name; version: OASISVersion.t; license: OASISLicense.t; license_file: unix_filename option; copyrights: string list; maintainers: string list; authors: string list; homepage: url option; synopsis: string; description: OASISText.t option; categories: url list; conf_type: [`Configure] plugin; conf_custom: custom; build_type: [`Build] plugin; build_custom: custom; install_type: [`Install] plugin; install_custom: custom; uninstall_custom: custom; clean_custom: custom; distclean_custom: custom; files_ab: unix_filename list; sections: section list; plugins: [`Extra] plugin list; disable_oasis_section: unix_filename list; schema_data: PropList.Data.t; plugin_data: plugin_data; } end module OASISFeatures = struct (* # 22 "src/oasis/OASISFeatures.ml" *) open OASISTypes open OASISUtils open OASISGettext open OASISVersion module MapPlugin = Map.Make (struct type t = plugin_kind * name let compare = Pervasives.compare end) module Data = struct type t = { oasis_version: OASISVersion.t; plugin_versions: OASISVersion.t option MapPlugin.t; alpha_features: string list; beta_features: string list; } let create oasis_version alpha_features beta_features = { oasis_version = oasis_version; plugin_versions = MapPlugin.empty; alpha_features = alpha_features; beta_features = beta_features } let of_package pkg = create pkg.OASISTypes.oasis_version pkg.OASISTypes.alpha_features pkg.OASISTypes.beta_features let add_plugin (plugin_kind, plugin_name, plugin_version) t = {t with plugin_versions = MapPlugin.add (plugin_kind, plugin_name) plugin_version t.plugin_versions} let plugin_version plugin_kind plugin_name t = MapPlugin.find (plugin_kind, plugin_name) t.plugin_versions let to_string t = Printf.sprintf "oasis_version: %s; alpha_features: %s; beta_features: %s; \ plugins_version: %s" (OASISVersion.string_of_version t.oasis_version) (String.concat ", " t.alpha_features) (String.concat ", " t.beta_features) (String.concat ", " (MapPlugin.fold (fun (_, plg) ver_opt acc -> (plg^ (match ver_opt with | Some v -> " "^(OASISVersion.string_of_version v) | None -> "")) :: acc) t.plugin_versions [])) end type origin = | Field of string * string | Section of string | NoOrigin type stage = Alpha | Beta let string_of_stage = function | Alpha -> "alpha" | Beta -> "beta" let field_of_stage = function | Alpha -> "AlphaFeatures" | Beta -> "BetaFeatures" type publication = InDev of stage | SinceVersion of OASISVersion.t type t = { name: string; plugin: all_plugin option; publication: publication; description: unit -> string; } (* TODO: mutex protect this. *) let all_features = Hashtbl.create 13 let since_version ver_str = SinceVersion (version_of_string ver_str) let alpha = InDev Alpha let beta = InDev Beta let to_string t = Printf.sprintf "feature: %s; plugin: %s; publication: %s" t.name (match t.plugin with | None -> "" | Some (_, nm, _) -> nm) (match t.publication with | InDev stage -> string_of_stage stage | SinceVersion ver -> ">= "^(OASISVersion.string_of_version ver)) let data_check t data origin = let no_message = "no message" in let check_feature features stage = let has_feature = List.mem t.name features in if not has_feature then match origin with | Field (fld, where) -> Some (Printf.sprintf (f_ "Field %s in %s is only available when feature %s \ is in field %s.") fld where t.name (field_of_stage stage)) | Section sct -> Some (Printf.sprintf (f_ "Section %s is only available when features %s \ is in field %s.") sct t.name (field_of_stage stage)) | NoOrigin -> Some no_message else None in let version_is_good ~min_version version fmt = let version_is_good = OASISVersion.comparator_apply version (OASISVersion.VGreaterEqual min_version) in Printf.ksprintf (fun str -> if version_is_good then None else Some str) fmt in match origin, t.plugin, t.publication with | _, _, InDev Alpha -> check_feature data.Data.alpha_features Alpha | _, _, InDev Beta -> check_feature data.Data.beta_features Beta | Field(fld, where), None, SinceVersion min_version -> version_is_good ~min_version data.Data.oasis_version (f_ "Field %s in %s is only valid since OASIS v%s, update \ OASISFormat field from '%s' to '%s' after checking \ OASIS changelog.") fld where (string_of_version min_version) (string_of_version data.Data.oasis_version) (string_of_version min_version) | Field(fld, where), Some(plugin_knd, plugin_name, _), SinceVersion min_version -> begin try let plugin_version_current = try match Data.plugin_version plugin_knd plugin_name data with | Some ver -> ver | None -> failwithf (f_ "Field %s in %s is only valid for the OASIS \ plugin %s since v%s, but no plugin version is \ defined in the _oasis file, change '%s' to \ '%s (%s)' in your _oasis file.") fld where plugin_name (string_of_version min_version) plugin_name plugin_name (string_of_version min_version) with Not_found -> failwithf (f_ "Field %s in %s is only valid when the OASIS plugin %s \ is defined.") fld where plugin_name in version_is_good ~min_version plugin_version_current (f_ "Field %s in %s is only valid for the OASIS plugin %s \ since v%s, update your plugin from '%s (%s)' to \ '%s (%s)' after checking the plugin's changelog.") fld where plugin_name (string_of_version min_version) plugin_name (string_of_version plugin_version_current) plugin_name (string_of_version min_version) with Failure msg -> Some msg end | Section sct, None, SinceVersion min_version -> version_is_good ~min_version data.Data.oasis_version (f_ "Section %s is only valid for since OASIS v%s, update \ OASISFormat field from '%s' to '%s' after checking OASIS \ changelog.") sct (string_of_version min_version) (string_of_version data.Data.oasis_version) (string_of_version min_version) | Section sct, Some(plugin_knd, plugin_name, _), SinceVersion min_version -> begin try let plugin_version_current = try match Data.plugin_version plugin_knd plugin_name data with | Some ver -> ver | None -> failwithf (f_ "Section %s is only valid for the OASIS \ plugin %s since v%s, but no plugin version is \ defined in the _oasis file, change '%s' to \ '%s (%s)' in your _oasis file.") sct plugin_name (string_of_version min_version) plugin_name plugin_name (string_of_version min_version) with Not_found -> failwithf (f_ "Section %s is only valid when the OASIS plugin %s \ is defined.") sct plugin_name in version_is_good ~min_version plugin_version_current (f_ "Section %s is only valid for the OASIS plugin %s \ since v%s, update your plugin from '%s (%s)' to \ '%s (%s)' after checking the plugin's changelog.") sct plugin_name (string_of_version min_version) plugin_name (string_of_version plugin_version_current) plugin_name (string_of_version min_version) with Failure msg -> Some msg end | NoOrigin, None, SinceVersion min_version -> version_is_good ~min_version data.Data.oasis_version "%s" no_message | NoOrigin, Some(plugin_knd, plugin_name, _), SinceVersion min_version -> begin try let plugin_version_current = match Data.plugin_version plugin_knd plugin_name data with | Some ver -> ver | None -> raise Not_found in version_is_good ~min_version plugin_version_current "%s" no_message with Not_found -> Some no_message end let data_assert t data origin = match data_check t data origin with | None -> () | Some str -> failwith str let data_test t data = match data_check t data NoOrigin with | None -> true | Some str -> false let package_test t pkg = data_test t (Data.of_package pkg) let create ?plugin name publication description = let () = if Hashtbl.mem all_features name then failwithf "Feature '%s' is already declared." name in let t = { name = name; plugin = plugin; publication = publication; description = description; } in Hashtbl.add all_features name t; t let get_stage name = try (Hashtbl.find all_features name).publication with Not_found -> failwithf (f_ "Feature %s doesn't exist.") name let list () = Hashtbl.fold (fun _ v acc -> v :: acc) all_features [] (* * Real flags. *) let features = create "features_fields" (since_version "0.4") (fun () -> s_ "Enable to experiment not yet official features.") let flag_docs = create "flag_docs" (since_version "0.3") (fun () -> s_ "Building docs require '-docs' flag at configure.") let flag_tests = create "flag_tests" (since_version "0.3") (fun () -> s_ "Running tests require '-tests' flag at configure.") let pack = create "pack" (since_version "0.3") (fun () -> s_ "Allow to create packed library.") let section_object = create "section_object" beta (fun () -> s_ "Implement an object section.") let dynrun_for_release = create "dynrun_for_release" alpha (fun () -> s_ "Make '-setup-update dynamic' suitable for releasing project.") let compiled_setup_ml = create "compiled_setup_ml" alpha (fun () -> s_ "It compiles the setup.ml and speed-up actions done with it.") let disable_oasis_section = create "disable_oasis_section" alpha (fun () -> s_ "Allows the OASIS section comments and digest to be omitted in \ generated files.") let no_automatic_syntax = create "no_automatic_syntax" alpha (fun () -> s_ "Disable the automatic inclusion of -syntax camlp4o for packages \ that matches the internal heuristic (if a dependency ends with \ a .syntax or is a well known syntax).") end module OASISUnixPath = struct (* # 22 "src/oasis/OASISUnixPath.ml" *) type unix_filename = string type unix_dirname = string type host_filename = string type host_dirname = string let current_dir_name = "." let parent_dir_name = ".." let is_current_dir fn = fn = current_dir_name || fn = "" let concat f1 f2 = if is_current_dir f1 then f2 else let f1' = try OASISString.strip_ends_with ~what:"/" f1 with Not_found -> f1 in f1'^"/"^f2 let make = function | hd :: tl -> List.fold_left (fun f p -> concat f p) hd tl | [] -> invalid_arg "OASISUnixPath.make" let dirname f = try String.sub f 0 (String.rindex f '/') with Not_found -> current_dir_name let basename f = try let pos_start = (String.rindex f '/') + 1 in String.sub f pos_start ((String.length f) - pos_start) with Not_found -> f let chop_extension f = try let last_dot = String.rindex f '.' in let sub = String.sub f 0 last_dot in try let last_slash = String.rindex f '/' in if last_slash < last_dot then sub else f with Not_found -> sub with Not_found -> f let capitalize_file f = let dir = dirname f in let base = basename f in concat dir (String.capitalize base) let uncapitalize_file f = let dir = dirname f in let base = basename f in concat dir (String.uncapitalize base) end module OASISHostPath = struct (* # 22 "src/oasis/OASISHostPath.ml" *) open Filename module Unix = OASISUnixPath let make = function | [] -> invalid_arg "OASISHostPath.make" | hd :: tl -> List.fold_left Filename.concat hd tl let of_unix ufn = if Sys.os_type = "Unix" then ufn else make (List.map (fun p -> if p = Unix.current_dir_name then current_dir_name else if p = Unix.parent_dir_name then parent_dir_name else p) (OASISString.nsplit ufn '/')) end module OASISSection = struct (* # 22 "src/oasis/OASISSection.ml" *) open OASISTypes let section_kind_common = function | Library (cs, _, _) -> `Library, cs | Object (cs, _, _) -> `Object, cs | Executable (cs, _, _) -> `Executable, cs | Flag (cs, _) -> `Flag, cs | SrcRepo (cs, _) -> `SrcRepo, cs | Test (cs, _) -> `Test, cs | Doc (cs, _) -> `Doc, cs let section_common sct = snd (section_kind_common sct) let section_common_set cs = function | Library (_, bs, lib) -> Library (cs, bs, lib) | Object (_, bs, obj) -> Object (cs, bs, obj) | Executable (_, bs, exec) -> Executable (cs, bs, exec) | Flag (_, flg) -> Flag (cs, flg) | SrcRepo (_, src_repo) -> SrcRepo (cs, src_repo) | Test (_, tst) -> Test (cs, tst) | Doc (_, doc) -> Doc (cs, doc) (** Key used to identify section *) let section_id sct = let k, cs = section_kind_common sct in k, cs.cs_name let string_of_section sct = let k, nm = section_id sct in (match k with | `Library -> "library" | `Object -> "object" | `Executable -> "executable" | `Flag -> "flag" | `SrcRepo -> "src repository" | `Test -> "test" | `Doc -> "doc") ^" "^nm let section_find id scts = List.find (fun sct -> id = section_id sct) scts module CSection = struct type t = section let id = section_id let compare t1 t2 = compare (id t1) (id t2) let equal t1 t2 = (id t1) = (id t2) let hash t = Hashtbl.hash (id t) end module MapSection = Map.Make(CSection) module SetSection = Set.Make(CSection) end module OASISBuildSection = struct (* # 22 "src/oasis/OASISBuildSection.ml" *) end module OASISExecutable = struct (* # 22 "src/oasis/OASISExecutable.ml" *) open OASISTypes let unix_exec_is (cs, bs, exec) is_native ext_dll suffix_program = let dir = OASISUnixPath.concat bs.bs_path (OASISUnixPath.dirname exec.exec_main_is) in let is_native_exec = match bs.bs_compiled_object with | Native -> true | Best -> is_native () | Byte -> false in OASISUnixPath.concat dir (cs.cs_name^(suffix_program ())), if not is_native_exec && not exec.exec_custom && bs.bs_c_sources <> [] then Some (dir^"/dll"^cs.cs_name^"_stubs"^(ext_dll ())) else None end module OASISLibrary = struct (* # 22 "src/oasis/OASISLibrary.ml" *) open OASISTypes open OASISUtils open OASISGettext open OASISSection (* Look for a module file, considering capitalization or not. *) let find_module source_file_exists bs modul = let possible_base_fn = List.map (OASISUnixPath.concat bs.bs_path) [modul; OASISUnixPath.uncapitalize_file modul; OASISUnixPath.capitalize_file modul] in (* TODO: we should be able to be able to determine the source for every * files. Hence we should introduce a Module(source: fn) for the fields * Modules and InternalModules *) List.fold_left (fun acc base_fn -> match acc with | `No_sources _ -> begin let file_found = List.fold_left (fun acc ext -> if source_file_exists (base_fn^ext) then (base_fn^ext) :: acc else acc) [] [".ml"; ".mli"; ".mll"; ".mly"] in match file_found with | [] -> acc | lst -> `Sources (base_fn, lst) end | `Sources _ -> acc) (`No_sources possible_base_fn) possible_base_fn let source_unix_files ~ctxt (cs, bs, lib) source_file_exists = List.fold_left (fun acc modul -> match find_module source_file_exists bs modul with | `Sources (base_fn, lst) -> (base_fn, lst) :: acc | `No_sources _ -> OASISMessage.warning ~ctxt (f_ "Cannot find source file matching \ module '%s' in library %s") modul cs.cs_name; acc) [] (lib.lib_modules @ lib.lib_internal_modules) let generated_unix_files ~ctxt ~is_native ~has_native_dynlink ~ext_lib ~ext_dll ~source_file_exists (cs, bs, lib) = let find_modules lst ext = let find_module modul = match find_module source_file_exists bs modul with | `Sources (base_fn, [fn]) when ext <> "cmi" && Filename.check_suffix fn ".mli" -> None (* No implementation files for pure interface. *) | `Sources (base_fn, _) -> Some [base_fn] | `No_sources lst -> OASISMessage.warning ~ctxt (f_ "Cannot find source file matching \ module '%s' in library %s") modul cs.cs_name; Some lst in List.fold_left (fun acc nm -> match find_module nm with | None -> acc | Some base_fns -> List.map (fun base_fn -> base_fn ^"."^ext) base_fns :: acc) [] lst in (* The .cmx that be compiled along *) let cmxs = let should_be_built = match bs.bs_compiled_object with | Native -> true | Best -> is_native | Byte -> false in if should_be_built then if lib.lib_pack then find_modules [cs.cs_name] "cmx" else find_modules (lib.lib_modules @ lib.lib_internal_modules) "cmx" else [] in let acc_nopath = [] in (* The headers and annot/cmt files that should be compiled along *) let headers = let sufx = if lib.lib_pack then [".cmti"; ".cmt"; ".annot"] else [".cmi"; ".cmti"; ".cmt"; ".annot"] in List.map begin List.fold_left begin fun accu s -> let dot = String.rindex s '.' in let base = String.sub s 0 dot in List.map ((^) base) sufx @ accu end [] end (find_modules lib.lib_modules "cmi") in (* Compute what libraries should be built *) let acc_nopath = (* Add the packed header file if required *) let add_pack_header acc = if lib.lib_pack then [cs.cs_name^".cmi"; cs.cs_name^".cmti"; cs.cs_name^".cmt"] :: acc else acc in let byte acc = add_pack_header ([cs.cs_name^".cma"] :: acc) in let native acc = let acc = add_pack_header (if has_native_dynlink then [cs.cs_name^".cmxs"] :: acc else acc) in [cs.cs_name^".cmxa"] :: [cs.cs_name^ext_lib] :: acc in match bs.bs_compiled_object with | Native -> byte (native acc_nopath) | Best when is_native -> byte (native acc_nopath) | Byte | Best -> byte acc_nopath in (* Add C library to be built *) let acc_nopath = if bs.bs_c_sources <> [] then begin ["lib"^cs.cs_name^"_stubs"^ext_lib] :: ["dll"^cs.cs_name^"_stubs"^ext_dll] :: acc_nopath end else acc_nopath in (* All the files generated *) List.rev_append (List.rev_map (List.rev_map (OASISUnixPath.concat bs.bs_path)) acc_nopath) (headers @ cmxs) end module OASISObject = struct (* # 22 "src/oasis/OASISObject.ml" *) open OASISTypes open OASISGettext let source_unix_files ~ctxt (cs, bs, obj) source_file_exists = List.fold_left (fun acc modul -> match OASISLibrary.find_module source_file_exists bs modul with | `Sources (base_fn, lst) -> (base_fn, lst) :: acc | `No_sources _ -> OASISMessage.warning ~ctxt (f_ "Cannot find source file matching \ module '%s' in object %s") modul cs.cs_name; acc) [] obj.obj_modules let generated_unix_files ~ctxt ~is_native ~source_file_exists (cs, bs, obj) = let find_module ext modul = match OASISLibrary.find_module source_file_exists bs modul with | `Sources (base_fn, _) -> [base_fn ^ ext] | `No_sources lst -> OASISMessage.warning ~ctxt (f_ "Cannot find source file matching \ module '%s' in object %s") modul cs.cs_name ; lst in let header, byte, native, c_object, f = match obj.obj_modules with | [ m ] -> (find_module ".cmi" m, find_module ".cmo" m, find_module ".cmx" m, find_module ".o" m, fun x -> x) | _ -> ([cs.cs_name ^ ".cmi"], [cs.cs_name ^ ".cmo"], [cs.cs_name ^ ".cmx"], [cs.cs_name ^ ".o"], OASISUnixPath.concat bs.bs_path) in List.map (List.map f) ( match bs.bs_compiled_object with | Native -> native :: c_object :: byte :: header :: [] | Best when is_native -> native :: c_object :: byte :: header :: [] | Byte | Best -> byte :: header :: []) end module OASISFindlib = struct (* # 22 "src/oasis/OASISFindlib.ml" *) open OASISTypes open OASISUtils open OASISGettext open OASISSection type library_name = name type findlib_part_name = name type 'a map_of_findlib_part_name = 'a OASISUtils.MapString.t exception InternalLibraryNotFound of library_name exception FindlibPackageNotFound of findlib_name type group_t = | Container of findlib_name * group_t list | Package of (findlib_name * common_section * build_section * [`Library of library | `Object of object_] * group_t list) type data = common_section * build_section * [`Library of library | `Object of object_] type tree = | Node of (data option) * (tree MapString.t) | Leaf of data let findlib_mapping pkg = (* Map from library name to either full findlib name or parts + parent. *) let fndlb_parts_of_lib_name = let fndlb_parts cs lib = let name = match lib.lib_findlib_name with | Some nm -> nm | None -> cs.cs_name in let name = String.concat "." (lib.lib_findlib_containers @ [name]) in name in List.fold_left (fun mp -> function | Library (cs, _, lib) -> begin let lib_name = cs.cs_name in let fndlb_parts = fndlb_parts cs lib in if MapString.mem lib_name mp then failwithf (f_ "The library name '%s' is used more than once.") lib_name; match lib.lib_findlib_parent with | Some lib_name_parent -> MapString.add lib_name (`Unsolved (lib_name_parent, fndlb_parts)) mp | None -> MapString.add lib_name (`Solved fndlb_parts) mp end | Object (cs, _, obj) -> begin let obj_name = cs.cs_name in if MapString.mem obj_name mp then failwithf (f_ "The object name '%s' is used more than once.") obj_name; let findlib_full_name = match obj.obj_findlib_fullname with | Some ns -> String.concat "." ns | None -> obj_name in MapString.add obj_name (`Solved findlib_full_name) mp end | Executable _ | Test _ | Flag _ | SrcRepo _ | Doc _ -> mp) MapString.empty pkg.sections in (* Solve the above graph to be only library name to full findlib name. *) let fndlb_name_of_lib_name = let rec solve visited mp lib_name lib_name_child = if SetString.mem lib_name visited then failwithf (f_ "Library '%s' is involved in a cycle \ with regard to findlib naming.") lib_name; let visited = SetString.add lib_name visited in try match MapString.find lib_name mp with | `Solved fndlb_nm -> fndlb_nm, mp | `Unsolved (lib_nm_parent, post_fndlb_nm) -> let pre_fndlb_nm, mp = solve visited mp lib_nm_parent lib_name in let fndlb_nm = pre_fndlb_nm^"."^post_fndlb_nm in fndlb_nm, MapString.add lib_name (`Solved fndlb_nm) mp with Not_found -> failwithf (f_ "Library '%s', which is defined as the findlib parent of \ library '%s', doesn't exist.") lib_name lib_name_child in let mp = MapString.fold (fun lib_name status mp -> match status with | `Solved _ -> (* Solved initialy, no need to go further *) mp | `Unsolved _ -> let _, mp = solve SetString.empty mp lib_name "" in mp) fndlb_parts_of_lib_name fndlb_parts_of_lib_name in MapString.map (function | `Solved fndlb_nm -> fndlb_nm | `Unsolved _ -> assert false) mp in (* Convert an internal library name to a findlib name. *) let findlib_name_of_library_name lib_nm = try MapString.find lib_nm fndlb_name_of_lib_name with Not_found -> raise (InternalLibraryNotFound lib_nm) in (* Add a library to the tree. *) let add sct mp = let fndlb_fullname = let cs, _, _ = sct in let lib_name = cs.cs_name in findlib_name_of_library_name lib_name in let rec add_children nm_lst (children: tree MapString.t) = match nm_lst with | (hd :: tl) -> begin let node = try add_node tl (MapString.find hd children) with Not_found -> (* New node *) new_node tl in MapString.add hd node children end | [] -> (* Should not have a nameless library. *) assert false and add_node tl node = if tl = [] then begin match node with | Node (None, children) -> Node (Some sct, children) | Leaf (cs', _, _) | Node (Some (cs', _, _), _) -> (* TODO: allow to merge Package, i.e. * archive(byte) = "foo.cma foo_init.cmo" *) let cs, _, _ = sct in failwithf (f_ "Library '%s' and '%s' have the same findlib name '%s'") cs.cs_name cs'.cs_name fndlb_fullname end else begin match node with | Leaf data -> Node (Some data, add_children tl MapString.empty) | Node (data_opt, children) -> Node (data_opt, add_children tl children) end and new_node = function | [] -> Leaf sct | hd :: tl -> Node (None, MapString.add hd (new_node tl) MapString.empty) in add_children (OASISString.nsplit fndlb_fullname '.') mp in let rec group_of_tree mp = MapString.fold (fun nm node acc -> let cur = match node with | Node (Some (cs, bs, lib), children) -> Package (nm, cs, bs, lib, group_of_tree children) | Node (None, children) -> Container (nm, group_of_tree children) | Leaf (cs, bs, lib) -> Package (nm, cs, bs, lib, []) in cur :: acc) mp [] in let group_mp = List.fold_left (fun mp -> function | Library (cs, bs, lib) -> add (cs, bs, `Library lib) mp | Object (cs, bs, obj) -> add (cs, bs, `Object obj) mp | _ -> mp) MapString.empty pkg.sections in let groups = group_of_tree group_mp in let library_name_of_findlib_name = lazy begin (* Revert findlib_name_of_library_name. *) MapString.fold (fun k v mp -> MapString.add v k mp) fndlb_name_of_lib_name MapString.empty end in let library_name_of_findlib_name fndlb_nm = try MapString.find fndlb_nm (Lazy.force library_name_of_findlib_name) with Not_found -> raise (FindlibPackageNotFound fndlb_nm) in groups, findlib_name_of_library_name, library_name_of_findlib_name let findlib_of_group = function | Container (fndlb_nm, _) | Package (fndlb_nm, _, _, _, _) -> fndlb_nm let root_of_group grp = let rec root_lib_aux = (* We do a DFS in the group. *) function | Container (_, children) -> List.fold_left (fun res grp -> if res = None then root_lib_aux grp else res) None children | Package (_, cs, bs, lib, _) -> Some (cs, bs, lib) in match root_lib_aux grp with | Some res -> res | None -> failwithf (f_ "Unable to determine root library of findlib library '%s'") (findlib_of_group grp) end module OASISFlag = struct (* # 22 "src/oasis/OASISFlag.ml" *) end module OASISPackage = struct (* # 22 "src/oasis/OASISPackage.ml" *) end module OASISSourceRepository = struct (* # 22 "src/oasis/OASISSourceRepository.ml" *) end module OASISTest = struct (* # 22 "src/oasis/OASISTest.ml" *) end module OASISDocument = struct (* # 22 "src/oasis/OASISDocument.ml" *) end module OASISExec = struct (* # 22 "src/oasis/OASISExec.ml" *) open OASISGettext open OASISUtils open OASISMessage (* TODO: I don't like this quote, it is there because $(rm) foo expands to * 'rm -f' foo... *) let run ~ctxt ?f_exit_code ?(quote=true) cmd args = let cmd = if quote then if Sys.os_type = "Win32" then if String.contains cmd ' ' then (* Double the 1st double quote... win32... sigh *) "\""^(Filename.quote cmd) else cmd else Filename.quote cmd else cmd in let cmdline = String.concat " " (cmd :: args) in info ~ctxt (f_ "Running command '%s'") cmdline; match f_exit_code, Sys.command cmdline with | None, 0 -> () | None, i -> failwithf (f_ "Command '%s' terminated with error code %d") cmdline i | Some f, i -> f i let run_read_output ~ctxt ?f_exit_code cmd args = let fn = Filename.temp_file "oasis-" ".txt" in try begin let () = run ~ctxt ?f_exit_code cmd (args @ [">"; Filename.quote fn]) in let chn = open_in fn in let routput = ref [] in begin try while true do routput := (input_line chn) :: !routput done with End_of_file -> () end; close_in chn; Sys.remove fn; List.rev !routput end with e -> (try Sys.remove fn with _ -> ()); raise e let run_read_one_line ~ctxt ?f_exit_code cmd args = match run_read_output ~ctxt ?f_exit_code cmd args with | [fst] -> fst | lst -> failwithf (f_ "Command return unexpected output %S") (String.concat "\n" lst) end module OASISFileUtil = struct (* # 22 "src/oasis/OASISFileUtil.ml" *) open OASISGettext let file_exists_case fn = let dirname = Filename.dirname fn in let basename = Filename.basename fn in if Sys.file_exists dirname then if basename = Filename.current_dir_name then true else List.mem basename (Array.to_list (Sys.readdir dirname)) else false let find_file ?(case_sensitive=true) paths exts = (* Cardinal product of two list *) let ( * ) lst1 lst2 = List.flatten (List.map (fun a -> List.map (fun b -> a, b) lst2) lst1) in let rec combined_paths lst = match lst with | p1 :: p2 :: tl -> let acc = (List.map (fun (a, b) -> Filename.concat a b) (p1 * p2)) in combined_paths (acc :: tl) | [e] -> e | [] -> [] in let alternatives = List.map (fun (p, e) -> if String.length e > 0 && e.[0] <> '.' then p ^ "." ^ e else p ^ e) ((combined_paths paths) * exts) in List.find (fun file -> (if case_sensitive then file_exists_case file else Sys.file_exists file) && not (Sys.is_directory file) ) alternatives let which ~ctxt prg = let path_sep = match Sys.os_type with | "Win32" -> ';' | _ -> ':' in let path_lst = OASISString.nsplit (Sys.getenv "PATH") path_sep in let exec_ext = match Sys.os_type with | "Win32" -> "" :: (OASISString.nsplit (Sys.getenv "PATHEXT") path_sep) | _ -> [""] in find_file ~case_sensitive:false [path_lst; [prg]] exec_ext (**/**) let rec fix_dir dn = (* Windows hack because Sys.file_exists "src\\" = false when * Sys.file_exists "src" = true *) let ln = String.length dn in if Sys.os_type = "Win32" && ln > 0 && dn.[ln - 1] = '\\' then fix_dir (String.sub dn 0 (ln - 1)) else dn let q = Filename.quote (**/**) let cp ~ctxt ?(recurse=false) src tgt = if recurse then match Sys.os_type with | "Win32" -> OASISExec.run ~ctxt "xcopy" [q src; q tgt; "/E"] | _ -> OASISExec.run ~ctxt "cp" ["-r"; q src; q tgt] else OASISExec.run ~ctxt (match Sys.os_type with | "Win32" -> "copy" | _ -> "cp") [q src; q tgt] let mkdir ~ctxt tgt = OASISExec.run ~ctxt (match Sys.os_type with | "Win32" -> "md" | _ -> "mkdir") [q tgt] let rec mkdir_parent ~ctxt f tgt = let tgt = fix_dir tgt in if Sys.file_exists tgt then begin if not (Sys.is_directory tgt) then OASISUtils.failwithf (f_ "Cannot create directory '%s', a file of the same name already \ exists") tgt end else begin mkdir_parent ~ctxt f (Filename.dirname tgt); if not (Sys.file_exists tgt) then begin f tgt; mkdir ~ctxt tgt end end let rmdir ~ctxt tgt = if Sys.readdir tgt = [||] then begin match Sys.os_type with | "Win32" -> OASISExec.run ~ctxt "rd" [q tgt] | _ -> OASISExec.run ~ctxt "rm" ["-r"; q tgt] end else begin OASISMessage.error ~ctxt (f_ "Cannot remove directory '%s': not empty.") tgt end let glob ~ctxt fn = let basename = Filename.basename fn in if String.length basename >= 2 && basename.[0] = '*' && basename.[1] = '.' then begin let ext_len = (String.length basename) - 2 in let ext = String.sub basename 2 ext_len in let dirname = Filename.dirname fn in Array.fold_left (fun acc fn -> try let fn_ext = String.sub fn ((String.length fn) - ext_len) ext_len in if fn_ext = ext then (Filename.concat dirname fn) :: acc else acc with Invalid_argument _ -> acc) [] (Sys.readdir dirname) end else begin if file_exists_case fn then [fn] else [] end end # 2893 "setup.ml" module BaseEnvLight = struct (* # 22 "src/base/BaseEnvLight.ml" *) module MapString = Map.Make(String) type t = string MapString.t let default_filename = Filename.concat (Sys.getcwd ()) "setup.data" let load ?(allow_empty=false) ?(filename=default_filename) () = if Sys.file_exists filename then begin let chn = open_in_bin filename in let st = Stream.of_channel chn in let line = ref 1 in let st_line = Stream.from (fun _ -> try match Stream.next st with | '\n' -> incr line; Some '\n' | c -> Some c with Stream.Failure -> None) in let lexer = Genlex.make_lexer ["="] st_line in let rec read_file mp = match Stream.npeek 3 lexer with | [Genlex.Ident nm; Genlex.Kwd "="; Genlex.String value] -> Stream.junk lexer; Stream.junk lexer; Stream.junk lexer; read_file (MapString.add nm value mp) | [] -> mp | _ -> failwith (Printf.sprintf "Malformed data file '%s' line %d" filename !line) in let mp = read_file MapString.empty in close_in chn; mp end else if allow_empty then begin MapString.empty end else begin failwith (Printf.sprintf "Unable to load environment, the file '%s' doesn't exist." filename) end let rec var_expand str env = let buff = Buffer.create ((String.length str) * 2) in Buffer.add_substitute buff (fun var -> try var_expand (MapString.find var env) env with Not_found -> failwith (Printf.sprintf "No variable %s defined when trying to expand %S." var str)) str; Buffer.contents buff let var_get name env = var_expand (MapString.find name env) env let var_choose lst env = OASISExpr.choose (fun nm -> var_get nm env) lst end # 2998 "setup.ml" module BaseContext = struct (* # 22 "src/base/BaseContext.ml" *) (* TODO: get rid of this module. *) open OASISContext let args () = fst (fspecs ()) let default = default end module BaseMessage = struct (* # 22 "src/base/BaseMessage.ml" *) (** Message to user, overrid for Base @author Sylvain Le Gall *) open OASISMessage open BaseContext let debug fmt = debug ~ctxt:!default fmt let info fmt = info ~ctxt:!default fmt let warning fmt = warning ~ctxt:!default fmt let error fmt = error ~ctxt:!default fmt end module BaseEnv = struct (* # 22 "src/base/BaseEnv.ml" *) open OASISGettext open OASISUtils open PropList module MapString = BaseEnvLight.MapString type origin_t = | ODefault | OGetEnv | OFileLoad | OCommandLine type cli_handle_t = | CLINone | CLIAuto | CLIWith | CLIEnable | CLIUser of (Arg.key * Arg.spec * Arg.doc) list type definition_t = { hide: bool; dump: bool; cli: cli_handle_t; arg_help: string option; group: string option; } let schema = Schema.create "environment" (* Environment data *) let env = Data.create () (* Environment data from file *) let env_from_file = ref MapString.empty (* Lexer for var *) let var_lxr = Genlex.make_lexer [] let rec var_expand str = let buff = Buffer.create ((String.length str) * 2) in Buffer.add_substitute buff (fun var -> try (* TODO: this is a quick hack to allow calling Test.Command * without defining executable name really. I.e. if there is * an exec Executable toto, then $(toto) should be replace * by its real name. It is however useful to have this function * for other variable that depend on the host and should be * written better than that. *) let st = var_lxr (Stream.of_string var) in match Stream.npeek 3 st with | [Genlex.Ident "utoh"; Genlex.Ident nm] -> OASISHostPath.of_unix (var_get nm) | [Genlex.Ident "utoh"; Genlex.String s] -> OASISHostPath.of_unix s | [Genlex.Ident "ocaml_escaped"; Genlex.Ident nm] -> String.escaped (var_get nm) | [Genlex.Ident "ocaml_escaped"; Genlex.String s] -> String.escaped s | [Genlex.Ident nm] -> var_get nm | _ -> failwithf (f_ "Unknown expression '%s' in variable expansion of %s.") var str with | Unknown_field (_, _) -> failwithf (f_ "No variable %s defined when trying to expand %S.") var str | Stream.Error e -> failwithf (f_ "Syntax error when parsing '%s' when trying to \ expand %S: %s") var str e) str; Buffer.contents buff and var_get name = let vl = try Schema.get schema env name with Unknown_field _ as e -> begin try MapString.find name !env_from_file with Not_found -> raise e end in var_expand vl let var_choose ?printer ?name lst = OASISExpr.choose ?printer ?name var_get lst let var_protect vl = let buff = Buffer.create (String.length vl) in String.iter (function | '$' -> Buffer.add_string buff "\\$" | c -> Buffer.add_char buff c) vl; Buffer.contents buff let var_define ?(hide=false) ?(dump=true) ?short_desc ?(cli=CLINone) ?arg_help ?group name (* TODO: type constraint on the fact that name must be a valid OCaml id *) dflt = let default = [ OFileLoad, (fun () -> MapString.find name !env_from_file); ODefault, dflt; OGetEnv, (fun () -> Sys.getenv name); ] in let extra = { hide = hide; dump = dump; cli = cli; arg_help = arg_help; group = group; } in (* Try to find a value that can be defined *) let var_get_low lst = let errors, res = List.fold_left (fun (errors, res) (o, v) -> if res = None then begin try errors, Some (v ()) with | Not_found -> errors, res | Failure rsn -> (rsn :: errors), res | e -> (Printexc.to_string e) :: errors, res end else errors, res) ([], None) (List.sort (fun (o1, _) (o2, _) -> Pervasives.compare o2 o1) lst) in match res, errors with | Some v, _ -> v | None, [] -> raise (Not_set (name, None)) | None, lst -> raise (Not_set (name, Some (String.concat (s_ ", ") lst))) in let help = match short_desc with | Some fs -> Some fs | None -> None in let var_get_lst = FieldRO.create ~schema ~name ~parse:(fun ?(context=ODefault) s -> [context, fun () -> s]) ~print:var_get_low ~default ~update:(fun ?context x old_x -> x @ old_x) ?help extra in fun () -> var_expand (var_get_low (var_get_lst env)) let var_redefine ?hide ?dump ?short_desc ?cli ?arg_help ?group name dflt = if Schema.mem schema name then begin (* TODO: look suspsicious, we want to memorize dflt not dflt () *) Schema.set schema env ~context:ODefault name (dflt ()); fun () -> var_get name end else begin var_define ?hide ?dump ?short_desc ?cli ?arg_help ?group name dflt end let var_ignore (e: unit -> string) = () let print_hidden = var_define ~hide:true ~dump:false ~cli:CLIAuto ~arg_help:"Print even non-printable variable. (debug)" "print_hidden" (fun () -> "false") let var_all () = List.rev (Schema.fold (fun acc nm def _ -> if not def.hide || bool_of_string (print_hidden ()) then nm :: acc else acc) [] schema) let default_filename = BaseEnvLight.default_filename let load ?allow_empty ?filename () = env_from_file := BaseEnvLight.load ?allow_empty ?filename () let unload () = env_from_file := MapString.empty; Data.clear env let dump ?(filename=default_filename) () = let chn = open_out_bin filename in let output nm value = Printf.fprintf chn "%s=%S\n" nm value in let mp_todo = (* Dump data from schema *) Schema.fold (fun mp_todo nm def _ -> if def.dump then begin try let value = Schema.get schema env nm in output nm value with Not_set _ -> () end; MapString.remove nm mp_todo) !env_from_file schema in (* Dump data defined outside of schema *) MapString.iter output mp_todo; (* End of the dump *) close_out chn let print () = let printable_vars = Schema.fold (fun acc nm def short_descr_opt -> if not def.hide || bool_of_string (print_hidden ()) then begin try let value = Schema.get schema env nm in let txt = match short_descr_opt with | Some s -> s () | None -> nm in (txt, value) :: acc with Not_set _ -> acc end else acc) [] schema in let max_length = List.fold_left max 0 (List.rev_map String.length (List.rev_map fst printable_vars)) in let dot_pad str = String.make ((max_length - (String.length str)) + 3) '.' in Printf.printf "\nConfiguration: \n"; List.iter (fun (name, value) -> Printf.printf "%s: %s %s\n" name (dot_pad name) value) (List.rev printable_vars); Printf.printf "\n%!" let args () = let arg_concat = OASISUtils.varname_concat ~hyphen:'-' in [ "--override", Arg.Tuple ( let rvr = ref "" in let rvl = ref "" in [ Arg.Set_string rvr; Arg.Set_string rvl; Arg.Unit (fun () -> Schema.set schema env ~context:OCommandLine !rvr !rvl) ] ), "var+val Override any configuration variable."; ] @ List.flatten (Schema.fold (fun acc name def short_descr_opt -> let var_set s = Schema.set schema env ~context:OCommandLine name s in let arg_name = OASISUtils.varname_of_string ~hyphen:'-' name in let hlp = match short_descr_opt with | Some txt -> txt () | None -> "" in let arg_hlp = match def.arg_help with | Some s -> s | None -> "str" in let default_value = try Printf.sprintf (f_ " [%s]") (Schema.get schema env name) with Not_set _ -> "" in let args = match def.cli with | CLINone -> [] | CLIAuto -> [ arg_concat "--" arg_name, Arg.String var_set, Printf.sprintf (f_ "%s %s%s") arg_hlp hlp default_value ] | CLIWith -> [ arg_concat "--with-" arg_name, Arg.String var_set, Printf.sprintf (f_ "%s %s%s") arg_hlp hlp default_value ] | CLIEnable -> let dflt = if default_value = " [true]" then s_ " [default: enabled]" else s_ " [default: disabled]" in [ arg_concat "--enable-" arg_name, Arg.Unit (fun () -> var_set "true"), Printf.sprintf (f_ " %s%s") hlp dflt; arg_concat "--disable-" arg_name, Arg.Unit (fun () -> var_set "false"), Printf.sprintf (f_ " %s%s") hlp dflt ] | CLIUser lst -> lst in args :: acc) [] schema) end module BaseArgExt = struct (* # 22 "src/base/BaseArgExt.ml" *) open OASISUtils open OASISGettext let parse argv args = (* Simulate command line for Arg *) let current = ref 0 in try Arg.parse_argv ~current:current (Array.concat [[|"none"|]; argv]) (Arg.align args) (failwithf (f_ "Don't know what to do with arguments: '%s'")) (s_ "configure options:") with | Arg.Help txt -> print_endline txt; exit 0 | Arg.Bad txt -> prerr_endline txt; exit 1 end module BaseCheck = struct (* # 22 "src/base/BaseCheck.ml" *) open BaseEnv open BaseMessage open OASISUtils open OASISGettext let prog_best prg prg_lst = var_redefine prg (fun () -> let alternate = List.fold_left (fun res e -> match res with | Some _ -> res | None -> try Some (OASISFileUtil.which ~ctxt:!BaseContext.default e) with Not_found -> None) None prg_lst in match alternate with | Some prg -> prg | None -> raise Not_found) let prog prg = prog_best prg [prg] let prog_opt prg = prog_best prg [prg^".opt"; prg] let ocamlfind = prog "ocamlfind" let version var_prefix cmp fversion () = (* Really compare version provided *) let var = var_prefix^"_version_"^(OASISVersion.varname_of_comparator cmp) in var_redefine ~hide:true var (fun () -> let version_str = match fversion () with | "[Distributed with OCaml]" -> begin try (var_get "ocaml_version") with Not_found -> warning (f_ "Variable ocaml_version not defined, fallback \ to default"); Sys.ocaml_version end | res -> res in let version = OASISVersion.version_of_string version_str in if OASISVersion.comparator_apply version cmp then version_str else failwithf (f_ "Cannot satisfy version constraint on %s: %s (version: %s)") var_prefix (OASISVersion.string_of_comparator cmp) version_str) () let package_version pkg = OASISExec.run_read_one_line ~ctxt:!BaseContext.default (ocamlfind ()) ["query"; "-format"; "%v"; pkg] let package ?version_comparator pkg () = let var = OASISUtils.varname_concat "pkg_" (OASISUtils.varname_of_string pkg) in let findlib_dir pkg = let dir = OASISExec.run_read_one_line ~ctxt:!BaseContext.default (ocamlfind ()) ["query"; "-format"; "%d"; pkg] in if Sys.file_exists dir && Sys.is_directory dir then dir else failwithf (f_ "When looking for findlib package %s, \ directory %s return doesn't exist") pkg dir in let vl = var_redefine var (fun () -> findlib_dir pkg) () in ( match version_comparator with | Some ver_cmp -> ignore (version var ver_cmp (fun _ -> package_version pkg) ()) | None -> () ); vl end module BaseOCamlcConfig = struct (* # 22 "src/base/BaseOCamlcConfig.ml" *) open BaseEnv open OASISUtils open OASISGettext module SMap = Map.Make(String) let ocamlc = BaseCheck.prog_opt "ocamlc" let ocamlc_config_map = (* Map name to value for ocamlc -config output (name ^": "^value) *) let rec split_field mp lst = match lst with | line :: tl -> let mp = try let pos_semicolon = String.index line ':' in if pos_semicolon > 1 then ( let name = String.sub line 0 pos_semicolon in let linelen = String.length line in let value = if linelen > pos_semicolon + 2 then String.sub line (pos_semicolon + 2) (linelen - pos_semicolon - 2) else "" in SMap.add name value mp ) else ( mp ) with Not_found -> ( mp ) in split_field mp tl | [] -> mp in let cache = lazy (var_protect (Marshal.to_string (split_field SMap.empty (OASISExec.run_read_output ~ctxt:!BaseContext.default (ocamlc ()) ["-config"])) [])) in var_redefine "ocamlc_config_map" ~hide:true ~dump:false (fun () -> (* TODO: update if ocamlc change !!! *) Lazy.force cache) let var_define nm = (* Extract data from ocamlc -config *) let avlbl_config_get () = Marshal.from_string (ocamlc_config_map ()) 0 in let chop_version_suffix s = try String.sub s 0 (String.index s '+') with _ -> s in let nm_config, value_config = match nm with | "ocaml_version" -> "version", chop_version_suffix | _ -> nm, (fun x -> x) in var_redefine nm (fun () -> try let map = avlbl_config_get () in let value = SMap.find nm_config map in value_config value with Not_found -> failwithf (f_ "Cannot find field '%s' in '%s -config' output") nm (ocamlc ())) end module BaseStandardVar = struct (* # 22 "src/base/BaseStandardVar.ml" *) open OASISGettext open OASISTypes open OASISExpr open BaseCheck open BaseEnv let ocamlfind = BaseCheck.ocamlfind let ocamlc = BaseOCamlcConfig.ocamlc let ocamlopt = prog_opt "ocamlopt" let ocamlbuild = prog "ocamlbuild" (**/**) let rpkg = ref None let pkg_get () = match !rpkg with | Some pkg -> pkg | None -> failwith (s_ "OASIS Package is not set") let var_cond = ref [] let var_define_cond ~since_version f dflt = let holder = ref (fun () -> dflt) in let since_version = OASISVersion.VGreaterEqual (OASISVersion.version_of_string since_version) in var_cond := (fun ver -> if OASISVersion.comparator_apply ver since_version then holder := f ()) :: !var_cond; fun () -> !holder () (**/**) let pkg_name = var_define ~short_desc:(fun () -> s_ "Package name") "pkg_name" (fun () -> (pkg_get ()).name) let pkg_version = var_define ~short_desc:(fun () -> s_ "Package version") "pkg_version" (fun () -> (OASISVersion.string_of_version (pkg_get ()).version)) let c = BaseOCamlcConfig.var_define let os_type = c "os_type" let system = c "system" let architecture = c "architecture" let ccomp_type = c "ccomp_type" let ocaml_version = c "ocaml_version" (* TODO: Check standard variable presence at runtime *) let standard_library_default = c "standard_library_default" let standard_library = c "standard_library" let standard_runtime = c "standard_runtime" let bytecomp_c_compiler = c "bytecomp_c_compiler" let native_c_compiler = c "native_c_compiler" let model = c "model" let ext_obj = c "ext_obj" let ext_asm = c "ext_asm" let ext_lib = c "ext_lib" let ext_dll = c "ext_dll" let default_executable_name = c "default_executable_name" let systhread_supported = c "systhread_supported" let flexlink = BaseCheck.prog "flexlink" let flexdll_version = var_define ~short_desc:(fun () -> "FlexDLL version (Win32)") "flexdll_version" (fun () -> let lst = OASISExec.run_read_output ~ctxt:!BaseContext.default (flexlink ()) ["-help"] in match lst with | line :: _ -> Scanf.sscanf line "FlexDLL version %s" (fun ver -> ver) | [] -> raise Not_found) (**/**) let p name hlp dflt = var_define ~short_desc:hlp ~cli:CLIAuto ~arg_help:"dir" name dflt let (/) a b = if os_type () = Sys.os_type then Filename.concat a b else if os_type () = "Unix" then OASISUnixPath.concat a b else OASISUtils.failwithf (f_ "Cannot handle os_type %s filename concat") (os_type ()) (**/**) let prefix = p "prefix" (fun () -> s_ "Install architecture-independent files dir") (fun () -> match os_type () with | "Win32" -> let program_files = Sys.getenv "PROGRAMFILES" in program_files/(pkg_name ()) | _ -> "/usr/local") let exec_prefix = p "exec_prefix" (fun () -> s_ "Install architecture-dependent files in dir") (fun () -> "$prefix") let bindir = p "bindir" (fun () -> s_ "User executables") (fun () -> "$exec_prefix"/"bin") let sbindir = p "sbindir" (fun () -> s_ "System admin executables") (fun () -> "$exec_prefix"/"sbin") let libexecdir = p "libexecdir" (fun () -> s_ "Program executables") (fun () -> "$exec_prefix"/"libexec") let sysconfdir = p "sysconfdir" (fun () -> s_ "Read-only single-machine data") (fun () -> "$prefix"/"etc") let sharedstatedir = p "sharedstatedir" (fun () -> s_ "Modifiable architecture-independent data") (fun () -> "$prefix"/"com") let localstatedir = p "localstatedir" (fun () -> s_ "Modifiable single-machine data") (fun () -> "$prefix"/"var") let libdir = p "libdir" (fun () -> s_ "Object code libraries") (fun () -> "$exec_prefix"/"lib") let datarootdir = p "datarootdir" (fun () -> s_ "Read-only arch-independent data root") (fun () -> "$prefix"/"share") let datadir = p "datadir" (fun () -> s_ "Read-only architecture-independent data") (fun () -> "$datarootdir") let infodir = p "infodir" (fun () -> s_ "Info documentation") (fun () -> "$datarootdir"/"info") let localedir = p "localedir" (fun () -> s_ "Locale-dependent data") (fun () -> "$datarootdir"/"locale") let mandir = p "mandir" (fun () -> s_ "Man documentation") (fun () -> "$datarootdir"/"man") let docdir = p "docdir" (fun () -> s_ "Documentation root") (fun () -> "$datarootdir"/"doc"/"$pkg_name") let htmldir = p "htmldir" (fun () -> s_ "HTML documentation") (fun () -> "$docdir") let dvidir = p "dvidir" (fun () -> s_ "DVI documentation") (fun () -> "$docdir") let pdfdir = p "pdfdir" (fun () -> s_ "PDF documentation") (fun () -> "$docdir") let psdir = p "psdir" (fun () -> s_ "PS documentation") (fun () -> "$docdir") let destdir = p "destdir" (fun () -> s_ "Prepend a path when installing package") (fun () -> raise (PropList.Not_set ("destdir", Some (s_ "undefined by construct")))) let findlib_version = var_define "findlib_version" (fun () -> BaseCheck.package_version "findlib") let is_native = var_define "is_native" (fun () -> try let _s: string = ocamlopt () in "true" with PropList.Not_set _ -> let _s: string = ocamlc () in "false") let ext_program = var_define "suffix_program" (fun () -> match os_type () with | "Win32" | "Cygwin" -> ".exe" | _ -> "") let rm = var_define ~short_desc:(fun () -> s_ "Remove a file.") "rm" (fun () -> match os_type () with | "Win32" -> "del" | _ -> "rm -f") let rmdir = var_define ~short_desc:(fun () -> s_ "Remove a directory.") "rmdir" (fun () -> match os_type () with | "Win32" -> "rd" | _ -> "rm -rf") let debug = var_define ~short_desc:(fun () -> s_ "Turn ocaml debug flag on") ~cli:CLIEnable "debug" (fun () -> "true") let profile = var_define ~short_desc:(fun () -> s_ "Turn ocaml profile flag on") ~cli:CLIEnable "profile" (fun () -> "false") let tests = var_define_cond ~since_version:"0.3" (fun () -> var_define ~short_desc:(fun () -> s_ "Compile tests executable and library and run them") ~cli:CLIEnable "tests" (fun () -> "false")) "true" let docs = var_define_cond ~since_version:"0.3" (fun () -> var_define ~short_desc:(fun () -> s_ "Create documentations") ~cli:CLIEnable "docs" (fun () -> "true")) "true" let native_dynlink = var_define ~short_desc:(fun () -> s_ "Compiler support generation of .cmxs.") ~cli:CLINone "native_dynlink" (fun () -> let res = let ocaml_lt_312 () = OASISVersion.comparator_apply (OASISVersion.version_of_string (ocaml_version ())) (OASISVersion.VLesser (OASISVersion.version_of_string "3.12.0")) in let flexdll_lt_030 () = OASISVersion.comparator_apply (OASISVersion.version_of_string (flexdll_version ())) (OASISVersion.VLesser (OASISVersion.version_of_string "0.30")) in let has_native_dynlink = let ocamlfind = ocamlfind () in try let fn = OASISExec.run_read_one_line ~ctxt:!BaseContext.default ocamlfind ["query"; "-predicates"; "native"; "dynlink"; "-format"; "%d/%a"] in Sys.file_exists fn with _ -> false in if not has_native_dynlink then false else if ocaml_lt_312 () then false else if (os_type () = "Win32" || os_type () = "Cygwin") && flexdll_lt_030 () then begin BaseMessage.warning (f_ ".cmxs generation disabled because FlexDLL needs to be \ at least 0.30. Please upgrade FlexDLL from %s to 0.30.") (flexdll_version ()); false end else true in string_of_bool res) let init pkg = rpkg := Some pkg; List.iter (fun f -> f pkg.oasis_version) !var_cond end module BaseFileAB = struct (* # 22 "src/base/BaseFileAB.ml" *) open BaseEnv open OASISGettext open BaseMessage let to_filename fn = let fn = OASISHostPath.of_unix fn in if not (Filename.check_suffix fn ".ab") then warning (f_ "File '%s' doesn't have '.ab' extension") fn; Filename.chop_extension fn let replace fn_lst = let buff = Buffer.create 13 in List.iter (fun fn -> let fn = OASISHostPath.of_unix fn in let chn_in = open_in fn in let chn_out = open_out (to_filename fn) in ( try while true do Buffer.add_string buff (var_expand (input_line chn_in)); Buffer.add_char buff '\n' done with End_of_file -> () ); Buffer.output_buffer chn_out buff; Buffer.clear buff; close_in chn_in; close_out chn_out) fn_lst end module BaseLog = struct (* # 22 "src/base/BaseLog.ml" *) open OASISUtils let default_filename = Filename.concat (Filename.dirname BaseEnv.default_filename) "setup.log" module SetTupleString = Set.Make (struct type t = string * string let compare (s11, s12) (s21, s22) = match String.compare s11 s21 with | 0 -> String.compare s12 s22 | n -> n end) let load () = if Sys.file_exists default_filename then begin let chn = open_in default_filename in let scbuf = Scanf.Scanning.from_file default_filename in let rec read_aux (st, lst) = if not (Scanf.Scanning.end_of_input scbuf) then begin let acc = try Scanf.bscanf scbuf "%S %S\n" (fun e d -> let t = e, d in if SetTupleString.mem t st then st, lst else SetTupleString.add t st, t :: lst) with Scanf.Scan_failure _ -> failwith (Scanf.bscanf scbuf "%l" (fun line -> Printf.sprintf "Malformed log file '%s' at line %d" default_filename line)) in read_aux acc end else begin close_in chn; List.rev lst end in read_aux (SetTupleString.empty, []) end else begin [] end let register event data = let chn_out = open_out_gen [Open_append; Open_creat; Open_text] 0o644 default_filename in Printf.fprintf chn_out "%S %S\n" event data; close_out chn_out let unregister event data = if Sys.file_exists default_filename then begin let lst = load () in let chn_out = open_out default_filename in let write_something = ref false in List.iter (fun (e, d) -> if e <> event || d <> data then begin write_something := true; Printf.fprintf chn_out "%S %S\n" e d end) lst; close_out chn_out; if not !write_something then Sys.remove default_filename end let filter events = let st_events = List.fold_left (fun st e -> SetString.add e st) SetString.empty events in List.filter (fun (e, _) -> SetString.mem e st_events) (load ()) let exists event data = List.exists (fun v -> (event, data) = v) (load ()) end module BaseBuilt = struct (* # 22 "src/base/BaseBuilt.ml" *) open OASISTypes open OASISGettext open BaseStandardVar open BaseMessage type t = | BExec (* Executable *) | BExecLib (* Library coming with executable *) | BLib (* Library *) | BObj (* Library *) | BDoc (* Document *) let to_log_event_file t nm = "built_"^ (match t with | BExec -> "exec" | BExecLib -> "exec_lib" | BLib -> "lib" | BObj -> "obj" | BDoc -> "doc")^ "_"^nm let to_log_event_done t nm = "is_"^(to_log_event_file t nm) let register t nm lst = BaseLog.register (to_log_event_done t nm) "true"; List.iter (fun alt -> let registered = List.fold_left (fun registered fn -> if OASISFileUtil.file_exists_case fn then begin BaseLog.register (to_log_event_file t nm) (if Filename.is_relative fn then Filename.concat (Sys.getcwd ()) fn else fn); true end else registered) false alt in if not registered then warning (f_ "Cannot find an existing alternative files among: %s") (String.concat (s_ ", ") alt)) lst let unregister t nm = List.iter (fun (e, d) -> BaseLog.unregister e d) (BaseLog.filter [to_log_event_file t nm; to_log_event_done t nm]) let fold t nm f acc = List.fold_left (fun acc (_, fn) -> if OASISFileUtil.file_exists_case fn then begin f acc fn end else begin warning (f_ "File '%s' has been marked as built \ for %s but doesn't exist") fn (Printf.sprintf (match t with | BExec | BExecLib -> (f_ "executable %s") | BLib -> (f_ "library %s") | BObj -> (f_ "object %s") | BDoc -> (f_ "documentation %s")) nm); acc end) acc (BaseLog.filter [to_log_event_file t nm]) let is_built t nm = List.fold_left (fun is_built (_, d) -> (try bool_of_string d with _ -> false)) false (BaseLog.filter [to_log_event_done t nm]) let of_executable ffn (cs, bs, exec) = let unix_exec_is, unix_dll_opt = OASISExecutable.unix_exec_is (cs, bs, exec) (fun () -> bool_of_string (is_native ())) ext_dll ext_program in let evs = (BExec, cs.cs_name, [[ffn unix_exec_is]]) :: (match unix_dll_opt with | Some fn -> [BExecLib, cs.cs_name, [[ffn fn]]] | None -> []) in evs, unix_exec_is, unix_dll_opt let of_library ffn (cs, bs, lib) = let unix_lst = OASISLibrary.generated_unix_files ~ctxt:!BaseContext.default ~source_file_exists:(fun fn -> OASISFileUtil.file_exists_case (OASISHostPath.of_unix fn)) ~is_native:(bool_of_string (is_native ())) ~has_native_dynlink:(bool_of_string (native_dynlink ())) ~ext_lib:(ext_lib ()) ~ext_dll:(ext_dll ()) (cs, bs, lib) in let evs = [BLib, cs.cs_name, List.map (List.map ffn) unix_lst] in evs, unix_lst let of_object ffn (cs, bs, obj) = let unix_lst = OASISObject.generated_unix_files ~ctxt:!BaseContext.default ~source_file_exists:(fun fn -> OASISFileUtil.file_exists_case (OASISHostPath.of_unix fn)) ~is_native:(bool_of_string (is_native ())) (cs, bs, obj) in let evs = [BObj, cs.cs_name, List.map (List.map ffn) unix_lst] in evs, unix_lst end module BaseCustom = struct (* # 22 "src/base/BaseCustom.ml" *) open BaseEnv open BaseMessage open OASISTypes open OASISGettext let run cmd args extra_args = OASISExec.run ~ctxt:!BaseContext.default ~quote:false (var_expand cmd) (List.map var_expand (args @ (Array.to_list extra_args))) let hook ?(failsafe=false) cstm f e = let optional_command lst = let printer = function | Some (cmd, args) -> String.concat " " (cmd :: args) | None -> s_ "No command" in match var_choose ~name:(s_ "Pre/Post Command") ~printer lst with | Some (cmd, args) -> begin try run cmd args [||] with e when failsafe -> warning (f_ "Command '%s' fail with error: %s") (String.concat " " (cmd :: args)) (match e with | Failure msg -> msg | e -> Printexc.to_string e) end | None -> () in let res = optional_command cstm.pre_command; f e in optional_command cstm.post_command; res end module BaseDynVar = struct (* # 22 "src/base/BaseDynVar.ml" *) open OASISTypes open OASISGettext open BaseEnv open BaseBuilt let init pkg = (* TODO: disambiguate exec vs other variable by adding exec_VARNAME. *) (* TODO: provide compile option for library libary_byte_args_VARNAME... *) List.iter (function | Executable (cs, bs, exec) -> if var_choose bs.bs_build then var_ignore (var_redefine (* We don't save this variable *) ~dump:false ~short_desc:(fun () -> Printf.sprintf (f_ "Filename of executable '%s'") cs.cs_name) (OASISUtils.varname_of_string cs.cs_name) (fun () -> let fn_opt = fold BExec cs.cs_name (fun _ fn -> Some fn) None in match fn_opt with | Some fn -> fn | None -> raise (PropList.Not_set (cs.cs_name, Some (Printf.sprintf (f_ "Executable '%s' not yet built.") cs.cs_name))))) | Library _ | Object _ | Flag _ | Test _ | SrcRepo _ | Doc _ -> ()) pkg.sections end module BaseTest = struct (* # 22 "src/base/BaseTest.ml" *) open BaseEnv open BaseMessage open OASISTypes open OASISExpr open OASISGettext let test lst pkg extra_args = let one_test (failure, n) (test_plugin, cs, test) = if var_choose ~name:(Printf.sprintf (f_ "test %s run") cs.cs_name) ~printer:string_of_bool test.test_run then begin let () = info (f_ "Running test '%s'") cs.cs_name in let back_cwd = match test.test_working_directory with | Some dir -> let cwd = Sys.getcwd () in let chdir d = info (f_ "Changing directory to '%s'") d; Sys.chdir d in chdir dir; fun () -> chdir cwd | None -> fun () -> () in try let failure_percent = BaseCustom.hook test.test_custom (test_plugin pkg (cs, test)) extra_args in back_cwd (); (failure_percent +. failure, n + 1) with e -> begin back_cwd (); raise e end end else begin info (f_ "Skipping test '%s'") cs.cs_name; (failure, n) end in let failed, n = List.fold_left one_test (0.0, 0) lst in let failure_percent = if n = 0 then 0.0 else failed /. (float_of_int n) in let msg = Printf.sprintf (f_ "Tests had a %.2f%% failure rate") (100. *. failure_percent) in if failure_percent > 0.0 then failwith msg else info "%s" msg; (* Possible explanation why the tests where not run. *) if OASISFeatures.package_test OASISFeatures.flag_tests pkg && not (bool_of_string (BaseStandardVar.tests ())) && lst <> [] then BaseMessage.warning "Tests are turned off, consider enabling with \ 'ocaml setup.ml -configure --enable-tests'" end module BaseDoc = struct (* # 22 "src/base/BaseDoc.ml" *) open BaseEnv open BaseMessage open OASISTypes open OASISGettext let doc lst pkg extra_args = let one_doc (doc_plugin, cs, doc) = if var_choose ~name:(Printf.sprintf (f_ "documentation %s build") cs.cs_name) ~printer:string_of_bool doc.doc_build then begin info (f_ "Building documentation '%s'") cs.cs_name; BaseCustom.hook doc.doc_custom (doc_plugin pkg (cs, doc)) extra_args end in List.iter one_doc lst; if OASISFeatures.package_test OASISFeatures.flag_docs pkg && not (bool_of_string (BaseStandardVar.docs ())) && lst <> [] then BaseMessage.warning "Docs are turned off, consider enabling with \ 'ocaml setup.ml -configure --enable-docs'" end module BaseSetup = struct (* # 22 "src/base/BaseSetup.ml" *) open BaseEnv open BaseMessage open OASISTypes open OASISSection open OASISGettext open OASISUtils type std_args_fun = package -> string array -> unit type ('a, 'b) section_args_fun = name * (package -> (common_section * 'a) -> string array -> 'b) type t = { configure: std_args_fun; build: std_args_fun; doc: ((doc, unit) section_args_fun) list; test: ((test, float) section_args_fun) list; install: std_args_fun; uninstall: std_args_fun; clean: std_args_fun list; clean_doc: (doc, unit) section_args_fun list; clean_test: (test, unit) section_args_fun list; distclean: std_args_fun list; distclean_doc: (doc, unit) section_args_fun list; distclean_test: (test, unit) section_args_fun list; package: package; oasis_fn: string option; oasis_version: string; oasis_digest: Digest.t option; oasis_exec: string option; oasis_setup_args: string list; setup_update: bool; } (* Associate a plugin function with data from package *) let join_plugin_sections filter_map lst = List.rev (List.fold_left (fun acc sct -> match filter_map sct with | Some e -> e :: acc | None -> acc) [] lst) (* Search for plugin data associated with a section name *) let lookup_plugin_section plugin action nm lst = try List.assoc nm lst with Not_found -> failwithf (f_ "Cannot find plugin %s matching section %s for %s action") plugin nm action let configure t args = (* Run configure *) BaseCustom.hook t.package.conf_custom (fun () -> (* Reload if preconf has changed it *) begin try unload (); load (); with _ -> () end; (* Run plugin's configure *) t.configure t.package args; (* Dump to allow postconf to change it *) dump ()) (); (* Reload environment *) unload (); load (); (* Save environment *) print (); (* Replace data in file *) BaseFileAB.replace t.package.files_ab let build t args = BaseCustom.hook t.package.build_custom (t.build t.package) args let doc t args = BaseDoc.doc (join_plugin_sections (function | Doc (cs, e) -> Some (lookup_plugin_section "documentation" (s_ "build") cs.cs_name t.doc, cs, e) | _ -> None) t.package.sections) t.package args let test t args = BaseTest.test (join_plugin_sections (function | Test (cs, e) -> Some (lookup_plugin_section "test" (s_ "run") cs.cs_name t.test, cs, e) | _ -> None) t.package.sections) t.package args let all t args = let rno_doc = ref false in let rno_test = ref false in let arg_rest = ref [] in Arg.parse_argv ~current:(ref 0) (Array.of_list ((Sys.executable_name^" all") :: (Array.to_list args))) [ "-no-doc", Arg.Set rno_doc, s_ "Don't run doc target"; "-no-test", Arg.Set rno_test, s_ "Don't run test target"; "--", Arg.Rest (fun arg -> arg_rest := arg :: !arg_rest), s_ "All arguments for configure."; ] (failwithf (f_ "Don't know what to do with '%s'")) ""; info "Running configure step"; configure t (Array.of_list (List.rev !arg_rest)); info "Running build step"; build t [||]; (* Load setup.log dynamic variables *) BaseDynVar.init t.package; if not !rno_doc then begin info "Running doc step"; doc t [||]; end else begin info "Skipping doc step" end; if not !rno_test then begin info "Running test step"; test t [||] end else begin info "Skipping test step" end let install t args = BaseCustom.hook t.package.install_custom (t.install t.package) args let uninstall t args = BaseCustom.hook t.package.uninstall_custom (t.uninstall t.package) args let reinstall t args = uninstall t args; install t args let clean, distclean = let failsafe f a = try f a with e -> warning (f_ "Action fail with error: %s") (match e with | Failure msg -> msg | e -> Printexc.to_string e) in let generic_clean t cstm mains docs tests args = BaseCustom.hook ~failsafe:true cstm (fun () -> (* Clean section *) List.iter (function | Test (cs, test) -> let f = try List.assoc cs.cs_name tests with Not_found -> fun _ _ _ -> () in failsafe (f t.package (cs, test)) args | Doc (cs, doc) -> let f = try List.assoc cs.cs_name docs with Not_found -> fun _ _ _ -> () in failsafe (f t.package (cs, doc)) args | Library _ | Object _ | Executable _ | Flag _ | SrcRepo _ -> ()) t.package.sections; (* Clean whole package *) List.iter (fun f -> failsafe (f t.package) args) mains) () in let clean t args = generic_clean t t.package.clean_custom t.clean t.clean_doc t.clean_test args in let distclean t args = (* Call clean *) clean t args; (* Call distclean code *) generic_clean t t.package.distclean_custom t.distclean t.distclean_doc t.distclean_test args; (* Remove generated file *) List.iter (fun fn -> if Sys.file_exists fn then begin info (f_ "Remove '%s'") fn; Sys.remove fn end) (BaseEnv.default_filename :: BaseLog.default_filename :: (List.rev_map BaseFileAB.to_filename t.package.files_ab)) in clean, distclean let version t _ = print_endline t.oasis_version let update_setup_ml, no_update_setup_ml_cli = let b = ref true in b, ("-no-update-setup-ml", Arg.Clear b, s_ " Don't try to update setup.ml, even if _oasis has changed.") let default_oasis_fn = "_oasis" let update_setup_ml t = let oasis_fn = match t.oasis_fn with | Some fn -> fn | None -> default_oasis_fn in let oasis_exec = match t.oasis_exec with | Some fn -> fn | None -> "oasis" in let ocaml = Sys.executable_name in let setup_ml, args = match Array.to_list Sys.argv with | setup_ml :: args -> setup_ml, args | [] -> failwith (s_ "Expecting non-empty command line arguments.") in let ocaml, setup_ml = if Sys.executable_name = Sys.argv.(0) then (* We are not running in standard mode, probably the script * is precompiled. *) "ocaml", "setup.ml" else ocaml, setup_ml in let no_update_setup_ml_cli, _, _ = no_update_setup_ml_cli in let do_update () = let oasis_exec_version = OASISExec.run_read_one_line ~ctxt:!BaseContext.default ~f_exit_code: (function | 0 -> () | 1 -> failwithf (f_ "Executable '%s' is probably an old version \ of oasis (< 0.3.0), please update to version \ v%s.") oasis_exec t.oasis_version | 127 -> failwithf (f_ "Cannot find executable '%s', please install \ oasis v%s.") oasis_exec t.oasis_version | n -> failwithf (f_ "Command '%s version' exited with code %d.") oasis_exec n) oasis_exec ["version"] in if OASISVersion.comparator_apply (OASISVersion.version_of_string oasis_exec_version) (OASISVersion.VGreaterEqual (OASISVersion.version_of_string t.oasis_version)) then begin (* We have a version >= for the executable oasis, proceed with * update. *) (* TODO: delegate this check to 'oasis setup'. *) if Sys.os_type = "Win32" then failwithf (f_ "It is not possible to update the running script \ setup.ml on Windows. Please update setup.ml by \ running '%s'.") (String.concat " " (oasis_exec :: "setup" :: t.oasis_setup_args)) else begin OASISExec.run ~ctxt:!BaseContext.default ~f_exit_code: (function | 0 -> () | n -> failwithf (f_ "Unable to update setup.ml using '%s', \ please fix the problem and retry.") oasis_exec) oasis_exec ("setup" :: t.oasis_setup_args); OASISExec.run ~ctxt:!BaseContext.default ocaml (setup_ml :: args) end end else failwithf (f_ "The version of '%s' (v%s) doesn't match the version of \ oasis used to generate the %s file. Please install at \ least oasis v%s.") oasis_exec oasis_exec_version setup_ml t.oasis_version in if !update_setup_ml then begin try match t.oasis_digest with | Some dgst -> if Sys.file_exists oasis_fn && dgst <> Digest.file default_oasis_fn then begin do_update (); true end else false | None -> false with e -> error (f_ "Error when updating setup.ml. If you want to avoid this error, \ you can bypass the update of %s by running '%s %s %s %s'") setup_ml ocaml setup_ml no_update_setup_ml_cli (String.concat " " args); raise e end else false let setup t = let catch_exn = ref true in try let act_ref = ref (fun _ -> failwithf (f_ "No action defined, run '%s %s -help'") Sys.executable_name Sys.argv.(0)) in let extra_args_ref = ref [] in let allow_empty_env_ref = ref false in let arg_handle ?(allow_empty_env=false) act = Arg.Tuple [ Arg.Rest (fun str -> extra_args_ref := str :: !extra_args_ref); Arg.Unit (fun () -> allow_empty_env_ref := allow_empty_env; act_ref := act); ] in Arg.parse (Arg.align ([ "-configure", arg_handle ~allow_empty_env:true configure, s_ "[options*] Configure the whole build process."; "-build", arg_handle build, s_ "[options*] Build executables and libraries."; "-doc", arg_handle doc, s_ "[options*] Build documents."; "-test", arg_handle test, s_ "[options*] Run tests."; "-all", arg_handle ~allow_empty_env:true all, s_ "[options*] Run configure, build, doc and test targets."; "-install", arg_handle install, s_ "[options*] Install libraries, data, executables \ and documents."; "-uninstall", arg_handle uninstall, s_ "[options*] Uninstall libraries, data, executables \ and documents."; "-reinstall", arg_handle reinstall, s_ "[options*] Uninstall and install libraries, data, \ executables and documents."; "-clean", arg_handle ~allow_empty_env:true clean, s_ "[options*] Clean files generated by a build."; "-distclean", arg_handle ~allow_empty_env:true distclean, s_ "[options*] Clean files generated by a build and configure."; "-version", arg_handle ~allow_empty_env:true version, s_ " Display version of OASIS used to generate this setup.ml."; "-no-catch-exn", Arg.Clear catch_exn, s_ " Don't catch exception, useful for debugging."; ] @ (if t.setup_update then [no_update_setup_ml_cli] else []) @ (BaseContext.args ()))) (failwithf (f_ "Don't know what to do with '%s'")) (s_ "Setup and run build process current package\n"); (* Build initial environment *) load ~allow_empty:!allow_empty_env_ref (); (** Initialize flags *) List.iter (function | Flag (cs, {flag_description = hlp; flag_default = choices}) -> begin let apply ?short_desc () = var_ignore (var_define ~cli:CLIEnable ?short_desc (OASISUtils.varname_of_string cs.cs_name) (fun () -> string_of_bool (var_choose ~name:(Printf.sprintf (f_ "default value of flag %s") cs.cs_name) ~printer:string_of_bool choices))) in match hlp with | Some hlp -> apply ~short_desc:(fun () -> hlp) () | None -> apply () end | _ -> ()) t.package.sections; BaseStandardVar.init t.package; BaseDynVar.init t.package; if t.setup_update && update_setup_ml t then () else !act_ref t (Array.of_list (List.rev !extra_args_ref)) with e when !catch_exn -> error "%s" (Printexc.to_string e); exit 1 end # 5409 "setup.ml" module InternalConfigurePlugin = struct (* # 22 "src/plugins/internal/InternalConfigurePlugin.ml" *) (** Configure using internal scheme @author Sylvain Le Gall *) open BaseEnv open OASISTypes open OASISUtils open OASISGettext open BaseMessage (** Configure build using provided series of check to be done * and then output corresponding file. *) let configure pkg argv = let var_ignore_eval var = let _s: string = var () in () in let errors = ref SetString.empty in let buff = Buffer.create 13 in let add_errors fmt = Printf.kbprintf (fun b -> errors := SetString.add (Buffer.contents b) !errors; Buffer.clear b) buff fmt in let warn_exception e = warning "%s" (Printexc.to_string e) in (* Check tools *) let check_tools lst = List.iter (function | ExternalTool tool -> begin try var_ignore_eval (BaseCheck.prog tool) with e -> warn_exception e; add_errors (f_ "Cannot find external tool '%s'") tool end | InternalExecutable nm1 -> (* Check that matching tool is built *) List.iter (function | Executable ({cs_name = nm2}, {bs_build = build}, _) when nm1 = nm2 -> if not (var_choose build) then add_errors (f_ "Cannot find buildable internal executable \ '%s' when checking build depends") nm1 | _ -> ()) pkg.sections) lst in let build_checks sct bs = if var_choose bs.bs_build then begin if bs.bs_compiled_object = Native then begin try var_ignore_eval BaseStandardVar.ocamlopt with e -> warn_exception e; add_errors (f_ "Section %s requires native compilation") (OASISSection.string_of_section sct) end; (* Check tools *) check_tools bs.bs_build_tools; (* Check depends *) List.iter (function | FindlibPackage (findlib_pkg, version_comparator) -> begin try var_ignore_eval (BaseCheck.package ?version_comparator findlib_pkg) with e -> warn_exception e; match version_comparator with | None -> add_errors (f_ "Cannot find findlib package %s") findlib_pkg | Some ver_cmp -> add_errors (f_ "Cannot find findlib package %s (%s)") findlib_pkg (OASISVersion.string_of_comparator ver_cmp) end | InternalLibrary nm1 -> (* Check that matching library is built *) List.iter (function | Library ({cs_name = nm2}, {bs_build = build}, _) when nm1 = nm2 -> if not (var_choose build) then add_errors (f_ "Cannot find buildable internal library \ '%s' when checking build depends") nm1 | _ -> ()) pkg.sections) bs.bs_build_depends end in (* Parse command line *) BaseArgExt.parse argv (BaseEnv.args ()); (* OCaml version *) begin match pkg.ocaml_version with | Some ver_cmp -> begin try var_ignore_eval (BaseCheck.version "ocaml" ver_cmp BaseStandardVar.ocaml_version) with e -> warn_exception e; add_errors (f_ "OCaml version %s doesn't match version constraint %s") (BaseStandardVar.ocaml_version ()) (OASISVersion.string_of_comparator ver_cmp) end | None -> () end; (* Findlib version *) begin match pkg.findlib_version with | Some ver_cmp -> begin try var_ignore_eval (BaseCheck.version "findlib" ver_cmp BaseStandardVar.findlib_version) with e -> warn_exception e; add_errors (f_ "Findlib version %s doesn't match version constraint %s") (BaseStandardVar.findlib_version ()) (OASISVersion.string_of_comparator ver_cmp) end | None -> () end; (* Make sure the findlib version is fine for the OCaml compiler. *) begin let ocaml_ge4 = OASISVersion.version_compare (OASISVersion.version_of_string (BaseStandardVar.ocaml_version())) (OASISVersion.version_of_string "4.0.0") >= 0 in if ocaml_ge4 then let findlib_lt132 = OASISVersion.version_compare (OASISVersion.version_of_string (BaseStandardVar.findlib_version())) (OASISVersion.version_of_string "1.3.2") < 0 in if findlib_lt132 then add_errors "OCaml >= 4.0.0 requires Findlib version >= 1.3.2" end; (* FlexDLL *) if BaseStandardVar.os_type () = "Win32" || BaseStandardVar.os_type () = "Cygwin" then begin try var_ignore_eval BaseStandardVar.flexlink with e -> warn_exception e; add_errors (f_ "Cannot find 'flexlink'") end; (* Check build depends *) List.iter (function | Executable (_, bs, _) | Library (_, bs, _) as sct -> build_checks sct bs | Doc (_, doc) -> if var_choose doc.doc_build then check_tools doc.doc_build_tools | Test (_, test) -> if var_choose test.test_run then check_tools test.test_tools | _ -> ()) pkg.sections; (* Check if we need native dynlink (presence of libraries that compile to * native) *) begin let has_cmxa = List.exists (function | Library (_, bs, _) -> var_choose bs.bs_build && (bs.bs_compiled_object = Native || (bs.bs_compiled_object = Best && bool_of_string (BaseStandardVar.is_native ()))) | _ -> false) pkg.sections in if has_cmxa then var_ignore_eval BaseStandardVar.native_dynlink end; (* Check errors *) if SetString.empty != !errors then begin List.iter (fun e -> error "%s" e) (SetString.elements !errors); failwithf (fn_ "%d configuration error" "%d configuration errors" (SetString.cardinal !errors)) (SetString.cardinal !errors) end end module InternalInstallPlugin = struct (* # 22 "src/plugins/internal/InternalInstallPlugin.ml" *) (** Install using internal scheme @author Sylvain Le Gall *) open BaseEnv open BaseStandardVar open BaseMessage open OASISTypes open OASISFindlib open OASISGettext open OASISUtils let exec_hook = ref (fun (cs, bs, exec) -> cs, bs, exec) let lib_hook = ref (fun (cs, bs, lib) -> cs, bs, lib, []) let obj_hook = ref (fun (cs, bs, obj) -> cs, bs, obj, []) let doc_hook = ref (fun (cs, doc) -> cs, doc) let install_file_ev = "install-file" let install_dir_ev = "install-dir" let install_findlib_ev = "install-findlib" let win32_max_command_line_length = 8000 let split_install_command ocamlfind findlib_name meta files = if Sys.os_type = "Win32" then (* Arguments for the first command: *) let first_args = ["install"; findlib_name; meta] in (* Arguments for remaining commands: *) let other_args = ["install"; findlib_name; "-add"] in (* Extract as much files as possible from [files], [len] is the current command line length: *) let rec get_files len acc files = match files with | [] -> (List.rev acc, []) | file :: rest -> let len = len + 1 + String.length file in if len > win32_max_command_line_length then (List.rev acc, files) else get_files len (file :: acc) rest in (* Split the command into several commands. *) let rec split args files = match files with | [] -> [] | _ -> (* Length of "ocamlfind install [META|-add]" *) let len = List.fold_left (fun len arg -> len + 1 (* for the space *) + String.length arg) (String.length ocamlfind) args in match get_files len [] files with | ([], _) -> failwith (s_ "Command line too long.") | (firsts, others) -> let cmd = args @ firsts in (* Use -add for remaining commands: *) let () = let findlib_ge_132 = OASISVersion.comparator_apply (OASISVersion.version_of_string (BaseStandardVar.findlib_version ())) (OASISVersion.VGreaterEqual (OASISVersion.version_of_string "1.3.2")) in if not findlib_ge_132 then failwithf (f_ "Installing the library %s require to use the \ flag '-add' of ocamlfind because the command \ line is too long. This flag is only available \ for findlib 1.3.2. Please upgrade findlib from \ %s to 1.3.2") findlib_name (BaseStandardVar.findlib_version ()) in let cmds = split other_args others in cmd :: cmds in (* The first command does not use -add: *) split first_args files else ["install" :: findlib_name :: meta :: files] let install pkg argv = let in_destdir = try let destdir = destdir () in (* Practically speaking destdir is prepended * at the beginning of the target filename *) fun fn -> destdir^fn with PropList.Not_set _ -> fun fn -> fn in let install_file ?tgt_fn src_file envdir = let tgt_dir = in_destdir (envdir ()) in let tgt_file = Filename.concat tgt_dir (match tgt_fn with | Some fn -> fn | None -> Filename.basename src_file) in (* Create target directory if needed *) OASISFileUtil.mkdir_parent ~ctxt:!BaseContext.default (fun dn -> info (f_ "Creating directory '%s'") dn; BaseLog.register install_dir_ev dn) tgt_dir; (* Really install files *) info (f_ "Copying file '%s' to '%s'") src_file tgt_file; OASISFileUtil.cp ~ctxt:!BaseContext.default src_file tgt_file; BaseLog.register install_file_ev tgt_file in (* Install data into defined directory *) let install_data srcdir lst tgtdir = let tgtdir = OASISHostPath.of_unix (var_expand tgtdir) in List.iter (fun (src, tgt_opt) -> let real_srcs = OASISFileUtil.glob ~ctxt:!BaseContext.default (Filename.concat srcdir src) in if real_srcs = [] then failwithf (f_ "Wildcard '%s' doesn't match any files") src; List.iter (fun fn -> install_file fn (fun () -> match tgt_opt with | Some s -> OASISHostPath.of_unix (var_expand s) | None -> tgtdir)) real_srcs) lst in let make_fnames modul sufx = List.fold_right begin fun sufx accu -> (String.capitalize modul ^ sufx) :: (String.uncapitalize modul ^ sufx) :: accu end sufx [] in (** Install all libraries *) let install_libs pkg = let files_of_library (f_data, acc) data_lib = let cs, bs, lib, lib_extra = !lib_hook data_lib in if var_choose bs.bs_install && BaseBuilt.is_built BaseBuilt.BLib cs.cs_name then begin let acc = (* Start with acc + lib_extra *) List.rev_append lib_extra acc in let acc = (* Add uncompiled header from the source tree *) let path = OASISHostPath.of_unix bs.bs_path in List.fold_left begin fun acc modul -> begin try [List.find OASISFileUtil.file_exists_case (List.map (Filename.concat path) (make_fnames modul [".mli"; ".ml"]))] with Not_found -> warning (f_ "Cannot find source header for module %s \ in library %s") modul cs.cs_name; [] end @ List.filter OASISFileUtil.file_exists_case (List.map (Filename.concat path) (make_fnames modul [".annot";".cmti";".cmt"])) @ acc end acc lib.lib_modules in let acc = (* Get generated files *) BaseBuilt.fold BaseBuilt.BLib cs.cs_name (fun acc fn -> fn :: acc) acc in let f_data () = (* Install data associated with the library *) install_data bs.bs_path bs.bs_data_files (Filename.concat (datarootdir ()) pkg.name); f_data () in (f_data, acc) end else begin (f_data, acc) end and files_of_object (f_data, acc) data_obj = let cs, bs, obj, obj_extra = !obj_hook data_obj in if var_choose bs.bs_install && BaseBuilt.is_built BaseBuilt.BObj cs.cs_name then begin let acc = (* Start with acc + obj_extra *) List.rev_append obj_extra acc in let acc = (* Add uncompiled header from the source tree *) let path = OASISHostPath.of_unix bs.bs_path in List.fold_left begin fun acc modul -> begin try [List.find OASISFileUtil.file_exists_case (List.map (Filename.concat path) (make_fnames modul [".mli"; ".ml"]))] with Not_found -> warning (f_ "Cannot find source header for module %s \ in object %s") modul cs.cs_name; [] end @ List.filter OASISFileUtil.file_exists_case (List.map (Filename.concat path) (make_fnames modul [".annot";".cmti";".cmt"])) @ acc end acc obj.obj_modules in let acc = (* Get generated files *) BaseBuilt.fold BaseBuilt.BObj cs.cs_name (fun acc fn -> fn :: acc) acc in let f_data () = (* Install data associated with the object *) install_data bs.bs_path bs.bs_data_files (Filename.concat (datarootdir ()) pkg.name); f_data () in (f_data, acc) end else begin (f_data, acc) end in (* Install one group of library *) let install_group_lib grp = (* Iterate through all group nodes *) let rec install_group_lib_aux data_and_files grp = let data_and_files, children = match grp with | Container (_, children) -> data_and_files, children | Package (_, cs, bs, `Library lib, children) -> files_of_library data_and_files (cs, bs, lib), children | Package (_, cs, bs, `Object obj, children) -> files_of_object data_and_files (cs, bs, obj), children in List.fold_left install_group_lib_aux data_and_files children in (* Findlib name of the root library *) let findlib_name = findlib_of_group grp in (* Determine root library *) let root_lib = root_of_group grp in (* All files to install for this library *) let f_data, files = install_group_lib_aux (ignore, []) grp in (* Really install, if there is something to install *) if files = [] then begin warning (f_ "Nothing to install for findlib library '%s'") findlib_name end else begin let meta = (* Search META file *) let _, bs, _ = root_lib in let res = Filename.concat bs.bs_path "META" in if not (OASISFileUtil.file_exists_case res) then failwithf (f_ "Cannot find file '%s' for findlib library %s") res findlib_name; res in let files = (* Make filename shorter to avoid hitting command max line length * too early, esp. on Windows. *) let remove_prefix p n = let plen = String.length p in let nlen = String.length n in if plen <= nlen && String.sub n 0 plen = p then begin let fn_sep = if Sys.os_type = "Win32" then '\\' else '/' in let cutpoint = plen + (if plen < nlen && n.[plen] = fn_sep then 1 else 0) in String.sub n cutpoint (nlen - cutpoint) end else n in List.map (remove_prefix (Sys.getcwd ())) files in info (f_ "Installing findlib library '%s'") findlib_name; let ocamlfind = ocamlfind () in let commands = split_install_command ocamlfind findlib_name meta files in List.iter (OASISExec.run ~ctxt:!BaseContext.default ocamlfind) commands; BaseLog.register install_findlib_ev findlib_name end; (* Install data files *) f_data (); in let group_libs, _, _ = findlib_mapping pkg in (* We install libraries in groups *) List.iter install_group_lib group_libs in let install_execs pkg = let install_exec data_exec = let cs, bs, exec = !exec_hook data_exec in if var_choose bs.bs_install && BaseBuilt.is_built BaseBuilt.BExec cs.cs_name then begin let exec_libdir () = Filename.concat (libdir ()) pkg.name in BaseBuilt.fold BaseBuilt.BExec cs.cs_name (fun () fn -> install_file ~tgt_fn:(cs.cs_name ^ ext_program ()) fn bindir) (); BaseBuilt.fold BaseBuilt.BExecLib cs.cs_name (fun () fn -> install_file fn exec_libdir) (); install_data bs.bs_path bs.bs_data_files (Filename.concat (datarootdir ()) pkg.name) end in List.iter (function | Executable (cs, bs, exec)-> install_exec (cs, bs, exec) | _ -> ()) pkg.sections in let install_docs pkg = let install_doc data = let cs, doc = !doc_hook data in if var_choose doc.doc_install && BaseBuilt.is_built BaseBuilt.BDoc cs.cs_name then begin let tgt_dir = OASISHostPath.of_unix (var_expand doc.doc_install_dir) in BaseBuilt.fold BaseBuilt.BDoc cs.cs_name (fun () fn -> install_file fn (fun () -> tgt_dir)) (); install_data Filename.current_dir_name doc.doc_data_files doc.doc_install_dir end in List.iter (function | Doc (cs, doc) -> install_doc (cs, doc) | _ -> ()) pkg.sections in install_libs pkg; install_execs pkg; install_docs pkg (* Uninstall already installed data *) let uninstall _ argv = List.iter (fun (ev, data) -> if ev = install_file_ev then begin if OASISFileUtil.file_exists_case data then begin info (f_ "Removing file '%s'") data; Sys.remove data end else begin warning (f_ "File '%s' doesn't exist anymore") data end end else if ev = install_dir_ev then begin if Sys.file_exists data && Sys.is_directory data then begin if Sys.readdir data = [||] then begin info (f_ "Removing directory '%s'") data; OASISFileUtil.rmdir ~ctxt:!BaseContext.default data end else begin warning (f_ "Directory '%s' is not empty (%s)") data (String.concat ", " (Array.to_list (Sys.readdir data))) end end else begin warning (f_ "Directory '%s' doesn't exist anymore") data end end else if ev = install_findlib_ev then begin info (f_ "Removing findlib library '%s'") data; OASISExec.run ~ctxt:!BaseContext.default (ocamlfind ()) ["remove"; data] end else failwithf (f_ "Unknown log event '%s'") ev; BaseLog.unregister ev data) (* We process event in reverse order *) (List.rev (BaseLog.filter [install_file_ev; install_dir_ev; install_findlib_ev])) end # 6273 "setup.ml" module OCamlbuildCommon = struct (* # 22 "src/plugins/ocamlbuild/OCamlbuildCommon.ml" *) (** Functions common to OCamlbuild build and doc plugin *) open OASISGettext open BaseEnv open BaseStandardVar open OASISTypes type extra_args = string list let ocamlbuild_clean_ev = "ocamlbuild-clean" let ocamlbuildflags = var_define ~short_desc:(fun () -> "OCamlbuild additional flags") "ocamlbuildflags" (fun () -> "") (** Fix special arguments depending on environment *) let fix_args args extra_argv = List.flatten [ if (os_type ()) = "Win32" then [ "-classic-display"; "-no-log"; "-no-links"; "-install-lib-dir"; (Filename.concat (standard_library ()) "ocamlbuild") ] else []; if not (bool_of_string (is_native ())) || (os_type ()) = "Win32" then [ "-byte-plugin" ] else []; args; if bool_of_string (debug ()) then ["-tag"; "debug"] else []; if bool_of_string (tests ()) then ["-tag"; "tests"] else []; if bool_of_string (profile ()) then ["-tag"; "profile"] else []; OASISString.nsplit (ocamlbuildflags ()) ' '; Array.to_list extra_argv; ] (** Run 'ocamlbuild -clean' if not already done *) let run_clean extra_argv = let extra_cli = String.concat " " (Array.to_list extra_argv) in (* Run if never called with these args *) if not (BaseLog.exists ocamlbuild_clean_ev extra_cli) then begin OASISExec.run ~ctxt:!BaseContext.default (ocamlbuild ()) (fix_args ["-clean"] extra_argv); BaseLog.register ocamlbuild_clean_ev extra_cli; at_exit (fun () -> try BaseLog.unregister ocamlbuild_clean_ev extra_cli with _ -> ()) end (** Run ocamlbuild, unregister all clean events *) let run_ocamlbuild args extra_argv = (* TODO: enforce that target in args must be UNIX encoded i.e. toto/index.html *) OASISExec.run ~ctxt:!BaseContext.default (ocamlbuild ()) (fix_args args extra_argv); (* Remove any clean event, we must run it again *) List.iter (fun (e, d) -> BaseLog.unregister e d) (BaseLog.filter [ocamlbuild_clean_ev]) (** Determine real build directory *) let build_dir extra_argv = let rec search_args dir = function | "-build-dir" :: dir :: tl -> search_args dir tl | _ :: tl -> search_args dir tl | [] -> dir in search_args "_build" (fix_args [] extra_argv) end module OCamlbuildPlugin = struct (* # 22 "src/plugins/ocamlbuild/OCamlbuildPlugin.ml" *) (** Build using ocamlbuild @author Sylvain Le Gall *) open OASISTypes open OASISGettext open OASISUtils open OASISString open BaseEnv open OCamlbuildCommon open BaseStandardVar open BaseMessage let cond_targets_hook = ref (fun lst -> lst) let build extra_args pkg argv = (* Return the filename in build directory *) let in_build_dir fn = Filename.concat (build_dir argv) fn in (* Return the unix filename in host build directory *) let in_build_dir_of_unix fn = in_build_dir (OASISHostPath.of_unix fn) in let cond_targets = List.fold_left (fun acc -> function | Library (cs, bs, lib) when var_choose bs.bs_build -> begin let evs, unix_files = BaseBuilt.of_library in_build_dir_of_unix (cs, bs, lib) in let tgts = List.flatten (List.filter (fun l -> l <> []) (List.map (List.filter (fun fn -> ends_with ~what:".cma" fn || ends_with ~what:".cmxs" fn || ends_with ~what:".cmxa" fn || ends_with ~what:(ext_lib ()) fn || ends_with ~what:(ext_dll ()) fn)) unix_files)) in match tgts with | _ :: _ -> (evs, tgts) :: acc | [] -> failwithf (f_ "No possible ocamlbuild targets for library %s") cs.cs_name end | Object (cs, bs, obj) when var_choose bs.bs_build -> begin let evs, unix_files = BaseBuilt.of_object in_build_dir_of_unix (cs, bs, obj) in let tgts = List.flatten (List.filter (fun l -> l <> []) (List.map (List.filter (fun fn -> ends_with ".cmo" fn || ends_with ".cmx" fn)) unix_files)) in match tgts with | _ :: _ -> (evs, tgts) :: acc | [] -> failwithf (f_ "No possible ocamlbuild targets for object %s") cs.cs_name end | Executable (cs, bs, exec) when var_choose bs.bs_build -> begin let evs, unix_exec_is, unix_dll_opt = BaseBuilt.of_executable in_build_dir_of_unix (cs, bs, exec) in let target ext = let unix_tgt = (OASISUnixPath.concat bs.bs_path (OASISUnixPath.chop_extension exec.exec_main_is))^ext in let evs = (* Fix evs, we want to use the unix_tgt, without copying *) List.map (function | BaseBuilt.BExec, nm, lst when nm = cs.cs_name -> BaseBuilt.BExec, nm, [[in_build_dir_of_unix unix_tgt]] | ev -> ev) evs in evs, [unix_tgt] in (* Add executable *) let acc = match bs.bs_compiled_object with | Native -> (target ".native") :: acc | Best when bool_of_string (is_native ()) -> (target ".native") :: acc | Byte | Best -> (target ".byte") :: acc in acc end | Library _ | Object _ | Executable _ | Test _ | SrcRepo _ | Flag _ | Doc _ -> acc) [] (* Keep the pkg.sections ordered *) (List.rev pkg.sections); in (* Check and register built files *) let check_and_register (bt, bnm, lst) = List.iter (fun fns -> if not (List.exists OASISFileUtil.file_exists_case fns) then failwithf (fn_ "Expected built file %s doesn't exist." "None of expected built files %s exists." (List.length fns)) (String.concat (s_ " or ") (List.map (Printf.sprintf "'%s'") fns))) lst; (BaseBuilt.register bt bnm lst) in (* Run the hook *) let cond_targets = !cond_targets_hook cond_targets in (* Run a list of target... *) run_ocamlbuild (List.flatten (List.map snd cond_targets) @ extra_args) argv; (* ... and register events *) List.iter check_and_register (List.flatten (List.map fst cond_targets)) let clean pkg extra_args = run_clean extra_args; List.iter (function | Library (cs, _, _) -> BaseBuilt.unregister BaseBuilt.BLib cs.cs_name | Executable (cs, _, _) -> BaseBuilt.unregister BaseBuilt.BExec cs.cs_name; BaseBuilt.unregister BaseBuilt.BExecLib cs.cs_name | _ -> ()) pkg.sections end module OCamlbuildDocPlugin = struct (* # 22 "src/plugins/ocamlbuild/OCamlbuildDocPlugin.ml" *) (* Create documentation using ocamlbuild .odocl files @author Sylvain Le Gall *) open OASISTypes open OASISGettext open OASISMessage open OCamlbuildCommon open BaseStandardVar type run_t = { extra_args: string list; run_path: unix_filename; } let doc_build run pkg (cs, doc) argv = let index_html = OASISUnixPath.make [ run.run_path; cs.cs_name^".docdir"; "index.html"; ] in let tgt_dir = OASISHostPath.make [ build_dir argv; OASISHostPath.of_unix run.run_path; cs.cs_name^".docdir"; ] in run_ocamlbuild (index_html :: run.extra_args) argv; List.iter (fun glb -> BaseBuilt.register BaseBuilt.BDoc cs.cs_name [OASISFileUtil.glob ~ctxt:!BaseContext.default (Filename.concat tgt_dir glb)]) ["*.html"; "*.css"] let doc_clean run pkg (cs, doc) argv = run_clean argv; BaseBuilt.unregister BaseBuilt.BDoc cs.cs_name end # 6651 "setup.ml" module CustomPlugin = struct (* # 22 "src/plugins/custom/CustomPlugin.ml" *) (** Generate custom configure/build/doc/test/install system @author *) open BaseEnv open OASISGettext open OASISTypes type t = { cmd_main: command_line conditional; cmd_clean: (command_line option) conditional; cmd_distclean: (command_line option) conditional; } let run = BaseCustom.run let main t _ extra_args = let cmd, args = var_choose ~name:(s_ "main command") t.cmd_main in run cmd args extra_args let clean t pkg extra_args = match var_choose t.cmd_clean with | Some (cmd, args) -> run cmd args extra_args | _ -> () let distclean t pkg extra_args = match var_choose t.cmd_distclean with | Some (cmd, args) -> run cmd args extra_args | _ -> () module Build = struct let main t pkg extra_args = main t pkg extra_args; List.iter (fun sct -> let evs = match sct with | Library (cs, bs, lib) when var_choose bs.bs_build -> begin let evs, _ = BaseBuilt.of_library OASISHostPath.of_unix (cs, bs, lib) in evs end | Executable (cs, bs, exec) when var_choose bs.bs_build -> begin let evs, _, _ = BaseBuilt.of_executable OASISHostPath.of_unix (cs, bs, exec) in evs end | _ -> [] in List.iter (fun (bt, bnm, lst) -> BaseBuilt.register bt bnm lst) evs) pkg.sections let clean t pkg extra_args = clean t pkg extra_args; (* TODO: this seems to be pretty generic (at least wrt to ocamlbuild * considering moving this to BaseSetup? *) List.iter (function | Library (cs, _, _) -> BaseBuilt.unregister BaseBuilt.BLib cs.cs_name | Executable (cs, _, _) -> BaseBuilt.unregister BaseBuilt.BExec cs.cs_name; BaseBuilt.unregister BaseBuilt.BExecLib cs.cs_name | _ -> ()) pkg.sections let distclean t pkg extra_args = distclean t pkg extra_args end module Test = struct let main t pkg (cs, test) extra_args = try main t pkg extra_args; 0.0 with Failure s -> BaseMessage.warning (f_ "Test '%s' fails: %s") cs.cs_name s; 1.0 let clean t pkg (cs, test) extra_args = clean t pkg extra_args let distclean t pkg (cs, test) extra_args = distclean t pkg extra_args end module Doc = struct let main t pkg (cs, _) extra_args = main t pkg extra_args; BaseBuilt.register BaseBuilt.BDoc cs.cs_name [] let clean t pkg (cs, _) extra_args = clean t pkg extra_args; BaseBuilt.unregister BaseBuilt.BDoc cs.cs_name let distclean t pkg (cs, _) extra_args = distclean t pkg extra_args end end # 6799 "setup.ml" open OASISTypes;; let setup_t = { BaseSetup.configure = InternalConfigurePlugin.configure; build = OCamlbuildPlugin.build []; test = [ ("pgocaml_lowlevel", CustomPlugin.Test.main { CustomPlugin.cmd_main = [(OASISExpr.EBool true, ("$test_pgocaml_lowlevel", []))]; cmd_clean = [(OASISExpr.EBool true, None)]; cmd_distclean = [(OASISExpr.EBool true, None)] }); ("pgocaml_highlevel", CustomPlugin.Test.main { CustomPlugin.cmd_main = [(OASISExpr.EBool true, ("$test_pgocaml_highlevel", []))]; cmd_clean = [(OASISExpr.EBool true, None)]; cmd_distclean = [(OASISExpr.EBool true, None)] }) ]; doc = [ ("APIDOC", OCamlbuildDocPlugin.doc_build {OCamlbuildDocPlugin.extra_args = []; run_path = "src"}) ]; install = InternalInstallPlugin.install; uninstall = InternalInstallPlugin.uninstall; clean = [OCamlbuildPlugin.clean]; clean_test = [ ("pgocaml_lowlevel", CustomPlugin.Test.clean { CustomPlugin.cmd_main = [(OASISExpr.EBool true, ("$test_pgocaml_lowlevel", []))]; cmd_clean = [(OASISExpr.EBool true, None)]; cmd_distclean = [(OASISExpr.EBool true, None)] }); ("pgocaml_highlevel", CustomPlugin.Test.clean { CustomPlugin.cmd_main = [(OASISExpr.EBool true, ("$test_pgocaml_highlevel", []))]; cmd_clean = [(OASISExpr.EBool true, None)]; cmd_distclean = [(OASISExpr.EBool true, None)] }) ]; clean_doc = [ ("APIDOC", OCamlbuildDocPlugin.doc_clean {OCamlbuildDocPlugin.extra_args = []; run_path = "src"}) ]; distclean = []; distclean_test = [ ("pgocaml_lowlevel", CustomPlugin.Test.distclean { CustomPlugin.cmd_main = [(OASISExpr.EBool true, ("$test_pgocaml_lowlevel", []))]; cmd_clean = [(OASISExpr.EBool true, None)]; cmd_distclean = [(OASISExpr.EBool true, None)] }); ("pgocaml_highlevel", CustomPlugin.Test.distclean { CustomPlugin.cmd_main = [(OASISExpr.EBool true, ("$test_pgocaml_highlevel", []))]; cmd_clean = [(OASISExpr.EBool true, None)]; cmd_distclean = [(OASISExpr.EBool true, None)] }) ]; distclean_doc = []; package = { oasis_version = "0.4"; ocaml_version = None; findlib_version = None; alpha_features = []; beta_features = []; name = "PG'OCaml"; version = "2.2"; license = OASISLicense.DEP5License (OASISLicense.DEP5Unit { OASISLicense.license = "LGPL"; excption = Some "OCaml linking"; version = OASISLicense.Version "2.0" }); license_file = Some "LICENSE.txt"; copyrights = []; maintainers = ["Dario Teixeira"]; authors = ["Richard W. M. Jones et al."]; homepage = None; synopsis = "OCaml bindings for the PostgreSQL database"; description = None; categories = []; conf_type = (`Configure, "internal", Some "0.4"); conf_custom = { pre_command = [(OASISExpr.EBool true, None)]; post_command = [(OASISExpr.EBool true, None)] }; build_type = (`Build, "ocamlbuild", Some "0.4"); build_custom = { pre_command = [(OASISExpr.EBool true, Some (("$make", ["config"])))]; post_command = [(OASISExpr.EBool true, None)] }; install_type = (`Install, "internal", Some "0.4"); install_custom = { pre_command = [(OASISExpr.EBool true, None)]; post_command = [(OASISExpr.EBool true, None)] }; uninstall_custom = { pre_command = [(OASISExpr.EBool true, None)]; post_command = [(OASISExpr.EBool true, None)] }; clean_custom = { pre_command = [(OASISExpr.EBool true, None)]; post_command = [(OASISExpr.EBool true, None)] }; distclean_custom = { pre_command = [(OASISExpr.EBool true, None)]; post_command = [(OASISExpr.EBool true, None)] }; files_ab = []; sections = [ Flag ({ cs_name = "pgprof"; cs_data = PropList.Data.create (); cs_plugin_data = [] }, { flag_description = Some "Build PG'OCaml profiling utility"; flag_default = [(OASISExpr.EBool true, true)] }); Library ({ cs_name = "pgocaml"; cs_data = PropList.Data.create (); cs_plugin_data = [] }, { bs_build = [(OASISExpr.EBool true, true)]; bs_install = [(OASISExpr.EBool true, true)]; bs_path = "src"; bs_compiled_object = Best; bs_build_depends = [ FindlibPackage ("unix", None); FindlibPackage ("calendar", None); FindlibPackage ("csv", None); FindlibPackage ("pcre", None); FindlibPackage ("bytes", None) ]; bs_build_tools = [ ExternalTool "ocamlbuild"; ExternalTool "make"; ExternalTool "camlp4" ]; bs_c_sources = []; bs_data_files = []; bs_ccopt = [(OASISExpr.EBool true, [])]; bs_cclib = [(OASISExpr.EBool true, [])]; bs_dlllib = [(OASISExpr.EBool true, [])]; bs_dllpath = [(OASISExpr.EBool true, [])]; bs_byteopt = [(OASISExpr.EBool true, [])]; bs_nativeopt = [(OASISExpr.EBool true, [])] }, { lib_modules = [ "PGOCaml_aux"; "PGOCaml"; "PGOCaml_generic"; "PGOCaml_config" ]; lib_pack = false; lib_internal_modules = []; lib_findlib_parent = None; lib_findlib_name = Some "pgocaml"; lib_findlib_containers = [] }); Library ({ cs_name = "pa_pgsql"; cs_data = PropList.Data.create (); cs_plugin_data = [] }, { bs_build = [(OASISExpr.EBool true, true)]; bs_install = [(OASISExpr.EBool true, true)]; bs_path = "src"; bs_compiled_object = Byte; bs_build_depends = [ InternalLibrary "pgocaml"; FindlibPackage ("camlp4.quotations", None); FindlibPackage ("camlp4.extend", None) ]; bs_build_tools = [ ExternalTool "ocamlbuild"; ExternalTool "make"; ExternalTool "camlp4" ]; bs_c_sources = []; bs_data_files = []; bs_ccopt = [(OASISExpr.EBool true, [])]; bs_cclib = [(OASISExpr.EBool true, [])]; bs_dlllib = [(OASISExpr.EBool true, [])]; bs_dllpath = [(OASISExpr.EBool true, [])]; bs_byteopt = [(OASISExpr.EBool true, [])]; bs_nativeopt = [(OASISExpr.EBool true, [])] }, { lib_modules = ["Pa_pgsql"]; lib_pack = false; lib_internal_modules = []; lib_findlib_parent = Some "pgocaml"; lib_findlib_name = Some "syntax"; lib_findlib_containers = [] }); Doc ({ cs_name = "APIDOC"; cs_data = PropList.Data.create (); cs_plugin_data = [] }, { doc_type = (`Doc, "ocamlbuild", Some "0.4"); doc_custom = { pre_command = [(OASISExpr.EBool true, None)]; post_command = [(OASISExpr.EBool true, None)] }; doc_build = [ (OASISExpr.ENot (OASISExpr.EFlag "docs"), false); (OASISExpr.EFlag "docs", true) ]; doc_install = [(OASISExpr.EBool true, true)]; doc_install_dir = "$htmldir"; doc_title = "API reference for PG'OCaml"; doc_authors = []; doc_abstract = None; doc_format = OtherDoc; doc_data_files = []; doc_build_tools = [ ExternalTool "ocamlbuild"; ExternalTool "make"; ExternalTool "camlp4"; ExternalTool "ocamldoc" ] }); Executable ({ cs_name = "pgocaml_prof"; cs_data = PropList.Data.create (); cs_plugin_data = [] }, { bs_build = [ (OASISExpr.EBool true, false); (OASISExpr.EFlag "pgprof", true) ]; bs_install = [(OASISExpr.EBool true, false)]; bs_path = "utils"; bs_compiled_object = Byte; bs_build_depends = [InternalLibrary "pgocaml"]; bs_build_tools = [ ExternalTool "ocamlbuild"; ExternalTool "make"; ExternalTool "camlp4" ]; bs_c_sources = []; bs_data_files = []; bs_ccopt = [(OASISExpr.EBool true, [])]; bs_cclib = [(OASISExpr.EBool true, [])]; bs_dlllib = [(OASISExpr.EBool true, [])]; bs_dllpath = [(OASISExpr.EBool true, [])]; bs_byteopt = [(OASISExpr.EBool true, [])]; bs_nativeopt = [(OASISExpr.EBool true, [])] }, {exec_custom = false; exec_main_is = "pgocaml_prof.ml"}); Executable ({ cs_name = "test_pgocaml_lowlevel"; cs_data = PropList.Data.create (); cs_plugin_data = [] }, { bs_build = [ (OASISExpr.EBool true, false); (OASISExpr.EFlag "tests", true) ]; bs_install = [(OASISExpr.EBool true, false)]; bs_path = "tests"; bs_compiled_object = Byte; bs_build_depends = [InternalLibrary "pgocaml"]; bs_build_tools = [ ExternalTool "ocamlbuild"; ExternalTool "make"; ExternalTool "camlp4" ]; bs_c_sources = []; bs_data_files = []; bs_ccopt = [(OASISExpr.EBool true, [])]; bs_cclib = [(OASISExpr.EBool true, [])]; bs_dlllib = [(OASISExpr.EBool true, [])]; bs_dllpath = [(OASISExpr.EBool true, [])]; bs_byteopt = [(OASISExpr.EBool true, [])]; bs_nativeopt = [(OASISExpr.EBool true, [])] }, { exec_custom = false; exec_main_is = "test_pgocaml_lowlevel.ml" }); Executable ({ cs_name = "test_pgocaml_highlevel"; cs_data = PropList.Data.create (); cs_plugin_data = [] }, { bs_build = [ (OASISExpr.EBool true, false); (OASISExpr.EFlag "tests", true) ]; bs_install = [(OASISExpr.EBool true, false)]; bs_path = "tests"; bs_compiled_object = Byte; bs_build_depends = [ InternalLibrary "pgocaml"; InternalLibrary "pa_pgsql" ]; bs_build_tools = [ ExternalTool "ocamlbuild"; ExternalTool "make"; ExternalTool "camlp4" ]; bs_c_sources = []; bs_data_files = []; bs_ccopt = [(OASISExpr.EBool true, [])]; bs_cclib = [(OASISExpr.EBool true, [])]; bs_dlllib = [(OASISExpr.EBool true, [])]; bs_dllpath = [(OASISExpr.EBool true, [])]; bs_byteopt = [(OASISExpr.EBool true, [])]; bs_nativeopt = [(OASISExpr.EBool true, [])] }, { exec_custom = false; exec_main_is = "test_pgocaml_highlevel.ml" }); Test ({ cs_name = "pgocaml_lowlevel"; cs_data = PropList.Data.create (); cs_plugin_data = [] }, { test_type = (`Test, "custom", Some "0.4"); test_command = [ (OASISExpr.EBool true, ("$test_pgocaml_lowlevel", [])) ]; test_custom = { pre_command = [(OASISExpr.EBool true, None)]; post_command = [(OASISExpr.EBool true, None)] }; test_working_directory = Some "tests"; test_run = [ (OASISExpr.ENot (OASISExpr.EFlag "tests"), false); (OASISExpr.EFlag "tests", true) ]; test_tools = [ ExternalTool "ocamlbuild"; ExternalTool "make"; ExternalTool "camlp4" ] }); Test ({ cs_name = "pgocaml_highlevel"; cs_data = PropList.Data.create (); cs_plugin_data = [] }, { test_type = (`Test, "custom", Some "0.4"); test_command = [ (OASISExpr.EBool true, ("$test_pgocaml_highlevel", [])) ]; test_custom = { pre_command = [(OASISExpr.EBool true, None)]; post_command = [(OASISExpr.EBool true, None)] }; test_working_directory = Some "tests"; test_run = [ (OASISExpr.ENot (OASISExpr.EFlag "tests"), false); (OASISExpr.EFlag "tests", true) ]; test_tools = [ ExternalTool "ocamlbuild"; ExternalTool "make"; ExternalTool "camlp4" ] }) ]; plugins = [(`Extra, "META", Some "0.4"); (`Extra, "DevFiles", Some "0.4")]; disable_oasis_section = []; schema_data = PropList.Data.create (); plugin_data = [] }; oasis_fn = Some "_oasis"; oasis_version = "0.4.5"; oasis_digest = Some "=#\021(1\214^*s;\006\230\026P3."; oasis_exec = None; oasis_setup_args = []; setup_update = false };; let setup () = BaseSetup.setup setup_t;; # 7260 "setup.ml" (* OASIS_STOP *) let () = setup ();; pgocaml-2.2/src/000077500000000000000000000000001251121767700135775ustar00rootroot00000000000000pgocaml-2.2/src/APIDOC.odocl000066400000000000000000000002071251121767700155570ustar00rootroot00000000000000# OASIS_START # DO NOT EDIT (digest: da5a8c724b284262dc76d857dc1e81b6) PGOCaml_aux PGOCaml PGOCaml_generic PGOCaml_config # OASIS_STOP pgocaml-2.2/src/META000066400000000000000000000011331251121767700142460ustar00rootroot00000000000000# OASIS_START # DO NOT EDIT (digest: 023ae9385c9c2cfcf39a4b5db8ae0c35) version = "2.2" description = "OCaml bindings for the PostgreSQL database" requires = "unix calendar csv pcre bytes" archive(byte) = "pgocaml.cma" archive(byte, plugin) = "pgocaml.cma" archive(native) = "pgocaml.cmxa" archive(native, plugin) = "pgocaml.cmxs" exists_if = "pgocaml.cma" package "syntax" ( version = "2.2" description = "Syntax extension for PG'OCaml" requires = "pgocaml camlp4" archive(syntax, preprocessor) = "pa_pgsql.cma" archive(syntax, toploop) = "pa_pgsql.cma" exists_if = "pa_pgsql.cma" ) # OASIS_STOP pgocaml-2.2/src/PGOCaml.ml000066400000000000000000000030271251121767700153550ustar00rootroot00000000000000(* PG'OCaml is a set of OCaml bindings for the PostgreSQL database. * * PG'OCaml - type safe interface to PostgreSQL. * Copyright (C) 2005-2009 Richard Jones and other authors. * * 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 COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) module Simple_thread = struct type 'a t = 'a let return x = x let (>>=) v f = f v let fail = raise let catch f fexn = try f () with e -> fexn e type in_channel = Pervasives.in_channel type out_channel = Pervasives.out_channel let open_connection = Unix.open_connection let output_char = output_char let output_binary_int = output_binary_int let output_string = output_string let flush = flush let input_char = input_char let input_binary_int = input_binary_int let really_input = really_input let close_in = close_in end module M = PGOCaml_generic.Make (Simple_thread) include M pgocaml-2.2/src/PGOCaml.mli000066400000000000000000000020671251121767700155310ustar00rootroot00000000000000(* PG'OCaml is a set of OCaml bindings for the PostgreSQL database. * * PG'OCaml - type safe interface to PostgreSQL. * Copyright (C) 2005-2009 Richard Jones and other authors. * * 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 COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) module Simple_thread : PGOCaml_generic.THREAD with type 'a t = 'a include PGOCaml_generic.PGOCAML_GENERIC with type 'a monad = 'a pgocaml-2.2/src/PGOCaml_aux.ml000066400000000000000000000026321251121767700162330ustar00rootroot00000000000000module String = struct include String let starts_with str prefix = let len = length prefix in if length str < len then false else let rec aux i = if i >= len then true else if unsafe_get str i <> unsafe_get prefix i then false else aux (i + 1) in aux 0 let join = concat let implode xs = let buf = Buffer.create (List.length xs) in List.iter (Buffer.add_char buf) xs; Buffer.contents buf let fold_left f init str = let len = length str in let rec loop i accum = if i = len then accum else loop (i + 1) (f accum str.[i]) in loop 0 init (* Only available in the standard library since OCaml 4.02 *) let init n f = let s = Bytes.create n in for i = 0 to n - 1 do Bytes.unsafe_set s i (f i) done; Bytes.to_string s end module Option = struct let default v = function | Some v -> v | None -> v let get = function | Some v -> v | None -> invalid_arg "PGOCaml_aux.Option.get" let map f = function | Some v -> Some (f v) | None -> None end module List = struct include List let iteri f xs = let rec loop i = function | [] -> () | hd :: tl -> f i hd; loop (i+1) tl in loop 0 xs let mapi f xs = let rec loop i = function | [] -> [] | hd :: tl -> let hd' = f i hd in hd' :: loop (i+1) tl in loop 0 xs end pgocaml-2.2/src/PGOCaml_aux.mli000066400000000000000000000011561251121767700164040ustar00rootroot00000000000000module String : sig include module type of String val starts_with : string -> string -> bool val join : string -> string list -> string val implode : char list -> string val fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a val init: int -> (int -> char) -> string end module Option : sig val default : 'a -> 'a option -> 'a val get : 'a option -> 'a val map : ('a -> 'b) -> 'a option -> 'b option end module List : sig include module type of List val iteri : (int -> 'a -> unit) -> 'a list -> unit val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list end pgocaml-2.2/src/PGOCaml_config.ml000066400000000000000000000000731251121767700167000ustar00rootroot00000000000000let default_unix_domain_socket_dir = "/var/run/postgresql" pgocaml-2.2/src/PGOCaml_generic.ml000066400000000000000000001603731251121767700170610ustar00rootroot00000000000000(* PG'OCaml is a set of OCaml bindings for the PostgreSQL database. * * PG'OCaml - type safe interface to PostgreSQL. * Copyright (C) 2005-2009 Richard Jones and other authors. * * 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 COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open PGOCaml_aux open CalendarLib open Printf module type THREAD = sig type 'a t val return : 'a -> 'a t val (>>=) : 'a t -> ('a -> 'b t) -> 'b t val fail : exn -> 'a t val catch : (unit -> 'a t) -> (exn -> 'a t) -> 'a t type in_channel type out_channel val open_connection : Unix.sockaddr -> (in_channel * out_channel) t val output_char : out_channel -> char -> unit t val output_binary_int : out_channel -> int -> unit t val output_string : out_channel -> string -> unit t val flush : out_channel -> unit t val input_char : in_channel -> char t val input_binary_int : in_channel -> int t val really_input : in_channel -> Bytes.t -> int -> int -> unit t val close_in : in_channel -> unit t end module type PGOCAML_GENERIC = sig type 'a t (** Database handle. *) type 'a monad type isolation = [ `Serializable | `Repeatable_read | `Read_committed | `Read_uncommitted ] type access = [ `Read_write | `Read_only ] exception Error of string (** For library errors. *) exception PostgreSQL_Error of string * (char * string) list (** For errors generated by the PostgreSQL database back-end. The * first argument is a printable error message. The second argument * is the complete set of error fields returned from the back-end. * See [http://www.postgresql.org/docs/8.1/static/protocol-error-fields.html] *) (** {6 Connection management} *) val connect : ?host:string -> ?port:int -> ?user:string -> ?password:string -> ?database:string -> ?unix_domain_socket_dir:string -> unit -> 'a t monad (** Connect to the database. The normal [$PGDATABASE], etc. environment * variables are available. *) val close : 'a t -> unit monad (** Close the database handle. You must call this after you have * finished with the handle, or else you will get leaked file * descriptors. *) val ping : 'a t -> unit monad (** Ping the database. If the database is not available, some sort of * exception will be thrown. *) val alive : 'a t -> bool monad (** This function is a wrapper of [ping] that returns a boolean instead of * raising an exception. *) (** {6 Transactions} *) val begin_work : ?isolation:isolation -> ?access:access -> ?deferrable:bool -> 'a t -> unit monad (** Start a transaction. *) val commit : 'a t -> unit monad (** Perform a COMMIT operation on the database. *) val rollback : 'a t -> unit monad (** Perform a ROLLBACK operation on the database. *) val transact : 'a t -> ?isolation:isolation -> ?access:access -> ?deferrable:bool -> ('a t -> 'b monad) -> 'b monad (** [transact db ?isolation ?access ?deferrable f] wraps your * function [f] inside a transactional block. * First it calls [begin_work] with [isolation], [access] and [deferrable], * then calls [f] and do [rollback] if [f] raises * an exception, [commit] otherwise. *) (** {6 Serial column} *) val serial : 'a t -> string -> int64 monad (** This is a shorthand for [SELECT CURRVAL(serial)]. For a table * called [table] with serial column [id] you would typically * call this as [serial dbh "table_id_seq"] after the previous INSERT * operation to get the serial number of the inserted row. *) val serial4 : 'a t -> string -> int32 monad (** As {!serial} but assumes that the column is a SERIAL or * SERIAL4 type. *) val serial8 : 'a t -> string -> int64 monad (** Same as {!serial}. *) (** {6 Miscellaneous} *) val max_message_length : int ref (** Maximum message length accepted from the back-end. The default * is [Sys.max_string_length], which means that we will try to read as * much data from the back-end as we can, and this may cause us to * run out of memory (particularly on 64 bit machines), causing a * possible denial of service. You may want to set this to a smaller * size to avoid this happening. *) val verbose : int ref (** Verbosity. 0 means don't print anything. 1 means print short * error messages as returned from the back-end. 2 means print all * messages as returned from the back-end. Messages are printed on [stderr]. * Default verbosity level is 1. *) val set_private_data : 'a t -> 'a -> unit (** Attach some private data to the database handle. * * NB. The pa_pgsql camlp4 extension uses this for its own purposes, which * means that in most programs you will not be able to attach private data * to the database handle. *) val private_data : 'a t -> 'a (** Retrieve some private data previously attached to the database handle. * If no data has been attached, raises [Not_found]. * * NB. The pa_pgsql camlp4 extension uses this for its own purposes, which * means that in most programs you will not be able to attach private data * to the database handle. *) val uuid : 'a t -> string type pa_pg_data = (string, bool) Hashtbl.t (** When using pa_pgsql, database handles have type * [PGOCaml.pa_pg_data PGOCaml.t] *) (** {6 Low level query interface - DO NOT USE DIRECTLY} *) type oid = int32 type param = string option (** None is NULL. *) type result = string option (** None is NULL. *) type row = result list (** One row is a list of fields. *) val prepare : 'a t -> query:string -> ?name:string -> ?types:oid list -> unit -> unit monad (** [prepare conn ~query ?name ?types ()] prepares the statement [query] * and optionally names it [name] and sets the parameter types to [types]. * If no name is given, then the "unnamed" statement is overwritten. If * no types are given, then the PostgreSQL engine infers types. * Synchronously checks for errors. *) val execute_rev : 'a t -> ?name:string -> ?portal:string -> params:param list -> unit -> row list monad val execute : 'a t -> ?name:string -> ?portal:string -> params:param list -> unit -> row list monad (** [execute conn ?name ~params ()] executes the named or unnamed * statement [name], with the given parameters [params], * returning the result rows (if any). * * There are several steps involved at the protocol layer: * (1) a "portal" is created from the statement, binding the * parameters in the statement (Bind). * (2) the portal is executed (Execute). * (3) we synchronise the connection (Sync). * * The optional [?portal] parameter may be used to name the portal * created in step (1) above (otherwise the unnamed portal is used). * This is only important if you want to call {!PGOCaml.describe_portal} * to find out the result types. *) val cursor : 'a t -> ?name:string -> ?portal:string -> params:param list -> (row -> unit monad) -> unit monad val close_statement : 'a t -> ?name:string -> unit -> unit monad (** [close_statement conn ?name ()] closes a prepared statement and frees * up any resources. *) val close_portal : 'a t -> ?portal:string -> unit -> unit monad (** [close_portal conn ?portal ()] closes a portal and frees up any resources. *) val inject : 'a t -> ?name:string -> string -> row list monad (** [inject conn ?name query] executes the statement [query] * and optionally names it [name] and gives the result. *) val alter : 'a t -> ?name:string -> string -> unit monad (** [alter conn ?name query] executes the statement [query] * and optionally names it [name]. Same as inject but ignoring the result. *) type row_description = result_description list and result_description = { name : string; (** Field name. *) table : oid option; (** OID of table. *) column : int option; (** Column number of field in table. *) field_type : oid; (** The type of the field. *) length : int; (** Length of the field. *) modifier : int32; (** Type modifier. *) } type params_description = param_description list and param_description = { param_type : oid; (** The type of the parameter. *) } val describe_statement : 'a t -> ?name:string -> unit -> (params_description * row_description option) monad (** [describe_statement conn ?name ()] describes the named or unnamed * statement's parameter types and result types. *) val describe_portal : 'a t -> ?portal:string -> unit -> row_description option monad (** [describe_portal conn ?portal ()] describes the named or unnamed * portal's result types. *) (** {6 Low level type conversion functions - DO NOT USE DIRECTLY} *) val name_of_type : ?modifier:int32 -> oid -> string (** Returns the OCaml equivalent type name to the PostgreSQL type [oid]. * For instance, [name_of_type (Int32.of_int 23)] returns ["int32"] because * the OID for PostgreSQL's internal [int4] type is [23]. As another * example, [name_of_type (Int32.of_int 25)] returns ["string"]. *) type inet = Unix.inet_addr * int type timestamptz = Calendar.t * Time_Zone.t type int16 = int type bytea = string (* XXX *) type point = float * float type hstore = (string * string option) list type numeric = string type bool_array = bool option list type int32_array = int32 option list type int64_array = int64 option list type string_array = string option list type float_array = float option list (** The following conversion functions are used by pa_pgsql to convert * values in and out of the database. *) val string_of_oid : oid -> string val string_of_bool : bool -> string val string_of_int : int -> string val string_of_int16 : int16 -> string val string_of_int32 : int32 -> string val string_of_int64 : int64 -> string val string_of_float : float -> string val string_of_point : point -> string val string_of_hstore : hstore -> string val string_of_numeric : numeric -> string val string_of_inet : inet -> string val string_of_timestamp : Calendar.t -> string val string_of_timestamptz : timestamptz -> string val string_of_date : Date.t -> string val string_of_time : Time.t -> string val string_of_interval : Calendar.Period.t -> string val string_of_bytea : bytea -> string val string_of_string : string -> string val string_of_unit : unit -> string val string_of_bool_array : bool_array -> string val string_of_int32_array : int32_array -> string val string_of_int64_array : int64_array -> string val string_of_string_array : string_array -> string val string_of_float_array : float_array -> string val oid_of_string : string -> oid val bool_of_string : string -> bool val int_of_string : string -> int val int16_of_string : string -> int16 val int32_of_string : string -> int32 val int64_of_string : string -> int64 val float_of_string : string -> float val point_of_string : string -> point val hstore_of_string : string -> hstore val numeric_of_string : string -> numeric val inet_of_string : string -> inet val timestamp_of_string : string -> Calendar.t val timestamptz_of_string : string -> timestamptz val date_of_string : string -> Date.t val time_of_string : string -> Time.t val interval_of_string : string -> Calendar.Period.t val bytea_of_string : string -> bytea val unit_of_string : string -> unit val bool_array_of_string : string -> bool_array val int32_array_of_string : string -> int32_array val int64_array_of_string : string -> int64_array val string_array_of_string : string -> string_array val float_array_of_string : string -> float_array val bind : 'a monad -> ('a -> 'b monad) -> 'b monad val return : 'a -> 'a monad end module Make (Thread : THREAD) = struct open Thread type 'a t = { ichan : in_channel; (* In_channel wrapping socket. *) chan : out_channel; (* Out_channel wrapping socket. *) mutable private_data : 'a option; uuid : string; (* UUID for this connection. *) } type 'a monad = 'a Thread.t type isolation = [ `Serializable | `Repeatable_read | `Read_committed | `Read_uncommitted ] type access = [ `Read_write | `Read_only ] exception Error of string exception PostgreSQL_Error of string * (char * string) list (* If true, emit a lot of debugging information about the protocol on stderr.*) let debug_protocol = false (*----- Code to generate messages for the back-end. -----*) let new_message typ = let buf = Buffer.create 128 in buf, Some typ (* StartUpMessage and SSLRequest are special messages which don't * have a type byte field. *) let new_start_message () = let buf = Buffer.create 128 in buf, None let add_byte (buf, _) i = (* Deliberately throw an exception if i isn't [0..255]. *) Buffer.add_char buf (Char.chr i) let add_char (buf, _) c = Buffer.add_char buf c let add_int16 (buf, _) i = if i < 0 || i > 65_535 then raise (Error "PGOCaml: int16 is outside range [0..65535]."); Buffer.add_char buf (Char.unsafe_chr ((i lsr 8) land 0xff)); Buffer.add_char buf (Char.unsafe_chr (i land 0xff)) let add_int32 (buf, _) i = let base = Int32.to_int i in let big = Int32.to_int (Int32.shift_right_logical i 24) in Buffer.add_char buf (Char.unsafe_chr (big land 0xff)); Buffer.add_char buf (Char.unsafe_chr ((base lsr 16) land 0xff)); Buffer.add_char buf (Char.unsafe_chr ((base lsr 8) land 0xff)); Buffer.add_char buf (Char.unsafe_chr (base land 0xff)) let add_int64 msg i = add_int32 msg (Int64.to_int32 (Int64.shift_right_logical i 32)); add_int32 msg (Int64.to_int32 i) let add_string_no_trailing_nil (buf, _) str = (* Check the string doesn't contain '\0' characters. *) if String.contains str '\000' then raise (Error (sprintf "PGOCaml: string contains ASCII NIL character: %S" str)); if String.length str > 0x3fff_ffff then raise (Error "PGOCaml: string is too long."); Buffer.add_string buf str let add_string msg str = add_string_no_trailing_nil msg str; add_byte msg 0 let send_message { chan = chan } (buf, typ) = (* Get the length in bytes. *) let len = 4 + Buffer.length buf in (* If the length is longer than a 31 bit integer, then the message is * too long to send. This limits messages to 1 GB, which should be * enough for anyone :-) *) if Int64.of_int len >= 0x4000_0000L then raise (Error "PGOCaml: message is larger than 1 GB"); if debug_protocol then eprintf "> %s%d %S\n%!" (match typ with | None -> "" | Some c -> sprintf "%c " c) len (Buffer.contents buf); (* Write the type byte? *) (match typ with | None -> Thread.return () | Some c -> output_char chan c ) >>= fun () -> (* Write the length field. *) output_binary_int chan len >>= fun () -> (* Write the buffer. *) output_string chan (Buffer.contents buf) (* Max message length accepted from back-end. *) let max_message_length = ref Sys.max_string_length (* Receive a single result message. Parse out the message type, * message length, and binary message content. *) let receive_message { ichan = ichan; chan = chan } = (* Flush output buffer. *) flush chan >>= fun () -> input_char ichan >>= fun typ -> input_binary_int ichan >>= fun len -> (* Discount the length word itself. *) let len = len - 4 in (* If the message is too long, give up now. *) if len > !max_message_length then ( (* Skip the message so we stay in synch with the stream. *) let bufsize = 65_536 in let buf = Bytes.create bufsize in let rec loop n = if n > 0 then begin let m = min n bufsize in really_input ichan buf 0 m >>= fun () -> loop (n - m) end else return () in loop len >>= fun () -> fail (Error "PGOCaml: back-end message is longer than max_message_length") ) else ( (* Read the binary message content. *) let msg = Bytes.create len in really_input ichan msg 0 len >>= fun () -> return (typ, Bytes.to_string msg) ) (* Send a message and expect a single result. *) let send_recv conn msg = send_message conn msg >>= fun () -> receive_message conn (* Parse a back-end message. *) type msg_t = | AuthenticationOk | AuthenticationKerberosV5 | AuthenticationCleartextPassword | AuthenticationCryptPassword of string | AuthenticationMD5Password of string | AuthenticationSCMCredential | BackendKeyData of int32 * int32 | BindComplete | CloseComplete | CommandComplete of string | DataRow of (int * string) list | EmptyQueryResponse | ErrorResponse of (char * string) list | NoData | NoticeResponse of (char * string) list | ParameterDescription of int32 list | ParameterStatus of string * string | ParseComplete | ReadyForQuery of char | RowDescription of (string * int32 * int * int32 * int * int32 * int) list | UnknownMessage of char * string let string_of_msg_t = function | AuthenticationOk -> "AuthenticationOk" | AuthenticationKerberosV5 -> "AuthenticationKerberosV5" | AuthenticationCleartextPassword -> "AuthenticationCleartextPassword" | AuthenticationCryptPassword str -> sprintf "AuthenticationCleartextPassword %S" str | AuthenticationMD5Password str -> sprintf "AuthenticationMD5Password %S" str | AuthenticationSCMCredential -> "AuthenticationMD5Password" | BackendKeyData (i1, i2) -> sprintf "BackendKeyData %ld, %ld" i1 i2 | BindComplete -> "BindComplete" | CloseComplete -> "CloseComplete" | CommandComplete str -> sprintf "CommandComplete %S" str | DataRow fields -> sprintf "DataRow [%s]" (String.concat "; " (List.map (fun (len, bytes) -> sprintf "%d, %S" len bytes) fields)) | EmptyQueryResponse -> "EmptyQueryResponse" | ErrorResponse strs -> sprintf "ErrorResponse [%s]" (String.concat "; " (List.map (fun (k, v) -> sprintf "%c, %S" k v) strs)) | NoData -> "NoData" | NoticeResponse strs -> sprintf "NoticeResponse [%s]" (String.concat "; " (List.map (fun (k, v) -> sprintf "%c, %S" k v) strs)) | ParameterDescription fields -> sprintf "ParameterDescription [%s]" (String.concat "; " (List.map (fun oid -> sprintf "%ld" oid) fields)) | ParameterStatus (s1, s2) -> sprintf "ParameterStatus %S, %S" s1 s2 | ParseComplete -> "ParseComplete" | ReadyForQuery c -> sprintf "ReadyForQuery %s" (match c with | 'I' -> "Idle" | 'T' -> "inTransaction" | 'E' -> "Error" | c -> sprintf "unknown(%c)" c) | RowDescription fields -> sprintf "RowDescription [%s]" (String.concat "; " (List.map (fun (name, table, col, oid, len, modifier, format) -> sprintf "%s %ld %d %ld %d %ld %d" name table col oid len modifier format) fields)) | UnknownMessage (typ, msg) -> sprintf "UnknownMessage %c, %S" typ msg let parse_backend_message (typ, msg) = let pos = ref 0 in let len = String.length msg in (* Functions to grab the next object from the string 'msg'. *) let get_char where = if !pos < len then ( let r = msg.[!pos] in incr pos; r ) else raise (Error ("PGOCaml: parse_backend_message: " ^ where ^ ": short message")) in let get_byte where = Char.code (get_char where) in let get_int16 () = let r0 = get_byte "get_int16" in let r1 = get_byte "get_int16" in (r0 lsr 8) + r1 in let get_int32 () = let r0 = get_byte "get_int32" in let r1 = get_byte "get_int32" in let r2 = get_byte "get_int32" in let r3 = get_byte "get_int32" in let r = Int32.of_int r0 in let r = Int32.shift_left r 8 in let r = Int32.logor r (Int32.of_int r1) in let r = Int32.shift_left r 8 in let r = Int32.logor r (Int32.of_int r2) in let r = Int32.shift_left r 8 in let r = Int32.logor r (Int32.of_int r3) in r in (*let get_int64 () = let r0 = get_byte "get_int64" in let r1 = get_byte "get_int64" in let r2 = get_byte "get_int64" in let r3 = get_byte "get_int64" in let r4 = get_byte "get_int64" in let r5 = get_byte "get_int64" in let r6 = get_byte "get_int64" in let r7 = get_byte "get_int64" in let r = Int64.of_int r0 in let r = Int64.shift_left r 8 in let r = Int64.logor r (Int64.of_int r1) in let r = Int64.shift_left r 8 in let r = Int64.logor r (Int64.of_int r2) in let r = Int64.shift_left r 8 in let r = Int64.logor r (Int64.of_int r3) in let r = Int64.shift_left r 8 in let r = Int64.logor r (Int64.of_int r4) in let r = Int64.shift_left r 8 in let r = Int64.logor r (Int64.of_int r5) in let r = Int64.shift_left r 8 in let r = Int64.logor r (Int64.of_int r6) in let r = Int64.shift_left r 8 in let r = Int64.logor r (Int64.of_int r7) in r in*) let get_string () = let buf = Buffer.create 16 in let rec loop () = let c = get_char "get_string" in if c <> '\000' then ( Buffer.add_char buf c; loop () ) else Buffer.contents buf in loop () in let get_n_bytes n = String.init n (fun _ -> get_char "get_n_bytes") in let get_char () = get_char "get_char" in (*let get_byte () = get_byte "get_byte" in*) let msg = match typ with | 'R' -> let t = get_int32 () in (match t with | 0l -> AuthenticationOk | 2l -> AuthenticationKerberosV5 | 3l -> AuthenticationCleartextPassword | 4l -> let salt = String.init 2 (fun _ -> get_char ()) in AuthenticationCryptPassword salt | 5l -> let salt = String.init 4 (fun _ -> get_char ()) in AuthenticationMD5Password salt | 6l -> AuthenticationSCMCredential | _ -> UnknownMessage (typ, msg) ); | 'E' -> let strs = ref [] in let rec loop () = let field_type = get_char () in if field_type = '\000' then List.rev !strs (* end of list *) else ( strs := (field_type, get_string ()) :: !strs; loop () ) in ErrorResponse (loop ()) | 'N' -> let strs = ref [] in let rec loop () = let field_type = get_char () in if field_type = '\000' then List.rev !strs (* end of list *) else ( strs := (field_type, get_string ()) :: !strs; loop () ) in NoticeResponse (loop ()) | 'Z' -> let c = get_char () in ReadyForQuery c | 'K' -> let pid = get_int32 () in let key = get_int32 () in BackendKeyData (pid, key) | 'S' -> let param = get_string () in let value = get_string () in ParameterStatus (param, value) | '1' -> ParseComplete | '2' -> BindComplete | '3' -> CloseComplete | 'C' -> let str = get_string () in CommandComplete str | 'D' -> let nr_fields = get_int16 () in let fields = ref [] in for i = 0 to nr_fields-1 do let len = get_int32 () in let field = if len < 0l then (-1, "") else ( if len >= 0x4000_0000l then raise (Error "PGOCaml: result field is too long"); let len = Int32.to_int len in if len > Sys.max_string_length then raise (Error "PGOCaml: result field is too wide for string"); let bytes = get_n_bytes len in len, bytes ) in fields := field :: !fields done; DataRow (List.rev !fields) | 'I' -> EmptyQueryResponse | 'n' -> NoData | 'T' -> let nr_fields = get_int16 () in let fields = ref [] in for i = 0 to nr_fields-1 do let name = get_string () in let table = get_int32 () in let column = get_int16 () in let oid = get_int32 () in let length = get_int16 () in let modifier = get_int32 () in let format = get_int16 () in fields := (name, table, column, oid, length, modifier, format) :: !fields done; RowDescription (List.rev !fields) | 't' -> let nr_fields = get_int16 () in let fields = ref [] in for i = 0 to nr_fields - 1 do let oid = get_int32 () in fields := oid :: !fields done; ParameterDescription (List.rev !fields) | _ -> UnknownMessage (typ, msg) in if debug_protocol then eprintf "< %s\n%!" (string_of_msg_t msg); msg let verbose = ref 1 (* Print an ErrorResponse on stderr. *) let print_ErrorResponse fields = if !verbose >= 1 then ( try let severity = List.assoc 'S' fields in let code = List.assoc 'C' fields in let message = List.assoc 'M' fields in if !verbose = 1 then match severity with | "ERROR" | "FATAL" | "PANIC" -> eprintf "%s: %s: %s\n%!" severity code message | _ -> () else eprintf "%s: %s: %s\n%!" severity code message with Not_found -> eprintf "WARNING: 'Always present' field is missing in error message\n%!" ); if !verbose >= 2 then ( List.iter ( fun (field_type, field) -> if field_type <> 'S' && field_type <> 'C' && field_type <> 'M' then eprintf "%c: %s\n%!" field_type field ) fields ) (* Handle an ErrorResponse anywhere, by printing and raising an exception. *) let pg_error ?conn fields = print_ErrorResponse fields; let str = try let severity = List.assoc 'S' fields in let code = List.assoc 'C' fields in let message = List.assoc 'M' fields in sprintf "%s: %s: %s" severity code message with Not_found -> "WARNING: 'Always present' field is missing in error message" in (* If conn parameter was given, then resynch - read messages until we * see ReadyForQuery. *) (match conn with | None -> return () | Some conn -> let rec loop () = receive_message conn >>= fun msg -> let msg = parse_backend_message msg in match msg with ReadyForQuery _ -> return () | _ -> loop () in loop () ) >>= fun () -> fail (PostgreSQL_Error (str, fields)) (*----- Profiling. -----*) type 'a retexn = Ret of 'a | Exn of exn (* profile_op : * string -> string -> string list -> (unit -> 'a Thread.t) -> 'a Thread.t *) let profile_op uuid op detail f = let chan = try let filename = Sys.getenv "PGPROFILING" in let flags = [ Open_wronly; Open_append; Open_creat ] in let chan = open_out_gen flags 0o644 filename in Some chan with | Not_found | Sys_error _ -> None in match chan with | None -> f () (* No profiling - just run it. *) | Some chan -> (* Profiling. *) let start_time = Unix.gettimeofday () in catch (fun () -> f () >>= fun x -> return (Ret x)) (fun exn -> return (Exn exn)) >>= fun ret -> let end_time = Unix.gettimeofday () in let elapsed_time_ms = int_of_float (1000. *. (end_time -. start_time)) in let row = [ "1"; (* Version number. *) uuid; op; string_of_int elapsed_time_ms; match ret with | Ret _ -> "ok" | Exn exn -> Printexc.to_string exn ] @ detail in (* Lock the output channel while we write the row, to prevent * corruption from multiple writers. *) let fd = Unix.descr_of_out_channel chan in Unix.lockf fd Unix.F_LOCK 0; Csv.save_out chan [row]; close_out chan; (* Return result or re-raise the exception. *) match ret with | Ret r -> return r | Exn exn -> fail exn (*----- Connection. -----*) let connect ?host ?port ?user ?password ?database ?(unix_domain_socket_dir = PGOCaml_config.default_unix_domain_socket_dir) () = (* Get the username. *) let user = match user with | Some user -> user | None -> try Sys.getenv "PGUSER" with Not_found -> try let pw = Unix.getpwuid (Unix.geteuid ()) in pw.Unix.pw_name with Not_found -> "postgres" in (* Get the password. *) let password = match password with | Some password -> password | None -> try Sys.getenv "PGPASSWORD" with Not_found -> "" in (* Get the database name. *) let database = match database with | Some database -> database | None -> try Sys.getenv "PGDATABASE" with Not_found -> user in (* Hostname and port number. *) let host = match host with | Some _ -> host | None -> try Some (Sys.getenv "PGHOST") with Not_found -> None in (* use Unix domain socket. *) let port = match port with | Some port -> port | None -> try int_of_string (Sys.getenv "PGPORT") with Not_found | Failure "int_of_string" -> 5432 in (* Make the socket address. *) let sockaddr = match host with | Some hostname -> (try let hostent = Unix.gethostbyname hostname in let domain = hostent.Unix.h_addrtype in match domain with | Unix.PF_INET | Unix.PF_INET6 -> (* Choose a random address from the list. *) let addrs = hostent.Unix.h_addr_list in let len = Array.length addrs in if len <= 0 then raise (Error ("PGOCaml: unknown host: " ^ hostname)); let i = Random.int len in let addr = addrs.(i) in Unix.ADDR_INET (addr, port) | Unix.PF_UNIX -> (* Would we trust a pathname returned through DNS? *) raise (Error "PGOCaml: DNS returned PF_UNIX record") with Not_found -> raise (Error ("PGOCaml: unknown host: " ^ hostname)) ); | None -> (* Unix domain socket. *) let sockaddr = sprintf "%s/.s.PGSQL.%d" unix_domain_socket_dir port in Unix.ADDR_UNIX sockaddr in (* Create a universally unique identifier for this connection. This * is mainly for debugging and profiling. *) let uuid = (* * On Windows, the result of Unix.getpid is largely meaningless (it's not unique) * and, more importantly, Unix.getppid is not implemented. *) let ppid = try Unix.getppid () with Invalid_argument "Unix.getppid not implemented" -> 0 in sprintf "%s %d %d %g %s %g" (Unix.gethostname ()) (Unix.getpid ()) ppid (Unix.gettimeofday ()) Sys.executable_name ((Unix.times ()).Unix.tms_utime) in let uuid = Digest.to_hex (Digest.string uuid) in let do_connect () = open_connection sockaddr >>= fun (ichan, chan) -> (* Create the connection structure. *) let conn = { ichan = ichan; chan = chan; private_data = None; uuid = uuid } in (* Send the StartUpMessage. NB. At present we do not support SSL. *) let msg = new_start_message () in add_int32 msg 196608l; add_string msg "user"; add_string msg user; add_string msg "database"; add_string msg database; add_byte msg 0; (* Loop around here until the database gives a ReadyForQuery message. *) let rec loop msg = (match msg with | Some msg -> send_recv conn msg | None -> receive_message conn) >>= fun msg -> let msg = parse_backend_message msg in match msg with | ReadyForQuery _ -> return () (* Finished connecting! *) | BackendKeyData _ -> (* XXX We should save this key. *) loop None | ParameterStatus _ -> (* Should we do something with this? *) loop None | AuthenticationOk -> loop None | AuthenticationKerberosV5 -> fail (Error "PGOCaml: Kerberos authentication not supported") | AuthenticationCleartextPassword -> let msg = new_message 'p' in (* PasswordMessage *) add_string msg password; loop (Some msg) | AuthenticationCryptPassword salt -> (* Crypt password not supported because there is no crypt(3) function * in OCaml. *) fail (Error "PGOCaml: crypt password authentication not supported") | AuthenticationMD5Password salt -> (* (* This is a guess at how the salt is used ... *) let password = salt ^ password in let password = Digest.string password in*) let password = "md5" ^ Digest.to_hex (Digest.string (Digest.to_hex (Digest.string (password ^ user)) ^ salt)) in let msg = new_message 'p' in (* PasswordMessage *) add_string msg password; loop (Some msg) | AuthenticationSCMCredential -> fail (Error "PGOCaml: SCM Credential authentication not supported") | ErrorResponse err -> pg_error ~conn err | NoticeResponse err -> (* XXX Do or print something here? *) loop None | _ -> (* Silently ignore unknown or unexpected message types. *) loop None in loop (Some msg) >>= fun () -> return conn in let detail = [ "user"; user; "database"; database; "host"; Option.default "unix" host; "port"; string_of_int port; "prog"; Sys.executable_name ] in profile_op uuid "connect" detail do_connect let close conn = let do_close () = (* Be nice and send the terminate message. *) let msg = new_message 'X' in send_message conn msg >>= fun () -> flush conn.chan >>= fun () -> (* Closes the underlying socket too. *) close_in conn.ichan in profile_op conn.uuid "close" [] do_close let set_private_data conn data = conn.private_data <- Some data let private_data { private_data = private_data } = match private_data with | None -> raise Not_found | Some private_data -> private_data let uuid conn = conn.uuid type pa_pg_data = (string, bool) Hashtbl.t let ping conn = let do_ping () = let msg = new_message 'S' in send_message conn msg >>= fun () -> (* Wait for ReadyForQuery. *) let rec loop () = receive_message conn >>= fun msg -> let msg = parse_backend_message msg in match msg with | ReadyForQuery _ -> return () (* Finished! *) | ErrorResponse err -> pg_error ~conn err (* Error *) | _ -> loop () in loop () in profile_op conn.uuid "ping" [] do_ping let alive conn = catch (fun () -> ping conn >>= fun () -> return true) (fun _ -> return false) type oid = int32 type param = string option type result = string option type row = result list let flush_msg conn = let msg = new_message 'H' in send_message conn msg >>= fun () -> (* Might as well actually flush the channel too, otherwise what is the * point of executing this command? *) flush conn.chan let prepare conn ~query ?(name = "") ?(types = []) () = let do_prepare () = let msg = new_message 'P' in add_string msg name; add_string msg query; add_int16 msg (List.length types); List.iter (add_int32 msg) types; send_message conn msg >>= fun () -> flush_msg conn >>= fun () -> let rec loop () = receive_message conn >>= fun msg -> let msg = parse_backend_message msg in match msg with | ErrorResponse err -> pg_error err | ParseComplete -> return () (* Finished! *) | NoticeResponse _ -> (* XXX Do or print something here? *) loop () | _ -> fail (Error ("PGOCaml: unknown response from parse: " ^ string_of_msg_t msg)) in loop () in let details = [ "query"; query; "name"; name ] in profile_op conn.uuid "prepare" details do_prepare let iter_execute conn name portal params proc () = (* Bind *) let msg = new_message 'B' in add_string msg portal; add_string msg name; add_int16 msg 0; (* Send all parameters as text. *) add_int16 msg (List.length params); List.iter ( fun param -> match param with | None -> add_int32 msg 0xffff_ffffl (* NULL *) | Some str -> add_int32 msg (Int32.of_int (String.length str)); add_string_no_trailing_nil msg str ) params; add_int16 msg 0; (* Send back all results as text. *) send_message conn msg >>= fun () -> (* Execute *) let msg = new_message 'E' in add_string msg portal; add_int32 msg 0l; (* no limit on rows *) send_message conn msg >>= fun () -> (* Sync *) let msg = new_message 'S' in send_message conn msg >>= fun () -> (* Process the message(s) received from the database until we read * ReadyForQuery. In the process we may get some rows back from * the database, no data, or an error. *) let rec loop () = (* NB: receive_message flushes the output connection. *) receive_message conn >>= fun msg -> let msg = parse_backend_message msg in match msg with | ReadyForQuery _ -> return () (* Finished! *) | ErrorResponse err -> pg_error ~conn err (* Error *) | NoticeResponse err -> (* XXX Do or print something here? *) loop () | BindComplete -> loop () | CommandComplete _ -> loop () | EmptyQueryResponse -> loop () | DataRow fields -> let fields = List.map ( function | (i, _) when i < 0 -> None (* NULL *) | (0, _) -> Some "" | (i, bytes) -> Some bytes ) fields in proc fields >>= loop | NoData -> loop () | ParameterStatus _ -> (* 43.2.6: ParameterStatus messages will be generated whenever * the active value changes for any of the parameters the backend * believes the frontend should know about. Most commonly this * occurs in response to a SET SQL command executed by the * frontend, and this case is effectively synchronous -- but it * is also possible for parameter status changes to occur because * the administrator changed a configuration file and then sent * the SIGHUP signal to the postmaster. *) loop () | _ -> fail (Error ("PGOCaml: unknown response message: " ^ string_of_msg_t msg)) in loop () let do_execute conn name portal params rev () = let rows = ref [] in iter_execute conn name portal params (fun fields -> return (rows := fields :: !rows)) () >>= fun () -> (* Return the result rows. *) return (if rev then List.rev !rows else !rows) let execute_rev conn ?(name = "") ?(portal = "") ~params () = let do_execute = do_execute conn name portal params false in let details = [ "name"; name; "portal"; portal ] in profile_op conn.uuid "execute" details do_execute let execute conn ?(name = "") ?(portal = "") ~params () = let do_execute = do_execute conn name portal params true in let details = [ "name"; name; "portal"; portal ] in profile_op conn.uuid "execute" details do_execute let cursor conn ?(name = "") ?(portal = "") ~params proc = let do_execute = iter_execute conn name portal params proc in let details = [ "name"; name; "portal"; portal ] in profile_op conn.uuid "cursor" details do_execute let begin_work ?isolation ?access ?deferrable conn = let isolation_str = match isolation with | None -> "" | Some x -> " isolation level " ^ (match x with | `Serializable -> "serializable" | `Repeatable_read -> "repeatable read" | `Read_committed -> "read committed" | `Read_uncommitted -> "read uncommitted") and access_str = match access with | None -> "" | Some x -> match x with | `Read_write -> " read write" | `Read_only -> " read only" and deferrable_str = match deferrable with | None -> "" | Some x -> (match x with true -> "" | false -> " not") ^ " deferrable" in let query = "begin work" ^ isolation_str ^ access_str ^ deferrable_str in prepare conn ~query () >>= fun () -> execute conn ~params:[] () >>= fun _ -> return () let commit conn = let query = "commit" in prepare conn ~query () >>= fun () -> execute conn ~params:[] () >>= fun _ -> return () let rollback conn = let query = "rollback" in prepare conn ~query () >>= fun () -> execute conn ~params:[] () >>= fun _ -> return () let transact conn ?isolation ?access ?deferrable f = begin_work ?isolation ?access ?deferrable conn >>= fun () -> catch (fun () -> f conn >>= fun r -> commit conn >>= fun () -> return r ) (fun e -> rollback conn >>= fun () -> fail e ) let serial conn name = let query = "select currval ($1)" in prepare conn ~query () >>= fun () -> execute conn ~params:[Some name] () >>= fun rows -> let row = List.hd rows in let result = List.hd row in (* NB. According to the manual, the return type of currval is * always a bigint, whether or not the column is serial or bigserial. *) return (Int64.of_string (Option.get result)) let serial4 conn name = serial conn name >>= fun s -> return (Int64.to_int32 s) let serial8 = serial let close_statement conn ?(name = "") () = let msg = new_message 'C' in add_char msg 'S'; add_string msg name; send_message conn msg >>= fun () -> flush_msg conn >>= fun () -> let rec loop () = receive_message conn >>= fun msg -> let msg = parse_backend_message msg in match msg with | ErrorResponse err -> pg_error err | CloseComplete -> return () (* Finished! *) | NoticeResponse _ -> (* XXX Do or print something here? *) loop () | _ -> fail (Error ("PGOCaml: unknown response from close: " ^ string_of_msg_t msg)) in loop () let close_portal conn ?(portal = "") () = let msg = new_message 'C' in add_char msg 'P'; add_string msg portal; send_message conn msg >>= fun () -> flush_msg conn >>= fun () -> let rec loop () = receive_message conn >>= fun msg -> let msg = parse_backend_message msg in match msg with | ErrorResponse err -> pg_error err | CloseComplete -> return () | NoticeResponse _ -> (* XXX Do or print something here? *) loop () | _ -> fail (Error ("PGOCaml: unknown response from close: " ^ string_of_msg_t msg)) in loop () let inject db ?name query = prepare db ~query ?name () >>= fun () -> execute db ?name ~params:[] () >>= fun ret -> close_statement db ?name () >>= fun () -> return ret let alter db ?name query = inject db ?name query >>= fun _ -> return () type row_description = result_description list and result_description = { name : string; table : oid option; column : int option; field_type : oid; length : int; modifier : int32; } type params_description = param_description list and param_description = { param_type : oid; } let describe_statement conn ?(name = "") () = let msg = new_message 'D' in add_char msg 'S'; add_string msg name; send_message conn msg >>= fun () -> flush_msg conn >>= fun () -> receive_message conn >>= fun msg -> let msg = parse_backend_message msg in ( match msg with | ErrorResponse err -> pg_error err | ParameterDescription params -> let params = List.map ( fun oid -> { param_type = oid } ) params in return params | _ -> fail (Error ("PGOCaml: unknown response from describe: " ^ string_of_msg_t msg))) >>= fun params -> receive_message conn >>= fun msg -> let msg = parse_backend_message msg in match msg with | ErrorResponse err -> pg_error err | NoData -> return (params, None) | RowDescription fields -> let fields = List.map ( fun (name, table, column, oid, length, modifier, _) -> { name = name; table = if table = 0l then None else Some table; column = if column = 0 then None else Some column; field_type = oid; length = length; modifier = modifier; } ) fields in return (params, Some fields) | _ -> fail (Error ("PGOCaml: unknown response from describe: " ^ string_of_msg_t msg)) let describe_portal conn ?(portal = "") () = let msg = new_message 'D' in add_char msg 'P'; add_string msg portal; send_message conn msg >>= fun () -> flush_msg conn >>= fun () -> receive_message conn >>= fun msg -> let msg = parse_backend_message msg in match msg with | ErrorResponse err -> pg_error err | NoData -> return None | RowDescription fields -> let fields = List.map ( fun (name, table, column, oid, length, modifier, _) -> { name = name; table = if table = 0l then None else Some table; column = if column = 0 then None else Some column; field_type = oid; length = length; modifier = modifier; } ) fields in return (Some fields) | _ -> fail (Error ("PGOCaml: unknown response from describe: " ^ string_of_msg_t msg)) (*----- Type conversion. -----*) (* For certain types, more information is available by looking * at the modifier field as well as just the OID. For example, * for NUMERIC the modifier tells us the precision. * However we don't always have the modifier field available - * in particular for parameters. *) let name_of_type ?modifier = function | 16_l -> "bool" (* BOOLEAN *) | 17_l -> "bytea" (* BYTEA *) | 20_l -> "int64" (* INT8 *) | 21_l -> "int16" (* INT2 *) | 23_l -> "int32" (* INT4 *) | 25_l -> "string" (* TEXT *) | 600_l -> "point" (* POINT *) | 700_l | 701_l -> "float" (* FLOAT4, FLOAT8 *) | 869_l -> "inet" (* INET *) | 1000_l -> "bool_array" (* BOOLEAN[] *) | 1007_l -> "int32_array" (* INT4[] *) | 1009_l -> "string_array" (* TEXT[] *) | 1016_l -> "int64_array" (* INT8[] *) | 1021_l | 1022_l -> "float_array" (* FLOAT4[], FLOAT8[] *) | 1042_l -> "string" (* CHAR(n) - treat as string *) | 1043_l -> "string" (* VARCHAR(n) - treat as string *) | 1082_l -> "date" (* DATE *) | 1083_l -> "time" (* TIME *) | 1114_l -> "timestamp" (* TIMESTAMP *) | 1184_l -> "timestamptz" (* TIMESTAMP WITH TIME ZONE *) | 1186_l -> "interval" (* INTERVAL *) | 2278_l -> "unit" (* VOID *) | 1700_l -> "string" (* NUMERIC *) | i -> (* For unknown types, look at . *) raise (Error ("PGOCaml: unknown type for OID " ^ Int32.to_string i)) type inet = Unix.inet_addr * int type timestamptz = Calendar.t * Time_Zone.t type int16 = int type bytea = string type point = float * float type hstore = (string * string option) list type numeric = string type bool_array = bool option list type int32_array = int32 option list type int64_array = int64 option list type string_array = string option list type float_array = float option list let string_of_hstore hstore = let string_of_quoted str = "\"" ^ str ^ "\"" in let string_of_mapping (key, value) = let key_str = string_of_quoted key and value_str = match value with | Some v -> string_of_quoted v | None -> "NULL" in key_str ^ "=>" ^ value_str in String.join ", " (List.map string_of_mapping hstore) let string_of_numeric (x : string) = x let string_of_inet (addr, mask) = let hostmask = if Unix.domain_of_sockaddr (Unix.ADDR_INET(addr, 1)) = Unix.PF_INET6 then 128 else 32 in let addr = Unix.string_of_inet_addr addr in if mask = hostmask then addr else if mask >= 0 && mask < hostmask then addr ^ "/" ^ string_of_int mask else failwith "string_of_inet" let string_of_oid = Int32.to_string let string_of_bool = function | true -> "t" | false -> "f" let string_of_int = Pervasives.string_of_int let string_of_int16 = Pervasives.string_of_int let string_of_int32 = Int32.to_string let string_of_int64 = Int64.to_string let string_of_float = string_of_float let string_of_point (x, y) = "(" ^ (string_of_float x) ^ "," ^ (string_of_float y) ^ ")" let string_of_timestamp = Printer.Calendar.to_string let string_of_timestamptz (cal, tz) = Printer.Calendar.to_string cal ^ match tz with | Time_Zone.UTC -> "+00" | Time_Zone.Local -> let gap = Time_Zone.gap Time_Zone.UTC Time_Zone.Local in if gap >= 0 then sprintf "+%02d" gap else sprintf "-%02d" (-gap) | Time_Zone.UTC_Plus gap -> if gap >= 0 then sprintf "+%02d" gap else sprintf "-%02d" (-gap) let string_of_date = Printer.Date.to_string let string_of_time = Printer.Time.to_string let string_of_interval p = let y, m, d, s = Calendar.Period.ymds p in sprintf "%d years %d mons %d days %d seconds" y m d s let string_of_unit () = "" (* NB. It is the responsibility of the caller of this function to * properly escape array elements. *) let string_of_any_array xs = let buf = Buffer.create 128 in Buffer.add_char buf '{'; let adder i x = if i > 0 then Buffer.add_char buf ','; match x with | Some x -> Buffer.add_char buf '"'; Buffer.add_string buf x; Buffer.add_char buf '"' | None -> Buffer.add_string buf "NULL" in List.iteri adder xs; Buffer.add_char buf '}'; Buffer.contents buf let option_map f = function | Some x -> Some (f x) | None -> None let escape_string str = let buf = Buffer.create 128 in for i = 0 to String.length str - 1 do match str.[i] with | '"' | '\\' as x -> Buffer.add_char buf '\\'; Buffer.add_char buf x | x -> Buffer.add_char buf x done; Buffer.contents buf let string_of_bool_array a = string_of_any_array (List.map (option_map string_of_bool) a) let string_of_int32_array a = string_of_any_array (List.map (option_map Int32.to_string) a) let string_of_int64_array a = string_of_any_array (List.map (option_map Int64.to_string) a) let string_of_string_array a = string_of_any_array (List.map (option_map escape_string) a) let string_of_float_array a = string_of_any_array (List.map (option_map string_of_float) a) let string_of_bytea b = let len = String.length b in let buf = Buffer.create (len * 2) in for i = 0 to len - 1 do let c = b.[i] in let cc = Char.code c in if cc < 0x20 || cc > 0x7e then Buffer.add_string buf (sprintf "\\%03o" cc) (* non-print -> \ooo *) else if c = '\\' then Buffer.add_string buf "\\\\" (* \ -> \\ *) else Buffer.add_char buf c done; Buffer.contents buf let string_of_string (x : string) = x let oid_of_string = Int32.of_string let bool_of_string = function | "true" | "t" -> true | "false" | "f" -> false | str -> raise (Error ("PGOCaml: not a boolean: " ^ str)) let int_of_string = Pervasives.int_of_string let int16_of_string = Pervasives.int_of_string let int32_of_string = Int32.of_string let int64_of_string = Int64.of_string let float_of_string = float_of_string let hstore_of_string str = let expect target stream = if List.exists (fun c -> c <> Stream.next stream) target then raise (Error ("PGOCaml: unexpected input in hstore_of_string")) in let parse_quoted stream = let rec loop accum stream = match Stream.next stream with | '"' -> String.implode (List.rev accum) | '\\' -> loop (Stream.next stream :: accum) stream | x -> loop (x :: accum) stream in expect ['"'] stream; loop [] stream in let parse_value stream = match Stream.peek stream with | Some 'N' -> (expect ['N'; 'U'; 'L'; 'L'] stream; None) | _ -> Some (parse_quoted stream) in let parse_mapping stream = let key = parse_quoted stream in expect ['='; '>'] stream; let value = parse_value stream in (key, value) in let parse_main stream = let rec loop accum stream = let mapping = parse_mapping stream in match Stream.peek stream with | Some _ -> (expect [','; ' '] stream; loop (mapping :: accum) stream) | None -> mapping :: accum in match Stream.peek stream with | Some _ -> loop [] stream | None -> [] in parse_main (Stream.of_string str) let numeric_of_string (x : string) = x let inet_of_string = let rex = Pcre.regexp "([^:./]*([:.])[^/]+)(?:/(.+))?" in fun str -> let res = Pcre.extract ~rex ~full_match:false str in let addr = Unix.inet_addr_of_string res.(0) and mask = res.(2) in if mask = "" then (addr, (if res.(1) = "." then 32 else 128)) else (addr, int_of_string mask) let point_of_string = let float_pat = "[+-]?[0-9]+.*[0-9]*|[Nn]a[Nn]|[+-]?[Ii]nfinity" in let point_pat = "\\([ \t]*(" ^ float_pat ^ ")[ \t]*,[ \t]*(" ^ float_pat ^ ")[ \t]*\\)" in let rex = Pcre.regexp point_pat in fun str -> try let res = Pcre.extract ~rex ~full_match:false str in ((float_of_string res.(0)), (float_of_string res.(1))) with | _ -> failwith "point_of_string" let date_of_string = Printer.Date.from_string let time_of_string str = (* Remove trailing ".microsecs" if present. *) let n = String.length str in let str = if n > 8 && str.[8] = '.' then String.sub str 0 8 else str in Printer.Time.from_string str let timestamp_of_string str = (* Remove trailing ".microsecs" if present. *) let n = String.length str in let str = if n > 19 && str.[19] = '.' then String.sub str 0 19 else str in Printer.Calendar.from_string str let timestamptz_of_string str = (* Split into datetime+timestamp. *) let n = String.length str in let cal, tz = if n >= 3 && (str.[n-3] = '+' || str.[n-3] = '-') then String.sub str 0 (n-3), Some (String.sub str (n-3) 3) else str, None in let cal = timestamp_of_string cal in let tz = match tz with | None -> Time_Zone.Local (* best guess? *) | Some tz -> let sgn = match tz.[0] with '+' -> 1 | '-' -> 0 | _ -> assert false in let mag = int_of_string (String.sub tz 1 2) in Time_Zone.UTC_Plus (sgn * mag) in cal, tz let re_interval = Pcre.regexp ~flags:[`EXTENDED] ("(?:(\\d+)\\syears?)? # years\n"^ "\\s* # \n"^ "(?:(\\d+)\\smons?)? # months\n"^ "\\s* # \n"^ "(?:(\\d+)\\sdays?)? # days\n"^ "\\s* # \n"^ "(?:(\\d\\d):(\\d\\d) # HH:MM\n"^ " (?::(\\d\\d))? # optional :SS\n"^ ")?") let interval_of_string = let int_opt s = if s = "" then 0 else int_of_string s in fun str -> try let sub = Pcre.extract ~rex:re_interval str in Calendar.Period.make (int_opt sub.(1)) (* year *) (int_opt sub.(2)) (* month *) (int_opt sub.(3)) (* day *) (int_opt sub.(4)) (* hour *) (int_opt sub.(5)) (* min *) (int_opt sub.(6)) (* sec *) with Not_found -> failwith ("interval_of_string: bad interval: " ^ str) let unit_of_string _ = () (* NB. This function also takes care of unescaping returned elements. *) let any_array_of_string str = let n = String.length str in assert (str.[0] = '{'); assert (str.[n-1] = '}'); let str = String.sub str 1 (n-2) in let buf = Buffer.create 128 in let add_field accum = let x = Buffer.contents buf in Buffer.clear buf; let field = if x = "NULL" then None else let n = String.length x in if n >= 2 && x.[0] = '"' then Some (String.sub x 1 (n-2)) else Some x in field :: accum in let loop (accum, quoted, escaped) = function | '\\' when not escaped -> (accum, quoted, true) | '"' when not escaped -> Buffer.add_char buf '"'; (accum, not quoted, false) | ',' when not escaped && not quoted -> (add_field accum, false, false) | x -> Buffer.add_char buf x; (accum, quoted, false) in let (accum, _, _) = String.fold_left loop ([], false, false) str in let accum = if Buffer.length buf = 0 then accum else add_field accum in List.rev accum let bool_array_of_string str = List.map (option_map bool_of_string) (any_array_of_string str) let int32_array_of_string str = List.map (option_map Int32.of_string) (any_array_of_string str) let int64_array_of_string str = List.map (option_map Int64.of_string) (any_array_of_string str) let string_array_of_string str = any_array_of_string str let float_array_of_string str = List.map (option_map float_of_string) (any_array_of_string str) let is_first_oct_digit c = c >= '0' && c <= '3' let is_oct_digit c = c >= '0' && c <= '7' let oct_val c = Char.code c - 0x30 let is_hex_digit = function '0'..'9' | 'a'..'f' | 'A'..'F' -> true | _ -> false let hex_val c = let offset = match c with | '0'..'9' -> 0x30 | 'a'..'f' -> 0x57 | 'A'..'F' -> 0x37 | _ -> failwith "hex_val" in Char.code c - offset (* Deserialiser for the new 'hex' format introduced in PostgreSQL 9.0. *) let bytea_of_string_hex str = let len = String.length str in let buf = Buffer.create ((len-2)/2) in let i = ref 3 in while !i < len do let hi_nibble = str.[!i-1] in let lo_nibble = str.[!i] in i := !i+2; if is_hex_digit hi_nibble && is_hex_digit lo_nibble then begin let byte = ((hex_val hi_nibble) lsl 4) + (hex_val lo_nibble) in Buffer.add_char buf (Char.chr byte) end done; Buffer.contents buf (* Deserialiser for the old 'escape' format used in PostgreSQL < 9.0. *) let bytea_of_string_escape str = let len = String.length str in let buf = Buffer.create len in let i = ref 0 in while !i < len do let c = str.[!i] in if c = '\\' then ( incr i; if !i < len && str.[!i] = '\\' then ( Buffer.add_char buf '\\'; incr i ) else if !i+2 < len && is_first_oct_digit str.[!i] && is_oct_digit str.[!i+1] && is_oct_digit str.[!i+2] then ( let byte = oct_val str.[!i] in incr i; let byte = (byte lsl 3) + oct_val str.[!i] in incr i; let byte = (byte lsl 3) + oct_val str.[!i] in incr i; Buffer.add_char buf (Char.chr byte) ) ) else ( incr i; Buffer.add_char buf c ) done; Buffer.contents buf (* PostgreSQL 9.0 introduced the new 'hex' format for binary data. We must therefore check whether the data begins with a magic sequence that identifies this new format and if so call the appropriate parser; if it doesn't, then we invoke the parser for the old 'escape' format. *) let bytea_of_string str = if String.starts_with str "\\x" then bytea_of_string_hex str else bytea_of_string_escape str let bind = (>>=) let return = Thread.return end pgocaml-2.2/src/PGOCaml_generic.mli000066400000000000000000000277571251121767700172420ustar00rootroot00000000000000(* PG'OCaml is a set of OCaml bindings for the PostgreSQL database. * * PG'OCaml - type safe interface to PostgreSQL. * Copyright (C) 2005-2009 Richard Jones and other authors. * * 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 COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) (** Type-safe access to PostgreSQL databases. *) open CalendarLib module type THREAD = sig type 'a t val return : 'a -> 'a t val (>>=) : 'a t -> ('a -> 'b t) -> 'b t val fail : exn -> 'a t val catch : (unit -> 'a t) -> (exn -> 'a t) -> 'a t type in_channel type out_channel val open_connection : Unix.sockaddr -> (in_channel * out_channel) t val output_char : out_channel -> char -> unit t val output_binary_int : out_channel -> int -> unit t val output_string : out_channel -> string -> unit t val flush : out_channel -> unit t val input_char : in_channel -> char t val input_binary_int : in_channel -> int t val really_input : in_channel -> Bytes.t -> int -> int -> unit t val close_in : in_channel -> unit t end module type PGOCAML_GENERIC = sig type 'a t (** Database handle. *) type 'a monad type isolation = [ `Serializable | `Repeatable_read | `Read_committed | `Read_uncommitted ] type access = [ `Read_write | `Read_only ] exception Error of string (** For library errors. *) exception PostgreSQL_Error of string * (char * string) list (** For errors generated by the PostgreSQL database back-end. The * first argument is a printable error message. The second argument * is the complete set of error fields returned from the back-end. * See [http://www.postgresql.org/docs/8.1/static/protocol-error-fields.html] *) (** {6 Connection management} *) val connect : ?host:string -> ?port:int -> ?user:string -> ?password:string -> ?database:string -> ?unix_domain_socket_dir:string -> unit -> 'a t monad (** Connect to the database. The normal [$PGDATABASE], etc. environment * variables are available. *) val close : 'a t -> unit monad (** Close the database handle. You must call this after you have * finished with the handle, or else you will get leaked file * descriptors. *) val ping : 'a t -> unit monad (** Ping the database. If the database is not available, some sort of * exception will be thrown. *) val alive : 'a t -> bool monad (** This function is a wrapper of [ping] that returns a boolean instead of * raising an exception. *) (** {6 Transactions} *) val begin_work : ?isolation:isolation -> ?access:access -> ?deferrable:bool -> 'a t -> unit monad (** Start a transaction. *) val commit : 'a t -> unit monad (** Perform a COMMIT operation on the database. *) val rollback : 'a t -> unit monad (** Perform a ROLLBACK operation on the database. *) val transact : 'a t -> ?isolation:isolation -> ?access:access -> ?deferrable:bool -> ('a t -> 'b monad) -> 'b monad (** [transact db ?isolation ?access ?deferrable f] wraps your * function [f] inside a transactional block. * First it calls [begin_work] with [isolation], [access] and [deferrable], * then calls [f] and do [rollback] if [f] raises * an exception, [commit] otherwise. *) (** {6 Serial column} *) val serial : 'a t -> string -> int64 monad (** This is a shorthand for [SELECT CURRVAL(serial)]. For a table * called [table] with serial column [id] you would typically * call this as [serial dbh "table_id_seq"] after the previous INSERT * operation to get the serial number of the inserted row. *) val serial4 : 'a t -> string -> int32 monad (** As {!serial} but assumes that the column is a SERIAL or * SERIAL4 type. *) val serial8 : 'a t -> string -> int64 monad (** Same as {!serial}. *) (** {6 Miscellaneous} *) val max_message_length : int ref (** Maximum message length accepted from the back-end. The default * is [Sys.max_string_length], which means that we will try to read as * much data from the back-end as we can, and this may cause us to * run out of memory (particularly on 64 bit machines), causing a * possible denial of service. You may want to set this to a smaller * size to avoid this happening. *) val verbose : int ref (** Verbosity. 0 means don't print anything. 1 means print short * error messages as returned from the back-end. 2 means print all * messages as returned from the back-end. Messages are printed on [stderr]. * Default verbosity level is 1. *) val set_private_data : 'a t -> 'a -> unit (** Attach some private data to the database handle. * * NB. The pa_pgsql camlp4 extension uses this for its own purposes, which * means that in most programs you will not be able to attach private data * to the database handle. *) val private_data : 'a t -> 'a (** Retrieve some private data previously attached to the database handle. * If no data has been attached, raises [Not_found]. * * NB. The pa_pgsql camlp4 extension uses this for its own purposes, which * means that in most programs you will not be able to attach private data * to the database handle. *) val uuid : 'a t -> string (** Retrieve the unique identifier for this connection. *) type pa_pg_data = (string, bool) Hashtbl.t (** When using pa_pgsql, database handles have type * [PGOCaml.pa_pg_data PGOCaml.t] *) (** {6 Low level query interface - DO NOT USE DIRECTLY} *) type oid = int32 type param = string option (** None is NULL. *) type result = string option (** None is NULL. *) type row = result list (** One row is a list of fields. *) val prepare : 'a t -> query:string -> ?name:string -> ?types:oid list -> unit -> unit monad (** [prepare conn ~query ?name ?types ()] prepares the statement [query] * and optionally names it [name] and sets the parameter types to [types]. * If no name is given, then the "unnamed" statement is overwritten. If * no types are given, then the PostgreSQL engine infers types. * Synchronously checks for errors. *) val execute_rev : 'a t -> ?name:string -> ?portal:string -> params:param list -> unit -> row list monad val execute : 'a t -> ?name:string -> ?portal:string -> params:param list -> unit -> row list monad (** [execute conn ?name ~params ()] executes the named or unnamed * statement [name], with the given parameters [params], * returning the result rows (if any). * * There are several steps involved at the protocol layer: * (1) a "portal" is created from the statement, binding the * parameters in the statement (Bind). * (2) the portal is executed (Execute). * (3) we synchronise the connection (Sync). * * The optional [?portal] parameter may be used to name the portal * created in step (1) above (otherwise the unnamed portal is used). * This is only important if you want to call {!describe_portal} * to find out the result types. *) val cursor : 'a t -> ?name:string -> ?portal:string -> params:param list -> (row -> unit monad) -> unit monad val close_statement : 'a t -> ?name:string -> unit -> unit monad (** [close_statement conn ?name ()] closes a prepared statement and frees * up any resources. *) val close_portal : 'a t -> ?portal:string -> unit -> unit monad (** [close_portal conn ?portal ()] closes a portal and frees up any resources. *) val inject : 'a t -> ?name:string -> string -> row list monad (** [inject conn ?name query] executes the statement [query] * and optionally names it [name] and gives the result. *) val alter : 'a t -> ?name:string -> string -> unit monad (** [alter conn ?name query] executes the statement [query] * and optionally names it [name]. Same as inject but ignoring the result. *) type row_description = result_description list and result_description = { name : string; (** Field name. *) table : oid option; (** OID of table. *) column : int option; (** Column number of field in table. *) field_type : oid; (** The type of the field. *) length : int; (** Length of the field. *) modifier : int32; (** Type modifier. *) } type params_description = param_description list and param_description = { param_type : oid; (** The type of the parameter. *) } val describe_statement : 'a t -> ?name:string -> unit -> (params_description * row_description option) monad (** [describe_statement conn ?name ()] describes the named or unnamed * statement's parameter types and result types. *) val describe_portal : 'a t -> ?portal:string -> unit -> row_description option monad (** [describe_portal conn ?portal ()] describes the named or unnamed * portal's result types. *) (** {6 Low level type conversion functions - DO NOT USE DIRECTLY} *) val name_of_type : ?modifier:int32 -> oid -> string (** Returns the OCaml equivalent type name to the PostgreSQL type [oid]. * For instance, [name_of_type (Int32.of_int 23)] returns ["int32"] because * the OID for PostgreSQL's internal [int4] type is [23]. As another * example, [name_of_type (Int32.of_int 25)] returns ["string"]. *) type inet = Unix.inet_addr * int type timestamptz = Calendar.t * Time_Zone.t type int16 = int type bytea = string (* XXX *) type point = float * float type hstore = (string * string option) list type numeric = string type bool_array = bool option list type int32_array = int32 option list type int64_array = int64 option list type string_array = string option list type float_array = float option list (** The following conversion functions are used by pa_pgsql to convert * values in and out of the database. *) val string_of_oid : oid -> string val string_of_bool : bool -> string val string_of_int : int -> string val string_of_int16 : int16 -> string val string_of_int32 : int32 -> string val string_of_int64 : int64 -> string val string_of_float : float -> string val string_of_point : point -> string val string_of_hstore : hstore -> string val string_of_numeric : numeric -> string val string_of_inet : inet -> string val string_of_timestamp : Calendar.t -> string val string_of_timestamptz : timestamptz -> string val string_of_date : Date.t -> string val string_of_time : Time.t -> string val string_of_interval : Calendar.Period.t -> string val string_of_bytea : bytea -> string val string_of_string : string -> string val string_of_unit : unit -> string val string_of_bool_array : bool_array -> string val string_of_int32_array : int32_array -> string val string_of_int64_array : int64_array -> string val string_of_string_array : string_array -> string val string_of_float_array : float_array -> string val oid_of_string : string -> oid val bool_of_string : string -> bool val int_of_string : string -> int val int16_of_string : string -> int16 val int32_of_string : string -> int32 val int64_of_string : string -> int64 val float_of_string : string -> float val point_of_string : string -> point val hstore_of_string : string -> hstore val numeric_of_string : string -> numeric val inet_of_string : string -> inet val timestamp_of_string : string -> Calendar.t val timestamptz_of_string : string -> timestamptz val date_of_string : string -> Date.t val time_of_string : string -> Time.t val interval_of_string : string -> Calendar.Period.t val bytea_of_string : string -> bytea val unit_of_string : string -> unit val bool_array_of_string : string -> bool_array val int32_array_of_string : string -> int32_array val int64_array_of_string : string -> int64_array val string_array_of_string : string -> string_array val float_array_of_string : string -> float_array val bind : 'a monad -> ('a -> 'b monad) -> 'b monad val return : 'a -> 'a monad end module Make : functor (Thread : THREAD) -> PGOCAML_GENERIC with type 'a monad = 'a Thread.t pgocaml-2.2/src/pa_pgsql.ml000066400000000000000000000360671251121767700157530ustar00rootroot00000000000000(* PG'OCaml - type safe interface to PostgreSQL. * Copyright (C) 2005-2009 Richard Jones and other authors. * * 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 COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Camlp4.PreCast open PGOCaml_aux open Printf let nullable_name = "nullable" let unravel_name = "unravel" let hstore_name = "hstore" (* We need a database connection while compiling. If people use the * override flags like "database=foo", then we may connect to several * databases at once. Keep track of that here. Note that in the normal * case we have just one database handle, opened to the default * database (as controlled by environment variables such as $PGHOST, * $PGDATABASE, etc.) *) type key = { (* None in any of these fields means use the default. *) host : string option; port : int option; user : string option; password : string option; database : string option; unix_domain_socket_dir : string option; } let connections : (key, unit PGOCaml.t) Hashtbl.t = Hashtbl.create 13 let get_connection key = try Hashtbl.find connections key with Not_found -> (* Create a new connection. *) let host = key.host in let port = key.port in let user = key.user in let password = key.password in let database = key.database in let unix_domain_socket_dir = key.unix_domain_socket_dir in let dbh = PGOCaml.connect ?host ?port ?user ?password ?database ?unix_domain_socket_dir () in (* Prepare the nullable test - see result conversions below. *) let nullable_query = "select attnotnull from pg_attribute where attrelid = $1 and attnum = $2" in PGOCaml.prepare dbh ~query:nullable_query ~name:nullable_name (); (* Prepare the unravel test. *) let unravel_query = "select typtype, typbasetype from pg_type where oid = $1" in PGOCaml.prepare dbh ~query:unravel_query ~name:unravel_name (); (* Prepare the hstore test. *) let hstore_query = "select typname from pg_type where oid = $1" in PGOCaml.prepare dbh ~query:hstore_query ~name:hstore_name (); Hashtbl.add connections key dbh; dbh (* Wrapper around [PGOCaml.name_of_type]. *) let name_of_type_wrapper ?modifier dbh oid = try PGOCaml.name_of_type ?modifier oid with PGOCaml.Error msg as exc -> let params = [ Some (PGOCaml.string_of_oid oid) ] in let rows = PGOCaml.execute dbh ~name:hstore_name ~params () in match rows with | [ [ Some "hstore" ] ] -> "hstore" | _ -> raise exc (* By using CREATE DOMAIN, the user may define types which are essentially aliases * for existing types. If the original type is not recognised by PG'OCaml, this * functions recurses through the pg_type table to see if it happens to be an alias * for a type which we do know how to handle. *) let unravel_type dbh orig_type = let rec unravel_type_aux ft = try name_of_type_wrapper dbh ft with PGOCaml.Error msg as exc -> let params = [ Some (PGOCaml.string_of_oid ft) ] in let rows = PGOCaml.execute dbh ~name:unravel_name ~params () in match rows with | [ [ Some typtype ; Some typbasetype ] ] when typtype = "d" -> unravel_type_aux (PGOCaml.oid_of_string typbasetype) | _ -> raise exc in unravel_type_aux orig_type (* Return the list of numbers a <= i < b. *) let rec range a b = if a < b then a :: range (a+1) b else [];; let rex = Pcre.regexp "\\$(@?)(\\??)([_a-z][_a-zA-Z0-9']*)" let pgsql_expand ?(flags = []) _loc dbh query = (* Parse the flags. *) let f_execute = ref false in let f_nullable_results = ref false in let key = ref { host = None; port = None; user = None; password = None; database = None; unix_domain_socket_dir = None } in List.iter ( function | "execute" -> f_execute := true | "nullable-results" -> f_nullable_results := true | str when String.starts_with str "host=" -> let host = String.sub str 5 (String.length str - 5) in key := { !key with host = Some host } | str when String.starts_with str "port=" -> let port = int_of_string (String.sub str 5 (String.length str - 5)) in key := { !key with port = Some port } | str when String.starts_with str "user=" -> let user = String.sub str 5 (String.length str - 5) in key := { !key with user = Some user } | str when String.starts_with str "password=" -> let password = String.sub str 9 (String.length str - 9) in key := { !key with password = Some password } | str when String.starts_with str "database=" -> let database = String.sub str 9 (String.length str - 9) in key := { !key with database = Some database } | str when String.starts_with str "unix_domain_socket_dir=" -> let socket = String.sub str 23 (String.length str - 23) in key := { !key with unix_domain_socket_dir = Some socket } | str -> Loc.raise _loc ( Failure ("Unknown flag: " ^ str) ) ) flags; let f_execute = !f_execute in let f_nullable_results = !f_nullable_results in let key = !key in (* Connect, if necessary, to the database. *) let my_dbh = get_connection key in (* Split the query into text and variable name parts using Pcre.full_split. * eg. "select id from employees where name = $name and salary > $salary" * would become a structure equivalent to: * ["select id from employees where name = "; "$name"; " and salary > "; * "$salary"]. * Actually it's a wee bit more complicated than that ... *) let split = Pcre.full_split ~rex query in let split = let rec loop = function | [] -> [] | Pcre.Text text :: rest -> `Text text :: loop rest | Pcre.Delim _ :: Pcre.Group (1, list) :: Pcre.Group (2, option) :: Pcre.Group (3, _varname) :: rest -> let list = match list with | "" -> false | "@" -> true | _ -> assert false in let option = match option with | "" -> false | "?" -> true | _ -> assert false in `Var (_varname, list, option) :: loop rest | _ -> Loc.raise _loc ( Failure "Pcre.full_split: unexpected value returned" ) in loop split in (* Go to the database, prepare this statement, and find out exactly * what the parameter types and return values are. Exceptions can * be raised here if the statement is bad SQL. *) let (params, results), varmap = (* Rebuild the query with $n placeholders for each variable. *) let next = let i = ref 0 in fun () -> incr i; !i in let varmap = Hashtbl.create 7 in let query = String.concat "" ( List.map ( function | `Text text -> text | `Var (_varname, false, option) -> let i = next () in Hashtbl.add varmap i (_varname, false, option); sprintf "$%d" i | `Var (_varname, true, option) -> let i = next () in Hashtbl.add varmap i (_varname, true, option); sprintf "($%d)" i ) split ) in let varmap = Hashtbl.fold ( fun i var vars -> (i, var) :: vars ) varmap [] in try PGOCaml.prepare my_dbh ~query (); PGOCaml.describe_statement my_dbh (), varmap with exn -> Loc.raise _loc exn in (* If the PGSQL(dbh) "execute" flag was used, we will actually * execute the statement now. Normally this would never be used, but * some statements need to be executed, particularly CREATE TEMPORARY * TABLE. *) if f_execute then ignore (PGOCaml.execute my_dbh ~params:[] ()); (* Number of params should match length of map, otherwise something * has gone wrong in the substitution above. *) if List.length varmap <> List.length params then Loc.raise _loc ( Failure ("Mismatch in number of parameters found by database. " ^ "Most likely your statement contains bare $, $number, etc.") ); (* Generate a function for converting the parameters. * * See also: * http://archives.postgresql.org/pgsql-interfaces/2006-01/msg00043.php *) let params = List.fold_right (fun (i, { PGOCaml.param_type = param_type }) tail -> let _varname, list, option = List.assoc i varmap in let fn = "string_of_" ^ (unravel_type my_dbh param_type) in let head = match list, option with | false, false -> <:expr< [ Some (PGOCaml.$lid:fn$ $lid:_varname$) ] >> | false, true -> <:expr< [ PGOCaml_aux.Option.map PGOCaml.$lid:fn$ $lid:_varname$ ] >> | true, false -> <:expr< List.map (fun x -> Some (PGOCaml.$lid:fn$ x)) $lid:_varname$ >> | true, true -> <:expr< List.map (fun x -> PGOCaml_aux.Option.map PGOCaml.$lid:fn$ x) $lid:_varname$ >> in <:expr< [ $head$ :: $tail$ ] >> ) (List.combine (range 1 (1 + List.length varmap)) params) <:expr< [] >> in (* Substitute expression. *) let expr = let split = List.fold_right ( fun s tail -> let head = match s with | `Text text -> <:expr< `Text $str:text$ >> | `Var (_varname, list, option) -> let list = if list then <:expr< True >> else <:expr< False >> in let option = if option then <:expr< True >> else <:expr< False >> in <:expr< `Var $str:_varname$ $list$ $option$ >> in <:expr< [ $head$ :: $tail$ ] >> ) split <:expr< [] >> in <:expr< (* let original_query = $str:query$ in * original query string *) let dbh = $dbh$ in let params = $params$ in (* type: string option list list *) let split = $split$ in (* split up query *) (* Rebuild the query with appropriate placeholders. A single list * param can expand into several placeholders. *) let i = ref 0 in (* Counts parameters. *) let j = ref 0 in (* Counts placeholders. *) let query = String.concat "" ( List.map ( fun [ `Text text -> text | `Var _varname False _ -> (* non-list item *) let () = incr i in (* next parameter *) let () = incr j in (* next placeholder number *) "$" ^ string_of_int j.contents | `Var _varname True _ -> (* list item *) let param = List.nth params i.contents in let () = incr i in (* next parameter *) "(" ^ String.concat "," ( List.map ( fun _ -> let () = incr j in (* next placeholder number *) "$" ^ string_of_int j.contents ) param ) ^ ")" ] ) split) in (* Flatten the parameters to a simple list now. *) let params = List.flatten params in (* Get a unique name for this query using an MD5 digest. *) let name = "pa_pgsql." ^ Digest.to_hex (Digest.string query) in (* Get the hash table used to keep track of prepared statements. *) let hash = try PGOCaml.private_data dbh with [ Not_found -> let hash = Hashtbl.create 17 in do { PGOCaml.set_private_data dbh hash; hash } ] in (* Have we prepared this statement already? If not, do so. *) let is_prepared = Hashtbl.mem hash name in PGOCaml.bind (if not is_prepared then PGOCaml.bind (PGOCaml.prepare dbh ~name ~query ()) (fun () -> do { Hashtbl.add hash name True; PGOCaml.return () }) else PGOCaml.return ()) (fun () -> (* Execute the statement, returning the rows. *) PGOCaml.execute_rev dbh ~name ~params ()) >> in (* If we're expecting any result rows, then generate a function to * convert them. Otherwise return unit. Note that we can only * determine the nullability of results if they correspond to real * columns in a table, otherwise the type will always be 'type option'. *) match results with | Some results -> let list = List.fold_right (fun i tail -> <:patt< [ $lid:"c"^string_of_int i$ :: $tail$ ] >> ) (range 0 (List.length results)) <:patt< [] >> in let conversions = List.mapi ( fun i result -> let field_type = result.PGOCaml.field_type in let modifier = result.PGOCaml.modifier in let fn = name_of_type_wrapper ~modifier my_dbh field_type in let fn = fn ^ "_of_string" in let nullable = f_nullable_results || match (result.PGOCaml.table, result.PGOCaml.column) with | Some table, Some column -> (* Find out whether the column is nullable from the * database pg_attribute table. *) let params = [ Some (PGOCaml.string_of_oid table); Some (PGOCaml.string_of_int column) ] in let _rows = PGOCaml.execute my_dbh ~name:nullable_name ~params () in let not_nullable = match _rows with | [ [ Some b ] ] -> PGOCaml.bool_of_string b | _ -> false in not not_nullable | _ -> true (* Assume it could be nullable. *) in let col = <:expr< $lid:"c" ^ string_of_int i$ >> in if nullable then <:expr< PGOCaml_aux.Option.map PGOCaml.$lid:fn$ $col$ >> else <:expr< PGOCaml.$lid:fn$ (try PGOCaml_aux.Option.get $col$ with _ -> failwith "pa_pgsql's nullability heuristic has failed - use \"nullable-results\"") >> ) results in let convert = (* Avoid generating a single-element tuple. *) match conversions with | [] -> <:expr< () >> | [a] -> <:expr< $a$ >> | conversion :: conversions -> (*<:expr< ( $list:conversions$ ) >> in * http://caml.inria.fr/pub/ml-archives/caml-list/2007/05/ab49714d974451f669cf46455627466d.en.html *) <:expr< ( $conversion$, $Ast.exCom_of_list conversions$ ) >> in <:expr< PGOCaml.bind $expr$ (fun _rows -> PGOCaml.return (let original_query = $str:query$ in List.rev_map ( fun row -> match row with [ $list$ -> $convert$ | _ -> (* This should never happen, even if the schema changes. * Well, maybe if the user does 'SELECT *'. *) let msg = "pa_pgsql: internal error: " ^ "Incorrect number of columns returned from query: " ^ original_query ^ ". Columns are: " ^ String.concat "; " ( List.map ( fun [ Some str -> Printf.sprintf "%S" str | None -> "NULL" ] ) row ) in raise (PGOCaml.Error msg) ] ) _rows)) >> | None -> <:expr< PGOCaml.bind $expr$ (fun _rows -> PGOCaml.return ()) >> open Syntax EXTEND Gram expr: LEVEL "top" [ [ "PGSQL"; "("; dbh = expr; ")"; extras = LIST1 [ x = STRING -> x ] -> let query, flags = match List.rev extras with | [] -> assert false | query :: flags -> query, flags in pgsql_expand ~flags _loc dbh (Camlp4.Struct.Token.Eval.string query) ] ]; END pgocaml-2.2/src/pa_pgsql.mldylib000066400000000000000000000001351251121767700167620ustar00rootroot00000000000000# OASIS_START # DO NOT EDIT (digest: 7768dab9f11db9b2b947747fb6516f8b) Pa_pgsql # OASIS_STOP pgocaml-2.2/src/pa_pgsql.mllib000066400000000000000000000001351251121767700164250ustar00rootroot00000000000000# OASIS_START # DO NOT EDIT (digest: 7768dab9f11db9b2b947747fb6516f8b) Pa_pgsql # OASIS_STOP pgocaml-2.2/src/pgocaml.mldylib000066400000000000000000000002071251121767700165760ustar00rootroot00000000000000# OASIS_START # DO NOT EDIT (digest: da5a8c724b284262dc76d857dc1e81b6) PGOCaml_aux PGOCaml PGOCaml_generic PGOCaml_config # OASIS_STOP pgocaml-2.2/src/pgocaml.mllib000066400000000000000000000002071251121767700162410ustar00rootroot00000000000000# OASIS_START # DO NOT EDIT (digest: da5a8c724b284262dc76d857dc1e81b6) PGOCaml_aux PGOCaml PGOCaml_generic PGOCaml_config # OASIS_STOP pgocaml-2.2/tests/000077500000000000000000000000001251121767700141525ustar00rootroot00000000000000pgocaml-2.2/tests/test_pgocaml_highlevel.ml000066400000000000000000000037171251121767700212240ustar00rootroot00000000000000(* Example program using typesafe calls to PostgreSQL. * * PG'OCaml - type safe interface to PostgreSQL. * Copyright (C) 2005-2009 Richard Jones and other authors. * * 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 COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Printf let () = let dbh = PGOCaml.connect () in let () = PGSQL(dbh) "execute" "create temporary table employees ( id serial not null primary key, name text not null, salary int4 not null, email text )" in let insert name salary email = PGSQL(dbh) "insert into employees (name, salary, email) values ($name, $salary, $?email)" in insert "Ann" 10_000_l None; insert "Bob" 45_000_l None; insert "Jim" 20_000_l None; insert "Mary" 30_000_l (Some "mary@example.com"); let rows = PGSQL(dbh) "select id, name, salary, email from employees" in List.iter begin fun (id, name, salary, email) -> let email = match email with Some email -> email | None -> "-" in printf "%ld %S %ld %S\n" id name salary email end rows; let ids = [ 1_l; 3_l ] in let rows = PGSQL(dbh) "select id, name, salary, email from employees where id in $@ids" in List.iter begin fun (id, name, salary, email) -> let email = match email with Some email -> email | None -> "-" in printf "%ld %S %ld %S\n" id name salary email end rows; PGOCaml.close dbh pgocaml-2.2/tests/test_pgocaml_lowlevel.ml000066400000000000000000000076651251121767700211140ustar00rootroot00000000000000(* Test the lowlevel interface to the database. * Assumes that $PGHOST, etc. are set to point to a database. * * PG'OCaml - type safe interface to PostgreSQL. * Copyright (C) 2005-2009 Richard Jones and other authors. * * 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 COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Printf let print_row i row = printf "row %d: [%s]\n" i (String.concat "; " (List.map (function | None -> "NULL" | Some str -> sprintf "%S" str) row)) let print_rows rows = List.iteri print_row rows let print_params_description params = printf "params:\n"; List.iteri ( fun i param -> printf " parameter %d:\n" i; printf " type: %ld\n" param.PGOCaml.param_type ) params let print_row_description results = match results with | None -> printf "this statement returns no data\n" | Some results -> printf "results:\n"; List.iteri ( fun i result -> printf " field %d:\n" i; printf " name: %s\n" result.PGOCaml.name; printf " type: %ld\n" result.PGOCaml.field_type ) results let () = let dbh = PGOCaml.connect () in (* Simple query with no parameters. *) let query = "select current_timestamp" in let name = "timestamp_query" in ignore (PGOCaml.prepare dbh ~query ~name ()); let i = ref 0 in PGOCaml.cursor dbh ~name ~params:[] (fun row -> incr i; print_row !i row); (* Describe the statement. *) let params, results = PGOCaml.describe_statement dbh ~name () in print_params_description params; print_row_description results; (* A query with parameters. *) let query = "select $1 + $2" in let types = [ 23l; 23l ] in (* 23 = int4 *) let name = "sum_query" in ignore (PGOCaml.prepare dbh ~query ~name ~types ()); let i = ref 0 in PGOCaml.cursor dbh ~name ~params:[Some "1"; Some "2"] (fun row -> incr i; print_row !i row); (* Describe the statement. *) let params, results = PGOCaml.describe_statement dbh ~name () in print_params_description params; print_row_description results; (* Create a temporary table and populate it. *) let query = "create temporary table employees ( id serial not null primary key, name text not null, salary numeric(8,2) not null, email text )" in ignore (PGOCaml.prepare dbh ~query ()); ignore (PGOCaml.execute dbh ~params:[] ()); let query = "insert into employees (name, salary, email) values ($1, $2, $3)" in ignore (PGOCaml.prepare dbh ~query ()); let params, results = PGOCaml.describe_statement dbh () in print_params_description params; print_row_description results; ignore (PGOCaml.execute dbh ~params:[Some "Ann"; Some "10000.00"; None] ()); ignore (PGOCaml.execute dbh ~params:[Some "Bob"; Some "45000.00"; None] ()); ignore (PGOCaml.execute dbh ~params:[Some "Jim"; Some "20000.00"; None] ()); ignore (PGOCaml.execute dbh ~params:[Some "Mary"; Some "30000.00"; None] ()); let query = "select * from employees where salary > $1 order by id" in ignore (PGOCaml.prepare dbh ~query ()); let params, results = PGOCaml.describe_statement dbh () in print_params_description params; print_row_description results; let rows = PGOCaml.execute dbh ~params:[Some "0"] () in print_rows rows; PGOCaml.close dbh pgocaml-2.2/utils/000077500000000000000000000000001251121767700141505ustar00rootroot00000000000000pgocaml-2.2/utils/pgocaml_prof.ml000066400000000000000000000257051251121767700171630ustar00rootroot00000000000000(* A tool to analyse profiling traces generated by $PGPROFILING=filename. * See README.profiling for more information. * * PG'OCaml - type safe interface to PostgreSQL. * Copyright (C) 2005-2009 Richard Jones and other authors. * * 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 COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open PGOCaml_aux open Printf let (//) = Filename.concat (* Don't keep the rows in memory, instead divide them by connection * and write into a temporary directory. This allows us to handle * very large profiles. *) let tmpdir = Filename.temp_file "pgocamlprof" ".d" let nr_rows = ref 0 let () = (* Filename.temp_file actually creates the file - delete it. *) (try Unix.unlink tmpdir with _ -> ()); Unix.mkdir tmpdir 0o755; (* Little bit of caching helps in the common case where adjacent * rows belong to the same connection. *) let get_chan, close_chan = let last_conn = ref None in let last_chan = ref None in let close_chan () = match !last_chan with | None -> () | Some chan -> close_out chan; last_chan := None; last_conn := None in let get_chan conn = match !last_conn with | Some conn' when conn = conn' -> Option.get !last_chan | _ -> close_chan (); let open_flags = [ Open_wronly; Open_append; Open_creat ] in let filename = tmpdir // conn in let chan = open_out_gen open_flags 0o644 filename in last_chan := Some chan; last_conn := Some conn; chan in get_chan, close_chan in let f = function | ("1" as version) :: conn :: rest -> let chan = get_chan conn in incr nr_rows; Csv.save_out chan [version :: rest]; | _ -> () (* just ignore versions we don't understand *) in let chan = open_in Sys.argv.(1) in Csv.load_rows f chan; close_in chan; (* Close the cached out_channel. *) close_chan () (* Accumulate results by query and by connection. * (Implicitly assume that queries can be treated independently.) *) type query_data = { query : string; (* Query (prepared, w/placeholders). *) qprogs : string list; (* Programs which used this. *) nr_preps : int; (* Number of times prepared. *) prep_time : int; (* Total prep time (ms). *) nr_execs : int; (* Number of times executed. *) exec_time : int; (* Total exec time (ms). *) nr_qfailures : int; (* Number of failures (prep+exec). *) } let queries = Hashtbl.create 31 let set_query query update = let data = try Hashtbl.find queries query with Not_found -> { query = query; qprogs = []; nr_preps = 0; prep_time = 0; nr_execs = 0; exec_time = 0; nr_qfailures = 0 } in let data = update data in Hashtbl.replace queries query data type connection_data = { params : connection_params; (* Connection parameters. *) progs : string list; (* Programs which used this. *) nr_connects : int; (* Number of connects. *) connect_time : int; (* Total connect time (ms). *) nr_closes : int; (* Number of explicit closes. *) close_time : int; (* Total close time (ms). *) nr_pings : int; (* Number of pings. *) ping_time : int; (* Total ping time (ms). *) nr_failures : int; (* Number of failures. *) } and connection_params = { user : string; database : string; host : string; port : int; } let connections = Hashtbl.create 31 let set_connection params update = let data = try Hashtbl.find connections params with Not_found -> { params = params; progs = []; nr_connects = 0; connect_time = 0; nr_closes = 0; close_time = 0; nr_pings = 0; ping_time = 0; nr_failures = 0; } in let data = update data in Hashtbl.replace connections params data exception Ignore of string let files = Array.to_list (Sys.readdir tmpdir) let nr_rows' = ref 0 let () = List.iter ( fun uuid -> let rows = Csv.load (tmpdir // uuid) in nr_rows' := !nr_rows' + (List.length rows); let ignore msg = raise (Ignore msg) in try assert (rows <> []); let rec assoc i = function | x :: y :: xs when x = i -> y | _ :: _ :: xs -> assoc i xs | [] -> ignore (sprintf "key %s not found" i) | [_] -> ignore "odd number of elements in association list" in (* NB. We expect the rows to begin with a "connect" operation, * then have a series of prepare/executes, and possibly finish * with a "close". *) let params, prog = match rows with | ("1" :: "connect" :: time :: status :: details) :: _ -> { user = assoc "user" details; database = assoc "database" details; host = assoc "host" details; port = int_of_string (assoc "port" details) }, assoc "prog" details | _ -> ignore (sprintf "connection %s did not start with a 'connect' operation" uuid) in set_connection params (fun data -> { data with progs = prog :: List.filter ((<>) prog) data.progs }); (* qnames maps prepared query names to query. *) let qnames = Hashtbl.create 13 in List.iter ( function | "1" :: "connect" :: time :: status :: details -> let time = int_of_string time in let failures = if status = "ok" then 0 else 1 in set_connection params (fun data -> { data with nr_connects = data.nr_connects + 1; connect_time = data.connect_time + time; nr_failures = data.nr_failures + failures; }) | "1" :: "prepare" :: time :: status :: details -> let time = int_of_string time in let failures = if status = "ok" then 0 else 1 in let query = assoc "query" details in let name = assoc "name" details in (* Put it in qnames so we can look it up in execute below. *) Hashtbl.replace qnames name query; set_query query (fun data -> { data with qprogs = prog :: List.filter ((<>) prog) data.qprogs; nr_preps = data.nr_preps + 1; prep_time = data.prep_time + time; nr_qfailures = data.nr_qfailures + failures; }) | "1" :: "execute" :: time :: status :: details -> let time = int_of_string time in let failures = if status = "ok" then 0 else 1 in let name = assoc "name" details in let query = try Hashtbl.find qnames name with Not_found -> ignore (sprintf "execute on unprepared query name '%s'" name) in set_query query (fun data -> { data with nr_execs = data.nr_execs + 1; exec_time = data.exec_time + time; nr_qfailures = data.nr_qfailures + failures; }) | "1" :: "close" :: time :: status :: _ -> let time = int_of_string time in let failures = if status = "ok" then 0 else 1 in set_connection params (fun data -> { data with nr_closes = data.nr_closes + 1; close_time = data.close_time + time; nr_failures = data.nr_failures + failures; }) | "1" :: "ping" :: time :: status :: _ -> let time = int_of_string time in let failures = if status = "ok" then 0 else 1 in set_connection params (fun data -> { data with nr_pings = data.nr_pings + 1; ping_time = data.ping_time + time; nr_failures = data.nr_failures + failures; }) | _ -> ignore "invalid row" ) rows with Ignore msg -> eprintf "warning: %s\n" msg ) files (* Clean up temporary directory. *) let () = List.iter ( fun filename -> Unix.unlink (tmpdir // filename) ) files; Unix.rmdir tmpdir (* Sanity check - did we read back the same number of rows that * we wrote? *) let () = assert (!nr_rows = !nr_rows') (* More manageable as lists. *) let queries = Hashtbl.fold (fun query data xs -> (query, data) :: xs) queries [] let connections = Hashtbl.fold (fun params data xs -> (params, data) :: xs) connections [] (* Sort them so that the ones with the most cumulative time are first. *) let queries = let f (_, { prep_time = prep_time1; exec_time = exec_time1 }) (_, { prep_time = prep_time2; exec_time = exec_time2 }) = compare (prep_time2 + exec_time2) (prep_time1 + exec_time1) in List.sort f queries let connections = let f (_, { connect_time = connect_time1; close_time = close_time1; ping_time = ping_time1 }) (_, { connect_time = connect_time2; close_time = close_time2; ping_time = ping_time2 }) = compare (connect_time2 + close_time2 + ping_time2) (connect_time1 + close_time1 + ping_time1) in List.sort f connections (* Print out the results of the analysis. *) let () = printf "---------------------------------------- QUERIES ---------\n\n"; List.iter ( fun (query, data) -> printf "Query:\n%s\n\n" query; printf "Total time: %d ms\n" (data.prep_time + data.exec_time); printf " Prepare: %d ms\n" data.prep_time; printf " Calls: %d\n" data.nr_preps; if data.nr_preps > 0 then printf " Avg time/prep: %d ms\n" (data.prep_time / data.nr_preps); printf " Execute: %d ms\n" data.exec_time; printf " Calls: %d\n" data.nr_execs; if data.nr_execs > 0 then printf " Avg time/exec: %d ms\n" (data.exec_time / data.nr_execs); printf " Failures: %d\n" data.nr_qfailures; printf "Called from: %s\n" (String.concat ", " data.qprogs); printf "\n\n"; ) queries; printf "---------------------------------------- CONNECTIONS -----\n\n"; List.iter ( fun (params, data) -> printf "Connection:\n"; printf " user = %s\n" params.user; printf " database = %s\n" params.database; printf " host = %s\n" params.host; printf " port = %d\n" params.port; printf "\n"; printf "Total time: %d ms\n" (data.connect_time + data.close_time + data.ping_time); printf " Connect: %d ms\n" data.connect_time; printf " Calls: %d\n" data.nr_connects; if data.nr_connects > 0 then printf " Avg time/conn: %d ms\n" (data.connect_time / data.nr_connects); printf " Close: %d ms\n" data.close_time; printf " Calls: %d\n" data.nr_closes; if data.nr_closes > 0 then printf " Avg time/close: %d ms\n" (data.close_time / data.nr_closes); printf " Ping: %d ms\n" data.ping_time; printf " Calls: %d\n" data.nr_pings; if data.nr_pings > 0 then printf " Avg time/ping: %d ms\n" (data.ping_time / data.nr_pings); printf "Called from: %s\n" (String.concat ", " data.progs); printf "\n\n"; ) connections