Example #1
0
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim(array('mode' => 'development'));
// Only invoked if mode is "production"
$app->configureMode('production', function () use($app) {
    $app->config(array('log.enable' => false, 'debug' => false, 'config.path' => 'config/prod/'));
});
// Only invoked if mode is "development"
$app->configureMode('development', function () use($app) {
    $app->config(array('log.enable' => false, 'debug' => true, 'config.path' => 'config/dev/'));
});
// Define mysql connector
$app->container->singleton('mysql', function () {
    $app = \Slim\Slim::getInstance();
    $config = parse_ini_file(getAppConfigFile('mysql.ini'));
    $pdo = new PDO("mysql:host=" . $config['db.hostname'] . ";dbname=" . $config['db.schema'], $config['db.user'], $config['db.password']);
    // set the character set to utf8 to ensure proper json encoding
    $pdo->exec("SET NAMES 'utf8'");
    return $pdo;
});
/*$app->container->singleton('log', function() {
$app = \Slim\Slim::getInstance();
Logger::configure(getAppConfigFile('log4php-config.xml'));
return Logger::getLogger('default');
});*/
// FIXME: Implement separation of view and data
// TODO: move index.html into the /views directory and
// point the templates to /views
$view = $app->view();
$view->setTemplatesDirectory('./');
Example #2
0
// Only invoked if mode is "development"
$app->configureMode('development', function () use($app) {
    $app->config(array('log.enable' => false, 'debug' => true, 'config.path' => 'config/dev/'));
});
// Define mysql connector
$app->container->singleton('mysql', function () {
    $app = \Slim\Slim::getInstance();
    $config = parse_ini_file(getAppConfigFile('mysql.ini'));
    $pdo = new PDO("mysql:host=" . $config['db.hostname'] . ";dbname=" . $config['db.schema'], $config['db.user'], $config['db.password']);
    // set the character set to utf8 to ensure proper json encoding
    $pdo->exec("SET NAMES 'utf8'");
    return $pdo;
});
$app->container->singleton('log', function () {
    $app = \Slim\Slim::getInstance();
    Logger::configure(getAppConfigFile('log4php-config.xml'));
    return Logger::getLogger('default');
});
// FIXME: Implement separation of view and data
// TODO: move index.html into the /views directory and
// point the templates to /views
$view = $app->view();
$view->setTemplatesDirectory('./');
function executeSql($query, array $params = array())
{
    $app = \Slim\Slim::getInstance();
    $app->log->debug(sprintf("Executing query: %s with params: %s", $query, json_encode($params)));
    $mysql = $app->mysql;
    $handler = $mysql->prepare($query);
    $handler->execute($params);
    return $handler->fetchAll(PDO::FETCH_OBJ);
Example #3
0
function executeQueries($statements)
{
    $logger = Logger::getLogger("main");
    $config = parse_ini_file(getAppConfigFile('mysql.ini'));
    $logger->info(sprintf("Connecting to database with mysql.ini: %s", json_encode($config)));
    $pdo = new PDO("mysql:host=" . $config['db.hostname'], $config['db.user'], $config['db.password']);
    $logger->info("Setting character set to UTF-8");
    // set the character set to utf8
    $pdo->exec("SET NAMES 'utf8'");
    // Create schemas
    $scripts = ["drop database if exists blueeconomics;", "create database blueeconomics;"];
    $logger->info(sprintf("Recreating schema for blueeconomics:%s", json_encode($scripts)));
    foreach ($scripts as $script) {
        $status = $pdo->exec($script);
        if ($status === false) {
            printf("Failed to execute %s", $script);
        }
    }
    // execute sql scripts
    $scripts = ['ddl.sql', '../DDL_TABDELIMITED_DATA/blueecondbDUMP.sql'];
    $logger->info(sprintf("Loading SQL scripts: %s", json_encode($scripts)));
    foreach ($scripts as $script) {
        $ddl = file_get_contents($script);
        $status = $pdo->exec($ddl);
        if ($status === false) {
            printf("Failed to execute %s", $script);
        }
    }
    // execute generated statements
    $logger->info(sprintf("Inserting %d generated records", count($statements)));
    foreach ($statements as $stmt) {
        $status = $pdo->exec($stmt);
        if ($status === false) {
            printf("Failed to execute %s", $stmt);
        }
    }
    // Test that the data loaded successfully
    $result = $pdo->query("show table status")->fetchAll(PDO::FETCH_ASSOC);
    $logger->info("data_loader summary:");
    printAsciiTable($result);
    $logger->info("Successfully loaded Blue Economics jobs data");
}