Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Simon Binder
    @simolus3
    @khotkov Can you describe what what doesn't work as expected? You can also enable logStatements: true on the VmDatabase to make moor print the generated SQL, does that look like something you'd expect?
    khotkov
    @khotkov
    @simolus3 I'll try to debug it with it, thanks
    khotkov
    @khotkov
    Question is how I can group by date (without time), when date is DateTime type
    I'm trying to addColumn formattedDate with that expression
    and then groupBy that formattedDate
    @simolus3 is it right solution for that problem?
    khotkov
    @khotkov
    When I print out values after passing it to that expression - it doesnt cut out time
    log: date(operations.date) AS "c15" FROM operations ORDER BY date(operations.date)
    Simon Binder
    @simolus3
    You could try using the date extension that might work better?
    khotkov
    @khotkov

    Expression<String> getDate(Expression<DateTime> datetime) {
    return FunctionCallExpression('date', [datetime]);
    }

    Like that?

    I'm using it, but for some reason it doesn't work - output still contains time
    khotkov
    @khotkov
    I apologize for disturbing, I think I should solve this problem myself
    @simolus3 thanks for awesome lib
    GP4cK
    @GP4cK
    Hello!
    I'm trying to upgrade to moor v4 but when I do so, I get:
    Because every version of encrypted_moor from git depends on moor ^3.0.0 and [your_project] depends on moor ^4.1.0, encrypted_moor from git is forbidden.
    However, when I go here, I see that it's depending on moor v4... So I don't understand why I get the error. I tried running flutter clean + flutter pub get but same thing.
    Also, I see that there is now also an encrypted version of moor/ffi. What's the difference?
    Thanks a lot!
    Simon Binder
    @simolus3

    @GP4cK It sounds like you may have to update your ref on your encrypted_moor git dependency? If you have a hardcoded commit or tag that might not work as expected.

    Also, I see that there is now also an encrypted version of moor/ffi. What's the difference?

    Basically, it lets you use the regular moor/ffi library with libsqlcipher (instead of libsqlite3) underneath. It's a relationship similar to moor_flutter vs. moor/ffi (one of them uses platform channels, one of them uses dart:ffi to call the native library directly.

    GP4cK
    @GP4cK

    @simolus3 from my pubspec I have this:

    dependencies:
      moor: ^3.3.1
      encrypted_moor:
        git:
          url: https://github.com/simolus3/moor.git
          path: extras/encryption
    dependency_overrides:
      # See install instructions of https://pub.dev/packages/sqflite_sqlcipher
      sqflite:
        git:
          url: https://www.github.com/davidmartos96/sqflite_sqlcipher.git
          path: sqflite
          ref: fmdb_override

    So no reference to any commit... But ok I'll investigate a bit more

    So is it right to say that moor/ffi should be faster than moor_flutter since it calls the native library?
    I noticed that my writes are significantly slower after I switched to the encrypted version
    Simon Binder
    @simolus3
    I think the writes being slower is natural, since there's an extra encryption step happening. It might get better by bundling them in transactions or batches if that's possible. The git problem might be happening because pub doesn't fetch the repository on every pub get. Can you try deleting the encrypted_moor folders in ~/.pub-cache/git?
    In general moor/ffi should be faster, but it might block the main thread since sqlite3 and sqlcipher apis are all synchronous. You can use moor isolates to work around that.
    GP4cK
    @GP4cK
    Cool. Thank you so much!
    GP4cK
    @GP4cK
    @simolus3 sorry one other question:
    What about an option to generate toJson() methods on the companion classes?
    My use case is that I use the companions to insert / update records in the db. After each insert/update, I also JSON stringify the companion and store the resulting string as a row in an outbox. After this, I try to send these changes to my server and if I can't reach it, I will try again 5 minutes later.
    As of today, I simply declare an extension on all the companion class to add this toJson() method but it would be super nice if moor could do it for me :)
    Simon Binder
    @simolus3
    Inside your db, you can use yourTable.mapFromCompanion(yourCompanion) to turn a companion (with all values set) back to a data class.
    GP4cK
    @GP4cK
    Ok so that works if you have all the values set (good for an insert) but not if you have them partially (update)
    Simon Binder
    @simolus3
    Right. You could use toColumns on a companion(which gives you a Map<String, Expression> and then cast values to a Variable from which you could extract the right value. The keys in that map reflect the column name though, not the json key from data classes
    GP4cK
    @GP4cK
    Yeah.. I think I may as well declare the extension toJson ^^
    Michael Joseph Rosenthal
    @micimize

    Hi – I was wondering if it is possible to do non-literal customConstraints – i.e:

      @override
      List<String> get customConstraints => [
            'FOREIGN KEY(${table}_entity_id, ${table}_valid_from) REFERENCES ${table}(entity_id, valid_from)',
          ];

    Or perhaps even

      @override
      List<String> get customConstraints => [
            ...constraintsFromMixinA,
            ...constraintsFromMixinB,
          ];
    Michael Joseph Rosenthal
    @micimize
    nevermind I figured out how to log sql :thumbsup:
    Michael Joseph Rosenthal
    @micimize
    Hi – I'm trying to figure out how to insert a record, then return the row dataclass with the populated defaults, etc. Is there a way to do this using the rowid? Even if I didn't define a rowid on my table?
    David Martos
    @davidmartos96
    @micimize I think you may be interested in the following issue simolus3/moor#1096
    Michael Joseph Rosenthal
    @micimize

    @davidmartos96 thanks – knowing returning * is on the roadmap is great news.

    I guess I can probably just use

    final rowId = await _db.into(todos).insert(record);
    customSelect(
      'SELECT * FROM todos WHERE rowid = ?',
      variables: [Variable.withInt(rowId)],
      readsFrom: {todos},
    );
    David Martos
    @davidmartos96
    @micimize Yes, you can do that. Another option is to recreate your todos object by with the id returned by the database. That won't query the database again. But it really depends on the situation, Sqlite is really fast :)
    William
    @monolidth
    Hey, I have a short question. Lets say I have the following table: id: Int and a total: Int. I want to replace every total column by -total. How could I achieve this?
    Thanks in advance!
    David Martos
    @davidmartos96
    @monolidth You can do an update without a Where clause, that will match all rows in the table. Something like this: UPDATE myTable SET total = -total
    Simon Binder
    @simolus3
    If you want to do that in Dart, something like this should work: await update(yourTable).write(YourTableCompanion.custom(total: -yourTable.total))
    William
    @monolidth
    Thank you @davidmartos96 @simolus3 :)
    Nattapon Pondongnok
    @naijab

    I want to watch sum of record. I have issue stream builder not update when record has changed

    Screen Shot 2564-03-31 at 22.13.47.png

    Simon Binder
    @simolus3
    If you use a customSelect, you need to use readsFrom so that moor knows which tables are involved in the query.
    sergeylappo
    @sergeylappo

    Hello, @simolus3 .
    I'm not sure, probably it's late, my code is broken, but for me those two functions seems to be controversal:

    @override
      Uint8List? mapFromDatabaseResponse(dynamic response) {
        return response as String?;
      }
    
      @override
      String mapToSqlConstant(Uint8List? content) {
        if (content == null) return 'NULL';
        // BLOB literals are string literals containing hexadecimal data and
        // preceded by a single "x" or "X" character. Example: X'53514C697465'
        return "x'${hex.encode(content)}'";
      }

    When I put a 16 uint8 values to db I get back 32.
    Why do we use mapping to put to db and do not to get back?
    Sorry if I missed something, didn't have a chance to look deeper and try on a clean project

    Simon Binder
    @simolus3
    The current moor version has response as Uint8List? in mapFromDatabaseResponse, I'm not sure where the String? comes from? Have you tried upgrading moor?
    William
    @monolidth
    Hallo @simolus3, I have a short question again. Lets say I have the following table: id: Int and a tag: String. I want to replace every tag column by tag + const. suffix. How could I achieve this?. Thanks in advance!
    Simon Binder
    @simolus3
    @monolidth You can use YourCompanion.custom(tag: tag + Constant('your suffix')) in an update statement without where.
    William
    @monolidth
    Vielen Dank für die schnelle Antwort. Ich hatte das const vergessen. Dir noch einen schönen Tag :)
    sergeylappo
    @sergeylappo

    Uint8List? in mapFromDatabaseResponse, I'm not sure where the String? comes from? Have you tried upgrading moor?

    Sorry, this was an accidental edit, it even won’t compile with this code. But the question is still valid after reverting this

    Simon Binder
    @simolus3
    @sergeylappo Can you share more on how to reproduce this? You you just have a regular table where you put 16 bytes but get back 32? That sounds like a bug in moor, but we have tests for that so I'd be surprised if it was a general problem.
    sergeylappo
    @sergeylappo
    @simolus3 would try to create a sample app for that.
    basically yes, i have blobs, where I write uuids.
    Furthermore, datagrip shows that its representation in database is correct
    sergeylappo
    @sergeylappo
    Sorry for the inconvenience
    Ended up finding out that I was incorrectly converting uuid to bytes.
    Strangely enough, data grip was showing uuid which seemed to be correct (and was perfectly valid).
    David Martos
    @davidmartos96
    @simolus3 While migrating to null safety I've just found out that for one of my tables in the moor file I did INTEGER PRIMARY KEY. That generates a nullable id in the moor DataClass. Shouldn't it be non null? Integer primary keys get assigned an id automatically when inserted with null, but for that we have the companions, or not?
    Also, INTEGER PRIMARY KEY Is an alias for ROWID so maybe it should be treated as non null for that reason as well.