27 require_once NOALYSS_INCLUDE.
'/constant.php';
28 require_once NOALYSS_INCLUDE.
'/lib/ac_common.php';
50 if (IsNumber($p_database_id)==
false||strlen($p_database_id)>10)
51 die(
"-->Dossier invalide [$p_database_id]");
52 $noalyss_user=(defined(
"noalyss_user"))?noalyss_user:phpcompta_user;
53 $password=(defined(
"noalyss_password"))?noalyss_password:phpcompta_password;
54 $port=(defined(
"noalyss_psql_port"))?noalyss_psql_port:phpcompta_psql_port;
55 $host=(!defined(
"noalyss_psql_host") )?
'127.0.0.1':noalyss_psql_host;
56 if (defined(
"MULTI")&&MULTI==
"0")
63 if ($p_database_id==0)
65 $l_dossier=sprintf(
"%saccount_repository", strtolower(domaine));
69 $l_dossier=sprintf(
"%sdossier%d", strtolower(domaine), $p_database_id);
73 $l_dossier=sprintf(
"%smod%d", strtolower(domaine), $p_database_id);
77 $l_dossier=
'template1';
81 throw new Exception(
'Connection invalide');
86 $a=pg_connect(
"dbname=$l_dossier host='$host' user='$noalyss_user'
87 password='$password' port=$port");
94 echo
'<h2 class="error">Impossible de se connecter à postgreSql !</h2>';
96 echo
"Vos paramètres sont incorrectes : <br>";
98 echo
"base de donnée : $l_dossier<br>";
99 echo
"Domaine : ".domaine.
"<br>";
100 echo
"Port $port <br>";
101 echo
"Utilisateur : $noalyss_user <br>";
104 die(
"Connection impossible : vérifiez vos paramètres de base
109 echo
'<h2 class="error">Erreur de connexion !</h2>';
110 $this->is_open=
false;
117 pg_exec($this->
db,
'set search_path to public,comptaproc,pg_catalog;');
119 pg_exec($this->
db,
'set DateStyle to ISO, MDY;');
131 static function connect($p_user,$p_password,$p_dbname,$p_host,$p_port) {
132 $a=pg_connect(
"dbname=$p_dbname host='$p_host' user='$p_user'
133 password='$p_password' port=$p_port");
143 pg_set_client_encoding($this->
db, $p_charset);
147 return pg_client_encoding($this->
db);
163 if ( ! $this->is_open )
throw new Exception(
' Database is closed');
164 $this->sql=$p_string;
170 $this->ret=pg_query($this->
db, $p_string);
172 $this->ret=@pg_query($this->
db, $p_string);
179 throw new Exception(
"Erreur : exec_sql attend un array");
182 $this->ret=pg_query_params($this->
db, $p_string,
$p_array);
184 $this->ret=@pg_query_params($this->
db, $p_string,
$p_array);
188 $str_error=pg_last_error($this->
db).pg_result_error($this->ret);
189 throw new Exception(
" SQL ERROR $p_string ".$str_error, 1);
198 echo $a->getMessage();
200 echo $a->getTraceAsString();
201 echo pg_last_error($this->
db);
203 error_log($a->getTraceAsString());
222 return pg_NumRows($r_sql);
241 $Res=$this->
exec_sql(
"select nextval('$p_seq') as seq");
261 if ( ! $this->is_open)
return;
270 if ( ! $this->is_open)
return;
283 $Res=$this->
exec_sql(
"alter sequence $p_name restart $min");
299 throw new Exception (
'Ne peut ouvrir '.
$script);
302 $flag_function=
false;
306 $buffer=str_replace(
"$",
"\$", $buffer);
307 print $buffer.
"<br>";
309 if (substr($buffer, 0, 2)==
"--")
315 If (Strlen($buffer)==0)
320 if (strpos(strtolower($buffer),
"create function")===0)
322 echo
"found a function";
327 if (strpos(strtolower($buffer),
"create or replace function")===0)
329 echo
"found a function";
335 if ($flag_function==
false&&strpos($buffer,
';')==
false)
342 if (strpos(strtolower($buffer),
"language plpgsql")===
false&&
343 strpos(strtolower($buffer),
"language 'plpgsql'")===
false)
352 $buffer=str_replace(
';',
'', $buffer);
360 print "ERROR : $sql";
361 throw new Exception(
"ERROR : $sql");
364 $flag_function=
false;
393 if ($this->ret==
false)
394 throw new Exception(
'this->ret is empty');
395 return pg_fetch_array($this->ret, $p_indice);
410 return pg_NumRows($this->ret);
412 return pg_NumRows($p_ret);
421 return $this->
size($p_ret);
434 echo _(
'Base de donnée vide');
438 $succeed=
"<span style=\"font-size:18px;color:green\">✓</span>";
439 echo
'<ul style="list-type-style:square">';
440 for ($i=4; $i<=$MaxVersion; $i++)
450 $this->
exec_sql(
'ALTER TABLE amortissement.amortissement_histo
451 ADD CONSTRAINT internal_fk FOREIGN KEY (jr_internal) REFERENCES jrn (jr_internal)
452 ON UPDATE CASCADE ON DELETE SET NULL');
455 echo
"<li>Patching ".$p_name.
456 " from the version ".$this->get_version().
" to $to ";
458 $this->
execute_script (NOALYSS_INCLUDE.
'/sql/patch/upgrade'.$i.
'.sql');
466 $sql=
"select jrn_def_id from jrn_def ";
472 $sql=sprintf(
"create sequence s_jrn_%d",
$row[
'jrn_def_id']);
481 $Res2=$this->
exec_sql(
'select coalesce(max(jr_grpt_id),1) as l from jrn');
482 $Max2=pg_NumRows($Res2);
485 $Row=pg_fetch_array($Res2, 0);
488 $this->
exec_sql(
"select setval('s_grpt',$M,true)");
494 $this->
execute_script(NOALYSS_INCLUDE.
'/sql/patch/upgrade17.sql');
495 $max=$this->
get_value(
'select last_value from s_jnt_fic_att_value');
500 if ($i==30&&$p_name==
"mod")
502 $a_seq=array(
's_jrn',
's_jrn_op',
's_centralized',
503 's_stock_goods',
'c_order',
's_central');
504 foreach ($a_seq as
$seq)
506 $sql=sprintf(
"select setval('%s',1,false)", $seq);
509 $sql=
"select jrn_def_id from jrn_def ";
512 for ($seq=0; $seq<
$Max; $seq++)
515 $sql=sprintf(
"select setval('s_jrn_%d',1,false)",
$row[
'jrn_def_id']);
522 $res=$this->
exec_sql(
"select pr_value from parameter where pr_id='MY_COUNTRY'");
523 $country=pg_fetch_result(
$res, 0, 0);
524 $this->
execute_script(NOALYSS_INCLUDE.
"/sql/patch/upgrade36.".$country.
".sql");
525 $this->
exec_sql(
'update tmp_pcmn set pcm_type=find_pcm_type(pcm_val)');
529 $res=$this->
exec_sql(
"select pr_value from parameter where pr_id='MY_COUNTRY'");
530 $country=pg_fetch_result(
$res, 0, 0);
532 $this->
exec_sql(
"insert into parm_code values ('SUPPLIER',440,'Poste par défaut pour les fournisseurs')");
534 $this->
exec_sql(
"insert into parm_code values ('SUPPLIER',400,'Poste par défaut pour les fournisseurs')");
538 $country=$this->
get_value(
"select pr_value from parameter where pr_id='MY_COUNTRY'");
539 $this->
execute_script(NOALYSS_INCLUDE.
"/sql/patch/upgrade61.".$country.
".sql");
565 $r=pg_NumRows($this->ret);
570 $array=pg_fetch_all($this->ret);
571 throw new Exception(
"Attention $p_sql retourne ".pg_NumRows($this->ret).
" valeurs ".
574 $r=pg_fetch_row($this->ret, 0);
597 if (pg_NumRows(
$r)==0)
611 if (empty(
$array) )
return null;
613 throw new Exception(
"Database:get_row retourne trop de lignes",100);
624 $sql=
"create sequence ".$p_name.
" minvalue $min";
635 $r=$this->
count_sql(
"select relname from pg_class where relname=lower($1)", array($p_name));
650 $r=$this->
count_sql(
"select table_name from information_schema.tables where table_schema=$1 and table_name=lower($2)", array($p_schema, $p_name));
665 $r=$this->
get_value(
'select count(*) from information_schema.columns where table_name=lower($1) and column_name=lower($2) and table_schema=lower($3)', array($col,
$table, $schema));
682 $sys_name=sprintf(
"%sdossier%d", strtolower(domaine),
$p_id);
685 $sys_name=sprintf(
"%smod%d", strtolower(domaine),
$p_id);
689 throw new Exception(__FILE__.
" format_name invalid type ".
$p_type. __LINE__);
701 $database_exist=$this->
get_value(
'select count(*)
702 from pg_catalog.pg_database where datname = lower($1)', array($p_name));
703 return $database_exist;
713 $r=$this->
get_value(
'select count(*) from pg_largeobject_metadata where oid=$1'
728 $r=$this->
count_sql(
"select viewname from pg_views where viewname=lower($1)", array($p_name));
741 $r=$this->
count_sql(
"select nspname from pg_namespace where nspname=lower($1)", array($p_name));
766 $aIdx=array_keys($aArray[0]);
770 for ($i=0; $i<
count($aArray); $i++)
819 if (
$max==0&&$p_null==0)
821 for ($i=0; $i<
$max; $i++)
830 for ($i=$max; $i!=0; $i--)
832 $r[$i][
'value']=
$r[$i-1][
'value'];
833 $r[$i][
'label']=
h(
$r[$i-1][
'label']);
857 $ret=$this->
exec_sql(
"select jr_pj from jrn where jr_grpt_id=$seq");
858 if (pg_num_rows(
$ret)!=0)
860 $r=pg_fetch_array(
$ret, 0);
861 $old_oid=
$r[
'jr_pj'];
862 if (strlen($old_oid)!=0)
863 pg_lo_unlink(
$cn, $old_oid);
866 $this->
exec_sql(
"update jrn set jr_pj=$1 , jr_pj_name=$2,
867 jr_pj_type=$3 where jr_grpt_id=$4",
868 array($oid,$_FILES[
'pj'][
'name'] ,$_FILES[
'pj'][
'type'],
$seq));
885 if ($_FILES[$p_name][
"error"]==UPLOAD_ERR_NO_FILE)
890 $new_name=tempnam($_ENV[
'TMP'], $p_name);
891 if ($_FILES[$p_name][
"error"]>0)
894 echo_error(__FILE__.
":".__LINE__.
"Error: ".$_FILES[$p_name][
"error"]);
897 if (strlen($_FILES[$p_name][
'tmp_name'])!=0)
899 if (move_uploaded_file($_FILES[$p_name][
'tmp_name'], $new_name))
902 $oid=pg_lo_import($this->
db, $new_name);
905 echo_error(__FILE__, __LINE__,
"cannot upload document");
913 echo
"<H1>Error</H1>";
928 return pg_NumRows(
$ret);
939 return pg_fetch_array(
$ret, $p_indice);
949 return pg_fetch_all(
$ret);
961 return pg_fetch_result(
$ret, $p_row, $p_col);
972 return pg_fetch_row(
$ret, $p_row);
983 return pg_lo_unlink($this->
db, $p_oid);
994 return pg_prepare($this->
db, $p_string, $p_sql);
1007 $this->ret=pg_execute($this->
db, $p_string,
$p_array);
1020 return pg_lo_export($this->
db, $p_oid,
$tmp);
1031 return pg_lo_import($this->
db, $p_oid);
1041 return pg_escape_string($p_string);
1049 if ( $this->is_open ) pg_close($this->
db);
1050 $this->is_open=FALSE;
1072 return pg_transaction_status($this->
db);
1085 for ($i=0; $i<
count($aheader); $i++)
1087 echo
$seq.
'"'.$aheader[$i][
'title'].
'"';
1099 switch ($aheader[
$e][
'type'])
1102 echo $sep2.nb(
$row[$e]);
1105 echo $sep2.
'"'.
$row[
$e].
'"';
1120 select table_schema,table_name,column_name
1122 information_schema.columns
1123 where table_schema not in ('information_schema','pg_catalog')
1124 and data_type='oid'";
1126 select oid,'N' as used from pg_largeobject_metadata
1130 if ( $a_table ==
false || $a_lob ==
false )
return;
1132 $nb_lob=
count($a_lob);
1133 $nb_table=
count($a_table);
1134 for ($i=0;$i < $nb_lob;$i++)
1136 $lob=$a_lob[$i][
'oid'];
1137 if ( $a_lob[$i][
'used']==
'Y')
continue;
1138 for ($j=0;$j < $nb_table;$j++)
1140 if ( $a_lob[$i][
'used']==
'Y')
continue;
1142 $a_table[$j][
'table_schema'].
".".$a_table[$j][
'table_name'].
1144 $a_table[$j][
'column_name'].
"=$1",array($lob));
1146 $a_lob[$i][
'used']=
'Y';
1150 for ($i=0;$i < $nb_lob;$i++)
1152 if ( $a_lob[$i][
'used']==
'Y')
continue;
count_sql($p_sql, $p_array=null)
Count the number of row returned by a sql statement.
format_name($p_id, $p_type)
return the name of the database with the domain name
static fetch_all($ret)
wrapper for the function pg_fetch_all
execute($p_string, $p_array)
wrapper for the function pg_execute
exist_table($p_name, $p_schema='public')
test if a table exist
exist_sequence($p_name)
test if a sequence exist
get_affected()
return the number of rows affected by the previous query
static num_row($ret)
wrapper for the function pg_NumRows
lo_import($p_oid)
wrapper for the function pg_lo_export
size($p_ret=null)
return the number of rows found by the last query, or the number of rows from $p_ret ...
make_list($sql, $p_array=null)
create a string containing the value separated by comma for use in a SQL in statement ...
create_sequence($p_name, $min=1)
Create a sequence.
clean_orphan_lob()
Find all lob and remove those which are not used by any tables.
start()
: start a transaction
const DBVERSION(!defined("SYSINFO_DISPLAY"))
fetch($p_indice)
fetch the $p_indice array from the last query
query_to_csv($ret, $aheader)
with the handle of a successull query, echo each row into CSV and send it directly ...
close()
wrapper for the function pg_close
static escape_string($p_string)
wrapper for the function pg_escape_string
exec_sql($p_string, $p_array=null)
send a sql string to the database
execute_script($script)
Execute a sql script.
static fetch_array($ret, $p_indice=0)
wrapper for the function pg_fetch_array
echo_error($p_log, $p_line="", $p_message="")
log error into the /tmp/noalyss_error.log it doesn't work on windows
get_version()
Get version of a database, the content of the table version.
commit()
Commit the transaction.
apply_patch($p_name)
loop to apply all the path to a folder or a template
get_current_seq($p_seq)
get the current sequence value
lo_export($p_oid, $tmp)
wrapper for the function pg_lo_export
lo_unlink($p_oid)
wrapper for the function pg_lo_unlink
static fetch_row($ret, $p_row)
wrapper for the function pg_fetch_row
function trim(s)
remove trailing and heading space
get_next_seq($p_seq)
get the next sequence value
rollback()
rollback the current transaction
h($p_string)
to protect again bad characters which can lead to a cross scripting attack the string to be diplayed ...
This class allow you to connect to the postgresql database, execute sql, retrieve data...
static fetch_result($ret, $p_row=0, $p_col=0)
wrapper for the function pg_fetch_all
get_row($p_sql, $p_array=NULL)
Returns only one row from a query.
static connect($p_user, $p_password, $p_dbname, $p_host, $p_port)
Connect to a database return an connx to db or false if it fails.
exist_column($col, $table, $schema)
Check if a column exists in a table.
__construct($p_database_id=0, $p_type='dos')
constructor
prepare($p_string, $p_sql)
wrapper for the function pg_prepare
make_array($p_sql, $p_null=0, $p_array=null)
make a array with the sql.
count($p_ret=null)
synomym for size()
alter_seq($p_name, $min)
alter the sequence value
exist_blob($p_oid)
check if the large object exists
get_value($p_sql, $p_array=null)
return the value of the sql, the sql will return only one value with the value
get_array($p_sql, $p_array=null)
purpose return the result of a sql statment in a array
exist_database($p_name)
Count the database name in a system view.