summaryrefslogtreecommitdiff
path: root/public/config/init-setup-db.php
blob: 848d8786f18e88140496d53a36f63f145c53dc96 (plain)
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
<?php
// THIS SCRIPTS RUNS SOME LOW LEVEL OPERATIONS
// AND SHALL BE USED BY WEB-ADMINISTRATOR ONLY
////////////////////////////////////////////////////////////////////////////////
//
// This script may used to
// -- * init database     (TODO: do=init-db)
// -- * setup sub-tables to handle items identity per CCODE (do=init-idx)
// -- * re-index items into sub-tables    (TODO: do=re-idx)
// -- * re-calculate      (TODO: do=re-calc[&idx=##_#])
// -----------------------------------------------------------------------------
//
// Call:
// %domain%%path%/setup-db.php[?pin=id][&do=action][&idx=##_##]
// ex.
// %domain%%path%/setup-db.php : return a pin (TTL = 1min)
// %domain%%path%/setup-db.php

// 0. INITIALIZATION
// -----------------------------------------------------------------------------
// Load parametres (for db-connection)
// Load main Json for CCODES
// Init anything needed before starting the main script
////////////////////////////////////////////////////////////////////////////////

include("parametres.php");
$ccodeJSON = file_get_contents('ccode.json');
$ccodeARRAY = json_decode($ccodeJSON);

// an [on|off] line of code to prevent running the script accidentaly
// -----------------------------------------------------------------------------
echo "remove a line of code to run"; die();

// init database connection
// -----------------------------------------------------------------------------
$_dbc = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DBMS);
if ($_dbc->connect_errno) {
  echo "Database connection failed; Please try in a few minutes. ";
  if (TESTING) echo $_dbc->connect_error;
  $_dbc->close(); die();
}
$_dbc->set_charset("utf8");

// parse behaviour ($_GET) arguments
//
// BUILD indexing tables for all ccodes
foreach ($ccodeARRAY as $key => $val) {

    $_Table = "tt_". $val->idx;

    $_stm = $_dbc->prepare("DROP TABLE IF EXISTS {$_Table}");
    $_stm->execute();               // execute query
    $_stm->close();

    // prepare statemet
    $_stm = $_dbc->prepare("CREATE TABLE {$_Table} LIKE template");
    $_stm->execute();               // execute query
    $_stm->close();

    // Should you re-index tables? run the following

    // INSERT INTO {$_Table} (id) SELECT id FROM items WHERE ccode = ? ORDER BY id

}

echo "Tables are ready";