Repository: AlecStrong/sql-psi (kotlin)

Issue:
Cannot access ROWID of aliased table in ORDER BY
Dialect: sqlite

failing sql:

```sql
CREATE TABLE Test1(id TEXT);
CREATE TABLE Test2(id TEXT);

fetchFirst:
SELECT t1.*, t2.* FROM Test1 t1
JOIN Test2 t2 ON t1.id = t2.id
WHERE t1.id = 'something good'
ORDER BY t1.rowid ASC
LIMIT 1;
```
In our .sq, we get this error:
```
line 17:12 - No column found with name rowid
14    SELECT t1.*, t2.* FROM Test1 t1
15    JOIN Test2 t2 ON t1.id = t2.uid
16    WHERE id = 'something good'
17    ORDER BY t1.rowid ASC
                  ^^^^^
18    LIMIT 1
```
Workaround is to add this to the join `JOIN (SELECT rowid AS rowid, id FROM t1) rowids ON t1.id = rowids.id`, then you can access `rowids.rowid`.

Pull request:
Closes https://github.com/AlecStrong/sql-psi/issues/142

Patch:
diff --git a/core/src/main/kotlin/com/alecstrong/sql/psi/core/psi/QueryElement.kt b/core/src/main/kotlin/com/alecstrong/sql/psi/core/psi/QueryElement.kt
index d518f6f9..e967baab 100644
--- a/core/src/main/kotlin/com/alecstrong/sql/psi/core/psi/QueryElement.kt
+++ b/core/src/main/kotlin/com/alecstrong/sql/psi/core/psi/QueryElement.kt
@@ -35,7 +35,7 @@ interface QueryElement : PsiElement {
     constructor(column: PsiElement) : this(columns = listOf(QueryColumn(column)))
 
     override fun toString(): String {
-      return "${table?.name} : [${columns.joinToString { it.element.text }}]"
+      return "${table?.name} : [${columns.joinToString { it.element.text }} [${synthesizedColumns.joinToString { it.acceptableValues.toString() }}]]"
     }
   }
 
diff --git a/core/src/main/kotlin/com/alecstrong/sql/psi/core/psi/mixins/TableOrSubqueryMixin.kt b/core/src/main/kotlin/com/alecstrong/sql/psi/core/psi/mixins/TableOrSubqueryMixin.kt
index 851f4945..4593affa 100644
--- a/core/src/main/kotlin/com/alecstrong/sql/psi/core/psi/mixins/TableOrSubqueryMixin.kt
+++ b/core/src/main/kotlin/com/alecstrong/sql/psi/core/psi/mixins/TableOrSubqueryMixin.kt
@@ -17,7 +17,7 @@ internal abstract class TableOrSubqueryMixin(
         return@lazy emptyList<QueryResult>()
       }
       tableAlias?.let { alias ->
-        return@lazy listOf(QueryResult(alias, result.flatMap { it.columns }))
+        return@lazy listOf(QueryResult(alias, result.flatMap { it.columns }, result.flatMap { it.synthesizedColumns }))
       }
       return@lazy result
     }


Test patch:
diff --git a/test-fixtures/src/main/resources/fixtures/rowid-orderby/Test.s b/test-fixtures/src/main/resources/fixtures/rowid-orderby/Test.s
new file mode 100644
index 00000000..90b412a2
--- /dev/null
+++ b/test-fixtures/src/main/resources/fixtures/rowid-orderby/Test.s
@@ -0,0 +1,14 @@
+CREATE TABLE Test1(id TEXT);
+CREATE TABLE Test2(id TEXT);
+
+SELECT t1.*
+FROM Test1 t1
+WHERE t1.rowid IS NOT NULL
+ORDER BY t1.rowid ASC
+LIMIT 1;
+
+SELECT t1.*, t2.* FROM Test1 t1
+JOIN Test2 t2 ON t1.id = t2.id
+WHERE t1.id = 'something good'
+ORDER BY t1.rowid ASC
+LIMIT 1;
\ No newline at end of file