@@ -249,6 +249,21 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
249249
250250 sb .injection .WriteTo (buf , selectMarkerAfterSelect )
251251
252+ if flavor == Oracle && sb .limit >= 0 && sb .offset > 0 {
253+ if len (sb .selectCols ) > 0 {
254+ buf .WriteLeadingString ("FROM ( SELECT " )
255+
256+ if sb .distinct {
257+ buf .WriteString ("DISTINCT " )
258+ }
259+
260+ var selectCols = make ([]string , 0 , len (sb .selectCols )+ 1 )
261+ selectCols = append (selectCols , sb .selectCols ... )
262+ selectCols = append (selectCols , "ROWNUM r " )
263+ buf .WriteString (strings .Join (selectCols , ", " ))
264+ }
265+ }
266+
252267 if len (sb .tables ) > 0 {
253268 buf .WriteLeadingString ("FROM " )
254269 buf .WriteString (strings .Join (sb .tables , ", " ))
@@ -274,6 +289,14 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
274289 sb .injection .WriteTo (buf , selectMarkerAfterJoin )
275290 }
276291
292+ if flavor == Oracle && sb .limit >= 0 {
293+ upper := sb .limit
294+ if sb .offset >= 0 {
295+ upper += sb .offset
296+ }
297+ sb .whereExprs = append (sb .whereExprs , sb .LE ("ROWNUM" , upper ))
298+ }
299+
277300 if len (sb .whereExprs ) > 0 {
278301 buf .WriteLeadingString ("WHERE " )
279302 buf .WriteString (strings .Join (sb .whereExprs , " AND " ))
@@ -354,6 +377,16 @@ func (sb *SelectBuilder) BuildWithFlavor(flavor Flavor, initialArg ...interface{
354377 buf .WriteString (strconv .Itoa (sb .limit ))
355378 buf .WriteString (" ROWS ONLY" )
356379 }
380+
381+ case Oracle :
382+ if flavor == Oracle && sb .limit >= 0 && sb .offset > 0 {
383+ buf .WriteString (" ) " )
384+ if len (sb .tables ) > 0 {
385+ buf .WriteString (strings .Join (sb .tables , ", " ))
386+ }
387+ buf .WriteString (" WHERE " )
388+ buf .WriteString (sb .G ("r" , sb .offset ))
389+ }
357390 }
358391
359392 if sb .limit >= 0 {
0 commit comments