noalyss Version-9
Public Member Functions | Static Public Member Functions | Data Fields | Private Attributes
DatabaseCore Class Reference

This class allow you to connect to the postgresql database, execute sql, retrieve data. More...

+ Inheritance diagram for DatabaseCore:
+ Collaboration diagram for DatabaseCore:

Public Member Functions

 __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 ()
 

Static Public Member Functions

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

 $array
 
 $sql
 last SQL stmt executed More...
 

Private Attributes

 $db
 database connection More...
 
 $is_open
 
 $ret
 return value
More...
 

Detailed Description

This class allow you to connect to the postgresql database, execute sql, retrieve data.

Definition at line 35 of file database_core.class.php.

Constructor & Destructor Documentation

◆ __construct()

DatabaseCore::__construct (   $p_user,
  $p_password,
  $p_dbname,
  $p_host,
  $p_port 
)

Definition at line 54 of file database_core.class.php.

55 {
56 $this->db = @pg_connect("dbname=$p_dbname host='$p_host' user='$p_user'
57 password='$p_password' port=$p_port");
58 if ($this->db == false) {
59 if ( DEBUGNOALYSS > 0 ) {
60
61 echo '<h2 class="error">'._('Impossible de se connecter à postgreSql').'</h2>';
62 echo '<p>';
63 echo _("Vos paramètres sont incorrectes").": <br>";
64 echo "<br>";
65 printf (_("base de donnée = %s"), $p_dbname)."<br>";
66 printf (_("Port %s"),$p_port )."<br>";
67 printf ( _("Utilisateur : %s"),$p_user )."<br>";
68 echo '</p>';
69
70 die();
71 } else {
72 echo '<h2 class="error">' . _('Erreur de connexion !') . '</h2>';
73 $this->is_open = false;
74 throw new Exception(_('Erreur Connexion'));
75 }
76 }
77
78 $this->is_open = TRUE;
79 $this->sql="";
80
81 }
$SecUser db

References db.

Member Function Documentation

◆ __toString()

DatabaseCore::__toString ( )
Parameters

return

Note
See also

Definition at line 889 of file database_core.class.php.

890 {
891 return "database ";
892 }

◆ alter_seq()

DatabaseCore::alter_seq (   $p_name,
  $min 
)

alter the sequence value

Parameters
$p_namename of the sequence
$minthe start value of the sequence

Definition at line 292 of file database_core.class.php.

293 {
294 if ($min < 1)
295 $min = 1;
296 $Res = $this->exec_sql("alter sequence $p_name restart $min");
297 }
exec_sql($p_string, $p_array=null)
send a sql string to the database
$Res

References $min, $Res, and exec_sql().

Referenced by Database\apply_patch().

+ Here is the call graph for this function:

◆ clean_orphan_lob()

DatabaseCore::clean_orphan_lob ( )

Find all lob and remove those which are not used by any tables.

Definition at line 920 of file database_core.class.php.

921 {
922 // find all columns of type lob
923 $sql = "
924 select table_schema,table_name,column_name
925 from
926 information_schema.columns
927 where table_schema not in ('information_schema','pg_catalog')
928 and data_type='oid'";
929 $all_lob = "
930 select oid,'N' as used from pg_largeobject_metadata
931 ";
932 $a_table = $this->get_array($sql);
933 $a_lob = $this->get_array($all_lob);
934 if ($a_table == false || $a_lob == false) return;
935 // for each lob
936 $nb_lob = count($a_lob);
937 $nb_table = count($a_table);
938 for ($i = 0; $i < $nb_lob; $i++) {
939 $lob = $a_lob[$i]['oid'];
940 if ($a_lob[$i]['used'] == 'Y') continue;
941 for ($j = 0; $j < $nb_table; $j++) {
942 if ($a_lob[$i]['used'] == 'Y') continue;
943 $check = $this->get_value(" select count(*) from " .
944 $a_table[$j]['table_schema'] . "." . $a_table[$j]['table_name'] .
945 " where " .
946 $a_table[$j]['column_name'] . "=$1", array($lob));
947 if ($check != 0)
948 $a_lob[$i]['used'] = 'Y';
949
950 }
951 }
952 for ($i = 0; $i < $nb_lob; $i++) {
953 if ($a_lob[$i]['used'] == 'Y') continue;
954 $this->lo_unlink($a_lob[$i]['oid']);
955 }
956 }
get_array($p_sql, $p_array=null)
purpose return the result of a sql statment in a array
$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
count($p_ret=null)
synomym for size()
lo_unlink($p_oid)
wrapper for the function pg_lo_unlink
$check

References $check, $i, $sql, count(), get_array(), get_value(), and lo_unlink().

+ Here is the call graph for this function:

◆ clear_all_prepare()

DatabaseCore::clear_all_prepare ( )

clear all prepare stmt

See also
DatabaseCore::is_prepare
DatabaseCore::execute
DatabaseCore::prepare

Definition at line 1045 of file database_core.class.php.

1046 {
1047 pg_exec($this->db,'DEALLOCATE ALL');
1048 }

References db.

◆ clear_prepare()

DatabaseCore::clear_prepare (   $sql_name)

clear a prepare stmt

See also
DatabaseCore::is_prepare
DatabaseCore::execute
DatabaseCore::prepare
Parameters
$sql_namename of the prepare SQL

Definition at line 1035 of file database_core.class.php.

1036 {
1037 pg_exec($this->db,sprintf('DEALLOCATE "%s"'),DatabaseCore::escape_string($sql_name));
1038 }
static escape_string($p_string)
wrapper for the function pg_escape_string

References db, and escape_string().

+ Here is the call graph for this function:

◆ close()

DatabaseCore::close ( )

wrapper for the function pg_close

Definition at line 875 of file database_core.class.php.

876 {
877 if ($this->is_open) pg_close($this->db);
878 $this->is_open = FALSE;
879 }

References db.

◆ commit()

DatabaseCore::commit ( )

Commit the transaction.

Definition at line 272 of file database_core.class.php.

273 {
274 if (!$this->is_open) return;
275 $Res = $this->exec_sql("commit");
276 }

References $Res, and exec_sql().

+ Here is the call graph for this function:

◆ count()

DatabaseCore::count (   $p_ret = null)

synomym for size()

Definition at line 417 of file database_core.class.php.

418 {
419 return $this->size($p_ret);
420 }
size($p_ret=null)
return the number of rows found by the last query, or the number of rows from $p_ret

References size().

Referenced by clean_orphan_lob(), get_row(), make_list(), and query_to_csv().

+ Here is the call graph for this function:

◆ count_sql()

DatabaseCore::count_sql (   $p_sql,
  $p_array = null 
)

Count the number of row returned by a sql statement.

Parameters
$p_sqlsql string
$p_arrayif not null we use the safer pg_query_params

Definition at line 232 of file database_core.class.php.

233 {
234 $r_sql = $this->exec_sql($p_sql, $p_array);
235 return pg_num_rows($r_sql);
236 }

References $p_array, and exec_sql().

Referenced by exist_schema(), exist_sequence(), exist_table(), and exist_view().

+ Here is the call graph for this function:

◆ create_sequence()

DatabaseCore::create_sequence (   $p_name,
  $min = 1 
)

Create a sequence.

Parameters
string$p_nameSequence Name
int$minstarting value

Definition at line 502 of file database_core.class.php.

503 {
504 if ($min < 1)
505 $min = 1;
506 $sql = "create sequence " . $p_name . " minvalue $min";
507 $this->exec_sql($sql);
508 }

References $min, $sql, and exec_sql().

+ Here is the call graph for this function:

◆ escape_string()

static DatabaseCore::escape_string (   $p_string)
static

wrapper for the function pg_escape_string

Parameters
$p_stringis the string to escape
Returns
escaped string

Definition at line 864 of file database_core.class.php.

865 {
866 static $cn=null;
867 if ( $cn==null) $cn=new Database();
868 return pg_escape_string($cn->db,$p_string);
869 }
contains the class for connecting to Noalyss

References $cn.

Referenced by clear_prepare(), Noalyss_User\connect_user(), Acc_Account_Ledger\get_used_accounting(), Anc_Acc_List\load_anc_account(), Anc_Acc_List\load_anc_card(), Anc_Acc_List\load_card(), Anc_Table\load_card(), Anc_Acc_List\load_poste(), Anc_Table\load_poste(), Follow_Up\myList(), and Noalyss_User\set_session_var().

◆ exec_sql()

DatabaseCore::exec_sql (   $p_string,
  $p_array = null 
)

send a sql string to the database

Parameters
$p_stringsql string
$p_arrayarray for the SQL string (see pg_query_params)
Returns
the result of the query, a resource or false if an error occured

Definition at line 180 of file database_core.class.php.

181 {
182 try {
183 if (!$this->is_open) throw new Exception(' Database is closed');
184 $this->sql = $p_string;
185 $this->array = $p_array;
186
187 if ($p_array == null) {
188 if ( DEBUGNOALYSS == 0 )
189 $this->ret = pg_query($this->db, $p_string);
190 else
191 $this->ret = @pg_query($this->db, $p_string);
192 } else {
193 $a = is_array($p_array);
194 if (!is_array($p_array)) {
195 throw new Exception(_("Erreur : exec_sql attend un array"));
196 }
197 if ( DEBUGNOALYSS == 0 )
198 $this->ret =@pg_query_params($this->db, $p_string, $p_array);
199 else
200 $this->ret = pg_query_params($this->db, $p_string, $p_array);
201 }
202 if ($this->ret == false) {
203 $str_error = pg_last_error($this->db) ;
204 throw new Exception(" SQL ERROR $p_string " . $str_error, 1);
205 }
206 } catch (Exception $a) {
207 if ( DEBUGNOALYSS > 0 ) {
208 print_r($p_string);
209 print_r($p_array);
210 echo $a->getMessage();
211 echo $a->getTraceAsString();
212 echo pg_last_error($this->db);
213 }
214 record_log($a);
215 record_log($p_string);
217 $this->rollback();
218
219 throw ($a);
220 }
221
222 return $this->ret;
223 }
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1342
rollback()
rollback the current transaction

References $a, $p_array, $ret, db, record_log(), and rollback().

Referenced by Database\__construct(), alter_seq(), Database\apply_patch(), Extension\clean(), commit(), count_sql(), create_sequence(), execute_script(), get_array(), get_next_seq(), get_value(), Sendmail\increment_mail(), make_array(), rollback(), Database\save_receipt(), and start().

+ Here is the call graph for this function:

◆ execute()

DatabaseCore::execute (   $p_string,
  $p_array 
)

wrapper for the function pg_execute

Parameters
$p_stringstring name of the stmt given in pg_prepare function
$p_arraycontains the variables
Note
set this->ret to the return of pg_execute
Returns
return the result of the operation,

Definition at line 828 of file database_core.class.php.

829 {
830 $this->ret = pg_execute($this->db, $p_string, $p_array);
831 return $this->ret;
832 }

References $p_array, $ret, and db.

◆ execute_script()

DatabaseCore::execute_script (   $script)

Execute a sql script.

Parameters
$scriptscript name

Definition at line 304 of file database_core.class.php.

305 {
306
307 if ( DEBUGNOALYSS == 0 ) {
308 ob_start();
309 } else {
310 $debug = fopen("/tmp/debug_execute_script".uniqid().".log", "w+");
311 }
312 $hf = fopen($script, 'r');
313 if ($hf == false) {
314 throw new Exception ('Ne peut ouvrir ' . $script);
315 }
316 printf (" open %s <br>", $script);
317 $sql = "";
318 $flag_function = false;
319 while (!feof($hf)) {
320 $buffer = fgets($hf);
321 $buffer = str_replace('$BODY$', '$_$', $buffer);
322 print $buffer . "<br>";
323 // comment are not execute
324 if (substr($buffer, 0, 2) == "--") {
325 //echo "comment $buffer";
326 continue;
327 }
328 // Blank Lines Are Skipped
329 If (Strlen($buffer) == 0) {
330 //echo "Blank $buffer";
331 Continue;
332 }
333 if (strpos(strtolower($buffer), "create function") === 0) {
334 echo "found a function";
335 $flag_function = true;
336 $sql = $buffer;
337 continue;
338 }
339 if (strpos(strtolower($buffer), "create or replace function") === 0) {
340 echo "found a function";
341 $flag_function = true;
342 $sql = $buffer;
343 continue;
344 }
345 // No semi colon -> multiline command
346 if ($flag_function == false && strpos($buffer, ';') == false) {
347 $sql .= $buffer;
348 continue;
349 }
350 if ($flag_function) {
351 if (strpos(strtolower($buffer), "$$;") === false &&
352 strpos(strtolower($buffer), '$_$;') === false &&
353 strpos(strtolower($buffer), '$function$;') === false &&
354 strpos(strtolower($buffer), 'language plpgsql;') === false &&
355 strpos(strtolower($buffer), 'language plpgsql ;') === false
356 ) {
357 $sql .= $buffer;
358 continue;
359 }
360 } else {
361 // cut the semi colon
362 $buffer = str_replace(';', '', $buffer);
363 }
364 $sql .= $buffer;
365 if ( DEBUGNOALYSS > 0 ) fwrite($debug, $sql);
366 if ($this->exec_sql($sql) == false) {
367
368 $this->rollback();
369 if ( DEBUGNOALYSS == 0 )
370 ob_end_clean();
371 print "ERROR : $sql";
372 throw new Exception("ERROR : $sql");
373 }
374 $sql = "";
375 $flag_function = false;
376 print "<hr>";
377 } // while (feof)
378 fclose($hf);
379 if ( DEBUGNOALYSS == 0 )
380 ob_end_clean();
381 }
print
Type of printing.
$script
Definition: popup.php:125

References $script, $sql, exec_sql(), print, and rollback().

Referenced by Database\apply_patch().

+ Here is the call graph for this function:

◆ exist_blob()

DatabaseCore::exist_blob (   $p_oid)

check if the large object exists

Parameters
$p_oidof the large object
Returns
return true if the large obj exist or false if not

Definition at line 571 of file database_core.class.php.

572 {
573 $r = $this->get_value('select count(*) from pg_largeobject_metadata where oid=$1'
574 , array($p_oid));
575 if ($r > 0)
576 return true;
577 else
578 return false;
579 }
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r

References $r, and get_value().

Referenced by lo_unlink().

+ Here is the call graph for this function:

◆ exist_column()

DatabaseCore::exist_column (   $col,
  $table,
  $schema 
)

Check if a column exists in a table.

Parameters
$col: column name
$table:table name
$schema:schema name, default public
Returns
true or false

Definition at line 545 of file database_core.class.php.

546 {
547 $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));
548 if ($r > 0)
549 return true;
550 return false;
551 }
$table
Definition: menu.inc.php:103

References $r, $table, and get_value().

+ Here is the call graph for this function:

◆ exist_database()

DatabaseCore::exist_database (   $p_name)

Count the database name in a system view.

Parameters
$p_namestring database name
Returns
number of database found (normally 0 or 1)

Definition at line 559 of file database_core.class.php.

560 {
561 $database_exist = $this->get_value('select count(*)
562 from pg_catalog.pg_database where datname = lower($1)', array($p_name));
563 return $database_exist;
564 }

References get_value().

+ Here is the call graph for this function:

◆ exist_schema()

DatabaseCore::exist_schema (   $p_name)

Definition at line 599 of file database_core.class.php.

600 {
601 $r = $this->count_sql("select nspname from pg_namespace where nspname=lower($1)", array($p_name));
602 if ($r == 0)
603 return false;
604 return true;
605 }
count_sql($p_sql, $p_array=null)
Count the number of row returned by a sql statement.

References $r, and count_sql().

Referenced by Database\__construct(), Database\apply_patch(), and Extension\clean().

+ Here is the call graph for this function:

◆ exist_sequence()

DatabaseCore::exist_sequence (   $p_name)

test if a sequence exist

Definition at line 515 of file database_core.class.php.

516 {
517 $r = $this->count_sql("select relname from pg_class where relname=lower($1)", array($p_name));
518 if ($r == 0)
519 return false;
520 return true;
521 }

References $r, and count_sql().

+ Here is the call graph for this function:

◆ exist_table()

DatabaseCore::exist_table (   $p_name,
  $p_schema = 'public' 
)

test if a table exist

Parameters
$p_nametable name
$schemaname of the schema default public
Returns
true if a table exist otherwise false

Definition at line 530 of file database_core.class.php.

531 {
532 $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));
533 if ($r == 0)
534 return false;
535 return true;
536 }

References $r, and count_sql().

Referenced by Database\apply_patch().

+ Here is the call graph for this function:

◆ exist_view()

DatabaseCore::exist_view (   $p_name)

Definition at line 586 of file database_core.class.php.

587 {
588 $r = $this->count_sql("select viewname from pg_views where viewname=lower($1)", array($p_name));
589 if ($r == 0)
590 return false;
591 return true;
592 }

References $r, and count_sql().

+ Here is the call graph for this function:

◆ fetch()

DatabaseCore::fetch (   $p_indice)

fetch the $p_indice array from the last query

Parameters
$p_indiceindex

Definition at line 389 of file database_core.class.php.

390 {
391 if ($this->ret == false)
392 throw new Exception('this->ret is empty');
393 return pg_fetch_array($this->ret, $p_indice,PGSQL_ASSOC);
394 }

◆ fetch_all()

static DatabaseCore::fetch_all (   $ret)
static

◆ fetch_array()

static DatabaseCore::fetch_array (   $ret,
  $p_indice = 0,
  $p_mode = PGSQL_ASSOC 
)
static

wrapper for the function pg_fetch_array

Parameters
$retis the result of a pg_exec
$p_indiceis the index
$p_indiceis the index
Returns
$array of column

Definition at line 757 of file database_core.class.php.

758 {
759 return pg_fetch_array($ret, $p_indice,$p_mode);
760 }

References $ret.

Referenced by Noalyss_Parameter_Folder\__construct(), Card_Property\build_input(), Noalyss_User\Check(), Document_modele\Delete(), Manage_Table_SQL\display_table(), Periode_Ledger_Table\display_table(), Fiche_Def\DisplayAttribut(), Print_Ledger_Detail_Item\export(), Print_Ledger_Simple\export(), Print_Ledger_Simple_Without_Vat\export(), Acc_Ledger_History_Purchase\export_csv(), Acc_Ledger_History_Sale\export_csv(), Follow_Up\export_csv_detail(), Document\export_file(), Acc_Operation\find_tiers(), Document\generate(), Acc_Reconciliation\get(), Anc_Plan\get(), Document\get(), Document_Type\get(), Fiche_Def\get(), Fiche_Def\get_all(), Acc_Reconciliation\get_amount_noautovat(), Fiche_Def\get_attr_min(), Fiche\get_bk_balance(), Customer\get_by_account(), Supplier\get_by_account(), Fiche_Def\get_by_category(), Fiche\get_by_category(), Acc_Operation\get_data(), Periode\get_date_limit(), Acc_Operation\get_internal(), Acc_Account_Ledger\get_name(), Acc_Ledger\get_name(), Acc_Report\get_row(), Fiche\get_row_result_deprecated(), Acc_Account_Ledger\get_row_sql_deprecated(), Acc_Account_Ledger\get_solde(), Acc_Account_Ledger\get_solde_detail(), Fiche\get_solde_detail(), Acc_Ledger\get_type(), Fiche_Def\getAttribut(), html_min_page_start(), html_page_start(), isValid_deprecrated(), Acc_Ledger_Search\list_operation(), Acc_Ledger_Search\list_operation_to_reconcile(), Acc_Ledger\listing(), Acc_Bilan\load(), Acc_Ledger_Info\load(), Acc_Parm_Code\load(), Acc_Payment\load(), Anc_Group\load(), Dossier\load(), Forecast\load(), Noalyss_User\load(), Todo_List\load(), Card_Property\load(), Fiche_Def\myList(), query_to_csv(), Document_modele\Save(), Document\send(), Profile_Menu\sub_menu(), Document_modele\update(), Card_Property\update(), Anc_Operation\update_from_jrnx(), Dossier\upgrade(), Customer\VatListing(), Acc_Bilan\verify(), and Acc_Bilan\warning().

◆ fetch_result()

static DatabaseCore::fetch_result (   $ret,
  $p_row = 0,
  $p_col = 0 
)
static

wrapper for the function pg_fetch_all

Parameters
$retis the result of pg_exec (exec_sql)
$p_rowis the indice of the row
$p_colis the indice of the col
Returns
a string or an integer

Definition at line 781 of file database_core.class.php.

782 {
783 return pg_fetch_result($ret, $p_row, $p_col);
784 }

References $p_col, and $ret.

Referenced by Fiche\empty_attribute(), Print_Ledger_Financial\export(), Periode\find_periode(), Periode\get_exercice(), Acc_Account_Ledger\get_row(), Fiche\get_row(), Fiche\get_row_date(), Acc_Ledger_Info\insert(), Forecast\insert(), Todo_List\insert(), Periode\is_centralized(), Periode\is_closed(), Acc_Ledger_Info\search_id_internal(), and Acc_Operation\seek_internal().

◆ fetch_row()

static DatabaseCore::fetch_row (   $ret,
  $p_row 
)
static

wrapper for the function pg_fetch_row

Parameters
$retis the result of pg_exec (exec_sql)
$p_rowis the indice of the row
Returns
an array indexed from 0

Definition at line 792 of file database_core.class.php.

793 {
794 return pg_fetch_row($ret, $p_row);
795 }

References $ret.

Referenced by Html_Table\sql2table().

◆ get_affected()

DatabaseCore::get_affected ( )

return the number of rows affected by the previous query

Definition at line 459 of file database_core.class.php.

460 {
461 return Database::num_row($this->ret);
462 }
static num_row($ret)
wrapper for the function pg_num_rows

References num_row().

+ Here is the call graph for this function:

◆ get_array()

DatabaseCore::get_array (   $p_sql,
  $p_array = null 
)

purpose return the result of a sql statment in a array

Parameters
$p_sqlsql query
$p_arrayif not null we use ExecSqlParam
Returns
false if nothing is found

Definition at line 472 of file database_core.class.php.

473 {
474 $r = $this->exec_sql($p_sql, $p_array);
475
476 if (pg_num_rows($r) == 0)
477 return array();
478 $array = pg_fetch_all($r);
479 return $array;
480 }

References $array, $p_array, $r, and exec_sql().

Referenced by clean_orphan_lob(), get_row(), and make_list().

+ Here is the call graph for this function:

◆ get_current_seq()

DatabaseCore::get_current_seq (   $p_seq)

get the current sequence value

Definition at line 242 of file database_core.class.php.

243 {
244 $Res = $this->get_value("select currval('$p_seq') as seq");
245 return $Res;
246 }

References $Res, and get_value().

+ Here is the call graph for this function:

◆ get_db()

DatabaseCore::get_db ( )
Returns
mixed

Definition at line 110 of file database_core.class.php.

111 {
112 return $this->db;
113 }
$db
database connection

References $db.

◆ get_encoding()

DatabaseCore::get_encoding ( )

Definition at line 101 of file database_core.class.php.

102 {
103 return pg_client_encoding($this->db);
104 }

References db.

◆ get_is_open()

DatabaseCore::get_is_open ( )
Returns
mixed

Definition at line 142 of file database_core.class.php.

143 {
144 return $this->is_open;
145 }

References $is_open.

◆ get_name()

DatabaseCore::get_name ( )

return the name of the current database

Returns
false|string

Definition at line 87 of file database_core.class.php.

87 {
88 return pg_dbname($this->ret);
89 }

◆ get_next_seq()

DatabaseCore::get_next_seq (   $p_seq)

get the next sequence value

Definition at line 252 of file database_core.class.php.

253 {
254 $Res = $this->exec_sql("select nextval('$p_seq') as seq");
255 $seq = pg_fetch_array($Res, 0);
256 return $seq['seq'];
257 }

References $Res, $seq, and exec_sql().

+ Here is the call graph for this function:

◆ get_ret()

DatabaseCore::get_ret ( )
Returns
mixed

Definition at line 126 of file database_core.class.php.

127 {
128 return $this->ret;
129 }

References $ret.

◆ get_row()

DatabaseCore::get_row (   $p_sql,
  $p_array = NULL 
)

Returns only one row from a query.

Parameters
string$p_sql
array$p_array
Returns
array , idx = column of the table or null if nothing is found
Exceptions
Exceptionif too many rows are found code 100

Definition at line 489 of file database_core.class.php.

490 {
491 $array = $this->get_array($p_sql, $p_array);
492 if (empty($array)) return null;
493 if (count($array) == 1) return $array[0];
494 throw new Exception(_("Database:get_row retourne trop de lignes"), 100);
495 }

References $array, $p_array, count(), and get_array().

+ Here is the call graph for this function:

◆ get_sql()

DatabaseCore::get_sql ( )

last SQL stmt executed

Parameters
string$sql

Definition at line 158 of file database_core.class.php.

159 {
160 return $this->sql;
161 }

References $sql.

◆ get_value()

DatabaseCore::get_value (   $p_sql,
  $p_array = null 
)

return the value of the sql, the sql will return only one value with the value

Parameters
$p_sqlthe sql stmt example :select s_value from document_state where s_id=2
$p_arrayif array is not null we use the ExecSqlParm (safer)
See also
exec_sql
Note
print a warning if several value are found, if only the first value is needed consider using a LIMIT clause
Returns
only the first value or an empty string if nothing is found

Definition at line 436 of file database_core.class.php.

437 {
438 try {
439 $this->ret = $this->exec_sql($p_sql, $p_array);
440 $r = pg_num_rows($this->ret);
441 if ($r == 0)
442 return "";
443 if ($r > 1) {
444 $array = pg_fetch_all($this->ret);
445 throw new Exception("Attention $p_sql retourne " . pg_num_rows($this->ret) . " valeurs " .
446 var_export($p_array, true) . " values=" . var_export($array, true));
447 }
448 $r = pg_fetch_row($this->ret, 0);
449 return $r[0];
450
451 } catch (Exception $ex) {
452 throw($ex);
453 }
454 }
$ex
Definition: balance.inc.php:45

References $array, $ex, $p_array, $r, and exec_sql().

Referenced by Acc_Account\__construct(), Database\apply_patch(), clean_orphan_lob(), exist_blob(), exist_column(), exist_database(), get_current_seq(), Sendmail\get_email_sent(), Sendmail\get_max_email(), Database\get_version(), and is_prepare().

+ Here is the call graph for this function:

◆ is_prepare()

DatabaseCore::is_prepare (   $query_name)

Check if a prepared statement already exists or not.

Parameters
string$query_namename of the prepared query
Returns
boolean false is not yet prepared

Definition at line 963 of file database_core.class.php.

964 {
965 $nb_prepared = $this->get_value("select count(*) from pg_prepared_statements where name=$1", [$query_name]);
966 if ($nb_prepared == 0) return FALSE;
967 return TRUE;
968 }

References get_value().

+ Here is the call graph for this function:

◆ lo_export()

DatabaseCore::lo_export (   $p_oid,
  $tmp_file 
)

wrapper for the function pg_lo_export

Parameters
$p_oidis the oid of the log
$tmp_fileis the file
Returns
result of the operation

Definition at line 841 of file database_core.class.php.

842 {
843 return pg_lo_export($this->db, $p_oid, $tmp_file);
844 }

References db.

◆ lo_import()

DatabaseCore::lo_import (   $p_filename)

wrapper for the function pg_lo_export

Parameters
$p_filenameis the filename
$tmpis the file
Returns
result of the operation

Definition at line 853 of file database_core.class.php.

854 {
855 return pg_lo_import($this->db, $p_filename);
856 }

References db.

◆ lo_unlink()

DatabaseCore::lo_unlink (   $p_oid)

wrapper for the function pg_lo_unlink

Parameters
$p_oidis the of oid
Returns
return the result of the operation

Definition at line 803 of file database_core.class.php.

804 {
805 if (!$this->exist_blob($p_oid)) return;
806 return pg_lo_unlink($this->db, $p_oid);
807 }
exist_blob($p_oid)
check if the large object exists

References db, and exist_blob().

Referenced by clean_orphan_lob(), and Database\save_receipt().

+ Here is the call graph for this function:

◆ make_array()

DatabaseCore::make_array (   $p_sql,
  $p_null = 0,
  $p_array = null 
)

make a array with the sql.

Parameters
$p_sqlsql statement, only the first two column will be returned in an array. The first col. is the label and the second the value
$p_nullif the array start with a null value Yes = 1 , No=0
$p_arrayis the array with the bind value
Note
this function is used with ISelect when it is needed to have a list of options.
Returns
: a double array like
* Array
* (
* [0] => Array
* (
* [value] => 1
* [label] => Marchandise A
* )
*
* [1] => Array
* (
* [value] => 2
* [label] => Marchandise B
* )
*
* [2] => Array
* (
* [value] => 3
* [label] => Marchandise C
* )
* )
* 
See also
ISelect

Definition at line 671 of file database_core.class.php.

672 {
673 $a = $this->exec_sql($p_sql, $p_array);
674 $max = pg_num_rows($a);
675 if ($max == 0 && $p_null == 0)
676 return null;
677 for ($i = 0; $i < $max; $i++) {
678 $row = pg_fetch_row($a);
679 $r[$i]['value'] = $row[0];
680 $r[$i]['label'] = h($row[1]);
681 }
682 // add a blank item ?
683 if ($p_null == 1) {
684 for ($i = $max; $i != 0; $i--) {
685 $r[$i]['value'] = $r[$i - 1]['value'];
686 $r[$i]['label'] = $r[$i - 1]['label'];
687 }
688 $r[0]['value'] = -1;
689 $r[0]['label'] = " ";
690 } // if ( $p_null == 1 )
691
692 return $r;
693 }
h( $row[ 'oa_description'])

References $a, $i, $max, $p_array, $r, $row, exec_sql(), and h.

+ Here is the call graph for this function:

◆ make_list()

DatabaseCore::make_list (   $sql,
  $p_array = null 
)

create a string containing the value separated by comma for use in a SQL in statement

Returns
the string or empty if nothing is found
See also
fid_card.php

Definition at line 614 of file database_core.class.php.

615 {
616 if ($p_array == null) {
617 $aArray = $this->get_array($sql);
618 } else {
619 $aArray = $this->get_array($sql, $p_array);
620 }
621 if (empty($aArray))
622 return "";
623 $aIdx = array_keys($aArray[0]);
624 $idx = $aIdx[0];
625 $ret = "";
626 $f = "";
627 for ($i = 0; $i < count($aArray); $i++) {
628 $row = $aArray[$i];
629 $ret .= $f . $aArray[$i][$idx];
630 $f = ',';
631 }
632 $ret = trim($ret, ',');
633 return $ret;
634 }
$idx

References $f, $i, $idx, $p_array, $ret, $row, $sql, count(), and get_array().

+ Here is the call graph for this function:

◆ nb_column()

static DatabaseCore::nb_column (   $p_ret)
static

Returns the number of columns in a ret.

Parameters
handler$p_rethandler to a query

Definition at line 1006 of file database_core.class.php.

1006 {
1007 return pg_num_fields($p_ret);
1008 }

Referenced by Export_Data_PDF\__construct().

◆ num_row()

static DatabaseCore::num_row (   $ret)
static

wrapper for the function pg_num_rows

Parameters
$retis the result of a exec_sql
Returns
number of line affected

Definition at line 744 of file database_core.class.php.

745 {
746 return pg_num_rows($ret);
747 }

References $ret.

Referenced by Noalyss_Parameter_Folder\__construct(), Default_Menu\__construct(), Fiche\belong_ledger(), Noalyss_User\Check(), Data_SQL\collect_objects(), Periode\display_periode_global(), Manage_Table_SQL\display_table(), Periode_Ledger_Table\display_table(), Fiche_Def\DisplayAttribut(), Acc_Account_Ledger\do_exist(), Fiche\empty_attribute(), Acc_Report\exist(), Export_Data_PDF\export(), Print_Ledger_Detail_Item\export(), Print_Ledger_Simple\export(), Print_Ledger_Simple_Without_Vat\export(), Follow_Up\export_csv(), Acc_Ledger_History_Purchase\export_csv(), Acc_Ledger_History_Sale\export_csv(), Follow_Up\export_csv_detail(), Document\export_file(), Periode\find_periode(), Acc_Operation\find_tiers(), Acc_Reconciliation\get(), Anc_Plan\get(), Document\get(), Fiche_Def\get(), get_affected(), Fiche_Def\get_all(), Acc_Reconciliation\get_amount_noautovat(), Fiche_Def\get_attr_min(), Fiche\get_bk_balance(), Customer\get_by_account(), Supplier\get_by_account(), Fiche_Def\get_by_category(), Fiche\get_by_category(), Acc_Operation\get_data(), Periode\get_date_limit(), Periode\get_exercice(), Acc_Operation\get_internal(), Anc_Operation\get_jrid(), Acc_Operation\get_jrnx_detail(), Acc_Report\get_list(), Acc_Account_Ledger\get_name(), Acc_Ledger\get_name(), Acc_Reconciliation\get_reconciled_amount(), Acc_Account_Ledger\get_row(), Fiche\get_row(), Acc_Ledger_History_Generic\get_row(), Acc_Report\get_row(), Fiche\get_row_date(), Fiche\get_row_result_deprecated(), Acc_Account_Ledger\get_row_sql_deprecated(), Acc_Ledger_History_Generic\get_rowSimple(), Acc_Account_Ledger\get_solde(), Acc_Account_Ledger\get_solde_detail(), Fiche\get_solde_detail(), Acc_Ledger\get_type(), Fiche_Def\getAttribut(), Stock\history(), html_min_page_start(), html_page_start(), isValid_deprecrated(), Acc_Ledger_Search\list_operation(), Acc_Ledger_Search\list_operation_to_reconcile(), Acc_Ledger\listing(), Acc_Bilan\load(), Acc_Ledger_Info\load(), Acc_Parm_Code\load(), Acc_Payment\load(), Anc_Balance_Simple\load(), Dossier\load(), Forecast\load(), Noalyss_User\load(), Todo_List\load(), Card_Property\load(), Acc_Ledger_Info\load_all(), Document_modele\myList(), Fiche_Def\myList(), query_to_csv(), Document_modele\Save(), Acc_Ledger_Info\search_id_internal(), Acc_Operation\seek_internal(), Document\send(), Acc_Reconciliation\show_detail(), Html_Table\sql2table(), Profile_Menu\sub_menu(), Document_modele\update(), Card_Property\update(), Anc_Operation\update_from_jrnx(), Customer\VatListing(), and Acc_Bilan\warning().

◆ prepare()

DatabaseCore::prepare (   $p_string,
  $p_sql 
)

wrapper for the function pg_prepare

Parameters
$p_stringstring name for pg_prepare function
$p_sqlis the sql to prepare
Returns
return the result of the operation

Definition at line 815 of file database_core.class.php.

816 {
817 return pg_prepare($this->db, $p_string, $p_sql);
818 }

References db.

◆ query_to_csv()

DatabaseCore::query_to_csv (   $ret,
  $aheader 
)

with the handle of a successull query, echo each row into CSV and send it directly

Parameters
type$rethandle to a query
type$aheaderdouble array, each item of the array contains a key type (num) and a key title

Definition at line 977 of file database_core.class.php.

978 {
979 $csv = new Noalyss_Csv("db-query");
980 $a_header = [];
981 for ($i = 0; $i < count($aheader); $i++) {
982 $a_header[] = $aheader[$i]['title'];
983 }
984 $csv->write_header($a_header);
985
986 // fetch all the rows
987 for ($i = 0; $i < Database::num_row($ret); $i++) {
989 // for each rows, for each value
990 for ($e = 0; $e < count($row) / 2; $e++) {
991 switch ($aheader[$e]['type']) {
992 case 'num':
993 $csv->add($row[$e], "number");
994 break;
995 default:
996 $csv->add($row[$e]);
997 }
998 }
999 $csv->write();
1000 }
1001 }
static fetch_array($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_array
Manage the CSV : manage files and write CSV record.

References $csv, $e, $i, $ret, $row, count(), fetch_array(), and num_row().

+ Here is the call graph for this function:

◆ rollback()

DatabaseCore::rollback ( )

rollback the current transaction

Definition at line 281 of file database_core.class.php.

282 {
283 if (!$this->is_open) return;
284 $Res = $this->exec_sql("rollback");
285 }

References $Res, and exec_sql().

Referenced by exec_sql(), execute_script(), and upload().

+ Here is the call graph for this function:

◆ search_sql_inject()

DatabaseCore::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.

Throw an exception EXC_INVALID

Definition at line 1015 of file database_core.class.php.

1016 {
1017 $forbid_sql=array("update","delete","truncate","insert");
1018 // protect against SQL inject
1019 foreach ($forbid_sql as $forbid_key) {
1020 if (stripos($p_sql,$forbid_key) !== false)
1021 {
1022 throw new Exception(_("Possible SQL inject"),EXC_INVALID);
1023 }
1024
1025 }
1026 }
const EXC_INVALID
Definition: constant.php:346

References EXC_INVALID.

◆ set_db()

DatabaseCore::set_db (   $db)
Parameters
mixed$db

Definition at line 118 of file database_core.class.php.

119 {
120 $this->db = $db;
121 }

References $db, and db.

◆ set_encoding()

DatabaseCore::set_encoding (   $p_charset)

Definition at line 96 of file database_core.class.php.

97 {
98 pg_set_client_encoding($this->db, $p_charset);
99 }

References db.

◆ set_is_open()

DatabaseCore::set_is_open (   $is_open)
Parameters
mixed$is_open

Definition at line 150 of file database_core.class.php.

151 {
152 $this->is_open = $is_open;
153 }

References $is_open.

◆ set_ret()

DatabaseCore::set_ret (   $ret)
Parameters
mixed$ret

Definition at line 134 of file database_core.class.php.

135 {
136 $this->ret = $ret;
137 }

References $ret.

◆ set_sql()

DatabaseCore::set_sql (   $sql)

last SQL stmt executed

Parameters
string$sql

Definition at line 166 of file database_core.class.php.

167 {
168 $this->sql=$sql;
169 return $this;
170 }

References $sql.

◆ size()

DatabaseCore::size (   $p_ret = null)

return the number of rows found by the last query, or the number of rows from $p_ret

Parameters
$p_retis the result of a query, the default value is null, in that case it is related to the last query
Note
synomym for count()

Definition at line 405 of file database_core.class.php.

406 {
407 if ($p_ret == null)
408 return pg_num_rows($this->ret);
409 else
410 return pg_num_rows($p_ret);
411 }

Referenced by Database\apply_patch(), and count().

◆ start()

DatabaseCore::start ( )

: start a transaction

Definition at line 263 of file database_core.class.php.

264 {
265 $Res = $this->exec_sql("start transaction");
266 }

References $Res, and exec_sql().

+ Here is the call graph for this function:

◆ status()

DatabaseCore::status ( )

get the transaction status : The status can be

  • PGSQL_TRANSACTION_IDLE (currently idle),
  • PGSQL_TRANSACTION_ACTIVE (a command is in progress),
  • PGSQL_TRANSACTION_INTRANS (idle, in a valid transaction block),
  • PGSQL_TRANSACTION_INERROR (idle, in a failed transaction block).
  • PGSQL_TRANSACTION_UNKNOWN is reported if the connection is bad.
  • PGSQL_TRANSACTION_ACTIVE is reported only when a query has been sent to the server and not yet completed.
Returns
PGSQL_TRANSACTION_IDLE | PGSQL_TRANSACTION_ACTIVE | PGSQL_TRANSACTION_INTRANS | PGSQL_TRANSACTION_INERROR | PGSQL_TRANSACTION_UNKNOWN | PGSQL_TRANSACTION_ACTIVE

Definition at line 910 of file database_core.class.php.

911 {
912 return pg_transaction_status($this->db);
913 }

References db.

◆ test_me()

static DatabaseCore::test_me ( )
static

Definition at line 894 of file database_core.class.php.

895 {
896
897 }

◆ upload()

DatabaseCore::upload (   $p_name)

Definition at line 706 of file database_core.class.php.

707 {
708 /* there is no file to upload */
709 if ($_FILES[$p_name]["error"] == UPLOAD_ERR_NO_FILE) {
710 return false;
711 }
712
713 $new_name = tempnam($_ENV['TMP'], $p_name);
714 if ($_FILES[$p_name]["error"] > 0) {
715 print_r($_FILES);
716 echo_error(__FILE__ . ":" . __LINE__ . "Error: " . $_FILES[$p_name]["error"]);
717 return false;
718 }
719 if (strlen($_FILES[$p_name]['tmp_name']) != 0) {
720 if (move_uploaded_file($_FILES[$p_name]['tmp_name'], $new_name)) {
721 // echo "Image saved";
722 $oid = pg_lo_import($this->db, $new_name);
723 if ($oid == false) {
724 echo_error(__FILE__, __LINE__, "cannot upload document");
725 $this->rollback();
726 return false;
727 }
728 return $oid;
729 } else {
730 echo "<H1>Error</H1>";
731 $this->rollback();
732 return false;
733 }
734 }
735 return false;
736 }
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
$new_name

References $new_name, db, echo_error(), and rollback().

Referenced by Database\save_receipt().

+ Here is the call graph for this function:

◆ verify()

DatabaseCore::verify ( )

Definition at line 91 of file database_core.class.php.

92 {
93 // Verify that the elt we want to add is correct
94 }

Field Documentation

◆ $array

DatabaseCore::$array

Definition at line 44 of file database_core.class.php.

Referenced by get_array(), get_row(), and get_value().

◆ $db

DatabaseCore::$db
private

database connection

Definition at line 38 of file database_core.class.php.

Referenced by get_db(), and set_db().

◆ $is_open

DatabaseCore::$is_open
private

true is connected

Definition at line 42 of file database_core.class.php.

Referenced by get_is_open(), and set_is_open().

◆ $ret

DatabaseCore::$ret
private

◆ $sql

DatabaseCore::$sql

last SQL stmt executed

Definition at line 43 of file database_core.class.php.

Referenced by Database\apply_patch(), clean_orphan_lob(), create_sequence(), execute_script(), get_sql(), make_list(), and set_sql().


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