Skip to content
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

BUG: query does not work when the SRKObject definition contains an enum field. #138

Open
silencer07 opened this issue Jan 28, 2021 · 3 comments

Comments

@silencer07
Copy link

silencer07 commented Jan 28, 2021

Context: I am trying to use this framework in react-native. essentially I will have to create a bridging header and add the sharkORM header there. it is running good and was able to insert to the database. But I have a weird problem

//LCLog.m
#import <SharkORM/SharkORM.h>

//LCTimeScale.swift
@objc
enum LCTimeScale: Int, CaseIterable {
  case SECOND = 0
  case MINUTE = 1
  case HOUR = 2
  case FOUR_HOURS = 3
  case BREACHED_ONLY = 4
}

// LCLog.swift
@objc
class LCLog: SRKObject {
  
  @objc(deviceId)
  dynamic var deviceId: String = ""
  
 // ... ommitted code
  
  @objc(timeScale)
  dynamic var timeScale = LCTimeScale.SECOND
  
  // tried the syntax below, same result
  // .where(withFormat: "deviceId = %@ AND timeScale = %@", withParameters: [deviceId, timeScale])
  static func countByDeviceIdAndTimeScale(deviceId: String, timeScale: LCTimeScale) -> UInt64 {
    let count = query()
      .where("deviceId = ? AND timeScale = ?", parameters: [deviceId, timeScale])
      .count()
    return count
  }
}

then tried using the countBy function

print("SECONDS: \(LCLog.countByDeviceIdAndTimeScale(deviceId: deviceId, timeScale: LCTimeScale.SECOND))")
print("MINUTE: \(LCLog.countByDeviceIdAndTimeScale(deviceId: deviceId, timeScale: LCTimeScale.MINUTE))")
print("HOUR: \(LCLog.countByDeviceIdAndTimeScale(deviceId: deviceId, timeScale: LCTimeScale.HOUR))")
print("FOUR_HOURS: \(LCLog.countByDeviceIdAndTimeScale(deviceId: deviceId, timeScale: LCTimeScale.FOUR_HOURS))")

output(1000 is the total record)

SECONDS: 1000
MINUTE: 1000
HOUR: 1000
FOUR_HOURS: 1000

seems to me that this is not being included in the querytimeScale = ?

@silencer07 silencer07 changed the title where does not consider the second parameter when querying BUG: where does not consider the second parameter when querying Jan 28, 2021
@editfmah
Copy link
Owner

Hi, if you pass in 'LCTimeScale.SECOND.rawValue'. I would expect that to work. The OBJC code will essentially accept Any?, but will not be able to support the typed enum because it can't inspect the object as it's out of the module scope.

@silencer07
Copy link
Author

silencer07 commented Jan 28, 2021

@editfmah it tried your suggestion

static func countByDeviceIdAndTimeScale(deviceId: String, timeScale: LCTimeScale) -> UInt64 {
    let count = query()
      .where("deviceId = ? AND timeScale = ?", parameters: [deviceId, timeScale.rawValue])
      .count()
    return count
  }

but the result is still wrong

SECONDS: 0
MINUTE: 0
HOUR: 0
FOUR_HOURS: 0

but will not be able to support the typed enum because it can't inspect the object as it's out of the module scope.

Hmmm i kinda find it odd. I make the enum as Int because I will need it to be objc anyway.

@silencer07 silencer07 changed the title BUG: where does not consider the second parameter when querying BUG: where on count does not consider the second parameter when querying. fetch works fine Jan 28, 2021
@silencer07
Copy link
Author

silencer07 commented Jan 28, 2021

@editfmah changing the property to Int seems to work

  @objc(alarmStatus)
  dynamic var alarmStatus: Int = LCAlarmStatus.NOT_SET.rawValue
  
  @objc(timeScale)
  dynamic var timeScale: Int = LCTimeScale.SECOND.rawValue
SECONDS: 979
MINUTE: 19
HOUR: 1
FOUR_HOURS: 1

Seems that we can conclude that where query for table with enum fields does not work

For now I have a workaround. But I suppose this is still a bug.

Thanks!

@silencer07 silencer07 changed the title BUG: where on count does not consider the second parameter when querying. fetch works fine BUG: count query does not work when the SRKObject definition contains an enum field. fetch however works fine Jan 28, 2021
@silencer07 silencer07 changed the title BUG: count query does not work when the SRKObject definition contains an enum field. fetch however works fine BUG: query does not work when the SRKObject definition contains an enum field. Jan 28, 2021
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

2 participants