phpcompta-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpcompta-dev] r5537 - phpcompta/trunk/dev/manage-code/create-file


From: phpcompta-dev
Subject: [Phpcompta-dev] r5537 - phpcompta/trunk/dev/manage-code/create-file
Date: Tue, 5 Nov 2013 22:28:39 +0100 (CET)

Author: danydb
Date: 2013-11-05 22:28:39 +0100 (Tue, 05 Nov 2013)
New Revision: 5537

Modified:
   phpcompta/trunk/dev/manage-code/create-file/
   phpcompta/trunk/dev/manage-code/create-file/create_config.py
   phpcompta/trunk/dev/manage-code/create-file/create_phpclass.py
Log:
Create file for SQL class


Property changes on: phpcompta/trunk/dev/manage-code/create-file
___________________________________________________________________
Name: svn:ignore
   - .menu_ref_struct.txt.swp
.class_menu_ref_sql.php.swp
.create_config.py.swp
.create_phpclass.py.swp
.profile_struct.txt.swp
.profile_menu_struct.txt.swp

   + .menu_ref_struct.txt.swp
.class_menu_ref_sql.php.swp
.create_config.py.swp
.create_phpclass.py.swp
.profile_struct.txt.swp
.profile_menu_struct.txt.swp
.create_config.py.kate-swp
.create_phpclass.py.kate-swp
.create_phpclass2.py.kate-swp
.class_Jrnx_sql_SQL.php.swo


Modified: phpcompta/trunk/dev/manage-code/create-file/create_config.py
===================================================================
--- phpcompta/trunk/dev/manage-code/create-file/create_config.py        
2013-10-29 12:58:56 UTC (rev 5536)
+++ phpcompta/trunk/dev/manage-code/create-file/create_config.py        
2013-11-05 21:28:39 UTC (rev 5537)
@@ -27,6 +27,11 @@
 except:
     print "Utilisation "+sys.argv[0]+" -s nom_du_schema -t nom de la table + c 
connexion string"
     print 'example -t jrnxc -c "dbname=xxx user=xx port=xx  password=xxx"'
+    print """
+    This utility create a file, this file can be given as input to the script
+    create_phpclass.py with the option -f
+    This will create the corresponding PHP File that you need to put in the 
include folder
+    """
     sys.exit(-1)
 
 cnx=psycopg2.connect (connexion_string)
@@ -52,7 +57,7 @@
     worlds.append(i.capitalize())
 
 class_name='_'.join(worlds)
-class_name=class_name+"_sql"
+class_name=class_name+"_SQL"
 
 file.write(class_name+"\n")
 file.write(schema+"."+table+"\n")

Modified: phpcompta/trunk/dev/manage-code/create-file/create_phpclass.py
===================================================================
--- phpcompta/trunk/dev/manage-code/create-file/create_phpclass.py      
2013-10-29 12:58:56 UTC (rev 5536)
+++ phpcompta/trunk/dev/manage-code/create-file/create_phpclass.py      
2013-11-05 21:28:39 UTC (rev 5537)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+  #!/usr/bin/python
 
 
 # Command we have to replace
@@ -8,13 +8,12 @@
 # @class_name@ Name of the class (uppercase)
 # @column_array@ fill the $variable
 # @sql_update@ the sql update
-# @column_comma the column for insert and update
 # read the file with the name
+# @column_comma the column for insert and update
 # first line = table name
 # second line = pk
 
 import sys, getopt
-import pdb
 
 def help():
     print """
@@ -64,418 +63,55 @@
 
 @endcode
  */
-require_once('class_database.php');
-require_once('ac_common.php');
+require_once('class_phpcompta_sql.php');
 
 
 /**
  address@hidden Manage the table @table@
 */
-class @class_name@  @mother_class@
+class @class_name@ extends Phpcompta_SQL
 {
   /* example private 
$variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0);
 */
-  
-  protected $variable=array(@column_array@);
-  function __construct ( & $p_cn,$p_id=-1) {
-        $this->cn=$p_cn;
-        $this->@address@hidden;
-        
-        if ( $p_id == -1 ) {
-        /* Initialize an empty object */
-            foreach ($this->variable as $key=>$value) $this->$value=null;
-            $this->@address@hidden;
-        } else {
-         /* load it */
+  function __construct($p_id=-1)
+       {
 
-         $this->load();
-      }
-  }
-  public function get_parameter($p_string) {
-    if ( array_key_exists($p_string,$this->variable) ) {
-      $idx=$this->variable[$p_string];
-      return $this->$idx;
-    }
-    else 
-      throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut 
inexistant');
-  }
-  public function set_parameter($p_string,$p_value) {
-    if ( array_key_exists($p_string,$this->variable) ) {
-      $idx=$this->variable[$p_string];
-      $this->$idx=$p_value;
-    }
-    else 
-      throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut 
inexistant');
-  }
-  public function get_info() {    return var_export($this,true);  }
-  public function verify() {
-    // Verify that the elt we want to add is correct
-    /* verify only the datatype */
-    @verify_data_type@
-    @set_tech_user@
-  }
-  public function save() {
-  /* please adapt */
-    if (  $this->@id@ == -1 ) 
-      $this->insert();
-    else
-      $this->update();
-  }
-  /**
-   address@hidden retrieve array of object thanks a condition
-   address@hidden $cond condition (where clause) (optional by default all the 
rows are fetched)
-   * you can use this parameter for the order or subselect
-   address@hidden $p_array array for the SQL stmt
-   address@hidden Database::exec_sql get_object  Database::num_row
-   address@hidden the return value of exec_sql
-   */
-   public function seek($cond='',$p_array=null) 
-   {
-     $sql="select * from @table@  $cond";
-     $aobj=array();
-     $ret= $this->cn->exec_sql($sql,$p_array);
-     return $ret;
-   }
-   /**
-    *get_seek return the next object, the return of the query must have all 
the column
-    * of the object
-    address@hidden $p_ret is the return value of an exec_sql
-    address@hidden $idx is the index
-    address@hidden seek
-    address@hidden object 
-    */
-   public function get_object($p_ret,$idx)
-    {
-     // map each row in a object
-     $oobj=new @class_name@ ($this->cn);
-     $array=Database::fetch_array($p_ret,$idx);
-     foreach ($array as $idx=>$value) { $oobj->$idx=$value; }
-     return $oobj;
-   }
-  public function insert() {
-    if ( $this->verify() != 0 ) return;
-      if( $this->@address@hidden ){
-    /*  please adapt */
-    $sql="insert into @table@(@column_noid@) values (@column_insert@) 
returning @id@";
-    
-    $this->@address@hidden>cn->get_value(
-                $sql,
-                array( @column_this@)
-                );
-          } else {
-              $sql="insert into @table@(@column_noid@,@id@) values 
(@column_insert_id@) returning @id@";
-    
-    $this->@address@hidden>cn->get_value(
-                $sql,
-                array( @column_this_id@)
-                );
 
-          }
-   
-  }
+               $this->table = "@table@";
+               $this->primary_key = "@id@";
 
-  public function update() {
-    if ( $this->verify() != 0 ) return;
-    /*   please adapt */
-    $sql="@sql_update@";
-    $res=$this->cn->exec_sql(
-                $sql,
-                array(@column_comma@,$this->@id@)
-                );
-                
-  }
-/**
- address@hidden load a object
- address@hidden 0 on success -1 the object is not found
- */
-  public function load() {
+               $this->name = array(
+                       @column_array@
+               );
 
-   $sql="select @column_select@ from @table@ where @address@hidden"; 
-    /* please adapt */
-    $res=$this->cn->get_array(
-                $sql,
-                array($this->@id@)
-                );
-                
-    if ( count($res) == 0 ) {
-          /* Initialize an empty object */
-          foreach ($this->variable as $key=>$value) $this->$key='';
+               $this->type = array(
+                       @column_type_array@
+               );
 
-          return -1;
-          }
-    foreach ($res[0] as $idx=>$value) { $this->$idx=$value; }
-    return 0;
-  }
+               $this->default = array(
+                       "@id@" => "auto"
+               );
+               global $cn;
 
-  public function delete() {
-    $sql="delete from @table@ where @address@hidden"; 
-    $res=$this->cn->exec_sql($sql,array($this->@id@));
-  }
-  /**
-   * Unit test for the class
-   */  
-  static function test_me() {
-      $cn=new Database(25);
-$cn->start();
-    echo h2info('Test object vide');
-    $obj=new @class_name@($cn);
-    var_dump($obj);
-
-    echo h2info('Test object NON vide');
-    $obj->set_parameter('j_id',3);
-    $obj->load();
-    var_dump($obj);
-
-    echo h2info('Update');
-    $obj->set_parameter('j_qcode','NOUVEAU CODE');
-    $obj->save();
-    $obj->load();
-    var_dump($obj);
-
-    echo h2info('Insert');
-    $obj->set_parameter('j_id',0);
-    $obj->save();
-    $obj->load();
-    var_dump($obj);
-
-    echo h2info('Delete');
-    $obj->delete();
-    echo (($obj->load()==0)?'Trouve':'non trouve');
-    var_dump($obj);
-$cn->rollback();
-
-  }
-  
-}
-// @class_name@::test_me();
-?>
-"""
-    sChild="""<?php
-/**
- address@hidden
- address@hidden Manage the table @table@
- *
- *
-Example
address@hidden
-
address@hidden
- */
-require_once('class_database.php');
-require_once('ac_common.php');
-
-/**
- address@hidden Manage the table @table@
- address@hidden @mother_class@
-*/
-class @class_name@  @mother_class@
-{
-  /* example private 
$variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0);
 */
-  
-  protected $variable=array(@column_array@);
-
+               parent::__construct($cn, $p_id);
+       }
   public function verify() {
-    // Verify that the elt we want to add is correct
-    /* verify only the datatype */
-    @verify_data_type@
-    @set_tech_user@
-  }
-  public function save() {
-  /* please adapt */
-    if (  $this->@id@ == -1 ) 
-      $this->insert();
-    else
-      $this->update();
-  }
-  public function insert() {
-    if ( $this->verify() != 0 ) return;
-    /*  please adapt */
-    $sql="insert into @table@(@column_noid@) values (@column_insert@) 
returning @id@";
+    parent::verify();
     
-    $this->@address@hidden>cn->get_value(
-                $sql,
-                array( @column_this@)
-                );
-   
   }
-
-  public function update() {
-    if ( $this->verify() != 0 ) return;
-    /*   please adapt */
-    $sql="@sql_update@";
-    $res=$this->cn->exec_sql(
-                $sql,
-                array(@column_comma@,$this->@id@)
-                );
-                
-  }
-/**
- address@hidden load ONE object, the pk must be set before calling
- address@hidden 0 on success -1 the object is not found
- */
-  public function load() {
-
-   $sql="select @column_select@ from @table@ where @address@hidden"; 
-    /* please adapt */
-    $res=$this->cn->get_array(
-                $sql,
-                array($this->@id@)
-                );
-                
-    if ( count($res) == 0 ) {
-          /* Initialize an empty object */
-          foreach ($this->variable as $key=>$value) $this->$key='';
-
-          return -1;
-          }
-    foreach ($res[0] as $idx=>$value) { $this->$idx=$value; }
-    return 0;
-  }
-
-  public function delete() {
-    $sql="delete from @table@ where @address@hidden"; 
-    $res=$this->cn->exec_sql($sql,array($this->@id@));
-  }
-  /**
-   * Unit test for the class
-   */  
-  static function test_me() {
-      $cn=new Database(25);
-$cn->start();
-    echo h2info('Test object vide');
-    $obj=new @class_name@($cn);
-    var_dump($obj);
-
-    echo h2info('Test object NON vide');
-    $obj->set_parameter('j_id',3);
-    $obj->load();
-    var_dump($obj);
-
-    echo h2info('Update');
-    $obj->set_parameter('j_qcode','NOUVEAU CODE');
-    $obj->save();
-    $obj->load();
-    var_dump($obj);
-
-    echo h2info('Insert');
-    $obj->set_parameter('j_id',0);
-    $obj->save();
-    $obj->load();
-    var_dump($obj);
-
-    echo h2info('Delete');
-    $obj->delete();
-    echo (($obj->load()==0)?'Trouve':'non trouve');
-    var_dump($obj);
-$cn->rollback();
-
-  }
   
 }
address@hidden@::test_me();
+  
 ?>
 """
-    sView="""<?php
-/**
- address@hidden
- address@hidden Manage the view @table@
- *
- *
-Example
address@hidden
-
address@hidden
- */
-require_once('class_database.php');
-require_once('ac_common.php');
-
-/**
- address@hidden Manage the view @table@
-*/
-class @class_name@
-{
-  /* example private 
$variable=array("easy_name"=>column_name,"email"=>"column_name_email","val3"=>0);
 */
   
-  protected $variable=array(@column_array@);
-  function __construct ( & $p_cn) {
-        $this->cn=$p_cn;
-  }
 
-
-/**
- address@hidden load ONE object
- address@hidden 0 on success -1 the object is not found
- */
-  public function load($cond,$array=null) {
-
-   $sql="select @column_select@ from @table@ $cond"; 
-    /* please adapt */
-    $res=$this->cn->get_array(
-                $sql,
-                $array
-                );
-                
-    if ( count($res) == 0 ) {
-          /* Initialize an empty object */
-          foreach ($this->variable as $key=>$value) $this->$key='';
-
-          return -1;
-          }
-    foreach ($res[0] as $idx=>$value) { $this->$idx=$value; }
-    return 0;
-  }
-
-
-  /**
-   address@hidden retrieve array of object thanks a condition
-   address@hidden $cond condition (where clause) (optional by default all the 
rows are fetched)
-   * you can use this parameter for the order or subselect
-   address@hidden $p_array array for the SQL stmt
-   address@hidden Database::exec_sql get_object  Database::num_row
-   address@hidden the return value of exec_sql
-   */
-   public function seek($cond='',$p_array=null) 
-   {
-     $sql="select * from @table@  $cond";
-     $aobj=array();
-     $ret= $this->cn->exec_sql($sql,$p_array);
-     return $ret;
-   }
-   /**
-    *get_seek return the next object, the return of the query must have all 
the column
-    * of the object
-    address@hidden $p_ret is the return value of an exec_sql
-    address@hidden $idx is the index
-    address@hidden seek Database::num_row
-    address@hidden object 
-    */
-   public function get_object($p_ret,$idx)
-    {
-     // map each row in a object
-     $oobj=new @class_name@ ($this->cn);
-     $array=Database::fetch_array($p_ret,$idx);
-     foreach ($array as $idx=>$value) { $oobj->$idx=$value; }
-     $aobj=clone $oobj;
-
-     return $aobj;
-   }
-}
-?>
-
-"""
-
     # read the file
     try :
         file=open(filein,'r')
         line=file.readlines()
-        mother_name='';mother_class=''
-        if line[0].find(':') > 0 :
-            class_name=(line[0].split(':'))[0].strip()
-            mother_name=(line[0].split(':'))[1].strip()
-            mother_class="extends "+mother_name
-        else:
-            class_name=line[0].strip()
-            mother_name=''
-            mother_class=''
+        class_name=line[0].strip()
+                  
+            
         table=line[1].strip()
         (id,type_id,default)=line[2].strip().split('|')
         id=id.strip()
@@ -484,7 +120,7 @@
         column_select=''
         column_insert=''
         fileoutput=open("class_"+class_name.lower()+".php",'w+')
-       print "Create the file %s "+fileoutput.name
+       print "Create the file "+fileoutput.name
         
         sep=''
         i=1
@@ -514,12 +150,23 @@
         column_insert_id=column_insert+sep+'$'+str(i)+"\n"
         column_this_id=column_this+sep+'$this->'+id
         column_array=''
+        column_type_array=''
         sep=''
         for e in line [3:]:
             if e.find('|') < 0 :
                 continue
             col_name=(e.split('|'))[0].strip()
+            col_type=(e.split('|'))[1].strip()
+            if col_type == 'integer' or col_type == 'numeric' or 
col_type=='bigint':
+               col_type="numeric"
+           elif col_type=='text' or col_type=='character varying':
+               col_type="text"
+           elif col_type=='date':
+               col_type='date'
+           else :
+               col_type='set_me'
             column_array+=sep+'"'+col_name+'"=>"'+col_name+'"'+"\n"
+            column_type_array+=sep+'"'+col_name+'"=>"'+col_type+'"'+"\n"
             sep=','
         column_array='"'+id+'"=>"'+id+'",'+column_array
         sql_update=" update "+table
@@ -553,59 +200,23 @@
                    if col_type in ('date',' timestamp without time 
zone','timestamp with time zone'):
                        verify_data_type+=" if (isDate($this->"+col_id+") == 
null )\n \
             throw new Exception('DATATYPE "+col_id+" $this->"+col_id+" date 
invalide');\n"
-        if  child == True :
-            sChild=sChild.replace('@id@',id)
-            sChild=sChild.replace('@table@',table)
-            sChild=sChild.replace('@class_name@',class_name)
-            sChild=sChild.replace('@column_noid@',column_noid)
-            sChild=sChild.replace('@column_array@',column_array)
-            sChild=sChild.replace('@sql_update@',sql_update)
-            sChild=sChild.replace('@column_comma@',column_comma)
-            sChild=sChild.replace('@column_this@',column_this)
-            sChild=sChild.replace('@column_this_id@',column_this_id)   
-            sChild=sChild.replace('@verify_data_type@',verify_data_type)
-            sChild=sChild.replace('@column_select@',column_select)
-            sChild=sChild.replace('@column_insert@',column_insert)
-            sChild=sChild.replace('@mother_name@',mother_name)
-            sChild=sChild.replace('@mother_class@',mother_class)            
-            sChild=sChild.replace('@column_insert_id@',column_insert_id)
-            sChild=sChild.replace('@set_tech_user@',set_tech_user)
-            fileoutput.writelines(sChild)
-        elif view == True:
-            sView=sView.replace('@id@',id)
-            sView=sView.replace('@table@',table)
-            sView=sView.replace('@class_name@',class_name)
-            sView=sView.replace('@column_noid@',column_noid)
-            sView=sView.replace('@column_array@',column_array)
-            sView=sView.replace('@sql_update@',sql_update)
-            sView=sView.replace('@column_comma@',column_comma)
-            sView=sView.replace('@column_this@',column_this)
-            sView=sView.replace('@column_this_id@',column_this_id)     
-            sView=sView.replace('@verify_data_type@',verify_data_type)
-            sView=sView.replace('@column_select@',column_select)
-            sView=sView.replace('@column_insert@',column_insert)
-            sView=sView.replace('@mother_name@',mother_name)
-            sView=sView.replace('@mother_class@',mother_class)            
-            sView=sView.replace('@column_insert_id@',column_insert_id)
-            sView=sView.replace('@set_tech_user@',set_tech_user)
-            fileoutput.writelines(sView)
-        else:
-            sParent=sParent.replace('@id@',id)
-            sParent=sParent.replace('@table@',table)
-            sParent=sParent.replace('@class_name@',class_name)
-            sParent=sParent.replace('@column_noid@',column_noid)
-            sParent=sParent.replace('@column_array@',column_array)
-            sParent=sParent.replace('@sql_update@',sql_update)
-            sParent=sParent.replace('@column_comma@',column_comma)
-            sParent=sParent.replace('@column_this@',column_this)
-           sParent=sParent.replace('@column_this_id@',column_this_id)  
-            sParent=sParent.replace('@verify_data_type@',verify_data_type)
-            sParent=sParent.replace('@column_select@',column_select)
-            sParent=sParent.replace('@column_insert@',column_insert)
-            sParent=sParent.replace('@column_insert_id@',column_insert_id)
-            sParent=sParent.replace('@mother_class@',mother_class)
-            sParent=sParent.replace('@set_tech_user@',set_tech_user)
-            fileoutput.writelines(sParent)
+       
+       sParent=sParent.replace('@id@',id)
+       sParent=sParent.replace('@table@',table)
+       sParent=sParent.replace('@class_name@',class_name)
+       sParent=sParent.replace('@column_noid@',column_noid)
+       sParent=sParent.replace('@column_array@',column_array)
+       sParent=sParent.replace('@column_type_array@',column_type_array)
+       sParent=sParent.replace('@sql_update@',sql_update)
+       sParent=sParent.replace('@column_comma@',column_comma)
+       sParent=sParent.replace('@column_this@',column_this)
+       sParent=sParent.replace('@column_this_id@',column_this_id)      
+       sParent=sParent.replace('@verify_data_type@',verify_data_type)
+       sParent=sParent.replace('@column_select@',column_select)
+       sParent=sParent.replace('@column_insert@',column_insert)
+       sParent=sParent.replace('@column_insert_id@',column_insert_id)
+       sParent=sParent.replace('@set_tech_user@',set_tech_user)
+       fileoutput.writelines(sParent)
 
     except :
         print "error "



---
PhpCompta est un logiciel de comptabilité libre en ligne (full web)
Projet opensource http://www.phpcompta.eu



reply via email to

[Prev in Thread] Current Thread [Next in Thread]