8000 SQLite ModelBuilder Regex bug · Issue #1939 · slick/slick · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

SQLite ModelBuilder Regex bug #1939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
GlulkAlex opened this issue Aug 1, 2018 · 3 comments
Closed

SQLite ModelBuilder Regex bug #1939

GlulkAlex opened this issue Aug 1, 2018 · 3 comments
Milestone

Comments

@GlulkAlex
Copy link
Contributor
GlulkAlex commented Aug 1, 2018

I'm trying to extract a db schema to build tables models
e.g. for this table:

CREATE TABLE [Invoice]
(
    -- ...
    [Total] NUMERIC(10,2)  NOT NULL,
    -- ...
);

and I got an exception:

scala.MatchError: NUMERIC(10,2) (of class java.lang.String)
	at slick.jdbc.SQLiteProfile$ModelBuilder$$anon$1.<init>(SQLiteProfile.scala:99)
	at slick.jdbc.SQLiteProfile$ModelBuilder.createColumnBuilder(SQLiteProfile.scala:94)
	at slick.jdbc.JdbcModelBuilder$TableBuilder.$anonfun$columns$1(JdbcModelBuilder.scala:162)
	at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:234)
	at scala.collection.Iterator.foreach(Iterator.scala:944)
	at scala.collection.Iterator.foreach$(Iterator.scala:944)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1432)
	at scala.collection.IterableLike.foreach(IterableLike.scala:71)
	at scala.collection.IterableLike.foreach$(IterableLike.scala:70)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
	at scala.collection.TraversableLike.map(TraversableLike.scala:234)
	at scala.collection.TraversableLike.map$(TraversableLike.scala:227)
	at scala.collection.AbstractTraversable.map(Traversable.scala:104)
	at slick.jdbc.JdbcModelBuilder$TableBuilder.columns$lzycompute(JdbcModelBuilder.scala:162)
	at slick.jdbc.JdbcModelBuilder$TableBuilder.columns(JdbcModelBuilder.scala:162)
	at slick.jdbc.JdbcModelBuilder$TableBuilder.buildModel(JdbcModelBuilder.scala:160)
	at slick.jdbc.JdbcModelBuilder.$anonfun$buildModel$6(JdbcModelBuilder.scala:95)
	at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:234)
	at scala.collection.immutable.List.foreach(List.scala:389)
	at scala.collection.TraversableLike.map(TraversableLike.scala:234)
	at scala.collection.TraversableLike.map$(TraversableLike.scala:227)
	at scala.collection.immutable.List.map(List.scala:295)
	at slick.jdbc.JdbcModelBuilder.$anonfun$buildModel$4(JdbcModelBuilder.scala:95)
	at slick.dbio.DBIOAction.$anonfun$map$1(DBIOAction.scala:43)
	at slick.basic.BasicBackend$DatabaseDef.$anonfun$runInContextInline$1(BasicBackend.scala:171)
	at scala.concurrent.Future.$anonfun$flatMap$1(Future.scala:303)
	at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
	at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
	at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

I think that cased by this expression:

final val TypePattern = "^([A-Z\\s]+)(\\(([0-9]+)\\))?$".r

e.g. following test snippet recreates the original error:

scala> val TypePattern = "^([A-Z\\s]+)(\\(([0-9]+)\\))?$".r
TypePattern: scala.util.matching.Regex = ^([A-Z\s]+)(\(([0-9]+)\))?$

scala> "NUMERIC(10,2)" match {
     |           case TypePattern(d,_,s) => (d, Option(s).map(_.toInt))
     |           case "" => ("TEXT", None)
     |         }
scala.MatchError: NUMERIC(10,2) (of class java.lang.String)
  ... 39 elided

as a workaround I wrote my own extension for SQLiteProfile
with override for createModelBuilder
where replaced RegEx pattern with this function:

/** Regex matcher 
to extract name and length 
out of a db type name 
with length ascription */
def extract( 
  s: String = "NUMERIC(10,2)"
, pattern: scala.util.matching.Regex = "^([A-Z\\s]+)(\\(([0-9]+),?([0-9]+)?\\))?$".r
): ( String, Option[ Int ] ) = s match {
  case pattern( 
    name_Group, numeric_Group, integer_Group, fractional_Group 
  ) => ( name_Group, Option( integer_Group ).map( _.toInt ) )
  case "" => ( "TEXT", None )
}
private 
      val ( _dbType, _size ) = extract( 
        s = meta.typeName
      , pattern = "^([A-Z\\s]+)(\\(\\s?([0-9]+)\\s?,?\\s?([0-9]+)?\\s?\\))?$".r 
      )
@trevorsibanda
Copy link
Contributor

Hi @GlulkAlex

Do you mind submitting this as a pull request and adding the test case to the sqlite schema used by the codegen tests.

@GlulkAlex
Copy link
Contributor Author
GlulkAlex commented Aug 9, 2018

Hi @trevorsibanda,
I don't know much
about the sqlite schema and the codegen tests yet,
but I'll see what I can do .
As I understand ( correct me if I'm wrong )
it must be something like this #1828 .

@GlulkAlex
Copy link
Contributor Author
GlulkAlex commented Aug 10, 2018

Also
after exploring Affinity Name Examples
I'm not sure
that use of NUMERIC(10,2) ( instead of DECIMAL(10,2) )
in the column type definition
for tables in Chinook_Sqlite db
was entirely appropriate .
( Nevertheless existing pattern matching fails the same way on DECIMAL(10,2) )

GlulkAlex added a commit to GlulkAlex/slick that referenced this issue Aug 11, 2018
 . Fixes slick#1939

* Add new table defenition with Decimal_Types to sqlite.sql
* Add new asserts for DecimalTypes.baseTableRow in GeneratedCodeTest.scala
* Add extract_Type_Props def to ModelBuilder in SQLiteProfile.scala
octonato added a commit that referenced this issue Jan 30, 2019
…r-Regex-bug

Replacing RegEx pattern matching. Fixes #1939
@hvesalai hvesalai added this to the 3.3 milestone Jan 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
0