From d7c5ad7d6263fd1baf9bfdbaa4c50b70ef2fbdb2 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Tue, 8 Jun 2010 08:22:05 +0200 Subject: reverted folder structure change for better mergeing with upstream --- .../modules/sqlbase/persistent_vars.js | 57 +++ .../framework-src/modules/sqlbase/sqlbase.js | 205 +++++++++ .../framework-src/modules/sqlbase/sqlcommon.js | 99 ++++ .../framework-src/modules/sqlbase/sqlobj.js | 505 +++++++++++++++++++++ 4 files changed, 866 insertions(+) create mode 100644 trunk/infrastructure/framework-src/modules/sqlbase/persistent_vars.js create mode 100644 trunk/infrastructure/framework-src/modules/sqlbase/sqlbase.js create mode 100644 trunk/infrastructure/framework-src/modules/sqlbase/sqlcommon.js create mode 100644 trunk/infrastructure/framework-src/modules/sqlbase/sqlobj.js (limited to 'trunk/infrastructure/framework-src/modules/sqlbase') diff --git a/trunk/infrastructure/framework-src/modules/sqlbase/persistent_vars.js b/trunk/infrastructure/framework-src/modules/sqlbase/persistent_vars.js new file mode 100644 index 0000000..1c4cc95 --- /dev/null +++ b/trunk/infrastructure/framework-src/modules/sqlbase/persistent_vars.js @@ -0,0 +1,57 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import("sqlbase.sqlobj"); +import("sqlbase.sqlcommon"); + +jimport("java.lang.System.out.println"); + +// TODO: add caching? + +// Curently supports: +// Strings + +function get(name) { + if (!sqlcommon.doesTableExist('persistent_vars')) { + return undefined; + } + var r = sqlobj.selectSingle('persistent_vars', {name: name}); + if (!r) { + return undefined; + } + return r.stringVal; +} + +function put(name, val) { + if (typeof(val) != 'string') { + throw Error("unsupported type for persistent_vars: "+typeof(val)); + } + + var r = sqlobj.selectSingle('persistent_vars', {name: name}); + if (r) { + sqlobj.updateSingle('persistent_vars', {id: r.id}, {stringVal: val}); + } else { + sqlobj.insert('persistent_vars', {name: name, stringVal: val}); + } +} + +function remove(name) { + var r = sqlobj.selectSingle('persistent_vars', {name: name}); + if (r) { + sqlobj.deleteRows('persistent_vars', {id: r.id}); + } +} diff --git a/trunk/infrastructure/framework-src/modules/sqlbase/sqlbase.js b/trunk/infrastructure/framework-src/modules/sqlbase/sqlbase.js new file mode 100644 index 0000000..3df1a0f --- /dev/null +++ b/trunk/infrastructure/framework-src/modules/sqlbase/sqlbase.js @@ -0,0 +1,205 @@ +/** + * Copyright 2009 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS-IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import("jsutils.*"); +import("sqlbase.sqlcommon"); +import("fastJSON"); +import("timer"); + +jimport("java.lang.System.out.println"); + +function _sqlbase() { + return sqlcommon.getSqlBase(); +} + +/** + * Creates a SQL table suitable for storing a mapping from String to JSON value. + * Maximum key length is 128 characters. Has no effect if the table already exists. + */ +function createJSONTable(tableName) { + _sqlbase().createJSONTable(String(tableName)); +} + +/** + * Retrieves a JavaScript object or value from a table. Returns undefined + * if there is no mapping for the given string key. Requires that the table + * exist. + */ +function getJSON(tableName, stringKey) { + var result = _sqlbase().getJSON(String(tableName), String(stringKey)); + if (result) { + + return fastJSON.parse(String(result))['x']; + + /* performance-testing JSON + var obj1 = timer.time("JSON.parse (json2)", function() { + return JSON.parse(String(result))['x']; + }); + var obj2 = timer.time("JSON.parse (fastJSON)", function() { + return fastJSON.parse(String(result))['x']; + }); + return obj2; + */ + } + return undefined; +} + +function getAllJSON(tableName, start, count) { + var result = _sqlbase().getAllJSON(String(tableName), Number(start), Number(count)); + return Array.prototype.map.call(result, function(x) { + return {id: x.id(), value: fastJSON.parse(String(x.value()))['x']}; + }) +} + +function getAllJSONKeys(tableName) { + var result = _sqlbase().getAllJSONKeys(String(tableName)); + return Array.prototype.map.call(result, function(x) { return String(x); }); +} + +/** + * Assigns a JavaScript object or primitive value to a string key in a table. + * Maximum key length is 128 characters. Requires that the table exist. + */ +function putJSON(tableName, stringKey, objectOrValue) { + var obj = ({x:objectOrValue}); + + var json = fastJSON.stringify(obj); + + /* performance-testing JSON + + var json1 = timer.time("JSON.stringify (json2)", function() { + return JSON.stringify(obj); + }); + var json2 = timer.time("JSON.stringify (fastJSON)", function() { + return fastJSON.stringify(obj); + }); + + if (json1 != json2) { + println("json strings do not match!"); + println("\n\n"); + println(json1); + println("\n"); + println(json2); + println("\n\n"); + }*/ + + _sqlbase().putJSON(String(tableName), String(stringKey), json); +} + +/** + * Removes the mapping for a string key from a table. Requires that the table + * exist. + */ +function deleteJSON(tableName, stringKey) { + _sqlbase().deleteJSON(String(tableName), String(stringKey)); +} + +/** + * Creates a SQL table suitable for storing a mapping from (key,n) to string. + * The mapping may be sparse, but storage is most efficient when n are consecutive. + * The "length" of the array is not stored and must be externally maintained. + * Maximum key length is 128 characters. This call has no effect if the table + * already exists. + */ +function createStringArrayTable(tableName) { + _sqlbase().createStringArrayTable(String(tableName)); +} + +/** + * Assigns a string value to a (key,n) pair in a StringArray table. Maximum key length + * is 128 characters. Requires that the table exist. + */ +function putStringArrayElement(tableName, stringKey, n, value) { + _sqlbase().putStringArrayElement(String(tableName), String(stringKey), + Number(n), String(value)); +} + +/** + * Equivalent to a series of consecutive puts of the elements of valueArray, with the first + * one going to n=startN, the second to n=startN+1, and so on, but much more efficient. + */ +function putConsecutiveStringArrayElements(tableName, stringKey, startN, valueArray) { + var putter = _sqlbase().putMultipleStringArrayElements(String(tableName), String(stringKey)); + for(var i=0;i 0) { + stmnt += "WHERE ("; + stmnt += constraintKeys.map(function(key) { + return _makeConstraintString(key, constraints[key]); + }).join(' AND '); + stmnt += ')'; + } + + if (options.orderBy) { + var orderEntries = []; + options.orderBy.split(",").forEach(function(orderBy) { + var asc = "ASC"; + if (orderBy.charAt(0) == '-') { + orderBy = orderBy.substr(1); + asc = "DESC"; + } + orderEntries.push(_bq(orderBy)+" "+asc); + }); + stmnt += " ORDER BY "+orderEntries.join(", "); + } + + if (options.limit) { + stmnt += " LIMIT "+options.limit; + } + + return withConnection(function(conn) { + var pstmnt = conn.prepareStatement(stmnt); + return closing(pstmnt, function() { + _setPreparedValues( + tableName, pstmnt, constraintKeys, + _preparedValuesConstraints(constraints), 0); + + _qdebug(stmnt); + var resultSet = pstmnt.executeQuery(); + var resultArray = []; + + return closing(resultSet, function() { + while (resultSet.next()) { + resultArray.push(_resultRowToJsObj(resultSet)); + } + + return resultArray; + }); + }); + }); +} + +/* returns number of rows updated */ +function update(tableName, constraints, obj) { + var objKeys = keys(obj); + var constraintKeys = keys(constraints); + + var stmnt = "UPDATE "+_bq(tableName)+" SET "; + stmnt += objKeys.map(function(k) { return ''+_bq(k)+' = ?'; }).join(', '); + stmnt += " WHERE ("; + stmnt += constraintKeys.map(function(k) { return '('+_bq(k)+' = ?)'; }).join(' AND '); + stmnt += ')'; + + return withConnection(function(conn) { + var pstmnt = conn.prepareStatement(stmnt); + return closing(pstmnt, function() { + _setPreparedValues(tableName, pstmnt, objKeys, obj, 0); + _setPreparedValues(tableName, pstmnt, constraintKeys, constraints, objKeys.length); + _qdebug(stmnt); + return pstmnt.executeUpdate(); + }); + }); +} + +function updateSingle(tableName, constraints, obj) { + var count = update(tableName, constraints, obj); + if (count != 1) { + throw Error("save count != 1. instead, count = "+count); + } +} + +function deleteRows(tableName, constraints) { + var constraintKeys = keys(constraints); + var stmnt = "DELETE FROM "+_bq(tableName)+" WHERE ("; + stmnt += constraintKeys.map(function(k) { return '('+_bq(k)+' = ?)'; }).join(' AND '); + stmnt += ')'; + withConnection(function(conn) { + var pstmnt = conn.prepareStatement(stmnt); + closing(pstmnt, function() { + _setPreparedValues(tableName, pstmnt, constraintKeys, constraints); + _qdebug(stmnt); + pstmnt.executeUpdate(); + }); + }) +} + +//---------------------------------------------------------------- +// table management +//---------------------------------------------------------------- + +/* + * Create a SQL table, specifying column names and types with a + * javascript object. + */ +function createTable(tableName, colspec, indices) { + if (doesTableExist(tableName)) { + return; + } + + var stmnt = "CREATE TABLE "+_bq(tableName)+ " ("; + stmnt += keys(colspec).map(function(k) { return (_bq(k) + ' ' + colspec[k]); }).join(', '); + if (indices) { + stmnt += ', ' + keys(indices).map(function(k) { return 'INDEX (' + _bq(k) + ')'; }).join(', '); + } + stmnt += ')'+createTableOptions(); + _execute(stmnt); +} + +function dropTable(tableName) { + _execute("DROP TABLE "+_bq(tableName)); +} + +function dropAndCreateTable(tableName, colspec, indices) { + if (doesTableExist(tableName)) { + dropTable(tableName); + } + + return createTable(tableName, colspec, indices); +} + +function renameTable(oldName, newName) { + _executeUpdate("RENAME TABLE "+_bq(oldName)+" TO "+_bq(newName)); +} + +function modifyColumn(tableName, columnName, newSpec) { + _executeUpdate("ALTER TABLE "+_bq(tableName)+" MODIFY "+_bq(columnName)+" "+newSpec); +} + +function alterColumn(tableName, columnName, alteration) { + var q = "ALTER TABLE "+_bq(tableName)+" ALTER COLUMN "+_bq(columnName)+" "+alteration; + _executeUpdate(q); +} + +function changeColumn(tableName, columnName, newSpec) { + var q = ("ALTER TABLE "+_bq(tableName)+" CHANGE COLUMN "+_bq(columnName) + +" "+newSpec); + _executeUpdate(q); +} + +function addColumns(tableName, colspec) { + inTransaction(function(conn) { + eachProperty(colspec, function(name, definition) { + var stmnt = "ALTER TABLE "+_bq(tableName)+" ADD COLUMN "+_bq(name)+" "+definition; + _executeUpdate(stmnt); + }); + }); +} + +function dropColumn(tableName, columnName) { + var stmnt = "ALTER TABLE "+_bq(tableName)+" DROP COLUMN "+_bq(columnName); + _executeUpdate(stmnt); +} + +function listTables() { + return withConnection(function(conn) { + var metadata = conn.getMetaData(); + var resultSet = metadata.getTables(null, null, null, null); + var resultArray = []; + + return closing(resultSet, function() { + while (resultSet.next()) { + resultArray.push(resultSet.getString("TABLE_NAME")); + } + return resultArray; + }); + }); +} + +function setTableEngine(tableName, engineName) { + var stmnt = "ALTER TABLE "+_bq(tableName)+" ENGINE="+_bq(engineName); + _executeUpdate(stmnt); +} + +function getTableEngine(tableName) { + if (!isMysql()) { + throw Error("getTableEngine() only supported by MySQL database type."); + } + + var tableEngines = {}; + + withConnection(function(conn) { + var stmnt = "show table status"; + var pstmnt = conn.prepareStatement(stmnt); + closing(pstmnt, function() { + _qdebug(stmnt); + var resultSet = pstmnt.executeQuery(); + closing(resultSet, function() { + while (resultSet.next()) { + var n = resultSet.getString("Name"); + var eng = resultSet.getString("Engine"); + tableEngines[n] = eng; + } + }); + }); + }); + + return tableEngines[tableName]; +} + +function createIndex(tableName, columns) { + var indexName = "idx_"+(columns.join("_")); + var stmnt = "CREATE INDEX "+_bq(indexName)+" on "+_bq(tableName)+" ("; + stmnt += columns.map(_bq).join(", "); + stmnt += ")"; + _executeUpdate(stmnt); +} + -- cgit v1.2.3