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...
 
 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_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 ... 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)
 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_NumRows More...
 
static test_me ()
 

Data Fields

 $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  }

References db.

Member Function Documentation

◆ __toString()

DatabaseCore::__toString ( )
Parameters

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

869  {
870  return "database ";
871  }

◆ 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 283 of file database_core.class.php.

284  {
285  if ($min < 1)
286  $min = 1;
287  $Res = $this->exec_sql("alter sequence $p_name restart $min");
288  }

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 899 of file database_core.class.php.

900  {
901  // find all columns of type lob
902  $sql = "
903  select table_schema,table_name,column_name
904  from
905  information_schema.columns
906  where table_schema not in ('information_schema','pg_catalog')
907  and data_type='oid'";
908  $all_lob = "
909  select oid,'N' as used from pg_largeobject_metadata
910  ";
911  $a_table = $this->get_array($sql);
912  $a_lob = $this->get_array($all_lob);
913  if ($a_table == false || $a_lob == false) return;
914  // for each lob
915  $nb_lob = count($a_lob);
916  $nb_table = count($a_table);
917  for ($i = 0; $i < $nb_lob; $i++) {
918  $lob = $a_lob[$i]['oid'];
919  if ($a_lob[$i]['used'] == 'Y') continue;
920  for ($j = 0; $j < $nb_table; $j++) {
921  if ($a_lob[$i]['used'] == 'Y') continue;
922  $check = $this->get_value(" select count(*) from " .
923  $a_table[$j]['table_schema'] . "." . $a_table[$j]['table_name'] .
924  " where " .
925  $a_table[$j]['column_name'] . "=$1", array($lob));
926  if ($check != 0)
927  $a_lob[$i]['used'] = 'Y';
928 
929  }
930  }
931  for ($i = 0; $i < $nb_lob; $i++) {
932  if ($a_lob[$i]['used'] == 'Y') continue;
933  $this->lo_unlink($a_lob[$i]['oid']);
934  }
935  }

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

+ Here is the call graph for this function:

◆ close()

DatabaseCore::close ( )

wrapper for the function pg_close

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

856  {
857  if ($this->is_open) pg_close($this->db);
858  $this->is_open = FALSE;
859  }

References db.

◆ commit()

DatabaseCore::commit ( )

Commit the transaction.

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

264  {
265  if (!$this->is_open) return;
266  $Res = $this->exec_sql("commit");
267  }

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 408 of file database_core.class.php.

409  {
410  return $this->size($p_ret);
411  }

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 223 of file database_core.class.php.

224  {
225  $r_sql = $this->exec_sql($p_sql, $p_array);
226  return pg_NumRows($r_sql);
227  }

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 493 of file database_core.class.php.

494  {
495  if ($min < 1)
496  $min = 1;
497  $sql = "create sequence " . $p_name . " minvalue $min";
498  $this->exec_sql($sql);
499  }

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 847 of file database_core.class.php.

848  {
849  return pg_escape_string($p_string);
850  }

Referenced by Anc_Group\insert(), Anc_Acc_List\load_anc_account(), Anc_Acc_List\load_anc_card(), Anc_Table\load_card(), Anc_Acc_List\load_card(), Anc_Acc_List\load_poste(), Anc_Table\load_poste(), and Follow_Up\myList().

◆ 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 173 of file database_core.class.php.

174  {
175  try {
176  if (!$this->is_open) throw new Exception(' Database is closed');
177  $this->sql = $p_string;
178  $this->array = $p_array;
179 
180  if ($p_array == null) {
181  if ( DEBUGNOALYSS == 0 )
182  $this->ret = pg_query($this->db, $p_string);
183  else
184  $this->ret = @pg_query($this->db, $p_string);
185  } else {
186  $a = is_array($p_array);
187  if (!is_array($p_array)) {
188  throw new Exception(_("Erreur : exec_sql attend un array"));
189  }
190  if ( DEBUGNOALYSS == 0 )
191  $this->ret =@pg_query_params($this->db, $p_string, $p_array);
192  else
193  $this->ret = pg_query_params($this->db, $p_string, $p_array);
194  }
195  if (!$this->ret) {
196  $str_error = pg_last_error($this->db) . pg_result_error($this->ret);
197  throw new Exception(" SQL ERROR $p_string " . $str_error, 1);
198  }
199  } catch (Exception $a) {
200  if ( DEBUGNOALYSS > 0 ) {
201  print_r($p_string);
202  print_r($p_array);
203  echo $a->getMessage();
204  echo $a->getTraceAsString();
205  echo pg_last_error($this->db);
206  }
207  record_log($a->getTraceAsString());
208  $this->rollback();
209 
210  throw ($a);
211  }
212 
213  return $this->ret;
214  }

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 813 of file database_core.class.php.

814  {
815  $this->ret = pg_execute($this->db, $p_string, $p_array);
816  return $this->ret;
817  }

References $p_array, $ret, and db.

◆ execute_script()

DatabaseCore::execute_script (   $script)

Execute a sql script.

Parameters
$scriptscript name

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

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

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 562 of file database_core.class.php.

563  {
564  $r = $this->get_value('select count(*) from pg_largeobject_metadata where oid=$1'
565  , array($p_oid));
566  if ($r > 0)
567  return true;
568  else
569  return false;
570  }

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 536 of file database_core.class.php.

537  {
538  $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));
539  if ($r > 0)
540  return true;
541  return false;
542  }

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 550 of file database_core.class.php.

551  {
552  $database_exist = $this->get_value('select count(*)
553  from pg_catalog.pg_database where datname = lower($1)', array($p_name));
554  return $database_exist;
555  }

References get_value().

+ Here is the call graph for this function:

◆ exist_schema()

DatabaseCore::exist_schema (   $p_name)

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

591  {
592  $r = $this->count_sql("select nspname from pg_namespace where nspname=lower($1)", array($p_name));
593  if ($r == 0)
594  return false;
595  return true;
596  }

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 506 of file database_core.class.php.

507  {
508  $r = $this->count_sql("select relname from pg_class where relname=lower($1)", array($p_name));
509  if ($r == 0)
510  return false;
511  return true;
512  }

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 521 of file database_core.class.php.

522  {
523  $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));
524  if ($r == 0)
525  return false;
526  return true;
527  }

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 577 of file database_core.class.php.

578  {
579  $r = $this->count_sql("select viewname from pg_views where viewname=lower($1)", array($p_name));
580  if ($r == 0)
581  return false;
582  return true;
583  }

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 380 of file database_core.class.php.

381  {
382  if ($this->ret == false)
383  throw new Exception('this->ret is empty');
384  return pg_fetch_array($this->ret, $p_indice);
385  }

◆ fetch_all()

static DatabaseCore::fetch_all (   $ret)
static

◆ fetch_array()

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

wrapper for the function pg_fetch_array

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

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

746  {
747  return pg_fetch_array($ret, $p_indice);
748  }

References $ret.

Referenced by Noalyss_Parameter_Folder\__construct(), User\Check(), Document_modele\Delete(), Periode_Ledger_Table\display_table(), Manage_Table_SQL\display_table(), Fiche_Def\DisplayAttribut(), Print_Ledger_Detail_Item\export(), Print_Ledger_Simple_Without_Vat\export(), Print_Ledger_Simple\export(), Acc_Ledger_History_Sale\export_csv(), Acc_Ledger_History_Purchase\export_csv(), Follow_Up\export_csv_detail(), Document\export_file(), Acc_Operation\find_tiers(), Document\generate(), Document_Type\get(), Anc_Plan\get(), Fiche_Def\get(), Acc_Reconciliation\get(), Document\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_Reconciliation\get_reconciled_amount(), 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(), Card_Property\input(), isValid_deprecrated(), Acc_Ledger_Search\list_operation(), Acc_Ledger_Search\list_operation_to_reconcile(), Acc_Ledger\listing(), Acc_Payment\load(), Acc_Ledger_Info\load(), Forecast\load(), Acc_Tva\load(), Card_Property\load(), Acc_Parm_Code\load(), Todo_List\load(), Dossier\load(), Acc_Bilan\load(), User\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 767 of file database_core.class.php.

768  {
769  return pg_fetch_result($ret, $p_row, $p_col);
770  }

References $ret.

Referenced by Fiche\empty_attribute(), Print_Ledger_Financial\export(), Periode\find_periode(), Periode\get_exercice(), Acc_Account_Ledger\get_row(), Fiche\get_row(), Acc_Account_Ledger\get_row_date(), Fiche\get_row_date(), Acc_Ledger_Info\insert(), Forecast\insert(), Acc_Tva\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 778 of file database_core.class.php.

779  {
780  return pg_fetch_row($ret, $p_row);
781  }

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 450 of file database_core.class.php.

451  {
452  return Database::num_row($this->ret);
453  }

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 463 of file database_core.class.php.

464  {
465  $r = $this->exec_sql($p_sql, $p_array);
466 
467  if (pg_NumRows($r) == 0)
468  return array();
469  $array = pg_fetch_all($r);
470  return $array;
471  }

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 233 of file database_core.class.php.

234  {
235  $Res = $this->get_value("select currval('$p_seq') as seq");
236  return $Res;
237  }

References $Res, and get_value().

+ Here is the call graph for this function:

◆ get_db()

DatabaseCore::get_db ( )
Returns
mixed

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

104  {
105  return $this->db;
106  }

References $db.

◆ get_encoding()

DatabaseCore::get_encoding ( )

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

95  {
96  return pg_client_encoding($this->db);
97  }

References db.

◆ get_is_open()

DatabaseCore::get_is_open ( )
Returns
mixed

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

136  {
137  return $this->is_open;
138  }

References $is_open.

◆ get_next_seq()

DatabaseCore::get_next_seq (   $p_seq)

get the next sequence value

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

244  {
245  $Res = $this->exec_sql("select nextval('$p_seq') as seq");
246  $seq = pg_fetch_array($Res, 0);
247  return $seq['seq'];
248  }

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

+ Here is the call graph for this function:

◆ get_ret()

DatabaseCore::get_ret ( )
Returns
mixed

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

120  {
121  return $this->ret;
122  }

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 480 of file database_core.class.php.

481  {
482  $array = $this->get_array($p_sql, $p_array);
483  if (empty($array)) return null;
484  if (count($array) == 1) return $array[0];
485  throw new Exception(_("Database:get_row retourne trop de lignes"), 100);
486  }

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 151 of file database_core.class.php.

152  {
153  return $this->sql;
154  }

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 427 of file database_core.class.php.

428  {
429  try {
430  $this->ret = $this->exec_sql($p_sql, $p_array);
431  $r = pg_NumRows($this->ret);
432  if ($r == 0)
433  return "";
434  if ($r > 1) {
435  $array = pg_fetch_all($this->ret);
436  throw new Exception("Attention $p_sql retourne " . pg_NumRows($this->ret) . " valeurs " .
437  var_export($p_array, true) . " values=" . var_export($array, true));
438  }
439  $r = pg_fetch_row($this->ret, 0);
440  return $r[0];
441 
442  } catch (Exception $ex) {
443  throw($ex);
444  }
445  }

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 942 of file database_core.class.php.

943  {
944  $nb_prepared = $this->get_value("select count(*) from pg_prepared_statements where name=$1", [$query_name]);
945  if ($nb_prepared == 0) return FALSE;
946  return TRUE;
947  }

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 826 of file database_core.class.php.

827  {
828  return pg_lo_export($this->db, $p_oid, $tmp_file);
829  }

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 837 of file database_core.class.php.

838  {
839  return pg_lo_import($this->db, $p_filename);
840  }

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 788 of file database_core.class.php.

789  {
790  if (!$this->exist_blob($p_oid)) return;
791  return pg_lo_unlink($this->db, $p_oid);
792  }

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 662 of file database_core.class.php.

663  {
664  $a = $this->exec_sql($p_sql, $p_array);
665  $max = pg_NumRows($a);
666  if ($max == 0 && $p_null == 0)
667  return null;
668  for ($i = 0; $i < $max; $i++) {
669  $row = pg_fetch_row($a);
670  $r[$i]['value'] = $row[0];
671  $r[$i]['label'] = h($row[1]);
672  }
673  // add a blank item ?
674  if ($p_null == 1) {
675  for ($i = $max; $i != 0; $i--) {
676  $r[$i]['value'] = $r[$i - 1]['value'];
677  $r[$i]['label'] = h($r[$i - 1]['label']);
678  }
679  $r[0]['value'] = -1;
680  $r[0]['label'] = " ";
681  } // if ( $p_null == 1 )
682 
683  return $r;
684  }

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 605 of file database_core.class.php.

606  {
607  if ($p_array == null) {
608  $aArray = $this->get_array($sql);
609  } else {
610  $aArray = $this->get_array($sql, $p_array);
611  }
612  if (empty($aArray))
613  return "";
614  $aIdx = array_keys($aArray[0]);
615  $idx = $aIdx[0];
616  $ret = "";
617  $f = "";
618  for ($i = 0; $i < count($aArray); $i++) {
619  $row = $aArray[$i];
620  $ret .= $f . $aArray[$i][$idx];
621  $f = ',';
622  }
623  $ret = trim($ret, ',');
624  return $ret;
625  }

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 985 of file database_core.class.php.

985  {
986  return pg_num_fields($p_ret);
987  }

Referenced by Export_Data_PDF\__construct().

◆ num_row()

static DatabaseCore::num_row (   $ret)
static

wrapper for the function pg_NumRows

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

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

735  {
736  return pg_NumRows($ret);
737  }

References $ret.

Referenced by Default_Menu\__construct(), Noalyss_Parameter_Folder\__construct(), Fiche\belong_ledger(), User\Check(), Data_SQL\collect_objects(), Periode\display_periode_global(), Periode_Ledger_Table\display_table(), Manage_Table_SQL\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_Without_Vat\export(), Print_Ledger_Simple\export(), Acc_Ledger_History_Sale\export_csv(), Acc_Ledger_History_Purchase\export_csv(), Follow_Up\export_csv(), Follow_Up\export_csv_detail(), Document\export_file(), Periode\find_periode(), Acc_Operation\find_tiers(), Anc_Plan\get(), Fiche_Def\get(), Acc_Reconciliation\get(), Document\get(), get_affected(), Fiche_Def\get_all(), Acc_Reconciliation\get_amount_noautovat(), Fiche_Def\get_attr_min(), Fiche\get_bk_balance(), Supplier\get_by_account(), Customer\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_Report\get_row(), Acc_Account_Ledger\get_row(), Acc_Ledger_History_Generic\get_row(), Fiche\get_row(), Acc_Account_Ledger\get_row_date(), 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(), Anc_Balance_Simple\load(), Acc_Payment\load(), Acc_Ledger_Info\load(), Forecast\load(), Card_Property\load(), Acc_Parm_Code\load(), Todo_List\load(), Dossier\load(), Acc_Bilan\load(), User\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 800 of file database_core.class.php.

801  {
802  return pg_prepare($this->db, $p_string, $p_sql);
803  }

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 956 of file database_core.class.php.

957  {
958  $csv = new Noalyss_Csv("db-query");
959  $a_header = [];
960  for ($i = 0; $i < count($aheader); $i++) {
961  $a_header[] = $aheader[$i]['title'];
962  }
963  $csv->write_header($a_header);
964 
965  // fetch all the rows
966  for ($i = 0; $i < Database::num_row($ret); $i++) {
968  // for each rows, for each value
969  for ($e = 0; $e < count($row) / 2; $e++) {
970  switch ($aheader[$e]['type']) {
971  case 'num':
972  $csv->add($row[$e], "number");
973  break;
974  default:
975  $csv->add($row[$e]);
976  }
977  }
978  $csv->write();
979  }
980  }

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 272 of file database_core.class.php.

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

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 994 of file database_core.class.php.

995  {
996  $forbid_sql=array("update","delete","truncate","insert");
997  // protect against SQL inject
998  foreach ($forbid_sql as $forbid_key) {
999  if (stripos($p_sql,$forbid_key) !== false)
1000  {
1001  throw new Exception(_("Possible SQL inject",EXC_INVALID));
1002  }
1003 
1004  }
1005  }

References EXC_INVALID.

◆ set_db()

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

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

112  {
113  $this->db = $db;
114  }

References $db, and db.

◆ set_encoding()

DatabaseCore::set_encoding (   $p_charset)

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

90  {
91  pg_set_client_encoding($this->db, $p_charset);
92  }

References db.

◆ set_is_open()

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

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

144  {
145  $this->is_open = $is_open;
146  }

References $is_open.

◆ set_ret()

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

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

128  {
129  $this->ret = $ret;
130  }

References $ret.

◆ set_sql()

DatabaseCore::set_sql (   $sql)

last SQL stmt executed

Parameters
string$sql

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

160  {
161  $this->sql=$sql;
162  return $this;
163  }

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 396 of file database_core.class.php.

397  {
398  if ($p_ret == null)
399  return pg_NumRows($this->ret);
400  else
401  return pg_NumRows($p_ret);
402  }

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

◆ start()

DatabaseCore::start ( )

: start a transaction

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

255  {
256  $Res = $this->exec_sql("start transaction");
257  }

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 889 of file database_core.class.php.

890  {
891  return pg_transaction_status($this->db);
892  }

References db.

◆ test_me()

static DatabaseCore::test_me ( )
static

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

874  {
875 
876  }

◆ upload()

DatabaseCore::upload (   $p_name)

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

698  {
699  /* there is no file to upload */
700  if ($_FILES[$p_name]["error"] == UPLOAD_ERR_NO_FILE) {
701  return false;
702  }
703 
704  $new_name = tempnam($_ENV['TMP'], $p_name);
705  if ($_FILES[$p_name]["error"] > 0) {
706  print_r($_FILES);
707  echo_error(__FILE__ . ":" . __LINE__ . "Error: " . $_FILES[$p_name]["error"]);
708  return false;
709  }
710  if (strlen($_FILES[$p_name]['tmp_name']) != 0) {
711  if (move_uploaded_file($_FILES[$p_name]['tmp_name'], $new_name)) {
712  // echo "Image saved";
713  $oid = pg_lo_import($this->db, $new_name);
714  if ($oid == false) {
715  echo_error(__FILE__, __LINE__, "cannot upload document");
716  $this->rollback();
717  return false;
718  }
719  return $oid;
720  } else {
721  echo "<H1>Error</H1>";
722  $this->rollback();
723  return false;
724  }
725  }
726  return false;
727  }

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

Referenced by Database\save_receipt().

+ Here is the call graph for this function:

◆ verify()

DatabaseCore::verify ( )

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

85  {
86  // Verify that the elt we want to add is correct
87  }

Field Documentation

◆ $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:
DatabaseCore\$ret
$ret
return value
Definition: database_core.class.php:40
$ex
$ex
Definition: balance.inc.php:45
h
h( $row[ 'oa_description'])
Definition: ajax_anc_detail_operation.php:46
$e
$e
Definition: result_cat_card_summary.php:26
$table
$table
Definition: menu.inc.php:103
$max
$max
Definition: ajax_boxcard_search.php:40
db
$SecUser db
Definition: export_security_pdf.php:118
EXC_INVALID
const EXC_INVALID
Definition: constant.php:340
$check
$check
Definition: letter_prop.php:98
DatabaseCore\fetch_array
static fetch_array($ret, $p_indice=0)
wrapper for the function pg_fetch_array
Definition: database_core.class.php:745
DatabaseCore\count
count($p_ret=null)
synomym for size()
Definition: database_core.class.php:408
$array
$array
Definition: ajax_add_concerned_card.php:115
$csv
$csv
Definition: ajax_action_concerned_list.php:44
Noalyss_Csv
Manage the CSV : manage files and write CSV record.
Definition: noalyss_csv.class.php:33
$script
$script
Definition: popup.php:125
DatabaseCore\get_array
get_array($p_sql, $p_array=null)
purpose return the result of a sql statment in a array
Definition: database_core.class.php:463
$i
$i
Definition: action_document_type_mtable_input.php:83
$a
$a
Definition: ajax_add_concerned_card.php:108
DatabaseCore\num_row
static num_row($ret)
wrapper for the function pg_NumRows
Definition: database_core.class.php:734
DatabaseCore\rollback
rollback()
rollback the current transaction
Definition: database_core.class.php:272
DatabaseCore\lo_unlink
lo_unlink($p_oid)
wrapper for the function pg_lo_unlink
Definition: database_core.class.php:788
$r
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
Definition: ajax_add_concerned_card.php:53
$idx
$idx
Definition: ajax_bookmark.php:79
echo_error
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
$Res
$Res
Definition: export_security_pdf.php:117
DatabaseCore\exist_blob
exist_blob($p_oid)
check if the large object exists
Definition: database_core.class.php:562
DatabaseCore\get_value
get_value($p_sql, $p_array=null)
return the value of the sql, the sql will return only one value with the value
Definition: database_core.class.php:427
$f
$f
Definition: category_card.inc.php:69
DatabaseCore\count_sql
count_sql($p_sql, $p_array=null)
Count the number of row returned by a sql statement.
Definition: database_core.class.php:223
print
print
Type of printing.
Definition: impress_jrn.inc.php:118
DatabaseCore\$sql
$sql
last SQL stmt executed
Definition: database_core.class.php:43
DatabaseCore\exec_sql
exec_sql($p_string, $p_array=null)
send a sql string to the database
Definition: database_core.class.php:173
$min
$min
Definition: ajax_display_letter.php:89
DatabaseCore\$is_open
$is_open
Definition: database_core.class.php:42
$row
$row
Definition: ajax_anc_detail_operation.php:33
DatabaseCore\size
size($p_ret=null)
return the number of rows found by the last query, or the number of rows from $p_ret
Definition: database_core.class.php:396
$p_array
$p_array
Definition: ajax_view_mod_stock.php:33
$seq
$seq
Definition: action_document_type_mtable_input.php:62
record_log
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1400
DatabaseCore\$db
$db
database connection
Definition: database_core.class.php:38