2
0
forked from Wavyzz/dolibarr

Fix: On cre les fonctions si le fichier functions.sql existe

This commit is contained in:
Laurent Destailleur
2005-03-18 00:57:30 +00:00
parent 954f4d879e
commit ef933c09d2
2 changed files with 374 additions and 261 deletions

View File

@@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org> /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2004-2005 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@
* $Source$ * $Source$
* *
*/ */
include_once("./inc.php"); include_once("./inc.php");
$setuplang=isset($_POST["selectlang"])?$_POST["selectlang"]:(isset($_GET["selectlang"])?$_GET["selectlang"]:$langcode); $setuplang=isset($_POST["selectlang"])?$_POST["selectlang"]:(isset($_GET["selectlang"])?$_GET["selectlang"]:$langcode);
@@ -33,296 +34,408 @@ $etape = 2;
if (file_exists($conffile)) if (file_exists($conffile))
{ {
include_once($conffile); include_once($conffile);
} }
if($dolibarr_main_db_type == "mysql") if($dolibarr_main_db_type == "mysql")
{ {
require ($dolibarr_main_document_root . "/lib/mysql.lib.php"); require ($dolibarr_main_document_root . "/lib/mysql.lib.php");
$choix=1; $choix=1;
} }
else else
{ {
require ($dolibarr_main_document_root . "/lib/pgsql.lib.php"); require ($dolibarr_main_document_root . "/lib/pgsql.lib.php");
require ($dolibarr_main_document_root . "/lib/grant.postgres.php"); require ($dolibarr_main_document_root . "/lib/grant.postgres.php");
$choix=2; $choix=2;
} }
require ($dolibarr_main_document_root . "/conf/conf.class.php"); require ($dolibarr_main_document_root . "/conf/conf.class.php");
if ($_POST["action"] == "set") if ($_POST["action"] == "set")
{ {
print '<h2>'.$langs->trans("Database").'</h2>'; print '<h2>'.$langs->trans("Database").'</h2>';
print '<table cellspacing="0" cellpadding="4" border="0" width="100%">'; print '<table cellspacing="0" cellpadding="4" border="0" width="100%">';
$error=0; $error=0;
$conf = new Conf();// on pourrait s'en passer $conf = new Conf();// on pourrait s'en passer
$conf->db->host = $dolibarr_main_db_host; $conf->db->host = $dolibarr_main_db_host;
$conf->db->name = $dolibarr_main_db_name; $conf->db->name = $dolibarr_main_db_name;
$conf->db->user = $dolibarr_main_db_user; $conf->db->user = $dolibarr_main_db_user;
$conf->db->pass = $dolibarr_main_db_pass; $conf->db->pass = $dolibarr_main_db_pass;
$db = new DoliDb(); $db = new DoliDb();
$ok = 0; $ok = 0;
if ($db->connected == 1) if ($db->connected == 1)
{ {
print "<tr><td>"; print "<tr><td>";
print $langs->trans("ServerConnection")." : $dolibarr_main_db_host</td><td>".$langs->trans("OK")."</td></tr>"; print $langs->trans("ServerConnection")." : $dolibarr_main_db_host</td><td>".$langs->trans("OK")."</td></tr>";
$ok = 1 ; $ok = 1 ;
} }
else else
{ {
print "<tr><td>Erreur lors de la cr<63>ation de : $dolibarr_main_db_name</td><td>".$langs->trans("Error")."</td></tr>"; print "<tr><td>Erreur lors de la cr<63>ation de : $dolibarr_main_db_name</td><td>".$langs->trans("Error")."</td></tr>";
}
/***************************************************************************************
*
*
*/
if ($ok)
{
if($db->database_selected == 1)
{
dolibarr_syslog("Connexion r<>ussie <20> la base : $dolibarr_main_db_name");
}
else
{
$ok = 0 ;
}
}
/***************************************************************************************
*
*
*/
if ($ok)
{
$ok = 0;
if ($choix == 1)
{
$dir = "../../mysql/tables/";
}
else
{
$dir = "../../pgsql/tables/";
}
$handle=opendir($dir);
$table_exists = 0;
while (($file = readdir($handle))!==false)
{
if (substr($file, strlen($file) - 4) == '.sql' && substr($file,0,4) == 'llx_' && substr($file, - 8) <> '.key.sql')
{
$name = substr($file, 0, strlen($file) - 4);
//print "<tr><td>Cr<43>ation de la table $name</td>";
$buffer = '';
$fp = fopen($dir.$file,"r");
if ($fp)
{
while (!feof ($fp))
{
$buf = fgets($fp, 4096);
if (substr($buf, 0, 2) <> '--')
{
$buffer .= $buf;
}
}
fclose($fp);
}
if ($db->query($buffer))
{
//print "<td>OK requete ==== $buffer</td></tr>";
}
else
{
if ($db->errno() == DB_ERROR_TABLE_ALREADY_EXISTS)
{
//print "<td>D<>j<EFBFBD> existante</td></tr>";
$table_exists = 1;
}
else
{
print "<tr><td>Cr<43>ation de la table $name</td>";
print "<td>".$langs->trans("Error")." ".$db->errno()." ".$db->error()."</td></tr>";
$error++;
}
}
}
}
//droit sur les tables
if ($db->query($grant_query))
{
print "<tr><td>Grant User '$nom' </td><td>".$langs->trans("OK")."</td></tr>";
}
closedir($handle);
if ($error == 0)
{
print '<tr><td>';
print $langs->trans("TablesAndPrimaryKeyCreation").'</td><td>'.$langs->trans("OK").'</td></tr>';
$ok = 1;
}
} }
/*************************************************************************************** if ($ok)
*
* Chargement fichier data.sql
*
***************************************************************************************/
if ($ok == 1)
{ {
// if($db->database_selected == 1)
// Donn<6E>es {
//
if ($choix==1) dolibarr_syslog("Connexion r<>ussie <20> la base : $dolibarr_main_db_name");
{ }
$dir = "../../mysql/data/"; else
$file = "data.sql"; {
$fp = fopen($dir.$file,"r"); $ok = 0 ;
if ($fp) }
{
while (!feof ($fp))
{
$buffer = fgets($fp, 4096);
if (strlen(trim(ereg_replace("--","",$buffer))))
{
if ($db->query($buffer))
{
$ok = 1;
}
else
{
if ($db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS)
{
// print "<tr><td>Insertion ligne : $buffer</td><td>
}
else
{
$ok = 0;
print "Erreur SQL ".$db->errno()." sur requete '$buffer': ".$db->error()."<br>";
}
}
}
}
fclose($fp);
}
}//choix==1
else
{
$dir = "../../pgsql/data/";
$file = "data.sql";
$fp = fopen($dir.$file,"r");
$buffer='';
if ($fp)
{
while (!feof ($fp))
{
$buffer .= fgets($fp, 4096);
}
if ($db->query($buffer))
{
$ok = 1;
}
else
{
if ($db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS)
{
// print "<tr><td>Insertion ligne : $buffer</td><td>D<>ja existante</td></tr>";
}
else
{
$ok = 0;
print "Erreur SQL ".$db->errno()." sur requete '$buffer': ".$db->error()."<br>";
}
}
//}//while
fclose($fp);
}
}//else
print "<tr><td>".$langs->trans("ReferenceDataLoading")."</td>";
if ($ok)
{
print "<td>".$langs->trans("OK")."</td></tr>";
}
else
{
print "<td>".$langs->trans("Error")."</td></tr>";
$ok = 1 ;
}
} }
/*************************************************************************************** /***************************************************************************************
* *
* Les variables qui ecrase le chemin par defaut sont red<65>finies * Chargement fichiers tables/*.sql (non *.key.sql)
* * A faire avant les fichiers *.key.sql
***************************************************************************************/ *
if ($ok == 1) ***************************************************************************************/
if ($ok)
{ {
$sql[0] = "UPDATE llx_const SET value = '".$dolibarr_main_data_root."/facture', if ($choix==1) $dir = "../../mysql/tables/";
type = 'chaine', else $dir = "../../pgsql/tables/";
visible = 0
where name ='FAC_OUTPUTDIR';" ;
$sql[1] = "UPDATE llx_const SET value = '".$dolibarr_main_data_root."/propale',
type = 'chaine',
visible = 0
where name = 'PROPALE_OUTPUTDIR';" ;
$sql[2] = "UPDATE llx_const SET value = '".$dolibarr_main_data_root."/ficheinter',
type = 'chaine',
visible = 0
where name = 'FICHEINTER_OUTPUTDIR';" ;
$sql[3] = "UPDATE llx_const SET value='".$dolibarr_main_data_root."/societe',
type = 'chaine',
visible = 0
where name = 'SOCIETE_OUTPUTDIR';" ;
$sql[4] = "DELETE from llx_const where name like '%_OUTPUT_URL';";
$ok = 0;
$handle=opendir($dir);
$table_exists = 0;
while (($file = readdir($handle))!==false)
{
if (substr($file, strlen($file) - 4) == '.sql' && substr($file,0,4) == 'llx_' && substr($file, -8) <> '.key.sql')
{
$name = substr($file, 0, strlen($file) - 4);
$buffer = '';
$fp = fopen($dir.$file,"r");
if ($fp)
{
while (!feof ($fp))
{
$buf = fgets($fp, 4096);
if (substr($buf, 0, 2) <> '--')
{
$buffer .= $buf;
}
}
fclose($fp);
}
$sql[5] = "UPDATE llx_const SET value='".$langs->defaultlang."', //print "<tr><td>Cr<43>ation de la table $name/td>";
type = 'chaine',
visible = 0 if ($db->query($buffer))
where name = 'MAIN_LANG_DEFAULT';" ; {
//print "<td>OK requete ==== $buffer</td></tr>";
$result = 0; }
else
for ($i=0; $i < sizeof($sql);$i++) {
{ if ($db->errno() == DB_ERROR_TABLE_ALREADY_EXISTS)
if ($db->query($sql[$i])) {
{ //print "<td>D<>j<EFBFBD> existante</td></tr>";
$result++; $table_exists = 1;
} }
} else
{
if ($result == sizeof($sql)) print "<tr><td>".$langs->trans("CreateTableAndPrimaryKey",$name)."</td>";
{ print "<td>".$langs->trans("Error")." ".$db->errno()." ".$db->error()."</td></tr>";
if ($error == 0) $error++;
{ }
$db->query("DELETE FROM llx_const WHERE name='MAIN_NOT_INSTALLED'"); }
} }
}
}
closedir($handle);
if ($error == 0)
{
print '<tr><td>';
print $langs->trans("TablesAndPrimaryKeysCreation").'</td><td>'.$langs->trans("OK").'</td></tr>';
$ok = 1;
}
} }
/*************************************************************************************** /***************************************************************************************
* *
* * Chargement fichiers tables/*.key.sql
* * A faire apr<70>s les fichiers *.sql
* *
***************************************************************************************/ ***************************************************************************************/
if ($ok)
{
if ($choix==1) $dir = "../../mysql/tables/";
else $dir = "../../pgsql/tables/";
$okkeys = 0;
$handle=opendir($dir);
$table_exists = 0;
while (($file = readdir($handle))!==false)
{
if (substr($file, strlen($file) - 4) == '.sql' && substr($file,0,4) == 'llx_' && substr($file, -8) == '.key.sql')
{
$name = substr($file, 0, strlen($file) - 4);
//print "<tr><td>Cr<43>ation de la table $name</td>";
$buffer = '';
$fp = fopen($dir.$file,"r");
if ($fp)
{
while (!feof ($fp))
{
$buf = fgets($fp, 4096);
if (substr($buf, 0, 2) <> '--')
{
$buffer .= $buf;
}
}
fclose($fp);
}
// Si plusieurs requetes, on boucle sur chaque
$listesql=split(';',$buffer);
foreach ($listesql as $buffer) {
if (trim($buffer)) {
//print "<tr><td>Cr<43>ation des cl<63>s et index de la table $name: '$buffer'</td>";
if ($db->query(trim($buffer)))
{
//print "<td>OK requete ==== $buffer</td></tr>";
}
else
{
if ($db->errno() == DB_ERROR_CANNOT_CREATE || eregi('duplicate key name',$db->error()))
{
//print "<td>D<>j<EFBFBD> existante</td></tr>";
$key_exists = 1;
}
else
{
print "<tr><td>".$langs->trans("CreateOtherKeysForTable",$name)."</td>";
print "<td>".$langs->trans("Error")." ".$db->errno()." ".$db->error()."</td></tr>";
$error++;
}
}
}
}
}
}
closedir($handle);
if ($error == 0)
{
print '<tr><td>';
print $langs->trans("OtherKeysCreation").'</td><td>'.$langs->trans("OK").'</td></tr>';
$okkeys = 1;
}
}
/***************************************************************************************
*
* Positionnement des droits
*
***************************************************************************************/
if ($ok)
{
// Droits sur les tables
if ($grant_query) {
if ($db->query($grant_query))
{
print "<tr><td>Grant User</td><td>".$langs->trans("OK")."</td></tr>";
}
}
}
print '</table>'; /***************************************************************************************
*
* Chargement fichier functions.sql
*
***************************************************************************************/
if ($ok)
{
if ($choix==1) $dir = "../../mysql/functions/";
else $dir = "../../pgsql/functions/";
$db->close(); // Cr<43>ation donn<6E>es
$file = "functions.sql";
if (file_exists($dir.$file)) {
$fp = fopen($dir.$file,"r");
if ($fp)
{
while (!feof ($fp))
{
$buffer = fgets($fp, 4096);
if (substr($buf, 0, 2) <> '--')
{
$buffer .= $buf;
}
}
fclose($fp);
}
// Si plusieurs requetes, on boucle sur chaque
$listesql=split('<27>',eregi_replace(";';",";'<27>",$buffer));
foreach ($listesql as $buffer) {
if (trim($buffer)) {
if ($db->query(trim($buffer)))
{
$ok = 1;
}
else
{
if ($db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS)
{
// print "<tr><td>Insertion ligne : $buffer</td><td>
}
else
{
$ok = 0;
print "Erreur SQL ".$db->errno()." sur requete '$buffer': ".$db->error()."<br>";
}
}
}
}
print "<tr><td>".$langs->trans("FunctionsCreation")."</td>";
if ($ok)
{
print "<td>".$langs->trans("OK")."</td></tr>";
}
else
{
print "<td>".$langs->trans("Error")."</td></tr>";
$ok = 1 ;
}
}
}
/***************************************************************************************
*
* Chargement fichier data.sql
*
***************************************************************************************/
if ($ok)
{
if ($choix==1) $dir = "../../mysql/data/";
else $dir = "../../pgsql/data/";
// Cr<43>ation donn<6E>es
$file = "data.sql";
$fp = fopen($dir.$file,"r");
if ($fp)
{
while (!feof ($fp))
{
$buffer = fgets($fp, 4096);
if (strlen(trim(ereg_replace("--","",$buffer))))
{
if ($db->query($buffer))
{
$ok = 1;
}
else
{
if ($db->errno() == DB_ERROR_RECORD_ALREADY_EXISTS)
{
// print "<tr><td>Insertion ligne : $buffer</td><td>
}
else
{
$ok = 0;
print "Erreur SQL ".$db->errno()." sur requete '$buffer': ".$db->error()."<br>";
}
}
}
}
fclose($fp);
}
print "<tr><td>".$langs->trans("ReferenceDataLoading")."</td>";
if ($ok)
{
print "<td>".$langs->trans("OK")."</td></tr>";
}
else
{
print "<td>".$langs->trans("Error")."</td></tr>";
$ok = 1 ;
}
}
/***************************************************************************************
*
* Les variables qui ecrase le chemin par defaut sont red<65>finies
*
***************************************************************************************/
if ($ok == 1)
{
$sql[0] = "UPDATE llx_const SET value = '".$dolibarr_main_data_root."/facture',
type = 'chaine',
visible = 0
where name ='FAC_OUTPUTDIR';" ;
$sql[1] = "UPDATE llx_const SET value = '".$dolibarr_main_data_root."/propale',
type = 'chaine',
visible = 0
where name = 'PROPALE_OUTPUTDIR';" ;
$sql[2] = "UPDATE llx_const SET value = '".$dolibarr_main_data_root."/ficheinter',
type = 'chaine',
visible = 0
where name = 'FICHEINTER_OUTPUTDIR';" ;
$sql[3] = "UPDATE llx_const SET value='".$dolibarr_main_data_root."/societe',
type = 'chaine',
visible = 0
where name = 'SOCIETE_OUTPUTDIR';" ;
$sql[4] = "DELETE from llx_const where name like '%_OUTPUT_URL';";
$sql[5] = "UPDATE llx_const SET value='".$langs->defaultlang."',
type = 'chaine',
visible = 0
where name = 'MAIN_LANG_DEFAULT';" ;
$result = 0;
for ($i=0; $i < sizeof($sql);$i++)
{
if ($db->query($sql[$i]))
{
$result++;
}
}
if ($result == sizeof($sql))
{
if ($error == 0)
{
$db->query("DELETE FROM llx_const WHERE name='MAIN_NOT_INSTALLED'");
}
}
}
/***************************************************************************************
*
*
*
*
***************************************************************************************/
print '</table>';
$db->close();
} }
pFooter(!$ok); pFooter(!$ok);
?> ?>

View File

@@ -36,4 +36,4 @@ CREATE OR REPLACE FUNCTION FROM_UNIXTIME(BIGINT, VARCHAR)
RETURNS TIMESTAMP WITH TIME ZONE RETURNS TIMESTAMP WITH TIME ZONE
LANGUAGE SQL LANGUAGE SQL
IMMUTABLE STRICT IMMUTABLE STRICT
AS 'SELECT TIMESTAMP WITH TIME ZONE \'epoch\' + $1 * interval \'1 second\';'; AS 'SELECT TIMESTAMP WITH TIME ZONE \'epoch\' + $1 * interval \'1 second\' ;';