Skip to content
This repository was archived by the owner on May 24, 2018. It is now read-only.

Commit 27db4a1

Browse files
author
Dror
committed
* support selects
1 parent 5a27f24 commit 27db4a1

File tree

12 files changed

+84
-18
lines changed

12 files changed

+84
-18
lines changed

client/example/main.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<h1> Meteor SQL </h1>
1717
<ul class="nav nav-pills active" id="formBuilderTabs">
1818
<li class='active' ><a data-toggle='pill' href="#employeeTable">Employee Table</a></li>
19+
<li ><a data-toggle='pill' href="#select">Select with Join</a></li>
1920
<li ><a data-toggle='pill' href="#schema">Database Schema</a></li>
2021
</ul>
2122

@@ -25,6 +26,9 @@ <h1> Meteor SQL </h1>
2526
{{> devwikEmployees}}
2627
</div>
2728
</div>
29+
<div class="pill-pane" id="select">
30+
{{> devwikSelects}}
31+
</div>
2832
<div class="pill-pane" id="schema">
2933
{{> devwikTables}}
3034
</div>

client/example/schema.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<template name="devwikTables">
2+
<div>
3+
The database schema is automatically generated. <strong>Each table has to have a unique key.</strong>
4+
</div>
25
{{#each tables}}
36
{{> devwikTable}}
47
{{/each}}

client/example/select.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
<template name="devwikSelect">
1+
<template name="devwikSelects">
2+
<div class='row'>
3+
4+
On the server:
5+
<pre> new Devwik.SQL.Select('empsCities', 'select employees.*, offices.city from employees, offices where offices.officeCode = employees.officecode');
6+
</pre>
7+
On the client:
8+
<pre> var Select = new Meteor.Select('empsCities'); </pre>
9+
And then just use standard Meteor operations:
10+
<pre> Template.devwikSelects.selects = function () {
11+
return Select.find({});
12+
}; </pre>
13+
<p>
14+
</div>
215
<div class='row'>
316
<h4>
417
<div class='span1'>

client/example/select.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var notDone = true;
2-
var Select = new Meteor.Table('empsCities');
2+
var Select = new Meteor.Select('empsCities');
33

44

5-
Template.devwikSelect.selects = function () {
5+
Template.devwikSelects.selects = function () {
66
return Select.find({});
77
};
88

client/lib/sql/select.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use strict";
2-
Meteor.Joing = function(name) {
2+
Meteor.Select = function(name) {
33
Meteor.subscribe(name);
44
var myCollection = new Meteor.Collection(name);
55

common/lib/devwik.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
//based on http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
2-
Devwik.toType = function(obj) {
3-
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
4-
};
1+
if (typeof Devwik == 'undefined') {
2+
Devwik = function() {};
3+
}
4+
5+
//based on http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
6+
Devwik.toType = function(obj) {
7+
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
8+
};

server/client.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Handle calls from the client
33
*/
44
Meteor.methods({
5-
//TODO: Remember Bobby, need to sanitize all the data
65
SQLinsert: function (table, args) {
76
try {
87
var statement = squel.insert().into(table);

server/dbinit.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pool.getConnection(function(err, connection) {
2727
Devwik.SQL.dbChanges();
2828
// Poll the table with changes
2929
Devwik.SQL.Poll();
30+
Devwik.SQL.runTests();
3031
Devwik.SQL.tables = {};
3132
_.each(result, function(row){ //For each table in the db
3233
if(!(row.Tables_in_meteor === dbChanges)) {
@@ -75,9 +76,11 @@ Devwik.SQL.execStatement(statement.toString());
7576
* @param {Boolean} ignoreErr -- optional. By default, we throw an exception on error
7677
* This makes us ignore the error.
7778
* @returns {Array} result. The rows, if any returned by the query
78-
* TODO: We actually should no throw an error as a rule.
79+
* TODO: We actually should not throw an error as a rule.
80+
* TODO: escapge to avoid SQL injection
7981
*/
8082
Devwik.SQL.execStatement = function(statement, ignoreErr) {
83+
//statement = Devwik.SQL.connection.escape(statement);//TODO: is this good enough to avoid injections?
8184
var future = new Future();
8285
query = Devwik.SQL.connection.query(statement, function(err, result) {
8386
//console.log(statement); //TODO: provide a way to show

server/lib/lib.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
var Devwik = function() {}; //Provide a name space
33
Devwik.SQL = function() {};
44

5+
//based on http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
6+
Devwik.toType = function(obj) {
7+
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
8+
};

server/select.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44
*/
55

66
//An SQL select statement
7-
Devwik.SQL.Select = function(statement) {
7+
Devwik.SQL.Select = function(name, statement) {
88
var self = this;
9+
self.name = name;
910
self.statement = statement;
1011
self.cols = [];
1112
var future = new Future();
12-
//Get the structure for a table
13+
//Get the structure for a select
1314
Devwik.SQL.connection.query(statement, function(err, rows) {
1415
if (err) throw err;
1516
_.each(rows, function(row){
1617
if(self.cols.length === 0) {
1718
self.setCols(row);
18-
console.log(row);
1919
}
2020
});
2121
future.ret();
2222
}, self);
2323
future.wait();
2424

25-
//this.setPublish();
25+
this.setPublish();
2626
return;
2727
};
2828

@@ -31,8 +31,44 @@ Devwik.SQL.Select.prototype.setCols = function(row) {
3131
var self = this;
3232
_.each(row, function(value, name){
3333
var col = {};
34-
col[name] = value;
34+
col.name = name;
35+
col.type = Devwik.toType(value);
3536
self.cols.push(col);
3637
console.log(col);
3738
});
3839
};
40+
41+
/*
42+
* Publish the Select to the client
43+
*/
44+
45+
Devwik.SQL.Select.prototype.setPublish = function() {
46+
console.log('publish JOIN');
47+
var select = this;
48+
Meteor.publish(select.name, function () {
49+
console.log('publish JOIN 1');
50+
var self = this;
51+
/*
52+
* Set up the callbacks
53+
*/
54+
select.added = function(name, id, data) {
55+
self.added(name, id, data);
56+
};
57+
select.changed = function(name, id, data) {
58+
self.changed(name, id, data);
59+
};
60+
select.removed = function(name, id) {
61+
self.removed(name, id);
62+
};
63+
//fut.ret();
64+
query = Devwik.SQL.connection.query(select.statement, function(err, result) {
65+
if (err) {
66+
throw err;
67+
}
68+
_.each(result, function(row){
69+
self.added(select.name, new Meteor.Collection.ObjectID(), row);
70+
});
71+
self.ready();//indicate that the initial rows are ready
72+
});
73+
});
74+
};

0 commit comments

Comments
 (0)