77define ('MAD_ENV ' , isset ($ _SERVER ['MAD_ENV ' ]) ? $ _SERVER ['MAD_ENV ' ] : 'development ' );
88require_once dirname (dirname (__FILE__ )).'/config/environment.php ' ;
99
10- // sets $config
11- $ config = Horde_Yaml::loadFile (MAD_ROOT .'/config/database.yml ' );
12- $ spec = $ config [MAD_ENV ];
13-
14- function findCmd ($ command )
10+ function find_command ($ command )
1511{
1612 $ dirsOnPath = explode (PATH_SEPARATOR , getenv ("PATH " ));
1713
1814 foreach ($ dirsOnPath as $ dir ) {
19- $ cmd = $ dir. DIRECTORY_SEPARATOR . $ command ;
15+ $ cmd = $ dir . DIRECTORY_SEPARATOR . $ command ;
2016 if (is_executable ($ cmd )) { return $ cmd ; }
21- }
22- die ("Couldn't find database client: $ command. $ check your \$ PATH and try again. " );
17+ }
18+ die ("Couldn't find database client: $ command\n " );
2319}
2420
25- if (strstr ($ spec ["adapter " ], "mysql " )) {
21+ function mysql_console ($ spec )
22+ {
2623 $ args = array (
2724 'host ' => '--host ' ,
2825 'port ' => '--port ' ,
2926 'socket ' => '--socket ' ,
3027 'username ' => '--user ' ,
3128 'encoding ' => '--default-character-set ' ,
32- 'password ' => '--password ' ,
29+ 'password ' => '--password ' ,
3330 );
3431
3532 $ opts = array ();
3633 foreach ($ args as $ opt => $ arg ) {
37- if (isset ($ spec [$ opt ])) {
34+ if (isset ($ spec [$ opt ]) && $ spec [ $ opt ] !== '' ) {
3835 $ opts [] = "$ arg= " .$ spec [$ opt ];
3936 }
4037 }
4138 $ opts [] = $ spec ["database " ];
4239
43- pcntl_exec (findCmd ("mysql " ), $ opts );
40+ pcntl_exec (find_command ("mysql " ), $ opts );
41+ }
4442
43+ function sqlite_console ($ spec )
44+ {
45+ $ db = $ spec ["database " ];
46+ if ($ db [0 ] != '/ ' && $ db [0 ] != ': ' ) {
47+ $ db = MAD_ROOT . '/ ' . $ db ;
48+ }
49+
50+ pcntl_exec (find_command ("sqlite3 " ), array ($ db ));
51+ }
52+
53+ $ config = Horde_Yaml::loadFile (MAD_ROOT .'/config/database.yml ' );
54+ $ spec = $ config [MAD_ENV ];
55+ $ adapter = str_replace ('pdo_ ' , '' , $ spec ['adapter ' ]);
56+ $ func = $ adapter . '_console ' ;
57+
58+ if (function_exists ($ func )) {
59+ $ func ($ spec );
4560} else {
46- die ("Unknown command-line client for " .$ spec ['database ' ] );
47- }
61+ die ("Unknown database adapter: " .$ spec ['adapter ' ]. "\n" );
62+ }
0 commit comments