noalyss Version-9
Public Member Functions
Database Class Reference

contains the class for connecting to Noalyss More...

+ Inheritance diagram for Database:
+ Collaboration diagram for Database:

Public Member Functions

 __construct ($p_database_id=0, $p_type='dos')
 constructor More...
 
 apply_patch ($p_name)
 loop to apply all the path to a folder or a template Upgrade check if the folder $p_name needs to be upgrade thanks the variable DBVERSION and run all the SQL script named upgradeX.sql from the folder noalyss/include/sql/patch until X equal DBVERSION-1 More...
 
 format_name ($p_id, $p_type)
 return the name of the database with the domain name More...
 
 get_version ()
 Get version of a database, the content of the table version. More...
 
 save_receipt ($seq)
 
- Public Member Functions inherited from DatabaseCore
 __construct ($p_user, $p_password, $p_dbname, $p_host, $p_port)
 
 __toString ()
 
 alter_seq ($p_name, $min)
 alter the sequence value More...
 
 clean_orphan_lob ()
 Find all lob and remove those which are not used by any tables. More...
 
 clear_all_prepare ()
 clear all prepare stmt More...
 
 clear_prepare ($sql_name)
 clear a prepare stmt More...
 
 close ()
 wrapper for the function pg_close More...
 
 commit ()
 Commit the transaction. More...
 
 count ($p_ret=null)
 synomym for size() More...
 
 count_sql ($p_sql, $p_array=null)
 Count the number of row returned by a sql statement. More...
 
 create_sequence ($p_name, $min=1)
 Create a sequence. More...
 
 exec_sql ($p_string, $p_array=null)
 send a sql string to the database More...
 
 execute ($p_string, $p_array)
 wrapper for the function pg_execute More...
 
 execute_script ($script)
 Execute a sql script. More...
 
 exist_blob ($p_oid)
 check if the large object exists More...
 
 exist_column ($col, $table, $schema)
 Check if a column exists in a table. More...
 
 exist_database ($p_name)
 Count the database name in a system view. More...
 
 exist_schema ($p_name)
 
 exist_sequence ($p_name)
 test if a sequence exist More...
 
 exist_table ($p_name, $p_schema='public')
 test if a table exist More...
 
 exist_view ($p_name)
 
 fetch ($p_indice)
 fetch the $p_indice array from the last query More...
 
 get_affected ()
 return the number of rows affected by the previous query More...
 
 get_array ($p_sql, $p_array=null)
 purpose return the result of a sql statment in a array More...
 
 get_current_seq ($p_seq)
 get the current sequence value More...
 
 get_db ()
 
 get_encoding ()
 
 get_is_open ()
 
 get_name ()
 return the name of the current database More...
 
 get_next_seq ($p_seq)
 get the next sequence value More...
 
 get_ret ()
 
 get_row ($p_sql, $p_array=NULL)
 Returns only one row from a query. More...
 
 get_sql ()
 last SQL stmt executed More...
 
 get_value ($p_sql, $p_array=null)
 return the value of the sql, the sql will return only one value with the value More...
 
 is_prepare ($query_name)
 Check if a prepared statement already exists or not. More...
 
 lo_export ($p_oid, $tmp_file)
 wrapper for the function pg_lo_export More...
 
 lo_import ($p_filename)
 wrapper for the function pg_lo_export More...
 
 lo_unlink ($p_oid)
 wrapper for the function pg_lo_unlink More...
 
 make_array ($p_sql, $p_null=0, $p_array=null)
 make a array with the sql. More...
 
 make_list ($sql, $p_array=null)
 create a string containing the value separated by comma for use in a SQL in statement More...
 
 prepare ($p_string, $p_sql)
 wrapper for the function pg_prepare More...
 
 query_to_csv ($ret, $aheader)
 with the handle of a successull query, echo each row into CSV and send it directly More...
 
 rollback ()
 rollback the current transaction More...
 
 search_sql_inject ($p_sql)
 FInd if a SQL Select has a SQL stmt to inject or damage Data When a SELECT SQL string is build, this string could contain a SQL attempt to damage data, so the statement DELETE TRUNCATE ... are forbidden. More...
 
 set_db ($db)
 
 set_encoding ($p_charset)
 
 set_is_open ($is_open)
 
 set_ret ($ret)
 
 set_sql ($sql)
 last SQL stmt executed More...
 
 size ($p_ret=null)
 return the number of rows found by the last query, or the number of rows from $p_ret More...
 
 start ()
 : start a transaction More...
 
 status ()
 get the transaction status : The status can be More...
 
 upload ($p_name)
 
 verify ()
 

Additional Inherited Members

- Static Public Member Functions inherited from DatabaseCore
static escape_string ($p_string)
 wrapper for the function pg_escape_string More...
 
static fetch_all ($ret)
 wrapper for the function pg_fetch_all More...
 
static fetch_array ($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
 wrapper for the function pg_fetch_array More...
 
static fetch_result ($ret, $p_row=0, $p_col=0)
 wrapper for the function pg_fetch_all More...
 
static fetch_row ($ret, $p_row)
 wrapper for the function pg_fetch_row More...
 
static nb_column ($p_ret)
 Returns the number of columns in a ret. More...
 
static num_row ($ret)
 wrapper for the function pg_num_rows More...
 
static test_me ()
 
- Data Fields inherited from DatabaseCore
 $array
 
 $sql
 last SQL stmt executed More...
 

Detailed Description

contains the class for connecting to Noalyss

Definition at line 32 of file database.class.php.

Constructor & Destructor Documentation

◆ __construct()

Database::__construct (   $p_database_id = 0,
  $p_type = 'dos' 
)

constructor

Parameters
$p_database_idis the id of the dossier, or the modele following the p_type if = 0 then connect to the repository
$p_typeis 'DOS' (defaut) for dossier or 'MOD'

Definition at line 40 of file database.class.php.

41 {
42 if (IsNumber($p_database_id) == false || strlen($p_database_id??"") > 10)
43 die("-->Dossier invalide [$p_database_id]");
44 $noalyss_user = (defined("noalyss_user")) ? noalyss_user : phpcompta_user;
45 $password = (defined("noalyss_password")) ? noalyss_password : phpcompta_password;
46 $port = (defined("noalyss_psql_port")) ? noalyss_psql_port : phpcompta_psql_port;
47 $host = (!defined("noalyss_psql_host")) ? '127.0.0.1' : noalyss_psql_host;
48 if (defined("MULTI") && MULTI == "0") {
49 $l_dossier = dbname;
50 } else {
51
52 if ($p_database_id == 0) { /* connect to the repository */
53 $l_dossier = sprintf("%saccount_repository", strtolower(domaine));
54 } else if ($p_type == 'dos') { /* connect to a folder (dossier) */
55 $l_dossier = sprintf("%sdossier%d", strtolower(domaine), $p_database_id);
56 } else if ($p_type == 'mod') { /* connect to a template (modele) */
57 $l_dossier = sprintf("%smod%d", strtolower(domaine), $p_database_id);
58 } else if ($p_type == 'template') {
59 $l_dossier = 'template1';
60 } else {
61 throw new Exception('Connection invalide');
62 }
63 }
64
65 parent::__construct($noalyss_user, $password, $l_dossier, $host, $port);
66
67 if ($this->exist_schema('comptaproc')) {
68 $this->exec_sql('set search_path to public,comptaproc,pg_catalog;');
69 }
70 $this->exec_sql('set DateStyle to ISO, MDY;');
71
72 }
exec_sql($p_string, $p_array=null)
send a sql string to the database
domaine
Definition: install.php:370

References $p_type, domaine, DatabaseCore\exec_sql(), and DatabaseCore\exist_schema().

+ Here is the call graph for this function:

Member Function Documentation

◆ apply_patch()

Database::apply_patch (   $p_name)

loop to apply all the path to a folder or a template Upgrade check if the folder $p_name needs to be upgrade thanks the variable DBVERSION and run all the SQL script named upgradeX.sql from the folder noalyss/include/sql/patch until X equal DBVERSION-1

Parameters
$p_namedatabase name

Bug in parm_code for FRANCE

Definition at line 127 of file database.class.php.

128 {
129 if (!$this->exist_table('version')) {
130 echo _('Base de donnée vide');
131 return;
132 }
133 $MaxVersion = DBVERSION - 1;
134 $succeed = "<span style=\"font-size:18px;color:green\">&#x2713;</span>";
135 echo '<ul style="list-type-style:square">';
136 for ($i = 4; $i <= $MaxVersion; $i++) {
137 $to = $i + 1;
138 if ($this->get_version() <= $i) {
139 if ($this->get_version() == 97) {
140 if ($this->exist_schema("amortissement")) {
141 $this->exec_sql('ALTER TABLE amortissement.amortissement_histo
142 ADD CONSTRAINT internal_fk FOREIGN KEY (jr_internal) REFERENCES jrn (jr_internal)
143 ON UPDATE CASCADE ON DELETE SET NULL');
144 }
145 }
146 echo "<li>Patching " . $p_name .
147 " from the version " . $this->get_version() . " to $to ";
148
149 $this->execute_script(NOALYSS_INCLUDE . '/sql/patch/upgrade' . $i . '.sql');
150 echo $succeed;
151
152 if ( DEBUGNOALYSS == 0)
153 ob_start();
154 // specific for version 4
155 if ($i == 4) {
156 $sql = "select jrn_def_id from jrn_def ";
157 $Res = $this->exec_sql($sql);
158 $Max = $this->size();
159 for ($seq = 0; $seq < $Max; $seq++) {
160 $row = pg_fetch_array($Res, $seq);
161 $sql = sprintf("create sequence s_jrn_%d", $row['jrn_def_id']);
162 $this->exec_sql($sql);
163 }
164 }
165 // specific to version 7
166 if ($i == 7) {
167 // now we use sequence instead of computing a max
168 //
169 $Res2 = $this->exec_sql('select coalesce(max(jr_grpt_id),1) as l from jrn');
170 $Max2 = pg_num_rows($Res2);
171 if ($Max2 == 1) {
172 $Row = pg_fetch_array($Res2, 0);
173 var_dump($Row);
174 $M = $Row['l'];
175 $this->exec_sql("select setval('s_grpt',$M,true)");
176 }
177 }
178 // specific to version 17
179 if ($i == 17) {
180 $this->execute_script(NOALYSS_INCLUDE . '/sql/patch/upgrade17.sql');
181 $max = $this->get_value('select last_value from s_jnt_fic_att_value');
182 $this->alter_seq('s_jnt_fic_att_value', $max + 1);
183 } // version
184 // reset sequence in the modele
185 //--
186 if ($i == 30 && $p_name == "mod") {
187 $a_seq = array('s_jrn', 's_jrn_op', 's_centralized',
188 's_stock_goods', 'c_order', 's_central');
189 foreach ($a_seq as $seq) {
190 $sql = sprintf("select setval('%s',1,false)", $seq);
191 $Res = $this->exec_sql($sql);
192 }
193 $sql = "select jrn_def_id from jrn_def ";
194 $Res = $this->exec_sql($sql);
195 $Max = pg_num_rows($Res);
196 for ($seq = 0; $seq < $Max; $seq++) {
197 $row = pg_fetch_array($Res, $seq);
198 $sql = sprintf("select setval('s_jrn_%d',1,false)", $row['jrn_def_id']);
199 $this->exec_sql($sql);
200 }
201 }
202 if ($i == 36) {
203 /* check the country and apply the path */
204 $res = $this->exec_sql("select pr_value from parameter where pr_id='MY_COUNTRY'");
205 $country = pg_fetch_result($res, 0, 0);
206 $this->execute_script(NOALYSS_INCLUDE . "/sql/patch/upgrade36." . $country . ".sql");
207 $this->exec_sql('update tmp_pcmn set pcm_type=find_pcm_type(pcm_val)');
208 }
209 if ($i == 59) {
210 $res = $this->exec_sql("select pr_value from parameter where pr_id='MY_COUNTRY'");
211 $country = pg_fetch_result($res, 0, 0);
212 if ($country == 'BE')
213 $this->exec_sql("insert into parm_code values ('SUPPLIER',440,'Poste par défaut pour les fournisseurs')");
214 if ($country == 'FR')
215 $this->exec_sql("insert into parm_code values ('SUPPLIER',400,'Poste par défaut pour les fournisseurs')");
216 }
217 if ($i == 61) {
218 $country = $this->get_value("select pr_value from parameter where pr_id='MY_COUNTRY'");
219 $this->execute_script(NOALYSS_INCLUDE . "/sql/patch/upgrade61." . $country . ".sql");
220 }
221 /**
222 * Bug in parm_code for FRANCE
223 */
224 if ($i == 141 ) {
225 $country = $this->get_value("select pr_value from parameter where pr_id='MY_COUNTRY'");
226 $this->execute_script(NOALYSS_INCLUDE . "/sql/patch/upgrade141." . $country . ".sql");
227 }
228
229 if (DEBUGNOALYSS == 0 )
230 ob_end_clean();
231
232 }
233 }
234 echo '</ul>';
235 }
$to
Definition: balance.inc.php:75
alter_seq($p_name, $min)
alter the sequence value
$sql
last SQL stmt executed
get_value($p_sql, $p_array=null)
return the value of the sql, the sql will return only one value with the value
size($p_ret=null)
return the number of rows found by the last query, or the number of rows from $p_ret
execute_script($script)
Execute a sql script.
exist_table($p_name, $p_schema='public')
test if a table exist
get_version()
Get version of a database, the content of the table version.
const DBVERSION
Definition: constant.php:31
$Res
$succeed
Definition: install.php:183

References $i, $max, $Max, $res, $Res, $row, $seq, DatabaseCore\$sql, $succeed, $to, DatabaseCore\alter_seq(), DBVERSION, DatabaseCore\exec_sql(), DatabaseCore\execute_script(), DatabaseCore\exist_schema(), DatabaseCore\exist_table(), DatabaseCore\get_value(), get_version(), and DatabaseCore\size().

+ Here is the call graph for this function:

◆ format_name()

Database::format_name (   $p_id,
  $p_type 
)

return the name of the database with the domain name

Parameters
$p_idof the folder WITHOUT the domain name
$p_typedos for folder mod for template
Returns
formatted name

Definition at line 243 of file database.class.php.

244 {
245 switch ($p_type) {
246 case 'dos':
247 $sys_name = sprintf("%sdossier%d", strtolower(domaine), $p_id);
248 break;
249 case 'mod':
250 $sys_name = sprintf("%smod%d", strtolower(domaine), $p_id);
251 break;
252 default:
253 echo_error(__FILE__ . " format_name invalid type " . $p_type, __LINE__);
254 throw new Exception(__FILE__ . " format_name invalid type " . $p_type . __LINE__);
255 }
256 return $sys_name;
257 }
echo_error($p_log, $p_line="", $p_message="")
log error into the /tmp/noalyss_error.log it doesn't work on windows
Definition: ac_common.php:169

References $p_id, $p_type, domaine, and echo_error().

+ Here is the call graph for this function:

◆ get_version()

Database::get_version ( )

Get version of a database, the content of the table version.

Returns
version number

Definition at line 111 of file database.class.php.

112 {
113 $Res = $this->get_value("select max(val) from version");
114 return $Res;
115 }

References $Res, and DatabaseCore\get_value().

Referenced by apply_patch().

+ Here is the call graph for this function:

◆ save_receipt()

Database::save_receipt (   $seq)

Definition at line 82 of file database.class.php.

83 {
84 $oid = $this->upload('pj');
85 if ($oid == false) {
86 return false;
87 }
88 // Remove old document
89 $ret = $this->exec_sql("select jr_pj from jrn where jr_grpt_id=$seq");
90 if (pg_num_rows($ret) != 0) {
91 $r = pg_fetch_array($ret, 0);
92 $old_oid = $r['jr_pj'];
93 if (strlen($old_oid??"") != 0)
94 $this->lo_unlink( $old_oid);
95 }
96 // Load new document
97 $this->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2,
98 jr_pj_type=$3 where jr_grpt_id=$4",
99 array($oid, $_FILES['pj']['name'], $_FILES['pj']['type'], $seq));
100 return $oid;
101 }
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
lo_unlink($p_oid)
wrapper for the function pg_lo_unlink

References $r, DatabaseCore\$ret, $seq, DatabaseCore\exec_sql(), DatabaseCore\lo_unlink(), and DatabaseCore\upload().

+ Here is the call graph for this function:

The documentation for this class was generated from the following file: