|
| 1 | +var config = {}; |
| 2 | +var sys = require('sys'); |
| 3 | +var args = process.argv; |
| 4 | +for(var i = 0; i < args.length; i++) { |
| 5 | + switch(args[i].toLowerCase()) { |
| 6 | + case '-u': |
| 7 | + case '--user': |
| 8 | + config.user = args[++i]; |
| 9 | + break; |
| 10 | + case '--password': |
| 11 | + config.password = args[++i]; |
| 12 | + throw new Error("Passwords not supported yet"); |
| 13 | + break; |
| 14 | + case '-d': |
| 15 | + case '--database': |
| 16 | + config.database = args[++i]; |
| 17 | + break; |
| 18 | + case '-p': |
| 19 | + case '--port': |
| 20 | + config.port = args[++i]; |
| 21 | + break; |
| 22 | + case '-h': |
| 23 | + case '--host': |
| 24 | + config.host = args[++i]; |
| 25 | + break; |
| 26 | + case '--down': |
| 27 | + config.down = true; |
| 28 | + break; |
| 29 | + default: |
| 30 | + break; |
| 31 | + } |
| 32 | +} |
| 33 | +var log = function(keys) { |
| 34 | + keys.forEach(function(key) { |
| 35 | + console.log(key + ": '" + config[key] + "'"); |
| 36 | + }); |
| 37 | +} |
| 38 | +log(['user','password','database','port','host']) |
| 39 | + |
| 40 | +var pg = require(__dirname + '/../lib'); |
| 41 | +var con = new pg.Connection(); |
| 42 | +var people |
| 43 | +con.connect(config.port, config.host); |
| 44 | +con.on('connect', function() { |
| 45 | + console.log('connected'); |
| 46 | + con.startupMessage({ |
| 47 | + user: config.user, |
| 48 | + database: config.database |
| 49 | + }); |
| 50 | + con.once('readyForQuery', function() { |
| 51 | + config.down===true ? dropTable(con) : createTable(con); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +var people = [ |
| 56 | + {name: 'Aaron', age: 10}, |
| 57 | + {name: 'Brian', age: 20}, |
| 58 | + {name: 'Chris', age: 30}, |
| 59 | + {name: 'David', age: 40}, |
| 60 | + {name: 'Elvis', age: 50}, |
| 61 | + {name: 'Frank', age: 60}, |
| 62 | + {name: 'Grace', age: 70}, |
| 63 | + {name: 'Haley', age: 80}, |
| 64 | + {name: 'Irma', age: 90}, |
| 65 | + {name: 'Jenny', age: 100}, |
| 66 | + {name: 'Kevin', age: 110}, |
| 67 | + {name: 'Larry', age: 120}, |
| 68 | + {name: 'Michelle', age: 130}, |
| 69 | + {name: 'Nancy', age: 140}, |
| 70 | + {name: 'Olivia', age: 150}, |
| 71 | + {name: 'Peter', age: 160}, |
| 72 | + {name: 'Quinn', age: 170}, |
| 73 | + {name: 'Ronda', age: 180}, |
| 74 | + {name: 'Shelley', age: 190}, |
| 75 | + {name: 'Tobias', age: 200}, |
| 76 | + {name: 'Uma', age: 210}, |
| 77 | + {name: 'Veena', age: 220}, |
| 78 | + {name: 'Wanda', age: 230}, |
| 79 | + {name: 'Xavier', age: 240}, |
| 80 | + {name: 'Yoyo', age: 250}, |
| 81 | + {name: 'Zanzabar', age: 260} |
| 82 | +] |
| 83 | + |
| 84 | +var makeInsert = function(person) { |
| 85 | + return "insert into person(name, age) values('"+person.name + "', '" + person.age + "')"; |
| 86 | +}; |
| 87 | +var personIndex = 0; |
| 88 | +var createTable = function(con) { |
| 89 | + console.log("creating table 'person'"); |
| 90 | + con.query('create table person (id serial, name varchar(30), age integer)'); |
| 91 | + con.once('readyForQuery', function() { |
| 92 | + console.log('created person table'); |
| 93 | + insertPerson(con); |
| 94 | + }); |
| 95 | +}; |
| 96 | + |
| 97 | +var insertPerson = function(con) { |
| 98 | + if(personIndex < people.length) { |
| 99 | + var query = makeInsert(people[personIndex++]); |
| 100 | + con.query(query); |
| 101 | + con.once('readyForQuery', function() { |
| 102 | + insertPerson(con); |
| 103 | + }); |
| 104 | + } |
| 105 | + else { |
| 106 | + con.query("select * from person"); |
| 107 | + con.on('dataRow', function(row) { |
| 108 | + console.log(row.fields); |
| 109 | + }); |
| 110 | + con.once('readyForQuery', function() { |
| 111 | + con.end(); |
| 112 | + }); |
| 113 | + } |
| 114 | +}; |
| 115 | + |
| 116 | +var dropTable = function(con){ |
| 117 | + console.log("dropping table 'person'"); |
| 118 | + con.query('drop table person'); |
| 119 | + con.once('readyForQuery', function() { |
| 120 | + console.log("dropped table 'person'"); |
| 121 | + con.end(); |
| 122 | + }); |
| 123 | +} |
0 commit comments