| 217 | * ``` |
| 218 | */ |
| 219 | export function raw<R = RawQueryFragment & symbol, T extends object = any>( |
| 220 | sql: EntityKey<T> | EntityKey<T>[] | AnyString | ((alias: string) => string) | RawQueryFragment, |
| 221 | params?: readonly unknown[] | Dictionary<unknown>, |
| 222 | ): R { |
| 223 | if (isRaw(sql)) { |
| 224 | return sql as R; |
| 225 | } |
| 226 | |
| 227 | if (sql instanceof Function) { |
| 228 | sql = sql(ALIAS_REPLACEMENT); |
| 229 | } |
| 230 | |
| 231 | if (sql === '??' && Array.isArray(params)) { |
| 232 | return new RawQueryFragment(sql, params) as R; |
| 233 | } |
| 234 | |
| 235 | if (Array.isArray(sql)) { |
| 236 | // for composite FK we return just a simple string |
| 237 | return Utils.getPrimaryKeyHash(sql) as R; |
| 238 | } |
| 239 | |
| 240 | if (typeof params === 'object' && !Array.isArray(params)) { |
| 241 | const pairs = Object.entries(params); |
| 242 | const objectParams = []; |
| 243 | |
| 244 | for (const [key, value] of pairs) { |
| 245 | sql = sql.replace(`:${key}:`, '??'); |
| 246 | sql = sql.replace(`:${key}`, '?'); |
| 247 | objectParams.push(value); |
| 248 | } |
| 249 | |
| 250 | return new RawQueryFragment(sql, objectParams) as R; |
| 251 | } |
| 252 | |
| 253 | return new RawQueryFragment(sql, params) as R; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Alternative to the `raw()` helper allowing to use it as a tagged template function for the simple cases. |