diff options
author | Egil Moeller <egil.moller@freecode.no> | 2010-03-12 22:03:37 +0100 |
---|---|---|
committer | Egil Moeller <egil.moller@freecode.no> | 2010-03-12 22:03:37 +0100 |
commit | 616a926a29ff850ff83555e9c0b9b4a87140f464 (patch) | |
tree | d321014f0cd1cbd159f915e89171c49e4b957553 /trunk/infrastructure/framework-src/modules/sqlbase/sqlobj.js | |
parent | 22035e909988e28f6777be39a9a4c67e910a65d0 (diff) | |
download | etherpad-616a926a29ff850ff83555e9c0b9b4a87140f464.tar.gz etherpad-616a926a29ff850ff83555e9c0b9b4a87140f464.tar.xz etherpad-616a926a29ff850ff83555e9c0b9b4a87140f464.zip |
Had to cherry-pick one single patch from the twitterstyletags branch to get this to work :S Adds the executeRaw db function, plus bugfixes some db stuff by adding support for more sql types; this allows sql joins and the like.
Diffstat (limited to '')
-rw-r--r-- | trunk/infrastructure/framework-src/modules/sqlbase/sqlobj.js | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/trunk/infrastructure/framework-src/modules/sqlbase/sqlobj.js b/trunk/infrastructure/framework-src/modules/sqlbase/sqlobj.js index 4bc1263..e599c92 100644 --- a/trunk/infrastructure/framework-src/modules/sqlbase/sqlobj.js +++ b/trunk/infrastructure/framework-src/modules/sqlbase/sqlobj.js @@ -17,6 +17,7 @@ import("cache_utils.syncedWithCache"); import("sqlbase.sqlcommon.*"); import("jsutils.*"); +import("etherpad.log"); jimport("java.lang.System.out.println"); jimport("java.sql.Statement"); @@ -112,10 +113,13 @@ function _getJsValFromResultSet(rs, type, colName) { } else { r = null; } - } else if (type == java.sql.Types.INTEGER || + } else if (type == java.sql.Types.BIGINT || + type == java.sql.Types.INTEGER || type == java.sql.Types.SMALLINT || type == java.sql.Types.TINYINT) { r = rs.getInt(colName); + } else if (type == java.sql.Types.DECIMAL) { + r = rs.getFloat(colName); } else if (type == java.sql.Types.BIT) { r = rs.getBoolean(colName); } else { @@ -192,8 +196,9 @@ function _resultRowToJsObj(resultSet) { var metaData = resultSet.getMetaData(); var colCount = metaData.getColumnCount(); + for (var i = 1; i <= colCount; i++) { - var colName = metaData.getColumnName(i); + var colName = metaData.getColumnLabel(i); var type = metaData.getColumnType(i); resultObj[colName] = _getJsValFromResultSet(resultSet, type, colName); } @@ -338,6 +343,47 @@ function selectMulti(tableName, constraints, options) { }); } +function executeRaw(stmnt, params) { + return withConnection(function(conn) { + var pstmnt = conn.prepareStatement(stmnt); + return closing(pstmnt, function() { + for (var i = 0; i < params.length; i++) { + var v = params[i]; + + if (v === undefined) { + throw Error("value is undefined for key "+i); + } + + if (typeof(v) == 'object' && v.isnull) { + pstmnt.setNull(i+1, v.type); + } else if (typeof(v) == 'string') { + pstmnt.setString(i+1, v); + } else if (typeof(v) == 'number') { + pstmnt.setInt(i+1, v); + } else if (typeof(v) == 'boolean') { + pstmnt.setBoolean(i+1, v); + } else if (v.valueOf && v.getDate && v.getHours) { + pstmnt.setTimestamp(i+1, new java.sql.Timestamp(+v)); + } else { + throw Error("Cannot insert this type of javascript object: "+typeof(v)+" (key="+i+", value = "+v+")"); + } + } + + _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); |