2014-09-16 07:27:27 +00:00
#!/usr/bin/env node
2015-01-05 06:10:10 +00:00
/ *
* Copyright ( c ) 2015 Alex Yatskov < alex @ foosoft . net >
*
* Permission is hereby granted , free of charge , to any person obtaining a copy of
* this software and associated documentation files ( the "Software" ) , to deal in
* the Software without restriction , including without limitation the rights to
* use , copy , modify , merge , publish , distribute , sublicense , and / or sell copies of
* the Software , and to permit persons to whom the Software is furnished to do so ,
* subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED "AS IS" , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY , FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER LIABILITY , WHETHER
* IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM , OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE .
* /
2014-09-16 07:27:27 +00:00
var mysql = require ( 'mysql' ) ;
var data = require ( './data.json' ) ;
2015-03-11 06:25:17 +00:00
//
// Setup
//
var conn = mysql . createConnection ( { host : 'localhost' , user : 'hscd' } ) ;
conn . query ( 'DROP DATABASE IF EXISTS hscd' ) ;
conn . query ( 'CREATE DATABASE hscd' ) ;
conn . query ( 'USE hscd' ) ;
2014-09-16 07:27:27 +00:00
//
// Reviews
//
conn . query ( 'DROP TABLE IF EXISTS reviews' ) ;
2015-01-05 07:10:23 +00:00
conn . query ( 'CREATE TABLE reviews(name VARCHAR(100) NOT NULL, url VARCHAR(200) NOT NULL, delicious FLOAT NOT NULL, accomodating FLOAT NOT NULL, affordable FLOAT NOT NULL, atmospheric FLOAT NOT NULL, latitude FLOAT NOT NULL, longitude FLOAT NOT NULL, distanceToStn FLOAT NOT NULL, closestStn VARCHAR(100) NOT NULL, id INT NOT NULL AUTO_INCREMENT PRIMARY KEY) DEFAULT CHARACTER SET utf8' ) ;
2014-09-16 07:27:27 +00:00
for ( var i = 0 , count = data . length ; i < count ; ++ i ) {
var record = data [ i ] ;
2015-01-05 06:49:54 +00:00
conn . query ( 'INSERT INTO reviews(name, url, delicious, accomodating, affordable, atmospheric, latitude, longitude, distanceToStn, closestStn) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' , [
2014-09-16 07:27:27 +00:00
record . name ,
record . relativeUrl ,
record . rating . food ,
record . rating . service ,
record . rating . value ,
2014-11-17 06:16:05 +00:00
record . rating . atmosphere ,
record . geo . latitude ,
2015-01-05 05:32:58 +00:00
record . geo . longitude ,
2015-01-05 06:49:54 +00:00
record . distanceToStn ,
record . closestStn
2014-09-16 07:27:27 +00:00
] ) ;
}
2015-03-02 07:23:52 +00:00
//
// Categories
//
conn . query ( 'DROP TABLE IF EXISTS categories' ) ;
2015-03-11 05:41:17 +00:00
conn . query ( 'CREATE TABLE categories(description VARCHAR(200) NOT NULL, id INT NOT NULL AUTO_INCREMENT PRIMARY KEY)' ) ;
2015-03-02 07:23:52 +00:00
var categories = [
'I prefer quiet places' ,
'I enjoy Mexican Food' ,
'I drive a car'
] ;
for ( var i = 0 , count = categories . length ; i < count ; ++ i ) {
2015-03-11 05:41:17 +00:00
conn . query ( 'INSERT INTO categories(description) VALUES (?)' , [ categories [ i ] ] ) ;
2015-03-02 07:23:52 +00:00
}
2015-03-11 05:25:30 +00:00
//
// History
//
conn . query ( 'DROP TABLE IF EXISTS history' ) ;
2015-03-11 06:25:17 +00:00
conn . query ( 'CREATE TABLE history(date DATETIME NOT NULL, reviewId INT NOT NULL, subspaceId INT NOT NULL)' ) ;
2015-03-11 05:25:30 +00:00
//
2015-03-11 06:25:17 +00:00
// Subspaces
2015-03-11 05:25:30 +00:00
//
2015-03-11 06:25:17 +00:00
conn . query ( 'DROP TABLE IF EXISTS subspaces' ) ;
conn . query ( 'CREATE TABLE subspaces(categoryId INT NOT NULL, subspaceId INT NOT NULL AUTO_INCREMENT PRIMARY KEY)' ) ;
2015-03-11 05:25:30 +00:00
2014-09-16 07:27:27 +00:00
//
2015-01-13 06:59:09 +00:00
// Cleanup
2014-09-16 07:27:27 +00:00
//
conn . end ( ) ;