@@ -868,6 +868,51 @@ impl PostgresConnection {
868868 self . prepare ( query) . and_then ( |stmt| stmt. execute ( params) )
869869 }
870870
871+ /// Execute a sequence of SQL statements.
872+ ///
873+ /// Statements should be separated by `;` characters. If an error occurs,
874+ /// execution of the sequence will stop at that point. This is intended for
875+ /// execution of batches of non-dynamic statements - for example, creation
876+ /// of a schema for a fresh database.
877+ ///
878+ /// # Warning
879+ ///
880+ /// Prepared statements should be used for any SQL statement which contains
881+ /// user-specified data, as it provides functionality to safely embed that
882+ /// data in the statment. Do not form statements via string concatenation
883+ /// and feed them into this method.
884+ ///
885+ /// # Example
886+ ///
887+ /// ```rust
888+ /// # use postgres::{PostgresConnection, PostgresResult};
889+ ///
890+ /// fn init_db(conn: &PostgresConnection) -> PostgresResult<()> {
891+ /// static INIT_DB: &'static str = "
892+ /// CREATE TABLE person (
893+ /// id SERIAL PRIMARY KEY,
894+ /// name NOT NULL
895+ /// );
896+ ///
897+ /// CREATE TABLE purchase (
898+ /// id SERIAL PRIMARY KEY,
899+ /// person INT NOT NULL REFERENCES person (id),
900+ /// time TIMESTAMPTZ NOT NULL,
901+ /// );
902+ ///
903+ /// CREATE INDEX ON purchase (time);
904+ /// ";
905+ /// conn.batch_execute(INIT_DB)
906+ /// }
907+ /// ```
908+ pub fn batch_execute ( & self , query : & str ) -> PostgresResult < ( ) > {
909+ let mut conn = self . conn . borrow_mut ( ) ;
910+ if conn. trans_depth != 0 {
911+ return Err ( PgWrongTransaction ) ;
912+ }
913+ conn. quick_query ( query) . map ( |_| ( ) )
914+ }
915+
871916 /// Returns information used to cancel pending queries.
872917 ///
873918 /// Used with the `cancel_query` function. The object returned can be used
@@ -976,6 +1021,14 @@ impl<'conn> PostgresTransaction<'conn> {
9761021 self . prepare ( query) . and_then ( |s| s. execute ( params) )
9771022 }
9781023
1024+ /// Like `PostgresConnection::batch_execute`.
1025+ pub fn batch_execute ( & self , query : & str ) -> PostgresResult < ( ) > {
1026+ if self . conn . conn . borrow ( ) . trans_depth != self . depth {
1027+ return Err ( PgWrongTransaction ) ;
1028+ }
1029+ self . conn . batch_execute ( query)
1030+ }
1031+
9791032 /// Like `PostgresConnection::transaction`.
9801033 pub fn transaction < ' a > ( & ' a self )
9811034 -> PostgresResult < PostgresTransaction < ' a > > {
0 commit comments