-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
sqlite.class.php
1138 lines (1004 loc) · 38.5 KB
/
sqlite.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Copyright (C) 2001 Fabien Seisen <seisen@linuxfr.org>
* Copyright (C) 2002-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
* Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
*
* 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
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/core/db/sqlite.class.php
* \brief Class file to manage Dolibarr database access for a Sqlite database
*/
require_once DOL_DOCUMENT_ROOT .'/core/db/DoliDB.class.php';
/**
* Class to manage Dolibarr database access for a Sqlite database
*/
class DoliDBSqlite extends DoliDB
{
//! Database type
public $type='sqlite';
//! Database label
const LABEL='PDO Sqlite';
//! Version min database
const VERSIONMIN='3.0.0';
//! Resultset of last query
private $_results;
/**
* Constructor.
* This create an opened connexion to a database server and eventually to a database
*
* @param string $type Type of database (mysql, pgsql...)
* @param string $host Address of database server
* @param string $user Nom de l'utilisateur autorise
* @param string $pass Mot de passe
* @param string $name Nom de la database
* @param int $port Port of database server
* @return int 1 if OK, 0 if not
*/
function __construct($type, $host, $user, $pass, $name='', $port=0)
{
global $conf,$langs;
// Note that having "static" property for "$forcecharset" and "$forcecollate" will make error here in strict mode, so they are not static
if (! empty($conf->db->character_set)) $this->forcecharset=$conf->db->character_set;
if (! empty($conf->db->dolibarr_main_db_collation)) $this->forcecollate=$conf->db->dolibarr_main_db_collation;
$this->database_user=$user;
$this->transaction_opened=0;
//print "Name DB: $host,$user,$pass,$name<br>";
/*if (! function_exists("sqlite_query"))
{
$this->connected = 0;
$this->ok = 0;
$this->error="Sqlite PHP functions for using Sqlite driver are not available in this version of PHP. Try to use another driver.";
dol_syslog(get_class($this)."::DoliDBSqlite : Sqlite PHP functions for using Sqlite driver are not available in this version of PHP. Try to use another driver.",LOG_ERR);
return $this->ok;
}*/
/*if (! $host)
{
$this->connected = 0;
$this->ok = 0;
$this->error=$langs->trans("ErrorWrongHostParameter");
dol_syslog(get_class($this)."::DoliDBSqlite : Erreur Connect, wrong host parameters",LOG_ERR);
return $this->ok;
}*/
// Essai connexion serveur
// We do not try to connect to database, only to server. Connect to database is done later in constrcutor
$this->db = $this->connect($host, $user, $pass, $name, $port);
if ($this->db)
{
$this->connected = 1;
$this->ok = 1;
$this->database_selected = 1;
$this->database_name = $name;
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
else
{
// host, login ou password incorrect
$this->connected = 0;
$this->ok = 0;
$this->database_selected = 0;
$this->database_name = '';
//$this->error=sqlite_connect_error();
dol_syslog(get_class($this)."::DoliDBSqlite : Erreur Connect ".$this->error,LOG_ERR);
}
return $this->ok;
}
/**
* Convert a SQL request in Mysql syntax to native syntax
*
* @param string $line SQL request line to convert
* @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
* @return string SQL request line converted
*/
static function convertSQLFromMysql($line,$type='ddl')
{
// Removed empty line if this is a comment line for SVN tagging
if (preg_match('/^--\s\$Id/i',$line)) {
return '';
}
// Return line if this is a comment
if (preg_match('/^#/i',$line) || preg_match('/^$/i',$line) || preg_match('/^--/i',$line))
{
return $line;
}
if ($line != "")
{
if ($type == 'auto')
{
if (preg_match('/ALTER TABLE/i',$line)) $type='dml';
else if (preg_match('/CREATE TABLE/i',$line)) $type='dml';
else if (preg_match('/DROP TABLE/i',$line)) $type='dml';
}
if ($type == 'dml')
{
$line=preg_replace('/\s/',' ',$line); // Replace tabulation with space
// we are inside create table statement so lets process datatypes
if (preg_match('/(ISAM|innodb)/i',$line)) { // end of create table sequence
$line=preg_replace('/\)[\s\t]*type[\s\t]*=[\s\t]*(MyISAM|innodb);/i',');',$line);
$line=preg_replace('/\)[\s\t]*engine[\s\t]*=[\s\t]*(MyISAM|innodb);/i',');',$line);
$line=preg_replace('/,$/','',$line);
}
// Process case: "CREATE TABLE llx_mytable(rowid integer NOT NULL AUTO_INCREMENT PRIMARY KEY,code..."
if (preg_match('/[\s\t\(]*(\w*)[\s\t]+int.*auto_increment/i',$line,$reg)) {
$newline=preg_replace('/([\s\t\(]*)([a-zA-Z_0-9]*)[\s\t]+int.*auto_increment[^,]*/i','\\1 \\2 SERIAL PRIMARY KEY',$line);
//$line = "-- ".$line." replaced by --\n".$newline;
$line=$newline;
}
// tinyint type conversion
$line=str_replace('tinyint','smallint',$line);
// nuke unsigned
$line=preg_replace('/(int\w+|smallint)\s+unsigned/i','\\1',$line);
// blob -> text
$line=preg_replace('/\w*blob/i','text',$line);
// tinytext/mediumtext -> text
$line=preg_replace('/tinytext/i','text',$line);
$line=preg_replace('/mediumtext/i','text',$line);
// change not null datetime field to null valid ones
// (to support remapping of "zero time" to null
$line=preg_replace('/datetime not null/i','datetime',$line);
$line=preg_replace('/datetime/i','timestamp',$line);
// double -> numeric
$line=preg_replace('/^double/i','numeric',$line);
$line=preg_replace('/(\s*)double/i','\\1numeric',$line);
// float -> numeric
$line=preg_replace('/^float/i','numeric',$line);
$line=preg_replace('/(\s*)float/i','\\1numeric',$line);
// unique index(field1,field2)
if (preg_match('/unique index\s*\((\w+\s*,\s*\w+)\)/i',$line))
{
$line=preg_replace('/unique index\s*\((\w+\s*,\s*\w+)\)/i','UNIQUE\(\\1\)',$line);
}
// We remove end of requests "AFTER fieldxxx"
$line=preg_replace('/AFTER [a-z0-9_]+/i','',$line);
// We remove start of requests "ALTER TABLE tablexxx" if this is a DROP INDEX
$line=preg_replace('/ALTER TABLE [a-z0-9_]+ DROP INDEX/i','DROP INDEX',$line);
// Translate order to rename fields
if (preg_match('/ALTER TABLE ([a-z0-9_]+) CHANGE(?: COLUMN)? ([a-z0-9_]+) ([a-z0-9_]+)(.*)$/i',$line,$reg))
{
$line = "-- ".$line." replaced by --\n";
$line.= "ALTER TABLE ".$reg[1]." RENAME COLUMN ".$reg[2]." TO ".$reg[3];
}
// Translate order to modify field format
if (preg_match('/ALTER TABLE ([a-z0-9_]+) MODIFY(?: COLUMN)? ([a-z0-9_]+) (.*)$/i',$line,$reg))
{
$line = "-- ".$line." replaced by --\n";
$newreg3=$reg[3];
$newreg3=preg_replace('/ DEFAULT NULL/i','',$newreg3);
$newreg3=preg_replace('/ NOT NULL/i','',$newreg3);
$newreg3=preg_replace('/ NULL/i','',$newreg3);
$newreg3=preg_replace('/ DEFAULT 0/i','',$newreg3);
$newreg3=preg_replace('/ DEFAULT \'[0-9a-zA-Z_@]*\'/i','',$newreg3);
$line.= "ALTER TABLE ".$reg[1]." ALTER COLUMN ".$reg[2]." TYPE ".$newreg3;
// TODO Add alter to set default value or null/not null if there is this in $reg[3]
}
// alter table add primary key (field1, field2 ...) -> We create a unique index instead as dynamic creation of primary key is not supported
// ALTER TABLE llx_dolibarr_modules ADD PRIMARY KEY pk_dolibarr_modules (numero, entity);
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+PRIMARY\s+KEY\s*(.*)\s*\((.*)$/i',$line,$reg))
{
$line = "-- ".$line." replaced by --\n";
$line.= "CREATE UNIQUE INDEX ".$reg[2]." ON ".$reg[1]."(".$reg[3];
}
// Translate order to drop foreign keys
// ALTER TABLE llx_dolibarr_modules DROP FOREIGN KEY fk_xxx;
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*DROP\s+FOREIGN\s+KEY\s*(.*)$/i',$line,$reg))
{
$line = "-- ".$line." replaced by --\n";
$line.= "ALTER TABLE ".$reg[1]." DROP CONSTRAINT ".$reg[2];
}
// alter table add [unique] [index] (field1, field2 ...)
// ALTER TABLE llx_accountingaccount ADD INDEX idx_accountingaccount_fk_pcg_version (fk_pcg_version)
if (preg_match('/ALTER\s+TABLE\s*(.*)\s*ADD\s+(UNIQUE INDEX|INDEX|UNIQUE)\s+(.*)\s*\(([\w,\s]+)\)/i',$line,$reg))
{
$fieldlist=$reg[4];
$idxname=$reg[3];
$tablename=$reg[1];
$line = "-- ".$line." replaced by --\n";
$line.= "CREATE ".(preg_match('/UNIQUE/',$reg[2])?'UNIQUE ':'')."INDEX ".$idxname." ON ".$tablename." (".$fieldlist.")";
}
}
// To have postgresql case sensitive
$line=str_replace(' LIKE \'',' ILIKE \'',$line);
// Delete using criteria on other table must not declare twice the deleted table
// DELETE FROM tabletodelete USING tabletodelete, othertable -> DELETE FROM tabletodelete USING othertable
if (preg_match('/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i',$line,$reg))
{
if ($reg[1] == $reg[2]) // If same table, we remove second one
{
$line=preg_replace('/DELETE FROM ([a-z_]+) USING ([a-z_]+), ([a-z_]+)/i','DELETE FROM \\1 USING \\3', $line);
}
}
// Remove () in the tables in FROM if one table
$line=preg_replace('/FROM\s*\((([a-z_]+)\s+as\s+([a-z_]+)\s*)\)/i','FROM \\1',$line);
//print $line."\n";
// Remove () in the tables in FROM if two table
$line=preg_replace('/FROM\s*\(([a-z_]+\s+as\s+[a-z_]+)\s*,\s*([a-z_]+\s+as\s+[a-z_]+\s*)\)/i','FROM \\1, \\2',$line);
//print $line."\n";
// Remove () in the tables in FROM if two table
$line=preg_replace('/FROM\s*\(([a-z_]+\s+as\s+[a-z_]+)\s*,\s*([a-z_]+\s+as\s+[a-z_]+\s*),\s*([a-z_]+\s+as\s+[a-z_]+\s*)\)/i','FROM \\1, \\2, \\3',$line);
//print $line."\n";
//print "type=".$type." newline=".$line."<br>\n";
}
return $line;
}
/**
* Select a database
*
* @param string $database Name of database
* @return boolean true if OK, false if KO
*/
function select_db($database)
{
dol_syslog(get_class($this)."::select_db database=".$database, LOG_DEBUG);
return sqlite_select_db($this->db,$database);
}
/**
* Connexion to server
*
* @param string $host database server host
* @param string $login login
* @param string $passwd password
* @param string $name name of database (not used for mysql, used for pgsql)
* @param string $port Port of database server
* @return resource Database access handler
* @see close
*/
function connect($host, $login, $passwd, $name, $port=0)
{
global $conf,$main_data_dir;
dol_syslog(get_class($this)."::connect name=".$name,LOG_DEBUG);
$dir=$main_data_dir;
if (empty($dir)) $dir=DOL_DATA_ROOT;
// With sqlite, port must be in connect parameters
//if (! $newport) $newport=3306;
try {
/*** connect to SQLite database ***/
$this->db = new PDO("sqlite:".$dir.'/database_'.$name.'.sdb');
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$this->error='PDO SQLITE '.$e->getMessage().' current dir='.$dir.'/database_'.$name.'.sdb';
return '';
}
//print "Resultat fonction connect: ".$this->db;
return $this->db;
}
/**
* Return version of database server
*
* @return string Version string
*/
function getVersion()
{
$resql=$this->query('SELECT sqlite_version() as sqliteversion');
$row=$this->fetch_row($resql);
return $row[0];
}
/**
* Return version of database client driver
*
* @return string Version string
*/
function getDriverInfo()
{
// FIXME: Dummy method
// TODO: Implement
return '';
}
/**
* Close database connexion
*
* @return boolean True if disconnect successfull, false otherwise
* @see connect
*/
function close()
{
if ($this->db)
{
if ($this->transaction_opened > 0) dol_syslog(get_class($this)."::close Closing a connection with an opened transaction depth=".$this->transaction_opened,LOG_ERR);
$this->connected=0;
$this->db=null; // Clean this->db
return true;
}
return false;
}
/**
* Execute a SQL request and return the resultset
*
* @param string $query SQL query string
* @param int $usesavepoint 0=Default mode, 1=Run a savepoint before and a rollbock to savepoint if error (this allow to have some request with errors inside global transactions).
* Note that with Mysql, this parameter is not used as Myssql can already commit a transaction even if one request is in error, without using savepoints.
* @param string $type Type of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
* @return resource Resultset of answer
*/
function query($query,$usesavepoint=0,$type='auto')
{
$errmsg='';
$ret='';
$query = trim($query);
$this->error = 0;
// Convert MySQL syntax to SQLite syntax
$query=$this->convertSQLFromMysql($query,$type);
//print "After convertSQLFromMysql:\n".$query."<br>\n";
dol_syslog('sql='.$query, LOG_DEBUG);
// Ordre SQL ne necessitant pas de connexion a une base (exemple: CREATE DATABASE)
try {
//$ret = $this->db->exec($query);
$ret = $this->db->query($query); // $ret is a PDO object
}
catch(PDOException $e)
{
$this->error=$e->getMessage();
}
if (! preg_match("/^COMMIT/i",$query) && ! preg_match("/^ROLLBACK/i",$query))
{
// Si requete utilisateur, on la sauvegarde ainsi que son resultset
if (! is_object($ret) || $this->error)
{
$this->lastqueryerror = $query;
$this->lasterror = $this->error();
$this->lasterrno = $this->errno();
dol_syslog(get_class($this)."::query SQL Error query: ".$query, LOG_ERR);
$errormsg = get_class($this)."::query SQL Error message: ".$this->lasterror;
if (preg_match('/[0-9]/',$this->lasterrno)) {
$errormsg .= ' ('.$this->lasterrno.')';
}
dol_syslog($errormsg, LOG_ERR);
}
$this->lastquery=$query;
$this->_results = $ret;
}
return $ret;
}
/**
* Renvoie la ligne courante (comme un objet) pour le curseur resultset
*
* @param Resultset $resultset Curseur de la requete voulue
* @return Object Object result line or false if KO or end of cursor
*/
function fetch_object($resultset)
{
// Si le resultset n'est pas fourni, on prend le dernier utilise sur cette connexion
if (! is_object($resultset)) { $resultset=$this->_results; }
return $resultset->fetch(PDO::FETCH_OBJ);
}
/**
* Return datas as an array
*
* @param Resultset $resultset Resultset of request
* @return array Array
*/
function fetch_array($resultset)
{
// If resultset not provided, we take the last used by connexion
if (! is_object($resultset)) { $resultset=$this->_results; }
return $resultset->fetch(PDO::FETCH_ASSOC);
}
/**
* Return datas as an array
*
* @param Resultset $resultset Resultset of request
* @return array Array
*/
function fetch_row($resultset)
{
// If resultset not provided, we take the last used by connexion
if (! is_bool($resultset))
{
if (! is_object($resultset)) { $resultset=$this->_results; }
return $resultset->fetch(PDO::FETCH_NUM);
}
else
{
// si le curseur est un booleen on retourne la valeur 0
return 0;
}
}
/**
* Return number of lines for result of a SELECT
*
* @param Resultset $resultset Resulset of requests
* @return int Nb of lines
* @see affected_rows
*/
function num_rows($resultset)
{
// If resultset not provided, we take the last used by connexion
if (! is_object($resultset)) { $resultset=$this->_results; }
return $resultset->rowCount();
}
/**
* Return number of lines for result of a SELECT
*
* @param Resultset $resultset Resulset of requests
* @return int Nb of lines
* @see affected_rows
*/
function affected_rows($resultset)
{
// If resultset not provided, we take the last used by connexion
if (! is_object($resultset)) { $resultset=$this->_results; }
// mysql necessite un link de base pour cette fonction contrairement
// a pqsql qui prend un resultset
return $resultset->rowCount();
}
/**
* Free last resultset used.
*
* @param resultset $resultset Curseur de la requete voulue
* @return void
*/
function free($resultset=0)
{
// If resultset not provided, we take the last used by connexion
if (! is_object($resultset)) { $resultset=$this->_results; }
// Si resultset en est un, on libere la memoire
if (is_object($resultset)) $resultset->closeCursor();
}
/**
* Escape a string to insert data
*
* @param string $stringtoencode String to escape
* @return string String escaped
*/
function escape($stringtoencode)
{
return $this->db->quote($stringtoencode);
}
/**
* Renvoie le code erreur generique de l'operation precedente.
*
* @return string $error_num (Exemples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...)
*/
function errno()
{
if (! $this->connected) {
// Si il y a eu echec de connexion, $this->db n'est pas valide.
return 'DB_ERROR_FAILED_TO_CONNECT';
}
else {
// Constants to convert error code to a generic Dolibarr error code
/*$errorcode_map = array(
1004 => 'DB_ERROR_CANNOT_CREATE',
1005 => 'DB_ERROR_CANNOT_CREATE',
1006 => 'DB_ERROR_CANNOT_CREATE',
1007 => 'DB_ERROR_ALREADY_EXISTS',
1008 => 'DB_ERROR_CANNOT_DROP',
1025 => 'DB_ERROR_NO_FOREIGN_KEY_TO_DROP',
1044 => 'DB_ERROR_ACCESSDENIED',
1046 => 'DB_ERROR_NODBSELECTED',
1048 => 'DB_ERROR_CONSTRAINT',
'HY000' => 'DB_ERROR_TABLE_ALREADY_EXISTS',
1051 => 'DB_ERROR_NOSUCHTABLE',
1054 => 'DB_ERROR_NOSUCHFIELD',
1060 => 'DB_ERROR_COLUMN_ALREADY_EXISTS',
1061 => 'DB_ERROR_KEY_NAME_ALREADY_EXISTS',
1062 => 'DB_ERROR_RECORD_ALREADY_EXISTS',
1064 => 'DB_ERROR_SYNTAX',
1068 => 'DB_ERROR_PRIMARY_KEY_ALREADY_EXISTS',
1075 => 'DB_ERROR_CANT_DROP_PRIMARY_KEY',
1091 => 'DB_ERROR_NOSUCHFIELD',
1100 => 'DB_ERROR_NOT_LOCKED',
1136 => 'DB_ERROR_VALUE_COUNT_ON_ROW',
1146 => 'DB_ERROR_NOSUCHTABLE',
1216 => 'DB_ERROR_NO_PARENT',
1217 => 'DB_ERROR_CHILD_EXISTS',
1451 => 'DB_ERROR_CHILD_EXISTS'
);
if (isset($errorcode_map[$this->db->errorCode()]))
{
return $errorcode_map[$this->db->errorCode()];
}*/
$errno=$this->db->errorCode();
if ($errno=='HY000')
{
if (preg_match('/table.*already exists/i',$this->error)) return 'DB_ERROR_TABLE_ALREADY_EXISTS';
elseif (preg_match('/index.*already exists/i',$this->error)) return 'DB_ERROR_KEY_NAME_ALREADY_EXISTS';
elseif (preg_match('/syntax error/i',$this->error)) return 'DB_ERROR_SYNTAX';
}
if ($errno=='23000')
{
if (preg_match('/column.* not unique/i',$this->error)) return 'DB_ERROR_RECORD_ALREADY_EXISTS';
elseif (preg_match('/PRIMARY KEY must be unique/i',$this->error)) return 'DB_ERROR_RECORD_ALREADY_EXISTS';
}
return ($errno?'DB_ERROR_'.$errno:'0');
}
}
/**
* Renvoie le texte de l'erreur mysql de l'operation precedente.
*
* @return string $error_text
*/
function error()
{
if (! $this->connected) {
// Si il y a eu echec de connexion, $this->db n'est pas valide pour sqlite_error.
return 'Not connected. Check setup parameters in conf/conf.php file and your sqlite version';
}
else {
return $this->error;
}
}
/**
* Get last ID after an insert INSERT
*
* @param string $tab Table name concerned by insert. Ne sert pas sous MySql mais requis pour compatibilite avec Postgresql
* @param string $fieldid Field name
* @return int Id of row
*/
function last_insert_id($tab,$fieldid='rowid')
{
return $this->db->lastInsertId();
}
/**
* Encrypt sensitive data in database
* Warning: This function includes the escape, so it must use direct value
*
* @param string $fieldorvalue Field name or value to encrypt
* @param int $withQuotes Return string with quotes
* @return return XXX(field) or XXX('value') or field or 'value'
*/
function encrypt($fieldorvalue, $withQuotes=0)
{
global $conf;
// Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
$cryptType = ($conf->db->dolibarr_main_db_encryption?$conf->db->dolibarr_main_db_encryption:0);
//Encryption key
$cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:'');
$return = ($withQuotes?"'":"").$this->escape($fieldorvalue).($withQuotes?"'":"");
if ($cryptType && !empty($cryptKey))
{
if ($cryptType == 2)
{
$return = 'AES_ENCRYPT('.$return.',\''.$cryptKey.'\')';
}
else if ($cryptType == 1)
{
$return = 'DES_ENCRYPT('.$return.',\''.$cryptKey.'\')';
}
}
return $return;
}
/**
* Decrypt sensitive data in database
*
* @param string $value Value to decrypt
* @return string Decrypted value if used
*/
function decrypt($value)
{
global $conf;
// Type of encryption (2: AES (recommended), 1: DES , 0: no encryption)
$cryptType = ($conf->db->dolibarr_main_db_encryption?$conf->db->dolibarr_main_db_encryption:0);
//Encryption key
$cryptKey = (!empty($conf->db->dolibarr_main_db_cryptkey)?$conf->db->dolibarr_main_db_cryptkey:'');
$return = $value;
if ($cryptType && !empty($cryptKey))
{
if ($cryptType == 2)
{
$return = 'AES_DECRYPT('.$value.',\''.$cryptKey.'\')';
}
else if ($cryptType == 1)
{
$return = 'DES_DECRYPT('.$value.',\''.$cryptKey.'\')';
}
}
return $return;
}
/**
* Return connexion ID
*
* @return string Id connexion
*/
function DDLGetConnectId()
{
return '?';
}
/**
* Create a new database
* Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated
* We force to create database with charset this->forcecharset and collate this->forcecollate
*
* @param string $database Database name to create
* @param string $charset Charset used to store data
* @param string $collation Charset used to sort data
* @param string $owner Username of database owner
* @return resource resource defined if OK, null if KO
*/
function DDLCreateDb($database,$charset='',$collation='',$owner='')
{
if (empty($charset)) $charset=$this->forcecharset;
if (empty($collation)) $collation=$this->forcecollate;
// ALTER DATABASE dolibarr_db DEFAULT CHARACTER SET latin DEFAULT COLLATE latin1_swedish_ci
$sql = 'CREATE DATABASE '.$database;
$sql.= ' DEFAULT CHARACTER SET '.$charset.' DEFAULT COLLATE '.$collation;
dol_syslog($sql,LOG_DEBUG);
$ret=$this->query($sql);
if (! $ret)
{
// We try again for compatibility with Mysql < 4.1.1
$sql = 'CREATE DATABASE '.$database;
$ret=$this->query($sql);
dol_syslog($sql,LOG_DEBUG);
}
return $ret;
}
/**
* List tables into a database
*
* @param string $database Name of database
* @param string $table Nmae of table filter ('xxx%')
* @return resource Resource
*/
function DDLListTables($database, $table='')
{
$listtables=array();
$like = '';
if ($table) $like = "LIKE '".$table."'";
$sql="SHOW TABLES FROM ".$database." ".$like.";";
//print $sql;
$result = $this->query($sql);
while($row = $this->fetch_row($result))
{
$listtables[] = $row[0];
}
return $listtables;
}
/**
* List information of columns into a table.
*
* @param string $table Name of table
* @return array Tableau des informations des champs de la table
* TODO modify for sqlite
*/
function DDLInfoTable($table)
{
$infotables=array();
$sql="SHOW FULL COLUMNS FROM ".$table.";";
dol_syslog($sql,LOG_DEBUG);
$result = $this->query($sql);
while($row = $this->fetch_row($result))
{
$infotables[] = $row;
}
return $infotables;
}
/**
* Create a table into database
*
* @param string $table Nom de la table
* @param array $fields Tableau associatif [nom champ][tableau des descriptions]
* @param string $primary_key Nom du champ qui sera la clef primaire
* @param string $type Type de la table
* @param array $unique_keys Tableau associatifs Nom de champs qui seront clef unique => valeur
* @param array $fulltext_keys Tableau des Nom de champs qui seront indexes en fulltext
* @param string $keys Tableau des champs cles noms => valeur
* @return int <0 if KO, >=0 if OK
*/
function DDLCreateTable($table,$fields,$primary_key,$type,$unique_keys="",$fulltext_keys="",$keys="")
{
// cles recherchees dans le tableau des descriptions (fields) : type,value,attribute,null,default,extra
// ex. : $fields['rowid'] = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
$sql = "create table ".$table."(";
$i=0;
foreach($fields as $field_name => $field_desc)
{
$sqlfields[$i] = $field_name." ";
$sqlfields[$i] .= $field_desc['type'];
if( preg_match("/^[^\s]/i",$field_desc['value']))
$sqlfields[$i] .= "(".$field_desc['value'].")";
else if( preg_match("/^[^\s]/i",$field_desc['attribute']))
$sqlfields[$i] .= " ".$field_desc['attribute'];
else if( preg_match("/^[^\s]/i",$field_desc['default']))
{
if(preg_match("/null/i",$field_desc['default']))
$sqlfields[$i] .= " default ".$field_desc['default'];
else
$sqlfields[$i] .= " default '".$field_desc['default']."'";
}
else if( preg_match("/^[^\s]/i",$field_desc['null']))
$sqlfields[$i] .= " ".$field_desc['null'];
else if( preg_match("/^[^\s]/i",$field_desc['extra']))
$sqlfields[$i] .= " ".$field_desc['extra'];
$i++;
}
if($primary_key != "")
$pk = "primary key(".$primary_key.")";
if($unique_keys != "")
{
$i = 0;
foreach($unique_keys as $key => $value)
{
$sqluq[$i] = "UNIQUE KEY '".$key."' ('".$value."')";
$i++;
}
}
if($keys != "")
{
$i = 0;
foreach($keys as $key => $value)
{
$sqlk[$i] = "KEY ".$key." (".$value.")";
$i++;
}
}
$sql .= implode(',',$sqlfields);
if($primary_key != "")
$sql .= ",".$pk;
if($unique_keys != "")
$sql .= ",".implode(',',$sqluq);
if($keys != "")
$sql .= ",".implode(',',$sqlk);
$sql .=") type=".$type;
dol_syslog($sql,LOG_DEBUG);
if(! $this -> query($sql))
return -1;
else
return 1;
}
/**
* Return a pointer of line with description of a table or field
*
* @param string $table Name of table
* @param string $field Optionnel : Name of field if we want description of field
* @return resource Resource
*/
function DDLDescTable($table,$field="")
{
$sql="DESC ".$table." ".$field;
dol_syslog(get_class($this)."::DDLDescTable ".$sql,LOG_DEBUG);
$this->_results = $this->query($sql);
return $this->_results;
}
/**
* Create a new field into table
*
* @param string $table Name of table
* @param string $field_name Name of field to add
* @param string $field_desc Tableau associatif de description du champ a inserer[nom du parametre][valeur du parametre]
* @param string $field_position Optionnel ex.: "after champtruc"
* @return int <0 if KO, >0 if OK
*/
function DDLAddField($table,$field_name,$field_desc,$field_position="")
{
// cles recherchees dans le tableau des descriptions (field_desc) : type,value,attribute,null,default,extra
// ex. : $field_desc = array('type'=>'int','value'=>'11','null'=>'not null','extra'=> 'auto_increment');
$sql= "ALTER TABLE ".$table." ADD ".$field_name." ";
$sql.= $field_desc['type'];
if(preg_match("/^[^\s]/i",$field_desc['value']))
if (! in_array($field_desc['type'],array('date','datetime')))
{
$sql.= "(".$field_desc['value'].")";
}
if(preg_match("/^[^\s]/i",$field_desc['attribute']))
$sql.= " ".$field_desc['attribute'];
if(preg_match("/^[^\s]/i",$field_desc['null']))
$sql.= " ".$field_desc['null'];
if(preg_match("/^[^\s]/i",$field_desc['default']))
{
if(preg_match("/null/i",$field_desc['default']))
$sql.= " default ".$field_desc['default'];
else
$sql.= " default '".$field_desc['default']."'";
}
if(preg_match("/^[^\s]/i",$field_desc['extra']))
$sql.= " ".$field_desc['extra'];
$sql.= " ".$field_position;
dol_syslog(get_class($this)."::DDLAddField ".$sql,LOG_DEBUG);
if(! $this->query($sql))
{
return -1;
}
else
{
return 1;
}
}
/**
* Update format of a field into a table
*
* @param string $table Name of table
* @param string $field_name Name of field to modify
* @param string $field_desc Array with description of field format
* @return int <0 if KO, >0 if OK
*/
function DDLUpdateField($table,$field_name,$field_desc)
{
$sql = "ALTER TABLE ".$table;
$sql .= " MODIFY COLUMN ".$field_name." ".$field_desc['type'];
if ($field_desc['type'] == 'tinyint' || $field_desc['type'] == 'int' || $field_desc['type'] == 'varchar') {
$sql.="(".$field_desc['value'].")";
}
dol_syslog(get_class($this)."::DDLUpdateField ".$sql,LOG_DEBUG);
if (! $this->query($sql))
return -1;
else
return 1;
}
/**
* Drop a field from table
*
* @param string $table Name of table
* @param string $field_name Name of field to drop
* @return int <0 if KO, >0 if OK
*/
function DDLDropField($table,$field_name)
{
$sql= "ALTER TABLE ".$table." DROP COLUMN `".$field_name."`";
dol_syslog(get_class($this)."::DDLDropField ".$sql,LOG_DEBUG);
if (! $this->query($sql))
{
$this->error=$this->lasterror();
return -1;
}
else return 1;
}
/**
* Create a user and privileges to connect to database (even if database does not exists yet)
*
* @param string $dolibarr_main_db_host Ip serveur
* @param string $dolibarr_main_db_user Nom user a creer
* @param string $dolibarr_main_db_pass Mot de passe user a creer
* @param string $dolibarr_main_db_name Database name where user must be granted
* @return int <0 if KO, >=0 if OK
*/
function DDLCreateUser($dolibarr_main_db_host,$dolibarr_main_db_user,$dolibarr_main_db_pass,$dolibarr_main_db_name)
{
$sql = "INSERT INTO user ";
$sql.= "(Host,User,password,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Index_Priv,Alter_priv,Lock_tables_priv)";
$sql.= " VALUES ('".$this->escape($dolibarr_main_db_host)."','".$this->escape($dolibarr_main_db_user)."',password('".addslashes($dolibarr_main_db_pass)."')";
$sql.= ",'Y','Y','Y','Y','Y','Y','Y','Y','Y')";
dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG); // No sql to avoid password in log
$resql=$this->query($sql);
if (! $resql)
{
return -1;
}
$sql = "INSERT INTO db ";
$sql.= "(Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv,Index_Priv,Alter_priv,Lock_tables_priv)";
$sql.= " VALUES ('".$this->escape($dolibarr_main_db_host)."','".$this->escape($dolibarr_main_db_name)."','".addslashes($dolibarr_main_db_user)."'";
$sql.= ",'Y','Y','Y','Y','Y','Y','Y','Y','Y')";
dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG);
$resql=$this->query($sql);
if (! $resql)
{
return -1;
}
$sql="FLUSH Privileges";
dol_syslog(get_class($this)."::DDLCreateUser", LOG_DEBUG);