@@ -522,6 +522,91 @@ abstract class SchoolDbTestRun extends SchoolDbTestBase {
522522 passed(' blobTest )
523523 }
524524
525+ /**
526+ * POC for raw SQL "facilities"
527+ */
528+ class RawQuery (query : String , args : Seq [Any ]) {
529+
530+ private def prep = {
531+ // We'll pretend we don't care about connection, statement, resultSet leaks for now ...
532+ val s = Session .currentSession
533+
534+ val st = s.connection.prepareStatement(query)
535+ for (z <- args.zipWithIndex)
536+ st.setObject(z._2 + 1 , z._1.asInstanceOf [AnyRef ])
537+ st
538+ }
539+
540+ import org .squeryl .internals ._
541+ import org .squeryl .dsl .ast ._
542+
543+ def toSeq [A ](t : Table [A ]) = {
544+ val st = prep
545+ val resultSet = st.executeQuery
546+ val res = new scala.collection.mutable.ArrayBuffer [A ]
547+
548+ // now for mapping a query to Schema objects :
549+ val rm = new ResultSetMapper
550+
551+ for ((fmd, i) <- t.posoMetaData.fieldsMetaData.zipWithIndex) {
552+ val jdbcIndex = i + 1
553+ val fse = new FieldSelectElement (null , fmd, rm)
554+ fse.prepareColumnMapper(jdbcIndex)
555+ fse.prepareMapper(jdbcIndex)
556+ }
557+
558+ while (resultSet.next) {
559+ val v = t.give(rm, resultSet)
560+ res.append(v)
561+ }
562+ res.toSeq
563+ }
564+
565+ def toTuple [A1 ,A2 ]()(implicit f1 : TypedExpressionFactory [A1 ,_], f2 : TypedExpressionFactory [A2 ,_]) = {
566+
567+ val st = prep
568+ val rs = st.executeQuery
569+
570+ if (! rs.next)
571+ sys.error(" consider using toOptionTuple[....]" )
572+
573+ // let's pretend there was no shame to be had for such grotesque cheating :
574+ val m1 = f1.thisMapper.asInstanceOf [PrimitiveJdbcMapper [A1 ]]
575+ val m2 = f2.thisMapper.asInstanceOf [PrimitiveJdbcMapper [A2 ]]
576+ // in fact, there should be a wrapper type of TypedExpressionFactory only for primitive types
577+ // for use in such toTuple mapping ...
578+
579+ (m1.convertFromJdbc(m1.extractNativeJdbcValue(rs, 1 )),
580+ m2.convertFromJdbc(m2.extractNativeJdbcValue(rs, 2 )))
581+ }
582+ }
583+
584+ def query (q : String , a : Any * ) = new RawQuery (q, a)
585+
586+ test(" raw sql" , SingleTestRun ) {
587+
588+ val r =
589+ query(" select s.* from t_student s where s.name = ? and s.age = ?" ,
590+ " Xiao" , 24 ).
591+ toSeq(students)
592+
593+ r.map(_.name) match {
594+ case Seq (" Xiao" ) => passed(' rawQueryPOC )
595+ case a: Any => sys.error(" Failed: " + a)
596+ }
597+ }
598+
599+ test(" raw sql to Tuple" , SingleTestRun ) {
600+
601+ val (name, age) =
602+ query(" select s.name, s.age from t_student s where s.name = 'Xiao' and s.age = 24" ).
603+ toTuple[String ,Int ]
604+
605+ assert(name == " Xiao" )
606+
607+ assert(age == 24 )
608+ }
609+
525610 test(" InOpWithStringList" ){
526611 val testInstance = sharedTestInstance; import testInstance ._
527612 val r =
0 commit comments