-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple table model, add short style initialiser to class co…
…nstructor "a = klass(1);" instead "a = new klass(1);"
- Loading branch information
Showing
8 changed files
with
106 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
global.Model.Table = Model.base.extend({ | ||
init: function (schema, table_name) { | ||
this.schema = schema; | ||
this.table = table_name; | ||
}, | ||
|
||
rename: function (new_name, callback) { | ||
|
||
}, | ||
|
||
remove: function(callback) { | ||
this.q("DROP TABLE %s", this.table, callback); | ||
}, | ||
|
||
getStructure: function (callback) { | ||
var sql = "select * from information_schema.columns where table_schema = '%s' and table_name = '%s';" | ||
this.q(sql, this.schema, this.table, function(data) { | ||
this.getPrimaryKey(function(rows, error) { | ||
if (!error) { | ||
var keys = rows.map(function(r) { | ||
return r.attname; | ||
}); | ||
|
||
data.rows.forEach(function(row) { | ||
row.is_primary_key = keys.indexOf(row.column_name) != -1; | ||
}); | ||
} | ||
callback(data.rows); | ||
}); | ||
}.bind(this)); | ||
}, | ||
|
||
getPrimaryKey: function (callback) { | ||
var sql = "SELECT pg_attribute.attname \ | ||
FROM pg_index, pg_class, pg_attribute \ | ||
WHERE \ | ||
pg_class.oid = '%s'::regclass AND \ | ||
indrelid = pg_class.oid AND \ | ||
pg_attribute.attrelid = pg_class.oid AND \ | ||
pg_attribute.attnum = any(pg_index.indkey) \ | ||
AND indisprimary;"; | ||
this.q(sql, this.table, function(data, error) { | ||
callback((data || {}).rows, error); | ||
}); | ||
} | ||
}); | ||
|
||
Model.Table.create = function (tableName, schema, callback) { | ||
sql = "CREATE TABLE %s (id SERIAL PRIMARY KEY)"; | ||
if (schema != '' && schema != 'public') { | ||
sql += sprintf(" TABLESPACE %s", schema); | ||
} | ||
|
||
Model.base.connection().q(sql, tableName, callback); | ||
}; | ||
|
||
Model.Table.l = function (schema, table_name) { | ||
return new Model.Table(schema, table_name); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters