Permalink
Browse files

first commit to Stored Procedure to Method

  • Loading branch information...
0 parents commit 2aefba4e705b5850d85c3e86c6f75fd1d0115751 @iannsp iannsp committed Aug 5, 2011
No changes.
@@ -0,0 +1 @@
+Package PEAR2_DB_Pl2Method API release notes for version 0.1.0.
@@ -0,0 +1,2 @@
+;; A PHP programmer
+Ivo Nascimento [ivonascimento] <ivo@o8o.com.br> (lead)
24 README
@@ -0,0 +1,24 @@
+This package was port to PEAR2 to add the benefits of good distribution
+and great enviroment.
+
+This package make stored procedures from PostgreSQL and others SGDB
+can used as PHP Methods.
+You have PDOStatement as answer like in this sample:
+
+ -- in database dev
+ create or replace function teste() returns varchar(10) as
+ $$
+ begin
+ return 'teste';
+ end;
+ $$
+ language 'plpgsql';
+
+$a = new PDO("pgsql:host=localhost;port=5432;dbname=dev;user=postgres;password=root");
+$pl2method = \PEAR2\DB\Pl2Method\Pl2Method::getInstance($a);
+
+here you have a PDOStatement
+$returned = $pl2method->public->teste();
+var_dump( $returned );
+ and You can FetchAll()
+var_dump($returned->FetchAll());
@@ -0,0 +1,3 @@
+Package PEAR2_DB_Pl2Method release notes for version 0.1.0.
+Support to just Psql PDO Driver
+without type validation yet
@@ -0,0 +1,33 @@
+<?php
+include_once '../src/DB/Pl2method.php';
+include_once '../src/DB/Pl2Method/SGDBDriver.php';
+include_once '../src/DB/Pl2Method/SGDBInterface.php';
+include_once '../src/DB/Pl2Method/Driver/Pgsql.php';
+error_reporting(E_ALL);
+/*
+ * For this examples I use a Database Called artigo
+ * and create these Stored Procedure
+ create or replace function teste() returns varchar(10) as
+ $$
+ begin
+ return 'teste';
+ end;
+ $$
+ language 'plpgsql';
+
+ select * from public.teste();
+ *
+ */
+
+$a = new PDO("pgsql:host=localhost;port=5432;dbname=artigo;user=postgres;password=root");
+$pl2method = \PEAR2\DB\Pl2Method\Pl2Method::getInstance($a);
+// here you have a PDOStatement
+$returned = $pl2method->public->teste();
+var_dump( $returned );
+// You can FetchAll()
+var_dump($returned->FetchAll());
+
+
+
+
+?>
@@ -0,0 +1,25 @@
+<?php
+/**
+ * This file is used to provide extra files/packages outside package.xml
+ * More information: http://pear.php.net/manual/en/pyrus.commands.package.php#pyrus.commands.package.extrasetup
+ */
+$extrafiles = array();
+
+/**
+ * for example:
+if (basename(__DIR__) == 'trunk') {
+ $extrafiles = array(
+ new \PEAR2\Pyrus\Package(__DIR__ . '/../../HTTP_Request/trunk/package.xml'),
+ new \PEAR2\Pyrus\Package(__DIR__ . '/../../sandbox/Console_CommandLine/trunk/package.xml'),
+ new \PEAR2\Pyrus\Package(__DIR__ . '/../../MultiErrors/trunk/package.xml'),
+ new \PEAR2\Pyrus\Package(__DIR__ . '/../../Exception/trunk/package.xml'),
+ );
+} else {
+ $extrafiles = array(
+ new \PEAR2\Pyrus\Package(__DIR__ . '/../HTTP_Request/package.xml'),
+ new \PEAR2\Pyrus\Package(__DIR__ . '/../sandbox/Console_CommandLine/package.xml'),
+ new \PEAR2\Pyrus\Package(__DIR__ . '/../MultiErrors/package.xml'),
+ new \PEAR2\Pyrus\Package(__DIR__ . '/../Exception/package.xml'),
+ );
+}
+*/
@@ -0,0 +1,51 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package version="2.1" xmlns="http://pear.php.net/dtd/package-2.1" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.1 http://pear.php.net/dtd/package-2.1.xsd">
+ <name>PEAR2_DB_Pl2Method</name>
+ <channel>pear2.php.net</channel>
+ <summary>Package PEAR2_DB_Pl2Method summary.
+</summary>
+ <description>
+Package detailed description here (found in README)</description>
+ <lead>
+ <name>Your Name</name>
+ <user>handle</user>
+ <email>handle@php.net</email>
+ <active>yes</active>
+ </lead>
+ <date>2011-08-05</date>
+ <time>16:06:46</time>
+ <version>
+ <release>0.1.0</release>
+ <api>0.1.0</api>
+ </version>
+ <stability>
+ <release>alpha</release>
+ <api>alpha</api>
+ </stability>
+ <license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
+ <notes>Package PEAR2_DB_Pl2Method release notes for version 0.1.0.
+
+Package PEAR2_DB_Pl2Method API release notes for version 0.1.0.</notes>
+ <contents>
+ <dir name="/">
+ <dir name="src" baseinstalldir="PEAR2">
+ <dir name="DB">
+ <dir name="Pl2Method">
+ <file role="php" name="Main.php"/>
+ </dir>
+ </dir>
+ </dir>
+ </dir>
+ </contents>
+ <dependencies>
+ <required>
+ <php>
+ <min>5.2.0</min>
+ </php>
+ <pearinstaller>
+ <min>2.0.0a1</min>
+ </pearinstaller>
+ </required>
+ </dependencies>
+ <phprelease/>
+</package>
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<package version="2.1" xmlns="http://pear.php.net/dtd/package-2.1" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.1 http://pear.php.net/dtd/package-2.1.xsd">
+ <name>PEAR2_DB_Pl2Method</name>
+ <channel>pear2.php.net</channel>
+ <summary>Package PEAR2_DB_Pl2Method summary.
+</summary>
+ <description>
+Package detailed description here (found in README)</description>
+ <lead>
+ <name>Your Name</name>
+ <user>handle</user>
+ <email>handle@php.net</email>
+ <active>yes</active>
+ </lead>
+ <date>2011-08-05</date>
+ <time>16:06:46</time>
+ <version>
+ <release>0.1.0</release>
+ <api>0.1.0</api>
+ </version>
+ <stability>
+ <release>alpha</release>
+ <api>alpha</api>
+ </stability>
+ <license uri="http://www.opensource.org/licenses/bsd-license.php">New BSD License</license>
+ <notes>Package PEAR2_DB_Pl2Method release notes for version 0.1.0.
+
+Package PEAR2_DB_Pl2Method API release notes for version 0.1.0.</notes>
+ <contents>
+ <dir name="/">
+ <file role="php" name="php/PEAR2/DB/Pl2Method/Main.php"/>
+ </dir>
+ </contents>
+ <dependencies>
+ <required>
+ <php>
+ <min>5.2.0</min>
+ </php>
+ <pearinstaller>
+ <min>1.4.8</min>
+ </pearinstaller>
+ </required>
+ </dependencies>
+ <phprelease>
+ <filelist>
+ <install name="php/PEAR2/DB/Pl2Method/Main.php" as="PEAR2/DB/Pl2Method/Main.php"/>
+ </filelist>
+ </phprelease>
+</package>
@@ -0,0 +1,18 @@
+<?php
+/**
+ * Extra package.xml settings such as dependencies.
+ * More information: http://pear.php.net/manual/en/pyrus.commands.make.php#pyrus.commands.make.packagexmlsetup
+ */
+/**
+ * for example:
+$package->dependencies['required']->package['pear2.php.net/PEAR2_Autoload']->save();
+$package->dependencies['required']->package['pear2.php.net/PEAR2_Exception']->save();
+$package->dependencies['required']->package['pear2.php.net/PEAR2_MultiErrors']->save();
+$package->dependencies['required']->package['pear2.php.net/PEAR2_HTTP_Request']->save();
+
+$compatible->dependencies['required']->package['pear2.php.net/PEAR2_Autoload']->save();
+$compatible->dependencies['required']->package['pear2.php.net/PEAR2_Exception']->save();
+$compatible->dependencies['required']->package['pear2.php.net/PEAR2_MultiErrors']->save();
+$compatible->dependencies['required']->package['pear2.php.net/PEAR2_HTTP_Request']->save();
+*/
+?>
@@ -0,0 +1,43 @@
+<?php
+/**
+ * PEAR2\DATABASE\Pl2Method\Pl2Method
+ *
+ * PHP version 5
+ *
+ * @category DB
+ * @package PEAR2_DATABASE_Pl2Method
+ * @author Ivo Nascimento <ivo@o8o.com.br>
+ * @copyright 2011 Ivo Nascimento
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version SVN: $Id$
+ * @link http://svn.php.net/repository/pear2/PEAR2_DATABASE_Pl2Method
+ */
+
+/**
+ * Main class for PEAR2_DATABASE_Pl2Method
+ *
+ * @category Database
+ * @package PEAR2_DATABASE_Pl2Method
+ * @author Ivo Nascimento <ivo@o8o.com.br>
+ * @copyright 2011 Ivo Nascimento
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://svn.php.net/repository/pear2/PEAR2_DATABASE_Pl2Method
+ */
+namespace PEAR2\DB\Pl2Method;
+class Pl2Method
+{
+ public static function getInstance(\PDO $conn){
+ return self::getDriverInstance($conn);
+ }
+ /**
+ *
+ * Enter description here ...
+ * @param PDO $conn
+ * @return SGDBDriver
+ * @access Private
+ */
+ private static function getDriverInstance(\PDO $conn){
+ $driverName = "\\PEAR2\\DB\\Pl2Method\\Driver\\".ucfirst($conn->getAttribute( \PDO::ATTR_DRIVER_NAME));
+ return New $driverName($conn);
+ }
+}
@@ -0,0 +1,2 @@
+ipgpl.class.php
+ipgtype.class.php
@@ -0,0 +1,115 @@
+<?php
+/**
+ * PEAR2\DATABASE\Pl2Method\Driver\Pgsql
+ *
+ * PHP version 5
+ *
+ * @category DB
+ * @package PEAR2_DATABASE_Pl2Method
+ * @author Ivo nascimento <ivo@o8o.com.br>
+ * @copyright 2011 Your Name
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @version SVN: $Id$
+ * @link http://svn.php.net/repository/pear2/PEAR2_DATABASE_Pl2Method
+ */
+
+/**
+ * PostgreSQL Driver class for PEAR2_DATABASE_Pl2Method
+ *
+ * @category Database
+ * @package PEAR2_DATABASE_Pl2Method
+ * @author Ivo Nascimento <handle@php.net>
+ * @copyright 2011 Your Name
+ * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
+ * @link http://svn.php.net/repository/pear2/PEAR2_DATABASE_Pl2Method
+ */
+namespace PEAR2\DB\Pl2Method\Driver;
+class Pgsql extends \PEAR2\DB\Pl2Method\SGDBDriver
+{
+ public function __construct(\PDO $conn)
+ {
+ parent::__construct($conn);
+ }
+ public function __get($val)
+ {
+ $this->SCHEMA = $val;
+ $this->conn->query("set search_path to {$this->SCHEMA};");
+ return $this;
+ }
+ public function __call($fct,$values)
+ {
+/* if (isset($values) && $values[0]==self::_INFO)
+ return $this->getSPInfo($fct);
+ if ( $this->checkParam($fct,$values) )
+*/ return $this->getSP($fct,$this->specParam($values));
+// else
+// throw new Exception("Tipo dos Argumentos não conferem com parametros esperados.",1000);
+ }
+ private function specParam(Array $P)
+ {
+ $retorno = Array();
+ foreach ($P as $idx => $param)
+ {
+ $nvalue = $this->formatValue($param);
+ $retorno[]= $nvalue;
+ }
+ $retorno = implode(',',$retorno);
+ return $retorno;
+ }
+ private function getSP($spname,$paramstr)
+ {
+ $sqlpl ="select * from {$this->SCHEMA}.$spname ( {$paramstr});";
+ $this->statment = $this->conn->query($sqlpl);
+ return $this->statment;
+ }
+ private function checkParam($fct, $values)
+ {
+ return $this->validateData($fct, $values, $this->getSPInfo($fct));
+ }
+ public function getSPInfo($spname)
+ {
+ $sqlstr = "SELECT n.nspname as \"Schema\",
+ p.proname as \"Name\",
+ CASE WHEN p.proretset THEN 'setof ' ELSE '' END ||
+ pg_catalog.format_type(p.prorettype, NULL) as \"Result data type\",
+ CASE WHEN proallargtypes IS NOT NULL THEN
+ pg_catalog.array_to_string(ARRAY(
+ SELECT
+ CASE
+ WHEN p.proargmodes[s.i] = 'i' THEN ''
+ WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '
+ WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '
+ END ||
+ CASE
+ WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''
+ ELSE p.proargnames[s.i] || ' '
+ END ||
+ pg_catalog.format_type(p.proallargtypes[s.i], NULL)
+ FROM
+ pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)
+ ), ', ')
+ ELSE
+ pg_catalog.array_to_string(ARRAY(
+ SELECT
+ CASE
+ WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''
+ ELSE p.proargnames[s.i+1] || ' '
+ END ||
+ pg_catalog.format_type(p.proargtypes[s.i], NULL)
+ FROM
+ pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)
+ ), ', ')
+ END AS \"Argument data types\"
+ FROM pg_catalog.pg_proc p
+ LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
+ WHERE p.prorettype <> 'pg_catalog.cstring'::pg_catalog.regtype
+ AND (p.proargtypes[0] IS NULL
+ OR p.proargtypes[0] <> 'pg_catalog.cstring'::pg_catalog.regtype)
+ AND NOT p.proisagg
+ AND p.proname ~ '^({$spname})$'
+ AND pg_catalog.pg_function_is_visible(p.oid)
+ ORDER BY 1, 2, 3, 4;";
+ $this->Info = $this->PDO->query($sqlstr);//"select * from soma2(10,20);");
+ return $this->Info->fetch(PDO::FETCH_NUM);
+ }
+}
Oops, something went wrong.

0 comments on commit 2aefba4

Please sign in to comment.